blob: 6d183f60d63a0521e8c4461e4d5fd705163d17d1 [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);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200324 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
Jan Schmidt097b8a72012-06-21 11:08:04 +0200344static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200345{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200346 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200347}
348
Jan Schmidt097b8a72012-06-21 11:08:04 +0200349static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200350{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200351 read_unlock(&fs_info->tree_mod_log_lock);
352}
353
354static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
355{
356 write_lock(&fs_info->tree_mod_log_lock);
357}
358
359static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
360{
361 write_unlock(&fs_info->tree_mod_log_lock);
362}
363
364/*
365 * This adds a new blocker to the tree mod log's blocker list if the @elem
366 * passed does not already have a sequence number set. So when a caller expects
367 * to record tree modifications, it should ensure to set elem->seq to zero
368 * before calling btrfs_get_tree_mod_seq.
369 * Returns a fresh, unused tree log modification sequence number, even if no new
370 * blocker was added.
371 */
372u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
373 struct seq_list *elem)
374{
375 u64 seq;
376
377 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) {
380 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
381 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
382 }
383 seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200384 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200385 tree_mod_log_write_unlock(fs_info);
386
387 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200388}
389
390void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
391 struct seq_list *elem)
392{
393 struct rb_root *tm_root;
394 struct rb_node *node;
395 struct rb_node *next;
396 struct seq_list *cur_elem;
397 struct tree_mod_elem *tm;
398 u64 min_seq = (u64)-1;
399 u64 seq_putting = elem->seq;
400
401 if (!seq_putting)
402 return;
403
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200404 spin_lock(&fs_info->tree_mod_seq_lock);
405 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200406 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200407
408 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200409 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 if (seq_putting > cur_elem->seq) {
411 /*
412 * blocker with lower sequence number exists, we
413 * cannot remove anything from the log
414 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200415 spin_unlock(&fs_info->tree_mod_seq_lock);
416 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200417 }
418 min_seq = cur_elem->seq;
419 }
420 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200421 spin_unlock(&fs_info->tree_mod_seq_lock);
422
423 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200424 * anything that's lower than the lowest existing (read: blocked)
425 * sequence number can be removed from the tree.
426 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200427 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200428 tm_root = &fs_info->tree_mod_log;
429 for (node = rb_first(tm_root); node; node = next) {
430 next = rb_next(node);
431 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200432 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200433 continue;
434 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200435 kfree(tm);
436 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200437 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200438}
439
440/*
441 * key order of the log:
442 * index -> sequence
443 *
444 * the index is the shifted logical of the *new* root node for root replace
445 * operations, or the shifted logical of the affected block for all other
446 * operations.
447 */
448static noinline int
449__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
450{
451 struct rb_root *tm_root;
452 struct rb_node **new;
453 struct rb_node *parent = NULL;
454 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200455
Jan Schmidt097b8a72012-06-21 11:08:04 +0200456 BUG_ON(!tm || !tm->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200457
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200458 tm_root = &fs_info->tree_mod_log;
459 new = &tm_root->rb_node;
460 while (*new) {
461 cur = container_of(*new, struct tree_mod_elem, node);
462 parent = *new;
463 if (cur->index < tm->index)
464 new = &((*new)->rb_left);
465 else if (cur->index > tm->index)
466 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200467 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200468 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470 new = &((*new)->rb_right);
471 else {
472 kfree(tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200473 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200474 }
475 }
476
477 rb_link_node(&tm->node, parent, new);
478 rb_insert_color(&tm->node, tm_root);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200479 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200480}
481
Jan Schmidt097b8a72012-06-21 11:08:04 +0200482/*
483 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
484 * returns zero with the tree_mod_log_lock acquired. The caller must hold
485 * this until all tree mod log insertions are recorded in the rb tree and then
486 * call tree_mod_log_write_unlock() to release.
487 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200488static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
489 struct extent_buffer *eb) {
490 smp_mb();
491 if (list_empty(&(fs_info)->tree_mod_seq_list))
492 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200493 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200494 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200495
496 tree_mod_log_write_lock(fs_info);
497 if (list_empty(&fs_info->tree_mod_seq_list)) {
498 /*
499 * someone emptied the list while we were waiting for the lock.
500 * we must not add to the list when no blocker exists.
501 */
502 tree_mod_log_write_unlock(fs_info);
503 return 1;
504 }
505
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200506 return 0;
507}
508
Jan Schmidt3310c362012-06-11 10:52:38 +0200509/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200510 * This allocates memory and gets a tree modification sequence number.
Jan Schmidt3310c362012-06-11 10:52:38 +0200511 *
Jan Schmidt097b8a72012-06-21 11:08:04 +0200512 * Returns <0 on error.
513 * Returns >0 (the added sequence number) on success.
Jan Schmidt3310c362012-06-11 10:52:38 +0200514 */
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200515static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
516 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200517{
518 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200519
Jan Schmidt097b8a72012-06-21 11:08:04 +0200520 /*
521 * once we switch from spin locks to something different, we should
522 * honor the flags parameter here.
523 */
524 tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200525 if (!tm)
526 return -ENOMEM;
527
Jan Schmidt097b8a72012-06-21 11:08:04 +0200528 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
529 return tm->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200530}
531
Jan Schmidt097b8a72012-06-21 11:08:04 +0200532static inline int
533__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
534 struct extent_buffer *eb, int slot,
535 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200536{
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200537 int ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200538 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200539
540 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200541 if (ret < 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200542 return ret;
543
544 tm->index = eb->start >> PAGE_CACHE_SHIFT;
545 if (op != MOD_LOG_KEY_ADD) {
546 btrfs_node_key(eb, &tm->key, slot);
547 tm->blockptr = btrfs_node_blockptr(eb, slot);
548 }
549 tm->op = op;
550 tm->slot = slot;
551 tm->generation = btrfs_node_ptr_generation(eb, slot);
552
Jan Schmidt097b8a72012-06-21 11:08:04 +0200553 return __tree_mod_log_insert(fs_info, tm);
554}
555
556static noinline int
557tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
558 struct extent_buffer *eb, int slot,
559 enum mod_log_op op, gfp_t flags)
560{
561 int ret;
562
563 if (tree_mod_dont_log(fs_info, eb))
564 return 0;
565
566 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
567
568 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200569 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200570}
571
572static noinline int
573tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
574 int slot, enum mod_log_op op)
575{
576 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
577}
578
579static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200580tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info,
581 struct extent_buffer *eb, int slot,
582 enum mod_log_op op)
583{
584 return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS);
585}
586
587static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200588tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
589 struct extent_buffer *eb, int dst_slot, int src_slot,
590 int nr_items, gfp_t flags)
591{
592 struct tree_mod_elem *tm;
593 int ret;
594 int i;
595
Jan Schmidtf3956942012-05-31 15:02:32 +0200596 if (tree_mod_dont_log(fs_info, eb))
597 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200598
599 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200600 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200601 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
602 BUG_ON(ret < 0);
603 }
604
Jan Schmidtf3956942012-05-31 15:02:32 +0200605 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200606 if (ret < 0)
607 goto out;
Jan Schmidtf3956942012-05-31 15:02:32 +0200608
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200609 tm->index = eb->start >> PAGE_CACHE_SHIFT;
610 tm->slot = src_slot;
611 tm->move.dst_slot = dst_slot;
612 tm->move.nr_items = nr_items;
613 tm->op = MOD_LOG_MOVE_KEYS;
614
Jan Schmidt3310c362012-06-11 10:52:38 +0200615 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200616out:
617 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200618 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200619}
620
Jan Schmidt097b8a72012-06-21 11:08:04 +0200621static inline void
622__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
623{
624 int i;
625 u32 nritems;
626 int ret;
627
Chris Masonb12a3b12012-08-07 15:34:49 -0400628 if (btrfs_header_level(eb) == 0)
629 return;
630
Jan Schmidt097b8a72012-06-21 11:08:04 +0200631 nritems = btrfs_header_nritems(eb);
632 for (i = nritems - 1; i >= 0; i--) {
633 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
634 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
635 BUG_ON(ret < 0);
636 }
637}
638
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639static noinline int
640tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
641 struct extent_buffer *old_root,
642 struct extent_buffer *new_root, gfp_t flags)
643{
644 struct tree_mod_elem *tm;
645 int ret;
646
Jan Schmidt097b8a72012-06-21 11:08:04 +0200647 if (tree_mod_dont_log(fs_info, NULL))
648 return 0;
649
650 __tree_mod_log_free_eb(fs_info, old_root);
651
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200652 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200653 if (ret < 0)
654 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200655
656 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
657 tm->old_root.logical = old_root->start;
658 tm->old_root.level = btrfs_header_level(old_root);
659 tm->generation = btrfs_header_generation(old_root);
660 tm->op = MOD_LOG_ROOT_REPLACE;
661
Jan Schmidt3310c362012-06-11 10:52:38 +0200662 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200663out:
664 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200665 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200666}
667
668static struct tree_mod_elem *
669__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
670 int smallest)
671{
672 struct rb_root *tm_root;
673 struct rb_node *node;
674 struct tree_mod_elem *cur = NULL;
675 struct tree_mod_elem *found = NULL;
676 u64 index = start >> PAGE_CACHE_SHIFT;
677
Jan Schmidt097b8a72012-06-21 11:08:04 +0200678 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200679 tm_root = &fs_info->tree_mod_log;
680 node = tm_root->rb_node;
681 while (node) {
682 cur = container_of(node, struct tree_mod_elem, node);
683 if (cur->index < index) {
684 node = node->rb_left;
685 } else if (cur->index > index) {
686 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200687 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200688 node = node->rb_left;
689 } else if (!smallest) {
690 /* we want the node with the highest seq */
691 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200692 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200693 found = cur;
694 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200696 /* we want the node with the smallest seq */
697 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200698 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200699 found = cur;
700 node = node->rb_right;
701 } else {
702 found = cur;
703 break;
704 }
705 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200706 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200707
708 return found;
709}
710
711/*
712 * this returns the element from the log with the smallest time sequence
713 * value that's in the log (the oldest log item). any element with a time
714 * sequence lower than min_seq will be ignored.
715 */
716static struct tree_mod_elem *
717tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
718 u64 min_seq)
719{
720 return __tree_mod_log_search(fs_info, start, min_seq, 1);
721}
722
723/*
724 * this returns the element from the log with the largest time sequence
725 * value that's in the log (the most recent log item). any element with
726 * a time sequence lower than min_seq will be ignored.
727 */
728static struct tree_mod_elem *
729tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
730{
731 return __tree_mod_log_search(fs_info, start, min_seq, 0);
732}
733
Jan Schmidt097b8a72012-06-21 11:08:04 +0200734static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
736 struct extent_buffer *src, unsigned long dst_offset,
737 unsigned long src_offset, int nr_items)
738{
739 int ret;
740 int i;
741
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200742 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200743 return;
744
Jan Schmidt097b8a72012-06-21 11:08:04 +0200745 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
746 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200747 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200748 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200749
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200750 for (i = 0; i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200751 ret = tree_mod_log_insert_key_locked(fs_info, src,
752 i + src_offset,
753 MOD_LOG_KEY_REMOVE);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200754 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200755 ret = tree_mod_log_insert_key_locked(fs_info, dst,
756 i + dst_offset,
757 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200758 BUG_ON(ret < 0);
759 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200760
761 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200762}
763
764static inline void
765tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
766 int dst_offset, int src_offset, int nr_items)
767{
768 int ret;
769 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
770 nr_items, GFP_NOFS);
771 BUG_ON(ret < 0);
772}
773
Jan Schmidt097b8a72012-06-21 11:08:04 +0200774static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200775tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
776 struct extent_buffer *eb,
777 struct btrfs_disk_key *disk_key, int slot, int atomic)
778{
779 int ret;
780
781 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
782 MOD_LOG_KEY_REPLACE,
783 atomic ? GFP_ATOMIC : GFP_NOFS);
784 BUG_ON(ret < 0);
785}
786
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787static noinline void
788tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200790 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791 return;
792
Jan Schmidt097b8a72012-06-21 11:08:04 +0200793 __tree_mod_log_free_eb(fs_info, eb);
794
795 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796}
797
Jan Schmidt097b8a72012-06-21 11:08:04 +0200798static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200799tree_mod_log_set_root_pointer(struct btrfs_root *root,
800 struct extent_buffer *new_root_node)
801{
802 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200803 ret = tree_mod_log_insert_root(root->fs_info, root->node,
804 new_root_node, GFP_NOFS);
805 BUG_ON(ret < 0);
806}
807
Chris Masond352ac62008-09-29 15:18:18 -0400808/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400809 * check if the tree block can be shared by multiple trees
810 */
811int btrfs_block_can_be_shared(struct btrfs_root *root,
812 struct extent_buffer *buf)
813{
814 /*
815 * Tree blocks not in refernece counted trees and tree roots
816 * are never shared. If a block was allocated after the last
817 * snapshot and the block was not allocated by tree relocation,
818 * we know the block is not shared.
819 */
820 if (root->ref_cows &&
821 buf != root->node && buf != root->commit_root &&
822 (btrfs_header_generation(buf) <=
823 btrfs_root_last_snapshot(&root->root_item) ||
824 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
825 return 1;
826#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
827 if (root->ref_cows &&
828 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
829 return 1;
830#endif
831 return 0;
832}
833
834static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400837 struct extent_buffer *cow,
838 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839{
840 u64 refs;
841 u64 owner;
842 u64 flags;
843 u64 new_flags = 0;
844 int ret;
845
846 /*
847 * Backrefs update rules:
848 *
849 * Always use full backrefs for extent pointers in tree block
850 * allocated by tree relocation.
851 *
852 * If a shared tree block is no longer referenced by its owner
853 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
854 * use full backrefs for extent pointers in tree block.
855 *
856 * If a tree block is been relocating
857 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
858 * use full backrefs for extent pointers in tree block.
859 * The reason for this is some operations (such as drop tree)
860 * are only allowed for blocks use full backrefs.
861 */
862
863 if (btrfs_block_can_be_shared(root, buf)) {
864 ret = btrfs_lookup_extent_info(trans, root, buf->start,
865 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700866 if (ret)
867 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700868 if (refs == 0) {
869 ret = -EROFS;
870 btrfs_std_error(root->fs_info, ret);
871 return ret;
872 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 } else {
874 refs = 1;
875 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
876 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
877 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
878 else
879 flags = 0;
880 }
881
882 owner = btrfs_header_owner(buf);
883 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
884 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
885
886 if (refs > 1) {
887 if ((owner == root->root_key.objectid ||
888 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
889 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200890 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892
893 if (root->root_key.objectid ==
894 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200895 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200897 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 }
900 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
901 } else {
902
903 if (root->root_key.objectid ==
904 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200905 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200907 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100908 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 }
910 if (new_flags != 0) {
911 ret = btrfs_set_disk_extent_flags(trans, root,
912 buf->start,
913 buf->len,
914 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700915 if (ret)
916 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 }
918 } else {
919 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
920 if (root->root_key.objectid ==
921 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200922 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200924 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200926 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200929 /*
930 * don't log freeing in case we're freeing the root node, this
931 * is done by tree_mod_log_set_root_pointer later
932 */
933 if (buf != root->node && btrfs_header_level(buf) != 0)
934 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400936 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 }
938 return 0;
939}
940
941/*
Chris Masond3977122009-01-05 21:25:51 -0500942 * does the dirty work in cow of a single block. The parent block (if
943 * supplied) is updated to point to the new cow copy. The new buffer is marked
944 * dirty and returned locked. If you modify the block it needs to be marked
945 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400946 *
947 * search_start -- an allocation hint for the new block
948 *
Chris Masond3977122009-01-05 21:25:51 -0500949 * empty_size -- a hint that you plan on doing more cow. This is the size in
950 * bytes the allocator should try to find free next to the block it returns.
951 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400952 */
Chris Masond3977122009-01-05 21:25:51 -0500953static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400954 struct btrfs_root *root,
955 struct extent_buffer *buf,
956 struct extent_buffer *parent, int parent_slot,
957 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400958 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400959{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400961 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700962 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400963 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400964 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400966
Chris Mason925baed2008-06-25 16:01:30 -0400967 if (*cow_ret == buf)
968 unlock_orig = 1;
969
Chris Masonb9447ef2009-03-09 11:45:38 -0400970 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400971
Chris Mason7bb86312007-12-11 09:25:06 -0500972 WARN_ON(root->ref_cows && trans->transid !=
973 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400974 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400975
Chris Mason7bb86312007-12-11 09:25:06 -0500976 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400977
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400978 if (level == 0)
979 btrfs_item_key(buf, &disk_key, 0);
980 else
981 btrfs_node_key(buf, &disk_key, 0);
982
983 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
984 if (parent)
985 parent_start = parent->start;
986 else
987 parent_start = 0;
988 } else
989 parent_start = 0;
990
991 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
992 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200993 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400994 if (IS_ERR(cow))
995 return PTR_ERR(cow);
996
Chris Masonb4ce94d2009-02-04 09:25:08 -0500997 /* cow is set to blocking by btrfs_init_new_buffer */
998
Chris Mason5f39d392007-10-15 16:14:19 -0400999 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001000 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001001 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1003 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1004 BTRFS_HEADER_FLAG_RELOC);
1005 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1006 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1007 else
1008 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001009
Yan Zheng2b820322008-11-17 21:11:30 -05001010 write_extent_buffer(cow, root->fs_info->fsid,
1011 (unsigned long)btrfs_header_fsid(cow),
1012 BTRFS_FSID_SIZE);
1013
Mark Fashehbe1a5562011-08-08 13:20:18 -07001014 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001015 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001017 return ret;
1018 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001019
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001020 if (root->ref_cows)
1021 btrfs_reloc_cow_block(trans, root, buf, cow);
1022
Chris Mason6702ed42007-08-07 16:15:09 -04001023 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001024 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1026 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1027 parent_start = buf->start;
1028 else
1029 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001030
Chris Mason5f39d392007-10-15 16:14:19 -04001031 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +02001032 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -04001033 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001034
Yan, Zhengf0486c62010-05-16 10:46:25 -04001035 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001036 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001037 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001038 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001039 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1041 parent_start = parent->start;
1042 else
1043 parent_start = 0;
1044
1045 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001046 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1047 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001048 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001049 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001050 btrfs_set_node_ptr_generation(parent, parent_slot,
1051 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001052 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001053 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001054 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001055 }
Chris Mason925baed2008-06-25 16:01:30 -04001056 if (unlock_orig)
1057 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001058 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001059 btrfs_mark_buffer_dirty(cow);
1060 *cow_ret = cow;
1061 return 0;
1062}
1063
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001064/*
1065 * returns the logical address of the oldest predecessor of the given root.
1066 * entries older than time_seq are ignored.
1067 */
1068static struct tree_mod_elem *
1069__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1070 struct btrfs_root *root, u64 time_seq)
1071{
1072 struct tree_mod_elem *tm;
1073 struct tree_mod_elem *found = NULL;
1074 u64 root_logical = root->node->start;
1075 int looped = 0;
1076
1077 if (!time_seq)
1078 return 0;
1079
1080 /*
1081 * the very last operation that's logged for a root is the replacement
1082 * operation (if it is replaced at all). this has the index of the *new*
1083 * root, making it the very first operation that's logged for this root.
1084 */
1085 while (1) {
1086 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1087 time_seq);
1088 if (!looped && !tm)
1089 return 0;
1090 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001091 * if there are no tree operation for the oldest root, we simply
1092 * return it. this should only happen if that (old) root is at
1093 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001094 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001095 if (!tm)
1096 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001097
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001098 /*
1099 * if there's an operation that's not a root replacement, we
1100 * found the oldest version of our root. normally, we'll find a
1101 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1102 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001103 if (tm->op != MOD_LOG_ROOT_REPLACE)
1104 break;
1105
1106 found = tm;
1107 root_logical = tm->old_root.logical;
1108 BUG_ON(root_logical == root->node->start);
1109 looped = 1;
1110 }
1111
Jan Schmidta95236d2012-06-05 16:41:24 +02001112 /* if there's no old root to return, return what we found instead */
1113 if (!found)
1114 found = tm;
1115
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001116 return found;
1117}
1118
1119/*
1120 * tm is a pointer to the first operation to rewind within eb. then, all
1121 * previous operations will be rewinded (until we reach something older than
1122 * time_seq).
1123 */
1124static void
1125__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1126 struct tree_mod_elem *first_tm)
1127{
1128 u32 n;
1129 struct rb_node *next;
1130 struct tree_mod_elem *tm = first_tm;
1131 unsigned long o_dst;
1132 unsigned long o_src;
1133 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1134
1135 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001136 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001137 /*
1138 * all the operations are recorded with the operator used for
1139 * the modification. as we're going backwards, we do the
1140 * opposite of each operation here.
1141 */
1142 switch (tm->op) {
1143 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1144 BUG_ON(tm->slot < n);
1145 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1146 case MOD_LOG_KEY_REMOVE:
1147 btrfs_set_node_key(eb, &tm->key, tm->slot);
1148 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1149 btrfs_set_node_ptr_generation(eb, tm->slot,
1150 tm->generation);
1151 n++;
1152 break;
1153 case MOD_LOG_KEY_REPLACE:
1154 BUG_ON(tm->slot >= n);
1155 btrfs_set_node_key(eb, &tm->key, tm->slot);
1156 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1157 btrfs_set_node_ptr_generation(eb, tm->slot,
1158 tm->generation);
1159 break;
1160 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001161 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001162 n--;
1163 break;
1164 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001165 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1166 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1167 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001168 tm->move.nr_items * p_size);
1169 break;
1170 case MOD_LOG_ROOT_REPLACE:
1171 /*
1172 * this operation is special. for roots, this must be
1173 * handled explicitly before rewinding.
1174 * for non-roots, this operation may exist if the node
1175 * was a root: root A -> child B; then A gets empty and
1176 * B is promoted to the new root. in the mod log, we'll
1177 * have a root-replace operation for B, a tree block
1178 * that is no root. we simply ignore that operation.
1179 */
1180 break;
1181 }
1182 next = rb_next(&tm->node);
1183 if (!next)
1184 break;
1185 tm = container_of(next, struct tree_mod_elem, node);
1186 if (tm->index != first_tm->index)
1187 break;
1188 }
1189 btrfs_set_header_nritems(eb, n);
1190}
1191
1192static struct extent_buffer *
1193tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1194 u64 time_seq)
1195{
1196 struct extent_buffer *eb_rewin;
1197 struct tree_mod_elem *tm;
1198
1199 if (!time_seq)
1200 return eb;
1201
1202 if (btrfs_header_level(eb) == 0)
1203 return eb;
1204
1205 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1206 if (!tm)
1207 return eb;
1208
1209 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1210 BUG_ON(tm->slot != 0);
1211 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1212 fs_info->tree_root->nodesize);
1213 BUG_ON(!eb_rewin);
1214 btrfs_set_header_bytenr(eb_rewin, eb->start);
1215 btrfs_set_header_backref_rev(eb_rewin,
1216 btrfs_header_backref_rev(eb));
1217 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001218 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001219 } else {
1220 eb_rewin = btrfs_clone_extent_buffer(eb);
1221 BUG_ON(!eb_rewin);
1222 }
1223
1224 extent_buffer_get(eb_rewin);
1225 free_extent_buffer(eb);
1226
1227 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1228
1229 return eb_rewin;
1230}
1231
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001232/*
1233 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1234 * value. If there are no changes, the current root->root_node is returned. If
1235 * anything changed in between, there's a fresh buffer allocated on which the
1236 * rewind operations are done. In any case, the returned buffer is read locked.
1237 * Returns NULL on error (with no locks held).
1238 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239static inline struct extent_buffer *
1240get_old_root(struct btrfs_root *root, u64 time_seq)
1241{
1242 struct tree_mod_elem *tm;
1243 struct extent_buffer *eb;
Jan Schmidta95236d2012-06-05 16:41:24 +02001244 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001245 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001246 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001247
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001248 eb = btrfs_read_lock_root_node(root);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001249 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1250 if (!tm)
1251 return root->node;
1252
Jan Schmidta95236d2012-06-05 16:41:24 +02001253 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1254 old_root = &tm->old_root;
1255 old_generation = tm->generation;
1256 logical = old_root->logical;
1257 } else {
1258 logical = root->node->start;
1259 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001260
Jan Schmidta95236d2012-06-05 16:41:24 +02001261 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidta95236d2012-06-05 16:41:24 +02001262 if (old_root)
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001263 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidta95236d2012-06-05 16:41:24 +02001264 else
1265 eb = btrfs_clone_extent_buffer(root->node);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001266 btrfs_tree_read_unlock(root->node);
1267 free_extent_buffer(root->node);
1268 if (!eb)
1269 return NULL;
1270 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001271 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001272 btrfs_set_header_bytenr(eb, eb->start);
1273 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1274 btrfs_set_header_owner(eb, root->root_key.objectid);
Jan Schmidta95236d2012-06-05 16:41:24 +02001275 btrfs_set_header_level(eb, old_root->level);
1276 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001277 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001278 if (tm)
1279 __tree_mod_log_rewind(eb, time_seq, tm);
1280 else
1281 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001282 extent_buffer_get(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001283
1284 return eb;
1285}
1286
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287static inline int should_cow_block(struct btrfs_trans_handle *trans,
1288 struct btrfs_root *root,
1289 struct extent_buffer *buf)
1290{
Liu Bof1ebcc72011-11-14 20:48:06 -05001291 /* ensure we can see the force_cow */
1292 smp_rmb();
1293
1294 /*
1295 * We do not need to cow a block if
1296 * 1) this block is not created or changed in this transaction;
1297 * 2) this block does not belong to TREE_RELOC tree;
1298 * 3) the root is not forced COW.
1299 *
1300 * What is forced COW:
1301 * when we create snapshot during commiting the transaction,
1302 * after we've finished coping src root, we must COW the shared
1303 * block to ensure the metadata consistency.
1304 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 if (btrfs_header_generation(buf) == trans->transid &&
1306 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1307 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001308 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1309 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310 return 0;
1311 return 1;
1312}
1313
Chris Masond352ac62008-09-29 15:18:18 -04001314/*
1315 * cows a single block, see __btrfs_cow_block for the real work.
1316 * This version of it has extra checks so that a block isn't cow'd more than
1317 * once per transaction, as long as it hasn't been written yet
1318 */
Chris Masond3977122009-01-05 21:25:51 -05001319noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001320 struct btrfs_root *root, struct extent_buffer *buf,
1321 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001322 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001323{
Chris Mason6702ed42007-08-07 16:15:09 -04001324 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001325 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001326
Chris Masonccd467d2007-06-28 15:57:36 -04001327 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001328 printk(KERN_CRIT "trans %llu running %llu\n",
1329 (unsigned long long)trans->transid,
1330 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001331 root->fs_info->running_transaction->transid);
1332 WARN_ON(1);
1333 }
1334 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001335 printk(KERN_CRIT "trans %llu running %llu\n",
1336 (unsigned long long)trans->transid,
1337 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001338 WARN_ON(1);
1339 }
Chris Masondc17ff82008-01-08 15:46:30 -05001340
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001341 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001342 *cow_ret = buf;
1343 return 0;
1344 }
Chris Masonc4876852009-02-04 09:24:25 -05001345
Chris Mason0b86a832008-03-24 15:01:56 -04001346 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001347
1348 if (parent)
1349 btrfs_set_lock_blocking(parent);
1350 btrfs_set_lock_blocking(buf);
1351
Chris Masonf510cfe2007-10-15 16:14:48 -04001352 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001353 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001354
1355 trace_btrfs_cow_block(root, buf, *cow_ret);
1356
Chris Masonf510cfe2007-10-15 16:14:48 -04001357 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001358}
1359
Chris Masond352ac62008-09-29 15:18:18 -04001360/*
1361 * helper function for defrag to decide if two blocks pointed to by a
1362 * node are actually close by
1363 */
Chris Mason6b800532007-10-15 16:17:34 -04001364static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001365{
Chris Mason6b800532007-10-15 16:17:34 -04001366 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001367 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001368 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001369 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001370 return 0;
1371}
1372
Chris Mason081e9572007-11-06 10:26:24 -05001373/*
1374 * compare two keys in a memcmp fashion
1375 */
1376static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1377{
1378 struct btrfs_key k1;
1379
1380 btrfs_disk_key_to_cpu(&k1, disk);
1381
Diego Calleja20736ab2009-07-24 11:06:52 -04001382 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001383}
1384
Josef Bacikf3465ca2008-11-12 14:19:50 -05001385/*
1386 * same as comp_keys only with two btrfs_key's
1387 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001389{
1390 if (k1->objectid > k2->objectid)
1391 return 1;
1392 if (k1->objectid < k2->objectid)
1393 return -1;
1394 if (k1->type > k2->type)
1395 return 1;
1396 if (k1->type < k2->type)
1397 return -1;
1398 if (k1->offset > k2->offset)
1399 return 1;
1400 if (k1->offset < k2->offset)
1401 return -1;
1402 return 0;
1403}
Chris Mason081e9572007-11-06 10:26:24 -05001404
Chris Masond352ac62008-09-29 15:18:18 -04001405/*
1406 * this is used by the defrag code to go through all the
1407 * leaves pointed to by a node and reallocate them so that
1408 * disk order is close to key order
1409 */
Chris Mason6702ed42007-08-07 16:15:09 -04001410int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001411 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001412 int start_slot, int cache_only, u64 *last_ret,
1413 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001414{
Chris Mason6b800532007-10-15 16:17:34 -04001415 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001416 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001417 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001418 u64 search_start = *last_ret;
1419 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001420 u64 other;
1421 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001422 int end_slot;
1423 int i;
1424 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001425 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001426 int uptodate;
1427 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001428 int progress_passed = 0;
1429 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001430
Chris Mason5708b952007-10-25 15:43:18 -04001431 parent_level = btrfs_header_level(parent);
1432 if (cache_only && parent_level != 1)
1433 return 0;
1434
Chris Masond3977122009-01-05 21:25:51 -05001435 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001436 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001437 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001438 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001439
Chris Mason6b800532007-10-15 16:17:34 -04001440 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001441 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001442 end_slot = parent_nritems;
1443
1444 if (parent_nritems == 1)
1445 return 0;
1446
Chris Masonb4ce94d2009-02-04 09:25:08 -05001447 btrfs_set_lock_blocking(parent);
1448
Chris Mason6702ed42007-08-07 16:15:09 -04001449 for (i = start_slot; i < end_slot; i++) {
1450 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001451
Chris Mason081e9572007-11-06 10:26:24 -05001452 btrfs_node_key(parent, &disk_key, i);
1453 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1454 continue;
1455
1456 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001457 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001458 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001459 if (last_block == 0)
1460 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001461
Chris Mason6702ed42007-08-07 16:15:09 -04001462 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001463 other = btrfs_node_blockptr(parent, i - 1);
1464 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001465 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001466 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001467 other = btrfs_node_blockptr(parent, i + 1);
1468 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001469 }
Chris Masone9d0b132007-08-10 14:06:19 -04001470 if (close) {
1471 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001472 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001473 }
Chris Mason6702ed42007-08-07 16:15:09 -04001474
Chris Mason6b800532007-10-15 16:17:34 -04001475 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1476 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001477 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001478 else
1479 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001480 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001481 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001482 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001483 continue;
1484 }
Chris Mason6b800532007-10-15 16:17:34 -04001485 if (!cur) {
1486 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001487 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001488 if (!cur)
1489 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001490 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001491 err = btrfs_read_buffer(cur, gen);
1492 if (err) {
1493 free_extent_buffer(cur);
1494 return err;
1495 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001496 }
Chris Mason6702ed42007-08-07 16:15:09 -04001497 }
Chris Masone9d0b132007-08-10 14:06:19 -04001498 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001499 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001500
Chris Masone7a84562008-06-25 16:01:31 -04001501 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001502 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001503 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001504 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001505 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001506 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001507 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001508 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001509 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001510 break;
Yan252c38f2007-08-29 09:11:44 -04001511 }
Chris Masone7a84562008-06-25 16:01:31 -04001512 search_start = cur->start;
1513 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001514 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001515 btrfs_tree_unlock(cur);
1516 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001517 }
1518 return err;
1519}
1520
Chris Mason74123bd2007-02-02 11:05:29 -05001521/*
1522 * The leaf data grows from end-to-front in the node.
1523 * this returns the address of the start of the last item,
1524 * which is the stop of the leaf data stack
1525 */
Chris Mason123abc82007-03-14 14:14:43 -04001526static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001527 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001528{
Chris Mason5f39d392007-10-15 16:14:19 -04001529 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001530 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001531 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001532 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001533}
1534
Chris Masonaa5d6be2007-02-28 16:35:06 -05001535
Chris Mason74123bd2007-02-02 11:05:29 -05001536/*
Chris Mason5f39d392007-10-15 16:14:19 -04001537 * search for key in the extent_buffer. The items start at offset p,
1538 * and they are item_size apart. There are 'max' items in p.
1539 *
Chris Mason74123bd2007-02-02 11:05:29 -05001540 * the slot in the array is returned via slot, and it points to
1541 * the place where you would insert key if it is not found in
1542 * the array.
1543 *
1544 * slot may point to max if the key is bigger than all of the keys
1545 */
Chris Masone02119d2008-09-05 16:13:11 -04001546static noinline int generic_bin_search(struct extent_buffer *eb,
1547 unsigned long p,
1548 int item_size, struct btrfs_key *key,
1549 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001550{
1551 int low = 0;
1552 int high = max;
1553 int mid;
1554 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001555 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001556 struct btrfs_disk_key unaligned;
1557 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001558 char *kaddr = NULL;
1559 unsigned long map_start = 0;
1560 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001561 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001562
Chris Masond3977122009-01-05 21:25:51 -05001563 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001564 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001565 offset = p + mid * item_size;
1566
Chris Masona6591712011-07-19 12:04:14 -04001567 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001568 (offset + sizeof(struct btrfs_disk_key)) >
1569 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001570
1571 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001572 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001573 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001574
Chris Mason479965d2007-10-15 16:14:27 -04001575 if (!err) {
1576 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1577 map_start);
1578 } else {
1579 read_extent_buffer(eb, &unaligned,
1580 offset, sizeof(unaligned));
1581 tmp = &unaligned;
1582 }
1583
Chris Mason5f39d392007-10-15 16:14:19 -04001584 } else {
1585 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1586 map_start);
1587 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001588 ret = comp_keys(tmp, key);
1589
1590 if (ret < 0)
1591 low = mid + 1;
1592 else if (ret > 0)
1593 high = mid;
1594 else {
1595 *slot = mid;
1596 return 0;
1597 }
1598 }
1599 *slot = low;
1600 return 1;
1601}
1602
Chris Mason97571fd2007-02-24 13:39:08 -05001603/*
1604 * simple bin_search frontend that does the right thing for
1605 * leaves vs nodes
1606 */
Chris Mason5f39d392007-10-15 16:14:19 -04001607static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1608 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001609{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001610 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001611 return generic_bin_search(eb,
1612 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001613 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001614 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001615 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001616 else
Chris Mason5f39d392007-10-15 16:14:19 -04001617 return generic_bin_search(eb,
1618 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001619 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001620 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001621 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001622}
1623
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1625 int level, int *slot)
1626{
1627 return bin_search(eb, key, level, slot);
1628}
1629
Yan, Zhengf0486c62010-05-16 10:46:25 -04001630static void root_add_used(struct btrfs_root *root, u32 size)
1631{
1632 spin_lock(&root->accounting_lock);
1633 btrfs_set_root_used(&root->root_item,
1634 btrfs_root_used(&root->root_item) + size);
1635 spin_unlock(&root->accounting_lock);
1636}
1637
1638static void root_sub_used(struct btrfs_root *root, u32 size)
1639{
1640 spin_lock(&root->accounting_lock);
1641 btrfs_set_root_used(&root->root_item,
1642 btrfs_root_used(&root->root_item) - size);
1643 spin_unlock(&root->accounting_lock);
1644}
1645
Chris Masond352ac62008-09-29 15:18:18 -04001646/* given a node and slot number, this reads the blocks it points to. The
1647 * extent buffer is returned with a reference taken (but unlocked).
1648 * NULL is returned on error.
1649 */
Chris Masone02119d2008-09-05 16:13:11 -04001650static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001651 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001652{
Chris Masonca7a79a2008-05-12 12:59:19 -04001653 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001654 if (slot < 0)
1655 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001656 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001657 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001658
1659 BUG_ON(level == 0);
1660
Chris Masondb945352007-10-15 16:15:53 -04001661 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001662 btrfs_level_size(root, level - 1),
1663 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001664}
1665
Chris Masond352ac62008-09-29 15:18:18 -04001666/*
1667 * node level balancing, used to make sure nodes are in proper order for
1668 * item deletion. We balance from the top down, so we have to make sure
1669 * that a deletion won't leave an node completely empty later on.
1670 */
Chris Masone02119d2008-09-05 16:13:11 -04001671static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001672 struct btrfs_root *root,
1673 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001674{
Chris Mason5f39d392007-10-15 16:14:19 -04001675 struct extent_buffer *right = NULL;
1676 struct extent_buffer *mid;
1677 struct extent_buffer *left = NULL;
1678 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001679 int ret = 0;
1680 int wret;
1681 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001682 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001683 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001684
1685 if (level == 0)
1686 return 0;
1687
Chris Mason5f39d392007-10-15 16:14:19 -04001688 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001689
Chris Masonbd681512011-07-16 15:23:14 -04001690 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1691 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001692 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1693
Chris Mason1d4f8a02007-03-13 09:28:32 -04001694 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001695
Li Zefana05a9bb2011-09-06 16:55:34 +08001696 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001697 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001698 pslot = path->slots[level + 1];
1699 }
Chris Masonbb803952007-03-01 12:04:21 -05001700
Chris Mason40689472007-03-17 14:29:23 -04001701 /*
1702 * deal with the case where there is only one pointer in the root
1703 * by promoting the node below to a root
1704 */
Chris Mason5f39d392007-10-15 16:14:19 -04001705 if (!parent) {
1706 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001707
Chris Mason5f39d392007-10-15 16:14:19 -04001708 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001709 return 0;
1710
1711 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001712 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001713 if (!child) {
1714 ret = -EROFS;
1715 btrfs_std_error(root->fs_info, ret);
1716 goto enospc;
1717 }
1718
Chris Mason925baed2008-06-25 16:01:30 -04001719 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001721 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001722 if (ret) {
1723 btrfs_tree_unlock(child);
1724 free_extent_buffer(child);
1725 goto enospc;
1726 }
Yan2f375ab2008-02-01 14:58:07 -05001727
Jan Schmidtf2304752012-05-26 11:43:17 +02001728 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001729 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001730
Chris Mason0b86a832008-03-24 15:01:56 -04001731 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001732 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001733
Chris Mason925baed2008-06-25 16:01:30 -04001734 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001735 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001737 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001738 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001739 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001740
1741 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001742 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001743 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001744 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001745 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001746 }
Chris Mason5f39d392007-10-15 16:14:19 -04001747 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001748 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001749 return 0;
1750
Chris Mason5f39d392007-10-15 16:14:19 -04001751 left = read_node_slot(root, parent, pslot - 1);
1752 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001753 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001754 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001755 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001756 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001757 if (wret) {
1758 ret = wret;
1759 goto enospc;
1760 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001761 }
Chris Mason5f39d392007-10-15 16:14:19 -04001762 right = read_node_slot(root, parent, pslot + 1);
1763 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001764 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001765 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001766 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001767 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001768 if (wret) {
1769 ret = wret;
1770 goto enospc;
1771 }
1772 }
1773
1774 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001775 if (left) {
1776 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001777 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001778 if (wret < 0)
1779 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001780 }
Chris Mason79f95c82007-03-01 15:16:26 -05001781
1782 /*
1783 * then try to empty the right most buffer into the middle
1784 */
Chris Mason5f39d392007-10-15 16:14:19 -04001785 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001786 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001787 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001788 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001789 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001790 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001791 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001792 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001793 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001794 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001795 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001796 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001797 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001798 struct btrfs_disk_key right_key;
1799 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001800 tree_mod_log_set_node_key(root->fs_info, parent,
1801 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001802 btrfs_set_node_key(parent, &right_key, pslot + 1);
1803 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001804 }
1805 }
Chris Mason5f39d392007-10-15 16:14:19 -04001806 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001807 /*
1808 * we're not allowed to leave a node with one item in the
1809 * tree during a delete. A deletion from lower in the tree
1810 * could try to delete the only pointer in this node.
1811 * So, pull some keys from the left.
1812 * There has to be a left pointer at this point because
1813 * otherwise we would have pulled some pointers from the
1814 * right
1815 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001816 if (!left) {
1817 ret = -EROFS;
1818 btrfs_std_error(root->fs_info, ret);
1819 goto enospc;
1820 }
Chris Mason5f39d392007-10-15 16:14:19 -04001821 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001822 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001823 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001824 goto enospc;
1825 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001826 if (wret == 1) {
1827 wret = push_node_left(trans, root, left, mid, 1);
1828 if (wret < 0)
1829 ret = wret;
1830 }
Chris Mason79f95c82007-03-01 15:16:26 -05001831 BUG_ON(wret == 1);
1832 }
Chris Mason5f39d392007-10-15 16:14:19 -04001833 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001834 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001835 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001836 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001837 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001838 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001839 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001840 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001841 } else {
1842 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001843 struct btrfs_disk_key mid_key;
1844 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001845 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1846 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001847 btrfs_set_node_key(parent, &mid_key, pslot);
1848 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001849 }
Chris Masonbb803952007-03-01 12:04:21 -05001850
Chris Mason79f95c82007-03-01 15:16:26 -05001851 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001852 if (left) {
1853 if (btrfs_header_nritems(left) > orig_slot) {
1854 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001855 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001856 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001857 path->slots[level + 1] -= 1;
1858 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001859 if (mid) {
1860 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001861 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001862 }
Chris Masonbb803952007-03-01 12:04:21 -05001863 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001864 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001865 path->slots[level] = orig_slot;
1866 }
1867 }
Chris Mason79f95c82007-03-01 15:16:26 -05001868 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001869 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001870 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001871 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001872enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001873 if (right) {
1874 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001875 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001876 }
1877 if (left) {
1878 if (path->nodes[level] != left)
1879 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001880 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001881 }
Chris Masonbb803952007-03-01 12:04:21 -05001882 return ret;
1883}
1884
Chris Masond352ac62008-09-29 15:18:18 -04001885/* Node balancing for insertion. Here we only split or push nodes around
1886 * when they are completely full. This is also done top down, so we
1887 * have to be pessimistic.
1888 */
Chris Masond3977122009-01-05 21:25:51 -05001889static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001890 struct btrfs_root *root,
1891 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001892{
Chris Mason5f39d392007-10-15 16:14:19 -04001893 struct extent_buffer *right = NULL;
1894 struct extent_buffer *mid;
1895 struct extent_buffer *left = NULL;
1896 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001897 int ret = 0;
1898 int wret;
1899 int pslot;
1900 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001901
1902 if (level == 0)
1903 return 1;
1904
Chris Mason5f39d392007-10-15 16:14:19 -04001905 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001906 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001907
Li Zefana05a9bb2011-09-06 16:55:34 +08001908 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001909 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001910 pslot = path->slots[level + 1];
1911 }
Chris Masone66f7092007-04-20 13:16:02 -04001912
Chris Mason5f39d392007-10-15 16:14:19 -04001913 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001914 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001915
Chris Mason5f39d392007-10-15 16:14:19 -04001916 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001917
1918 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001919 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001920 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001921
1922 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001923 btrfs_set_lock_blocking(left);
1924
Chris Mason5f39d392007-10-15 16:14:19 -04001925 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001926 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1927 wret = 1;
1928 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001929 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001930 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001931 if (ret)
1932 wret = 1;
1933 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001934 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001935 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001936 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001937 }
Chris Masone66f7092007-04-20 13:16:02 -04001938 if (wret < 0)
1939 ret = wret;
1940 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001941 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001942 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001943 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001944 tree_mod_log_set_node_key(root->fs_info, parent,
1945 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001946 btrfs_set_node_key(parent, &disk_key, pslot);
1947 btrfs_mark_buffer_dirty(parent);
1948 if (btrfs_header_nritems(left) > orig_slot) {
1949 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001950 path->slots[level + 1] -= 1;
1951 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001952 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001953 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001954 } else {
1955 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001956 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001957 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001958 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001959 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001960 }
Chris Masone66f7092007-04-20 13:16:02 -04001961 return 0;
1962 }
Chris Mason925baed2008-06-25 16:01:30 -04001963 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001964 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001965 }
Chris Mason925baed2008-06-25 16:01:30 -04001966 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001967
1968 /*
1969 * then try to empty the right most buffer into the middle
1970 */
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001972 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001973
Chris Mason925baed2008-06-25 16:01:30 -04001974 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001975 btrfs_set_lock_blocking(right);
1976
Chris Mason5f39d392007-10-15 16:14:19 -04001977 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001978 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1979 wret = 1;
1980 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001981 ret = btrfs_cow_block(trans, root, right,
1982 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001983 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001984 if (ret)
1985 wret = 1;
1986 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001987 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001988 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001989 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001990 }
Chris Masone66f7092007-04-20 13:16:02 -04001991 if (wret < 0)
1992 ret = wret;
1993 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001994 struct btrfs_disk_key disk_key;
1995
1996 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001997 tree_mod_log_set_node_key(root->fs_info, parent,
1998 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001999 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2000 btrfs_mark_buffer_dirty(parent);
2001
2002 if (btrfs_header_nritems(mid) <= orig_slot) {
2003 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002004 path->slots[level + 1] += 1;
2005 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002007 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002008 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002009 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002010 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002012 }
Chris Masone66f7092007-04-20 13:16:02 -04002013 return 0;
2014 }
Chris Mason925baed2008-06-25 16:01:30 -04002015 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002016 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002017 }
Chris Masone66f7092007-04-20 13:16:02 -04002018 return 1;
2019}
2020
Chris Mason74123bd2007-02-02 11:05:29 -05002021/*
Chris Masond352ac62008-09-29 15:18:18 -04002022 * readahead one full node of leaves, finding things that are close
2023 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002024 */
Chris Masonc8c42862009-04-03 10:14:18 -04002025static void reada_for_search(struct btrfs_root *root,
2026 struct btrfs_path *path,
2027 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002028{
Chris Mason5f39d392007-10-15 16:14:19 -04002029 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002030 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002031 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002032 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002033 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002034 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002035 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002036 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002037 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002038 u32 nr;
2039 u32 blocksize;
2040 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002041
Chris Masona6b6e752007-10-15 16:22:39 -04002042 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002043 return;
2044
Chris Mason6702ed42007-08-07 16:15:09 -04002045 if (!path->nodes[level])
2046 return;
2047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002049
Chris Mason3c69fae2007-08-07 15:52:22 -04002050 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002051 blocksize = btrfs_level_size(root, level - 1);
2052 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002053 if (eb) {
2054 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002055 return;
2056 }
2057
Chris Masona7175312009-01-22 09:23:10 -05002058 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002061 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002062
Chris Masond3977122009-01-05 21:25:51 -05002063 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002064 if (direction < 0) {
2065 if (nr == 0)
2066 break;
2067 nr--;
2068 } else if (direction > 0) {
2069 nr++;
2070 if (nr >= nritems)
2071 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002072 }
Chris Mason01f46652007-12-21 16:24:26 -05002073 if (path->reada < 0 && objectid) {
2074 btrfs_node_key(node, &disk_key, nr);
2075 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2076 break;
2077 }
Chris Mason6b800532007-10-15 16:17:34 -04002078 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002079 if ((search <= target && target - search <= 65536) ||
2080 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002081 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002082 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002083 nread += blocksize;
2084 }
2085 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002086 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002087 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002088 }
2089}
Chris Mason925baed2008-06-25 16:01:30 -04002090
Chris Masond352ac62008-09-29 15:18:18 -04002091/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002092 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2093 * cache
2094 */
2095static noinline int reada_for_balance(struct btrfs_root *root,
2096 struct btrfs_path *path, int level)
2097{
2098 int slot;
2099 int nritems;
2100 struct extent_buffer *parent;
2101 struct extent_buffer *eb;
2102 u64 gen;
2103 u64 block1 = 0;
2104 u64 block2 = 0;
2105 int ret = 0;
2106 int blocksize;
2107
Chris Mason8c594ea2009-04-20 15:50:10 -04002108 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002109 if (!parent)
2110 return 0;
2111
2112 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002113 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002114 blocksize = btrfs_level_size(root, level);
2115
2116 if (slot > 0) {
2117 block1 = btrfs_node_blockptr(parent, slot - 1);
2118 gen = btrfs_node_ptr_generation(parent, slot - 1);
2119 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002120 /*
2121 * if we get -eagain from btrfs_buffer_uptodate, we
2122 * don't want to return eagain here. That will loop
2123 * forever
2124 */
2125 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002126 block1 = 0;
2127 free_extent_buffer(eb);
2128 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002129 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002130 block2 = btrfs_node_blockptr(parent, slot + 1);
2131 gen = btrfs_node_ptr_generation(parent, slot + 1);
2132 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002133 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002134 block2 = 0;
2135 free_extent_buffer(eb);
2136 }
2137 if (block1 || block2) {
2138 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002139
2140 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002141 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002142
2143 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002144 if (block1)
2145 readahead_tree_block(root, block1, blocksize, 0);
2146 if (block2)
2147 readahead_tree_block(root, block2, blocksize, 0);
2148
2149 if (block1) {
2150 eb = read_tree_block(root, block1, blocksize, 0);
2151 free_extent_buffer(eb);
2152 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002153 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002154 eb = read_tree_block(root, block2, blocksize, 0);
2155 free_extent_buffer(eb);
2156 }
2157 }
2158 return ret;
2159}
2160
2161
2162/*
Chris Masond3977122009-01-05 21:25:51 -05002163 * when we walk down the tree, it is usually safe to unlock the higher layers
2164 * in the tree. The exceptions are when our path goes through slot 0, because
2165 * operations on the tree might require changing key pointers higher up in the
2166 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002167 *
Chris Masond3977122009-01-05 21:25:51 -05002168 * callers might also have set path->keep_locks, which tells this code to keep
2169 * the lock if the path points to the last slot in the block. This is part of
2170 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002171 *
Chris Masond3977122009-01-05 21:25:51 -05002172 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2173 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002174 */
Chris Masone02119d2008-09-05 16:13:11 -04002175static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002176 int lowest_unlock, int min_write_lock_level,
2177 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002178{
2179 int i;
2180 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002181 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002182 struct extent_buffer *t;
2183
2184 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2185 if (!path->nodes[i])
2186 break;
2187 if (!path->locks[i])
2188 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002189 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002190 skip_level = i + 1;
2191 continue;
2192 }
Chris Mason051e1b92008-06-25 16:01:30 -04002193 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002194 u32 nritems;
2195 t = path->nodes[i];
2196 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002197 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002198 skip_level = i + 1;
2199 continue;
2200 }
2201 }
Chris Mason051e1b92008-06-25 16:01:30 -04002202 if (skip_level < i && i >= lowest_unlock)
2203 no_skips = 1;
2204
Chris Mason925baed2008-06-25 16:01:30 -04002205 t = path->nodes[i];
2206 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002207 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002208 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002209 if (write_lock_level &&
2210 i > min_write_lock_level &&
2211 i <= *write_lock_level) {
2212 *write_lock_level = i - 1;
2213 }
Chris Mason925baed2008-06-25 16:01:30 -04002214 }
2215 }
2216}
2217
Chris Mason3c69fae2007-08-07 15:52:22 -04002218/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002219 * This releases any locks held in the path starting at level and
2220 * going all the way up to the root.
2221 *
2222 * btrfs_search_slot will keep the lock held on higher nodes in a few
2223 * corner cases, such as COW of the block at slot zero in the node. This
2224 * ignores those rules, and it should only be called when there are no
2225 * more updates to be done higher up in the tree.
2226 */
2227noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2228{
2229 int i;
2230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002231 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002232 return;
2233
2234 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2235 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002236 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002237 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002238 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002239 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002240 path->locks[i] = 0;
2241 }
2242}
2243
2244/*
Chris Masonc8c42862009-04-03 10:14:18 -04002245 * helper function for btrfs_search_slot. The goal is to find a block
2246 * in cache without setting the path to blocking. If we find the block
2247 * we return zero and the path is unchanged.
2248 *
2249 * If we can't find the block, we set the path blocking and do some
2250 * reada. -EAGAIN is returned and the search must be repeated.
2251 */
2252static int
2253read_block_for_search(struct btrfs_trans_handle *trans,
2254 struct btrfs_root *root, struct btrfs_path *p,
2255 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002256 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002257{
2258 u64 blocknr;
2259 u64 gen;
2260 u32 blocksize;
2261 struct extent_buffer *b = *eb_ret;
2262 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002263 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002264
2265 blocknr = btrfs_node_blockptr(b, slot);
2266 gen = btrfs_node_ptr_generation(b, slot);
2267 blocksize = btrfs_level_size(root, level - 1);
2268
2269 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002270 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002271 /* first we do an atomic uptodate check */
2272 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2273 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002274 /*
2275 * we found an up to date block without
2276 * sleeping, return
2277 * right away
2278 */
2279 *eb_ret = tmp;
2280 return 0;
2281 }
2282 /* the pages were up to date, but we failed
2283 * the generation number check. Do a full
2284 * read for the generation number that is correct.
2285 * We must do this without dropping locks so
2286 * we can trust our generation number
2287 */
2288 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002289 btrfs_set_path_blocking(p);
2290
Chris Masonb9fab912012-05-06 07:23:47 -04002291 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002292 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002293 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002294 *eb_ret = tmp;
2295 return 0;
2296 }
2297 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002298 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002299 return -EIO;
2300 }
Chris Masonc8c42862009-04-03 10:14:18 -04002301 }
2302
2303 /*
2304 * reduce lock contention at high levels
2305 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002306 * we read. Don't release the lock on the current
2307 * level because we need to walk this node to figure
2308 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002309 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002310 btrfs_unlock_up_safe(p, level + 1);
2311 btrfs_set_path_blocking(p);
2312
Chris Masoncb449212010-10-24 11:01:27 -04002313 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002314 if (p->reada)
2315 reada_for_search(root, p, level, slot, key->objectid);
2316
David Sterbab3b4aa72011-04-21 01:20:15 +02002317 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002318
2319 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002320 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002321 if (tmp) {
2322 /*
2323 * If the read above didn't mark this buffer up to date,
2324 * it will never end up being up to date. Set ret to EIO now
2325 * and give up so that our caller doesn't loop forever
2326 * on our EAGAINs.
2327 */
Chris Masonb9fab912012-05-06 07:23:47 -04002328 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002329 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002330 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002331 }
2332 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002333}
2334
2335/*
2336 * helper function for btrfs_search_slot. This does all of the checks
2337 * for node-level blocks and does any balancing required based on
2338 * the ins_len.
2339 *
2340 * If no extra work was required, zero is returned. If we had to
2341 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2342 * start over
2343 */
2344static int
2345setup_nodes_for_search(struct btrfs_trans_handle *trans,
2346 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002347 struct extent_buffer *b, int level, int ins_len,
2348 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002349{
2350 int ret;
2351 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2352 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2353 int sret;
2354
Chris Masonbd681512011-07-16 15:23:14 -04002355 if (*write_lock_level < level + 1) {
2356 *write_lock_level = level + 1;
2357 btrfs_release_path(p);
2358 goto again;
2359 }
2360
Chris Masonc8c42862009-04-03 10:14:18 -04002361 sret = reada_for_balance(root, p, level);
2362 if (sret)
2363 goto again;
2364
2365 btrfs_set_path_blocking(p);
2366 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002367 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002368
2369 BUG_ON(sret > 0);
2370 if (sret) {
2371 ret = sret;
2372 goto done;
2373 }
2374 b = p->nodes[level];
2375 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002376 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002377 int sret;
2378
Chris Masonbd681512011-07-16 15:23:14 -04002379 if (*write_lock_level < level + 1) {
2380 *write_lock_level = level + 1;
2381 btrfs_release_path(p);
2382 goto again;
2383 }
2384
Chris Masonc8c42862009-04-03 10:14:18 -04002385 sret = reada_for_balance(root, p, level);
2386 if (sret)
2387 goto again;
2388
2389 btrfs_set_path_blocking(p);
2390 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002391 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002392
2393 if (sret) {
2394 ret = sret;
2395 goto done;
2396 }
2397 b = p->nodes[level];
2398 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002399 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002400 goto again;
2401 }
2402 BUG_ON(btrfs_header_nritems(b) == 1);
2403 }
2404 return 0;
2405
2406again:
2407 ret = -EAGAIN;
2408done:
2409 return ret;
2410}
2411
2412/*
Chris Mason74123bd2007-02-02 11:05:29 -05002413 * look for key in the tree. path is filled in with nodes along the way
2414 * if key is found, we return zero and you can find the item in the leaf
2415 * level of the path (level 0)
2416 *
2417 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002418 * be inserted, and 1 is returned. If there are other errors during the
2419 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002420 *
2421 * if ins_len > 0, nodes and leaves will be split as we walk down the
2422 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2423 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002424 */
Chris Masone089f052007-03-16 16:20:31 -04002425int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2426 *root, struct btrfs_key *key, struct btrfs_path *p, int
2427 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428{
Chris Mason5f39d392007-10-15 16:14:19 -04002429 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002430 int slot;
2431 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002432 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002433 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002434 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002435 int root_lock;
2436 /* everything at write_lock_level or lower must be write locked */
2437 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002438 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002439 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002440
Chris Mason6702ed42007-08-07 16:15:09 -04002441 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002442 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002443 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002444
Chris Masonbd681512011-07-16 15:23:14 -04002445 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002446 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002447
Chris Masonbd681512011-07-16 15:23:14 -04002448 /* when we are removing items, we might have to go up to level
2449 * two as we update tree pointers Make sure we keep write
2450 * for those levels as well
2451 */
2452 write_lock_level = 2;
2453 } else if (ins_len > 0) {
2454 /*
2455 * for inserting items, make sure we have a write lock on
2456 * level 1 so we can update keys
2457 */
2458 write_lock_level = 1;
2459 }
2460
2461 if (!cow)
2462 write_lock_level = -1;
2463
2464 if (cow && (p->keep_locks || p->lowest_level))
2465 write_lock_level = BTRFS_MAX_LEVEL;
2466
Chris Masonf7c79f32012-03-19 15:54:38 -04002467 min_write_lock_level = write_lock_level;
2468
Chris Masonbb803952007-03-01 12:04:21 -05002469again:
Chris Masonbd681512011-07-16 15:23:14 -04002470 /*
2471 * we try very hard to do read locks on the root
2472 */
2473 root_lock = BTRFS_READ_LOCK;
2474 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002475 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002476 /*
2477 * the commit roots are read only
2478 * so we always do read locks
2479 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002480 b = root->commit_root;
2481 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002482 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002484 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002485 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002486 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002488 level = btrfs_header_level(b);
2489 } else {
2490 /* we don't know the level of the root node
2491 * until we actually have it read locked
2492 */
2493 b = btrfs_read_lock_root_node(root);
2494 level = btrfs_header_level(b);
2495 if (level <= write_lock_level) {
2496 /* whoops, must trade for write lock */
2497 btrfs_tree_read_unlock(b);
2498 free_extent_buffer(b);
2499 b = btrfs_lock_root_node(root);
2500 root_lock = BTRFS_WRITE_LOCK;
2501
2502 /* the level might have changed, check again */
2503 level = btrfs_header_level(b);
2504 }
2505 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506 }
Chris Masonbd681512011-07-16 15:23:14 -04002507 p->nodes[level] = b;
2508 if (!p->skip_locking)
2509 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002510
Chris Masoneb60cea2007-02-02 09:18:22 -05002511 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002512 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002513
2514 /*
2515 * setup the path here so we can release it under lock
2516 * contention with the cow code
2517 */
Chris Mason02217ed2007-03-02 16:08:05 -05002518 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002519 /*
2520 * if we don't really need to cow this block
2521 * then we don't want to set the path blocking,
2522 * so we test it here
2523 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002524 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002525 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526
Chris Masonb4ce94d2009-02-04 09:25:08 -05002527 btrfs_set_path_blocking(p);
2528
Chris Masonbd681512011-07-16 15:23:14 -04002529 /*
2530 * must have write locks on this node and the
2531 * parent
2532 */
2533 if (level + 1 > write_lock_level) {
2534 write_lock_level = level + 1;
2535 btrfs_release_path(p);
2536 goto again;
2537 }
2538
Yan Zheng33c66f42009-07-22 09:59:00 -04002539 err = btrfs_cow_block(trans, root, b,
2540 p->nodes[level + 1],
2541 p->slots[level + 1], &b);
2542 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002543 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002544 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002545 }
Chris Mason02217ed2007-03-02 16:08:05 -05002546 }
Chris Mason65b51a02008-08-01 15:11:20 -04002547cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002548 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002549
Chris Masoneb60cea2007-02-02 09:18:22 -05002550 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002551 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002552
2553 /*
2554 * we have a lock on b and as long as we aren't changing
2555 * the tree, there is no way to for the items in b to change.
2556 * It is safe to drop the lock on our parent before we
2557 * go through the expensive btree search on b.
2558 *
2559 * If cow is true, then we might be changing slot zero,
2560 * which may require changing the parent. So, we can't
2561 * drop the lock until after we know which slot we're
2562 * operating on.
2563 */
2564 if (!cow)
2565 btrfs_unlock_up_safe(p, level + 1);
2566
Chris Mason5f39d392007-10-15 16:14:19 -04002567 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002568
Chris Mason5f39d392007-10-15 16:14:19 -04002569 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002570 int dec = 0;
2571 if (ret && slot > 0) {
2572 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002574 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002576 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002577 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002578 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002579 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002580 if (err) {
2581 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002582 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002583 }
Chris Masonc8c42862009-04-03 10:14:18 -04002584 b = p->nodes[level];
2585 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002586
Chris Masonbd681512011-07-16 15:23:14 -04002587 /*
2588 * slot 0 is special, if we change the key
2589 * we have to update the parent pointer
2590 * which means we must have a write lock
2591 * on the parent
2592 */
2593 if (slot == 0 && cow &&
2594 write_lock_level < level + 1) {
2595 write_lock_level = level + 1;
2596 btrfs_release_path(p);
2597 goto again;
2598 }
2599
Chris Masonf7c79f32012-03-19 15:54:38 -04002600 unlock_up(p, level, lowest_unlock,
2601 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002602
Chris Mason925baed2008-06-25 16:01:30 -04002603 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002604 if (dec)
2605 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002606 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002607 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002608
Yan Zheng33c66f42009-07-22 09:59:00 -04002609 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002610 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002611 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002612 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002613 if (err) {
2614 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002615 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002616 }
Chris Mason76a05b32009-05-14 13:24:30 -04002617
Chris Masonb4ce94d2009-02-04 09:25:08 -05002618 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002619 level = btrfs_header_level(b);
2620 if (level <= write_lock_level) {
2621 err = btrfs_try_tree_write_lock(b);
2622 if (!err) {
2623 btrfs_set_path_blocking(p);
2624 btrfs_tree_lock(b);
2625 btrfs_clear_path_blocking(p, b,
2626 BTRFS_WRITE_LOCK);
2627 }
2628 p->locks[level] = BTRFS_WRITE_LOCK;
2629 } else {
2630 err = btrfs_try_tree_read_lock(b);
2631 if (!err) {
2632 btrfs_set_path_blocking(p);
2633 btrfs_tree_read_lock(b);
2634 btrfs_clear_path_blocking(p, b,
2635 BTRFS_READ_LOCK);
2636 }
2637 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002638 }
Chris Masonbd681512011-07-16 15:23:14 -04002639 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002640 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002641 } else {
2642 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002643 if (ins_len > 0 &&
2644 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002645 if (write_lock_level < 1) {
2646 write_lock_level = 1;
2647 btrfs_release_path(p);
2648 goto again;
2649 }
2650
Chris Masonb4ce94d2009-02-04 09:25:08 -05002651 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002652 err = split_leaf(trans, root, key,
2653 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002654 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002655
Yan Zheng33c66f42009-07-22 09:59:00 -04002656 BUG_ON(err > 0);
2657 if (err) {
2658 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002659 goto done;
2660 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002661 }
Chris Mason459931e2008-12-10 09:10:46 -05002662 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002663 unlock_up(p, level, lowest_unlock,
2664 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002665 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002666 }
2667 }
Chris Mason65b51a02008-08-01 15:11:20 -04002668 ret = 1;
2669done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002670 /*
2671 * we don't really know what they plan on doing with the path
2672 * from here on, so for now just mark it as blocking
2673 */
Chris Masonb9473432009-03-13 11:00:37 -04002674 if (!p->leave_spinning)
2675 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002676 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002677 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002678 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002679}
2680
Chris Mason74123bd2007-02-02 11:05:29 -05002681/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002682 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2683 * current state of the tree together with the operations recorded in the tree
2684 * modification log to search for the key in a previous version of this tree, as
2685 * denoted by the time_seq parameter.
2686 *
2687 * Naturally, there is no support for insert, delete or cow operations.
2688 *
2689 * The resulting path and return value will be set up as if we called
2690 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2691 */
2692int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2693 struct btrfs_path *p, u64 time_seq)
2694{
2695 struct extent_buffer *b;
2696 int slot;
2697 int ret;
2698 int err;
2699 int level;
2700 int lowest_unlock = 1;
2701 u8 lowest_level = 0;
2702
2703 lowest_level = p->lowest_level;
2704 WARN_ON(p->nodes[0] != NULL);
2705
2706 if (p->search_commit_root) {
2707 BUG_ON(time_seq);
2708 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2709 }
2710
2711again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002712 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002713 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002714 p->locks[level] = BTRFS_READ_LOCK;
2715
2716 while (b) {
2717 level = btrfs_header_level(b);
2718 p->nodes[level] = b;
2719 btrfs_clear_path_blocking(p, NULL, 0);
2720
2721 /*
2722 * we have a lock on b and as long as we aren't changing
2723 * the tree, there is no way to for the items in b to change.
2724 * It is safe to drop the lock on our parent before we
2725 * go through the expensive btree search on b.
2726 */
2727 btrfs_unlock_up_safe(p, level + 1);
2728
2729 ret = bin_search(b, key, level, &slot);
2730
2731 if (level != 0) {
2732 int dec = 0;
2733 if (ret && slot > 0) {
2734 dec = 1;
2735 slot -= 1;
2736 }
2737 p->slots[level] = slot;
2738 unlock_up(p, level, lowest_unlock, 0, NULL);
2739
2740 if (level == lowest_level) {
2741 if (dec)
2742 p->slots[level]++;
2743 goto done;
2744 }
2745
2746 err = read_block_for_search(NULL, root, p, &b, level,
2747 slot, key, time_seq);
2748 if (err == -EAGAIN)
2749 goto again;
2750 if (err) {
2751 ret = err;
2752 goto done;
2753 }
2754
2755 level = btrfs_header_level(b);
2756 err = btrfs_try_tree_read_lock(b);
2757 if (!err) {
2758 btrfs_set_path_blocking(p);
2759 btrfs_tree_read_lock(b);
2760 btrfs_clear_path_blocking(p, b,
2761 BTRFS_READ_LOCK);
2762 }
2763 p->locks[level] = BTRFS_READ_LOCK;
2764 p->nodes[level] = b;
2765 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2766 if (b != p->nodes[level]) {
2767 btrfs_tree_unlock_rw(p->nodes[level],
2768 p->locks[level]);
2769 p->locks[level] = 0;
2770 p->nodes[level] = b;
2771 }
2772 } else {
2773 p->slots[level] = slot;
2774 unlock_up(p, level, lowest_unlock, 0, NULL);
2775 goto done;
2776 }
2777 }
2778 ret = 1;
2779done:
2780 if (!p->leave_spinning)
2781 btrfs_set_path_blocking(p);
2782 if (ret < 0)
2783 btrfs_release_path(p);
2784
2785 return ret;
2786}
2787
2788/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002789 * helper to use instead of search slot if no exact match is needed but
2790 * instead the next or previous item should be returned.
2791 * When find_higher is true, the next higher item is returned, the next lower
2792 * otherwise.
2793 * When return_any and find_higher are both true, and no higher item is found,
2794 * return the next lower instead.
2795 * When return_any is true and find_higher is false, and no lower item is found,
2796 * return the next higher instead.
2797 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2798 * < 0 on error
2799 */
2800int btrfs_search_slot_for_read(struct btrfs_root *root,
2801 struct btrfs_key *key, struct btrfs_path *p,
2802 int find_higher, int return_any)
2803{
2804 int ret;
2805 struct extent_buffer *leaf;
2806
2807again:
2808 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2809 if (ret <= 0)
2810 return ret;
2811 /*
2812 * a return value of 1 means the path is at the position where the
2813 * item should be inserted. Normally this is the next bigger item,
2814 * but in case the previous item is the last in a leaf, path points
2815 * to the first free slot in the previous leaf, i.e. at an invalid
2816 * item.
2817 */
2818 leaf = p->nodes[0];
2819
2820 if (find_higher) {
2821 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2822 ret = btrfs_next_leaf(root, p);
2823 if (ret <= 0)
2824 return ret;
2825 if (!return_any)
2826 return 1;
2827 /*
2828 * no higher item found, return the next
2829 * lower instead
2830 */
2831 return_any = 0;
2832 find_higher = 0;
2833 btrfs_release_path(p);
2834 goto again;
2835 }
2836 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002837 if (p->slots[0] == 0) {
2838 ret = btrfs_prev_leaf(root, p);
2839 if (ret < 0)
2840 return ret;
2841 if (!ret) {
2842 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2843 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002844 }
Arne Jansene6793762011-09-13 11:18:10 +02002845 if (!return_any)
2846 return 1;
2847 /*
2848 * no lower item found, return the next
2849 * higher instead
2850 */
2851 return_any = 0;
2852 find_higher = 1;
2853 btrfs_release_path(p);
2854 goto again;
2855 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002856 --p->slots[0];
2857 }
2858 }
2859 return 0;
2860}
2861
2862/*
Chris Mason74123bd2007-02-02 11:05:29 -05002863 * adjust the pointers going up the tree, starting at level
2864 * making sure the right key of each node is points to 'key'.
2865 * This is used after shifting pointers to the left, so it stops
2866 * fixing up pointers when a given leaf/node is not in slot 0 of the
2867 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002868 *
Chris Mason74123bd2007-02-02 11:05:29 -05002869 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002870static void fixup_low_keys(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root, struct btrfs_path *path,
2872 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002873{
2874 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002875 struct extent_buffer *t;
2876
Chris Mason234b63a2007-03-13 10:46:10 -04002877 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002878 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002879 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002880 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002881 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002882 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002883 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002884 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002885 if (tslot != 0)
2886 break;
2887 }
2888}
2889
Chris Mason74123bd2007-02-02 11:05:29 -05002890/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002891 * update item key.
2892 *
2893 * This function isn't completely safe. It's the caller's responsibility
2894 * that the new key won't break the order
2895 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002896void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2897 struct btrfs_root *root, struct btrfs_path *path,
2898 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002899{
2900 struct btrfs_disk_key disk_key;
2901 struct extent_buffer *eb;
2902 int slot;
2903
2904 eb = path->nodes[0];
2905 slot = path->slots[0];
2906 if (slot > 0) {
2907 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002908 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002909 }
2910 if (slot < btrfs_header_nritems(eb) - 1) {
2911 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002912 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002913 }
2914
2915 btrfs_cpu_key_to_disk(&disk_key, new_key);
2916 btrfs_set_item_key(eb, &disk_key, slot);
2917 btrfs_mark_buffer_dirty(eb);
2918 if (slot == 0)
2919 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002920}
2921
2922/*
Chris Mason74123bd2007-02-02 11:05:29 -05002923 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002924 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002925 *
2926 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2927 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002928 */
Chris Mason98ed5172008-01-03 10:01:48 -05002929static int push_node_left(struct btrfs_trans_handle *trans,
2930 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002931 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002932{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002933 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002934 int src_nritems;
2935 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002936 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002937
Chris Mason5f39d392007-10-15 16:14:19 -04002938 src_nritems = btrfs_header_nritems(src);
2939 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002940 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002941 WARN_ON(btrfs_header_generation(src) != trans->transid);
2942 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002943
Chris Masonbce4eae2008-04-24 14:42:46 -04002944 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002945 return 1;
2946
Chris Masond3977122009-01-05 21:25:51 -05002947 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002948 return 1;
2949
Chris Masonbce4eae2008-04-24 14:42:46 -04002950 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002951 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002952 if (push_items < src_nritems) {
2953 /* leave at least 8 pointers in the node if
2954 * we aren't going to empty it
2955 */
2956 if (src_nritems - push_items < 8) {
2957 if (push_items <= 8)
2958 return 1;
2959 push_items -= 8;
2960 }
2961 }
2962 } else
2963 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002964
Jan Schmidtf2304752012-05-26 11:43:17 +02002965 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2966 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002967 copy_extent_buffer(dst, src,
2968 btrfs_node_key_ptr_offset(dst_nritems),
2969 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002970 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002971
Chris Masonbb803952007-03-01 12:04:21 -05002972 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002973 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2974 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002975 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2976 btrfs_node_key_ptr_offset(push_items),
2977 (src_nritems - push_items) *
2978 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002979 }
Chris Mason5f39d392007-10-15 16:14:19 -04002980 btrfs_set_header_nritems(src, src_nritems - push_items);
2981 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2982 btrfs_mark_buffer_dirty(src);
2983 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002984
Chris Masonbb803952007-03-01 12:04:21 -05002985 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002986}
2987
Chris Mason97571fd2007-02-24 13:39:08 -05002988/*
Chris Mason79f95c82007-03-01 15:16:26 -05002989 * try to push data from one node into the next node right in the
2990 * tree.
2991 *
2992 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2993 * error, and > 0 if there was no room in the right hand block.
2994 *
2995 * this will only push up to 1/2 the contents of the left node over
2996 */
Chris Mason5f39d392007-10-15 16:14:19 -04002997static int balance_node_right(struct btrfs_trans_handle *trans,
2998 struct btrfs_root *root,
2999 struct extent_buffer *dst,
3000 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003001{
Chris Mason79f95c82007-03-01 15:16:26 -05003002 int push_items = 0;
3003 int max_push;
3004 int src_nritems;
3005 int dst_nritems;
3006 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003007
Chris Mason7bb86312007-12-11 09:25:06 -05003008 WARN_ON(btrfs_header_generation(src) != trans->transid);
3009 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3010
Chris Mason5f39d392007-10-15 16:14:19 -04003011 src_nritems = btrfs_header_nritems(src);
3012 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003013 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003014 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003015 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003016
Chris Masond3977122009-01-05 21:25:51 -05003017 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003018 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003019
3020 max_push = src_nritems / 2 + 1;
3021 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003022 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003023 return 1;
Yan252c38f2007-08-29 09:11:44 -04003024
Chris Mason79f95c82007-03-01 15:16:26 -05003025 if (max_push < push_items)
3026 push_items = max_push;
3027
Jan Schmidtf2304752012-05-26 11:43:17 +02003028 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003029 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3030 btrfs_node_key_ptr_offset(0),
3031 (dst_nritems) *
3032 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003033
Jan Schmidtf2304752012-05-26 11:43:17 +02003034 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3035 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003036 copy_extent_buffer(dst, src,
3037 btrfs_node_key_ptr_offset(0),
3038 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003039 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003040
Chris Mason5f39d392007-10-15 16:14:19 -04003041 btrfs_set_header_nritems(src, src_nritems - push_items);
3042 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003043
Chris Mason5f39d392007-10-15 16:14:19 -04003044 btrfs_mark_buffer_dirty(src);
3045 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003046
Chris Mason79f95c82007-03-01 15:16:26 -05003047 return ret;
3048}
3049
3050/*
Chris Mason97571fd2007-02-24 13:39:08 -05003051 * helper function to insert a new root level in the tree.
3052 * A new node is allocated, and a single item is inserted to
3053 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003054 *
3055 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003056 */
Chris Masond3977122009-01-05 21:25:51 -05003057static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003058 struct btrfs_root *root,
3059 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003060{
Chris Mason7bb86312007-12-11 09:25:06 -05003061 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003062 struct extent_buffer *lower;
3063 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003064 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003065 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003066
3067 BUG_ON(path->nodes[level]);
3068 BUG_ON(path->nodes[level-1] != root->node);
3069
Chris Mason7bb86312007-12-11 09:25:06 -05003070 lower = path->nodes[level-1];
3071 if (level == 1)
3072 btrfs_item_key(lower, &lower_key, 0);
3073 else
3074 btrfs_node_key(lower, &lower_key, 0);
3075
Zheng Yan31840ae2008-09-23 13:14:14 -04003076 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003077 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003078 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003079 if (IS_ERR(c))
3080 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003081
Yan, Zhengf0486c62010-05-16 10:46:25 -04003082 root_add_used(root, root->nodesize);
3083
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003084 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003085 btrfs_set_header_nritems(c, 1);
3086 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003087 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003088 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003089 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003090 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003091
Chris Mason5f39d392007-10-15 16:14:19 -04003092 write_extent_buffer(c, root->fs_info->fsid,
3093 (unsigned long)btrfs_header_fsid(c),
3094 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003095
3096 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3097 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3098 BTRFS_UUID_SIZE);
3099
Chris Mason5f39d392007-10-15 16:14:19 -04003100 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003101 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003102 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003103 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003104
3105 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003106
3107 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003108
Chris Mason925baed2008-06-25 16:01:30 -04003109 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02003110 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04003111 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003112
3113 /* the super has an extra ref to root->node */
3114 free_extent_buffer(old);
3115
Chris Mason0b86a832008-03-24 15:01:56 -04003116 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003117 extent_buffer_get(c);
3118 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003119 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003120 path->slots[level] = 0;
3121 return 0;
3122}
3123
Chris Mason74123bd2007-02-02 11:05:29 -05003124/*
3125 * worker function to insert a single pointer in a node.
3126 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003127 *
Chris Mason74123bd2007-02-02 11:05:29 -05003128 * slot and level indicate where you want the key to go, and
3129 * blocknr is the block the key points to.
3130 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003131static void insert_ptr(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root, struct btrfs_path *path,
3133 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003134 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003135{
Chris Mason5f39d392007-10-15 16:14:19 -04003136 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003137 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003138 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003139
3140 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003141 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003142 lower = path->nodes[level];
3143 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003144 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003145 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003146 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003147 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003148 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3149 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003150 memmove_extent_buffer(lower,
3151 btrfs_node_key_ptr_offset(slot + 1),
3152 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003153 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003154 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003155 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003156 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3157 MOD_LOG_KEY_ADD);
3158 BUG_ON(ret < 0);
3159 }
Chris Mason5f39d392007-10-15 16:14:19 -04003160 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003161 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003162 WARN_ON(trans->transid == 0);
3163 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003164 btrfs_set_header_nritems(lower, nritems + 1);
3165 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003166}
3167
Chris Mason97571fd2007-02-24 13:39:08 -05003168/*
3169 * split the node at the specified level in path in two.
3170 * The path is corrected to point to the appropriate node after the split
3171 *
3172 * Before splitting this tries to make some room in the node by pushing
3173 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003174 *
3175 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003176 */
Chris Masone02119d2008-09-05 16:13:11 -04003177static noinline int split_node(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *root,
3179 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003180{
Chris Mason5f39d392007-10-15 16:14:19 -04003181 struct extent_buffer *c;
3182 struct extent_buffer *split;
3183 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003184 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003185 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003186 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003187
Chris Mason5f39d392007-10-15 16:14:19 -04003188 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003189 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003190 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003191 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003192 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003193 if (ret)
3194 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003195 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003196 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003197 c = path->nodes[level];
3198 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003199 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003200 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003201 if (ret < 0)
3202 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003203 }
Chris Masone66f7092007-04-20 13:16:02 -04003204
Chris Mason5f39d392007-10-15 16:14:19 -04003205 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003206 mid = (c_nritems + 1) / 2;
3207 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003209 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003210 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003211 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003212 if (IS_ERR(split))
3213 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003214
Yan, Zhengf0486c62010-05-16 10:46:25 -04003215 root_add_used(root, root->nodesize);
3216
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003217 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003218 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003219 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003220 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003221 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003222 btrfs_set_header_owner(split, root->root_key.objectid);
3223 write_extent_buffer(split, root->fs_info->fsid,
3224 (unsigned long)btrfs_header_fsid(split),
3225 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003226 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3227 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3228 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003229
Jan Schmidtf2304752012-05-26 11:43:17 +02003230 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003231 copy_extent_buffer(split, c,
3232 btrfs_node_key_ptr_offset(0),
3233 btrfs_node_key_ptr_offset(mid),
3234 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3235 btrfs_set_header_nritems(split, c_nritems - mid);
3236 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003237 ret = 0;
3238
Chris Mason5f39d392007-10-15 16:14:19 -04003239 btrfs_mark_buffer_dirty(c);
3240 btrfs_mark_buffer_dirty(split);
3241
Jeff Mahoney143bede2012-03-01 14:56:26 +01003242 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003243 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003244
Chris Mason5de08d72007-02-24 06:24:44 -05003245 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003246 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003247 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003248 free_extent_buffer(c);
3249 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003250 path->slots[level + 1] += 1;
3251 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003252 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003253 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003254 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003255 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003256}
3257
Chris Mason74123bd2007-02-02 11:05:29 -05003258/*
3259 * how many bytes are required to store the items in a leaf. start
3260 * and nr indicate which items in the leaf to check. This totals up the
3261 * space used both by the item structs and the item data
3262 */
Chris Mason5f39d392007-10-15 16:14:19 -04003263static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264{
3265 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003266 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003267 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003268
3269 if (!nr)
3270 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003271 data_len = btrfs_item_end_nr(l, start);
3272 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003273 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003274 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003275 return data_len;
3276}
3277
Chris Mason74123bd2007-02-02 11:05:29 -05003278/*
Chris Masond4dbff92007-04-04 14:08:15 -04003279 * The space between the end of the leaf items and
3280 * the start of the leaf data. IOW, how much room
3281 * the leaf has left for both items and data
3282 */
Chris Masond3977122009-01-05 21:25:51 -05003283noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003284 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003285{
Chris Mason5f39d392007-10-15 16:14:19 -04003286 int nritems = btrfs_header_nritems(leaf);
3287 int ret;
3288 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3289 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003290 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3291 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003292 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003293 leaf_space_used(leaf, 0, nritems), nritems);
3294 }
3295 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003296}
3297
Chris Mason99d8f832010-07-07 10:51:48 -04003298/*
3299 * min slot controls the lowest index we're willing to push to the
3300 * right. We'll push up to and including min_slot, but no lower
3301 */
Chris Mason44871b12009-03-13 10:04:31 -04003302static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3303 struct btrfs_root *root,
3304 struct btrfs_path *path,
3305 int data_size, int empty,
3306 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003307 int free_space, u32 left_nritems,
3308 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003309{
Chris Mason5f39d392007-10-15 16:14:19 -04003310 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003311 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003312 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003314 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003315 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003316 int push_space = 0;
3317 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003318 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003319 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003320 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003321 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003322 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003323
Chris Masoncfed81a2012-03-03 07:40:03 -05003324 btrfs_init_map_token(&token);
3325
Chris Mason34a38212007-11-07 13:31:03 -05003326 if (empty)
3327 nr = 0;
3328 else
Chris Mason99d8f832010-07-07 10:51:48 -04003329 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003330
Zheng Yan31840ae2008-09-23 13:14:14 -04003331 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003332 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003333
Chris Mason44871b12009-03-13 10:04:31 -04003334 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003335 i = left_nritems - 1;
3336 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003337 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003338
Zheng Yan31840ae2008-09-23 13:14:14 -04003339 if (!empty && push_items > 0) {
3340 if (path->slots[0] > i)
3341 break;
3342 if (path->slots[0] == i) {
3343 int space = btrfs_leaf_free_space(root, left);
3344 if (space + push_space * 2 > free_space)
3345 break;
3346 }
3347 }
3348
Chris Mason00ec4c52007-02-24 12:47:20 -05003349 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003350 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003351
Chris Masondb945352007-10-15 16:15:53 -04003352 this_item_size = btrfs_item_size(left, item);
3353 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003354 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003355
Chris Mason00ec4c52007-02-24 12:47:20 -05003356 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003357 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003358 if (i == 0)
3359 break;
3360 i--;
Chris Masondb945352007-10-15 16:15:53 -04003361 }
Chris Mason5f39d392007-10-15 16:14:19 -04003362
Chris Mason925baed2008-06-25 16:01:30 -04003363 if (push_items == 0)
3364 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003365
Chris Mason34a38212007-11-07 13:31:03 -05003366 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003367 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003368
Chris Mason00ec4c52007-02-24 12:47:20 -05003369 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003370 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003371
Chris Mason5f39d392007-10-15 16:14:19 -04003372 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003373 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003374
Chris Mason00ec4c52007-02-24 12:47:20 -05003375 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003376 data_end = leaf_data_end(root, right);
3377 memmove_extent_buffer(right,
3378 btrfs_leaf_data(right) + data_end - push_space,
3379 btrfs_leaf_data(right) + data_end,
3380 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3381
Chris Mason00ec4c52007-02-24 12:47:20 -05003382 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003383 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003384 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3385 btrfs_leaf_data(left) + leaf_data_end(root, left),
3386 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003387
3388 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3389 btrfs_item_nr_offset(0),
3390 right_nritems * sizeof(struct btrfs_item));
3391
Chris Mason00ec4c52007-02-24 12:47:20 -05003392 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003393 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3394 btrfs_item_nr_offset(left_nritems - push_items),
3395 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003396
3397 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003398 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003399 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003400 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003401 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003402 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003403 push_space -= btrfs_token_item_size(right, item, &token);
3404 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003405 }
3406
Chris Mason7518a232007-03-12 12:01:18 -04003407 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003408 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003409
Chris Mason34a38212007-11-07 13:31:03 -05003410 if (left_nritems)
3411 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003412 else
3413 clean_tree_block(trans, root, left);
3414
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003416
Chris Mason5f39d392007-10-15 16:14:19 -04003417 btrfs_item_key(right, &disk_key, 0);
3418 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003419 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003420
Chris Mason00ec4c52007-02-24 12:47:20 -05003421 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003422 if (path->slots[0] >= left_nritems) {
3423 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003424 if (btrfs_header_nritems(path->nodes[0]) == 0)
3425 clean_tree_block(trans, root, path->nodes[0]);
3426 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003427 free_extent_buffer(path->nodes[0]);
3428 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003429 path->slots[1] += 1;
3430 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003431 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003432 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003433 }
3434 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003435
3436out_unlock:
3437 btrfs_tree_unlock(right);
3438 free_extent_buffer(right);
3439 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003440}
Chris Mason925baed2008-06-25 16:01:30 -04003441
Chris Mason00ec4c52007-02-24 12:47:20 -05003442/*
Chris Mason44871b12009-03-13 10:04:31 -04003443 * push some data in the path leaf to the right, trying to free up at
3444 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3445 *
3446 * returns 1 if the push failed because the other node didn't have enough
3447 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003448 *
3449 * this will push starting from min_slot to the end of the leaf. It won't
3450 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003451 */
3452static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003453 *root, struct btrfs_path *path,
3454 int min_data_size, int data_size,
3455 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003456{
3457 struct extent_buffer *left = path->nodes[0];
3458 struct extent_buffer *right;
3459 struct extent_buffer *upper;
3460 int slot;
3461 int free_space;
3462 u32 left_nritems;
3463 int ret;
3464
3465 if (!path->nodes[1])
3466 return 1;
3467
3468 slot = path->slots[1];
3469 upper = path->nodes[1];
3470 if (slot >= btrfs_header_nritems(upper) - 1)
3471 return 1;
3472
3473 btrfs_assert_tree_locked(path->nodes[1]);
3474
3475 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003476 if (right == NULL)
3477 return 1;
3478
Chris Mason44871b12009-03-13 10:04:31 -04003479 btrfs_tree_lock(right);
3480 btrfs_set_lock_blocking(right);
3481
3482 free_space = btrfs_leaf_free_space(root, right);
3483 if (free_space < data_size)
3484 goto out_unlock;
3485
3486 /* cow and double check */
3487 ret = btrfs_cow_block(trans, root, right, upper,
3488 slot + 1, &right);
3489 if (ret)
3490 goto out_unlock;
3491
3492 free_space = btrfs_leaf_free_space(root, right);
3493 if (free_space < data_size)
3494 goto out_unlock;
3495
3496 left_nritems = btrfs_header_nritems(left);
3497 if (left_nritems == 0)
3498 goto out_unlock;
3499
Chris Mason99d8f832010-07-07 10:51:48 -04003500 return __push_leaf_right(trans, root, path, min_data_size, empty,
3501 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003502out_unlock:
3503 btrfs_tree_unlock(right);
3504 free_extent_buffer(right);
3505 return 1;
3506}
3507
3508/*
Chris Mason74123bd2007-02-02 11:05:29 -05003509 * push some data in the path leaf to the left, trying to free up at
3510 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003511 *
3512 * max_slot can put a limit on how far into the leaf we'll push items. The
3513 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3514 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003515 */
Chris Mason44871b12009-03-13 10:04:31 -04003516static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3517 struct btrfs_root *root,
3518 struct btrfs_path *path, int data_size,
3519 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003520 int free_space, u32 right_nritems,
3521 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522{
Chris Mason5f39d392007-10-15 16:14:19 -04003523 struct btrfs_disk_key disk_key;
3524 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003525 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003526 int push_space = 0;
3527 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003528 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003529 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003530 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003531 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003532 u32 this_item_size;
3533 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003534 struct btrfs_map_token token;
3535
3536 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003537
Chris Mason34a38212007-11-07 13:31:03 -05003538 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003539 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003540 else
Chris Mason99d8f832010-07-07 10:51:48 -04003541 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003542
3543 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003544 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003545
Zheng Yan31840ae2008-09-23 13:14:14 -04003546 if (!empty && push_items > 0) {
3547 if (path->slots[0] < i)
3548 break;
3549 if (path->slots[0] == i) {
3550 int space = btrfs_leaf_free_space(root, right);
3551 if (space + push_space * 2 > free_space)
3552 break;
3553 }
3554 }
3555
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003557 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003558
3559 this_item_size = btrfs_item_size(right, item);
3560 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003561 break;
Chris Masondb945352007-10-15 16:15:53 -04003562
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003564 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003565 }
Chris Masondb945352007-10-15 16:15:53 -04003566
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003568 ret = 1;
3569 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003570 }
Chris Mason34a38212007-11-07 13:31:03 -05003571 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003572 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003573
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003575 copy_extent_buffer(left, right,
3576 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3577 btrfs_item_nr_offset(0),
3578 push_items * sizeof(struct btrfs_item));
3579
Chris Mason123abc82007-03-14 14:14:43 -04003580 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003581 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003582
3583 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003584 leaf_data_end(root, left) - push_space,
3585 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003586 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003587 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003588 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003589 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003590
Chris Masondb945352007-10-15 16:15:53 -04003591 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003592 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003593 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003594
Chris Mason5f39d392007-10-15 16:14:19 -04003595 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003596
Chris Masoncfed81a2012-03-03 07:40:03 -05003597 ioff = btrfs_token_item_offset(left, item, &token);
3598 btrfs_set_token_item_offset(left, item,
3599 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3600 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003601 }
Chris Mason5f39d392007-10-15 16:14:19 -04003602 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003603
3604 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003605 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003606 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3607 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003608 WARN_ON(1);
3609 }
Chris Mason5f39d392007-10-15 16:14:19 -04003610
Chris Mason34a38212007-11-07 13:31:03 -05003611 if (push_items < right_nritems) {
3612 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3613 leaf_data_end(root, right);
3614 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3615 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3616 btrfs_leaf_data(right) +
3617 leaf_data_end(root, right), push_space);
3618
3619 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003620 btrfs_item_nr_offset(push_items),
3621 (btrfs_header_nritems(right) - push_items) *
3622 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003623 }
Yaneef1c492007-11-26 10:58:13 -05003624 right_nritems -= push_items;
3625 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003626 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003627 for (i = 0; i < right_nritems; i++) {
3628 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003629
Chris Masoncfed81a2012-03-03 07:40:03 -05003630 push_space = push_space - btrfs_token_item_size(right,
3631 item, &token);
3632 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003633 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003634
Chris Mason5f39d392007-10-15 16:14:19 -04003635 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003636 if (right_nritems)
3637 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003638 else
3639 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003640
Chris Mason5f39d392007-10-15 16:14:19 -04003641 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003642 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003643
3644 /* then fixup the leaf pointer in the path */
3645 if (path->slots[0] < push_items) {
3646 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003647 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003648 free_extent_buffer(path->nodes[0]);
3649 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003650 path->slots[1] -= 1;
3651 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003652 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003653 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003654 path->slots[0] -= push_items;
3655 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003656 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003657 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003658out:
3659 btrfs_tree_unlock(left);
3660 free_extent_buffer(left);
3661 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003662}
3663
Chris Mason74123bd2007-02-02 11:05:29 -05003664/*
Chris Mason44871b12009-03-13 10:04:31 -04003665 * push some data in the path leaf to the left, trying to free up at
3666 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003667 *
3668 * max_slot can put a limit on how far into the leaf we'll push items. The
3669 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3670 * items
Chris Mason44871b12009-03-13 10:04:31 -04003671 */
3672static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003673 *root, struct btrfs_path *path, int min_data_size,
3674 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003675{
3676 struct extent_buffer *right = path->nodes[0];
3677 struct extent_buffer *left;
3678 int slot;
3679 int free_space;
3680 u32 right_nritems;
3681 int ret = 0;
3682
3683 slot = path->slots[1];
3684 if (slot == 0)
3685 return 1;
3686 if (!path->nodes[1])
3687 return 1;
3688
3689 right_nritems = btrfs_header_nritems(right);
3690 if (right_nritems == 0)
3691 return 1;
3692
3693 btrfs_assert_tree_locked(path->nodes[1]);
3694
3695 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003696 if (left == NULL)
3697 return 1;
3698
Chris Mason44871b12009-03-13 10:04:31 -04003699 btrfs_tree_lock(left);
3700 btrfs_set_lock_blocking(left);
3701
3702 free_space = btrfs_leaf_free_space(root, left);
3703 if (free_space < data_size) {
3704 ret = 1;
3705 goto out;
3706 }
3707
3708 /* cow and double check */
3709 ret = btrfs_cow_block(trans, root, left,
3710 path->nodes[1], slot - 1, &left);
3711 if (ret) {
3712 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (ret == -ENOSPC)
3714 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003715 goto out;
3716 }
3717
3718 free_space = btrfs_leaf_free_space(root, left);
3719 if (free_space < data_size) {
3720 ret = 1;
3721 goto out;
3722 }
3723
Chris Mason99d8f832010-07-07 10:51:48 -04003724 return __push_leaf_left(trans, root, path, min_data_size,
3725 empty, left, free_space, right_nritems,
3726 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003727out:
3728 btrfs_tree_unlock(left);
3729 free_extent_buffer(left);
3730 return ret;
3731}
3732
3733/*
Chris Mason74123bd2007-02-02 11:05:29 -05003734 * split the path's leaf in two, making sure there is at least data_size
3735 * available for the resulting leaf level of the path.
3736 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003737static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3738 struct btrfs_root *root,
3739 struct btrfs_path *path,
3740 struct extent_buffer *l,
3741 struct extent_buffer *right,
3742 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744 int data_copy_size;
3745 int rt_data_off;
3746 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003747 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003748 struct btrfs_map_token token;
3749
3750 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003751
Chris Mason5f39d392007-10-15 16:14:19 -04003752 nritems = nritems - mid;
3753 btrfs_set_header_nritems(right, nritems);
3754 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3755
3756 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3757 btrfs_item_nr_offset(mid),
3758 nritems * sizeof(struct btrfs_item));
3759
3760 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003761 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3762 data_copy_size, btrfs_leaf_data(l) +
3763 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003764
Chris Mason5f39d392007-10-15 16:14:19 -04003765 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3766 btrfs_item_end_nr(l, mid);
3767
3768 for (i = 0; i < nritems; i++) {
3769 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003770 u32 ioff;
3771
Chris Masoncfed81a2012-03-03 07:40:03 -05003772 ioff = btrfs_token_item_offset(right, item, &token);
3773 btrfs_set_token_item_offset(right, item,
3774 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003775 }
Chris Mason74123bd2007-02-02 11:05:29 -05003776
Chris Mason5f39d392007-10-15 16:14:19 -04003777 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003778 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003779 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003780 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
3782 btrfs_mark_buffer_dirty(right);
3783 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003784 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003785
Chris Masonbe0e5c02007-01-26 15:51:26 -05003786 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003787 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003788 free_extent_buffer(path->nodes[0]);
3789 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003790 path->slots[0] -= mid;
3791 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003792 } else {
3793 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003794 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003795 }
Chris Mason5f39d392007-10-15 16:14:19 -04003796
Chris Masoneb60cea2007-02-02 09:18:22 -05003797 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003798}
3799
3800/*
Chris Mason99d8f832010-07-07 10:51:48 -04003801 * double splits happen when we need to insert a big item in the middle
3802 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3803 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3804 * A B C
3805 *
3806 * We avoid this by trying to push the items on either side of our target
3807 * into the adjacent leaves. If all goes well we can avoid the double split
3808 * completely.
3809 */
3810static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3811 struct btrfs_root *root,
3812 struct btrfs_path *path,
3813 int data_size)
3814{
3815 int ret;
3816 int progress = 0;
3817 int slot;
3818 u32 nritems;
3819
3820 slot = path->slots[0];
3821
3822 /*
3823 * try to push all the items after our slot into the
3824 * right leaf
3825 */
3826 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3827 if (ret < 0)
3828 return ret;
3829
3830 if (ret == 0)
3831 progress++;
3832
3833 nritems = btrfs_header_nritems(path->nodes[0]);
3834 /*
3835 * our goal is to get our slot at the start or end of a leaf. If
3836 * we've done so we're done
3837 */
3838 if (path->slots[0] == 0 || path->slots[0] == nritems)
3839 return 0;
3840
3841 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3842 return 0;
3843
3844 /* try to push all the items before our slot into the next leaf */
3845 slot = path->slots[0];
3846 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3847 if (ret < 0)
3848 return ret;
3849
3850 if (ret == 0)
3851 progress++;
3852
3853 if (progress)
3854 return 0;
3855 return 1;
3856}
3857
3858/*
Chris Mason44871b12009-03-13 10:04:31 -04003859 * split the path's leaf in two, making sure there is at least data_size
3860 * available for the resulting leaf level of the path.
3861 *
3862 * returns 0 if all went well and < 0 on failure.
3863 */
3864static noinline int split_leaf(struct btrfs_trans_handle *trans,
3865 struct btrfs_root *root,
3866 struct btrfs_key *ins_key,
3867 struct btrfs_path *path, int data_size,
3868 int extend)
3869{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003871 struct extent_buffer *l;
3872 u32 nritems;
3873 int mid;
3874 int slot;
3875 struct extent_buffer *right;
3876 int ret = 0;
3877 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003878 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003879 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003880 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003881
Yan, Zhenga5719522009-09-24 09:17:31 -04003882 l = path->nodes[0];
3883 slot = path->slots[0];
3884 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3885 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3886 return -EOVERFLOW;
3887
Chris Mason44871b12009-03-13 10:04:31 -04003888 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003889 if (data_size) {
3890 wret = push_leaf_right(trans, root, path, data_size,
3891 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003892 if (wret < 0)
3893 return wret;
3894 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003895 wret = push_leaf_left(trans, root, path, data_size,
3896 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003897 if (wret < 0)
3898 return wret;
3899 }
3900 l = path->nodes[0];
3901
3902 /* did the pushes work? */
3903 if (btrfs_leaf_free_space(root, l) >= data_size)
3904 return 0;
3905 }
3906
3907 if (!path->nodes[1]) {
3908 ret = insert_new_root(trans, root, path, 1);
3909 if (ret)
3910 return ret;
3911 }
3912again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003914 l = path->nodes[0];
3915 slot = path->slots[0];
3916 nritems = btrfs_header_nritems(l);
3917 mid = (nritems + 1) / 2;
3918
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003919 if (mid <= slot) {
3920 if (nritems == 1 ||
3921 leaf_space_used(l, mid, nritems - mid) + data_size >
3922 BTRFS_LEAF_DATA_SIZE(root)) {
3923 if (slot >= nritems) {
3924 split = 0;
3925 } else {
3926 mid = slot;
3927 if (mid != nritems &&
3928 leaf_space_used(l, mid, nritems - mid) +
3929 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003930 if (data_size && !tried_avoid_double)
3931 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003932 split = 2;
3933 }
3934 }
3935 }
3936 } else {
3937 if (leaf_space_used(l, 0, mid) + data_size >
3938 BTRFS_LEAF_DATA_SIZE(root)) {
3939 if (!extend && data_size && slot == 0) {
3940 split = 0;
3941 } else if ((extend || !data_size) && slot == 0) {
3942 mid = 1;
3943 } else {
3944 mid = slot;
3945 if (mid != nritems &&
3946 leaf_space_used(l, mid, nritems - mid) +
3947 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003948 if (data_size && !tried_avoid_double)
3949 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003950 split = 2 ;
3951 }
3952 }
3953 }
3954 }
3955
3956 if (split == 0)
3957 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3958 else
3959 btrfs_item_key(l, &disk_key, mid);
3960
3961 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003962 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003963 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003964 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003965 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003966
3967 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003968
3969 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3970 btrfs_set_header_bytenr(right, right->start);
3971 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003973 btrfs_set_header_owner(right, root->root_key.objectid);
3974 btrfs_set_header_level(right, 0);
3975 write_extent_buffer(right, root->fs_info->fsid,
3976 (unsigned long)btrfs_header_fsid(right),
3977 BTRFS_FSID_SIZE);
3978
3979 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3980 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3981 BTRFS_UUID_SIZE);
3982
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003983 if (split == 0) {
3984 if (mid <= slot) {
3985 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003986 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003987 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 btrfs_tree_unlock(path->nodes[0]);
3989 free_extent_buffer(path->nodes[0]);
3990 path->nodes[0] = right;
3991 path->slots[0] = 0;
3992 path->slots[1] += 1;
3993 } else {
3994 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003995 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003996 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003997 btrfs_tree_unlock(path->nodes[0]);
3998 free_extent_buffer(path->nodes[0]);
3999 path->nodes[0] = right;
4000 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004001 if (path->slots[1] == 0)
4002 fixup_low_keys(trans, root, path,
4003 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004004 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004005 btrfs_mark_buffer_dirty(right);
4006 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004007 }
4008
Jeff Mahoney143bede2012-03-01 14:56:26 +01004009 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004012 BUG_ON(num_doubles != 0);
4013 num_doubles++;
4014 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004015 }
Chris Mason44871b12009-03-13 10:04:31 -04004016
Jeff Mahoney143bede2012-03-01 14:56:26 +01004017 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004018
4019push_for_double:
4020 push_for_double_split(trans, root, path, data_size);
4021 tried_avoid_double = 1;
4022 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4023 return 0;
4024 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004025}
4026
Yan, Zhengad48fd752009-11-12 09:33:58 +00004027static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4028 struct btrfs_root *root,
4029 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004030{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004031 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004032 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004033 struct btrfs_file_extent_item *fi;
4034 u64 extent_len = 0;
4035 u32 item_size;
4036 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004037
4038 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004039 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4040
4041 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4042 key.type != BTRFS_EXTENT_CSUM_KEY);
4043
4044 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4045 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004046
4047 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004048 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4049 fi = btrfs_item_ptr(leaf, path->slots[0],
4050 struct btrfs_file_extent_item);
4051 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4052 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004053 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004054
Chris Mason459931e2008-12-10 09:10:46 -05004055 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004056 path->search_for_split = 1;
4057 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004058 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004059 if (ret < 0)
4060 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004061
Yan, Zhengad48fd752009-11-12 09:33:58 +00004062 ret = -EAGAIN;
4063 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004064 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004065 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4066 goto err;
4067
Chris Mason109f6ae2010-04-02 09:20:18 -04004068 /* the leaf has changed, it now has room. return now */
4069 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4070 goto err;
4071
Yan, Zhengad48fd752009-11-12 09:33:58 +00004072 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4073 fi = btrfs_item_ptr(leaf, path->slots[0],
4074 struct btrfs_file_extent_item);
4075 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4076 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004077 }
4078
Chris Masonb9473432009-03-13 11:00:37 -04004079 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004080 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004081 if (ret)
4082 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004083
Yan, Zhengad48fd752009-11-12 09:33:58 +00004084 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004085 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004086 return 0;
4087err:
4088 path->keep_locks = 0;
4089 return ret;
4090}
4091
4092static noinline int split_item(struct btrfs_trans_handle *trans,
4093 struct btrfs_root *root,
4094 struct btrfs_path *path,
4095 struct btrfs_key *new_key,
4096 unsigned long split_offset)
4097{
4098 struct extent_buffer *leaf;
4099 struct btrfs_item *item;
4100 struct btrfs_item *new_item;
4101 int slot;
4102 char *buf;
4103 u32 nritems;
4104 u32 item_size;
4105 u32 orig_offset;
4106 struct btrfs_disk_key disk_key;
4107
Chris Masonb9473432009-03-13 11:00:37 -04004108 leaf = path->nodes[0];
4109 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4110
Chris Masonb4ce94d2009-02-04 09:25:08 -05004111 btrfs_set_path_blocking(path);
4112
Chris Mason459931e2008-12-10 09:10:46 -05004113 item = btrfs_item_nr(leaf, path->slots[0]);
4114 orig_offset = btrfs_item_offset(leaf, item);
4115 item_size = btrfs_item_size(leaf, item);
4116
Chris Mason459931e2008-12-10 09:10:46 -05004117 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004118 if (!buf)
4119 return -ENOMEM;
4120
Chris Mason459931e2008-12-10 09:10:46 -05004121 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4122 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004123
Chris Mason459931e2008-12-10 09:10:46 -05004124 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004125 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004126 if (slot != nritems) {
4127 /* shift the items */
4128 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004129 btrfs_item_nr_offset(slot),
4130 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004131 }
4132
4133 btrfs_cpu_key_to_disk(&disk_key, new_key);
4134 btrfs_set_item_key(leaf, &disk_key, slot);
4135
4136 new_item = btrfs_item_nr(leaf, slot);
4137
4138 btrfs_set_item_offset(leaf, new_item, orig_offset);
4139 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4140
4141 btrfs_set_item_offset(leaf, item,
4142 orig_offset + item_size - split_offset);
4143 btrfs_set_item_size(leaf, item, split_offset);
4144
4145 btrfs_set_header_nritems(leaf, nritems + 1);
4146
4147 /* write the data for the start of the original item */
4148 write_extent_buffer(leaf, buf,
4149 btrfs_item_ptr_offset(leaf, path->slots[0]),
4150 split_offset);
4151
4152 /* write the data for the new item */
4153 write_extent_buffer(leaf, buf + split_offset,
4154 btrfs_item_ptr_offset(leaf, slot),
4155 item_size - split_offset);
4156 btrfs_mark_buffer_dirty(leaf);
4157
Yan, Zhengad48fd752009-11-12 09:33:58 +00004158 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004159 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004160 return 0;
4161}
4162
4163/*
4164 * This function splits a single item into two items,
4165 * giving 'new_key' to the new item and splitting the
4166 * old one at split_offset (from the start of the item).
4167 *
4168 * The path may be released by this operation. After
4169 * the split, the path is pointing to the old item. The
4170 * new item is going to be in the same node as the old one.
4171 *
4172 * Note, the item being split must be smaller enough to live alone on
4173 * a tree block with room for one extra struct btrfs_item
4174 *
4175 * This allows us to split the item in place, keeping a lock on the
4176 * leaf the entire time.
4177 */
4178int btrfs_split_item(struct btrfs_trans_handle *trans,
4179 struct btrfs_root *root,
4180 struct btrfs_path *path,
4181 struct btrfs_key *new_key,
4182 unsigned long split_offset)
4183{
4184 int ret;
4185 ret = setup_leaf_for_split(trans, root, path,
4186 sizeof(struct btrfs_item));
4187 if (ret)
4188 return ret;
4189
4190 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004191 return ret;
4192}
4193
4194/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004195 * This function duplicate a item, giving 'new_key' to the new item.
4196 * It guarantees both items live in the same tree leaf and the new item
4197 * is contiguous with the original item.
4198 *
4199 * This allows us to split file extent in place, keeping a lock on the
4200 * leaf the entire time.
4201 */
4202int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4203 struct btrfs_root *root,
4204 struct btrfs_path *path,
4205 struct btrfs_key *new_key)
4206{
4207 struct extent_buffer *leaf;
4208 int ret;
4209 u32 item_size;
4210
4211 leaf = path->nodes[0];
4212 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4213 ret = setup_leaf_for_split(trans, root, path,
4214 item_size + sizeof(struct btrfs_item));
4215 if (ret)
4216 return ret;
4217
4218 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004219 setup_items_for_insert(trans, root, path, new_key, &item_size,
4220 item_size, item_size +
4221 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004222 leaf = path->nodes[0];
4223 memcpy_extent_buffer(leaf,
4224 btrfs_item_ptr_offset(leaf, path->slots[0]),
4225 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4226 item_size);
4227 return 0;
4228}
4229
4230/*
Chris Masond352ac62008-09-29 15:18:18 -04004231 * make the item pointed to by the path smaller. new_size indicates
4232 * how small to make it, and from_end tells us if we just chop bytes
4233 * off the end of the item or if we shift the item to chop bytes off
4234 * the front.
4235 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004236void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4237 struct btrfs_root *root,
4238 struct btrfs_path *path,
4239 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004240{
Chris Masonb18c6682007-04-17 13:26:50 -04004241 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004242 struct extent_buffer *leaf;
4243 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004244 u32 nritems;
4245 unsigned int data_end;
4246 unsigned int old_data_start;
4247 unsigned int old_size;
4248 unsigned int size_diff;
4249 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004250 struct btrfs_map_token token;
4251
4252 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004253
Chris Mason5f39d392007-10-15 16:14:19 -04004254 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004255 slot = path->slots[0];
4256
4257 old_size = btrfs_item_size_nr(leaf, slot);
4258 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004259 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004260
Chris Mason5f39d392007-10-15 16:14:19 -04004261 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004262 data_end = leaf_data_end(root, leaf);
4263
Chris Mason5f39d392007-10-15 16:14:19 -04004264 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004265
Chris Masonb18c6682007-04-17 13:26:50 -04004266 size_diff = old_size - new_size;
4267
4268 BUG_ON(slot < 0);
4269 BUG_ON(slot >= nritems);
4270
4271 /*
4272 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4273 */
4274 /* first correct the data pointers */
4275 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004276 u32 ioff;
4277 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004278
Chris Masoncfed81a2012-03-03 07:40:03 -05004279 ioff = btrfs_token_item_offset(leaf, item, &token);
4280 btrfs_set_token_item_offset(leaf, item,
4281 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004282 }
Chris Masondb945352007-10-15 16:15:53 -04004283
Chris Masonb18c6682007-04-17 13:26:50 -04004284 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004285 if (from_end) {
4286 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4287 data_end + size_diff, btrfs_leaf_data(leaf) +
4288 data_end, old_data_start + new_size - data_end);
4289 } else {
4290 struct btrfs_disk_key disk_key;
4291 u64 offset;
4292
4293 btrfs_item_key(leaf, &disk_key, slot);
4294
4295 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4296 unsigned long ptr;
4297 struct btrfs_file_extent_item *fi;
4298
4299 fi = btrfs_item_ptr(leaf, slot,
4300 struct btrfs_file_extent_item);
4301 fi = (struct btrfs_file_extent_item *)(
4302 (unsigned long)fi - size_diff);
4303
4304 if (btrfs_file_extent_type(leaf, fi) ==
4305 BTRFS_FILE_EXTENT_INLINE) {
4306 ptr = btrfs_item_ptr_offset(leaf, slot);
4307 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004308 (unsigned long)fi,
4309 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004310 disk_bytenr));
4311 }
4312 }
4313
4314 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4315 data_end + size_diff, btrfs_leaf_data(leaf) +
4316 data_end, old_data_start - data_end);
4317
4318 offset = btrfs_disk_key_offset(&disk_key);
4319 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4320 btrfs_set_item_key(leaf, &disk_key, slot);
4321 if (slot == 0)
4322 fixup_low_keys(trans, root, path, &disk_key, 1);
4323 }
Chris Mason5f39d392007-10-15 16:14:19 -04004324
4325 item = btrfs_item_nr(leaf, slot);
4326 btrfs_set_item_size(leaf, item, new_size);
4327 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004328
Chris Mason5f39d392007-10-15 16:14:19 -04004329 if (btrfs_leaf_free_space(root, leaf) < 0) {
4330 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004331 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004332 }
Chris Masonb18c6682007-04-17 13:26:50 -04004333}
4334
Chris Masond352ac62008-09-29 15:18:18 -04004335/*
4336 * make the item pointed to by the path bigger, data_size is the new size.
4337 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004338void btrfs_extend_item(struct btrfs_trans_handle *trans,
4339 struct btrfs_root *root, struct btrfs_path *path,
4340 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004341{
Chris Mason6567e832007-04-16 09:22:45 -04004342 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004343 struct extent_buffer *leaf;
4344 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004345 u32 nritems;
4346 unsigned int data_end;
4347 unsigned int old_data;
4348 unsigned int old_size;
4349 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004350 struct btrfs_map_token token;
4351
4352 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004353
Chris Mason5f39d392007-10-15 16:14:19 -04004354 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004355
Chris Mason5f39d392007-10-15 16:14:19 -04004356 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004357 data_end = leaf_data_end(root, leaf);
4358
Chris Mason5f39d392007-10-15 16:14:19 -04004359 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4360 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004361 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004362 }
Chris Mason6567e832007-04-16 09:22:45 -04004363 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004364 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004365
4366 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004367 if (slot >= nritems) {
4368 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004369 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4370 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004371 BUG_ON(1);
4372 }
Chris Mason6567e832007-04-16 09:22:45 -04004373
4374 /*
4375 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4376 */
4377 /* first correct the data pointers */
4378 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004379 u32 ioff;
4380 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004381
Chris Masoncfed81a2012-03-03 07:40:03 -05004382 ioff = btrfs_token_item_offset(leaf, item, &token);
4383 btrfs_set_token_item_offset(leaf, item,
4384 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004385 }
Chris Mason5f39d392007-10-15 16:14:19 -04004386
Chris Mason6567e832007-04-16 09:22:45 -04004387 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004388 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004389 data_end - data_size, btrfs_leaf_data(leaf) +
4390 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004391
Chris Mason6567e832007-04-16 09:22:45 -04004392 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004393 old_size = btrfs_item_size_nr(leaf, slot);
4394 item = btrfs_item_nr(leaf, slot);
4395 btrfs_set_item_size(leaf, item, old_size + data_size);
4396 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004397
Chris Mason5f39d392007-10-15 16:14:19 -04004398 if (btrfs_leaf_free_space(root, leaf) < 0) {
4399 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004400 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004401 }
Chris Mason6567e832007-04-16 09:22:45 -04004402}
4403
Chris Mason74123bd2007-02-02 11:05:29 -05004404/*
Chris Masond352ac62008-09-29 15:18:18 -04004405 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004406 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004407 * Returns the number of keys that were inserted.
4408 */
4409int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4410 struct btrfs_root *root,
4411 struct btrfs_path *path,
4412 struct btrfs_key *cpu_key, u32 *data_size,
4413 int nr)
4414{
4415 struct extent_buffer *leaf;
4416 struct btrfs_item *item;
4417 int ret = 0;
4418 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004419 int i;
4420 u32 nritems;
4421 u32 total_data = 0;
4422 u32 total_size = 0;
4423 unsigned int data_end;
4424 struct btrfs_disk_key disk_key;
4425 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004426 struct btrfs_map_token token;
4427
4428 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004429
Yan Zheng87b29b22008-12-17 10:21:48 -05004430 for (i = 0; i < nr; i++) {
4431 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4432 BTRFS_LEAF_DATA_SIZE(root)) {
4433 break;
4434 nr = i;
4435 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004436 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004437 total_size += data_size[i] + sizeof(struct btrfs_item);
4438 }
4439 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004440
Josef Bacikf3465ca2008-11-12 14:19:50 -05004441 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4442 if (ret == 0)
4443 return -EEXIST;
4444 if (ret < 0)
4445 goto out;
4446
Josef Bacikf3465ca2008-11-12 14:19:50 -05004447 leaf = path->nodes[0];
4448
4449 nritems = btrfs_header_nritems(leaf);
4450 data_end = leaf_data_end(root, leaf);
4451
4452 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4453 for (i = nr; i >= 0; i--) {
4454 total_data -= data_size[i];
4455 total_size -= data_size[i] + sizeof(struct btrfs_item);
4456 if (total_size < btrfs_leaf_free_space(root, leaf))
4457 break;
4458 }
4459 nr = i;
4460 }
4461
4462 slot = path->slots[0];
4463 BUG_ON(slot < 0);
4464
4465 if (slot != nritems) {
4466 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4467
4468 item = btrfs_item_nr(leaf, slot);
4469 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4470
4471 /* figure out how many keys we can insert in here */
4472 total_data = data_size[0];
4473 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004474 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004475 break;
4476 total_data += data_size[i];
4477 }
4478 nr = i;
4479
4480 if (old_data < data_end) {
4481 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004482 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004483 slot, old_data, data_end);
4484 BUG_ON(1);
4485 }
4486 /*
4487 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4488 */
4489 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004490 for (i = slot; i < nritems; i++) {
4491 u32 ioff;
4492
4493 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004494 ioff = btrfs_token_item_offset(leaf, item, &token);
4495 btrfs_set_token_item_offset(leaf, item,
4496 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004497 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004498 /* shift the items */
4499 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4500 btrfs_item_nr_offset(slot),
4501 (nritems - slot) * sizeof(struct btrfs_item));
4502
4503 /* shift the data */
4504 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4505 data_end - total_data, btrfs_leaf_data(leaf) +
4506 data_end, old_data - data_end);
4507 data_end = old_data;
4508 } else {
4509 /*
4510 * this sucks but it has to be done, if we are inserting at
4511 * the end of the leaf only insert 1 of the items, since we
4512 * have no way of knowing whats on the next leaf and we'd have
4513 * to drop our current locks to figure it out
4514 */
4515 nr = 1;
4516 }
4517
4518 /* setup the item for the new data */
4519 for (i = 0; i < nr; i++) {
4520 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4521 btrfs_set_item_key(leaf, &disk_key, slot + i);
4522 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004523 btrfs_set_token_item_offset(leaf, item,
4524 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004525 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004526 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004527 }
4528 btrfs_set_header_nritems(leaf, nritems + nr);
4529 btrfs_mark_buffer_dirty(leaf);
4530
4531 ret = 0;
4532 if (slot == 0) {
4533 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004534 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004535 }
4536
4537 if (btrfs_leaf_free_space(root, leaf) < 0) {
4538 btrfs_print_leaf(root, leaf);
4539 BUG();
4540 }
4541out:
4542 if (!ret)
4543 ret = nr;
4544 return ret;
4545}
4546
4547/*
Chris Mason44871b12009-03-13 10:04:31 -04004548 * this is a helper for btrfs_insert_empty_items, the main goal here is
4549 * to save stack depth by doing the bulk of the work in a function
4550 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004551 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004552void setup_items_for_insert(struct btrfs_trans_handle *trans,
4553 struct btrfs_root *root, struct btrfs_path *path,
4554 struct btrfs_key *cpu_key, u32 *data_size,
4555 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556{
Chris Mason5f39d392007-10-15 16:14:19 -04004557 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004558 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004559 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004560 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004561 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004562 struct extent_buffer *leaf;
4563 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004564 struct btrfs_map_token token;
4565
4566 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004567
Chris Mason5f39d392007-10-15 16:14:19 -04004568 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004569 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004570
Chris Mason5f39d392007-10-15 16:14:19 -04004571 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004572 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004573
Chris Masonf25956c2008-09-12 15:32:53 -04004574 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004575 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004576 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004577 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004578 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004579 }
Chris Mason5f39d392007-10-15 16:14:19 -04004580
Chris Masonbe0e5c02007-01-26 15:51:26 -05004581 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004582 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004583
Chris Mason5f39d392007-10-15 16:14:19 -04004584 if (old_data < data_end) {
4585 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004586 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004587 slot, old_data, data_end);
4588 BUG_ON(1);
4589 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004590 /*
4591 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4592 */
4593 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004594 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004595 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004596
Chris Mason5f39d392007-10-15 16:14:19 -04004597 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004598 ioff = btrfs_token_item_offset(leaf, item, &token);
4599 btrfs_set_token_item_offset(leaf, item,
4600 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004601 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004602 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004603 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004604 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004605 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004606
4607 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004608 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004609 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004610 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004611 data_end = old_data;
4612 }
Chris Mason5f39d392007-10-15 16:14:19 -04004613
Chris Mason62e27492007-03-15 12:56:47 -04004614 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004615 for (i = 0; i < nr; i++) {
4616 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4617 btrfs_set_item_key(leaf, &disk_key, slot + i);
4618 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004619 btrfs_set_token_item_offset(leaf, item,
4620 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004621 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004622 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004623 }
Chris Mason44871b12009-03-13 10:04:31 -04004624
Chris Mason9c583092008-01-29 15:15:18 -05004625 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004626
Chris Mason5a01a2e2008-01-30 11:43:54 -05004627 if (slot == 0) {
4628 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004629 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004630 }
Chris Masonb9473432009-03-13 11:00:37 -04004631 btrfs_unlock_up_safe(path, 1);
4632 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004633
Chris Mason5f39d392007-10-15 16:14:19 -04004634 if (btrfs_leaf_free_space(root, leaf) < 0) {
4635 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004636 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004637 }
Chris Mason44871b12009-03-13 10:04:31 -04004638}
4639
4640/*
4641 * Given a key and some data, insert items into the tree.
4642 * This does all the path init required, making room in the tree if needed.
4643 */
4644int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4645 struct btrfs_root *root,
4646 struct btrfs_path *path,
4647 struct btrfs_key *cpu_key, u32 *data_size,
4648 int nr)
4649{
Chris Mason44871b12009-03-13 10:04:31 -04004650 int ret = 0;
4651 int slot;
4652 int i;
4653 u32 total_size = 0;
4654 u32 total_data = 0;
4655
4656 for (i = 0; i < nr; i++)
4657 total_data += data_size[i];
4658
4659 total_size = total_data + (nr * sizeof(struct btrfs_item));
4660 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4661 if (ret == 0)
4662 return -EEXIST;
4663 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004664 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004665
Chris Mason44871b12009-03-13 10:04:31 -04004666 slot = path->slots[0];
4667 BUG_ON(slot < 0);
4668
Jeff Mahoney143bede2012-03-01 14:56:26 +01004669 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004670 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004671 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004672}
4673
4674/*
4675 * Given a key and some data, insert an item into the tree.
4676 * This does all the path init required, making room in the tree if needed.
4677 */
Chris Masone089f052007-03-16 16:20:31 -04004678int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4679 *root, struct btrfs_key *cpu_key, void *data, u32
4680 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004681{
4682 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004683 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004684 struct extent_buffer *leaf;
4685 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004686
Chris Mason2c90e5d2007-04-02 10:50:19 -04004687 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004688 if (!path)
4689 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004690 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004691 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004692 leaf = path->nodes[0];
4693 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4694 write_extent_buffer(leaf, data, ptr, data_size);
4695 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004696 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004697 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004698 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004699}
4700
Chris Mason74123bd2007-02-02 11:05:29 -05004701/*
Chris Mason5de08d72007-02-24 06:24:44 -05004702 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004703 *
Chris Masond352ac62008-09-29 15:18:18 -04004704 * the tree should have been previously balanced so the deletion does not
4705 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004706 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004707static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004708 struct btrfs_path *path, int level, int slot,
4709 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004710{
Chris Mason5f39d392007-10-15 16:14:19 -04004711 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004712 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004713 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004714
Chris Mason5f39d392007-10-15 16:14:19 -04004715 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004716 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004717 if (tree_mod_log && level)
4718 tree_mod_log_eb_move(root->fs_info, parent, slot,
4719 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004720 memmove_extent_buffer(parent,
4721 btrfs_node_key_ptr_offset(slot),
4722 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004723 sizeof(struct btrfs_key_ptr) *
4724 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004725 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004726 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4727 MOD_LOG_KEY_REMOVE);
4728 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004729 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004730
Chris Mason7518a232007-03-12 12:01:18 -04004731 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004732 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004733 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004734 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004735 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004736 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004737 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004738 struct btrfs_disk_key disk_key;
4739
4740 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004741 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742 }
Chris Masond6025572007-03-30 14:27:56 -04004743 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004744}
4745
Chris Mason74123bd2007-02-02 11:05:29 -05004746/*
Chris Mason323ac952008-10-01 19:05:46 -04004747 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004748 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004749 *
4750 * This deletes the pointer in path->nodes[1] and frees the leaf
4751 * block extent. zero is returned if it all worked out, < 0 otherwise.
4752 *
4753 * The path must have already been setup for deleting the leaf, including
4754 * all the proper balancing. path->nodes[1] must be locked.
4755 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004756static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4757 struct btrfs_root *root,
4758 struct btrfs_path *path,
4759 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004760{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004761 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004762 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004763
Chris Mason4d081c42009-02-04 09:31:28 -05004764 /*
4765 * btrfs_free_extent is expensive, we want to make sure we
4766 * aren't holding any locks when we call it
4767 */
4768 btrfs_unlock_up_safe(path, 0);
4769
Yan, Zhengf0486c62010-05-16 10:46:25 -04004770 root_sub_used(root, leaf->len);
4771
Josef Bacik3083ee22012-03-09 16:01:49 -05004772 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004773 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004774 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004775}
4776/*
Chris Mason74123bd2007-02-02 11:05:29 -05004777 * delete the item at the leaf level in path. If that empties
4778 * the leaf, remove it from the tree
4779 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004780int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4781 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782{
Chris Mason5f39d392007-10-15 16:14:19 -04004783 struct extent_buffer *leaf;
4784 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004785 int last_off;
4786 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004787 int ret = 0;
4788 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004789 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004790 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004791 struct btrfs_map_token token;
4792
4793 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004794
Chris Mason5f39d392007-10-15 16:14:19 -04004795 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004796 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4797
4798 for (i = 0; i < nr; i++)
4799 dsize += btrfs_item_size_nr(leaf, slot + i);
4800
Chris Mason5f39d392007-10-15 16:14:19 -04004801 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004802
Chris Mason85e21ba2008-01-29 15:11:36 -05004803 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004804 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004805
4806 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004807 data_end + dsize,
4808 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004809 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004810
Chris Mason85e21ba2008-01-29 15:11:36 -05004811 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004812 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004813
Chris Mason5f39d392007-10-15 16:14:19 -04004814 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004815 ioff = btrfs_token_item_offset(leaf, item, &token);
4816 btrfs_set_token_item_offset(leaf, item,
4817 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004818 }
Chris Masondb945352007-10-15 16:15:53 -04004819
Chris Mason5f39d392007-10-15 16:14:19 -04004820 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004821 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004822 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004823 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004824 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004825 btrfs_set_header_nritems(leaf, nritems - nr);
4826 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004827
Chris Mason74123bd2007-02-02 11:05:29 -05004828 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004829 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004830 if (leaf == root->node) {
4831 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004832 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004833 btrfs_set_path_blocking(path);
4834 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004835 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004836 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004837 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004838 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004839 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004840 struct btrfs_disk_key disk_key;
4841
4842 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004843 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004844 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004845
Chris Mason74123bd2007-02-02 11:05:29 -05004846 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004847 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004848 /* push_leaf_left fixes the path.
4849 * make sure the path still points to our leaf
4850 * for possible call to del_ptr below
4851 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004852 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004853 extent_buffer_get(leaf);
4854
Chris Masonb9473432009-03-13 11:00:37 -04004855 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004856 wret = push_leaf_left(trans, root, path, 1, 1,
4857 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004858 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004859 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004860
4861 if (path->nodes[0] == leaf &&
4862 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004863 wret = push_leaf_right(trans, root, path, 1,
4864 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004865 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004866 ret = wret;
4867 }
Chris Mason5f39d392007-10-15 16:14:19 -04004868
4869 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004870 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004871 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004872 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004873 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004874 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004875 /* if we're still in the path, make sure
4876 * we're dirty. Otherwise, one of the
4877 * push_leaf functions must have already
4878 * dirtied this buffer
4879 */
4880 if (path->nodes[0] == leaf)
4881 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004882 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004883 }
Chris Masond5719762007-03-23 10:01:08 -04004884 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004885 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004886 }
4887 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004888 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004889}
4890
Chris Mason97571fd2007-02-24 13:39:08 -05004891/*
Chris Mason925baed2008-06-25 16:01:30 -04004892 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004893 * returns 0 if it found something or 1 if there are no lesser leaves.
4894 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004895 *
4896 * This may release the path, and so you may lose any locks held at the
4897 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004898 */
4899int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4900{
Chris Mason925baed2008-06-25 16:01:30 -04004901 struct btrfs_key key;
4902 struct btrfs_disk_key found_key;
4903 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004904
Chris Mason925baed2008-06-25 16:01:30 -04004905 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004906
Chris Mason925baed2008-06-25 16:01:30 -04004907 if (key.offset > 0)
4908 key.offset--;
4909 else if (key.type > 0)
4910 key.type--;
4911 else if (key.objectid > 0)
4912 key.objectid--;
4913 else
4914 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004915
David Sterbab3b4aa72011-04-21 01:20:15 +02004916 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004917 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4918 if (ret < 0)
4919 return ret;
4920 btrfs_item_key(path->nodes[0], &found_key, 0);
4921 ret = comp_keys(&found_key, &key);
4922 if (ret < 0)
4923 return 0;
4924 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004925}
4926
Chris Mason3f157a22008-06-25 16:01:31 -04004927/*
4928 * A helper function to walk down the tree starting at min_key, and looking
4929 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004930 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004931 *
4932 * This does not cow, but it does stuff the starting key it finds back
4933 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4934 * key and get a writable path.
4935 *
4936 * This does lock as it descends, and path->keep_locks should be set
4937 * to 1 by the caller.
4938 *
4939 * This honors path->lowest_level to prevent descent past a given level
4940 * of the tree.
4941 *
Chris Masond352ac62008-09-29 15:18:18 -04004942 * min_trans indicates the oldest transaction that you are interested
4943 * in walking through. Any nodes or leaves older than min_trans are
4944 * skipped over (without reading them).
4945 *
Chris Mason3f157a22008-06-25 16:01:31 -04004946 * returns zero if something useful was found, < 0 on error and 1 if there
4947 * was nothing in the tree that matched the search criteria.
4948 */
4949int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004950 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004951 struct btrfs_path *path, int cache_only,
4952 u64 min_trans)
4953{
4954 struct extent_buffer *cur;
4955 struct btrfs_key found_key;
4956 int slot;
Yan96524802008-07-24 12:19:49 -04004957 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004958 u32 nritems;
4959 int level;
4960 int ret = 1;
4961
Chris Mason934d3752008-12-08 16:43:10 -05004962 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004963again:
Chris Masonbd681512011-07-16 15:23:14 -04004964 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004965 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004966 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004967 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004968 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004969
4970 if (btrfs_header_generation(cur) < min_trans) {
4971 ret = 1;
4972 goto out;
4973 }
Chris Masond3977122009-01-05 21:25:51 -05004974 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004975 nritems = btrfs_header_nritems(cur);
4976 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004977 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004978
Chris Mason323ac952008-10-01 19:05:46 -04004979 /* at the lowest level, we're done, setup the path and exit */
4980 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004981 if (slot >= nritems)
4982 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004983 ret = 0;
4984 path->slots[level] = slot;
4985 btrfs_item_key_to_cpu(cur, &found_key, slot);
4986 goto out;
4987 }
Yan96524802008-07-24 12:19:49 -04004988 if (sret && slot > 0)
4989 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004990 /*
4991 * check this node pointer against the cache_only and
4992 * min_trans parameters. If it isn't in cache or is too
4993 * old, skip to the next one.
4994 */
Chris Masond3977122009-01-05 21:25:51 -05004995 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004996 u64 blockptr;
4997 u64 gen;
4998 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004999 struct btrfs_disk_key disk_key;
5000
Chris Mason3f157a22008-06-25 16:01:31 -04005001 blockptr = btrfs_node_blockptr(cur, slot);
5002 gen = btrfs_node_ptr_generation(cur, slot);
5003 if (gen < min_trans) {
5004 slot++;
5005 continue;
5006 }
5007 if (!cache_only)
5008 break;
5009
Chris Masone02119d2008-09-05 16:13:11 -04005010 if (max_key) {
5011 btrfs_node_key(cur, &disk_key, slot);
5012 if (comp_keys(&disk_key, max_key) >= 0) {
5013 ret = 1;
5014 goto out;
5015 }
5016 }
5017
Chris Mason3f157a22008-06-25 16:01:31 -04005018 tmp = btrfs_find_tree_block(root, blockptr,
5019 btrfs_level_size(root, level - 1));
5020
Chris Masonb9fab912012-05-06 07:23:47 -04005021 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04005022 free_extent_buffer(tmp);
5023 break;
5024 }
5025 if (tmp)
5026 free_extent_buffer(tmp);
5027 slot++;
5028 }
Chris Masone02119d2008-09-05 16:13:11 -04005029find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005030 /*
5031 * we didn't find a candidate key in this node, walk forward
5032 * and find another one
5033 */
5034 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005035 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005036 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005037 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04005038 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005039 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005040 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005041 goto again;
5042 } else {
5043 goto out;
5044 }
5045 }
5046 /* save our key for returning back */
5047 btrfs_node_key_to_cpu(cur, &found_key, slot);
5048 path->slots[level] = slot;
5049 if (level == path->lowest_level) {
5050 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005051 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005052 goto out;
5053 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005054 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005055 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005056 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005057
Chris Masonbd681512011-07-16 15:23:14 -04005058 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005059
Chris Masonbd681512011-07-16 15:23:14 -04005060 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005061 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005062 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005063 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005064 }
5065out:
5066 if (ret == 0)
5067 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005068 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005069 return ret;
5070}
5071
Alexander Block70698302012-06-05 21:07:48 +02005072static void tree_move_down(struct btrfs_root *root,
5073 struct btrfs_path *path,
5074 int *level, int root_level)
5075{
5076 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5077 path->slots[*level]);
5078 path->slots[*level - 1] = 0;
5079 (*level)--;
5080}
5081
5082static int tree_move_next_or_upnext(struct btrfs_root *root,
5083 struct btrfs_path *path,
5084 int *level, int root_level)
5085{
5086 int ret = 0;
5087 int nritems;
5088 nritems = btrfs_header_nritems(path->nodes[*level]);
5089
5090 path->slots[*level]++;
5091
5092 while (path->slots[*level] == nritems) {
5093 if (*level == root_level)
5094 return -1;
5095
5096 /* move upnext */
5097 path->slots[*level] = 0;
5098 free_extent_buffer(path->nodes[*level]);
5099 path->nodes[*level] = NULL;
5100 (*level)++;
5101 path->slots[*level]++;
5102
5103 nritems = btrfs_header_nritems(path->nodes[*level]);
5104 ret = 1;
5105 }
5106 return ret;
5107}
5108
5109/*
5110 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5111 * or down.
5112 */
5113static int tree_advance(struct btrfs_root *root,
5114 struct btrfs_path *path,
5115 int *level, int root_level,
5116 int allow_down,
5117 struct btrfs_key *key)
5118{
5119 int ret;
5120
5121 if (*level == 0 || !allow_down) {
5122 ret = tree_move_next_or_upnext(root, path, level, root_level);
5123 } else {
5124 tree_move_down(root, path, level, root_level);
5125 ret = 0;
5126 }
5127 if (ret >= 0) {
5128 if (*level == 0)
5129 btrfs_item_key_to_cpu(path->nodes[*level], key,
5130 path->slots[*level]);
5131 else
5132 btrfs_node_key_to_cpu(path->nodes[*level], key,
5133 path->slots[*level]);
5134 }
5135 return ret;
5136}
5137
5138static int tree_compare_item(struct btrfs_root *left_root,
5139 struct btrfs_path *left_path,
5140 struct btrfs_path *right_path,
5141 char *tmp_buf)
5142{
5143 int cmp;
5144 int len1, len2;
5145 unsigned long off1, off2;
5146
5147 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5148 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5149 if (len1 != len2)
5150 return 1;
5151
5152 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5153 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5154 right_path->slots[0]);
5155
5156 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5157
5158 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5159 if (cmp)
5160 return 1;
5161 return 0;
5162}
5163
5164#define ADVANCE 1
5165#define ADVANCE_ONLY_NEXT -1
5166
5167/*
5168 * This function compares two trees and calls the provided callback for
5169 * every changed/new/deleted item it finds.
5170 * If shared tree blocks are encountered, whole subtrees are skipped, making
5171 * the compare pretty fast on snapshotted subvolumes.
5172 *
5173 * This currently works on commit roots only. As commit roots are read only,
5174 * we don't do any locking. The commit roots are protected with transactions.
5175 * Transactions are ended and rejoined when a commit is tried in between.
5176 *
5177 * This function checks for modifications done to the trees while comparing.
5178 * If it detects a change, it aborts immediately.
5179 */
5180int btrfs_compare_trees(struct btrfs_root *left_root,
5181 struct btrfs_root *right_root,
5182 btrfs_changed_cb_t changed_cb, void *ctx)
5183{
5184 int ret;
5185 int cmp;
5186 struct btrfs_trans_handle *trans = NULL;
5187 struct btrfs_path *left_path = NULL;
5188 struct btrfs_path *right_path = NULL;
5189 struct btrfs_key left_key;
5190 struct btrfs_key right_key;
5191 char *tmp_buf = NULL;
5192 int left_root_level;
5193 int right_root_level;
5194 int left_level;
5195 int right_level;
5196 int left_end_reached;
5197 int right_end_reached;
5198 int advance_left;
5199 int advance_right;
5200 u64 left_blockptr;
5201 u64 right_blockptr;
5202 u64 left_start_ctransid;
5203 u64 right_start_ctransid;
5204 u64 ctransid;
5205
5206 left_path = btrfs_alloc_path();
5207 if (!left_path) {
5208 ret = -ENOMEM;
5209 goto out;
5210 }
5211 right_path = btrfs_alloc_path();
5212 if (!right_path) {
5213 ret = -ENOMEM;
5214 goto out;
5215 }
5216
5217 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5218 if (!tmp_buf) {
5219 ret = -ENOMEM;
5220 goto out;
5221 }
5222
5223 left_path->search_commit_root = 1;
5224 left_path->skip_locking = 1;
5225 right_path->search_commit_root = 1;
5226 right_path->skip_locking = 1;
5227
5228 spin_lock(&left_root->root_times_lock);
5229 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
5230 spin_unlock(&left_root->root_times_lock);
5231
5232 spin_lock(&right_root->root_times_lock);
5233 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
5234 spin_unlock(&right_root->root_times_lock);
5235
5236 trans = btrfs_join_transaction(left_root);
5237 if (IS_ERR(trans)) {
5238 ret = PTR_ERR(trans);
5239 trans = NULL;
5240 goto out;
5241 }
5242
5243 /*
5244 * Strategy: Go to the first items of both trees. Then do
5245 *
5246 * If both trees are at level 0
5247 * Compare keys of current items
5248 * If left < right treat left item as new, advance left tree
5249 * and repeat
5250 * If left > right treat right item as deleted, advance right tree
5251 * and repeat
5252 * If left == right do deep compare of items, treat as changed if
5253 * needed, advance both trees and repeat
5254 * If both trees are at the same level but not at level 0
5255 * Compare keys of current nodes/leafs
5256 * If left < right advance left tree and repeat
5257 * If left > right advance right tree and repeat
5258 * If left == right compare blockptrs of the next nodes/leafs
5259 * If they match advance both trees but stay at the same level
5260 * and repeat
5261 * If they don't match advance both trees while allowing to go
5262 * deeper and repeat
5263 * If tree levels are different
5264 * Advance the tree that needs it and repeat
5265 *
5266 * Advancing a tree means:
5267 * If we are at level 0, try to go to the next slot. If that's not
5268 * possible, go one level up and repeat. Stop when we found a level
5269 * where we could go to the next slot. We may at this point be on a
5270 * node or a leaf.
5271 *
5272 * If we are not at level 0 and not on shared tree blocks, go one
5273 * level deeper.
5274 *
5275 * If we are not at level 0 and on shared tree blocks, go one slot to
5276 * the right if possible or go up and right.
5277 */
5278
5279 left_level = btrfs_header_level(left_root->commit_root);
5280 left_root_level = left_level;
5281 left_path->nodes[left_level] = left_root->commit_root;
5282 extent_buffer_get(left_path->nodes[left_level]);
5283
5284 right_level = btrfs_header_level(right_root->commit_root);
5285 right_root_level = right_level;
5286 right_path->nodes[right_level] = right_root->commit_root;
5287 extent_buffer_get(right_path->nodes[right_level]);
5288
5289 if (left_level == 0)
5290 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5291 &left_key, left_path->slots[left_level]);
5292 else
5293 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5294 &left_key, left_path->slots[left_level]);
5295 if (right_level == 0)
5296 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5297 &right_key, right_path->slots[right_level]);
5298 else
5299 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5300 &right_key, right_path->slots[right_level]);
5301
5302 left_end_reached = right_end_reached = 0;
5303 advance_left = advance_right = 0;
5304
5305 while (1) {
5306 /*
5307 * We need to make sure the transaction does not get committed
5308 * while we do anything on commit roots. This means, we need to
5309 * join and leave transactions for every item that we process.
5310 */
5311 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5312 btrfs_release_path(left_path);
5313 btrfs_release_path(right_path);
5314
5315 ret = btrfs_end_transaction(trans, left_root);
5316 trans = NULL;
5317 if (ret < 0)
5318 goto out;
5319 }
5320 /* now rejoin the transaction */
5321 if (!trans) {
5322 trans = btrfs_join_transaction(left_root);
5323 if (IS_ERR(trans)) {
5324 ret = PTR_ERR(trans);
5325 trans = NULL;
5326 goto out;
5327 }
5328
5329 spin_lock(&left_root->root_times_lock);
5330 ctransid = btrfs_root_ctransid(&left_root->root_item);
5331 spin_unlock(&left_root->root_times_lock);
5332 if (ctransid != left_start_ctransid)
5333 left_start_ctransid = 0;
5334
5335 spin_lock(&right_root->root_times_lock);
5336 ctransid = btrfs_root_ctransid(&right_root->root_item);
5337 spin_unlock(&right_root->root_times_lock);
5338 if (ctransid != right_start_ctransid)
5339 right_start_ctransid = 0;
5340
5341 if (!left_start_ctransid || !right_start_ctransid) {
5342 WARN(1, KERN_WARNING
5343 "btrfs: btrfs_compare_tree detected "
5344 "a change in one of the trees while "
5345 "iterating. This is probably a "
5346 "bug.\n");
5347 ret = -EIO;
5348 goto out;
5349 }
5350
5351 /*
5352 * the commit root may have changed, so start again
5353 * where we stopped
5354 */
5355 left_path->lowest_level = left_level;
5356 right_path->lowest_level = right_level;
5357 ret = btrfs_search_slot(NULL, left_root,
5358 &left_key, left_path, 0, 0);
5359 if (ret < 0)
5360 goto out;
5361 ret = btrfs_search_slot(NULL, right_root,
5362 &right_key, right_path, 0, 0);
5363 if (ret < 0)
5364 goto out;
5365 }
5366
5367 if (advance_left && !left_end_reached) {
5368 ret = tree_advance(left_root, left_path, &left_level,
5369 left_root_level,
5370 advance_left != ADVANCE_ONLY_NEXT,
5371 &left_key);
5372 if (ret < 0)
5373 left_end_reached = ADVANCE;
5374 advance_left = 0;
5375 }
5376 if (advance_right && !right_end_reached) {
5377 ret = tree_advance(right_root, right_path, &right_level,
5378 right_root_level,
5379 advance_right != ADVANCE_ONLY_NEXT,
5380 &right_key);
5381 if (ret < 0)
5382 right_end_reached = ADVANCE;
5383 advance_right = 0;
5384 }
5385
5386 if (left_end_reached && right_end_reached) {
5387 ret = 0;
5388 goto out;
5389 } else if (left_end_reached) {
5390 if (right_level == 0) {
5391 ret = changed_cb(left_root, right_root,
5392 left_path, right_path,
5393 &right_key,
5394 BTRFS_COMPARE_TREE_DELETED,
5395 ctx);
5396 if (ret < 0)
5397 goto out;
5398 }
5399 advance_right = ADVANCE;
5400 continue;
5401 } else if (right_end_reached) {
5402 if (left_level == 0) {
5403 ret = changed_cb(left_root, right_root,
5404 left_path, right_path,
5405 &left_key,
5406 BTRFS_COMPARE_TREE_NEW,
5407 ctx);
5408 if (ret < 0)
5409 goto out;
5410 }
5411 advance_left = ADVANCE;
5412 continue;
5413 }
5414
5415 if (left_level == 0 && right_level == 0) {
5416 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5417 if (cmp < 0) {
5418 ret = changed_cb(left_root, right_root,
5419 left_path, right_path,
5420 &left_key,
5421 BTRFS_COMPARE_TREE_NEW,
5422 ctx);
5423 if (ret < 0)
5424 goto out;
5425 advance_left = ADVANCE;
5426 } else if (cmp > 0) {
5427 ret = changed_cb(left_root, right_root,
5428 left_path, right_path,
5429 &right_key,
5430 BTRFS_COMPARE_TREE_DELETED,
5431 ctx);
5432 if (ret < 0)
5433 goto out;
5434 advance_right = ADVANCE;
5435 } else {
5436 ret = tree_compare_item(left_root, left_path,
5437 right_path, tmp_buf);
5438 if (ret) {
5439 ret = changed_cb(left_root, right_root,
5440 left_path, right_path,
5441 &left_key,
5442 BTRFS_COMPARE_TREE_CHANGED,
5443 ctx);
5444 if (ret < 0)
5445 goto out;
5446 }
5447 advance_left = ADVANCE;
5448 advance_right = ADVANCE;
5449 }
5450 } else if (left_level == right_level) {
5451 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5452 if (cmp < 0) {
5453 advance_left = ADVANCE;
5454 } else if (cmp > 0) {
5455 advance_right = ADVANCE;
5456 } else {
5457 left_blockptr = btrfs_node_blockptr(
5458 left_path->nodes[left_level],
5459 left_path->slots[left_level]);
5460 right_blockptr = btrfs_node_blockptr(
5461 right_path->nodes[right_level],
5462 right_path->slots[right_level]);
5463 if (left_blockptr == right_blockptr) {
5464 /*
5465 * As we're on a shared block, don't
5466 * allow to go deeper.
5467 */
5468 advance_left = ADVANCE_ONLY_NEXT;
5469 advance_right = ADVANCE_ONLY_NEXT;
5470 } else {
5471 advance_left = ADVANCE;
5472 advance_right = ADVANCE;
5473 }
5474 }
5475 } else if (left_level < right_level) {
5476 advance_right = ADVANCE;
5477 } else {
5478 advance_left = ADVANCE;
5479 }
5480 }
5481
5482out:
5483 btrfs_free_path(left_path);
5484 btrfs_free_path(right_path);
5485 kfree(tmp_buf);
5486
5487 if (trans) {
5488 if (!ret)
5489 ret = btrfs_end_transaction(trans, left_root);
5490 else
5491 btrfs_end_transaction(trans, left_root);
5492 }
5493
5494 return ret;
5495}
5496
Chris Mason3f157a22008-06-25 16:01:31 -04005497/*
5498 * this is similar to btrfs_next_leaf, but does not try to preserve
5499 * and fixup the path. It looks for and returns the next key in the
5500 * tree based on the current path and the cache_only and min_trans
5501 * parameters.
5502 *
5503 * 0 is returned if another key is found, < 0 if there are any errors
5504 * and 1 is returned if there are no higher keys in the tree
5505 *
5506 * path->keep_locks should be set to 1 on the search made before
5507 * calling this function.
5508 */
Chris Masone7a84562008-06-25 16:01:31 -04005509int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04005510 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04005511 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005512{
Chris Masone7a84562008-06-25 16:01:31 -04005513 int slot;
5514 struct extent_buffer *c;
5515
Chris Mason934d3752008-12-08 16:43:10 -05005516 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005517 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005518 if (!path->nodes[level])
5519 return 1;
5520
5521 slot = path->slots[level] + 1;
5522 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005523next:
Chris Masone7a84562008-06-25 16:01:31 -04005524 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005525 int ret;
5526 int orig_lowest;
5527 struct btrfs_key cur_key;
5528 if (level + 1 >= BTRFS_MAX_LEVEL ||
5529 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005530 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005531
5532 if (path->locks[level + 1]) {
5533 level++;
5534 continue;
5535 }
5536
5537 slot = btrfs_header_nritems(c) - 1;
5538 if (level == 0)
5539 btrfs_item_key_to_cpu(c, &cur_key, slot);
5540 else
5541 btrfs_node_key_to_cpu(c, &cur_key, slot);
5542
5543 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005544 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005545 path->lowest_level = level;
5546 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5547 0, 0);
5548 path->lowest_level = orig_lowest;
5549 if (ret < 0)
5550 return ret;
5551
5552 c = path->nodes[level];
5553 slot = path->slots[level];
5554 if (ret == 0)
5555 slot++;
5556 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005557 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005558
Chris Masone7a84562008-06-25 16:01:31 -04005559 if (level == 0)
5560 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005561 else {
5562 u64 blockptr = btrfs_node_blockptr(c, slot);
5563 u64 gen = btrfs_node_ptr_generation(c, slot);
5564
5565 if (cache_only) {
5566 struct extent_buffer *cur;
5567 cur = btrfs_find_tree_block(root, blockptr,
5568 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04005569 if (!cur ||
5570 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04005571 slot++;
5572 if (cur)
5573 free_extent_buffer(cur);
5574 goto next;
5575 }
5576 free_extent_buffer(cur);
5577 }
5578 if (gen < min_trans) {
5579 slot++;
5580 goto next;
5581 }
Chris Masone7a84562008-06-25 16:01:31 -04005582 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005583 }
Chris Masone7a84562008-06-25 16:01:31 -04005584 return 0;
5585 }
5586 return 1;
5587}
5588
Chris Mason7bb86312007-12-11 09:25:06 -05005589/*
Chris Mason925baed2008-06-25 16:01:30 -04005590 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005591 * returns 0 if it found something or 1 if there are no greater leaves.
5592 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005593 */
Chris Mason234b63a2007-03-13 10:46:10 -04005594int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005595{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005596 return btrfs_next_old_leaf(root, path, 0);
5597}
5598
5599int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5600 u64 time_seq)
5601{
Chris Masond97e63b2007-02-20 16:40:44 -05005602 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005603 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005604 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005605 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005606 struct btrfs_key key;
5607 u32 nritems;
5608 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005609 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005610 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005611
5612 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005613 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005614 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005615
Chris Mason8e73f272009-04-03 10:14:18 -04005616 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5617again:
5618 level = 1;
5619 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005620 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005621 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005622
Chris Masona2135012008-06-25 16:01:30 -04005623 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005624 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005625
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005626 if (time_seq)
5627 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5628 else
5629 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005630 path->keep_locks = 0;
5631
5632 if (ret < 0)
5633 return ret;
5634
Chris Masona2135012008-06-25 16:01:30 -04005635 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005636 /*
5637 * by releasing the path above we dropped all our locks. A balance
5638 * could have added more items next to the key that used to be
5639 * at the very end of the block. So, check again here and
5640 * advance the path if there are now more items available.
5641 */
Chris Masona2135012008-06-25 16:01:30 -04005642 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005643 if (ret == 0)
5644 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005645 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005646 goto done;
5647 }
Chris Masond97e63b2007-02-20 16:40:44 -05005648
Chris Masond3977122009-01-05 21:25:51 -05005649 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005650 if (!path->nodes[level]) {
5651 ret = 1;
5652 goto done;
5653 }
Chris Mason5f39d392007-10-15 16:14:19 -04005654
Chris Masond97e63b2007-02-20 16:40:44 -05005655 slot = path->slots[level] + 1;
5656 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005657 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005658 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005659 if (level == BTRFS_MAX_LEVEL) {
5660 ret = 1;
5661 goto done;
5662 }
Chris Masond97e63b2007-02-20 16:40:44 -05005663 continue;
5664 }
Chris Mason5f39d392007-10-15 16:14:19 -04005665
Chris Mason925baed2008-06-25 16:01:30 -04005666 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005667 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005668 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005669 }
Chris Mason5f39d392007-10-15 16:14:19 -04005670
Chris Mason8e73f272009-04-03 10:14:18 -04005671 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005672 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005673 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005674 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005675 if (ret == -EAGAIN)
5676 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005677
Chris Mason76a05b32009-05-14 13:24:30 -04005678 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005679 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005680 goto done;
5681 }
5682
Chris Mason5cd57b22008-06-25 16:01:30 -04005683 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005684 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005685 if (!ret && time_seq) {
5686 /*
5687 * If we don't get the lock, we may be racing
5688 * with push_leaf_left, holding that lock while
5689 * itself waiting for the leaf we've currently
5690 * locked. To solve this situation, we give up
5691 * on our lock and cycle.
5692 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005693 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005694 btrfs_release_path(path);
5695 cond_resched();
5696 goto again;
5697 }
Chris Mason8e73f272009-04-03 10:14:18 -04005698 if (!ret) {
5699 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005700 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005701 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005702 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005703 }
Chris Mason31533fb2011-07-26 16:01:59 -04005704 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005705 }
Chris Masond97e63b2007-02-20 16:40:44 -05005706 break;
5707 }
5708 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005709 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005710 level--;
5711 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005712 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005713 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005714
Chris Mason5f39d392007-10-15 16:14:19 -04005715 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005716 path->nodes[level] = next;
5717 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005718 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005719 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005720 if (!level)
5721 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005722
Chris Mason8e73f272009-04-03 10:14:18 -04005723 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005724 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005725 if (ret == -EAGAIN)
5726 goto again;
5727
Chris Mason76a05b32009-05-14 13:24:30 -04005728 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005729 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005730 goto done;
5731 }
5732
Chris Mason5cd57b22008-06-25 16:01:30 -04005733 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005734 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005735 if (!ret) {
5736 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005737 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005738 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005739 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005740 }
Chris Mason31533fb2011-07-26 16:01:59 -04005741 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005742 }
Chris Masond97e63b2007-02-20 16:40:44 -05005743 }
Chris Mason8e73f272009-04-03 10:14:18 -04005744 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005745done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005746 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005747 path->leave_spinning = old_spinning;
5748 if (!old_spinning)
5749 btrfs_set_path_blocking(path);
5750
5751 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005752}
Chris Mason0b86a832008-03-24 15:01:56 -04005753
Chris Mason3f157a22008-06-25 16:01:31 -04005754/*
5755 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5756 * searching until it gets past min_objectid or finds an item of 'type'
5757 *
5758 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5759 */
Chris Mason0b86a832008-03-24 15:01:56 -04005760int btrfs_previous_item(struct btrfs_root *root,
5761 struct btrfs_path *path, u64 min_objectid,
5762 int type)
5763{
5764 struct btrfs_key found_key;
5765 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005766 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005767 int ret;
5768
Chris Masond3977122009-01-05 21:25:51 -05005769 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005770 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005771 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005772 ret = btrfs_prev_leaf(root, path);
5773 if (ret != 0)
5774 return ret;
5775 } else {
5776 path->slots[0]--;
5777 }
5778 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005779 nritems = btrfs_header_nritems(leaf);
5780 if (nritems == 0)
5781 return 1;
5782 if (path->slots[0] == nritems)
5783 path->slots[0]--;
5784
Chris Mason0b86a832008-03-24 15:01:56 -04005785 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005786 if (found_key.objectid < min_objectid)
5787 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005788 if (found_key.type == type)
5789 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005790 if (found_key.objectid == min_objectid &&
5791 found_key.type < type)
5792 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005793 }
5794 return 1;
5795}