blob: 2fb4ab659a0ff72904175a82cc6964d37e0db13f [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 Masonb9447ef2009-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 Schmidt5d9e75c42012-05-16 18:25:47 +02001428
Jan Schmidt30b04632013-04-13 13:19:54 +00001429 eb_root = btrfs_read_lock_root_node(root);
1430 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001431 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001432 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001433
Jan Schmidta95236d2012-06-05 16:41:24 +02001434 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1435 old_root = &tm->old_root;
1436 old_generation = tm->generation;
1437 logical = old_root->logical;
1438 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001439 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001440 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001441
Jan Schmidta95236d2012-06-05 16:41:24 +02001442 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001443 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001444 btrfs_tree_read_unlock(eb_root);
1445 free_extent_buffer(eb_root);
David Sterbace86cd52014-06-15 01:07:32 +02001446 old = read_tree_block(root, logical, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301447 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001448 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001449 btrfs_warn(root->fs_info,
1450 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001451 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001452 eb = btrfs_clone_extent_buffer(old);
1453 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001454 }
1455 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001456 btrfs_tree_read_unlock(eb_root);
1457 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001458 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001459 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001460 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001461 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001462 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001463 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001464 }
1465
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001466 if (!eb)
1467 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001468 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001469 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001470 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001471 btrfs_set_header_bytenr(eb, eb->start);
1472 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001473 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001474 btrfs_set_header_level(eb, old_root->level);
1475 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001476 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001477 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001478 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001479 else
1480 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001481 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001482
1483 return eb;
1484}
1485
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001486int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1487{
1488 struct tree_mod_elem *tm;
1489 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001490 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001491
Jan Schmidt30b04632013-04-13 13:19:54 +00001492 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001493 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1494 level = tm->old_root.level;
1495 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001496 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001497 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001498 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001499
1500 return level;
1501}
1502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503static inline int should_cow_block(struct btrfs_trans_handle *trans,
1504 struct btrfs_root *root,
1505 struct extent_buffer *buf)
1506{
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001507#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1508 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
1509 return 0;
1510#endif
Liu Bof1ebcc72011-11-14 20:48:06 -05001511 /* ensure we can see the force_cow */
1512 smp_rmb();
1513
1514 /*
1515 * We do not need to cow a block if
1516 * 1) this block is not created or changed in this transaction;
1517 * 2) this block does not belong to TREE_RELOC tree;
1518 * 3) the root is not forced COW.
1519 *
1520 * What is forced COW:
1521 * when we create snapshot during commiting the transaction,
1522 * after we've finished coping src root, we must COW the shared
1523 * block to ensure the metadata consistency.
1524 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001525 if (btrfs_header_generation(buf) == trans->transid &&
1526 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1527 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001528 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001529 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530 return 0;
1531 return 1;
1532}
1533
Chris Masond352ac62008-09-29 15:18:18 -04001534/*
1535 * cows a single block, see __btrfs_cow_block for the real work.
1536 * This version of it has extra checks so that a block isn't cow'd more than
1537 * once per transaction, as long as it hasn't been written yet
1538 */
Chris Masond3977122009-01-05 21:25:51 -05001539noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001540 struct btrfs_root *root, struct extent_buffer *buf,
1541 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001542 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001543{
Chris Mason6702ed42007-08-07 16:15:09 -04001544 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001545 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001546
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001547 if (trans->transaction != root->fs_info->running_transaction)
1548 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001549 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001550 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001551
1552 if (trans->transid != root->fs_info->generation)
1553 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001554 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001556 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001557 *cow_ret = buf;
1558 return 0;
1559 }
Chris Masonc4876852009-02-04 09:24:25 -05001560
Chris Mason0b86a832008-03-24 15:01:56 -04001561 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001562
1563 if (parent)
1564 btrfs_set_lock_blocking(parent);
1565 btrfs_set_lock_blocking(buf);
1566
Chris Masonf510cfe2007-10-15 16:14:48 -04001567 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001568 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001569
1570 trace_btrfs_cow_block(root, buf, *cow_ret);
1571
Chris Masonf510cfe2007-10-15 16:14:48 -04001572 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
1576 * helper function for defrag to decide if two blocks pointed to by a
1577 * node are actually close by
1578 */
Chris Mason6b800532007-10-15 16:17:34 -04001579static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001580{
Chris Mason6b800532007-10-15 16:17:34 -04001581 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001582 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001583 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001584 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001585 return 0;
1586}
1587
Chris Mason081e9572007-11-06 10:26:24 -05001588/*
1589 * compare two keys in a memcmp fashion
1590 */
1591static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1592{
1593 struct btrfs_key k1;
1594
1595 btrfs_disk_key_to_cpu(&k1, disk);
1596
Diego Calleja20736ab2009-07-24 11:06:52 -04001597 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001598}
1599
Josef Bacikf3465ca2008-11-12 14:19:50 -05001600/*
1601 * same as comp_keys only with two btrfs_key's
1602 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001603int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001604{
1605 if (k1->objectid > k2->objectid)
1606 return 1;
1607 if (k1->objectid < k2->objectid)
1608 return -1;
1609 if (k1->type > k2->type)
1610 return 1;
1611 if (k1->type < k2->type)
1612 return -1;
1613 if (k1->offset > k2->offset)
1614 return 1;
1615 if (k1->offset < k2->offset)
1616 return -1;
1617 return 0;
1618}
Chris Mason081e9572007-11-06 10:26:24 -05001619
Chris Masond352ac62008-09-29 15:18:18 -04001620/*
1621 * this is used by the defrag code to go through all the
1622 * leaves pointed to by a node and reallocate them so that
1623 * disk order is close to key order
1624 */
Chris Mason6702ed42007-08-07 16:15:09 -04001625int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001626 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001627 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001628 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001629{
Chris Mason6b800532007-10-15 16:17:34 -04001630 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001631 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001632 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001633 u64 search_start = *last_ret;
1634 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001635 u64 other;
1636 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001637 int end_slot;
1638 int i;
1639 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001640 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001641 int uptodate;
1642 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001643 int progress_passed = 0;
1644 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001645
Chris Mason5708b952007-10-25 15:43:18 -04001646 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001647
Julia Lawall6c1500f2012-11-03 20:30:18 +00001648 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1649 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001650
Chris Mason6b800532007-10-15 16:17:34 -04001651 parent_nritems = btrfs_header_nritems(parent);
David Sterba707e8a02014-06-04 19:22:26 +02001652 blocksize = root->nodesize;
Chris Mason6702ed42007-08-07 16:15:09 -04001653 end_slot = parent_nritems;
1654
1655 if (parent_nritems == 1)
1656 return 0;
1657
Chris Masonb4ce94d2009-02-04 09:25:08 -05001658 btrfs_set_lock_blocking(parent);
1659
Chris Mason6702ed42007-08-07 16:15:09 -04001660 for (i = start_slot; i < end_slot; i++) {
1661 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001662
Chris Mason081e9572007-11-06 10:26:24 -05001663 btrfs_node_key(parent, &disk_key, i);
1664 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1665 continue;
1666
1667 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001668 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001669 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001670 if (last_block == 0)
1671 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001672
Chris Mason6702ed42007-08-07 16:15:09 -04001673 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001674 other = btrfs_node_blockptr(parent, i - 1);
1675 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001676 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001677 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001678 other = btrfs_node_blockptr(parent, i + 1);
1679 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001680 }
Chris Masone9d0b132007-08-10 14:06:19 -04001681 if (close) {
1682 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001683 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001684 }
Chris Mason6702ed42007-08-07 16:15:09 -04001685
David Sterba0308af42014-06-15 01:43:40 +02001686 cur = btrfs_find_tree_block(root, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001687 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001688 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001689 else
1690 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001691 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001692 if (!cur) {
David Sterbace86cd52014-06-15 01:07:32 +02001693 cur = read_tree_block(root, blocknr, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001694 if (!cur || !extent_buffer_uptodate(cur)) {
1695 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001696 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001697 }
Chris Mason6b800532007-10-15 16:17:34 -04001698 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001699 err = btrfs_read_buffer(cur, gen);
1700 if (err) {
1701 free_extent_buffer(cur);
1702 return err;
1703 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001704 }
Chris Mason6702ed42007-08-07 16:15:09 -04001705 }
Chris Masone9d0b132007-08-10 14:06:19 -04001706 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001707 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001708
Chris Masone7a84562008-06-25 16:01:31 -04001709 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001710 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001711 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001712 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001713 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001714 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001715 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001716 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001717 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001718 break;
Yan252c38f2007-08-29 09:11:44 -04001719 }
Chris Masone7a84562008-06-25 16:01:31 -04001720 search_start = cur->start;
1721 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001722 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001723 btrfs_tree_unlock(cur);
1724 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001725 }
1726 return err;
1727}
1728
Chris Mason74123bd2007-02-02 11:05:29 -05001729/*
1730 * The leaf data grows from end-to-front in the node.
1731 * this returns the address of the start of the last item,
1732 * which is the stop of the leaf data stack
1733 */
Chris Mason123abc82007-03-14 14:14:43 -04001734static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001735 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736{
Chris Mason5f39d392007-10-15 16:14:19 -04001737 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001738 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001739 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001740 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001741}
1742
Chris Masonaa5d6be2007-02-28 16:35:06 -05001743
Chris Mason74123bd2007-02-02 11:05:29 -05001744/*
Chris Mason5f39d392007-10-15 16:14:19 -04001745 * search for key in the extent_buffer. The items start at offset p,
1746 * and they are item_size apart. There are 'max' items in p.
1747 *
Chris Mason74123bd2007-02-02 11:05:29 -05001748 * the slot in the array is returned via slot, and it points to
1749 * the place where you would insert key if it is not found in
1750 * the array.
1751 *
1752 * slot may point to max if the key is bigger than all of the keys
1753 */
Chris Masone02119d2008-09-05 16:13:11 -04001754static noinline int generic_bin_search(struct extent_buffer *eb,
1755 unsigned long p,
1756 int item_size, struct btrfs_key *key,
1757 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758{
1759 int low = 0;
1760 int high = max;
1761 int mid;
1762 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001763 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001764 struct btrfs_disk_key unaligned;
1765 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 char *kaddr = NULL;
1767 unsigned long map_start = 0;
1768 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001769 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001770
Chris Masond3977122009-01-05 21:25:51 -05001771 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001772 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001773 offset = p + mid * item_size;
1774
Chris Masona6591712011-07-19 12:04:14 -04001775 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001776 (offset + sizeof(struct btrfs_disk_key)) >
1777 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001778
1779 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001780 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001781 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001782
Chris Mason479965d2007-10-15 16:14:27 -04001783 if (!err) {
1784 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1785 map_start);
1786 } else {
1787 read_extent_buffer(eb, &unaligned,
1788 offset, sizeof(unaligned));
1789 tmp = &unaligned;
1790 }
1791
Chris Mason5f39d392007-10-15 16:14:19 -04001792 } else {
1793 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1794 map_start);
1795 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796 ret = comp_keys(tmp, key);
1797
1798 if (ret < 0)
1799 low = mid + 1;
1800 else if (ret > 0)
1801 high = mid;
1802 else {
1803 *slot = mid;
1804 return 0;
1805 }
1806 }
1807 *slot = low;
1808 return 1;
1809}
1810
Chris Mason97571fd2007-02-24 13:39:08 -05001811/*
1812 * simple bin_search frontend that does the right thing for
1813 * leaves vs nodes
1814 */
Chris Mason5f39d392007-10-15 16:14:19 -04001815static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1816 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001817{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001818 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001819 return generic_bin_search(eb,
1820 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001821 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001822 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001823 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001824 else
Chris Mason5f39d392007-10-15 16:14:19 -04001825 return generic_bin_search(eb,
1826 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001827 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001828 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001829 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001830}
1831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001832int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1833 int level, int *slot)
1834{
1835 return bin_search(eb, key, level, slot);
1836}
1837
Yan, Zhengf0486c62010-05-16 10:46:25 -04001838static void root_add_used(struct btrfs_root *root, u32 size)
1839{
1840 spin_lock(&root->accounting_lock);
1841 btrfs_set_root_used(&root->root_item,
1842 btrfs_root_used(&root->root_item) + size);
1843 spin_unlock(&root->accounting_lock);
1844}
1845
1846static void root_sub_used(struct btrfs_root *root, u32 size)
1847{
1848 spin_lock(&root->accounting_lock);
1849 btrfs_set_root_used(&root->root_item,
1850 btrfs_root_used(&root->root_item) - size);
1851 spin_unlock(&root->accounting_lock);
1852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/* given a node and slot number, this reads the blocks it points to. The
1855 * extent buffer is returned with a reference taken (but unlocked).
1856 * NULL is returned on error.
1857 */
Chris Masone02119d2008-09-05 16:13:11 -04001858static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001859 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001860{
Chris Masonca7a79a2008-05-12 12:59:19 -04001861 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001862 struct extent_buffer *eb;
1863
Chris Masonbb803952007-03-01 12:04:21 -05001864 if (slot < 0)
1865 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001866 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001867 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001868
1869 BUG_ON(level == 0);
1870
Josef Bacik416bc652013-04-23 14:17:42 -04001871 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001872 btrfs_node_ptr_generation(parent, slot));
1873 if (eb && !extent_buffer_uptodate(eb)) {
1874 free_extent_buffer(eb);
1875 eb = NULL;
1876 }
1877
1878 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001879}
1880
Chris Masond352ac62008-09-29 15:18:18 -04001881/*
1882 * node level balancing, used to make sure nodes are in proper order for
1883 * item deletion. We balance from the top down, so we have to make sure
1884 * that a deletion won't leave an node completely empty later on.
1885 */
Chris Masone02119d2008-09-05 16:13:11 -04001886static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001887 struct btrfs_root *root,
1888 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001889{
Chris Mason5f39d392007-10-15 16:14:19 -04001890 struct extent_buffer *right = NULL;
1891 struct extent_buffer *mid;
1892 struct extent_buffer *left = NULL;
1893 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001894 int ret = 0;
1895 int wret;
1896 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001897 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001898 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001899
1900 if (level == 0)
1901 return 0;
1902
Chris Mason5f39d392007-10-15 16:14:19 -04001903 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001904
Chris Masonbd681512011-07-16 15:23:14 -04001905 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1906 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001907 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1908
Chris Mason1d4f8a02007-03-13 09:28:32 -04001909 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001910
Li Zefana05a9bb2011-09-06 16:55:34 +08001911 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001912 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001913 pslot = path->slots[level + 1];
1914 }
Chris Masonbb803952007-03-01 12:04:21 -05001915
Chris Mason40689472007-03-17 14:29:23 -04001916 /*
1917 * deal with the case where there is only one pointer in the root
1918 * by promoting the node below to a root
1919 */
Chris Mason5f39d392007-10-15 16:14:19 -04001920 if (!parent) {
1921 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001922
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001924 return 0;
1925
1926 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001927 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001928 if (!child) {
1929 ret = -EROFS;
1930 btrfs_std_error(root->fs_info, ret);
1931 goto enospc;
1932 }
1933
Chris Mason925baed2008-06-25 16:01:30 -04001934 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001935 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001936 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001937 if (ret) {
1938 btrfs_tree_unlock(child);
1939 free_extent_buffer(child);
1940 goto enospc;
1941 }
Yan2f375ab2008-02-01 14:58:07 -05001942
Jan Schmidt90f8d622013-04-13 13:19:53 +00001943 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001944 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001945
Chris Mason0b86a832008-03-24 15:01:56 -04001946 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001947 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001948
Chris Mason925baed2008-06-25 16:01:30 -04001949 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001950 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001951 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001952 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001953 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001954 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001955
1956 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001957 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001958 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001959 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001960 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001961 }
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001963 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001964 return 0;
1965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 left = read_node_slot(root, parent, pslot - 1);
1967 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001968 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001969 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001971 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001972 if (wret) {
1973 ret = wret;
1974 goto enospc;
1975 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001976 }
Chris Mason5f39d392007-10-15 16:14:19 -04001977 right = read_node_slot(root, parent, pslot + 1);
1978 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001979 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001980 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001981 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001982 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001983 if (wret) {
1984 ret = wret;
1985 goto enospc;
1986 }
1987 }
1988
1989 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001990 if (left) {
1991 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001992 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001993 if (wret < 0)
1994 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001995 }
Chris Mason79f95c82007-03-01 15:16:26 -05001996
1997 /*
1998 * then try to empty the right most buffer into the middle
1999 */
Chris Mason5f39d392007-10-15 16:14:19 -04002000 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04002001 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002002 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05002003 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002004 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002005 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002006 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002007 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002008 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002009 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002010 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002011 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002012 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002013 struct btrfs_disk_key right_key;
2014 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002015 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002016 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002017 btrfs_set_node_key(parent, &right_key, pslot + 1);
2018 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002019 }
2020 }
Chris Mason5f39d392007-10-15 16:14:19 -04002021 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002022 /*
2023 * we're not allowed to leave a node with one item in the
2024 * tree during a delete. A deletion from lower in the tree
2025 * could try to delete the only pointer in this node.
2026 * So, pull some keys from the left.
2027 * There has to be a left pointer at this point because
2028 * otherwise we would have pulled some pointers from the
2029 * right
2030 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002031 if (!left) {
2032 ret = -EROFS;
2033 btrfs_std_error(root->fs_info, ret);
2034 goto enospc;
2035 }
Chris Mason5f39d392007-10-15 16:14:19 -04002036 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002037 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002038 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002039 goto enospc;
2040 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002041 if (wret == 1) {
2042 wret = push_node_left(trans, root, left, mid, 1);
2043 if (wret < 0)
2044 ret = wret;
2045 }
Chris Mason79f95c82007-03-01 15:16:26 -05002046 BUG_ON(wret == 1);
2047 }
Chris Mason5f39d392007-10-15 16:14:19 -04002048 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002049 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002050 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002051 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002052 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002053 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002054 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002055 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002056 } else {
2057 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002058 struct btrfs_disk_key mid_key;
2059 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002060 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002061 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002062 btrfs_set_node_key(parent, &mid_key, pslot);
2063 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002064 }
Chris Masonbb803952007-03-01 12:04:21 -05002065
Chris Mason79f95c82007-03-01 15:16:26 -05002066 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002067 if (left) {
2068 if (btrfs_header_nritems(left) > orig_slot) {
2069 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002070 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002071 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002072 path->slots[level + 1] -= 1;
2073 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002074 if (mid) {
2075 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002076 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002077 }
Chris Masonbb803952007-03-01 12:04:21 -05002078 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002079 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002080 path->slots[level] = orig_slot;
2081 }
2082 }
Chris Mason79f95c82007-03-01 15:16:26 -05002083 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002084 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002086 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002087enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002088 if (right) {
2089 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002091 }
2092 if (left) {
2093 if (path->nodes[level] != left)
2094 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002095 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002096 }
Chris Masonbb803952007-03-01 12:04:21 -05002097 return ret;
2098}
2099
Chris Masond352ac62008-09-29 15:18:18 -04002100/* Node balancing for insertion. Here we only split or push nodes around
2101 * when they are completely full. This is also done top down, so we
2102 * have to be pessimistic.
2103 */
Chris Masond3977122009-01-05 21:25:51 -05002104static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002105 struct btrfs_root *root,
2106 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002107{
Chris Mason5f39d392007-10-15 16:14:19 -04002108 struct extent_buffer *right = NULL;
2109 struct extent_buffer *mid;
2110 struct extent_buffer *left = NULL;
2111 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002112 int ret = 0;
2113 int wret;
2114 int pslot;
2115 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002116
2117 if (level == 0)
2118 return 1;
2119
Chris Mason5f39d392007-10-15 16:14:19 -04002120 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002121 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002122
Li Zefana05a9bb2011-09-06 16:55:34 +08002123 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002124 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002125 pslot = path->slots[level + 1];
2126 }
Chris Masone66f7092007-04-20 13:16:02 -04002127
Chris Mason5f39d392007-10-15 16:14:19 -04002128 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002129 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002130
Chris Mason5f39d392007-10-15 16:14:19 -04002131 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002132
2133 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002134 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002135 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002136
2137 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002138 btrfs_set_lock_blocking(left);
2139
Chris Mason5f39d392007-10-15 16:14:19 -04002140 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002141 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2142 wret = 1;
2143 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002144 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002145 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002146 if (ret)
2147 wret = 1;
2148 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002149 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002150 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002151 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002152 }
Chris Masone66f7092007-04-20 13:16:02 -04002153 if (wret < 0)
2154 ret = wret;
2155 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002156 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002157 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002158 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002159 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002160 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 btrfs_set_node_key(parent, &disk_key, pslot);
2162 btrfs_mark_buffer_dirty(parent);
2163 if (btrfs_header_nritems(left) > orig_slot) {
2164 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002165 path->slots[level + 1] -= 1;
2166 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002167 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002169 } else {
2170 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002171 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002172 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002173 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002175 }
Chris Masone66f7092007-04-20 13:16:02 -04002176 return 0;
2177 }
Chris Mason925baed2008-06-25 16:01:30 -04002178 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002179 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002180 }
Chris Mason925baed2008-06-25 16:01:30 -04002181 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002182
2183 /*
2184 * then try to empty the right most buffer into the middle
2185 */
Chris Mason5f39d392007-10-15 16:14:19 -04002186 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002187 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002188
Chris Mason925baed2008-06-25 16:01:30 -04002189 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002190 btrfs_set_lock_blocking(right);
2191
Chris Mason5f39d392007-10-15 16:14:19 -04002192 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002193 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2194 wret = 1;
2195 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002196 ret = btrfs_cow_block(trans, root, right,
2197 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002198 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002199 if (ret)
2200 wret = 1;
2201 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002202 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002203 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002205 }
Chris Masone66f7092007-04-20 13:16:02 -04002206 if (wret < 0)
2207 ret = wret;
2208 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002209 struct btrfs_disk_key disk_key;
2210
2211 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002212 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002213 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2215 btrfs_mark_buffer_dirty(parent);
2216
2217 if (btrfs_header_nritems(mid) <= orig_slot) {
2218 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002219 path->slots[level + 1] += 1;
2220 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002221 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002222 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002223 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002224 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002225 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002226 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002227 }
Chris Masone66f7092007-04-20 13:16:02 -04002228 return 0;
2229 }
Chris Mason925baed2008-06-25 16:01:30 -04002230 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002231 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002232 }
Chris Masone66f7092007-04-20 13:16:02 -04002233 return 1;
2234}
2235
Chris Mason74123bd2007-02-02 11:05:29 -05002236/*
Chris Masond352ac62008-09-29 15:18:18 -04002237 * readahead one full node of leaves, finding things that are close
2238 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002239 */
Chris Masonc8c42862009-04-03 10:14:18 -04002240static void reada_for_search(struct btrfs_root *root,
2241 struct btrfs_path *path,
2242 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002243{
Chris Mason5f39d392007-10-15 16:14:19 -04002244 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002245 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002246 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002247 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002248 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002249 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002250 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002251 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002252 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002253 u32 nr;
2254 u32 blocksize;
2255 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002256
Chris Masona6b6e752007-10-15 16:22:39 -04002257 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002258 return;
2259
Chris Mason6702ed42007-08-07 16:15:09 -04002260 if (!path->nodes[level])
2261 return;
2262
Chris Mason5f39d392007-10-15 16:14:19 -04002263 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002264
Chris Mason3c69fae2007-08-07 15:52:22 -04002265 search = btrfs_node_blockptr(node, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002266 blocksize = root->nodesize;
David Sterba0308af42014-06-15 01:43:40 +02002267 eb = btrfs_find_tree_block(root, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002268 if (eb) {
2269 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002270 return;
2271 }
2272
Chris Masona7175312009-01-22 09:23:10 -05002273 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002274
Chris Mason5f39d392007-10-15 16:14:19 -04002275 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002276 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002277
Chris Masond3977122009-01-05 21:25:51 -05002278 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002279 if (direction < 0) {
2280 if (nr == 0)
2281 break;
2282 nr--;
2283 } else if (direction > 0) {
2284 nr++;
2285 if (nr >= nritems)
2286 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002287 }
Chris Mason01f46652007-12-21 16:24:26 -05002288 if (path->reada < 0 && objectid) {
2289 btrfs_node_key(node, &disk_key, nr);
2290 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2291 break;
2292 }
Chris Mason6b800532007-10-15 16:17:34 -04002293 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002294 if ((search <= target && target - search <= 65536) ||
2295 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002296 gen = btrfs_node_ptr_generation(node, nr);
David Sterba58dc4ce2014-06-15 00:29:04 +02002297 readahead_tree_block(root, search, blocksize);
Chris Mason6b800532007-10-15 16:17:34 -04002298 nread += blocksize;
2299 }
2300 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002301 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002302 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002303 }
2304}
Chris Mason925baed2008-06-25 16:01:30 -04002305
Josef Bacik0b088512013-06-17 14:23:02 -04002306static noinline void reada_for_balance(struct btrfs_root *root,
2307 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308{
2309 int slot;
2310 int nritems;
2311 struct extent_buffer *parent;
2312 struct extent_buffer *eb;
2313 u64 gen;
2314 u64 block1 = 0;
2315 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002316 int blocksize;
2317
Chris Mason8c594ea2009-04-20 15:50:10 -04002318 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002319 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002320 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002321
2322 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002323 slot = path->slots[level + 1];
David Sterba707e8a02014-06-04 19:22:26 +02002324 blocksize = root->nodesize;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002325
2326 if (slot > 0) {
2327 block1 = btrfs_node_blockptr(parent, slot - 1);
2328 gen = btrfs_node_ptr_generation(parent, slot - 1);
David Sterba0308af42014-06-15 01:43:40 +02002329 eb = btrfs_find_tree_block(root, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002330 /*
2331 * if we get -eagain from btrfs_buffer_uptodate, we
2332 * don't want to return eagain here. That will loop
2333 * forever
2334 */
2335 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002336 block1 = 0;
2337 free_extent_buffer(eb);
2338 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002339 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002340 block2 = btrfs_node_blockptr(parent, slot + 1);
2341 gen = btrfs_node_ptr_generation(parent, slot + 1);
David Sterba0308af42014-06-15 01:43:40 +02002342 eb = btrfs_find_tree_block(root, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002343 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002344 block2 = 0;
2345 free_extent_buffer(eb);
2346 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002347
Josef Bacik0b088512013-06-17 14:23:02 -04002348 if (block1)
David Sterba58dc4ce2014-06-15 00:29:04 +02002349 readahead_tree_block(root, block1, blocksize);
Josef Bacik0b088512013-06-17 14:23:02 -04002350 if (block2)
David Sterba58dc4ce2014-06-15 00:29:04 +02002351 readahead_tree_block(root, block2, blocksize);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002352}
2353
2354
2355/*
Chris Masond3977122009-01-05 21:25:51 -05002356 * when we walk down the tree, it is usually safe to unlock the higher layers
2357 * in the tree. The exceptions are when our path goes through slot 0, because
2358 * operations on the tree might require changing key pointers higher up in the
2359 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002360 *
Chris Masond3977122009-01-05 21:25:51 -05002361 * callers might also have set path->keep_locks, which tells this code to keep
2362 * the lock if the path points to the last slot in the block. This is part of
2363 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002364 *
Chris Masond3977122009-01-05 21:25:51 -05002365 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2366 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002367 */
Chris Masone02119d2008-09-05 16:13:11 -04002368static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002369 int lowest_unlock, int min_write_lock_level,
2370 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002371{
2372 int i;
2373 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002374 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002375 struct extent_buffer *t;
2376
2377 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2378 if (!path->nodes[i])
2379 break;
2380 if (!path->locks[i])
2381 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002382 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002383 skip_level = i + 1;
2384 continue;
2385 }
Chris Mason051e1b92008-06-25 16:01:30 -04002386 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002387 u32 nritems;
2388 t = path->nodes[i];
2389 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002390 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002391 skip_level = i + 1;
2392 continue;
2393 }
2394 }
Chris Mason051e1b92008-06-25 16:01:30 -04002395 if (skip_level < i && i >= lowest_unlock)
2396 no_skips = 1;
2397
Chris Mason925baed2008-06-25 16:01:30 -04002398 t = path->nodes[i];
2399 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002400 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002401 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002402 if (write_lock_level &&
2403 i > min_write_lock_level &&
2404 i <= *write_lock_level) {
2405 *write_lock_level = i - 1;
2406 }
Chris Mason925baed2008-06-25 16:01:30 -04002407 }
2408 }
2409}
2410
Chris Mason3c69fae2007-08-07 15:52:22 -04002411/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002412 * This releases any locks held in the path starting at level and
2413 * going all the way up to the root.
2414 *
2415 * btrfs_search_slot will keep the lock held on higher nodes in a few
2416 * corner cases, such as COW of the block at slot zero in the node. This
2417 * ignores those rules, and it should only be called when there are no
2418 * more updates to be done higher up in the tree.
2419 */
2420noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2421{
2422 int i;
2423
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002424 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002425 return;
2426
2427 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2428 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002429 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002430 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002431 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002432 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002433 path->locks[i] = 0;
2434 }
2435}
2436
2437/*
Chris Masonc8c42862009-04-03 10:14:18 -04002438 * helper function for btrfs_search_slot. The goal is to find a block
2439 * in cache without setting the path to blocking. If we find the block
2440 * we return zero and the path is unchanged.
2441 *
2442 * If we can't find the block, we set the path blocking and do some
2443 * reada. -EAGAIN is returned and the search must be repeated.
2444 */
2445static int
2446read_block_for_search(struct btrfs_trans_handle *trans,
2447 struct btrfs_root *root, struct btrfs_path *p,
2448 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002449 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002450{
2451 u64 blocknr;
2452 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002453 struct extent_buffer *b = *eb_ret;
2454 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002455 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002456
2457 blocknr = btrfs_node_blockptr(b, slot);
2458 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002459
David Sterba0308af42014-06-15 01:43:40 +02002460 tmp = btrfs_find_tree_block(root, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002461 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002462 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002463 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2464 *eb_ret = tmp;
2465 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002466 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002467
2468 /* the pages were up to date, but we failed
2469 * the generation number check. Do a full
2470 * read for the generation number that is correct.
2471 * We must do this without dropping locks so
2472 * we can trust our generation number
2473 */
2474 btrfs_set_path_blocking(p);
2475
2476 /* now we're allowed to do a blocking uptodate check */
2477 ret = btrfs_read_buffer(tmp, gen);
2478 if (!ret) {
2479 *eb_ret = tmp;
2480 return 0;
2481 }
2482 free_extent_buffer(tmp);
2483 btrfs_release_path(p);
2484 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002485 }
2486
2487 /*
2488 * reduce lock contention at high levels
2489 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002490 * we read. Don't release the lock on the current
2491 * level because we need to walk this node to figure
2492 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002493 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002494 btrfs_unlock_up_safe(p, level + 1);
2495 btrfs_set_path_blocking(p);
2496
Chris Masoncb449212010-10-24 11:01:27 -04002497 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002498 if (p->reada)
2499 reada_for_search(root, p, level, slot, key->objectid);
2500
David Sterbab3b4aa72011-04-21 01:20:15 +02002501 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002502
2503 ret = -EAGAIN;
David Sterbace86cd52014-06-15 01:07:32 +02002504 tmp = read_tree_block(root, blocknr, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002505 if (tmp) {
2506 /*
2507 * If the read above didn't mark this buffer up to date,
2508 * it will never end up being up to date. Set ret to EIO now
2509 * and give up so that our caller doesn't loop forever
2510 * on our EAGAINs.
2511 */
Chris Masonb9fab912012-05-06 07:23:47 -04002512 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002513 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002514 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002515 }
2516 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002517}
2518
2519/*
2520 * helper function for btrfs_search_slot. This does all of the checks
2521 * for node-level blocks and does any balancing required based on
2522 * the ins_len.
2523 *
2524 * If no extra work was required, zero is returned. If we had to
2525 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2526 * start over
2527 */
2528static int
2529setup_nodes_for_search(struct btrfs_trans_handle *trans,
2530 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002531 struct extent_buffer *b, int level, int ins_len,
2532 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002533{
2534 int ret;
2535 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2536 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2537 int sret;
2538
Chris Masonbd681512011-07-16 15:23:14 -04002539 if (*write_lock_level < level + 1) {
2540 *write_lock_level = level + 1;
2541 btrfs_release_path(p);
2542 goto again;
2543 }
2544
Chris Masonc8c42862009-04-03 10:14:18 -04002545 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002546 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002547 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002548 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002549
2550 BUG_ON(sret > 0);
2551 if (sret) {
2552 ret = sret;
2553 goto done;
2554 }
2555 b = p->nodes[level];
2556 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002557 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002558 int sret;
2559
Chris Masonbd681512011-07-16 15:23:14 -04002560 if (*write_lock_level < level + 1) {
2561 *write_lock_level = level + 1;
2562 btrfs_release_path(p);
2563 goto again;
2564 }
2565
Chris Masonc8c42862009-04-03 10:14:18 -04002566 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002567 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002568 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002569 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002570
2571 if (sret) {
2572 ret = sret;
2573 goto done;
2574 }
2575 b = p->nodes[level];
2576 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002577 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002578 goto again;
2579 }
2580 BUG_ON(btrfs_header_nritems(b) == 1);
2581 }
2582 return 0;
2583
2584again:
2585 ret = -EAGAIN;
2586done:
2587 return ret;
2588}
2589
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002590static void key_search_validate(struct extent_buffer *b,
2591 struct btrfs_key *key,
2592 int level)
2593{
2594#ifdef CONFIG_BTRFS_ASSERT
2595 struct btrfs_disk_key disk_key;
2596
2597 btrfs_cpu_key_to_disk(&disk_key, key);
2598
2599 if (level == 0)
2600 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2601 offsetof(struct btrfs_leaf, items[0].key),
2602 sizeof(disk_key)));
2603 else
2604 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2605 offsetof(struct btrfs_node, ptrs[0].key),
2606 sizeof(disk_key)));
2607#endif
2608}
2609
2610static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2611 int level, int *prev_cmp, int *slot)
2612{
2613 if (*prev_cmp != 0) {
2614 *prev_cmp = bin_search(b, key, level, slot);
2615 return *prev_cmp;
2616 }
2617
2618 key_search_validate(b, key, level);
2619 *slot = 0;
2620
2621 return 0;
2622}
2623
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002624int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002625 u64 iobjectid, u64 ioff, u8 key_type,
2626 struct btrfs_key *found_key)
2627{
2628 int ret;
2629 struct btrfs_key key;
2630 struct extent_buffer *eb;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002631 struct btrfs_path *path;
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002632
2633 key.type = key_type;
2634 key.objectid = iobjectid;
2635 key.offset = ioff;
2636
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002637 if (found_path == NULL) {
2638 path = btrfs_alloc_path();
2639 if (!path)
2640 return -ENOMEM;
2641 } else
2642 path = found_path;
2643
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002644 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002645 if ((ret < 0) || (found_key == NULL)) {
2646 if (path != found_path)
2647 btrfs_free_path(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002648 return ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002649 }
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002650
2651 eb = path->nodes[0];
2652 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2653 ret = btrfs_next_leaf(fs_root, path);
2654 if (ret)
2655 return ret;
2656 eb = path->nodes[0];
2657 }
2658
2659 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2660 if (found_key->type != key.type ||
2661 found_key->objectid != key.objectid)
2662 return 1;
2663
2664 return 0;
2665}
2666
Chris Masonc8c42862009-04-03 10:14:18 -04002667/*
Chris Mason74123bd2007-02-02 11:05:29 -05002668 * look for key in the tree. path is filled in with nodes along the way
2669 * if key is found, we return zero and you can find the item in the leaf
2670 * level of the path (level 0)
2671 *
2672 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002673 * be inserted, and 1 is returned. If there are other errors during the
2674 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002675 *
2676 * if ins_len > 0, nodes and leaves will be split as we walk down the
2677 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2678 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002679 */
Chris Masone089f052007-03-16 16:20:31 -04002680int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2681 *root, struct btrfs_key *key, struct btrfs_path *p, int
2682 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683{
Chris Mason5f39d392007-10-15 16:14:19 -04002684 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002685 int slot;
2686 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002687 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002688 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002689 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002690 int root_lock;
2691 /* everything at write_lock_level or lower must be write locked */
2692 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002693 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002694 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002695 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002696
Chris Mason6702ed42007-08-07 16:15:09 -04002697 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002698 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002699 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002700 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002701
Chris Masonbd681512011-07-16 15:23:14 -04002702 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002703 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002704
Chris Masonbd681512011-07-16 15:23:14 -04002705 /* when we are removing items, we might have to go up to level
2706 * two as we update tree pointers Make sure we keep write
2707 * for those levels as well
2708 */
2709 write_lock_level = 2;
2710 } else if (ins_len > 0) {
2711 /*
2712 * for inserting items, make sure we have a write lock on
2713 * level 1 so we can update keys
2714 */
2715 write_lock_level = 1;
2716 }
2717
2718 if (!cow)
2719 write_lock_level = -1;
2720
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002721 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002722 write_lock_level = BTRFS_MAX_LEVEL;
2723
Chris Masonf7c79f32012-03-19 15:54:38 -04002724 min_write_lock_level = write_lock_level;
2725
Chris Masonbb803952007-03-01 12:04:21 -05002726again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002727 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002728 /*
2729 * we try very hard to do read locks on the root
2730 */
2731 root_lock = BTRFS_READ_LOCK;
2732 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002733 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002734 /*
2735 * the commit roots are read only
2736 * so we always do read locks
2737 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002738 if (p->need_commit_sem)
2739 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002740 b = root->commit_root;
2741 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002742 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002743 if (p->need_commit_sem)
2744 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002745 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002746 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002747 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002748 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002749 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002750 level = btrfs_header_level(b);
2751 } else {
2752 /* we don't know the level of the root node
2753 * until we actually have it read locked
2754 */
2755 b = btrfs_read_lock_root_node(root);
2756 level = btrfs_header_level(b);
2757 if (level <= write_lock_level) {
2758 /* whoops, must trade for write lock */
2759 btrfs_tree_read_unlock(b);
2760 free_extent_buffer(b);
2761 b = btrfs_lock_root_node(root);
2762 root_lock = BTRFS_WRITE_LOCK;
2763
2764 /* the level might have changed, check again */
2765 level = btrfs_header_level(b);
2766 }
2767 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002768 }
Chris Masonbd681512011-07-16 15:23:14 -04002769 p->nodes[level] = b;
2770 if (!p->skip_locking)
2771 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002772
Chris Masoneb60cea2007-02-02 09:18:22 -05002773 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002774 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002775
2776 /*
2777 * setup the path here so we can release it under lock
2778 * contention with the cow code
2779 */
Chris Mason02217ed2007-03-02 16:08:05 -05002780 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002781 /*
2782 * if we don't really need to cow this block
2783 * then we don't want to set the path blocking,
2784 * so we test it here
2785 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002786 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002787 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002788
Chris Masonbd681512011-07-16 15:23:14 -04002789 /*
2790 * must have write locks on this node and the
2791 * parent
2792 */
Josef Bacik5124e002012-11-07 13:44:13 -05002793 if (level > write_lock_level ||
2794 (level + 1 > write_lock_level &&
2795 level + 1 < BTRFS_MAX_LEVEL &&
2796 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002797 write_lock_level = level + 1;
2798 btrfs_release_path(p);
2799 goto again;
2800 }
2801
Filipe Manana160f4082014-07-28 19:37:17 +01002802 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002803 err = btrfs_cow_block(trans, root, b,
2804 p->nodes[level + 1],
2805 p->slots[level + 1], &b);
2806 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002807 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002808 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002809 }
Chris Mason02217ed2007-03-02 16:08:05 -05002810 }
Chris Mason65b51a02008-08-01 15:11:20 -04002811cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002812 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002813 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002814
2815 /*
2816 * we have a lock on b and as long as we aren't changing
2817 * the tree, there is no way to for the items in b to change.
2818 * It is safe to drop the lock on our parent before we
2819 * go through the expensive btree search on b.
2820 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002821 * If we're inserting or deleting (ins_len != 0), then we might
2822 * be changing slot zero, which may require changing the parent.
2823 * So, we can't drop the lock until after we know which slot
2824 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002825 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002826 if (!ins_len && !p->keep_locks) {
2827 int u = level + 1;
2828
2829 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2830 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2831 p->locks[u] = 0;
2832 }
2833 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002834
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002835 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002836
Chris Mason5f39d392007-10-15 16:14:19 -04002837 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002838 int dec = 0;
2839 if (ret && slot > 0) {
2840 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002841 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002842 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002843 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002844 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002845 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002846 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002847 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002848 if (err) {
2849 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002850 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002851 }
Chris Masonc8c42862009-04-03 10:14:18 -04002852 b = p->nodes[level];
2853 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002854
Chris Masonbd681512011-07-16 15:23:14 -04002855 /*
2856 * slot 0 is special, if we change the key
2857 * we have to update the parent pointer
2858 * which means we must have a write lock
2859 * on the parent
2860 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002861 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002862 write_lock_level < level + 1) {
2863 write_lock_level = level + 1;
2864 btrfs_release_path(p);
2865 goto again;
2866 }
2867
Chris Masonf7c79f32012-03-19 15:54:38 -04002868 unlock_up(p, level, lowest_unlock,
2869 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002870
Chris Mason925baed2008-06-25 16:01:30 -04002871 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002872 if (dec)
2873 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002874 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002875 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002876
Yan Zheng33c66f42009-07-22 09:59:00 -04002877 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002878 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002879 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002880 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002881 if (err) {
2882 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002883 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002884 }
Chris Mason76a05b32009-05-14 13:24:30 -04002885
Chris Masonb4ce94d2009-02-04 09:25:08 -05002886 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002887 level = btrfs_header_level(b);
2888 if (level <= write_lock_level) {
2889 err = btrfs_try_tree_write_lock(b);
2890 if (!err) {
2891 btrfs_set_path_blocking(p);
2892 btrfs_tree_lock(b);
2893 btrfs_clear_path_blocking(p, b,
2894 BTRFS_WRITE_LOCK);
2895 }
2896 p->locks[level] = BTRFS_WRITE_LOCK;
2897 } else {
2898 err = btrfs_try_tree_read_lock(b);
2899 if (!err) {
2900 btrfs_set_path_blocking(p);
2901 btrfs_tree_read_lock(b);
2902 btrfs_clear_path_blocking(p, b,
2903 BTRFS_READ_LOCK);
2904 }
2905 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002906 }
Chris Masonbd681512011-07-16 15:23:14 -04002907 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002908 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002909 } else {
2910 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002911 if (ins_len > 0 &&
2912 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002913 if (write_lock_level < 1) {
2914 write_lock_level = 1;
2915 btrfs_release_path(p);
2916 goto again;
2917 }
2918
Chris Masonb4ce94d2009-02-04 09:25:08 -05002919 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002920 err = split_leaf(trans, root, key,
2921 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002922 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002923
Yan Zheng33c66f42009-07-22 09:59:00 -04002924 BUG_ON(err > 0);
2925 if (err) {
2926 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002927 goto done;
2928 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002929 }
Chris Mason459931e2008-12-10 09:10:46 -05002930 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002931 unlock_up(p, level, lowest_unlock,
2932 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002933 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002934 }
2935 }
Chris Mason65b51a02008-08-01 15:11:20 -04002936 ret = 1;
2937done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002938 /*
2939 * we don't really know what they plan on doing with the path
2940 * from here on, so for now just mark it as blocking
2941 */
Chris Masonb9473432009-03-13 11:00:37 -04002942 if (!p->leave_spinning)
2943 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002944 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002945 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002946 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002947}
2948
Chris Mason74123bd2007-02-02 11:05:29 -05002949/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002950 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2951 * current state of the tree together with the operations recorded in the tree
2952 * modification log to search for the key in a previous version of this tree, as
2953 * denoted by the time_seq parameter.
2954 *
2955 * Naturally, there is no support for insert, delete or cow operations.
2956 *
2957 * The resulting path and return value will be set up as if we called
2958 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2959 */
2960int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2961 struct btrfs_path *p, u64 time_seq)
2962{
2963 struct extent_buffer *b;
2964 int slot;
2965 int ret;
2966 int err;
2967 int level;
2968 int lowest_unlock = 1;
2969 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002970 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002971
2972 lowest_level = p->lowest_level;
2973 WARN_ON(p->nodes[0] != NULL);
2974
2975 if (p->search_commit_root) {
2976 BUG_ON(time_seq);
2977 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2978 }
2979
2980again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002981 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002982 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002983 p->locks[level] = BTRFS_READ_LOCK;
2984
2985 while (b) {
2986 level = btrfs_header_level(b);
2987 p->nodes[level] = b;
2988 btrfs_clear_path_blocking(p, NULL, 0);
2989
2990 /*
2991 * we have a lock on b and as long as we aren't changing
2992 * the tree, there is no way to for the items in b to change.
2993 * It is safe to drop the lock on our parent before we
2994 * go through the expensive btree search on b.
2995 */
2996 btrfs_unlock_up_safe(p, level + 1);
2997
Josef Bacikd4b40872013-09-24 14:09:34 -04002998 /*
2999 * Since we can unwind eb's we want to do a real search every
3000 * time.
3001 */
3002 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003003 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003004
3005 if (level != 0) {
3006 int dec = 0;
3007 if (ret && slot > 0) {
3008 dec = 1;
3009 slot -= 1;
3010 }
3011 p->slots[level] = slot;
3012 unlock_up(p, level, lowest_unlock, 0, NULL);
3013
3014 if (level == lowest_level) {
3015 if (dec)
3016 p->slots[level]++;
3017 goto done;
3018 }
3019
3020 err = read_block_for_search(NULL, root, p, &b, level,
3021 slot, key, time_seq);
3022 if (err == -EAGAIN)
3023 goto again;
3024 if (err) {
3025 ret = err;
3026 goto done;
3027 }
3028
3029 level = btrfs_header_level(b);
3030 err = btrfs_try_tree_read_lock(b);
3031 if (!err) {
3032 btrfs_set_path_blocking(p);
3033 btrfs_tree_read_lock(b);
3034 btrfs_clear_path_blocking(p, b,
3035 BTRFS_READ_LOCK);
3036 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003037 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003038 if (!b) {
3039 ret = -ENOMEM;
3040 goto done;
3041 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003042 p->locks[level] = BTRFS_READ_LOCK;
3043 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003044 } else {
3045 p->slots[level] = slot;
3046 unlock_up(p, level, lowest_unlock, 0, NULL);
3047 goto done;
3048 }
3049 }
3050 ret = 1;
3051done:
3052 if (!p->leave_spinning)
3053 btrfs_set_path_blocking(p);
3054 if (ret < 0)
3055 btrfs_release_path(p);
3056
3057 return ret;
3058}
3059
3060/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003061 * helper to use instead of search slot if no exact match is needed but
3062 * instead the next or previous item should be returned.
3063 * When find_higher is true, the next higher item is returned, the next lower
3064 * otherwise.
3065 * When return_any and find_higher are both true, and no higher item is found,
3066 * return the next lower instead.
3067 * When return_any is true and find_higher is false, and no lower item is found,
3068 * return the next higher instead.
3069 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3070 * < 0 on error
3071 */
3072int btrfs_search_slot_for_read(struct btrfs_root *root,
3073 struct btrfs_key *key, struct btrfs_path *p,
3074 int find_higher, int return_any)
3075{
3076 int ret;
3077 struct extent_buffer *leaf;
3078
3079again:
3080 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3081 if (ret <= 0)
3082 return ret;
3083 /*
3084 * a return value of 1 means the path is at the position where the
3085 * item should be inserted. Normally this is the next bigger item,
3086 * but in case the previous item is the last in a leaf, path points
3087 * to the first free slot in the previous leaf, i.e. at an invalid
3088 * item.
3089 */
3090 leaf = p->nodes[0];
3091
3092 if (find_higher) {
3093 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3094 ret = btrfs_next_leaf(root, p);
3095 if (ret <= 0)
3096 return ret;
3097 if (!return_any)
3098 return 1;
3099 /*
3100 * no higher item found, return the next
3101 * lower instead
3102 */
3103 return_any = 0;
3104 find_higher = 0;
3105 btrfs_release_path(p);
3106 goto again;
3107 }
3108 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003109 if (p->slots[0] == 0) {
3110 ret = btrfs_prev_leaf(root, p);
3111 if (ret < 0)
3112 return ret;
3113 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003114 leaf = p->nodes[0];
3115 if (p->slots[0] == btrfs_header_nritems(leaf))
3116 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003117 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003118 }
Arne Jansene6793762011-09-13 11:18:10 +02003119 if (!return_any)
3120 return 1;
3121 /*
3122 * no lower item found, return the next
3123 * higher instead
3124 */
3125 return_any = 0;
3126 find_higher = 1;
3127 btrfs_release_path(p);
3128 goto again;
3129 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003130 --p->slots[0];
3131 }
3132 }
3133 return 0;
3134}
3135
3136/*
Chris Mason74123bd2007-02-02 11:05:29 -05003137 * adjust the pointers going up the tree, starting at level
3138 * making sure the right key of each node is points to 'key'.
3139 * This is used after shifting pointers to the left, so it stops
3140 * fixing up pointers when a given leaf/node is not in slot 0 of the
3141 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003142 *
Chris Mason74123bd2007-02-02 11:05:29 -05003143 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003144static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003145 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003146{
3147 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003148 struct extent_buffer *t;
3149
Chris Mason234b63a2007-03-13 10:46:10 -04003150 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003151 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003152 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003153 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003154 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00003155 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003156 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003157 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003158 if (tslot != 0)
3159 break;
3160 }
3161}
3162
Chris Mason74123bd2007-02-02 11:05:29 -05003163/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003164 * update item key.
3165 *
3166 * This function isn't completely safe. It's the caller's responsibility
3167 * that the new key won't break the order
3168 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003169void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003170 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003171{
3172 struct btrfs_disk_key disk_key;
3173 struct extent_buffer *eb;
3174 int slot;
3175
3176 eb = path->nodes[0];
3177 slot = path->slots[0];
3178 if (slot > 0) {
3179 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003180 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003181 }
3182 if (slot < btrfs_header_nritems(eb) - 1) {
3183 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003184 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003185 }
3186
3187 btrfs_cpu_key_to_disk(&disk_key, new_key);
3188 btrfs_set_item_key(eb, &disk_key, slot);
3189 btrfs_mark_buffer_dirty(eb);
3190 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003191 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003192}
3193
3194/*
Chris Mason74123bd2007-02-02 11:05:29 -05003195 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003196 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003197 *
3198 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3199 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003200 */
Chris Mason98ed5172008-01-03 10:01:48 -05003201static int push_node_left(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003203 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003204{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003205 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003206 int src_nritems;
3207 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003208 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003209
Chris Mason5f39d392007-10-15 16:14:19 -04003210 src_nritems = btrfs_header_nritems(src);
3211 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003212 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003213 WARN_ON(btrfs_header_generation(src) != trans->transid);
3214 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003215
Chris Masonbce4eae2008-04-24 14:42:46 -04003216 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003217 return 1;
3218
Chris Masond3977122009-01-05 21:25:51 -05003219 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003220 return 1;
3221
Chris Masonbce4eae2008-04-24 14:42:46 -04003222 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003223 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003224 if (push_items < src_nritems) {
3225 /* leave at least 8 pointers in the node if
3226 * we aren't going to empty it
3227 */
3228 if (src_nritems - push_items < 8) {
3229 if (push_items <= 8)
3230 return 1;
3231 push_items -= 8;
3232 }
3233 }
3234 } else
3235 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003236
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003237 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3238 push_items);
3239 if (ret) {
3240 btrfs_abort_transaction(trans, root, ret);
3241 return ret;
3242 }
Chris Mason5f39d392007-10-15 16:14:19 -04003243 copy_extent_buffer(dst, src,
3244 btrfs_node_key_ptr_offset(dst_nritems),
3245 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003246 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003247
Chris Masonbb803952007-03-01 12:04:21 -05003248 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003249 /*
3250 * don't call tree_mod_log_eb_move here, key removal was already
3251 * fully logged by tree_mod_log_eb_copy above.
3252 */
Chris Mason5f39d392007-10-15 16:14:19 -04003253 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3254 btrfs_node_key_ptr_offset(push_items),
3255 (src_nritems - push_items) *
3256 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003257 }
Chris Mason5f39d392007-10-15 16:14:19 -04003258 btrfs_set_header_nritems(src, src_nritems - push_items);
3259 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3260 btrfs_mark_buffer_dirty(src);
3261 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003262
Chris Masonbb803952007-03-01 12:04:21 -05003263 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264}
3265
Chris Mason97571fd2007-02-24 13:39:08 -05003266/*
Chris Mason79f95c82007-03-01 15:16:26 -05003267 * try to push data from one node into the next node right in the
3268 * tree.
3269 *
3270 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3271 * error, and > 0 if there was no room in the right hand block.
3272 *
3273 * this will only push up to 1/2 the contents of the left node over
3274 */
Chris Mason5f39d392007-10-15 16:14:19 -04003275static int balance_node_right(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root,
3277 struct extent_buffer *dst,
3278 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003279{
Chris Mason79f95c82007-03-01 15:16:26 -05003280 int push_items = 0;
3281 int max_push;
3282 int src_nritems;
3283 int dst_nritems;
3284 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003285
Chris Mason7bb86312007-12-11 09:25:06 -05003286 WARN_ON(btrfs_header_generation(src) != trans->transid);
3287 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3288
Chris Mason5f39d392007-10-15 16:14:19 -04003289 src_nritems = btrfs_header_nritems(src);
3290 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003291 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003292 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003293 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003294
Chris Masond3977122009-01-05 21:25:51 -05003295 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003296 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003297
3298 max_push = src_nritems / 2 + 1;
3299 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003300 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003301 return 1;
Yan252c38f2007-08-29 09:11:44 -04003302
Chris Mason79f95c82007-03-01 15:16:26 -05003303 if (max_push < push_items)
3304 push_items = max_push;
3305
Jan Schmidtf2304752012-05-26 11:43:17 +02003306 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003307 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3308 btrfs_node_key_ptr_offset(0),
3309 (dst_nritems) *
3310 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003311
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003312 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3313 src_nritems - push_items, push_items);
3314 if (ret) {
3315 btrfs_abort_transaction(trans, root, ret);
3316 return ret;
3317 }
Chris Mason5f39d392007-10-15 16:14:19 -04003318 copy_extent_buffer(dst, src,
3319 btrfs_node_key_ptr_offset(0),
3320 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003321 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003322
Chris Mason5f39d392007-10-15 16:14:19 -04003323 btrfs_set_header_nritems(src, src_nritems - push_items);
3324 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003325
Chris Mason5f39d392007-10-15 16:14:19 -04003326 btrfs_mark_buffer_dirty(src);
3327 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003328
Chris Mason79f95c82007-03-01 15:16:26 -05003329 return ret;
3330}
3331
3332/*
Chris Mason97571fd2007-02-24 13:39:08 -05003333 * helper function to insert a new root level in the tree.
3334 * A new node is allocated, and a single item is inserted to
3335 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003336 *
3337 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003338 */
Chris Masond3977122009-01-05 21:25:51 -05003339static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003340 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003341 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003342{
Chris Mason7bb86312007-12-11 09:25:06 -05003343 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 struct extent_buffer *lower;
3345 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003346 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003347 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003348
3349 BUG_ON(path->nodes[level]);
3350 BUG_ON(path->nodes[level-1] != root->node);
3351
Chris Mason7bb86312007-12-11 09:25:06 -05003352 lower = path->nodes[level-1];
3353 if (level == 1)
3354 btrfs_item_key(lower, &lower_key, 0);
3355 else
3356 btrfs_node_key(lower, &lower_key, 0);
3357
Zheng Yan31840ae2008-09-23 13:14:14 -04003358 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003360 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003361 if (IS_ERR(c))
3362 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003363
Yan, Zhengf0486c62010-05-16 10:46:25 -04003364 root_add_used(root, root->nodesize);
3365
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003366 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003367 btrfs_set_header_nritems(c, 1);
3368 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003369 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003370 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003371 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003372 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003373
Ross Kirk0a4e5582013-09-24 10:12:38 +01003374 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003375 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003376
3377 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003378 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003379
Chris Mason5f39d392007-10-15 16:14:19 -04003380 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003381 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003382 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003383 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003384
3385 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003386
3387 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003388
Chris Mason925baed2008-06-25 16:01:30 -04003389 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003390 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003391 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003392
3393 /* the super has an extra ref to root->node */
3394 free_extent_buffer(old);
3395
Chris Mason0b86a832008-03-24 15:01:56 -04003396 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003397 extent_buffer_get(c);
3398 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003399 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003400 path->slots[level] = 0;
3401 return 0;
3402}
3403
Chris Mason74123bd2007-02-02 11:05:29 -05003404/*
3405 * worker function to insert a single pointer in a node.
3406 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003407 *
Chris Mason74123bd2007-02-02 11:05:29 -05003408 * slot and level indicate where you want the key to go, and
3409 * blocknr is the block the key points to.
3410 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003411static void insert_ptr(struct btrfs_trans_handle *trans,
3412 struct btrfs_root *root, struct btrfs_path *path,
3413 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003414 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003415{
Chris Mason5f39d392007-10-15 16:14:19 -04003416 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003417 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003418 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003419
3420 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003421 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003422 lower = path->nodes[level];
3423 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003424 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003425 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003426 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003427 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003428 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3429 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003430 memmove_extent_buffer(lower,
3431 btrfs_node_key_ptr_offset(slot + 1),
3432 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003433 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003434 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003435 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003436 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003437 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003438 BUG_ON(ret < 0);
3439 }
Chris Mason5f39d392007-10-15 16:14:19 -04003440 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003441 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003442 WARN_ON(trans->transid == 0);
3443 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003444 btrfs_set_header_nritems(lower, nritems + 1);
3445 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003446}
3447
Chris Mason97571fd2007-02-24 13:39:08 -05003448/*
3449 * split the node at the specified level in path in two.
3450 * The path is corrected to point to the appropriate node after the split
3451 *
3452 * Before splitting this tries to make some room in the node by pushing
3453 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003454 *
3455 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003456 */
Chris Masone02119d2008-09-05 16:13:11 -04003457static noinline int split_node(struct btrfs_trans_handle *trans,
3458 struct btrfs_root *root,
3459 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003460{
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct extent_buffer *c;
3462 struct extent_buffer *split;
3463 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003464 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003465 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003466 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003467
Chris Mason5f39d392007-10-15 16:14:19 -04003468 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003469 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003470 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003471 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003472 * trying to split the root, lets make a new one
3473 *
Liu Bofdd99c72013-05-22 12:06:51 +00003474 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003475 * insert_new_root, because that root buffer will be kept as a
3476 * normal node. We are going to log removal of half of the
3477 * elements below with tree_mod_log_eb_copy. We're holding a
3478 * tree lock on the buffer, which is why we cannot race with
3479 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003480 */
Liu Bofdd99c72013-05-22 12:06:51 +00003481 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003482 if (ret)
3483 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003484 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003485 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003486 c = path->nodes[level];
3487 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003488 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003489 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003490 if (ret < 0)
3491 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492 }
Chris Masone66f7092007-04-20 13:16:02 -04003493
Chris Mason5f39d392007-10-15 16:14:19 -04003494 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003495 mid = (c_nritems + 1) / 2;
3496 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003497
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003498 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003499 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003500 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 if (IS_ERR(split))
3502 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003503
Yan, Zhengf0486c62010-05-16 10:46:25 -04003504 root_add_used(root, root->nodesize);
3505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003506 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003507 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003508 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003510 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003511 btrfs_set_header_owner(split, root->root_key.objectid);
3512 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003513 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003514 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003515 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003516 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003517
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003518 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3519 mid, c_nritems - mid);
3520 if (ret) {
3521 btrfs_abort_transaction(trans, root, ret);
3522 return ret;
3523 }
Chris Mason5f39d392007-10-15 16:14:19 -04003524 copy_extent_buffer(split, c,
3525 btrfs_node_key_ptr_offset(0),
3526 btrfs_node_key_ptr_offset(mid),
3527 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3528 btrfs_set_header_nritems(split, c_nritems - mid);
3529 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003530 ret = 0;
3531
Chris Mason5f39d392007-10-15 16:14:19 -04003532 btrfs_mark_buffer_dirty(c);
3533 btrfs_mark_buffer_dirty(split);
3534
Jeff Mahoney143bede2012-03-01 14:56:26 +01003535 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003536 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003537
Chris Mason5de08d72007-02-24 06:24:44 -05003538 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003539 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003540 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 free_extent_buffer(c);
3542 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003543 path->slots[level + 1] += 1;
3544 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003545 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003546 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003547 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003548 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003549}
3550
Chris Mason74123bd2007-02-02 11:05:29 -05003551/*
3552 * how many bytes are required to store the items in a leaf. start
3553 * and nr indicate which items in the leaf to check. This totals up the
3554 * space used both by the item structs and the item data
3555 */
Chris Mason5f39d392007-10-15 16:14:19 -04003556static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003557{
Josef Bacik41be1f32012-10-15 13:43:18 -04003558 struct btrfs_item *start_item;
3559 struct btrfs_item *end_item;
3560 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003561 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003562 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003563 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003564
3565 if (!nr)
3566 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003567 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003568 start_item = btrfs_item_nr(start);
3569 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003570 data_len = btrfs_token_item_offset(l, start_item, &token) +
3571 btrfs_token_item_size(l, start_item, &token);
3572 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003573 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003574 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575 return data_len;
3576}
3577
Chris Mason74123bd2007-02-02 11:05:29 -05003578/*
Chris Masond4dbff92007-04-04 14:08:15 -04003579 * The space between the end of the leaf items and
3580 * the start of the leaf data. IOW, how much room
3581 * the leaf has left for both items and data
3582 */
Chris Masond3977122009-01-05 21:25:51 -05003583noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003584 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003585{
Chris Mason5f39d392007-10-15 16:14:19 -04003586 int nritems = btrfs_header_nritems(leaf);
3587 int ret;
3588 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3589 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003590 btrfs_crit(root->fs_info,
3591 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003592 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003593 leaf_space_used(leaf, 0, nritems), nritems);
3594 }
3595 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003596}
3597
Chris Mason99d8f832010-07-07 10:51:48 -04003598/*
3599 * min slot controls the lowest index we're willing to push to the
3600 * right. We'll push up to and including min_slot, but no lower
3601 */
Chris Mason44871b12009-03-13 10:04:31 -04003602static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3603 struct btrfs_root *root,
3604 struct btrfs_path *path,
3605 int data_size, int empty,
3606 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003607 int free_space, u32 left_nritems,
3608 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003609{
Chris Mason5f39d392007-10-15 16:14:19 -04003610 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003611 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003612 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003613 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003614 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003615 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003616 int push_space = 0;
3617 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003618 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003619 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003620 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003621 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003622 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003623
Chris Masoncfed81a2012-03-03 07:40:03 -05003624 btrfs_init_map_token(&token);
3625
Chris Mason34a38212007-11-07 13:31:03 -05003626 if (empty)
3627 nr = 0;
3628 else
Chris Mason99d8f832010-07-07 10:51:48 -04003629 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003630
Zheng Yan31840ae2008-09-23 13:14:14 -04003631 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003632 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003633
Chris Mason44871b12009-03-13 10:04:31 -04003634 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003635 i = left_nritems - 1;
3636 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003637 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003638
Zheng Yan31840ae2008-09-23 13:14:14 -04003639 if (!empty && push_items > 0) {
3640 if (path->slots[0] > i)
3641 break;
3642 if (path->slots[0] == i) {
3643 int space = btrfs_leaf_free_space(root, left);
3644 if (space + push_space * 2 > free_space)
3645 break;
3646 }
3647 }
3648
Chris Mason00ec4c52007-02-24 12:47:20 -05003649 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003650 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003651
Chris Masondb945352007-10-15 16:15:53 -04003652 this_item_size = btrfs_item_size(left, item);
3653 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003654 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003655
Chris Mason00ec4c52007-02-24 12:47:20 -05003656 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003657 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003658 if (i == 0)
3659 break;
3660 i--;
Chris Masondb945352007-10-15 16:15:53 -04003661 }
Chris Mason5f39d392007-10-15 16:14:19 -04003662
Chris Mason925baed2008-06-25 16:01:30 -04003663 if (push_items == 0)
3664 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003665
Julia Lawall6c1500f2012-11-03 20:30:18 +00003666 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003667
Chris Mason00ec4c52007-02-24 12:47:20 -05003668 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003669 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003670
Chris Mason5f39d392007-10-15 16:14:19 -04003671 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003672 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003673
Chris Mason00ec4c52007-02-24 12:47:20 -05003674 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003675 data_end = leaf_data_end(root, right);
3676 memmove_extent_buffer(right,
3677 btrfs_leaf_data(right) + data_end - push_space,
3678 btrfs_leaf_data(right) + data_end,
3679 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3680
Chris Mason00ec4c52007-02-24 12:47:20 -05003681 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003682 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003683 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3684 btrfs_leaf_data(left) + leaf_data_end(root, left),
3685 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003686
3687 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3688 btrfs_item_nr_offset(0),
3689 right_nritems * sizeof(struct btrfs_item));
3690
Chris Mason00ec4c52007-02-24 12:47:20 -05003691 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003692 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3693 btrfs_item_nr_offset(left_nritems - push_items),
3694 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003695
3696 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003697 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003698 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003699 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003700 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003701 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003702 push_space -= btrfs_token_item_size(right, item, &token);
3703 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003704 }
3705
Chris Mason7518a232007-03-12 12:01:18 -04003706 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003707 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003708
Chris Mason34a38212007-11-07 13:31:03 -05003709 if (left_nritems)
3710 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003711 else
3712 clean_tree_block(trans, root, left);
3713
Chris Mason5f39d392007-10-15 16:14:19 -04003714 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003715
Chris Mason5f39d392007-10-15 16:14:19 -04003716 btrfs_item_key(right, &disk_key, 0);
3717 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003718 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003719
Chris Mason00ec4c52007-02-24 12:47:20 -05003720 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003721 if (path->slots[0] >= left_nritems) {
3722 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003723 if (btrfs_header_nritems(path->nodes[0]) == 0)
3724 clean_tree_block(trans, root, path->nodes[0]);
3725 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003726 free_extent_buffer(path->nodes[0]);
3727 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003728 path->slots[1] += 1;
3729 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003730 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003731 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003732 }
3733 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003734
3735out_unlock:
3736 btrfs_tree_unlock(right);
3737 free_extent_buffer(right);
3738 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003739}
Chris Mason925baed2008-06-25 16:01:30 -04003740
Chris Mason00ec4c52007-02-24 12:47:20 -05003741/*
Chris Mason44871b12009-03-13 10:04:31 -04003742 * push some data in the path leaf to the right, trying to free up at
3743 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3744 *
3745 * returns 1 if the push failed because the other node didn't have enough
3746 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003747 *
3748 * this will push starting from min_slot to the end of the leaf. It won't
3749 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003750 */
3751static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003752 *root, struct btrfs_path *path,
3753 int min_data_size, int data_size,
3754 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003755{
3756 struct extent_buffer *left = path->nodes[0];
3757 struct extent_buffer *right;
3758 struct extent_buffer *upper;
3759 int slot;
3760 int free_space;
3761 u32 left_nritems;
3762 int ret;
3763
3764 if (!path->nodes[1])
3765 return 1;
3766
3767 slot = path->slots[1];
3768 upper = path->nodes[1];
3769 if (slot >= btrfs_header_nritems(upper) - 1)
3770 return 1;
3771
3772 btrfs_assert_tree_locked(path->nodes[1]);
3773
3774 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003775 if (right == NULL)
3776 return 1;
3777
Chris Mason44871b12009-03-13 10:04:31 -04003778 btrfs_tree_lock(right);
3779 btrfs_set_lock_blocking(right);
3780
3781 free_space = btrfs_leaf_free_space(root, right);
3782 if (free_space < data_size)
3783 goto out_unlock;
3784
3785 /* cow and double check */
3786 ret = btrfs_cow_block(trans, root, right, upper,
3787 slot + 1, &right);
3788 if (ret)
3789 goto out_unlock;
3790
3791 free_space = btrfs_leaf_free_space(root, right);
3792 if (free_space < data_size)
3793 goto out_unlock;
3794
3795 left_nritems = btrfs_header_nritems(left);
3796 if (left_nritems == 0)
3797 goto out_unlock;
3798
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003799 if (path->slots[0] == left_nritems && !empty) {
3800 /* Key greater than all keys in the leaf, right neighbor has
3801 * enough room for it and we're not emptying our leaf to delete
3802 * it, therefore use right neighbor to insert the new item and
3803 * no need to touch/dirty our left leaft. */
3804 btrfs_tree_unlock(left);
3805 free_extent_buffer(left);
3806 path->nodes[0] = right;
3807 path->slots[0] = 0;
3808 path->slots[1]++;
3809 return 0;
3810 }
3811
Chris Mason99d8f832010-07-07 10:51:48 -04003812 return __push_leaf_right(trans, root, path, min_data_size, empty,
3813 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003814out_unlock:
3815 btrfs_tree_unlock(right);
3816 free_extent_buffer(right);
3817 return 1;
3818}
3819
3820/*
Chris Mason74123bd2007-02-02 11:05:29 -05003821 * push some data in the path leaf to the left, trying to free up at
3822 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003823 *
3824 * max_slot can put a limit on how far into the leaf we'll push items. The
3825 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3826 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003827 */
Chris Mason44871b12009-03-13 10:04:31 -04003828static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *root,
3830 struct btrfs_path *path, int data_size,
3831 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003832 int free_space, u32 right_nritems,
3833 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834{
Chris Mason5f39d392007-10-15 16:14:19 -04003835 struct btrfs_disk_key disk_key;
3836 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003837 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003838 int push_space = 0;
3839 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003840 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003841 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003842 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003843 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003844 u32 this_item_size;
3845 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003846 struct btrfs_map_token token;
3847
3848 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003849
Chris Mason34a38212007-11-07 13:31:03 -05003850 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003851 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003852 else
Chris Mason99d8f832010-07-07 10:51:48 -04003853 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003854
3855 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003856 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003857
Zheng Yan31840ae2008-09-23 13:14:14 -04003858 if (!empty && push_items > 0) {
3859 if (path->slots[0] < i)
3860 break;
3861 if (path->slots[0] == i) {
3862 int space = btrfs_leaf_free_space(root, right);
3863 if (space + push_space * 2 > free_space)
3864 break;
3865 }
3866 }
3867
Chris Masonbe0e5c02007-01-26 15:51:26 -05003868 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003869 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003870
3871 this_item_size = btrfs_item_size(right, item);
3872 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003873 break;
Chris Masondb945352007-10-15 16:15:53 -04003874
Chris Masonbe0e5c02007-01-26 15:51:26 -05003875 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003876 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003877 }
Chris Masondb945352007-10-15 16:15:53 -04003878
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003880 ret = 1;
3881 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303883 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003884
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003886 copy_extent_buffer(left, right,
3887 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3888 btrfs_item_nr_offset(0),
3889 push_items * sizeof(struct btrfs_item));
3890
Chris Mason123abc82007-03-14 14:14:43 -04003891 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003892 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003893
3894 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003895 leaf_data_end(root, left) - push_space,
3896 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003897 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003898 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003899 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003900 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003901
Chris Masondb945352007-10-15 16:15:53 -04003902 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003903 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003904 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003905
Ross Kirkdd3cc162013-09-16 15:58:09 +01003906 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003907
Chris Masoncfed81a2012-03-03 07:40:03 -05003908 ioff = btrfs_token_item_offset(left, item, &token);
3909 btrfs_set_token_item_offset(left, item,
3910 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3911 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003912 }
Chris Mason5f39d392007-10-15 16:14:19 -04003913 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003914
3915 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003916 if (push_items > right_nritems)
3917 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003918 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003919
Chris Mason34a38212007-11-07 13:31:03 -05003920 if (push_items < right_nritems) {
3921 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3922 leaf_data_end(root, right);
3923 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3924 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3925 btrfs_leaf_data(right) +
3926 leaf_data_end(root, right), push_space);
3927
3928 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003929 btrfs_item_nr_offset(push_items),
3930 (btrfs_header_nritems(right) - push_items) *
3931 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003932 }
Yaneef1c492007-11-26 10:58:13 -05003933 right_nritems -= push_items;
3934 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003935 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003936 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003937 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003938
Chris Masoncfed81a2012-03-03 07:40:03 -05003939 push_space = push_space - btrfs_token_item_size(right,
3940 item, &token);
3941 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003942 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003943
Chris Mason5f39d392007-10-15 16:14:19 -04003944 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003945 if (right_nritems)
3946 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003947 else
3948 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003949
Chris Mason5f39d392007-10-15 16:14:19 -04003950 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003951 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003952
3953 /* then fixup the leaf pointer in the path */
3954 if (path->slots[0] < push_items) {
3955 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003956 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003957 free_extent_buffer(path->nodes[0]);
3958 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003959 path->slots[1] -= 1;
3960 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003961 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003962 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003963 path->slots[0] -= push_items;
3964 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003965 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003966 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003967out:
3968 btrfs_tree_unlock(left);
3969 free_extent_buffer(left);
3970 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003971}
3972
Chris Mason74123bd2007-02-02 11:05:29 -05003973/*
Chris Mason44871b12009-03-13 10:04:31 -04003974 * push some data in the path leaf to the left, trying to free up at
3975 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003976 *
3977 * max_slot can put a limit on how far into the leaf we'll push items. The
3978 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3979 * items
Chris Mason44871b12009-03-13 10:04:31 -04003980 */
3981static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003982 *root, struct btrfs_path *path, int min_data_size,
3983 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003984{
3985 struct extent_buffer *right = path->nodes[0];
3986 struct extent_buffer *left;
3987 int slot;
3988 int free_space;
3989 u32 right_nritems;
3990 int ret = 0;
3991
3992 slot = path->slots[1];
3993 if (slot == 0)
3994 return 1;
3995 if (!path->nodes[1])
3996 return 1;
3997
3998 right_nritems = btrfs_header_nritems(right);
3999 if (right_nritems == 0)
4000 return 1;
4001
4002 btrfs_assert_tree_locked(path->nodes[1]);
4003
4004 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004005 if (left == NULL)
4006 return 1;
4007
Chris Mason44871b12009-03-13 10:04:31 -04004008 btrfs_tree_lock(left);
4009 btrfs_set_lock_blocking(left);
4010
4011 free_space = btrfs_leaf_free_space(root, left);
4012 if (free_space < data_size) {
4013 ret = 1;
4014 goto out;
4015 }
4016
4017 /* cow and double check */
4018 ret = btrfs_cow_block(trans, root, left,
4019 path->nodes[1], slot - 1, &left);
4020 if (ret) {
4021 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004022 if (ret == -ENOSPC)
4023 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004024 goto out;
4025 }
4026
4027 free_space = btrfs_leaf_free_space(root, left);
4028 if (free_space < data_size) {
4029 ret = 1;
4030 goto out;
4031 }
4032
Chris Mason99d8f832010-07-07 10:51:48 -04004033 return __push_leaf_left(trans, root, path, min_data_size,
4034 empty, left, free_space, right_nritems,
4035 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004036out:
4037 btrfs_tree_unlock(left);
4038 free_extent_buffer(left);
4039 return ret;
4040}
4041
4042/*
Chris Mason74123bd2007-02-02 11:05:29 -05004043 * split the path's leaf in two, making sure there is at least data_size
4044 * available for the resulting leaf level of the path.
4045 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004046static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4047 struct btrfs_root *root,
4048 struct btrfs_path *path,
4049 struct extent_buffer *l,
4050 struct extent_buffer *right,
4051 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004052{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004053 int data_copy_size;
4054 int rt_data_off;
4055 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004056 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004057 struct btrfs_map_token token;
4058
4059 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004060
Chris Mason5f39d392007-10-15 16:14:19 -04004061 nritems = nritems - mid;
4062 btrfs_set_header_nritems(right, nritems);
4063 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4064
4065 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4066 btrfs_item_nr_offset(mid),
4067 nritems * sizeof(struct btrfs_item));
4068
4069 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004070 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4071 data_copy_size, btrfs_leaf_data(l) +
4072 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004073
Chris Mason5f39d392007-10-15 16:14:19 -04004074 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4075 btrfs_item_end_nr(l, mid);
4076
4077 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004078 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004079 u32 ioff;
4080
Chris Masoncfed81a2012-03-03 07:40:03 -05004081 ioff = btrfs_token_item_offset(right, item, &token);
4082 btrfs_set_token_item_offset(right, item,
4083 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004084 }
Chris Mason74123bd2007-02-02 11:05:29 -05004085
Chris Mason5f39d392007-10-15 16:14:19 -04004086 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004087 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004088 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004089 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004090
4091 btrfs_mark_buffer_dirty(right);
4092 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004093 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004094
Chris Masonbe0e5c02007-01-26 15:51:26 -05004095 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004096 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004097 free_extent_buffer(path->nodes[0]);
4098 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004099 path->slots[0] -= mid;
4100 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004101 } else {
4102 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004103 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004104 }
Chris Mason5f39d392007-10-15 16:14:19 -04004105
Chris Masoneb60cea2007-02-02 09:18:22 -05004106 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004107}
4108
4109/*
Chris Mason99d8f832010-07-07 10:51:48 -04004110 * double splits happen when we need to insert a big item in the middle
4111 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4112 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4113 * A B C
4114 *
4115 * We avoid this by trying to push the items on either side of our target
4116 * into the adjacent leaves. If all goes well we can avoid the double split
4117 * completely.
4118 */
4119static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4120 struct btrfs_root *root,
4121 struct btrfs_path *path,
4122 int data_size)
4123{
4124 int ret;
4125 int progress = 0;
4126 int slot;
4127 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004128 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004129
4130 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004131 if (slot < btrfs_header_nritems(path->nodes[0]))
4132 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004133
4134 /*
4135 * try to push all the items after our slot into the
4136 * right leaf
4137 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004138 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004139 if (ret < 0)
4140 return ret;
4141
4142 if (ret == 0)
4143 progress++;
4144
4145 nritems = btrfs_header_nritems(path->nodes[0]);
4146 /*
4147 * our goal is to get our slot at the start or end of a leaf. If
4148 * we've done so we're done
4149 */
4150 if (path->slots[0] == 0 || path->slots[0] == nritems)
4151 return 0;
4152
4153 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4154 return 0;
4155
4156 /* try to push all the items before our slot into the next leaf */
4157 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004158 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004159 if (ret < 0)
4160 return ret;
4161
4162 if (ret == 0)
4163 progress++;
4164
4165 if (progress)
4166 return 0;
4167 return 1;
4168}
4169
4170/*
Chris Mason44871b12009-03-13 10:04:31 -04004171 * split the path's leaf in two, making sure there is at least data_size
4172 * available for the resulting leaf level of the path.
4173 *
4174 * returns 0 if all went well and < 0 on failure.
4175 */
4176static noinline int split_leaf(struct btrfs_trans_handle *trans,
4177 struct btrfs_root *root,
4178 struct btrfs_key *ins_key,
4179 struct btrfs_path *path, int data_size,
4180 int extend)
4181{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004182 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004183 struct extent_buffer *l;
4184 u32 nritems;
4185 int mid;
4186 int slot;
4187 struct extent_buffer *right;
4188 int ret = 0;
4189 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004190 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004191 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004192 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004193
Yan, Zhenga5719522009-09-24 09:17:31 -04004194 l = path->nodes[0];
4195 slot = path->slots[0];
4196 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4197 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4198 return -EOVERFLOW;
4199
Chris Mason44871b12009-03-13 10:04:31 -04004200 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004201 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004202 int space_needed = data_size;
4203
4204 if (slot < btrfs_header_nritems(l))
4205 space_needed -= btrfs_leaf_free_space(root, l);
4206
4207 wret = push_leaf_right(trans, root, path, space_needed,
4208 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004209 if (wret < 0)
4210 return wret;
4211 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004212 wret = push_leaf_left(trans, root, path, space_needed,
4213 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004214 if (wret < 0)
4215 return wret;
4216 }
4217 l = path->nodes[0];
4218
4219 /* did the pushes work? */
4220 if (btrfs_leaf_free_space(root, l) >= data_size)
4221 return 0;
4222 }
4223
4224 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004225 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004226 if (ret)
4227 return ret;
4228 }
4229again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004230 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004231 l = path->nodes[0];
4232 slot = path->slots[0];
4233 nritems = btrfs_header_nritems(l);
4234 mid = (nritems + 1) / 2;
4235
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004236 if (mid <= slot) {
4237 if (nritems == 1 ||
4238 leaf_space_used(l, mid, nritems - mid) + data_size >
4239 BTRFS_LEAF_DATA_SIZE(root)) {
4240 if (slot >= nritems) {
4241 split = 0;
4242 } else {
4243 mid = slot;
4244 if (mid != nritems &&
4245 leaf_space_used(l, mid, nritems - mid) +
4246 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004247 if (data_size && !tried_avoid_double)
4248 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004249 split = 2;
4250 }
4251 }
4252 }
4253 } else {
4254 if (leaf_space_used(l, 0, mid) + data_size >
4255 BTRFS_LEAF_DATA_SIZE(root)) {
4256 if (!extend && data_size && slot == 0) {
4257 split = 0;
4258 } else if ((extend || !data_size) && slot == 0) {
4259 mid = 1;
4260 } else {
4261 mid = slot;
4262 if (mid != nritems &&
4263 leaf_space_used(l, mid, nritems - mid) +
4264 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004265 if (data_size && !tried_avoid_double)
4266 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304267 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004268 }
4269 }
4270 }
4271 }
4272
4273 if (split == 0)
4274 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4275 else
4276 btrfs_item_key(l, &disk_key, mid);
4277
David Sterba707e8a02014-06-04 19:22:26 +02004278 right = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004279 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004280 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004281 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004282 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004283
David Sterba707e8a02014-06-04 19:22:26 +02004284 root_add_used(root, root->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004285
4286 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4287 btrfs_set_header_bytenr(right, right->start);
4288 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004289 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004290 btrfs_set_header_owner(right, root->root_key.objectid);
4291 btrfs_set_header_level(right, 0);
4292 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004293 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004294
4295 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004296 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004297 BTRFS_UUID_SIZE);
4298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 if (split == 0) {
4300 if (mid <= slot) {
4301 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004302 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004303 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004304 btrfs_tree_unlock(path->nodes[0]);
4305 free_extent_buffer(path->nodes[0]);
4306 path->nodes[0] = right;
4307 path->slots[0] = 0;
4308 path->slots[1] += 1;
4309 } else {
4310 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004311 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004312 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004313 btrfs_tree_unlock(path->nodes[0]);
4314 free_extent_buffer(path->nodes[0]);
4315 path->nodes[0] = right;
4316 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004317 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004318 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004319 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 btrfs_mark_buffer_dirty(right);
4321 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004322 }
4323
Jeff Mahoney143bede2012-03-01 14:56:26 +01004324 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004326 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004327 BUG_ON(num_doubles != 0);
4328 num_doubles++;
4329 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004330 }
Chris Mason44871b12009-03-13 10:04:31 -04004331
Jeff Mahoney143bede2012-03-01 14:56:26 +01004332 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004333
4334push_for_double:
4335 push_for_double_split(trans, root, path, data_size);
4336 tried_avoid_double = 1;
4337 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4338 return 0;
4339 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004340}
4341
Yan, Zhengad48fd752009-11-12 09:33:58 +00004342static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4343 struct btrfs_root *root,
4344 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004345{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004346 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004347 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004348 struct btrfs_file_extent_item *fi;
4349 u64 extent_len = 0;
4350 u32 item_size;
4351 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004352
4353 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004354 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4355
4356 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4357 key.type != BTRFS_EXTENT_CSUM_KEY);
4358
4359 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4360 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004361
4362 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004363 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4364 fi = btrfs_item_ptr(leaf, path->slots[0],
4365 struct btrfs_file_extent_item);
4366 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4367 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004368 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004369
Chris Mason459931e2008-12-10 09:10:46 -05004370 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004371 path->search_for_split = 1;
4372 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004373 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004374 if (ret < 0)
4375 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004376
Yan, Zhengad48fd752009-11-12 09:33:58 +00004377 ret = -EAGAIN;
4378 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004379 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4381 goto err;
4382
Chris Mason109f6ae2010-04-02 09:20:18 -04004383 /* the leaf has changed, it now has room. return now */
4384 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4385 goto err;
4386
Yan, Zhengad48fd752009-11-12 09:33:58 +00004387 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4388 fi = btrfs_item_ptr(leaf, path->slots[0],
4389 struct btrfs_file_extent_item);
4390 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4391 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004392 }
4393
Chris Masonb9473432009-03-13 11:00:37 -04004394 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004395 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004396 if (ret)
4397 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004398
Yan, Zhengad48fd752009-11-12 09:33:58 +00004399 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004400 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004401 return 0;
4402err:
4403 path->keep_locks = 0;
4404 return ret;
4405}
4406
4407static noinline int split_item(struct btrfs_trans_handle *trans,
4408 struct btrfs_root *root,
4409 struct btrfs_path *path,
4410 struct btrfs_key *new_key,
4411 unsigned long split_offset)
4412{
4413 struct extent_buffer *leaf;
4414 struct btrfs_item *item;
4415 struct btrfs_item *new_item;
4416 int slot;
4417 char *buf;
4418 u32 nritems;
4419 u32 item_size;
4420 u32 orig_offset;
4421 struct btrfs_disk_key disk_key;
4422
Chris Masonb9473432009-03-13 11:00:37 -04004423 leaf = path->nodes[0];
4424 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4425
Chris Masonb4ce94d2009-02-04 09:25:08 -05004426 btrfs_set_path_blocking(path);
4427
Ross Kirkdd3cc162013-09-16 15:58:09 +01004428 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004429 orig_offset = btrfs_item_offset(leaf, item);
4430 item_size = btrfs_item_size(leaf, item);
4431
Chris Mason459931e2008-12-10 09:10:46 -05004432 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004433 if (!buf)
4434 return -ENOMEM;
4435
Chris Mason459931e2008-12-10 09:10:46 -05004436 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4437 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004438
Chris Mason459931e2008-12-10 09:10:46 -05004439 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004440 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004441 if (slot != nritems) {
4442 /* shift the items */
4443 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004444 btrfs_item_nr_offset(slot),
4445 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004446 }
4447
4448 btrfs_cpu_key_to_disk(&disk_key, new_key);
4449 btrfs_set_item_key(leaf, &disk_key, slot);
4450
Ross Kirkdd3cc162013-09-16 15:58:09 +01004451 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004452
4453 btrfs_set_item_offset(leaf, new_item, orig_offset);
4454 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4455
4456 btrfs_set_item_offset(leaf, item,
4457 orig_offset + item_size - split_offset);
4458 btrfs_set_item_size(leaf, item, split_offset);
4459
4460 btrfs_set_header_nritems(leaf, nritems + 1);
4461
4462 /* write the data for the start of the original item */
4463 write_extent_buffer(leaf, buf,
4464 btrfs_item_ptr_offset(leaf, path->slots[0]),
4465 split_offset);
4466
4467 /* write the data for the new item */
4468 write_extent_buffer(leaf, buf + split_offset,
4469 btrfs_item_ptr_offset(leaf, slot),
4470 item_size - split_offset);
4471 btrfs_mark_buffer_dirty(leaf);
4472
Yan, Zhengad48fd752009-11-12 09:33:58 +00004473 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004474 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004475 return 0;
4476}
4477
4478/*
4479 * This function splits a single item into two items,
4480 * giving 'new_key' to the new item and splitting the
4481 * old one at split_offset (from the start of the item).
4482 *
4483 * The path may be released by this operation. After
4484 * the split, the path is pointing to the old item. The
4485 * new item is going to be in the same node as the old one.
4486 *
4487 * Note, the item being split must be smaller enough to live alone on
4488 * a tree block with room for one extra struct btrfs_item
4489 *
4490 * This allows us to split the item in place, keeping a lock on the
4491 * leaf the entire time.
4492 */
4493int btrfs_split_item(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *root,
4495 struct btrfs_path *path,
4496 struct btrfs_key *new_key,
4497 unsigned long split_offset)
4498{
4499 int ret;
4500 ret = setup_leaf_for_split(trans, root, path,
4501 sizeof(struct btrfs_item));
4502 if (ret)
4503 return ret;
4504
4505 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004506 return ret;
4507}
4508
4509/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004510 * This function duplicate a item, giving 'new_key' to the new item.
4511 * It guarantees both items live in the same tree leaf and the new item
4512 * is contiguous with the original item.
4513 *
4514 * This allows us to split file extent in place, keeping a lock on the
4515 * leaf the entire time.
4516 */
4517int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4518 struct btrfs_root *root,
4519 struct btrfs_path *path,
4520 struct btrfs_key *new_key)
4521{
4522 struct extent_buffer *leaf;
4523 int ret;
4524 u32 item_size;
4525
4526 leaf = path->nodes[0];
4527 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4528 ret = setup_leaf_for_split(trans, root, path,
4529 item_size + sizeof(struct btrfs_item));
4530 if (ret)
4531 return ret;
4532
4533 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004534 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004535 item_size, item_size +
4536 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004537 leaf = path->nodes[0];
4538 memcpy_extent_buffer(leaf,
4539 btrfs_item_ptr_offset(leaf, path->slots[0]),
4540 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4541 item_size);
4542 return 0;
4543}
4544
4545/*
Chris Masond352ac62008-09-29 15:18:18 -04004546 * make the item pointed to by the path smaller. new_size indicates
4547 * how small to make it, and from_end tells us if we just chop bytes
4548 * off the end of the item or if we shift the item to chop bytes off
4549 * the front.
4550 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004551void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004552 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004553{
Chris Masonb18c6682007-04-17 13:26:50 -04004554 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004555 struct extent_buffer *leaf;
4556 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004557 u32 nritems;
4558 unsigned int data_end;
4559 unsigned int old_data_start;
4560 unsigned int old_size;
4561 unsigned int size_diff;
4562 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004563 struct btrfs_map_token token;
4564
4565 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004566
Chris Mason5f39d392007-10-15 16:14:19 -04004567 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004568 slot = path->slots[0];
4569
4570 old_size = btrfs_item_size_nr(leaf, slot);
4571 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004572 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004573
Chris Mason5f39d392007-10-15 16:14:19 -04004574 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004575 data_end = leaf_data_end(root, leaf);
4576
Chris Mason5f39d392007-10-15 16:14:19 -04004577 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004578
Chris Masonb18c6682007-04-17 13:26:50 -04004579 size_diff = old_size - new_size;
4580
4581 BUG_ON(slot < 0);
4582 BUG_ON(slot >= nritems);
4583
4584 /*
4585 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4586 */
4587 /* first correct the data pointers */
4588 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004589 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004590 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004591
Chris Masoncfed81a2012-03-03 07:40:03 -05004592 ioff = btrfs_token_item_offset(leaf, item, &token);
4593 btrfs_set_token_item_offset(leaf, item,
4594 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004595 }
Chris Masondb945352007-10-15 16:15:53 -04004596
Chris Masonb18c6682007-04-17 13:26:50 -04004597 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004598 if (from_end) {
4599 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4600 data_end + size_diff, btrfs_leaf_data(leaf) +
4601 data_end, old_data_start + new_size - data_end);
4602 } else {
4603 struct btrfs_disk_key disk_key;
4604 u64 offset;
4605
4606 btrfs_item_key(leaf, &disk_key, slot);
4607
4608 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4609 unsigned long ptr;
4610 struct btrfs_file_extent_item *fi;
4611
4612 fi = btrfs_item_ptr(leaf, slot,
4613 struct btrfs_file_extent_item);
4614 fi = (struct btrfs_file_extent_item *)(
4615 (unsigned long)fi - size_diff);
4616
4617 if (btrfs_file_extent_type(leaf, fi) ==
4618 BTRFS_FILE_EXTENT_INLINE) {
4619 ptr = btrfs_item_ptr_offset(leaf, slot);
4620 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004621 (unsigned long)fi,
4622 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004623 disk_bytenr));
4624 }
4625 }
4626
4627 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4628 data_end + size_diff, btrfs_leaf_data(leaf) +
4629 data_end, old_data_start - data_end);
4630
4631 offset = btrfs_disk_key_offset(&disk_key);
4632 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4633 btrfs_set_item_key(leaf, &disk_key, slot);
4634 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004635 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004636 }
Chris Mason5f39d392007-10-15 16:14:19 -04004637
Ross Kirkdd3cc162013-09-16 15:58:09 +01004638 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004639 btrfs_set_item_size(leaf, item, new_size);
4640 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 if (btrfs_leaf_free_space(root, leaf) < 0) {
4643 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004644 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004645 }
Chris Masonb18c6682007-04-17 13:26:50 -04004646}
4647
Chris Masond352ac62008-09-29 15:18:18 -04004648/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004649 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004650 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004651void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004652 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004653{
Chris Mason6567e832007-04-16 09:22:45 -04004654 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004655 struct extent_buffer *leaf;
4656 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004657 u32 nritems;
4658 unsigned int data_end;
4659 unsigned int old_data;
4660 unsigned int old_size;
4661 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004662 struct btrfs_map_token token;
4663
4664 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004665
Chris Mason5f39d392007-10-15 16:14:19 -04004666 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004667
Chris Mason5f39d392007-10-15 16:14:19 -04004668 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004669 data_end = leaf_data_end(root, leaf);
4670
Chris Mason5f39d392007-10-15 16:14:19 -04004671 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4672 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004673 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004674 }
Chris Mason6567e832007-04-16 09:22:45 -04004675 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004676 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004677
4678 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004679 if (slot >= nritems) {
4680 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004681 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004682 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004683 BUG_ON(1);
4684 }
Chris Mason6567e832007-04-16 09:22:45 -04004685
4686 /*
4687 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4688 */
4689 /* first correct the data pointers */
4690 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004691 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004692 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004693
Chris Masoncfed81a2012-03-03 07:40:03 -05004694 ioff = btrfs_token_item_offset(leaf, item, &token);
4695 btrfs_set_token_item_offset(leaf, item,
4696 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004697 }
Chris Mason5f39d392007-10-15 16:14:19 -04004698
Chris Mason6567e832007-04-16 09:22:45 -04004699 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004700 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004701 data_end - data_size, btrfs_leaf_data(leaf) +
4702 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004703
Chris Mason6567e832007-04-16 09:22:45 -04004704 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004705 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004706 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004707 btrfs_set_item_size(leaf, item, old_size + data_size);
4708 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004709
Chris Mason5f39d392007-10-15 16:14:19 -04004710 if (btrfs_leaf_free_space(root, leaf) < 0) {
4711 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004712 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004713 }
Chris Mason6567e832007-04-16 09:22:45 -04004714}
4715
Chris Mason74123bd2007-02-02 11:05:29 -05004716/*
Chris Mason44871b12009-03-13 10:04:31 -04004717 * this is a helper for btrfs_insert_empty_items, the main goal here is
4718 * to save stack depth by doing the bulk of the work in a function
4719 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004720 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004721void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004722 struct btrfs_key *cpu_key, u32 *data_size,
4723 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004724{
Chris Mason5f39d392007-10-15 16:14:19 -04004725 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004726 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004727 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004728 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004729 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004730 struct extent_buffer *leaf;
4731 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004732 struct btrfs_map_token token;
4733
Filipe Manana24cdc842014-07-28 19:34:35 +01004734 if (path->slots[0] == 0) {
4735 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4736 fixup_low_keys(root, path, &disk_key, 1);
4737 }
4738 btrfs_unlock_up_safe(path, 1);
4739
Chris Masoncfed81a2012-03-03 07:40:03 -05004740 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004741
Chris Mason5f39d392007-10-15 16:14:19 -04004742 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004743 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004744
Chris Mason5f39d392007-10-15 16:14:19 -04004745 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004746 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004747
Chris Masonf25956c2008-09-12 15:32:53 -04004748 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004749 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004750 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
Chris Mason9c583092008-01-29 15:15:18 -05004751 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004752 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004753 }
Chris Mason5f39d392007-10-15 16:14:19 -04004754
Chris Masonbe0e5c02007-01-26 15:51:26 -05004755 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004756 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004757
Chris Mason5f39d392007-10-15 16:14:19 -04004758 if (old_data < data_end) {
4759 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004760 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
Chris Mason5f39d392007-10-15 16:14:19 -04004761 slot, old_data, data_end);
4762 BUG_ON(1);
4763 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004764 /*
4765 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4766 */
4767 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004768 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004769 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004770
Ross Kirkdd3cc162013-09-16 15:58:09 +01004771 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004772 ioff = btrfs_token_item_offset(leaf, item, &token);
4773 btrfs_set_token_item_offset(leaf, item,
4774 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004775 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004776 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004777 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004778 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004779 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004780
4781 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004782 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004783 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004784 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004785 data_end = old_data;
4786 }
Chris Mason5f39d392007-10-15 16:14:19 -04004787
Chris Mason62e27492007-03-15 12:56:47 -04004788 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004789 for (i = 0; i < nr; i++) {
4790 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4791 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004792 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004793 btrfs_set_token_item_offset(leaf, item,
4794 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004795 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004796 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004797 }
Chris Mason44871b12009-03-13 10:04:31 -04004798
Chris Mason9c583092008-01-29 15:15:18 -05004799 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004800 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004801
Chris Mason5f39d392007-10-15 16:14:19 -04004802 if (btrfs_leaf_free_space(root, leaf) < 0) {
4803 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004804 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004805 }
Chris Mason44871b12009-03-13 10:04:31 -04004806}
4807
4808/*
4809 * Given a key and some data, insert items into the tree.
4810 * This does all the path init required, making room in the tree if needed.
4811 */
4812int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4813 struct btrfs_root *root,
4814 struct btrfs_path *path,
4815 struct btrfs_key *cpu_key, u32 *data_size,
4816 int nr)
4817{
Chris Mason44871b12009-03-13 10:04:31 -04004818 int ret = 0;
4819 int slot;
4820 int i;
4821 u32 total_size = 0;
4822 u32 total_data = 0;
4823
4824 for (i = 0; i < nr; i++)
4825 total_data += data_size[i];
4826
4827 total_size = total_data + (nr * sizeof(struct btrfs_item));
4828 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4829 if (ret == 0)
4830 return -EEXIST;
4831 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004832 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004833
Chris Mason44871b12009-03-13 10:04:31 -04004834 slot = path->slots[0];
4835 BUG_ON(slot < 0);
4836
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004837 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004838 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004839 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004840}
4841
4842/*
4843 * Given a key and some data, insert an item into the tree.
4844 * This does all the path init required, making room in the tree if needed.
4845 */
Chris Masone089f052007-03-16 16:20:31 -04004846int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4847 *root, struct btrfs_key *cpu_key, void *data, u32
4848 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004849{
4850 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004851 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004852 struct extent_buffer *leaf;
4853 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004854
Chris Mason2c90e5d2007-04-02 10:50:19 -04004855 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004856 if (!path)
4857 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004858 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004859 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004860 leaf = path->nodes[0];
4861 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4862 write_extent_buffer(leaf, data, ptr, data_size);
4863 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004864 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004865 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004866 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004867}
4868
Chris Mason74123bd2007-02-02 11:05:29 -05004869/*
Chris Mason5de08d72007-02-24 06:24:44 -05004870 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004871 *
Chris Masond352ac62008-09-29 15:18:18 -04004872 * the tree should have been previously balanced so the deletion does not
4873 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004874 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004875static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4876 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004877{
Chris Mason5f39d392007-10-15 16:14:19 -04004878 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004879 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004880 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004881
Chris Mason5f39d392007-10-15 16:14:19 -04004882 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004883 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004884 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004885 tree_mod_log_eb_move(root->fs_info, parent, slot,
4886 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004887 memmove_extent_buffer(parent,
4888 btrfs_node_key_ptr_offset(slot),
4889 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004890 sizeof(struct btrfs_key_ptr) *
4891 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004892 } else if (level) {
4893 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004894 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004895 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004896 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004897
Chris Mason7518a232007-03-12 12:01:18 -04004898 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004899 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004900 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004901 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004902 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004903 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004904 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004905 struct btrfs_disk_key disk_key;
4906
4907 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004908 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004909 }
Chris Masond6025572007-03-30 14:27:56 -04004910 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004911}
4912
Chris Mason74123bd2007-02-02 11:05:29 -05004913/*
Chris Mason323ac952008-10-01 19:05:46 -04004914 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004915 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004916 *
4917 * This deletes the pointer in path->nodes[1] and frees the leaf
4918 * block extent. zero is returned if it all worked out, < 0 otherwise.
4919 *
4920 * The path must have already been setup for deleting the leaf, including
4921 * all the proper balancing. path->nodes[1] must be locked.
4922 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004923static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4924 struct btrfs_root *root,
4925 struct btrfs_path *path,
4926 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004927{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004928 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004929 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004930
Chris Mason4d081c42009-02-04 09:31:28 -05004931 /*
4932 * btrfs_free_extent is expensive, we want to make sure we
4933 * aren't holding any locks when we call it
4934 */
4935 btrfs_unlock_up_safe(path, 0);
4936
Yan, Zhengf0486c62010-05-16 10:46:25 -04004937 root_sub_used(root, leaf->len);
4938
Josef Bacik3083ee22012-03-09 16:01:49 -05004939 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004940 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004941 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004942}
4943/*
Chris Mason74123bd2007-02-02 11:05:29 -05004944 * delete the item at the leaf level in path. If that empties
4945 * the leaf, remove it from the tree
4946 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004947int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4948 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004949{
Chris Mason5f39d392007-10-15 16:14:19 -04004950 struct extent_buffer *leaf;
4951 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004952 int last_off;
4953 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004954 int ret = 0;
4955 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004956 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004957 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004958 struct btrfs_map_token token;
4959
4960 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004961
Chris Mason5f39d392007-10-15 16:14:19 -04004962 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004963 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4964
4965 for (i = 0; i < nr; i++)
4966 dsize += btrfs_item_size_nr(leaf, slot + i);
4967
Chris Mason5f39d392007-10-15 16:14:19 -04004968 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004969
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004971 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004972
4973 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004974 data_end + dsize,
4975 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004976 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004977
Chris Mason85e21ba2008-01-29 15:11:36 -05004978 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004979 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004980
Ross Kirkdd3cc162013-09-16 15:58:09 +01004981 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004982 ioff = btrfs_token_item_offset(leaf, item, &token);
4983 btrfs_set_token_item_offset(leaf, item,
4984 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004985 }
Chris Masondb945352007-10-15 16:15:53 -04004986
Chris Mason5f39d392007-10-15 16:14:19 -04004987 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004988 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004989 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004990 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004991 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004992 btrfs_set_header_nritems(leaf, nritems - nr);
4993 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004994
Chris Mason74123bd2007-02-02 11:05:29 -05004995 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004996 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004997 if (leaf == root->node) {
4998 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004999 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005000 btrfs_set_path_blocking(path);
5001 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005002 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005003 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005004 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005005 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005006 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005007 struct btrfs_disk_key disk_key;
5008
5009 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00005010 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005011 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005012
Chris Mason74123bd2007-02-02 11:05:29 -05005013 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005014 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005015 /* push_leaf_left fixes the path.
5016 * make sure the path still points to our leaf
5017 * for possible call to del_ptr below
5018 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005019 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005020 extent_buffer_get(leaf);
5021
Chris Masonb9473432009-03-13 11:00:37 -04005022 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005023 wret = push_leaf_left(trans, root, path, 1, 1,
5024 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005025 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005026 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005027
5028 if (path->nodes[0] == leaf &&
5029 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005030 wret = push_leaf_right(trans, root, path, 1,
5031 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005032 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005033 ret = wret;
5034 }
Chris Mason5f39d392007-10-15 16:14:19 -04005035
5036 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005037 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005038 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005039 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005040 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005041 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005042 /* if we're still in the path, make sure
5043 * we're dirty. Otherwise, one of the
5044 * push_leaf functions must have already
5045 * dirtied this buffer
5046 */
5047 if (path->nodes[0] == leaf)
5048 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005049 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005050 }
Chris Masond5719762007-03-23 10:01:08 -04005051 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005052 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005053 }
5054 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005055 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005056}
5057
Chris Mason97571fd2007-02-24 13:39:08 -05005058/*
Chris Mason925baed2008-06-25 16:01:30 -04005059 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005060 * returns 0 if it found something or 1 if there are no lesser leaves.
5061 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005062 *
5063 * This may release the path, and so you may lose any locks held at the
5064 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005065 */
Josef Bacik16e75492013-10-22 12:18:51 -04005066int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005067{
Chris Mason925baed2008-06-25 16:01:30 -04005068 struct btrfs_key key;
5069 struct btrfs_disk_key found_key;
5070 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005071
Chris Mason925baed2008-06-25 16:01:30 -04005072 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005073
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005074 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005075 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005076 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005077 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005078 key.offset = (u64)-1;
5079 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005080 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005081 key.type = (u8)-1;
5082 key.offset = (u64)-1;
5083 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005084 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005085 }
Chris Mason7bb86312007-12-11 09:25:06 -05005086
David Sterbab3b4aa72011-04-21 01:20:15 +02005087 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005088 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5089 if (ret < 0)
5090 return ret;
5091 btrfs_item_key(path->nodes[0], &found_key, 0);
5092 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005093 /*
5094 * We might have had an item with the previous key in the tree right
5095 * before we released our path. And after we released our path, that
5096 * item might have been pushed to the first slot (0) of the leaf we
5097 * were holding due to a tree balance. Alternatively, an item with the
5098 * previous key can exist as the only element of a leaf (big fat item).
5099 * Therefore account for these 2 cases, so that our callers (like
5100 * btrfs_previous_item) don't miss an existing item with a key matching
5101 * the previous key we computed above.
5102 */
5103 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005104 return 0;
5105 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005106}
5107
Chris Mason3f157a22008-06-25 16:01:31 -04005108/*
5109 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005110 * for nodes or leaves that are have a minimum transaction id.
5111 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005112 *
5113 * This does not cow, but it does stuff the starting key it finds back
5114 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5115 * key and get a writable path.
5116 *
5117 * This does lock as it descends, and path->keep_locks should be set
5118 * to 1 by the caller.
5119 *
5120 * This honors path->lowest_level to prevent descent past a given level
5121 * of the tree.
5122 *
Chris Masond352ac62008-09-29 15:18:18 -04005123 * min_trans indicates the oldest transaction that you are interested
5124 * in walking through. Any nodes or leaves older than min_trans are
5125 * skipped over (without reading them).
5126 *
Chris Mason3f157a22008-06-25 16:01:31 -04005127 * returns zero if something useful was found, < 0 on error and 1 if there
5128 * was nothing in the tree that matched the search criteria.
5129 */
5130int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005131 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005132 u64 min_trans)
5133{
5134 struct extent_buffer *cur;
5135 struct btrfs_key found_key;
5136 int slot;
Yan96524802008-07-24 12:19:49 -04005137 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005138 u32 nritems;
5139 int level;
5140 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005141 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005142
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005143 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005144again:
Chris Masonbd681512011-07-16 15:23:14 -04005145 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005146 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005147 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005148 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005149 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005150
5151 if (btrfs_header_generation(cur) < min_trans) {
5152 ret = 1;
5153 goto out;
5154 }
Chris Masond3977122009-01-05 21:25:51 -05005155 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005156 nritems = btrfs_header_nritems(cur);
5157 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005158 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005159
Chris Mason323ac952008-10-01 19:05:46 -04005160 /* at the lowest level, we're done, setup the path and exit */
5161 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005162 if (slot >= nritems)
5163 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005164 ret = 0;
5165 path->slots[level] = slot;
5166 btrfs_item_key_to_cpu(cur, &found_key, slot);
5167 goto out;
5168 }
Yan96524802008-07-24 12:19:49 -04005169 if (sret && slot > 0)
5170 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005171 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005172 * check this node pointer against the min_trans parameters.
5173 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005174 */
Chris Masond3977122009-01-05 21:25:51 -05005175 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005176 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005177
Chris Mason3f157a22008-06-25 16:01:31 -04005178 gen = btrfs_node_ptr_generation(cur, slot);
5179 if (gen < min_trans) {
5180 slot++;
5181 continue;
5182 }
Eric Sandeende78b512013-01-31 18:21:12 +00005183 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005184 }
Chris Masone02119d2008-09-05 16:13:11 -04005185find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005186 /*
5187 * we didn't find a candidate key in this node, walk forward
5188 * and find another one
5189 */
5190 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005191 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005192 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005193 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005194 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005195 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005196 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005197 goto again;
5198 } else {
5199 goto out;
5200 }
5201 }
5202 /* save our key for returning back */
5203 btrfs_node_key_to_cpu(cur, &found_key, slot);
5204 path->slots[level] = slot;
5205 if (level == path->lowest_level) {
5206 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005207 goto out;
5208 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005209 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005210 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005211 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005212
Chris Masonbd681512011-07-16 15:23:14 -04005213 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005214
Chris Masonbd681512011-07-16 15:23:14 -04005215 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005216 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005217 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005218 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005219 }
5220out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005221 path->keep_locks = keep_locks;
5222 if (ret == 0) {
5223 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5224 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005225 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005226 }
Chris Mason3f157a22008-06-25 16:01:31 -04005227 return ret;
5228}
5229
Alexander Block70698302012-06-05 21:07:48 +02005230static void tree_move_down(struct btrfs_root *root,
5231 struct btrfs_path *path,
5232 int *level, int root_level)
5233{
Chris Mason74dd17f2012-08-07 16:25:13 -04005234 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005235 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5236 path->slots[*level]);
5237 path->slots[*level - 1] = 0;
5238 (*level)--;
5239}
5240
5241static int tree_move_next_or_upnext(struct btrfs_root *root,
5242 struct btrfs_path *path,
5243 int *level, int root_level)
5244{
5245 int ret = 0;
5246 int nritems;
5247 nritems = btrfs_header_nritems(path->nodes[*level]);
5248
5249 path->slots[*level]++;
5250
Chris Mason74dd17f2012-08-07 16:25:13 -04005251 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005252 if (*level == root_level)
5253 return -1;
5254
5255 /* move upnext */
5256 path->slots[*level] = 0;
5257 free_extent_buffer(path->nodes[*level]);
5258 path->nodes[*level] = NULL;
5259 (*level)++;
5260 path->slots[*level]++;
5261
5262 nritems = btrfs_header_nritems(path->nodes[*level]);
5263 ret = 1;
5264 }
5265 return ret;
5266}
5267
5268/*
5269 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5270 * or down.
5271 */
5272static int tree_advance(struct btrfs_root *root,
5273 struct btrfs_path *path,
5274 int *level, int root_level,
5275 int allow_down,
5276 struct btrfs_key *key)
5277{
5278 int ret;
5279
5280 if (*level == 0 || !allow_down) {
5281 ret = tree_move_next_or_upnext(root, path, level, root_level);
5282 } else {
5283 tree_move_down(root, path, level, root_level);
5284 ret = 0;
5285 }
5286 if (ret >= 0) {
5287 if (*level == 0)
5288 btrfs_item_key_to_cpu(path->nodes[*level], key,
5289 path->slots[*level]);
5290 else
5291 btrfs_node_key_to_cpu(path->nodes[*level], key,
5292 path->slots[*level]);
5293 }
5294 return ret;
5295}
5296
5297static int tree_compare_item(struct btrfs_root *left_root,
5298 struct btrfs_path *left_path,
5299 struct btrfs_path *right_path,
5300 char *tmp_buf)
5301{
5302 int cmp;
5303 int len1, len2;
5304 unsigned long off1, off2;
5305
5306 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5307 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5308 if (len1 != len2)
5309 return 1;
5310
5311 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5312 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5313 right_path->slots[0]);
5314
5315 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5316
5317 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5318 if (cmp)
5319 return 1;
5320 return 0;
5321}
5322
5323#define ADVANCE 1
5324#define ADVANCE_ONLY_NEXT -1
5325
5326/*
5327 * This function compares two trees and calls the provided callback for
5328 * every changed/new/deleted item it finds.
5329 * If shared tree blocks are encountered, whole subtrees are skipped, making
5330 * the compare pretty fast on snapshotted subvolumes.
5331 *
5332 * This currently works on commit roots only. As commit roots are read only,
5333 * we don't do any locking. The commit roots are protected with transactions.
5334 * Transactions are ended and rejoined when a commit is tried in between.
5335 *
5336 * This function checks for modifications done to the trees while comparing.
5337 * If it detects a change, it aborts immediately.
5338 */
5339int btrfs_compare_trees(struct btrfs_root *left_root,
5340 struct btrfs_root *right_root,
5341 btrfs_changed_cb_t changed_cb, void *ctx)
5342{
5343 int ret;
5344 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005345 struct btrfs_path *left_path = NULL;
5346 struct btrfs_path *right_path = NULL;
5347 struct btrfs_key left_key;
5348 struct btrfs_key right_key;
5349 char *tmp_buf = NULL;
5350 int left_root_level;
5351 int right_root_level;
5352 int left_level;
5353 int right_level;
5354 int left_end_reached;
5355 int right_end_reached;
5356 int advance_left;
5357 int advance_right;
5358 u64 left_blockptr;
5359 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005360 u64 left_gen;
5361 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005362
5363 left_path = btrfs_alloc_path();
5364 if (!left_path) {
5365 ret = -ENOMEM;
5366 goto out;
5367 }
5368 right_path = btrfs_alloc_path();
5369 if (!right_path) {
5370 ret = -ENOMEM;
5371 goto out;
5372 }
5373
David Sterba707e8a02014-06-04 19:22:26 +02005374 tmp_buf = kmalloc(left_root->nodesize, GFP_NOFS);
Alexander Block70698302012-06-05 21:07:48 +02005375 if (!tmp_buf) {
5376 ret = -ENOMEM;
5377 goto out;
5378 }
5379
5380 left_path->search_commit_root = 1;
5381 left_path->skip_locking = 1;
5382 right_path->search_commit_root = 1;
5383 right_path->skip_locking = 1;
5384
Alexander Block70698302012-06-05 21:07:48 +02005385 /*
5386 * Strategy: Go to the first items of both trees. Then do
5387 *
5388 * If both trees are at level 0
5389 * Compare keys of current items
5390 * If left < right treat left item as new, advance left tree
5391 * and repeat
5392 * If left > right treat right item as deleted, advance right tree
5393 * and repeat
5394 * If left == right do deep compare of items, treat as changed if
5395 * needed, advance both trees and repeat
5396 * If both trees are at the same level but not at level 0
5397 * Compare keys of current nodes/leafs
5398 * If left < right advance left tree and repeat
5399 * If left > right advance right tree and repeat
5400 * If left == right compare blockptrs of the next nodes/leafs
5401 * If they match advance both trees but stay at the same level
5402 * and repeat
5403 * If they don't match advance both trees while allowing to go
5404 * deeper and repeat
5405 * If tree levels are different
5406 * Advance the tree that needs it and repeat
5407 *
5408 * Advancing a tree means:
5409 * If we are at level 0, try to go to the next slot. If that's not
5410 * possible, go one level up and repeat. Stop when we found a level
5411 * where we could go to the next slot. We may at this point be on a
5412 * node or a leaf.
5413 *
5414 * If we are not at level 0 and not on shared tree blocks, go one
5415 * level deeper.
5416 *
5417 * If we are not at level 0 and on shared tree blocks, go one slot to
5418 * the right if possible or go up and right.
5419 */
5420
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005421 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005422 left_level = btrfs_header_level(left_root->commit_root);
5423 left_root_level = left_level;
5424 left_path->nodes[left_level] = left_root->commit_root;
5425 extent_buffer_get(left_path->nodes[left_level]);
5426
5427 right_level = btrfs_header_level(right_root->commit_root);
5428 right_root_level = right_level;
5429 right_path->nodes[right_level] = right_root->commit_root;
5430 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005431 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005432
5433 if (left_level == 0)
5434 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5435 &left_key, left_path->slots[left_level]);
5436 else
5437 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5438 &left_key, left_path->slots[left_level]);
5439 if (right_level == 0)
5440 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5441 &right_key, right_path->slots[right_level]);
5442 else
5443 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5444 &right_key, right_path->slots[right_level]);
5445
5446 left_end_reached = right_end_reached = 0;
5447 advance_left = advance_right = 0;
5448
5449 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005450 if (advance_left && !left_end_reached) {
5451 ret = tree_advance(left_root, left_path, &left_level,
5452 left_root_level,
5453 advance_left != ADVANCE_ONLY_NEXT,
5454 &left_key);
5455 if (ret < 0)
5456 left_end_reached = ADVANCE;
5457 advance_left = 0;
5458 }
5459 if (advance_right && !right_end_reached) {
5460 ret = tree_advance(right_root, right_path, &right_level,
5461 right_root_level,
5462 advance_right != ADVANCE_ONLY_NEXT,
5463 &right_key);
5464 if (ret < 0)
5465 right_end_reached = ADVANCE;
5466 advance_right = 0;
5467 }
5468
5469 if (left_end_reached && right_end_reached) {
5470 ret = 0;
5471 goto out;
5472 } else if (left_end_reached) {
5473 if (right_level == 0) {
5474 ret = changed_cb(left_root, right_root,
5475 left_path, right_path,
5476 &right_key,
5477 BTRFS_COMPARE_TREE_DELETED,
5478 ctx);
5479 if (ret < 0)
5480 goto out;
5481 }
5482 advance_right = ADVANCE;
5483 continue;
5484 } else if (right_end_reached) {
5485 if (left_level == 0) {
5486 ret = changed_cb(left_root, right_root,
5487 left_path, right_path,
5488 &left_key,
5489 BTRFS_COMPARE_TREE_NEW,
5490 ctx);
5491 if (ret < 0)
5492 goto out;
5493 }
5494 advance_left = ADVANCE;
5495 continue;
5496 }
5497
5498 if (left_level == 0 && right_level == 0) {
5499 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5500 if (cmp < 0) {
5501 ret = changed_cb(left_root, right_root,
5502 left_path, right_path,
5503 &left_key,
5504 BTRFS_COMPARE_TREE_NEW,
5505 ctx);
5506 if (ret < 0)
5507 goto out;
5508 advance_left = ADVANCE;
5509 } else if (cmp > 0) {
5510 ret = changed_cb(left_root, right_root,
5511 left_path, right_path,
5512 &right_key,
5513 BTRFS_COMPARE_TREE_DELETED,
5514 ctx);
5515 if (ret < 0)
5516 goto out;
5517 advance_right = ADVANCE;
5518 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005519 enum btrfs_compare_tree_result cmp;
5520
Chris Mason74dd17f2012-08-07 16:25:13 -04005521 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005522 ret = tree_compare_item(left_root, left_path,
5523 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005524 if (ret)
5525 cmp = BTRFS_COMPARE_TREE_CHANGED;
5526 else
5527 cmp = BTRFS_COMPARE_TREE_SAME;
5528 ret = changed_cb(left_root, right_root,
5529 left_path, right_path,
5530 &left_key, cmp, ctx);
5531 if (ret < 0)
5532 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005533 advance_left = ADVANCE;
5534 advance_right = ADVANCE;
5535 }
5536 } else if (left_level == right_level) {
5537 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5538 if (cmp < 0) {
5539 advance_left = ADVANCE;
5540 } else if (cmp > 0) {
5541 advance_right = ADVANCE;
5542 } else {
5543 left_blockptr = btrfs_node_blockptr(
5544 left_path->nodes[left_level],
5545 left_path->slots[left_level]);
5546 right_blockptr = btrfs_node_blockptr(
5547 right_path->nodes[right_level],
5548 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005549 left_gen = btrfs_node_ptr_generation(
5550 left_path->nodes[left_level],
5551 left_path->slots[left_level]);
5552 right_gen = btrfs_node_ptr_generation(
5553 right_path->nodes[right_level],
5554 right_path->slots[right_level]);
5555 if (left_blockptr == right_blockptr &&
5556 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005557 /*
5558 * As we're on a shared block, don't
5559 * allow to go deeper.
5560 */
5561 advance_left = ADVANCE_ONLY_NEXT;
5562 advance_right = ADVANCE_ONLY_NEXT;
5563 } else {
5564 advance_left = ADVANCE;
5565 advance_right = ADVANCE;
5566 }
5567 }
5568 } else if (left_level < right_level) {
5569 advance_right = ADVANCE;
5570 } else {
5571 advance_left = ADVANCE;
5572 }
5573 }
5574
5575out:
5576 btrfs_free_path(left_path);
5577 btrfs_free_path(right_path);
5578 kfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005579 return ret;
5580}
5581
Chris Mason3f157a22008-06-25 16:01:31 -04005582/*
5583 * this is similar to btrfs_next_leaf, but does not try to preserve
5584 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005585 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005586 *
5587 * 0 is returned if another key is found, < 0 if there are any errors
5588 * and 1 is returned if there are no higher keys in the tree
5589 *
5590 * path->keep_locks should be set to 1 on the search made before
5591 * calling this function.
5592 */
Chris Masone7a84562008-06-25 16:01:31 -04005593int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005594 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005595{
Chris Masone7a84562008-06-25 16:01:31 -04005596 int slot;
5597 struct extent_buffer *c;
5598
Chris Mason934d3752008-12-08 16:43:10 -05005599 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005600 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005601 if (!path->nodes[level])
5602 return 1;
5603
5604 slot = path->slots[level] + 1;
5605 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005606next:
Chris Masone7a84562008-06-25 16:01:31 -04005607 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005608 int ret;
5609 int orig_lowest;
5610 struct btrfs_key cur_key;
5611 if (level + 1 >= BTRFS_MAX_LEVEL ||
5612 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005613 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005614
5615 if (path->locks[level + 1]) {
5616 level++;
5617 continue;
5618 }
5619
5620 slot = btrfs_header_nritems(c) - 1;
5621 if (level == 0)
5622 btrfs_item_key_to_cpu(c, &cur_key, slot);
5623 else
5624 btrfs_node_key_to_cpu(c, &cur_key, slot);
5625
5626 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005627 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005628 path->lowest_level = level;
5629 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5630 0, 0);
5631 path->lowest_level = orig_lowest;
5632 if (ret < 0)
5633 return ret;
5634
5635 c = path->nodes[level];
5636 slot = path->slots[level];
5637 if (ret == 0)
5638 slot++;
5639 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005640 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005641
Chris Masone7a84562008-06-25 16:01:31 -04005642 if (level == 0)
5643 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005644 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005645 u64 gen = btrfs_node_ptr_generation(c, slot);
5646
Chris Mason3f157a22008-06-25 16:01:31 -04005647 if (gen < min_trans) {
5648 slot++;
5649 goto next;
5650 }
Chris Masone7a84562008-06-25 16:01:31 -04005651 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005652 }
Chris Masone7a84562008-06-25 16:01:31 -04005653 return 0;
5654 }
5655 return 1;
5656}
5657
Chris Mason7bb86312007-12-11 09:25:06 -05005658/*
Chris Mason925baed2008-06-25 16:01:30 -04005659 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005660 * returns 0 if it found something or 1 if there are no greater leaves.
5661 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005662 */
Chris Mason234b63a2007-03-13 10:46:10 -04005663int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005664{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005665 return btrfs_next_old_leaf(root, path, 0);
5666}
5667
5668int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5669 u64 time_seq)
5670{
Chris Masond97e63b2007-02-20 16:40:44 -05005671 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005672 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005673 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005674 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005675 struct btrfs_key key;
5676 u32 nritems;
5677 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005678 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005679 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005680
5681 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005682 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005683 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005684
Chris Mason8e73f272009-04-03 10:14:18 -04005685 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5686again:
5687 level = 1;
5688 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005689 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005690 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005691
Chris Masona2135012008-06-25 16:01:30 -04005692 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005693 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005694
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005695 if (time_seq)
5696 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5697 else
5698 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005699 path->keep_locks = 0;
5700
5701 if (ret < 0)
5702 return ret;
5703
Chris Masona2135012008-06-25 16:01:30 -04005704 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005705 /*
5706 * by releasing the path above we dropped all our locks. A balance
5707 * could have added more items next to the key that used to be
5708 * at the very end of the block. So, check again here and
5709 * advance the path if there are now more items available.
5710 */
Chris Masona2135012008-06-25 16:01:30 -04005711 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005712 if (ret == 0)
5713 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005714 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005715 goto done;
5716 }
Liu Bo0b43e042014-06-09 11:04:49 +08005717 /*
5718 * So the above check misses one case:
5719 * - after releasing the path above, someone has removed the item that
5720 * used to be at the very end of the block, and balance between leafs
5721 * gets another one with bigger key.offset to replace it.
5722 *
5723 * This one should be returned as well, or we can get leaf corruption
5724 * later(esp. in __btrfs_drop_extents()).
5725 *
5726 * And a bit more explanation about this check,
5727 * with ret > 0, the key isn't found, the path points to the slot
5728 * where it should be inserted, so the path->slots[0] item must be the
5729 * bigger one.
5730 */
5731 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5732 ret = 0;
5733 goto done;
5734 }
Chris Masond97e63b2007-02-20 16:40:44 -05005735
Chris Masond3977122009-01-05 21:25:51 -05005736 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005737 if (!path->nodes[level]) {
5738 ret = 1;
5739 goto done;
5740 }
Chris Mason5f39d392007-10-15 16:14:19 -04005741
Chris Masond97e63b2007-02-20 16:40:44 -05005742 slot = path->slots[level] + 1;
5743 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005744 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005745 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005746 if (level == BTRFS_MAX_LEVEL) {
5747 ret = 1;
5748 goto done;
5749 }
Chris Masond97e63b2007-02-20 16:40:44 -05005750 continue;
5751 }
Chris Mason5f39d392007-10-15 16:14:19 -04005752
Chris Mason925baed2008-06-25 16:01:30 -04005753 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005754 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005755 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005756 }
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Chris Mason8e73f272009-04-03 10:14:18 -04005758 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005759 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005760 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005761 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005762 if (ret == -EAGAIN)
5763 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005764
Chris Mason76a05b32009-05-14 13:24:30 -04005765 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005766 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005767 goto done;
5768 }
5769
Chris Mason5cd57b22008-06-25 16:01:30 -04005770 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005771 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005772 if (!ret && time_seq) {
5773 /*
5774 * If we don't get the lock, we may be racing
5775 * with push_leaf_left, holding that lock while
5776 * itself waiting for the leaf we've currently
5777 * locked. To solve this situation, we give up
5778 * on our lock and cycle.
5779 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005780 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005781 btrfs_release_path(path);
5782 cond_resched();
5783 goto again;
5784 }
Chris Mason8e73f272009-04-03 10:14:18 -04005785 if (!ret) {
5786 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005787 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005788 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005789 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005790 }
Chris Mason31533fb2011-07-26 16:01:59 -04005791 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005792 }
Chris Masond97e63b2007-02-20 16:40:44 -05005793 break;
5794 }
5795 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005796 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005797 level--;
5798 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005799 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005800 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005801
Chris Mason5f39d392007-10-15 16:14:19 -04005802 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005803 path->nodes[level] = next;
5804 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005805 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005806 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005807 if (!level)
5808 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005809
Chris Mason8e73f272009-04-03 10:14:18 -04005810 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005811 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005812 if (ret == -EAGAIN)
5813 goto again;
5814
Chris Mason76a05b32009-05-14 13:24:30 -04005815 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005816 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005817 goto done;
5818 }
5819
Chris Mason5cd57b22008-06-25 16:01:30 -04005820 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005821 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005822 if (!ret) {
5823 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005824 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005825 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005826 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005827 }
Chris Mason31533fb2011-07-26 16:01:59 -04005828 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005829 }
Chris Masond97e63b2007-02-20 16:40:44 -05005830 }
Chris Mason8e73f272009-04-03 10:14:18 -04005831 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005832done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005833 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005834 path->leave_spinning = old_spinning;
5835 if (!old_spinning)
5836 btrfs_set_path_blocking(path);
5837
5838 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005839}
Chris Mason0b86a832008-03-24 15:01:56 -04005840
Chris Mason3f157a22008-06-25 16:01:31 -04005841/*
5842 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5843 * searching until it gets past min_objectid or finds an item of 'type'
5844 *
5845 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5846 */
Chris Mason0b86a832008-03-24 15:01:56 -04005847int btrfs_previous_item(struct btrfs_root *root,
5848 struct btrfs_path *path, u64 min_objectid,
5849 int type)
5850{
5851 struct btrfs_key found_key;
5852 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005853 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005854 int ret;
5855
Chris Masond3977122009-01-05 21:25:51 -05005856 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005857 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005858 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005859 ret = btrfs_prev_leaf(root, path);
5860 if (ret != 0)
5861 return ret;
5862 } else {
5863 path->slots[0]--;
5864 }
5865 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005866 nritems = btrfs_header_nritems(leaf);
5867 if (nritems == 0)
5868 return 1;
5869 if (path->slots[0] == nritems)
5870 path->slots[0]--;
5871
Chris Mason0b86a832008-03-24 15:01:56 -04005872 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005873 if (found_key.objectid < min_objectid)
5874 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005875 if (found_key.type == type)
5876 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005877 if (found_key.objectid == min_objectid &&
5878 found_key.type < type)
5879 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005880 }
5881 return 1;
5882}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005883
5884/*
5885 * search in extent tree to find a previous Metadata/Data extent item with
5886 * min objecitd.
5887 *
5888 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5889 */
5890int btrfs_previous_extent_item(struct btrfs_root *root,
5891 struct btrfs_path *path, u64 min_objectid)
5892{
5893 struct btrfs_key found_key;
5894 struct extent_buffer *leaf;
5895 u32 nritems;
5896 int ret;
5897
5898 while (1) {
5899 if (path->slots[0] == 0) {
5900 btrfs_set_path_blocking(path);
5901 ret = btrfs_prev_leaf(root, path);
5902 if (ret != 0)
5903 return ret;
5904 } else {
5905 path->slots[0]--;
5906 }
5907 leaf = path->nodes[0];
5908 nritems = btrfs_header_nritems(leaf);
5909 if (nritems == 0)
5910 return 1;
5911 if (path->slots[0] == nritems)
5912 path->slots[0]--;
5913
5914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5915 if (found_key.objectid < min_objectid)
5916 break;
5917 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5918 found_key.type == BTRFS_METADATA_ITEM_KEY)
5919 return 0;
5920 if (found_key.objectid == min_objectid &&
5921 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5922 break;
5923 }
5924 return 1;
5925}