blob: d9308c38a8f270c740f2775b99bf070e3e9ccf56 [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
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200650 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200651 if (ret < 0)
652 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200653
654 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
655 tm->old_root.logical = old_root->start;
656 tm->old_root.level = btrfs_header_level(old_root);
657 tm->generation = btrfs_header_generation(old_root);
658 tm->op = MOD_LOG_ROOT_REPLACE;
659
Jan Schmidt3310c362012-06-11 10:52:38 +0200660 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661out:
662 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200663 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200664}
665
666static struct tree_mod_elem *
667__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
668 int smallest)
669{
670 struct rb_root *tm_root;
671 struct rb_node *node;
672 struct tree_mod_elem *cur = NULL;
673 struct tree_mod_elem *found = NULL;
674 u64 index = start >> PAGE_CACHE_SHIFT;
675
Jan Schmidt097b8a72012-06-21 11:08:04 +0200676 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200677 tm_root = &fs_info->tree_mod_log;
678 node = tm_root->rb_node;
679 while (node) {
680 cur = container_of(node, struct tree_mod_elem, node);
681 if (cur->index < index) {
682 node = node->rb_left;
683 } else if (cur->index > index) {
684 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200685 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200686 node = node->rb_left;
687 } else if (!smallest) {
688 /* we want the node with the highest seq */
689 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200690 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200691 found = cur;
692 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200693 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 /* we want the node with the smallest seq */
695 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200696 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697 found = cur;
698 node = node->rb_right;
699 } else {
700 found = cur;
701 break;
702 }
703 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200704 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200705
706 return found;
707}
708
709/*
710 * this returns the element from the log with the smallest time sequence
711 * value that's in the log (the oldest log item). any element with a time
712 * sequence lower than min_seq will be ignored.
713 */
714static struct tree_mod_elem *
715tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
716 u64 min_seq)
717{
718 return __tree_mod_log_search(fs_info, start, min_seq, 1);
719}
720
721/*
722 * this returns the element from the log with the largest time sequence
723 * value that's in the log (the most recent log item). any element with
724 * a time sequence lower than min_seq will be ignored.
725 */
726static struct tree_mod_elem *
727tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
728{
729 return __tree_mod_log_search(fs_info, start, min_seq, 0);
730}
731
Jan Schmidt097b8a72012-06-21 11:08:04 +0200732static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200733tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
734 struct extent_buffer *src, unsigned long dst_offset,
735 unsigned long src_offset, int nr_items)
736{
737 int ret;
738 int i;
739
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200740 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 return;
742
Jan Schmidt097b8a72012-06-21 11:08:04 +0200743 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
744 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200745 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200746 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200747
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200748 for (i = 0; i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200749 ret = tree_mod_log_insert_key_locked(fs_info, src,
750 i + src_offset,
751 MOD_LOG_KEY_REMOVE);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200752 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200753 ret = tree_mod_log_insert_key_locked(fs_info, dst,
754 i + dst_offset,
755 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200756 BUG_ON(ret < 0);
757 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200758
759 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200760}
761
762static inline void
763tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
764 int dst_offset, int src_offset, int nr_items)
765{
766 int ret;
767 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
768 nr_items, GFP_NOFS);
769 BUG_ON(ret < 0);
770}
771
Jan Schmidt097b8a72012-06-21 11:08:04 +0200772static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200773tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
774 struct extent_buffer *eb,
775 struct btrfs_disk_key *disk_key, int slot, int atomic)
776{
777 int ret;
778
779 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
780 MOD_LOG_KEY_REPLACE,
781 atomic ? GFP_ATOMIC : GFP_NOFS);
782 BUG_ON(ret < 0);
783}
784
Jan Schmidt097b8a72012-06-21 11:08:04 +0200785static noinline void
786tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200787{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200788 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789 return;
790
Jan Schmidt097b8a72012-06-21 11:08:04 +0200791 __tree_mod_log_free_eb(fs_info, eb);
792
793 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200794}
795
Jan Schmidt097b8a72012-06-21 11:08:04 +0200796static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200797tree_mod_log_set_root_pointer(struct btrfs_root *root,
798 struct extent_buffer *new_root_node)
799{
800 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200801 ret = tree_mod_log_insert_root(root->fs_info, root->node,
802 new_root_node, GFP_NOFS);
803 BUG_ON(ret < 0);
804}
805
Chris Masond352ac62008-09-29 15:18:18 -0400806/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400807 * check if the tree block can be shared by multiple trees
808 */
809int btrfs_block_can_be_shared(struct btrfs_root *root,
810 struct extent_buffer *buf)
811{
812 /*
813 * Tree blocks not in refernece counted trees and tree roots
814 * are never shared. If a block was allocated after the last
815 * snapshot and the block was not allocated by tree relocation,
816 * we know the block is not shared.
817 */
818 if (root->ref_cows &&
819 buf != root->node && buf != root->commit_root &&
820 (btrfs_header_generation(buf) <=
821 btrfs_root_last_snapshot(&root->root_item) ||
822 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
823 return 1;
824#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
825 if (root->ref_cows &&
826 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
827 return 1;
828#endif
829 return 0;
830}
831
832static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400835 struct extent_buffer *cow,
836 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400837{
838 u64 refs;
839 u64 owner;
840 u64 flags;
841 u64 new_flags = 0;
842 int ret;
843
844 /*
845 * Backrefs update rules:
846 *
847 * Always use full backrefs for extent pointers in tree block
848 * allocated by tree relocation.
849 *
850 * If a shared tree block is no longer referenced by its owner
851 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
852 * use full backrefs for extent pointers in tree block.
853 *
854 * If a tree block is been relocating
855 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
856 * use full backrefs for extent pointers in tree block.
857 * The reason for this is some operations (such as drop tree)
858 * are only allowed for blocks use full backrefs.
859 */
860
861 if (btrfs_block_can_be_shared(root, buf)) {
862 ret = btrfs_lookup_extent_info(trans, root, buf->start,
863 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700864 if (ret)
865 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700866 if (refs == 0) {
867 ret = -EROFS;
868 btrfs_std_error(root->fs_info, ret);
869 return ret;
870 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 } else {
872 refs = 1;
873 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
874 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
875 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
876 else
877 flags = 0;
878 }
879
880 owner = btrfs_header_owner(buf);
881 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
882 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
883
884 if (refs > 1) {
885 if ((owner == root->root_key.objectid ||
886 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
887 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200888 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890
891 if (root->root_key.objectid ==
892 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200893 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200895 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897 }
898 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
899 } else {
900
901 if (root->root_key.objectid ==
902 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200903 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200905 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 }
908 if (new_flags != 0) {
909 ret = btrfs_set_disk_extent_flags(trans, root,
910 buf->start,
911 buf->len,
912 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700913 if (ret)
914 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 }
916 } else {
917 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
918 if (root->root_key.objectid ==
919 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200920 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200922 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100923 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200924 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 }
Jan Schmidtba1bfbd2012-10-22 20:02:56 +0200927 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400929 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400930 }
931 return 0;
932}
933
934/*
Chris Masond3977122009-01-05 21:25:51 -0500935 * does the dirty work in cow of a single block. The parent block (if
936 * supplied) is updated to point to the new cow copy. The new buffer is marked
937 * dirty and returned locked. If you modify the block it needs to be marked
938 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400939 *
940 * search_start -- an allocation hint for the new block
941 *
Chris Masond3977122009-01-05 21:25:51 -0500942 * empty_size -- a hint that you plan on doing more cow. This is the size in
943 * bytes the allocator should try to find free next to the block it returns.
944 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400945 */
Chris Masond3977122009-01-05 21:25:51 -0500946static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400947 struct btrfs_root *root,
948 struct extent_buffer *buf,
949 struct extent_buffer *parent, int parent_slot,
950 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400951 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400952{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400954 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700955 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400956 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400957 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400959
Chris Mason925baed2008-06-25 16:01:30 -0400960 if (*cow_ret == buf)
961 unlock_orig = 1;
962
Chris Masonb9447ef2009-03-09 11:45:38 -0400963 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400964
Chris Mason7bb86312007-12-11 09:25:06 -0500965 WARN_ON(root->ref_cows && trans->transid !=
966 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400967 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400968
Chris Mason7bb86312007-12-11 09:25:06 -0500969 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400970
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 if (level == 0)
972 btrfs_item_key(buf, &disk_key, 0);
973 else
974 btrfs_node_key(buf, &disk_key, 0);
975
976 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
977 if (parent)
978 parent_start = parent->start;
979 else
980 parent_start = 0;
981 } else
982 parent_start = 0;
983
984 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
985 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200986 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400987 if (IS_ERR(cow))
988 return PTR_ERR(cow);
989
Chris Masonb4ce94d2009-02-04 09:25:08 -0500990 /* cow is set to blocking by btrfs_init_new_buffer */
991
Chris Mason5f39d392007-10-15 16:14:19 -0400992 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400993 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400994 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
996 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
997 BTRFS_HEADER_FLAG_RELOC);
998 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
999 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1000 else
1001 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001002
Yan Zheng2b820322008-11-17 21:11:30 -05001003 write_extent_buffer(cow, root->fs_info->fsid,
1004 (unsigned long)btrfs_header_fsid(cow),
1005 BTRFS_FSID_SIZE);
1006
Mark Fashehbe1a5562011-08-08 13:20:18 -07001007 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001008 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001010 return ret;
1011 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001012
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001013 if (root->ref_cows)
1014 btrfs_reloc_cow_block(trans, root, buf, cow);
1015
Chris Mason6702ed42007-08-07 16:15:09 -04001016 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001017 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1019 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1020 parent_start = buf->start;
1021 else
1022 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001023
Chris Mason5f39d392007-10-15 16:14:19 -04001024 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +02001025 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -04001026 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001027
Yan, Zhengf0486c62010-05-16 10:46:25 -04001028 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001029 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001030 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001031 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001032 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1034 parent_start = parent->start;
1035 else
1036 parent_start = 0;
1037
1038 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001039 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1040 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001041 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001042 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001043 btrfs_set_node_ptr_generation(parent, parent_slot,
1044 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001045 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001046 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001047 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001048 }
Chris Mason925baed2008-06-25 16:01:30 -04001049 if (unlock_orig)
1050 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001051 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001052 btrfs_mark_buffer_dirty(cow);
1053 *cow_ret = cow;
1054 return 0;
1055}
1056
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001057/*
1058 * returns the logical address of the oldest predecessor of the given root.
1059 * entries older than time_seq are ignored.
1060 */
1061static struct tree_mod_elem *
1062__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1063 struct btrfs_root *root, u64 time_seq)
1064{
1065 struct tree_mod_elem *tm;
1066 struct tree_mod_elem *found = NULL;
1067 u64 root_logical = root->node->start;
1068 int looped = 0;
1069
1070 if (!time_seq)
1071 return 0;
1072
1073 /*
1074 * the very last operation that's logged for a root is the replacement
1075 * operation (if it is replaced at all). this has the index of the *new*
1076 * root, making it the very first operation that's logged for this root.
1077 */
1078 while (1) {
1079 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1080 time_seq);
1081 if (!looped && !tm)
1082 return 0;
1083 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001084 * if there are no tree operation for the oldest root, we simply
1085 * return it. this should only happen if that (old) root is at
1086 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001087 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001088 if (!tm)
1089 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001090
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001091 /*
1092 * if there's an operation that's not a root replacement, we
1093 * found the oldest version of our root. normally, we'll find a
1094 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1095 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001096 if (tm->op != MOD_LOG_ROOT_REPLACE)
1097 break;
1098
1099 found = tm;
1100 root_logical = tm->old_root.logical;
1101 BUG_ON(root_logical == root->node->start);
1102 looped = 1;
1103 }
1104
Jan Schmidta95236d2012-06-05 16:41:24 +02001105 /* if there's no old root to return, return what we found instead */
1106 if (!found)
1107 found = tm;
1108
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001109 return found;
1110}
1111
1112/*
1113 * tm is a pointer to the first operation to rewind within eb. then, all
1114 * previous operations will be rewinded (until we reach something older than
1115 * time_seq).
1116 */
1117static void
1118__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1119 struct tree_mod_elem *first_tm)
1120{
1121 u32 n;
1122 struct rb_node *next;
1123 struct tree_mod_elem *tm = first_tm;
1124 unsigned long o_dst;
1125 unsigned long o_src;
1126 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1127
1128 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001129 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001130 /*
1131 * all the operations are recorded with the operator used for
1132 * the modification. as we're going backwards, we do the
1133 * opposite of each operation here.
1134 */
1135 switch (tm->op) {
1136 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1137 BUG_ON(tm->slot < n);
1138 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1139 case MOD_LOG_KEY_REMOVE:
1140 btrfs_set_node_key(eb, &tm->key, tm->slot);
1141 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1142 btrfs_set_node_ptr_generation(eb, tm->slot,
1143 tm->generation);
1144 n++;
1145 break;
1146 case MOD_LOG_KEY_REPLACE:
1147 BUG_ON(tm->slot >= n);
1148 btrfs_set_node_key(eb, &tm->key, tm->slot);
1149 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1150 btrfs_set_node_ptr_generation(eb, tm->slot,
1151 tm->generation);
1152 break;
1153 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001154 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 n--;
1156 break;
1157 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001158 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1159 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1160 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001161 tm->move.nr_items * p_size);
1162 break;
1163 case MOD_LOG_ROOT_REPLACE:
1164 /*
1165 * this operation is special. for roots, this must be
1166 * handled explicitly before rewinding.
1167 * for non-roots, this operation may exist if the node
1168 * was a root: root A -> child B; then A gets empty and
1169 * B is promoted to the new root. in the mod log, we'll
1170 * have a root-replace operation for B, a tree block
1171 * that is no root. we simply ignore that operation.
1172 */
1173 break;
1174 }
1175 next = rb_next(&tm->node);
1176 if (!next)
1177 break;
1178 tm = container_of(next, struct tree_mod_elem, node);
1179 if (tm->index != first_tm->index)
1180 break;
1181 }
1182 btrfs_set_header_nritems(eb, n);
1183}
1184
1185static struct extent_buffer *
1186tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1187 u64 time_seq)
1188{
1189 struct extent_buffer *eb_rewin;
1190 struct tree_mod_elem *tm;
1191
1192 if (!time_seq)
1193 return eb;
1194
1195 if (btrfs_header_level(eb) == 0)
1196 return eb;
1197
1198 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1199 if (!tm)
1200 return eb;
1201
1202 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1203 BUG_ON(tm->slot != 0);
1204 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1205 fs_info->tree_root->nodesize);
1206 BUG_ON(!eb_rewin);
1207 btrfs_set_header_bytenr(eb_rewin, eb->start);
1208 btrfs_set_header_backref_rev(eb_rewin,
1209 btrfs_header_backref_rev(eb));
1210 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001211 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001212 } else {
1213 eb_rewin = btrfs_clone_extent_buffer(eb);
1214 BUG_ON(!eb_rewin);
1215 }
1216
1217 extent_buffer_get(eb_rewin);
1218 free_extent_buffer(eb);
1219
1220 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001221 WARN_ON(btrfs_header_nritems(eb_rewin) >
1222 BTRFS_NODEPTRS_PER_BLOCK(fs_info->fs_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001223
1224 return eb_rewin;
1225}
1226
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001227/*
1228 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1229 * value. If there are no changes, the current root->root_node is returned. If
1230 * anything changed in between, there's a fresh buffer allocated on which the
1231 * rewind operations are done. In any case, the returned buffer is read locked.
1232 * Returns NULL on error (with no locks held).
1233 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001234static inline struct extent_buffer *
1235get_old_root(struct btrfs_root *root, u64 time_seq)
1236{
1237 struct tree_mod_elem *tm;
1238 struct extent_buffer *eb;
Jan Schmidta95236d2012-06-05 16:41:24 +02001239 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001240 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001241 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001242 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001243
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001244 eb = btrfs_read_lock_root_node(root);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001245 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1246 if (!tm)
1247 return root->node;
1248
Jan Schmidta95236d2012-06-05 16:41:24 +02001249 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1250 old_root = &tm->old_root;
1251 old_generation = tm->generation;
1252 logical = old_root->logical;
1253 } else {
1254 logical = root->node->start;
1255 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001256
Jan Schmidta95236d2012-06-05 16:41:24 +02001257 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001258 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1259 btrfs_tree_read_unlock(root->node);
1260 free_extent_buffer(root->node);
1261 blocksize = btrfs_level_size(root, old_root->level);
1262 eb = read_tree_block(root, logical, blocksize, 0);
1263 if (!eb) {
1264 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1265 logical);
1266 WARN_ON(1);
1267 } else {
1268 eb = btrfs_clone_extent_buffer(eb);
1269 }
1270 } else if (old_root) {
1271 btrfs_tree_read_unlock(root->node);
1272 free_extent_buffer(root->node);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001273 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001274 } else {
Jan Schmidta95236d2012-06-05 16:41:24 +02001275 eb = btrfs_clone_extent_buffer(root->node);
Jan Schmidt834328a2012-10-23 11:27:33 +02001276 btrfs_tree_read_unlock(root->node);
1277 free_extent_buffer(root->node);
1278 }
1279
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001280 if (!eb)
1281 return NULL;
1282 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001283 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001284 btrfs_set_header_bytenr(eb, eb->start);
1285 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1286 btrfs_set_header_owner(eb, root->root_key.objectid);
Jan Schmidta95236d2012-06-05 16:41:24 +02001287 btrfs_set_header_level(eb, old_root->level);
1288 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001289 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001290 if (tm)
1291 __tree_mod_log_rewind(eb, time_seq, tm);
1292 else
1293 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001294 extent_buffer_get(eb);
Jan Schmidt57911b82012-10-19 09:22:03 +02001295 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001296
1297 return eb;
1298}
1299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001300static inline int should_cow_block(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *root,
1302 struct extent_buffer *buf)
1303{
Liu Bof1ebcc72011-11-14 20:48:06 -05001304 /* ensure we can see the force_cow */
1305 smp_rmb();
1306
1307 /*
1308 * We do not need to cow a block if
1309 * 1) this block is not created or changed in this transaction;
1310 * 2) this block does not belong to TREE_RELOC tree;
1311 * 3) the root is not forced COW.
1312 *
1313 * What is forced COW:
1314 * when we create snapshot during commiting the transaction,
1315 * after we've finished coping src root, we must COW the shared
1316 * block to ensure the metadata consistency.
1317 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001318 if (btrfs_header_generation(buf) == trans->transid &&
1319 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1320 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001321 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1322 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001323 return 0;
1324 return 1;
1325}
1326
Chris Masond352ac62008-09-29 15:18:18 -04001327/*
1328 * cows a single block, see __btrfs_cow_block for the real work.
1329 * This version of it has extra checks so that a block isn't cow'd more than
1330 * once per transaction, as long as it hasn't been written yet
1331 */
Chris Masond3977122009-01-05 21:25:51 -05001332noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001333 struct btrfs_root *root, struct extent_buffer *buf,
1334 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001335 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001336{
Chris Mason6702ed42007-08-07 16:15:09 -04001337 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001338 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001339
Chris Masonccd467d2007-06-28 15:57:36 -04001340 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001341 printk(KERN_CRIT "trans %llu running %llu\n",
1342 (unsigned long long)trans->transid,
1343 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001344 root->fs_info->running_transaction->transid);
1345 WARN_ON(1);
1346 }
1347 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001348 printk(KERN_CRIT "trans %llu running %llu\n",
1349 (unsigned long long)trans->transid,
1350 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001351 WARN_ON(1);
1352 }
Chris Masondc17ff82008-01-08 15:46:30 -05001353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001355 *cow_ret = buf;
1356 return 0;
1357 }
Chris Masonc4876852009-02-04 09:24:25 -05001358
Chris Mason0b86a832008-03-24 15:01:56 -04001359 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001360
1361 if (parent)
1362 btrfs_set_lock_blocking(parent);
1363 btrfs_set_lock_blocking(buf);
1364
Chris Masonf510cfe2007-10-15 16:14:48 -04001365 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001366 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001367
1368 trace_btrfs_cow_block(root, buf, *cow_ret);
1369
Chris Masonf510cfe2007-10-15 16:14:48 -04001370 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001371}
1372
Chris Masond352ac62008-09-29 15:18:18 -04001373/*
1374 * helper function for defrag to decide if two blocks pointed to by a
1375 * node are actually close by
1376 */
Chris Mason6b800532007-10-15 16:17:34 -04001377static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001378{
Chris Mason6b800532007-10-15 16:17:34 -04001379 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001380 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001381 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001382 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001383 return 0;
1384}
1385
Chris Mason081e9572007-11-06 10:26:24 -05001386/*
1387 * compare two keys in a memcmp fashion
1388 */
1389static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1390{
1391 struct btrfs_key k1;
1392
1393 btrfs_disk_key_to_cpu(&k1, disk);
1394
Diego Calleja20736ab2009-07-24 11:06:52 -04001395 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001396}
1397
Josef Bacikf3465ca2008-11-12 14:19:50 -05001398/*
1399 * same as comp_keys only with two btrfs_key's
1400 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001402{
1403 if (k1->objectid > k2->objectid)
1404 return 1;
1405 if (k1->objectid < k2->objectid)
1406 return -1;
1407 if (k1->type > k2->type)
1408 return 1;
1409 if (k1->type < k2->type)
1410 return -1;
1411 if (k1->offset > k2->offset)
1412 return 1;
1413 if (k1->offset < k2->offset)
1414 return -1;
1415 return 0;
1416}
Chris Mason081e9572007-11-06 10:26:24 -05001417
Chris Masond352ac62008-09-29 15:18:18 -04001418/*
1419 * this is used by the defrag code to go through all the
1420 * leaves pointed to by a node and reallocate them so that
1421 * disk order is close to key order
1422 */
Chris Mason6702ed42007-08-07 16:15:09 -04001423int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001424 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001425 int start_slot, int cache_only, u64 *last_ret,
1426 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001427{
Chris Mason6b800532007-10-15 16:17:34 -04001428 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001429 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001430 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001431 u64 search_start = *last_ret;
1432 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001433 u64 other;
1434 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001435 int end_slot;
1436 int i;
1437 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001438 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001439 int uptodate;
1440 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001441 int progress_passed = 0;
1442 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001443
Chris Mason5708b952007-10-25 15:43:18 -04001444 parent_level = btrfs_header_level(parent);
1445 if (cache_only && parent_level != 1)
1446 return 0;
1447
Chris Masond3977122009-01-05 21:25:51 -05001448 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001449 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001450 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001451 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001452
Chris Mason6b800532007-10-15 16:17:34 -04001453 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001454 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001455 end_slot = parent_nritems;
1456
1457 if (parent_nritems == 1)
1458 return 0;
1459
Chris Masonb4ce94d2009-02-04 09:25:08 -05001460 btrfs_set_lock_blocking(parent);
1461
Chris Mason6702ed42007-08-07 16:15:09 -04001462 for (i = start_slot; i < end_slot; i++) {
1463 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001464
Chris Mason081e9572007-11-06 10:26:24 -05001465 btrfs_node_key(parent, &disk_key, i);
1466 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1467 continue;
1468
1469 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001470 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001471 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001472 if (last_block == 0)
1473 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001474
Chris Mason6702ed42007-08-07 16:15:09 -04001475 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001476 other = btrfs_node_blockptr(parent, i - 1);
1477 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001478 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001479 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001480 other = btrfs_node_blockptr(parent, i + 1);
1481 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001482 }
Chris Masone9d0b132007-08-10 14:06:19 -04001483 if (close) {
1484 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001485 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001486 }
Chris Mason6702ed42007-08-07 16:15:09 -04001487
Chris Mason6b800532007-10-15 16:17:34 -04001488 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1489 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001490 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001491 else
1492 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001493 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001494 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001495 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001496 continue;
1497 }
Chris Mason6b800532007-10-15 16:17:34 -04001498 if (!cur) {
1499 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001500 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001501 if (!cur)
1502 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001503 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001504 err = btrfs_read_buffer(cur, gen);
1505 if (err) {
1506 free_extent_buffer(cur);
1507 return err;
1508 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001509 }
Chris Mason6702ed42007-08-07 16:15:09 -04001510 }
Chris Masone9d0b132007-08-10 14:06:19 -04001511 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001512 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001513
Chris Masone7a84562008-06-25 16:01:31 -04001514 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001515 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001516 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001517 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001518 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001519 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001520 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001521 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001522 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001523 break;
Yan252c38f2007-08-29 09:11:44 -04001524 }
Chris Masone7a84562008-06-25 16:01:31 -04001525 search_start = cur->start;
1526 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001527 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001528 btrfs_tree_unlock(cur);
1529 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001530 }
1531 return err;
1532}
1533
Chris Mason74123bd2007-02-02 11:05:29 -05001534/*
1535 * The leaf data grows from end-to-front in the node.
1536 * this returns the address of the start of the last item,
1537 * which is the stop of the leaf data stack
1538 */
Chris Mason123abc82007-03-14 14:14:43 -04001539static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001540 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001541{
Chris Mason5f39d392007-10-15 16:14:19 -04001542 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001543 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001544 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001545 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001546}
1547
Chris Masonaa5d6be2007-02-28 16:35:06 -05001548
Chris Mason74123bd2007-02-02 11:05:29 -05001549/*
Chris Mason5f39d392007-10-15 16:14:19 -04001550 * search for key in the extent_buffer. The items start at offset p,
1551 * and they are item_size apart. There are 'max' items in p.
1552 *
Chris Mason74123bd2007-02-02 11:05:29 -05001553 * the slot in the array is returned via slot, and it points to
1554 * the place where you would insert key if it is not found in
1555 * the array.
1556 *
1557 * slot may point to max if the key is bigger than all of the keys
1558 */
Chris Masone02119d2008-09-05 16:13:11 -04001559static noinline int generic_bin_search(struct extent_buffer *eb,
1560 unsigned long p,
1561 int item_size, struct btrfs_key *key,
1562 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563{
1564 int low = 0;
1565 int high = max;
1566 int mid;
1567 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001568 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001569 struct btrfs_disk_key unaligned;
1570 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001571 char *kaddr = NULL;
1572 unsigned long map_start = 0;
1573 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001574 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001575
Chris Masond3977122009-01-05 21:25:51 -05001576 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001577 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 offset = p + mid * item_size;
1579
Chris Masona6591712011-07-19 12:04:14 -04001580 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001581 (offset + sizeof(struct btrfs_disk_key)) >
1582 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001583
1584 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001585 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001586 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001587
Chris Mason479965d2007-10-15 16:14:27 -04001588 if (!err) {
1589 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1590 map_start);
1591 } else {
1592 read_extent_buffer(eb, &unaligned,
1593 offset, sizeof(unaligned));
1594 tmp = &unaligned;
1595 }
1596
Chris Mason5f39d392007-10-15 16:14:19 -04001597 } else {
1598 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1599 map_start);
1600 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001601 ret = comp_keys(tmp, key);
1602
1603 if (ret < 0)
1604 low = mid + 1;
1605 else if (ret > 0)
1606 high = mid;
1607 else {
1608 *slot = mid;
1609 return 0;
1610 }
1611 }
1612 *slot = low;
1613 return 1;
1614}
1615
Chris Mason97571fd2007-02-24 13:39:08 -05001616/*
1617 * simple bin_search frontend that does the right thing for
1618 * leaves vs nodes
1619 */
Chris Mason5f39d392007-10-15 16:14:19 -04001620static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1621 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001622{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001623 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001624 return generic_bin_search(eb,
1625 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001626 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001627 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001628 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001629 else
Chris Mason5f39d392007-10-15 16:14:19 -04001630 return generic_bin_search(eb,
1631 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001632 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001633 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001634 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001635}
1636
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001637int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1638 int level, int *slot)
1639{
1640 return bin_search(eb, key, level, slot);
1641}
1642
Yan, Zhengf0486c62010-05-16 10:46:25 -04001643static void root_add_used(struct btrfs_root *root, u32 size)
1644{
1645 spin_lock(&root->accounting_lock);
1646 btrfs_set_root_used(&root->root_item,
1647 btrfs_root_used(&root->root_item) + size);
1648 spin_unlock(&root->accounting_lock);
1649}
1650
1651static void root_sub_used(struct btrfs_root *root, u32 size)
1652{
1653 spin_lock(&root->accounting_lock);
1654 btrfs_set_root_used(&root->root_item,
1655 btrfs_root_used(&root->root_item) - size);
1656 spin_unlock(&root->accounting_lock);
1657}
1658
Chris Masond352ac62008-09-29 15:18:18 -04001659/* given a node and slot number, this reads the blocks it points to. The
1660 * extent buffer is returned with a reference taken (but unlocked).
1661 * NULL is returned on error.
1662 */
Chris Masone02119d2008-09-05 16:13:11 -04001663static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001664 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001665{
Chris Masonca7a79a2008-05-12 12:59:19 -04001666 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001667 if (slot < 0)
1668 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001669 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001670 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001671
1672 BUG_ON(level == 0);
1673
Chris Masondb945352007-10-15 16:15:53 -04001674 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001675 btrfs_level_size(root, level - 1),
1676 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001677}
1678
Chris Masond352ac62008-09-29 15:18:18 -04001679/*
1680 * node level balancing, used to make sure nodes are in proper order for
1681 * item deletion. We balance from the top down, so we have to make sure
1682 * that a deletion won't leave an node completely empty later on.
1683 */
Chris Masone02119d2008-09-05 16:13:11 -04001684static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001685 struct btrfs_root *root,
1686 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001687{
Chris Mason5f39d392007-10-15 16:14:19 -04001688 struct extent_buffer *right = NULL;
1689 struct extent_buffer *mid;
1690 struct extent_buffer *left = NULL;
1691 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001692 int ret = 0;
1693 int wret;
1694 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001695 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001696 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001697
1698 if (level == 0)
1699 return 0;
1700
Chris Mason5f39d392007-10-15 16:14:19 -04001701 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001702
Chris Masonbd681512011-07-16 15:23:14 -04001703 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1704 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001705 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1706
Chris Mason1d4f8a02007-03-13 09:28:32 -04001707 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001708
Li Zefana05a9bb2011-09-06 16:55:34 +08001709 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001710 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001711 pslot = path->slots[level + 1];
1712 }
Chris Masonbb803952007-03-01 12:04:21 -05001713
Chris Mason40689472007-03-17 14:29:23 -04001714 /*
1715 * deal with the case where there is only one pointer in the root
1716 * by promoting the node below to a root
1717 */
Chris Mason5f39d392007-10-15 16:14:19 -04001718 if (!parent) {
1719 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001720
Chris Mason5f39d392007-10-15 16:14:19 -04001721 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001722 return 0;
1723
1724 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001725 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001726 if (!child) {
1727 ret = -EROFS;
1728 btrfs_std_error(root->fs_info, ret);
1729 goto enospc;
1730 }
1731
Chris Mason925baed2008-06-25 16:01:30 -04001732 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001733 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001734 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001735 if (ret) {
1736 btrfs_tree_unlock(child);
1737 free_extent_buffer(child);
1738 goto enospc;
1739 }
Yan2f375ab2008-02-01 14:58:07 -05001740
Jan Schmidtba1bfbd2012-10-22 20:02:56 +02001741 tree_mod_log_free_eb(root->fs_info, root->node);
Jan Schmidtf2304752012-05-26 11:43:17 +02001742 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001743 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001744
Chris Mason0b86a832008-03-24 15:01:56 -04001745 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001746 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001747
Chris Mason925baed2008-06-25 16:01:30 -04001748 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001749 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001750 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001751 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001752 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001753 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001754
1755 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001756 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001757 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001758 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001759 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001760 }
Chris Mason5f39d392007-10-15 16:14:19 -04001761 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001762 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001763 return 0;
1764
Chris Mason5f39d392007-10-15 16:14:19 -04001765 left = read_node_slot(root, parent, pslot - 1);
1766 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001767 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001769 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001770 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001771 if (wret) {
1772 ret = wret;
1773 goto enospc;
1774 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001775 }
Chris Mason5f39d392007-10-15 16:14:19 -04001776 right = read_node_slot(root, parent, pslot + 1);
1777 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001778 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001779 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001780 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001781 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001782 if (wret) {
1783 ret = wret;
1784 goto enospc;
1785 }
1786 }
1787
1788 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001789 if (left) {
1790 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001791 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001792 if (wret < 0)
1793 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001794 }
Chris Mason79f95c82007-03-01 15:16:26 -05001795
1796 /*
1797 * then try to empty the right most buffer into the middle
1798 */
Chris Mason5f39d392007-10-15 16:14:19 -04001799 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001800 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001801 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001802 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001803 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001804 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001805 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001806 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001807 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001808 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001809 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001810 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001811 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001812 struct btrfs_disk_key right_key;
1813 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001814 tree_mod_log_set_node_key(root->fs_info, parent,
1815 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001816 btrfs_set_node_key(parent, &right_key, pslot + 1);
1817 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001818 }
1819 }
Chris Mason5f39d392007-10-15 16:14:19 -04001820 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001821 /*
1822 * we're not allowed to leave a node with one item in the
1823 * tree during a delete. A deletion from lower in the tree
1824 * could try to delete the only pointer in this node.
1825 * So, pull some keys from the left.
1826 * There has to be a left pointer at this point because
1827 * otherwise we would have pulled some pointers from the
1828 * right
1829 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001830 if (!left) {
1831 ret = -EROFS;
1832 btrfs_std_error(root->fs_info, ret);
1833 goto enospc;
1834 }
Chris Mason5f39d392007-10-15 16:14:19 -04001835 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001836 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001837 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001838 goto enospc;
1839 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001840 if (wret == 1) {
1841 wret = push_node_left(trans, root, left, mid, 1);
1842 if (wret < 0)
1843 ret = wret;
1844 }
Chris Mason79f95c82007-03-01 15:16:26 -05001845 BUG_ON(wret == 1);
1846 }
Chris Mason5f39d392007-10-15 16:14:19 -04001847 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001848 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001849 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001850 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001851 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001852 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001853 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001854 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001855 } else {
1856 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001857 struct btrfs_disk_key mid_key;
1858 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001859 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1860 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001861 btrfs_set_node_key(parent, &mid_key, pslot);
1862 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001863 }
Chris Masonbb803952007-03-01 12:04:21 -05001864
Chris Mason79f95c82007-03-01 15:16:26 -05001865 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001866 if (left) {
1867 if (btrfs_header_nritems(left) > orig_slot) {
1868 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001869 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001870 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001871 path->slots[level + 1] -= 1;
1872 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001873 if (mid) {
1874 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001875 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001876 }
Chris Masonbb803952007-03-01 12:04:21 -05001877 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001878 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001879 path->slots[level] = orig_slot;
1880 }
1881 }
Chris Mason79f95c82007-03-01 15:16:26 -05001882 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001883 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001884 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001885 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001886enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001887 if (right) {
1888 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001889 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001890 }
1891 if (left) {
1892 if (path->nodes[level] != left)
1893 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001894 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001895 }
Chris Masonbb803952007-03-01 12:04:21 -05001896 return ret;
1897}
1898
Chris Masond352ac62008-09-29 15:18:18 -04001899/* Node balancing for insertion. Here we only split or push nodes around
1900 * when they are completely full. This is also done top down, so we
1901 * have to be pessimistic.
1902 */
Chris Masond3977122009-01-05 21:25:51 -05001903static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001904 struct btrfs_root *root,
1905 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001906{
Chris Mason5f39d392007-10-15 16:14:19 -04001907 struct extent_buffer *right = NULL;
1908 struct extent_buffer *mid;
1909 struct extent_buffer *left = NULL;
1910 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001911 int ret = 0;
1912 int wret;
1913 int pslot;
1914 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001915
1916 if (level == 0)
1917 return 1;
1918
Chris Mason5f39d392007-10-15 16:14:19 -04001919 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001920 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001921
Li Zefana05a9bb2011-09-06 16:55:34 +08001922 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001923 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001924 pslot = path->slots[level + 1];
1925 }
Chris Masone66f7092007-04-20 13:16:02 -04001926
Chris Mason5f39d392007-10-15 16:14:19 -04001927 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001928 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001931
1932 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001933 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001934 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001935
1936 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001937 btrfs_set_lock_blocking(left);
1938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001940 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1941 wret = 1;
1942 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001943 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001944 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001945 if (ret)
1946 wret = 1;
1947 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001948 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001949 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001950 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001951 }
Chris Masone66f7092007-04-20 13:16:02 -04001952 if (wret < 0)
1953 ret = wret;
1954 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001955 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001956 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001957 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001958 tree_mod_log_set_node_key(root->fs_info, parent,
1959 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001960 btrfs_set_node_key(parent, &disk_key, pslot);
1961 btrfs_mark_buffer_dirty(parent);
1962 if (btrfs_header_nritems(left) > orig_slot) {
1963 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001964 path->slots[level + 1] -= 1;
1965 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001966 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001967 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001968 } else {
1969 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001970 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001971 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001972 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001973 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001974 }
Chris Masone66f7092007-04-20 13:16:02 -04001975 return 0;
1976 }
Chris Mason925baed2008-06-25 16:01:30 -04001977 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001978 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001979 }
Chris Mason925baed2008-06-25 16:01:30 -04001980 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001981
1982 /*
1983 * then try to empty the right most buffer into the middle
1984 */
Chris Mason5f39d392007-10-15 16:14:19 -04001985 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001986 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001987
Chris Mason925baed2008-06-25 16:01:30 -04001988 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001989 btrfs_set_lock_blocking(right);
1990
Chris Mason5f39d392007-10-15 16:14:19 -04001991 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001992 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1993 wret = 1;
1994 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001995 ret = btrfs_cow_block(trans, root, right,
1996 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001997 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001998 if (ret)
1999 wret = 1;
2000 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002001 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002002 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002003 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002004 }
Chris Masone66f7092007-04-20 13:16:02 -04002005 if (wret < 0)
2006 ret = wret;
2007 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002008 struct btrfs_disk_key disk_key;
2009
2010 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002011 tree_mod_log_set_node_key(root->fs_info, parent,
2012 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002013 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2014 btrfs_mark_buffer_dirty(parent);
2015
2016 if (btrfs_header_nritems(mid) <= orig_slot) {
2017 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002018 path->slots[level + 1] += 1;
2019 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002020 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002021 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002022 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002023 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002024 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002025 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002026 }
Chris Masone66f7092007-04-20 13:16:02 -04002027 return 0;
2028 }
Chris Mason925baed2008-06-25 16:01:30 -04002029 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002030 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002031 }
Chris Masone66f7092007-04-20 13:16:02 -04002032 return 1;
2033}
2034
Chris Mason74123bd2007-02-02 11:05:29 -05002035/*
Chris Masond352ac62008-09-29 15:18:18 -04002036 * readahead one full node of leaves, finding things that are close
2037 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002038 */
Chris Masonc8c42862009-04-03 10:14:18 -04002039static void reada_for_search(struct btrfs_root *root,
2040 struct btrfs_path *path,
2041 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002042{
Chris Mason5f39d392007-10-15 16:14:19 -04002043 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002044 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002045 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002046 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002047 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002048 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002049 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002050 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002051 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002052 u32 nr;
2053 u32 blocksize;
2054 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002055
Chris Masona6b6e752007-10-15 16:22:39 -04002056 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002057 return;
2058
Chris Mason6702ed42007-08-07 16:15:09 -04002059 if (!path->nodes[level])
2060 return;
2061
Chris Mason5f39d392007-10-15 16:14:19 -04002062 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002063
Chris Mason3c69fae2007-08-07 15:52:22 -04002064 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002065 blocksize = btrfs_level_size(root, level - 1);
2066 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002067 if (eb) {
2068 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002069 return;
2070 }
2071
Chris Masona7175312009-01-22 09:23:10 -05002072 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002073
Chris Mason5f39d392007-10-15 16:14:19 -04002074 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002075 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002076
Chris Masond3977122009-01-05 21:25:51 -05002077 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002078 if (direction < 0) {
2079 if (nr == 0)
2080 break;
2081 nr--;
2082 } else if (direction > 0) {
2083 nr++;
2084 if (nr >= nritems)
2085 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002086 }
Chris Mason01f46652007-12-21 16:24:26 -05002087 if (path->reada < 0 && objectid) {
2088 btrfs_node_key(node, &disk_key, nr);
2089 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2090 break;
2091 }
Chris Mason6b800532007-10-15 16:17:34 -04002092 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002093 if ((search <= target && target - search <= 65536) ||
2094 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002095 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002096 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002097 nread += blocksize;
2098 }
2099 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002100 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002101 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002102 }
2103}
Chris Mason925baed2008-06-25 16:01:30 -04002104
Chris Masond352ac62008-09-29 15:18:18 -04002105/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002106 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2107 * cache
2108 */
2109static noinline int reada_for_balance(struct btrfs_root *root,
2110 struct btrfs_path *path, int level)
2111{
2112 int slot;
2113 int nritems;
2114 struct extent_buffer *parent;
2115 struct extent_buffer *eb;
2116 u64 gen;
2117 u64 block1 = 0;
2118 u64 block2 = 0;
2119 int ret = 0;
2120 int blocksize;
2121
Chris Mason8c594ea2009-04-20 15:50:10 -04002122 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002123 if (!parent)
2124 return 0;
2125
2126 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002127 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002128 blocksize = btrfs_level_size(root, level);
2129
2130 if (slot > 0) {
2131 block1 = btrfs_node_blockptr(parent, slot - 1);
2132 gen = btrfs_node_ptr_generation(parent, slot - 1);
2133 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002134 /*
2135 * if we get -eagain from btrfs_buffer_uptodate, we
2136 * don't want to return eagain here. That will loop
2137 * forever
2138 */
2139 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002140 block1 = 0;
2141 free_extent_buffer(eb);
2142 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002143 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002144 block2 = btrfs_node_blockptr(parent, slot + 1);
2145 gen = btrfs_node_ptr_generation(parent, slot + 1);
2146 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002147 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002148 block2 = 0;
2149 free_extent_buffer(eb);
2150 }
2151 if (block1 || block2) {
2152 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002153
2154 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002155 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002156
2157 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002158 if (block1)
2159 readahead_tree_block(root, block1, blocksize, 0);
2160 if (block2)
2161 readahead_tree_block(root, block2, blocksize, 0);
2162
2163 if (block1) {
2164 eb = read_tree_block(root, block1, blocksize, 0);
2165 free_extent_buffer(eb);
2166 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002167 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002168 eb = read_tree_block(root, block2, blocksize, 0);
2169 free_extent_buffer(eb);
2170 }
2171 }
2172 return ret;
2173}
2174
2175
2176/*
Chris Masond3977122009-01-05 21:25:51 -05002177 * when we walk down the tree, it is usually safe to unlock the higher layers
2178 * in the tree. The exceptions are when our path goes through slot 0, because
2179 * operations on the tree might require changing key pointers higher up in the
2180 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002181 *
Chris Masond3977122009-01-05 21:25:51 -05002182 * callers might also have set path->keep_locks, which tells this code to keep
2183 * the lock if the path points to the last slot in the block. This is part of
2184 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002185 *
Chris Masond3977122009-01-05 21:25:51 -05002186 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2187 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002188 */
Chris Masone02119d2008-09-05 16:13:11 -04002189static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002190 int lowest_unlock, int min_write_lock_level,
2191 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002192{
2193 int i;
2194 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002195 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002196 struct extent_buffer *t;
2197
2198 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2199 if (!path->nodes[i])
2200 break;
2201 if (!path->locks[i])
2202 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002203 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002204 skip_level = i + 1;
2205 continue;
2206 }
Chris Mason051e1b92008-06-25 16:01:30 -04002207 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002208 u32 nritems;
2209 t = path->nodes[i];
2210 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002211 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002212 skip_level = i + 1;
2213 continue;
2214 }
2215 }
Chris Mason051e1b92008-06-25 16:01:30 -04002216 if (skip_level < i && i >= lowest_unlock)
2217 no_skips = 1;
2218
Chris Mason925baed2008-06-25 16:01:30 -04002219 t = path->nodes[i];
2220 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002221 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002222 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002223 if (write_lock_level &&
2224 i > min_write_lock_level &&
2225 i <= *write_lock_level) {
2226 *write_lock_level = i - 1;
2227 }
Chris Mason925baed2008-06-25 16:01:30 -04002228 }
2229 }
2230}
2231
Chris Mason3c69fae2007-08-07 15:52:22 -04002232/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002233 * This releases any locks held in the path starting at level and
2234 * going all the way up to the root.
2235 *
2236 * btrfs_search_slot will keep the lock held on higher nodes in a few
2237 * corner cases, such as COW of the block at slot zero in the node. This
2238 * ignores those rules, and it should only be called when there are no
2239 * more updates to be done higher up in the tree.
2240 */
2241noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2242{
2243 int i;
2244
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002245 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002246 return;
2247
2248 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2249 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002250 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002251 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002252 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002253 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002254 path->locks[i] = 0;
2255 }
2256}
2257
2258/*
Chris Masonc8c42862009-04-03 10:14:18 -04002259 * helper function for btrfs_search_slot. The goal is to find a block
2260 * in cache without setting the path to blocking. If we find the block
2261 * we return zero and the path is unchanged.
2262 *
2263 * If we can't find the block, we set the path blocking and do some
2264 * reada. -EAGAIN is returned and the search must be repeated.
2265 */
2266static int
2267read_block_for_search(struct btrfs_trans_handle *trans,
2268 struct btrfs_root *root, struct btrfs_path *p,
2269 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002270 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002271{
2272 u64 blocknr;
2273 u64 gen;
2274 u32 blocksize;
2275 struct extent_buffer *b = *eb_ret;
2276 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002277 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002278
2279 blocknr = btrfs_node_blockptr(b, slot);
2280 gen = btrfs_node_ptr_generation(b, slot);
2281 blocksize = btrfs_level_size(root, level - 1);
2282
2283 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002284 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002285 /* first we do an atomic uptodate check */
2286 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2287 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002288 /*
2289 * we found an up to date block without
2290 * sleeping, return
2291 * right away
2292 */
2293 *eb_ret = tmp;
2294 return 0;
2295 }
2296 /* the pages were up to date, but we failed
2297 * the generation number check. Do a full
2298 * read for the generation number that is correct.
2299 * We must do this without dropping locks so
2300 * we can trust our generation number
2301 */
2302 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002303 btrfs_set_path_blocking(p);
2304
Chris Masonb9fab912012-05-06 07:23:47 -04002305 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002306 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002307 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002308 *eb_ret = tmp;
2309 return 0;
2310 }
2311 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002312 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002313 return -EIO;
2314 }
Chris Masonc8c42862009-04-03 10:14:18 -04002315 }
2316
2317 /*
2318 * reduce lock contention at high levels
2319 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002320 * we read. Don't release the lock on the current
2321 * level because we need to walk this node to figure
2322 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002323 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002324 btrfs_unlock_up_safe(p, level + 1);
2325 btrfs_set_path_blocking(p);
2326
Chris Masoncb449212010-10-24 11:01:27 -04002327 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002328 if (p->reada)
2329 reada_for_search(root, p, level, slot, key->objectid);
2330
David Sterbab3b4aa72011-04-21 01:20:15 +02002331 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002332
2333 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002334 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002335 if (tmp) {
2336 /*
2337 * If the read above didn't mark this buffer up to date,
2338 * it will never end up being up to date. Set ret to EIO now
2339 * and give up so that our caller doesn't loop forever
2340 * on our EAGAINs.
2341 */
Chris Masonb9fab912012-05-06 07:23:47 -04002342 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002343 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002344 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002345 }
2346 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002347}
2348
2349/*
2350 * helper function for btrfs_search_slot. This does all of the checks
2351 * for node-level blocks and does any balancing required based on
2352 * the ins_len.
2353 *
2354 * If no extra work was required, zero is returned. If we had to
2355 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2356 * start over
2357 */
2358static int
2359setup_nodes_for_search(struct btrfs_trans_handle *trans,
2360 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002361 struct extent_buffer *b, int level, int ins_len,
2362 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002363{
2364 int ret;
2365 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2366 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2367 int sret;
2368
Chris Masonbd681512011-07-16 15:23:14 -04002369 if (*write_lock_level < level + 1) {
2370 *write_lock_level = level + 1;
2371 btrfs_release_path(p);
2372 goto again;
2373 }
2374
Chris Masonc8c42862009-04-03 10:14:18 -04002375 sret = reada_for_balance(root, p, level);
2376 if (sret)
2377 goto again;
2378
2379 btrfs_set_path_blocking(p);
2380 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002381 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002382
2383 BUG_ON(sret > 0);
2384 if (sret) {
2385 ret = sret;
2386 goto done;
2387 }
2388 b = p->nodes[level];
2389 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002390 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002391 int sret;
2392
Chris Masonbd681512011-07-16 15:23:14 -04002393 if (*write_lock_level < level + 1) {
2394 *write_lock_level = level + 1;
2395 btrfs_release_path(p);
2396 goto again;
2397 }
2398
Chris Masonc8c42862009-04-03 10:14:18 -04002399 sret = reada_for_balance(root, p, level);
2400 if (sret)
2401 goto again;
2402
2403 btrfs_set_path_blocking(p);
2404 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002405 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002406
2407 if (sret) {
2408 ret = sret;
2409 goto done;
2410 }
2411 b = p->nodes[level];
2412 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002413 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002414 goto again;
2415 }
2416 BUG_ON(btrfs_header_nritems(b) == 1);
2417 }
2418 return 0;
2419
2420again:
2421 ret = -EAGAIN;
2422done:
2423 return ret;
2424}
2425
2426/*
Chris Mason74123bd2007-02-02 11:05:29 -05002427 * look for key in the tree. path is filled in with nodes along the way
2428 * if key is found, we return zero and you can find the item in the leaf
2429 * level of the path (level 0)
2430 *
2431 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002432 * be inserted, and 1 is returned. If there are other errors during the
2433 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002434 *
2435 * if ins_len > 0, nodes and leaves will be split as we walk down the
2436 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2437 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002438 */
Chris Masone089f052007-03-16 16:20:31 -04002439int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2440 *root, struct btrfs_key *key, struct btrfs_path *p, int
2441 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002442{
Chris Mason5f39d392007-10-15 16:14:19 -04002443 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002444 int slot;
2445 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002446 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002447 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002448 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002449 int root_lock;
2450 /* everything at write_lock_level or lower must be write locked */
2451 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002452 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002453 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002454
Chris Mason6702ed42007-08-07 16:15:09 -04002455 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002456 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002457 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002458
Chris Masonbd681512011-07-16 15:23:14 -04002459 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002460 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002461
Chris Masonbd681512011-07-16 15:23:14 -04002462 /* when we are removing items, we might have to go up to level
2463 * two as we update tree pointers Make sure we keep write
2464 * for those levels as well
2465 */
2466 write_lock_level = 2;
2467 } else if (ins_len > 0) {
2468 /*
2469 * for inserting items, make sure we have a write lock on
2470 * level 1 so we can update keys
2471 */
2472 write_lock_level = 1;
2473 }
2474
2475 if (!cow)
2476 write_lock_level = -1;
2477
2478 if (cow && (p->keep_locks || p->lowest_level))
2479 write_lock_level = BTRFS_MAX_LEVEL;
2480
Chris Masonf7c79f32012-03-19 15:54:38 -04002481 min_write_lock_level = write_lock_level;
2482
Chris Masonbb803952007-03-01 12:04:21 -05002483again:
Chris Masonbd681512011-07-16 15:23:14 -04002484 /*
2485 * we try very hard to do read locks on the root
2486 */
2487 root_lock = BTRFS_READ_LOCK;
2488 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002489 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002490 /*
2491 * the commit roots are read only
2492 * so we always do read locks
2493 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494 b = root->commit_root;
2495 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002496 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002498 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002499 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002500 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002502 level = btrfs_header_level(b);
2503 } else {
2504 /* we don't know the level of the root node
2505 * until we actually have it read locked
2506 */
2507 b = btrfs_read_lock_root_node(root);
2508 level = btrfs_header_level(b);
2509 if (level <= write_lock_level) {
2510 /* whoops, must trade for write lock */
2511 btrfs_tree_read_unlock(b);
2512 free_extent_buffer(b);
2513 b = btrfs_lock_root_node(root);
2514 root_lock = BTRFS_WRITE_LOCK;
2515
2516 /* the level might have changed, check again */
2517 level = btrfs_header_level(b);
2518 }
2519 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002520 }
Chris Masonbd681512011-07-16 15:23:14 -04002521 p->nodes[level] = b;
2522 if (!p->skip_locking)
2523 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002524
Chris Masoneb60cea2007-02-02 09:18:22 -05002525 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002526 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002527
2528 /*
2529 * setup the path here so we can release it under lock
2530 * contention with the cow code
2531 */
Chris Mason02217ed2007-03-02 16:08:05 -05002532 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002533 /*
2534 * if we don't really need to cow this block
2535 * then we don't want to set the path blocking,
2536 * so we test it here
2537 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002538 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002539 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540
Chris Masonb4ce94d2009-02-04 09:25:08 -05002541 btrfs_set_path_blocking(p);
2542
Chris Masonbd681512011-07-16 15:23:14 -04002543 /*
2544 * must have write locks on this node and the
2545 * parent
2546 */
2547 if (level + 1 > write_lock_level) {
2548 write_lock_level = level + 1;
2549 btrfs_release_path(p);
2550 goto again;
2551 }
2552
Yan Zheng33c66f42009-07-22 09:59:00 -04002553 err = btrfs_cow_block(trans, root, b,
2554 p->nodes[level + 1],
2555 p->slots[level + 1], &b);
2556 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002557 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002558 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002559 }
Chris Mason02217ed2007-03-02 16:08:05 -05002560 }
Chris Mason65b51a02008-08-01 15:11:20 -04002561cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002562 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002563
Chris Masoneb60cea2007-02-02 09:18:22 -05002564 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002565 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002566
2567 /*
2568 * we have a lock on b and as long as we aren't changing
2569 * the tree, there is no way to for the items in b to change.
2570 * It is safe to drop the lock on our parent before we
2571 * go through the expensive btree search on b.
2572 *
2573 * If cow is true, then we might be changing slot zero,
2574 * which may require changing the parent. So, we can't
2575 * drop the lock until after we know which slot we're
2576 * operating on.
2577 */
2578 if (!cow)
2579 btrfs_unlock_up_safe(p, level + 1);
2580
Chris Mason5f39d392007-10-15 16:14:19 -04002581 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002582
Chris Mason5f39d392007-10-15 16:14:19 -04002583 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002584 int dec = 0;
2585 if (ret && slot > 0) {
2586 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002587 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002588 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002590 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002591 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002592 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002593 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002594 if (err) {
2595 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002596 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002597 }
Chris Masonc8c42862009-04-03 10:14:18 -04002598 b = p->nodes[level];
2599 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002600
Chris Masonbd681512011-07-16 15:23:14 -04002601 /*
2602 * slot 0 is special, if we change the key
2603 * we have to update the parent pointer
2604 * which means we must have a write lock
2605 * on the parent
2606 */
2607 if (slot == 0 && cow &&
2608 write_lock_level < level + 1) {
2609 write_lock_level = level + 1;
2610 btrfs_release_path(p);
2611 goto again;
2612 }
2613
Chris Masonf7c79f32012-03-19 15:54:38 -04002614 unlock_up(p, level, lowest_unlock,
2615 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002616
Chris Mason925baed2008-06-25 16:01:30 -04002617 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002618 if (dec)
2619 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002620 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002621 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002622
Yan Zheng33c66f42009-07-22 09:59:00 -04002623 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002624 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002625 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002626 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002627 if (err) {
2628 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002629 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002630 }
Chris Mason76a05b32009-05-14 13:24:30 -04002631
Chris Masonb4ce94d2009-02-04 09:25:08 -05002632 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002633 level = btrfs_header_level(b);
2634 if (level <= write_lock_level) {
2635 err = btrfs_try_tree_write_lock(b);
2636 if (!err) {
2637 btrfs_set_path_blocking(p);
2638 btrfs_tree_lock(b);
2639 btrfs_clear_path_blocking(p, b,
2640 BTRFS_WRITE_LOCK);
2641 }
2642 p->locks[level] = BTRFS_WRITE_LOCK;
2643 } else {
2644 err = btrfs_try_tree_read_lock(b);
2645 if (!err) {
2646 btrfs_set_path_blocking(p);
2647 btrfs_tree_read_lock(b);
2648 btrfs_clear_path_blocking(p, b,
2649 BTRFS_READ_LOCK);
2650 }
2651 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002652 }
Chris Masonbd681512011-07-16 15:23:14 -04002653 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002654 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002655 } else {
2656 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002657 if (ins_len > 0 &&
2658 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002659 if (write_lock_level < 1) {
2660 write_lock_level = 1;
2661 btrfs_release_path(p);
2662 goto again;
2663 }
2664
Chris Masonb4ce94d2009-02-04 09:25:08 -05002665 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002666 err = split_leaf(trans, root, key,
2667 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002668 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002669
Yan Zheng33c66f42009-07-22 09:59:00 -04002670 BUG_ON(err > 0);
2671 if (err) {
2672 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002673 goto done;
2674 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002675 }
Chris Mason459931e2008-12-10 09:10:46 -05002676 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002677 unlock_up(p, level, lowest_unlock,
2678 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002679 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680 }
2681 }
Chris Mason65b51a02008-08-01 15:11:20 -04002682 ret = 1;
2683done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002684 /*
2685 * we don't really know what they plan on doing with the path
2686 * from here on, so for now just mark it as blocking
2687 */
Chris Masonb9473432009-03-13 11:00:37 -04002688 if (!p->leave_spinning)
2689 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002690 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002691 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002692 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002693}
2694
Chris Mason74123bd2007-02-02 11:05:29 -05002695/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002696 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2697 * current state of the tree together with the operations recorded in the tree
2698 * modification log to search for the key in a previous version of this tree, as
2699 * denoted by the time_seq parameter.
2700 *
2701 * Naturally, there is no support for insert, delete or cow operations.
2702 *
2703 * The resulting path and return value will be set up as if we called
2704 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2705 */
2706int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2707 struct btrfs_path *p, u64 time_seq)
2708{
2709 struct extent_buffer *b;
2710 int slot;
2711 int ret;
2712 int err;
2713 int level;
2714 int lowest_unlock = 1;
2715 u8 lowest_level = 0;
2716
2717 lowest_level = p->lowest_level;
2718 WARN_ON(p->nodes[0] != NULL);
2719
2720 if (p->search_commit_root) {
2721 BUG_ON(time_seq);
2722 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2723 }
2724
2725again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002726 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002727 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002728 p->locks[level] = BTRFS_READ_LOCK;
2729
2730 while (b) {
2731 level = btrfs_header_level(b);
2732 p->nodes[level] = b;
2733 btrfs_clear_path_blocking(p, NULL, 0);
2734
2735 /*
2736 * we have a lock on b and as long as we aren't changing
2737 * the tree, there is no way to for the items in b to change.
2738 * It is safe to drop the lock on our parent before we
2739 * go through the expensive btree search on b.
2740 */
2741 btrfs_unlock_up_safe(p, level + 1);
2742
2743 ret = bin_search(b, key, level, &slot);
2744
2745 if (level != 0) {
2746 int dec = 0;
2747 if (ret && slot > 0) {
2748 dec = 1;
2749 slot -= 1;
2750 }
2751 p->slots[level] = slot;
2752 unlock_up(p, level, lowest_unlock, 0, NULL);
2753
2754 if (level == lowest_level) {
2755 if (dec)
2756 p->slots[level]++;
2757 goto done;
2758 }
2759
2760 err = read_block_for_search(NULL, root, p, &b, level,
2761 slot, key, time_seq);
2762 if (err == -EAGAIN)
2763 goto again;
2764 if (err) {
2765 ret = err;
2766 goto done;
2767 }
2768
2769 level = btrfs_header_level(b);
2770 err = btrfs_try_tree_read_lock(b);
2771 if (!err) {
2772 btrfs_set_path_blocking(p);
2773 btrfs_tree_read_lock(b);
2774 btrfs_clear_path_blocking(p, b,
2775 BTRFS_READ_LOCK);
2776 }
2777 p->locks[level] = BTRFS_READ_LOCK;
2778 p->nodes[level] = b;
2779 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2780 if (b != p->nodes[level]) {
2781 btrfs_tree_unlock_rw(p->nodes[level],
2782 p->locks[level]);
2783 p->locks[level] = 0;
2784 p->nodes[level] = b;
2785 }
2786 } else {
2787 p->slots[level] = slot;
2788 unlock_up(p, level, lowest_unlock, 0, NULL);
2789 goto done;
2790 }
2791 }
2792 ret = 1;
2793done:
2794 if (!p->leave_spinning)
2795 btrfs_set_path_blocking(p);
2796 if (ret < 0)
2797 btrfs_release_path(p);
2798
2799 return ret;
2800}
2801
2802/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002803 * helper to use instead of search slot if no exact match is needed but
2804 * instead the next or previous item should be returned.
2805 * When find_higher is true, the next higher item is returned, the next lower
2806 * otherwise.
2807 * When return_any and find_higher are both true, and no higher item is found,
2808 * return the next lower instead.
2809 * When return_any is true and find_higher is false, and no lower item is found,
2810 * return the next higher instead.
2811 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2812 * < 0 on error
2813 */
2814int btrfs_search_slot_for_read(struct btrfs_root *root,
2815 struct btrfs_key *key, struct btrfs_path *p,
2816 int find_higher, int return_any)
2817{
2818 int ret;
2819 struct extent_buffer *leaf;
2820
2821again:
2822 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2823 if (ret <= 0)
2824 return ret;
2825 /*
2826 * a return value of 1 means the path is at the position where the
2827 * item should be inserted. Normally this is the next bigger item,
2828 * but in case the previous item is the last in a leaf, path points
2829 * to the first free slot in the previous leaf, i.e. at an invalid
2830 * item.
2831 */
2832 leaf = p->nodes[0];
2833
2834 if (find_higher) {
2835 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2836 ret = btrfs_next_leaf(root, p);
2837 if (ret <= 0)
2838 return ret;
2839 if (!return_any)
2840 return 1;
2841 /*
2842 * no higher item found, return the next
2843 * lower instead
2844 */
2845 return_any = 0;
2846 find_higher = 0;
2847 btrfs_release_path(p);
2848 goto again;
2849 }
2850 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002851 if (p->slots[0] == 0) {
2852 ret = btrfs_prev_leaf(root, p);
2853 if (ret < 0)
2854 return ret;
2855 if (!ret) {
2856 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2857 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002858 }
Arne Jansene6793762011-09-13 11:18:10 +02002859 if (!return_any)
2860 return 1;
2861 /*
2862 * no lower item found, return the next
2863 * higher instead
2864 */
2865 return_any = 0;
2866 find_higher = 1;
2867 btrfs_release_path(p);
2868 goto again;
2869 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002870 --p->slots[0];
2871 }
2872 }
2873 return 0;
2874}
2875
2876/*
Chris Mason74123bd2007-02-02 11:05:29 -05002877 * adjust the pointers going up the tree, starting at level
2878 * making sure the right key of each node is points to 'key'.
2879 * This is used after shifting pointers to the left, so it stops
2880 * fixing up pointers when a given leaf/node is not in slot 0 of the
2881 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002882 *
Chris Mason74123bd2007-02-02 11:05:29 -05002883 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002884static void fixup_low_keys(struct btrfs_trans_handle *trans,
2885 struct btrfs_root *root, struct btrfs_path *path,
2886 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002887{
2888 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002889 struct extent_buffer *t;
2890
Chris Mason234b63a2007-03-13 10:46:10 -04002891 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002892 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002893 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002894 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002895 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002896 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002897 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002898 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002899 if (tslot != 0)
2900 break;
2901 }
2902}
2903
Chris Mason74123bd2007-02-02 11:05:29 -05002904/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002905 * update item key.
2906 *
2907 * This function isn't completely safe. It's the caller's responsibility
2908 * that the new key won't break the order
2909 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002910void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *root, struct btrfs_path *path,
2912 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002913{
2914 struct btrfs_disk_key disk_key;
2915 struct extent_buffer *eb;
2916 int slot;
2917
2918 eb = path->nodes[0];
2919 slot = path->slots[0];
2920 if (slot > 0) {
2921 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002922 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002923 }
2924 if (slot < btrfs_header_nritems(eb) - 1) {
2925 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002926 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002927 }
2928
2929 btrfs_cpu_key_to_disk(&disk_key, new_key);
2930 btrfs_set_item_key(eb, &disk_key, slot);
2931 btrfs_mark_buffer_dirty(eb);
2932 if (slot == 0)
2933 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002934}
2935
2936/*
Chris Mason74123bd2007-02-02 11:05:29 -05002937 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002938 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002939 *
2940 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2941 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002942 */
Chris Mason98ed5172008-01-03 10:01:48 -05002943static int push_node_left(struct btrfs_trans_handle *trans,
2944 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002945 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002946{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002947 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002948 int src_nritems;
2949 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002950 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002951
Chris Mason5f39d392007-10-15 16:14:19 -04002952 src_nritems = btrfs_header_nritems(src);
2953 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002954 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002955 WARN_ON(btrfs_header_generation(src) != trans->transid);
2956 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002957
Chris Masonbce4eae2008-04-24 14:42:46 -04002958 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002959 return 1;
2960
Chris Masond3977122009-01-05 21:25:51 -05002961 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002962 return 1;
2963
Chris Masonbce4eae2008-04-24 14:42:46 -04002964 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002965 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002966 if (push_items < src_nritems) {
2967 /* leave at least 8 pointers in the node if
2968 * we aren't going to empty it
2969 */
2970 if (src_nritems - push_items < 8) {
2971 if (push_items <= 8)
2972 return 1;
2973 push_items -= 8;
2974 }
2975 }
2976 } else
2977 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002978
Jan Schmidtf2304752012-05-26 11:43:17 +02002979 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2980 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002981 copy_extent_buffer(dst, src,
2982 btrfs_node_key_ptr_offset(dst_nritems),
2983 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002984 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002985
Chris Masonbb803952007-03-01 12:04:21 -05002986 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02002987 /*
2988 * don't call tree_mod_log_eb_move here, key removal was already
2989 * fully logged by tree_mod_log_eb_copy above.
2990 */
Chris Mason5f39d392007-10-15 16:14:19 -04002991 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2992 btrfs_node_key_ptr_offset(push_items),
2993 (src_nritems - push_items) *
2994 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002995 }
Chris Mason5f39d392007-10-15 16:14:19 -04002996 btrfs_set_header_nritems(src, src_nritems - push_items);
2997 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2998 btrfs_mark_buffer_dirty(src);
2999 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003000
Chris Masonbb803952007-03-01 12:04:21 -05003001 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003002}
3003
Chris Mason97571fd2007-02-24 13:39:08 -05003004/*
Chris Mason79f95c82007-03-01 15:16:26 -05003005 * try to push data from one node into the next node right in the
3006 * tree.
3007 *
3008 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3009 * error, and > 0 if there was no room in the right hand block.
3010 *
3011 * this will only push up to 1/2 the contents of the left node over
3012 */
Chris Mason5f39d392007-10-15 16:14:19 -04003013static int balance_node_right(struct btrfs_trans_handle *trans,
3014 struct btrfs_root *root,
3015 struct extent_buffer *dst,
3016 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003017{
Chris Mason79f95c82007-03-01 15:16:26 -05003018 int push_items = 0;
3019 int max_push;
3020 int src_nritems;
3021 int dst_nritems;
3022 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003023
Chris Mason7bb86312007-12-11 09:25:06 -05003024 WARN_ON(btrfs_header_generation(src) != trans->transid);
3025 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3026
Chris Mason5f39d392007-10-15 16:14:19 -04003027 src_nritems = btrfs_header_nritems(src);
3028 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003029 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003030 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003031 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003032
Chris Masond3977122009-01-05 21:25:51 -05003033 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003034 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003035
3036 max_push = src_nritems / 2 + 1;
3037 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003038 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003039 return 1;
Yan252c38f2007-08-29 09:11:44 -04003040
Chris Mason79f95c82007-03-01 15:16:26 -05003041 if (max_push < push_items)
3042 push_items = max_push;
3043
Jan Schmidtf2304752012-05-26 11:43:17 +02003044 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003045 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3046 btrfs_node_key_ptr_offset(0),
3047 (dst_nritems) *
3048 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003049
Jan Schmidtf2304752012-05-26 11:43:17 +02003050 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3051 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003052 copy_extent_buffer(dst, src,
3053 btrfs_node_key_ptr_offset(0),
3054 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003055 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003056
Chris Mason5f39d392007-10-15 16:14:19 -04003057 btrfs_set_header_nritems(src, src_nritems - push_items);
3058 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003059
Chris Mason5f39d392007-10-15 16:14:19 -04003060 btrfs_mark_buffer_dirty(src);
3061 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003062
Chris Mason79f95c82007-03-01 15:16:26 -05003063 return ret;
3064}
3065
3066/*
Chris Mason97571fd2007-02-24 13:39:08 -05003067 * helper function to insert a new root level in the tree.
3068 * A new node is allocated, and a single item is inserted to
3069 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003070 *
3071 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003072 */
Chris Masond3977122009-01-05 21:25:51 -05003073static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003074 struct btrfs_root *root,
3075 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003076{
Chris Mason7bb86312007-12-11 09:25:06 -05003077 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003078 struct extent_buffer *lower;
3079 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003080 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003081 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003082
3083 BUG_ON(path->nodes[level]);
3084 BUG_ON(path->nodes[level-1] != root->node);
3085
Chris Mason7bb86312007-12-11 09:25:06 -05003086 lower = path->nodes[level-1];
3087 if (level == 1)
3088 btrfs_item_key(lower, &lower_key, 0);
3089 else
3090 btrfs_node_key(lower, &lower_key, 0);
3091
Zheng Yan31840ae2008-09-23 13:14:14 -04003092 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003093 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003094 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003095 if (IS_ERR(c))
3096 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003097
Yan, Zhengf0486c62010-05-16 10:46:25 -04003098 root_add_used(root, root->nodesize);
3099
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003100 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003101 btrfs_set_header_nritems(c, 1);
3102 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003103 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003104 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003106 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003107
Chris Mason5f39d392007-10-15 16:14:19 -04003108 write_extent_buffer(c, root->fs_info->fsid,
3109 (unsigned long)btrfs_header_fsid(c),
3110 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003111
3112 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3113 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3114 BTRFS_UUID_SIZE);
3115
Chris Mason5f39d392007-10-15 16:14:19 -04003116 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003117 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003118 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003119 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003120
3121 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003122
3123 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003124
Chris Mason925baed2008-06-25 16:01:30 -04003125 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02003126 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04003127 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003128
3129 /* the super has an extra ref to root->node */
3130 free_extent_buffer(old);
3131
Chris Mason0b86a832008-03-24 15:01:56 -04003132 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003133 extent_buffer_get(c);
3134 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003135 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003136 path->slots[level] = 0;
3137 return 0;
3138}
3139
Chris Mason74123bd2007-02-02 11:05:29 -05003140/*
3141 * worker function to insert a single pointer in a node.
3142 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003143 *
Chris Mason74123bd2007-02-02 11:05:29 -05003144 * slot and level indicate where you want the key to go, and
3145 * blocknr is the block the key points to.
3146 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003147static void insert_ptr(struct btrfs_trans_handle *trans,
3148 struct btrfs_root *root, struct btrfs_path *path,
3149 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003150 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003151{
Chris Mason5f39d392007-10-15 16:14:19 -04003152 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003153 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003154 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003155
3156 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003157 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003158 lower = path->nodes[level];
3159 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003160 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003161 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003162 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003163 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003164 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3165 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003166 memmove_extent_buffer(lower,
3167 btrfs_node_key_ptr_offset(slot + 1),
3168 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003169 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003170 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003171 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003172 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3173 MOD_LOG_KEY_ADD);
3174 BUG_ON(ret < 0);
3175 }
Chris Mason5f39d392007-10-15 16:14:19 -04003176 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003177 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003178 WARN_ON(trans->transid == 0);
3179 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003180 btrfs_set_header_nritems(lower, nritems + 1);
3181 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003182}
3183
Chris Mason97571fd2007-02-24 13:39:08 -05003184/*
3185 * split the node at the specified level in path in two.
3186 * The path is corrected to point to the appropriate node after the split
3187 *
3188 * Before splitting this tries to make some room in the node by pushing
3189 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003190 *
3191 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003192 */
Chris Masone02119d2008-09-05 16:13:11 -04003193static noinline int split_node(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root,
3195 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003196{
Chris Mason5f39d392007-10-15 16:14:19 -04003197 struct extent_buffer *c;
3198 struct extent_buffer *split;
3199 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003200 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003201 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003202 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003203
Chris Mason5f39d392007-10-15 16:14:19 -04003204 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003205 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003206 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003207 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003208 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003209 if (ret)
3210 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003211 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003212 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003213 c = path->nodes[level];
3214 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003215 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003216 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003217 if (ret < 0)
3218 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003219 }
Chris Masone66f7092007-04-20 13:16:02 -04003220
Chris Mason5f39d392007-10-15 16:14:19 -04003221 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003222 mid = (c_nritems + 1) / 2;
3223 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003225 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003226 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003227 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003228 if (IS_ERR(split))
3229 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003230
Yan, Zhengf0486c62010-05-16 10:46:25 -04003231 root_add_used(root, root->nodesize);
3232
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003233 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003234 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003235 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003236 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003238 btrfs_set_header_owner(split, root->root_key.objectid);
3239 write_extent_buffer(split, root->fs_info->fsid,
3240 (unsigned long)btrfs_header_fsid(split),
3241 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003242 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3243 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3244 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003245
Jan Schmidtf2304752012-05-26 11:43:17 +02003246 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003247 copy_extent_buffer(split, c,
3248 btrfs_node_key_ptr_offset(0),
3249 btrfs_node_key_ptr_offset(mid),
3250 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3251 btrfs_set_header_nritems(split, c_nritems - mid);
3252 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003253 ret = 0;
3254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 btrfs_mark_buffer_dirty(c);
3256 btrfs_mark_buffer_dirty(split);
3257
Jeff Mahoney143bede2012-03-01 14:56:26 +01003258 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003259 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003260
Chris Mason5de08d72007-02-24 06:24:44 -05003261 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003262 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003263 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003264 free_extent_buffer(c);
3265 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003266 path->slots[level + 1] += 1;
3267 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003268 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003269 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003270 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003271 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272}
3273
Chris Mason74123bd2007-02-02 11:05:29 -05003274/*
3275 * how many bytes are required to store the items in a leaf. start
3276 * and nr indicate which items in the leaf to check. This totals up the
3277 * space used both by the item structs and the item data
3278 */
Chris Mason5f39d392007-10-15 16:14:19 -04003279static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003280{
3281 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003282 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003283 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003284
3285 if (!nr)
3286 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003287 data_len = btrfs_item_end_nr(l, start);
3288 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003289 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003290 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003291 return data_len;
3292}
3293
Chris Mason74123bd2007-02-02 11:05:29 -05003294/*
Chris Masond4dbff92007-04-04 14:08:15 -04003295 * The space between the end of the leaf items and
3296 * the start of the leaf data. IOW, how much room
3297 * the leaf has left for both items and data
3298 */
Chris Masond3977122009-01-05 21:25:51 -05003299noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003300 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003301{
Chris Mason5f39d392007-10-15 16:14:19 -04003302 int nritems = btrfs_header_nritems(leaf);
3303 int ret;
3304 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3305 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003306 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3307 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003308 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003309 leaf_space_used(leaf, 0, nritems), nritems);
3310 }
3311 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003312}
3313
Chris Mason99d8f832010-07-07 10:51:48 -04003314/*
3315 * min slot controls the lowest index we're willing to push to the
3316 * right. We'll push up to and including min_slot, but no lower
3317 */
Chris Mason44871b12009-03-13 10:04:31 -04003318static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3319 struct btrfs_root *root,
3320 struct btrfs_path *path,
3321 int data_size, int empty,
3322 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003323 int free_space, u32 left_nritems,
3324 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003325{
Chris Mason5f39d392007-10-15 16:14:19 -04003326 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003327 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003328 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003329 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003330 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003331 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003332 int push_space = 0;
3333 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003334 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003335 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003336 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003337 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003338 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003339
Chris Masoncfed81a2012-03-03 07:40:03 -05003340 btrfs_init_map_token(&token);
3341
Chris Mason34a38212007-11-07 13:31:03 -05003342 if (empty)
3343 nr = 0;
3344 else
Chris Mason99d8f832010-07-07 10:51:48 -04003345 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003346
Zheng Yan31840ae2008-09-23 13:14:14 -04003347 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003348 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003349
Chris Mason44871b12009-03-13 10:04:31 -04003350 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003351 i = left_nritems - 1;
3352 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003353 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003354
Zheng Yan31840ae2008-09-23 13:14:14 -04003355 if (!empty && push_items > 0) {
3356 if (path->slots[0] > i)
3357 break;
3358 if (path->slots[0] == i) {
3359 int space = btrfs_leaf_free_space(root, left);
3360 if (space + push_space * 2 > free_space)
3361 break;
3362 }
3363 }
3364
Chris Mason00ec4c52007-02-24 12:47:20 -05003365 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003366 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003367
Chris Masondb945352007-10-15 16:15:53 -04003368 this_item_size = btrfs_item_size(left, item);
3369 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003370 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003371
Chris Mason00ec4c52007-02-24 12:47:20 -05003372 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003373 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003374 if (i == 0)
3375 break;
3376 i--;
Chris Masondb945352007-10-15 16:15:53 -04003377 }
Chris Mason5f39d392007-10-15 16:14:19 -04003378
Chris Mason925baed2008-06-25 16:01:30 -04003379 if (push_items == 0)
3380 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003381
Chris Mason34a38212007-11-07 13:31:03 -05003382 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003383 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003384
Chris Mason00ec4c52007-02-24 12:47:20 -05003385 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003386 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003387
Chris Mason5f39d392007-10-15 16:14:19 -04003388 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003389 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003390
Chris Mason00ec4c52007-02-24 12:47:20 -05003391 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003392 data_end = leaf_data_end(root, right);
3393 memmove_extent_buffer(right,
3394 btrfs_leaf_data(right) + data_end - push_space,
3395 btrfs_leaf_data(right) + data_end,
3396 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3397
Chris Mason00ec4c52007-02-24 12:47:20 -05003398 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003399 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003400 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3401 btrfs_leaf_data(left) + leaf_data_end(root, left),
3402 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003403
3404 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3405 btrfs_item_nr_offset(0),
3406 right_nritems * sizeof(struct btrfs_item));
3407
Chris Mason00ec4c52007-02-24 12:47:20 -05003408 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003409 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3410 btrfs_item_nr_offset(left_nritems - push_items),
3411 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003412
3413 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003414 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003416 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003417 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003418 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003419 push_space -= btrfs_token_item_size(right, item, &token);
3420 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003421 }
3422
Chris Mason7518a232007-03-12 12:01:18 -04003423 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003424 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003425
Chris Mason34a38212007-11-07 13:31:03 -05003426 if (left_nritems)
3427 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003428 else
3429 clean_tree_block(trans, root, left);
3430
Chris Mason5f39d392007-10-15 16:14:19 -04003431 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003432
Chris Mason5f39d392007-10-15 16:14:19 -04003433 btrfs_item_key(right, &disk_key, 0);
3434 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003435 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003436
Chris Mason00ec4c52007-02-24 12:47:20 -05003437 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003438 if (path->slots[0] >= left_nritems) {
3439 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003440 if (btrfs_header_nritems(path->nodes[0]) == 0)
3441 clean_tree_block(trans, root, path->nodes[0]);
3442 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003443 free_extent_buffer(path->nodes[0]);
3444 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003445 path->slots[1] += 1;
3446 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003447 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003448 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003449 }
3450 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003451
3452out_unlock:
3453 btrfs_tree_unlock(right);
3454 free_extent_buffer(right);
3455 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003456}
Chris Mason925baed2008-06-25 16:01:30 -04003457
Chris Mason00ec4c52007-02-24 12:47:20 -05003458/*
Chris Mason44871b12009-03-13 10:04:31 -04003459 * push some data in the path leaf to the right, trying to free up at
3460 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3461 *
3462 * returns 1 if the push failed because the other node didn't have enough
3463 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003464 *
3465 * this will push starting from min_slot to the end of the leaf. It won't
3466 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003467 */
3468static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003469 *root, struct btrfs_path *path,
3470 int min_data_size, int data_size,
3471 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003472{
3473 struct extent_buffer *left = path->nodes[0];
3474 struct extent_buffer *right;
3475 struct extent_buffer *upper;
3476 int slot;
3477 int free_space;
3478 u32 left_nritems;
3479 int ret;
3480
3481 if (!path->nodes[1])
3482 return 1;
3483
3484 slot = path->slots[1];
3485 upper = path->nodes[1];
3486 if (slot >= btrfs_header_nritems(upper) - 1)
3487 return 1;
3488
3489 btrfs_assert_tree_locked(path->nodes[1]);
3490
3491 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003492 if (right == NULL)
3493 return 1;
3494
Chris Mason44871b12009-03-13 10:04:31 -04003495 btrfs_tree_lock(right);
3496 btrfs_set_lock_blocking(right);
3497
3498 free_space = btrfs_leaf_free_space(root, right);
3499 if (free_space < data_size)
3500 goto out_unlock;
3501
3502 /* cow and double check */
3503 ret = btrfs_cow_block(trans, root, right, upper,
3504 slot + 1, &right);
3505 if (ret)
3506 goto out_unlock;
3507
3508 free_space = btrfs_leaf_free_space(root, right);
3509 if (free_space < data_size)
3510 goto out_unlock;
3511
3512 left_nritems = btrfs_header_nritems(left);
3513 if (left_nritems == 0)
3514 goto out_unlock;
3515
Chris Mason99d8f832010-07-07 10:51:48 -04003516 return __push_leaf_right(trans, root, path, min_data_size, empty,
3517 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003518out_unlock:
3519 btrfs_tree_unlock(right);
3520 free_extent_buffer(right);
3521 return 1;
3522}
3523
3524/*
Chris Mason74123bd2007-02-02 11:05:29 -05003525 * push some data in the path leaf to the left, trying to free up at
3526 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003527 *
3528 * max_slot can put a limit on how far into the leaf we'll push items. The
3529 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3530 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003531 */
Chris Mason44871b12009-03-13 10:04:31 -04003532static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3533 struct btrfs_root *root,
3534 struct btrfs_path *path, int data_size,
3535 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003536 int free_space, u32 right_nritems,
3537 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538{
Chris Mason5f39d392007-10-15 16:14:19 -04003539 struct btrfs_disk_key disk_key;
3540 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003542 int push_space = 0;
3543 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003544 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003545 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003546 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003547 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003548 u32 this_item_size;
3549 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003550 struct btrfs_map_token token;
3551
3552 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553
Chris Mason34a38212007-11-07 13:31:03 -05003554 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003555 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003556 else
Chris Mason99d8f832010-07-07 10:51:48 -04003557 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003558
3559 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003560 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003561
Zheng Yan31840ae2008-09-23 13:14:14 -04003562 if (!empty && push_items > 0) {
3563 if (path->slots[0] < i)
3564 break;
3565 if (path->slots[0] == i) {
3566 int space = btrfs_leaf_free_space(root, right);
3567 if (space + push_space * 2 > free_space)
3568 break;
3569 }
3570 }
3571
Chris Masonbe0e5c02007-01-26 15:51:26 -05003572 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003573 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003574
3575 this_item_size = btrfs_item_size(right, item);
3576 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003577 break;
Chris Masondb945352007-10-15 16:15:53 -04003578
Chris Masonbe0e5c02007-01-26 15:51:26 -05003579 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003580 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003581 }
Chris Masondb945352007-10-15 16:15:53 -04003582
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003584 ret = 1;
3585 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586 }
Chris Mason34a38212007-11-07 13:31:03 -05003587 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003588 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003589
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003591 copy_extent_buffer(left, right,
3592 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3593 btrfs_item_nr_offset(0),
3594 push_items * sizeof(struct btrfs_item));
3595
Chris Mason123abc82007-03-14 14:14:43 -04003596 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003597 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003598
3599 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003600 leaf_data_end(root, left) - push_space,
3601 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003602 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003603 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003604 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003605 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003606
Chris Masondb945352007-10-15 16:15:53 -04003607 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003608 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003609 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003610
Chris Mason5f39d392007-10-15 16:14:19 -04003611 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003612
Chris Masoncfed81a2012-03-03 07:40:03 -05003613 ioff = btrfs_token_item_offset(left, item, &token);
3614 btrfs_set_token_item_offset(left, item,
3615 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3616 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003617 }
Chris Mason5f39d392007-10-15 16:14:19 -04003618 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003619
3620 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003621 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003622 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3623 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003624 WARN_ON(1);
3625 }
Chris Mason5f39d392007-10-15 16:14:19 -04003626
Chris Mason34a38212007-11-07 13:31:03 -05003627 if (push_items < right_nritems) {
3628 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3629 leaf_data_end(root, right);
3630 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3631 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3632 btrfs_leaf_data(right) +
3633 leaf_data_end(root, right), push_space);
3634
3635 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003636 btrfs_item_nr_offset(push_items),
3637 (btrfs_header_nritems(right) - push_items) *
3638 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003639 }
Yaneef1c492007-11-26 10:58:13 -05003640 right_nritems -= push_items;
3641 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003642 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003643 for (i = 0; i < right_nritems; i++) {
3644 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003645
Chris Masoncfed81a2012-03-03 07:40:03 -05003646 push_space = push_space - btrfs_token_item_size(right,
3647 item, &token);
3648 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003649 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003650
Chris Mason5f39d392007-10-15 16:14:19 -04003651 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003652 if (right_nritems)
3653 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003654 else
3655 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003656
Chris Mason5f39d392007-10-15 16:14:19 -04003657 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003658 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003659
3660 /* then fixup the leaf pointer in the path */
3661 if (path->slots[0] < push_items) {
3662 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003663 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003664 free_extent_buffer(path->nodes[0]);
3665 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003666 path->slots[1] -= 1;
3667 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003668 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003669 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670 path->slots[0] -= push_items;
3671 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003672 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003673 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003674out:
3675 btrfs_tree_unlock(left);
3676 free_extent_buffer(left);
3677 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678}
3679
Chris Mason74123bd2007-02-02 11:05:29 -05003680/*
Chris Mason44871b12009-03-13 10:04:31 -04003681 * push some data in the path leaf to the left, trying to free up at
3682 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003683 *
3684 * max_slot can put a limit on how far into the leaf we'll push items. The
3685 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3686 * items
Chris Mason44871b12009-03-13 10:04:31 -04003687 */
3688static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003689 *root, struct btrfs_path *path, int min_data_size,
3690 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003691{
3692 struct extent_buffer *right = path->nodes[0];
3693 struct extent_buffer *left;
3694 int slot;
3695 int free_space;
3696 u32 right_nritems;
3697 int ret = 0;
3698
3699 slot = path->slots[1];
3700 if (slot == 0)
3701 return 1;
3702 if (!path->nodes[1])
3703 return 1;
3704
3705 right_nritems = btrfs_header_nritems(right);
3706 if (right_nritems == 0)
3707 return 1;
3708
3709 btrfs_assert_tree_locked(path->nodes[1]);
3710
3711 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003712 if (left == NULL)
3713 return 1;
3714
Chris Mason44871b12009-03-13 10:04:31 -04003715 btrfs_tree_lock(left);
3716 btrfs_set_lock_blocking(left);
3717
3718 free_space = btrfs_leaf_free_space(root, left);
3719 if (free_space < data_size) {
3720 ret = 1;
3721 goto out;
3722 }
3723
3724 /* cow and double check */
3725 ret = btrfs_cow_block(trans, root, left,
3726 path->nodes[1], slot - 1, &left);
3727 if (ret) {
3728 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003729 if (ret == -ENOSPC)
3730 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003731 goto out;
3732 }
3733
3734 free_space = btrfs_leaf_free_space(root, left);
3735 if (free_space < data_size) {
3736 ret = 1;
3737 goto out;
3738 }
3739
Chris Mason99d8f832010-07-07 10:51:48 -04003740 return __push_leaf_left(trans, root, path, min_data_size,
3741 empty, left, free_space, right_nritems,
3742 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003743out:
3744 btrfs_tree_unlock(left);
3745 free_extent_buffer(left);
3746 return ret;
3747}
3748
3749/*
Chris Mason74123bd2007-02-02 11:05:29 -05003750 * split the path's leaf in two, making sure there is at least data_size
3751 * available for the resulting leaf level of the path.
3752 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003753static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3754 struct btrfs_root *root,
3755 struct btrfs_path *path,
3756 struct extent_buffer *l,
3757 struct extent_buffer *right,
3758 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003759{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003760 int data_copy_size;
3761 int rt_data_off;
3762 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003763 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003764 struct btrfs_map_token token;
3765
3766 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003767
Chris Mason5f39d392007-10-15 16:14:19 -04003768 nritems = nritems - mid;
3769 btrfs_set_header_nritems(right, nritems);
3770 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3771
3772 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3773 btrfs_item_nr_offset(mid),
3774 nritems * sizeof(struct btrfs_item));
3775
3776 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003777 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3778 data_copy_size, btrfs_leaf_data(l) +
3779 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003780
Chris Mason5f39d392007-10-15 16:14:19 -04003781 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3782 btrfs_item_end_nr(l, mid);
3783
3784 for (i = 0; i < nritems; i++) {
3785 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003786 u32 ioff;
3787
Chris Masoncfed81a2012-03-03 07:40:03 -05003788 ioff = btrfs_token_item_offset(right, item, &token);
3789 btrfs_set_token_item_offset(right, item,
3790 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003791 }
Chris Mason74123bd2007-02-02 11:05:29 -05003792
Chris Mason5f39d392007-10-15 16:14:19 -04003793 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003794 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003795 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003796 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003797
3798 btrfs_mark_buffer_dirty(right);
3799 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003800 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003801
Chris Masonbe0e5c02007-01-26 15:51:26 -05003802 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003803 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003804 free_extent_buffer(path->nodes[0]);
3805 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003806 path->slots[0] -= mid;
3807 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003808 } else {
3809 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003810 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003811 }
Chris Mason5f39d392007-10-15 16:14:19 -04003812
Chris Masoneb60cea2007-02-02 09:18:22 -05003813 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003814}
3815
3816/*
Chris Mason99d8f832010-07-07 10:51:48 -04003817 * double splits happen when we need to insert a big item in the middle
3818 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3819 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3820 * A B C
3821 *
3822 * We avoid this by trying to push the items on either side of our target
3823 * into the adjacent leaves. If all goes well we can avoid the double split
3824 * completely.
3825 */
3826static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3827 struct btrfs_root *root,
3828 struct btrfs_path *path,
3829 int data_size)
3830{
3831 int ret;
3832 int progress = 0;
3833 int slot;
3834 u32 nritems;
3835
3836 slot = path->slots[0];
3837
3838 /*
3839 * try to push all the items after our slot into the
3840 * right leaf
3841 */
3842 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3843 if (ret < 0)
3844 return ret;
3845
3846 if (ret == 0)
3847 progress++;
3848
3849 nritems = btrfs_header_nritems(path->nodes[0]);
3850 /*
3851 * our goal is to get our slot at the start or end of a leaf. If
3852 * we've done so we're done
3853 */
3854 if (path->slots[0] == 0 || path->slots[0] == nritems)
3855 return 0;
3856
3857 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3858 return 0;
3859
3860 /* try to push all the items before our slot into the next leaf */
3861 slot = path->slots[0];
3862 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3863 if (ret < 0)
3864 return ret;
3865
3866 if (ret == 0)
3867 progress++;
3868
3869 if (progress)
3870 return 0;
3871 return 1;
3872}
3873
3874/*
Chris Mason44871b12009-03-13 10:04:31 -04003875 * split the path's leaf in two, making sure there is at least data_size
3876 * available for the resulting leaf level of the path.
3877 *
3878 * returns 0 if all went well and < 0 on failure.
3879 */
3880static noinline int split_leaf(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *root,
3882 struct btrfs_key *ins_key,
3883 struct btrfs_path *path, int data_size,
3884 int extend)
3885{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003886 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003887 struct extent_buffer *l;
3888 u32 nritems;
3889 int mid;
3890 int slot;
3891 struct extent_buffer *right;
3892 int ret = 0;
3893 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003894 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003895 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003896 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003897
Yan, Zhenga5719522009-09-24 09:17:31 -04003898 l = path->nodes[0];
3899 slot = path->slots[0];
3900 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3901 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3902 return -EOVERFLOW;
3903
Chris Mason44871b12009-03-13 10:04:31 -04003904 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003905 if (data_size) {
3906 wret = push_leaf_right(trans, root, path, data_size,
3907 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003908 if (wret < 0)
3909 return wret;
3910 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003911 wret = push_leaf_left(trans, root, path, data_size,
3912 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003913 if (wret < 0)
3914 return wret;
3915 }
3916 l = path->nodes[0];
3917
3918 /* did the pushes work? */
3919 if (btrfs_leaf_free_space(root, l) >= data_size)
3920 return 0;
3921 }
3922
3923 if (!path->nodes[1]) {
3924 ret = insert_new_root(trans, root, path, 1);
3925 if (ret)
3926 return ret;
3927 }
3928again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003929 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003930 l = path->nodes[0];
3931 slot = path->slots[0];
3932 nritems = btrfs_header_nritems(l);
3933 mid = (nritems + 1) / 2;
3934
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 if (mid <= slot) {
3936 if (nritems == 1 ||
3937 leaf_space_used(l, mid, nritems - mid) + data_size >
3938 BTRFS_LEAF_DATA_SIZE(root)) {
3939 if (slot >= nritems) {
3940 split = 0;
3941 } else {
3942 mid = slot;
3943 if (mid != nritems &&
3944 leaf_space_used(l, mid, nritems - mid) +
3945 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003946 if (data_size && !tried_avoid_double)
3947 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003948 split = 2;
3949 }
3950 }
3951 }
3952 } else {
3953 if (leaf_space_used(l, 0, mid) + data_size >
3954 BTRFS_LEAF_DATA_SIZE(root)) {
3955 if (!extend && data_size && slot == 0) {
3956 split = 0;
3957 } else if ((extend || !data_size) && slot == 0) {
3958 mid = 1;
3959 } else {
3960 mid = slot;
3961 if (mid != nritems &&
3962 leaf_space_used(l, mid, nritems - mid) +
3963 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003964 if (data_size && !tried_avoid_double)
3965 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003966 split = 2 ;
3967 }
3968 }
3969 }
3970 }
3971
3972 if (split == 0)
3973 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3974 else
3975 btrfs_item_key(l, &disk_key, mid);
3976
3977 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003978 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003979 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003980 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003981 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003982
3983 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003984
3985 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3986 btrfs_set_header_bytenr(right, right->start);
3987 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003989 btrfs_set_header_owner(right, root->root_key.objectid);
3990 btrfs_set_header_level(right, 0);
3991 write_extent_buffer(right, root->fs_info->fsid,
3992 (unsigned long)btrfs_header_fsid(right),
3993 BTRFS_FSID_SIZE);
3994
3995 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3996 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3997 BTRFS_UUID_SIZE);
3998
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003999 if (split == 0) {
4000 if (mid <= slot) {
4001 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004002 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004003 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004004 btrfs_tree_unlock(path->nodes[0]);
4005 free_extent_buffer(path->nodes[0]);
4006 path->nodes[0] = right;
4007 path->slots[0] = 0;
4008 path->slots[1] += 1;
4009 } else {
4010 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004011 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004012 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004013 btrfs_tree_unlock(path->nodes[0]);
4014 free_extent_buffer(path->nodes[0]);
4015 path->nodes[0] = right;
4016 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004017 if (path->slots[1] == 0)
4018 fixup_low_keys(trans, root, path,
4019 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004020 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004021 btrfs_mark_buffer_dirty(right);
4022 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004023 }
4024
Jeff Mahoney143bede2012-03-01 14:56:26 +01004025 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004026
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004027 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004028 BUG_ON(num_doubles != 0);
4029 num_doubles++;
4030 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004031 }
Chris Mason44871b12009-03-13 10:04:31 -04004032
Jeff Mahoney143bede2012-03-01 14:56:26 +01004033 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004034
4035push_for_double:
4036 push_for_double_split(trans, root, path, data_size);
4037 tried_avoid_double = 1;
4038 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4039 return 0;
4040 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004041}
4042
Yan, Zhengad48fd752009-11-12 09:33:58 +00004043static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4044 struct btrfs_root *root,
4045 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004046{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004047 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004048 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004049 struct btrfs_file_extent_item *fi;
4050 u64 extent_len = 0;
4051 u32 item_size;
4052 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004053
4054 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004055 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4056
4057 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4058 key.type != BTRFS_EXTENT_CSUM_KEY);
4059
4060 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4061 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004062
4063 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004064 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4065 fi = btrfs_item_ptr(leaf, path->slots[0],
4066 struct btrfs_file_extent_item);
4067 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004069 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004070
Chris Mason459931e2008-12-10 09:10:46 -05004071 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004072 path->search_for_split = 1;
4073 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004074 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004075 if (ret < 0)
4076 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004077
Yan, Zhengad48fd752009-11-12 09:33:58 +00004078 ret = -EAGAIN;
4079 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004080 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004081 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4082 goto err;
4083
Chris Mason109f6ae2010-04-02 09:20:18 -04004084 /* the leaf has changed, it now has room. return now */
4085 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4086 goto err;
4087
Yan, Zhengad48fd752009-11-12 09:33:58 +00004088 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4089 fi = btrfs_item_ptr(leaf, path->slots[0],
4090 struct btrfs_file_extent_item);
4091 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4092 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004093 }
4094
Chris Masonb9473432009-03-13 11:00:37 -04004095 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004096 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004097 if (ret)
4098 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004099
Yan, Zhengad48fd752009-11-12 09:33:58 +00004100 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004101 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004102 return 0;
4103err:
4104 path->keep_locks = 0;
4105 return ret;
4106}
4107
4108static noinline int split_item(struct btrfs_trans_handle *trans,
4109 struct btrfs_root *root,
4110 struct btrfs_path *path,
4111 struct btrfs_key *new_key,
4112 unsigned long split_offset)
4113{
4114 struct extent_buffer *leaf;
4115 struct btrfs_item *item;
4116 struct btrfs_item *new_item;
4117 int slot;
4118 char *buf;
4119 u32 nritems;
4120 u32 item_size;
4121 u32 orig_offset;
4122 struct btrfs_disk_key disk_key;
4123
Chris Masonb9473432009-03-13 11:00:37 -04004124 leaf = path->nodes[0];
4125 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4126
Chris Masonb4ce94d2009-02-04 09:25:08 -05004127 btrfs_set_path_blocking(path);
4128
Chris Mason459931e2008-12-10 09:10:46 -05004129 item = btrfs_item_nr(leaf, path->slots[0]);
4130 orig_offset = btrfs_item_offset(leaf, item);
4131 item_size = btrfs_item_size(leaf, item);
4132
Chris Mason459931e2008-12-10 09:10:46 -05004133 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004134 if (!buf)
4135 return -ENOMEM;
4136
Chris Mason459931e2008-12-10 09:10:46 -05004137 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4138 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004139
Chris Mason459931e2008-12-10 09:10:46 -05004140 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004141 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004142 if (slot != nritems) {
4143 /* shift the items */
4144 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004145 btrfs_item_nr_offset(slot),
4146 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004147 }
4148
4149 btrfs_cpu_key_to_disk(&disk_key, new_key);
4150 btrfs_set_item_key(leaf, &disk_key, slot);
4151
4152 new_item = btrfs_item_nr(leaf, slot);
4153
4154 btrfs_set_item_offset(leaf, new_item, orig_offset);
4155 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4156
4157 btrfs_set_item_offset(leaf, item,
4158 orig_offset + item_size - split_offset);
4159 btrfs_set_item_size(leaf, item, split_offset);
4160
4161 btrfs_set_header_nritems(leaf, nritems + 1);
4162
4163 /* write the data for the start of the original item */
4164 write_extent_buffer(leaf, buf,
4165 btrfs_item_ptr_offset(leaf, path->slots[0]),
4166 split_offset);
4167
4168 /* write the data for the new item */
4169 write_extent_buffer(leaf, buf + split_offset,
4170 btrfs_item_ptr_offset(leaf, slot),
4171 item_size - split_offset);
4172 btrfs_mark_buffer_dirty(leaf);
4173
Yan, Zhengad48fd752009-11-12 09:33:58 +00004174 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004175 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004176 return 0;
4177}
4178
4179/*
4180 * This function splits a single item into two items,
4181 * giving 'new_key' to the new item and splitting the
4182 * old one at split_offset (from the start of the item).
4183 *
4184 * The path may be released by this operation. After
4185 * the split, the path is pointing to the old item. The
4186 * new item is going to be in the same node as the old one.
4187 *
4188 * Note, the item being split must be smaller enough to live alone on
4189 * a tree block with room for one extra struct btrfs_item
4190 *
4191 * This allows us to split the item in place, keeping a lock on the
4192 * leaf the entire time.
4193 */
4194int btrfs_split_item(struct btrfs_trans_handle *trans,
4195 struct btrfs_root *root,
4196 struct btrfs_path *path,
4197 struct btrfs_key *new_key,
4198 unsigned long split_offset)
4199{
4200 int ret;
4201 ret = setup_leaf_for_split(trans, root, path,
4202 sizeof(struct btrfs_item));
4203 if (ret)
4204 return ret;
4205
4206 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004207 return ret;
4208}
4209
4210/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004211 * This function duplicate a item, giving 'new_key' to the new item.
4212 * It guarantees both items live in the same tree leaf and the new item
4213 * is contiguous with the original item.
4214 *
4215 * This allows us to split file extent in place, keeping a lock on the
4216 * leaf the entire time.
4217 */
4218int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4219 struct btrfs_root *root,
4220 struct btrfs_path *path,
4221 struct btrfs_key *new_key)
4222{
4223 struct extent_buffer *leaf;
4224 int ret;
4225 u32 item_size;
4226
4227 leaf = path->nodes[0];
4228 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4229 ret = setup_leaf_for_split(trans, root, path,
4230 item_size + sizeof(struct btrfs_item));
4231 if (ret)
4232 return ret;
4233
4234 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004235 setup_items_for_insert(trans, root, path, new_key, &item_size,
4236 item_size, item_size +
4237 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004238 leaf = path->nodes[0];
4239 memcpy_extent_buffer(leaf,
4240 btrfs_item_ptr_offset(leaf, path->slots[0]),
4241 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4242 item_size);
4243 return 0;
4244}
4245
4246/*
Chris Masond352ac62008-09-29 15:18:18 -04004247 * make the item pointed to by the path smaller. new_size indicates
4248 * how small to make it, and from_end tells us if we just chop bytes
4249 * off the end of the item or if we shift the item to chop bytes off
4250 * the front.
4251 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004252void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4253 struct btrfs_root *root,
4254 struct btrfs_path *path,
4255 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004256{
Chris Masonb18c6682007-04-17 13:26:50 -04004257 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004258 struct extent_buffer *leaf;
4259 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004260 u32 nritems;
4261 unsigned int data_end;
4262 unsigned int old_data_start;
4263 unsigned int old_size;
4264 unsigned int size_diff;
4265 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004266 struct btrfs_map_token token;
4267
4268 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004269
Chris Mason5f39d392007-10-15 16:14:19 -04004270 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004271 slot = path->slots[0];
4272
4273 old_size = btrfs_item_size_nr(leaf, slot);
4274 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004275 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004276
Chris Mason5f39d392007-10-15 16:14:19 -04004277 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004278 data_end = leaf_data_end(root, leaf);
4279
Chris Mason5f39d392007-10-15 16:14:19 -04004280 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004281
Chris Masonb18c6682007-04-17 13:26:50 -04004282 size_diff = old_size - new_size;
4283
4284 BUG_ON(slot < 0);
4285 BUG_ON(slot >= nritems);
4286
4287 /*
4288 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4289 */
4290 /* first correct the data pointers */
4291 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004292 u32 ioff;
4293 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004294
Chris Masoncfed81a2012-03-03 07:40:03 -05004295 ioff = btrfs_token_item_offset(leaf, item, &token);
4296 btrfs_set_token_item_offset(leaf, item,
4297 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004298 }
Chris Masondb945352007-10-15 16:15:53 -04004299
Chris Masonb18c6682007-04-17 13:26:50 -04004300 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004301 if (from_end) {
4302 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4303 data_end + size_diff, btrfs_leaf_data(leaf) +
4304 data_end, old_data_start + new_size - data_end);
4305 } else {
4306 struct btrfs_disk_key disk_key;
4307 u64 offset;
4308
4309 btrfs_item_key(leaf, &disk_key, slot);
4310
4311 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4312 unsigned long ptr;
4313 struct btrfs_file_extent_item *fi;
4314
4315 fi = btrfs_item_ptr(leaf, slot,
4316 struct btrfs_file_extent_item);
4317 fi = (struct btrfs_file_extent_item *)(
4318 (unsigned long)fi - size_diff);
4319
4320 if (btrfs_file_extent_type(leaf, fi) ==
4321 BTRFS_FILE_EXTENT_INLINE) {
4322 ptr = btrfs_item_ptr_offset(leaf, slot);
4323 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004324 (unsigned long)fi,
4325 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004326 disk_bytenr));
4327 }
4328 }
4329
4330 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4331 data_end + size_diff, btrfs_leaf_data(leaf) +
4332 data_end, old_data_start - data_end);
4333
4334 offset = btrfs_disk_key_offset(&disk_key);
4335 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4336 btrfs_set_item_key(leaf, &disk_key, slot);
4337 if (slot == 0)
4338 fixup_low_keys(trans, root, path, &disk_key, 1);
4339 }
Chris Mason5f39d392007-10-15 16:14:19 -04004340
4341 item = btrfs_item_nr(leaf, slot);
4342 btrfs_set_item_size(leaf, item, new_size);
4343 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004344
Chris Mason5f39d392007-10-15 16:14:19 -04004345 if (btrfs_leaf_free_space(root, leaf) < 0) {
4346 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004347 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004348 }
Chris Masonb18c6682007-04-17 13:26:50 -04004349}
4350
Chris Masond352ac62008-09-29 15:18:18 -04004351/*
4352 * make the item pointed to by the path bigger, data_size is the new size.
4353 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004354void btrfs_extend_item(struct btrfs_trans_handle *trans,
4355 struct btrfs_root *root, struct btrfs_path *path,
4356 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004357{
Chris Mason6567e832007-04-16 09:22:45 -04004358 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004359 struct extent_buffer *leaf;
4360 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004361 u32 nritems;
4362 unsigned int data_end;
4363 unsigned int old_data;
4364 unsigned int old_size;
4365 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004366 struct btrfs_map_token token;
4367
4368 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004369
Chris Mason5f39d392007-10-15 16:14:19 -04004370 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004371
Chris Mason5f39d392007-10-15 16:14:19 -04004372 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004373 data_end = leaf_data_end(root, leaf);
4374
Chris Mason5f39d392007-10-15 16:14:19 -04004375 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4376 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004377 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004378 }
Chris Mason6567e832007-04-16 09:22:45 -04004379 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004380 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004381
4382 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004383 if (slot >= nritems) {
4384 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004385 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4386 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004387 BUG_ON(1);
4388 }
Chris Mason6567e832007-04-16 09:22:45 -04004389
4390 /*
4391 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4392 */
4393 /* first correct the data pointers */
4394 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004395 u32 ioff;
4396 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004397
Chris Masoncfed81a2012-03-03 07:40:03 -05004398 ioff = btrfs_token_item_offset(leaf, item, &token);
4399 btrfs_set_token_item_offset(leaf, item,
4400 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004401 }
Chris Mason5f39d392007-10-15 16:14:19 -04004402
Chris Mason6567e832007-04-16 09:22:45 -04004403 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004404 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004405 data_end - data_size, btrfs_leaf_data(leaf) +
4406 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004407
Chris Mason6567e832007-04-16 09:22:45 -04004408 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004409 old_size = btrfs_item_size_nr(leaf, slot);
4410 item = btrfs_item_nr(leaf, slot);
4411 btrfs_set_item_size(leaf, item, old_size + data_size);
4412 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004413
Chris Mason5f39d392007-10-15 16:14:19 -04004414 if (btrfs_leaf_free_space(root, leaf) < 0) {
4415 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004416 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004417 }
Chris Mason6567e832007-04-16 09:22:45 -04004418}
4419
Chris Mason74123bd2007-02-02 11:05:29 -05004420/*
Chris Mason44871b12009-03-13 10:04:31 -04004421 * this is a helper for btrfs_insert_empty_items, the main goal here is
4422 * to save stack depth by doing the bulk of the work in a function
4423 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004424 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004425void setup_items_for_insert(struct btrfs_trans_handle *trans,
4426 struct btrfs_root *root, struct btrfs_path *path,
4427 struct btrfs_key *cpu_key, u32 *data_size,
4428 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004429{
Chris Mason5f39d392007-10-15 16:14:19 -04004430 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004431 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004432 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004433 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004434 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004435 struct extent_buffer *leaf;
4436 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004437 struct btrfs_map_token token;
4438
4439 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004440
Chris Mason5f39d392007-10-15 16:14:19 -04004441 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004442 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004443
Chris Mason5f39d392007-10-15 16:14:19 -04004444 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004445 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004446
Chris Masonf25956c2008-09-12 15:32:53 -04004447 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004448 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004449 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004450 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004451 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004452 }
Chris Mason5f39d392007-10-15 16:14:19 -04004453
Chris Masonbe0e5c02007-01-26 15:51:26 -05004454 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004455 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004456
Chris Mason5f39d392007-10-15 16:14:19 -04004457 if (old_data < data_end) {
4458 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004459 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004460 slot, old_data, data_end);
4461 BUG_ON(1);
4462 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004463 /*
4464 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4465 */
4466 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004467 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004468 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004469
Chris Mason5f39d392007-10-15 16:14:19 -04004470 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004471 ioff = btrfs_token_item_offset(leaf, item, &token);
4472 btrfs_set_token_item_offset(leaf, item,
4473 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004474 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004475 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004476 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004477 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004478 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004479
4480 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004481 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004482 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004483 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004484 data_end = old_data;
4485 }
Chris Mason5f39d392007-10-15 16:14:19 -04004486
Chris Mason62e27492007-03-15 12:56:47 -04004487 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004488 for (i = 0; i < nr; i++) {
4489 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4490 btrfs_set_item_key(leaf, &disk_key, slot + i);
4491 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004492 btrfs_set_token_item_offset(leaf, item,
4493 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004494 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004495 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004496 }
Chris Mason44871b12009-03-13 10:04:31 -04004497
Chris Mason9c583092008-01-29 15:15:18 -05004498 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004499
Chris Mason5a01a2e2008-01-30 11:43:54 -05004500 if (slot == 0) {
4501 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004502 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004503 }
Chris Masonb9473432009-03-13 11:00:37 -04004504 btrfs_unlock_up_safe(path, 1);
4505 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004506
Chris Mason5f39d392007-10-15 16:14:19 -04004507 if (btrfs_leaf_free_space(root, leaf) < 0) {
4508 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004509 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004510 }
Chris Mason44871b12009-03-13 10:04:31 -04004511}
4512
4513/*
4514 * Given a key and some data, insert items into the tree.
4515 * This does all the path init required, making room in the tree if needed.
4516 */
4517int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4518 struct btrfs_root *root,
4519 struct btrfs_path *path,
4520 struct btrfs_key *cpu_key, u32 *data_size,
4521 int nr)
4522{
Chris Mason44871b12009-03-13 10:04:31 -04004523 int ret = 0;
4524 int slot;
4525 int i;
4526 u32 total_size = 0;
4527 u32 total_data = 0;
4528
4529 for (i = 0; i < nr; i++)
4530 total_data += data_size[i];
4531
4532 total_size = total_data + (nr * sizeof(struct btrfs_item));
4533 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4534 if (ret == 0)
4535 return -EEXIST;
4536 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004537 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004538
Chris Mason44871b12009-03-13 10:04:31 -04004539 slot = path->slots[0];
4540 BUG_ON(slot < 0);
4541
Jeff Mahoney143bede2012-03-01 14:56:26 +01004542 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004543 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004544 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004545}
4546
4547/*
4548 * Given a key and some data, insert an item into the tree.
4549 * This does all the path init required, making room in the tree if needed.
4550 */
Chris Masone089f052007-03-16 16:20:31 -04004551int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4552 *root, struct btrfs_key *cpu_key, void *data, u32
4553 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004554{
4555 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004556 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004557 struct extent_buffer *leaf;
4558 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004559
Chris Mason2c90e5d2007-04-02 10:50:19 -04004560 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004561 if (!path)
4562 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004563 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004564 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004565 leaf = path->nodes[0];
4566 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4567 write_extent_buffer(leaf, data, ptr, data_size);
4568 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004569 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004570 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004571 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004572}
4573
Chris Mason74123bd2007-02-02 11:05:29 -05004574/*
Chris Mason5de08d72007-02-24 06:24:44 -05004575 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004576 *
Chris Masond352ac62008-09-29 15:18:18 -04004577 * the tree should have been previously balanced so the deletion does not
4578 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004579 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004580static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004581 struct btrfs_path *path, int level, int slot,
4582 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004583{
Chris Mason5f39d392007-10-15 16:14:19 -04004584 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004585 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004586 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004587
Chris Mason5f39d392007-10-15 16:14:19 -04004588 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004589 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004590 if (tree_mod_log && level)
4591 tree_mod_log_eb_move(root->fs_info, parent, slot,
4592 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004593 memmove_extent_buffer(parent,
4594 btrfs_node_key_ptr_offset(slot),
4595 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004596 sizeof(struct btrfs_key_ptr) *
4597 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004598 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004599 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4600 MOD_LOG_KEY_REMOVE);
4601 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004602 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004603
Chris Mason7518a232007-03-12 12:01:18 -04004604 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004605 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004606 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004607 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004608 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004609 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004610 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004611 struct btrfs_disk_key disk_key;
4612
4613 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004614 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004615 }
Chris Masond6025572007-03-30 14:27:56 -04004616 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004617}
4618
Chris Mason74123bd2007-02-02 11:05:29 -05004619/*
Chris Mason323ac952008-10-01 19:05:46 -04004620 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004621 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004622 *
4623 * This deletes the pointer in path->nodes[1] and frees the leaf
4624 * block extent. zero is returned if it all worked out, < 0 otherwise.
4625 *
4626 * The path must have already been setup for deleting the leaf, including
4627 * all the proper balancing. path->nodes[1] must be locked.
4628 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004629static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4630 struct btrfs_root *root,
4631 struct btrfs_path *path,
4632 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004633{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004634 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004635 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004636
Chris Mason4d081c42009-02-04 09:31:28 -05004637 /*
4638 * btrfs_free_extent is expensive, we want to make sure we
4639 * aren't holding any locks when we call it
4640 */
4641 btrfs_unlock_up_safe(path, 0);
4642
Yan, Zhengf0486c62010-05-16 10:46:25 -04004643 root_sub_used(root, leaf->len);
4644
Josef Bacik3083ee22012-03-09 16:01:49 -05004645 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004646 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004647 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004648}
4649/*
Chris Mason74123bd2007-02-02 11:05:29 -05004650 * delete the item at the leaf level in path. If that empties
4651 * the leaf, remove it from the tree
4652 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004653int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4654 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004655{
Chris Mason5f39d392007-10-15 16:14:19 -04004656 struct extent_buffer *leaf;
4657 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 int last_off;
4659 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004660 int ret = 0;
4661 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004662 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004663 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004664 struct btrfs_map_token token;
4665
4666 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004667
Chris Mason5f39d392007-10-15 16:14:19 -04004668 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004669 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4670
4671 for (i = 0; i < nr; i++)
4672 dsize += btrfs_item_size_nr(leaf, slot + i);
4673
Chris Mason5f39d392007-10-15 16:14:19 -04004674 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004675
Chris Mason85e21ba2008-01-29 15:11:36 -05004676 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004677 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004678
4679 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004680 data_end + dsize,
4681 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004682 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004683
Chris Mason85e21ba2008-01-29 15:11:36 -05004684 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004685 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004686
Chris Mason5f39d392007-10-15 16:14:19 -04004687 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004688 ioff = btrfs_token_item_offset(leaf, item, &token);
4689 btrfs_set_token_item_offset(leaf, item,
4690 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004691 }
Chris Masondb945352007-10-15 16:15:53 -04004692
Chris Mason5f39d392007-10-15 16:14:19 -04004693 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004694 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004695 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004696 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004697 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004698 btrfs_set_header_nritems(leaf, nritems - nr);
4699 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004700
Chris Mason74123bd2007-02-02 11:05:29 -05004701 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004702 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004703 if (leaf == root->node) {
4704 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004705 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004706 btrfs_set_path_blocking(path);
4707 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004708 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004709 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004710 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004711 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004712 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004713 struct btrfs_disk_key disk_key;
4714
4715 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004716 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004717 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004718
Chris Mason74123bd2007-02-02 11:05:29 -05004719 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004720 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004721 /* push_leaf_left fixes the path.
4722 * make sure the path still points to our leaf
4723 * for possible call to del_ptr below
4724 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004725 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004726 extent_buffer_get(leaf);
4727
Chris Masonb9473432009-03-13 11:00:37 -04004728 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004729 wret = push_leaf_left(trans, root, path, 1, 1,
4730 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004731 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004732 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004733
4734 if (path->nodes[0] == leaf &&
4735 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004736 wret = push_leaf_right(trans, root, path, 1,
4737 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004738 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004739 ret = wret;
4740 }
Chris Mason5f39d392007-10-15 16:14:19 -04004741
4742 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004743 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004744 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004745 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004746 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004747 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004748 /* if we're still in the path, make sure
4749 * we're dirty. Otherwise, one of the
4750 * push_leaf functions must have already
4751 * dirtied this buffer
4752 */
4753 if (path->nodes[0] == leaf)
4754 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004755 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004756 }
Chris Masond5719762007-03-23 10:01:08 -04004757 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004758 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004759 }
4760 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004761 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004762}
4763
Chris Mason97571fd2007-02-24 13:39:08 -05004764/*
Chris Mason925baed2008-06-25 16:01:30 -04004765 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004766 * returns 0 if it found something or 1 if there are no lesser leaves.
4767 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004768 *
4769 * This may release the path, and so you may lose any locks held at the
4770 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004771 */
4772int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4773{
Chris Mason925baed2008-06-25 16:01:30 -04004774 struct btrfs_key key;
4775 struct btrfs_disk_key found_key;
4776 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004777
Chris Mason925baed2008-06-25 16:01:30 -04004778 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004779
Chris Mason925baed2008-06-25 16:01:30 -04004780 if (key.offset > 0)
4781 key.offset--;
4782 else if (key.type > 0)
4783 key.type--;
4784 else if (key.objectid > 0)
4785 key.objectid--;
4786 else
4787 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004788
David Sterbab3b4aa72011-04-21 01:20:15 +02004789 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004790 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4791 if (ret < 0)
4792 return ret;
4793 btrfs_item_key(path->nodes[0], &found_key, 0);
4794 ret = comp_keys(&found_key, &key);
4795 if (ret < 0)
4796 return 0;
4797 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004798}
4799
Chris Mason3f157a22008-06-25 16:01:31 -04004800/*
4801 * A helper function to walk down the tree starting at min_key, and looking
4802 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004803 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004804 *
4805 * This does not cow, but it does stuff the starting key it finds back
4806 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4807 * key and get a writable path.
4808 *
4809 * This does lock as it descends, and path->keep_locks should be set
4810 * to 1 by the caller.
4811 *
4812 * This honors path->lowest_level to prevent descent past a given level
4813 * of the tree.
4814 *
Chris Masond352ac62008-09-29 15:18:18 -04004815 * min_trans indicates the oldest transaction that you are interested
4816 * in walking through. Any nodes or leaves older than min_trans are
4817 * skipped over (without reading them).
4818 *
Chris Mason3f157a22008-06-25 16:01:31 -04004819 * returns zero if something useful was found, < 0 on error and 1 if there
4820 * was nothing in the tree that matched the search criteria.
4821 */
4822int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004823 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004824 struct btrfs_path *path, int cache_only,
4825 u64 min_trans)
4826{
4827 struct extent_buffer *cur;
4828 struct btrfs_key found_key;
4829 int slot;
Yan96524802008-07-24 12:19:49 -04004830 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004831 u32 nritems;
4832 int level;
4833 int ret = 1;
4834
Chris Mason934d3752008-12-08 16:43:10 -05004835 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004836again:
Chris Masonbd681512011-07-16 15:23:14 -04004837 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004838 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004839 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004840 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004841 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004842
4843 if (btrfs_header_generation(cur) < min_trans) {
4844 ret = 1;
4845 goto out;
4846 }
Chris Masond3977122009-01-05 21:25:51 -05004847 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004848 nritems = btrfs_header_nritems(cur);
4849 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004850 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004851
Chris Mason323ac952008-10-01 19:05:46 -04004852 /* at the lowest level, we're done, setup the path and exit */
4853 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004854 if (slot >= nritems)
4855 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004856 ret = 0;
4857 path->slots[level] = slot;
4858 btrfs_item_key_to_cpu(cur, &found_key, slot);
4859 goto out;
4860 }
Yan96524802008-07-24 12:19:49 -04004861 if (sret && slot > 0)
4862 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004863 /*
4864 * check this node pointer against the cache_only and
4865 * min_trans parameters. If it isn't in cache or is too
4866 * old, skip to the next one.
4867 */
Chris Masond3977122009-01-05 21:25:51 -05004868 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004869 u64 blockptr;
4870 u64 gen;
4871 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004872 struct btrfs_disk_key disk_key;
4873
Chris Mason3f157a22008-06-25 16:01:31 -04004874 blockptr = btrfs_node_blockptr(cur, slot);
4875 gen = btrfs_node_ptr_generation(cur, slot);
4876 if (gen < min_trans) {
4877 slot++;
4878 continue;
4879 }
4880 if (!cache_only)
4881 break;
4882
Chris Masone02119d2008-09-05 16:13:11 -04004883 if (max_key) {
4884 btrfs_node_key(cur, &disk_key, slot);
4885 if (comp_keys(&disk_key, max_key) >= 0) {
4886 ret = 1;
4887 goto out;
4888 }
4889 }
4890
Chris Mason3f157a22008-06-25 16:01:31 -04004891 tmp = btrfs_find_tree_block(root, blockptr,
4892 btrfs_level_size(root, level - 1));
4893
Chris Masonb9fab912012-05-06 07:23:47 -04004894 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004895 free_extent_buffer(tmp);
4896 break;
4897 }
4898 if (tmp)
4899 free_extent_buffer(tmp);
4900 slot++;
4901 }
Chris Masone02119d2008-09-05 16:13:11 -04004902find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004903 /*
4904 * we didn't find a candidate key in this node, walk forward
4905 * and find another one
4906 */
4907 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004908 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004909 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004910 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004911 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004912 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004913 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004914 goto again;
4915 } else {
4916 goto out;
4917 }
4918 }
4919 /* save our key for returning back */
4920 btrfs_node_key_to_cpu(cur, &found_key, slot);
4921 path->slots[level] = slot;
4922 if (level == path->lowest_level) {
4923 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004924 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004925 goto out;
4926 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004927 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004928 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004929 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004930
Chris Masonbd681512011-07-16 15:23:14 -04004931 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004932
Chris Masonbd681512011-07-16 15:23:14 -04004933 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004934 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004935 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004936 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004937 }
4938out:
4939 if (ret == 0)
4940 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004941 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004942 return ret;
4943}
4944
Alexander Block70698302012-06-05 21:07:48 +02004945static void tree_move_down(struct btrfs_root *root,
4946 struct btrfs_path *path,
4947 int *level, int root_level)
4948{
Chris Mason74dd17f2012-08-07 16:25:13 -04004949 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004950 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4951 path->slots[*level]);
4952 path->slots[*level - 1] = 0;
4953 (*level)--;
4954}
4955
4956static int tree_move_next_or_upnext(struct btrfs_root *root,
4957 struct btrfs_path *path,
4958 int *level, int root_level)
4959{
4960 int ret = 0;
4961 int nritems;
4962 nritems = btrfs_header_nritems(path->nodes[*level]);
4963
4964 path->slots[*level]++;
4965
Chris Mason74dd17f2012-08-07 16:25:13 -04004966 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004967 if (*level == root_level)
4968 return -1;
4969
4970 /* move upnext */
4971 path->slots[*level] = 0;
4972 free_extent_buffer(path->nodes[*level]);
4973 path->nodes[*level] = NULL;
4974 (*level)++;
4975 path->slots[*level]++;
4976
4977 nritems = btrfs_header_nritems(path->nodes[*level]);
4978 ret = 1;
4979 }
4980 return ret;
4981}
4982
4983/*
4984 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
4985 * or down.
4986 */
4987static int tree_advance(struct btrfs_root *root,
4988 struct btrfs_path *path,
4989 int *level, int root_level,
4990 int allow_down,
4991 struct btrfs_key *key)
4992{
4993 int ret;
4994
4995 if (*level == 0 || !allow_down) {
4996 ret = tree_move_next_or_upnext(root, path, level, root_level);
4997 } else {
4998 tree_move_down(root, path, level, root_level);
4999 ret = 0;
5000 }
5001 if (ret >= 0) {
5002 if (*level == 0)
5003 btrfs_item_key_to_cpu(path->nodes[*level], key,
5004 path->slots[*level]);
5005 else
5006 btrfs_node_key_to_cpu(path->nodes[*level], key,
5007 path->slots[*level]);
5008 }
5009 return ret;
5010}
5011
5012static int tree_compare_item(struct btrfs_root *left_root,
5013 struct btrfs_path *left_path,
5014 struct btrfs_path *right_path,
5015 char *tmp_buf)
5016{
5017 int cmp;
5018 int len1, len2;
5019 unsigned long off1, off2;
5020
5021 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5022 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5023 if (len1 != len2)
5024 return 1;
5025
5026 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5027 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5028 right_path->slots[0]);
5029
5030 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5031
5032 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5033 if (cmp)
5034 return 1;
5035 return 0;
5036}
5037
5038#define ADVANCE 1
5039#define ADVANCE_ONLY_NEXT -1
5040
5041/*
5042 * This function compares two trees and calls the provided callback for
5043 * every changed/new/deleted item it finds.
5044 * If shared tree blocks are encountered, whole subtrees are skipped, making
5045 * the compare pretty fast on snapshotted subvolumes.
5046 *
5047 * This currently works on commit roots only. As commit roots are read only,
5048 * we don't do any locking. The commit roots are protected with transactions.
5049 * Transactions are ended and rejoined when a commit is tried in between.
5050 *
5051 * This function checks for modifications done to the trees while comparing.
5052 * If it detects a change, it aborts immediately.
5053 */
5054int btrfs_compare_trees(struct btrfs_root *left_root,
5055 struct btrfs_root *right_root,
5056 btrfs_changed_cb_t changed_cb, void *ctx)
5057{
5058 int ret;
5059 int cmp;
5060 struct btrfs_trans_handle *trans = NULL;
5061 struct btrfs_path *left_path = NULL;
5062 struct btrfs_path *right_path = NULL;
5063 struct btrfs_key left_key;
5064 struct btrfs_key right_key;
5065 char *tmp_buf = NULL;
5066 int left_root_level;
5067 int right_root_level;
5068 int left_level;
5069 int right_level;
5070 int left_end_reached;
5071 int right_end_reached;
5072 int advance_left;
5073 int advance_right;
5074 u64 left_blockptr;
5075 u64 right_blockptr;
5076 u64 left_start_ctransid;
5077 u64 right_start_ctransid;
5078 u64 ctransid;
5079
5080 left_path = btrfs_alloc_path();
5081 if (!left_path) {
5082 ret = -ENOMEM;
5083 goto out;
5084 }
5085 right_path = btrfs_alloc_path();
5086 if (!right_path) {
5087 ret = -ENOMEM;
5088 goto out;
5089 }
5090
5091 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5092 if (!tmp_buf) {
5093 ret = -ENOMEM;
5094 goto out;
5095 }
5096
5097 left_path->search_commit_root = 1;
5098 left_path->skip_locking = 1;
5099 right_path->search_commit_root = 1;
5100 right_path->skip_locking = 1;
5101
5102 spin_lock(&left_root->root_times_lock);
5103 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
5104 spin_unlock(&left_root->root_times_lock);
5105
5106 spin_lock(&right_root->root_times_lock);
5107 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
5108 spin_unlock(&right_root->root_times_lock);
5109
5110 trans = btrfs_join_transaction(left_root);
5111 if (IS_ERR(trans)) {
5112 ret = PTR_ERR(trans);
5113 trans = NULL;
5114 goto out;
5115 }
5116
5117 /*
5118 * Strategy: Go to the first items of both trees. Then do
5119 *
5120 * If both trees are at level 0
5121 * Compare keys of current items
5122 * If left < right treat left item as new, advance left tree
5123 * and repeat
5124 * If left > right treat right item as deleted, advance right tree
5125 * and repeat
5126 * If left == right do deep compare of items, treat as changed if
5127 * needed, advance both trees and repeat
5128 * If both trees are at the same level but not at level 0
5129 * Compare keys of current nodes/leafs
5130 * If left < right advance left tree and repeat
5131 * If left > right advance right tree and repeat
5132 * If left == right compare blockptrs of the next nodes/leafs
5133 * If they match advance both trees but stay at the same level
5134 * and repeat
5135 * If they don't match advance both trees while allowing to go
5136 * deeper and repeat
5137 * If tree levels are different
5138 * Advance the tree that needs it and repeat
5139 *
5140 * Advancing a tree means:
5141 * If we are at level 0, try to go to the next slot. If that's not
5142 * possible, go one level up and repeat. Stop when we found a level
5143 * where we could go to the next slot. We may at this point be on a
5144 * node or a leaf.
5145 *
5146 * If we are not at level 0 and not on shared tree blocks, go one
5147 * level deeper.
5148 *
5149 * If we are not at level 0 and on shared tree blocks, go one slot to
5150 * the right if possible or go up and right.
5151 */
5152
5153 left_level = btrfs_header_level(left_root->commit_root);
5154 left_root_level = left_level;
5155 left_path->nodes[left_level] = left_root->commit_root;
5156 extent_buffer_get(left_path->nodes[left_level]);
5157
5158 right_level = btrfs_header_level(right_root->commit_root);
5159 right_root_level = right_level;
5160 right_path->nodes[right_level] = right_root->commit_root;
5161 extent_buffer_get(right_path->nodes[right_level]);
5162
5163 if (left_level == 0)
5164 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5165 &left_key, left_path->slots[left_level]);
5166 else
5167 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5168 &left_key, left_path->slots[left_level]);
5169 if (right_level == 0)
5170 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5171 &right_key, right_path->slots[right_level]);
5172 else
5173 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5174 &right_key, right_path->slots[right_level]);
5175
5176 left_end_reached = right_end_reached = 0;
5177 advance_left = advance_right = 0;
5178
5179 while (1) {
5180 /*
5181 * We need to make sure the transaction does not get committed
5182 * while we do anything on commit roots. This means, we need to
5183 * join and leave transactions for every item that we process.
5184 */
5185 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5186 btrfs_release_path(left_path);
5187 btrfs_release_path(right_path);
5188
5189 ret = btrfs_end_transaction(trans, left_root);
5190 trans = NULL;
5191 if (ret < 0)
5192 goto out;
5193 }
5194 /* now rejoin the transaction */
5195 if (!trans) {
5196 trans = btrfs_join_transaction(left_root);
5197 if (IS_ERR(trans)) {
5198 ret = PTR_ERR(trans);
5199 trans = NULL;
5200 goto out;
5201 }
5202
5203 spin_lock(&left_root->root_times_lock);
5204 ctransid = btrfs_root_ctransid(&left_root->root_item);
5205 spin_unlock(&left_root->root_times_lock);
5206 if (ctransid != left_start_ctransid)
5207 left_start_ctransid = 0;
5208
5209 spin_lock(&right_root->root_times_lock);
5210 ctransid = btrfs_root_ctransid(&right_root->root_item);
5211 spin_unlock(&right_root->root_times_lock);
5212 if (ctransid != right_start_ctransid)
5213 right_start_ctransid = 0;
5214
5215 if (!left_start_ctransid || !right_start_ctransid) {
5216 WARN(1, KERN_WARNING
5217 "btrfs: btrfs_compare_tree detected "
5218 "a change in one of the trees while "
5219 "iterating. This is probably a "
5220 "bug.\n");
5221 ret = -EIO;
5222 goto out;
5223 }
5224
5225 /*
5226 * the commit root may have changed, so start again
5227 * where we stopped
5228 */
5229 left_path->lowest_level = left_level;
5230 right_path->lowest_level = right_level;
5231 ret = btrfs_search_slot(NULL, left_root,
5232 &left_key, left_path, 0, 0);
5233 if (ret < 0)
5234 goto out;
5235 ret = btrfs_search_slot(NULL, right_root,
5236 &right_key, right_path, 0, 0);
5237 if (ret < 0)
5238 goto out;
5239 }
5240
5241 if (advance_left && !left_end_reached) {
5242 ret = tree_advance(left_root, left_path, &left_level,
5243 left_root_level,
5244 advance_left != ADVANCE_ONLY_NEXT,
5245 &left_key);
5246 if (ret < 0)
5247 left_end_reached = ADVANCE;
5248 advance_left = 0;
5249 }
5250 if (advance_right && !right_end_reached) {
5251 ret = tree_advance(right_root, right_path, &right_level,
5252 right_root_level,
5253 advance_right != ADVANCE_ONLY_NEXT,
5254 &right_key);
5255 if (ret < 0)
5256 right_end_reached = ADVANCE;
5257 advance_right = 0;
5258 }
5259
5260 if (left_end_reached && right_end_reached) {
5261 ret = 0;
5262 goto out;
5263 } else if (left_end_reached) {
5264 if (right_level == 0) {
5265 ret = changed_cb(left_root, right_root,
5266 left_path, right_path,
5267 &right_key,
5268 BTRFS_COMPARE_TREE_DELETED,
5269 ctx);
5270 if (ret < 0)
5271 goto out;
5272 }
5273 advance_right = ADVANCE;
5274 continue;
5275 } else if (right_end_reached) {
5276 if (left_level == 0) {
5277 ret = changed_cb(left_root, right_root,
5278 left_path, right_path,
5279 &left_key,
5280 BTRFS_COMPARE_TREE_NEW,
5281 ctx);
5282 if (ret < 0)
5283 goto out;
5284 }
5285 advance_left = ADVANCE;
5286 continue;
5287 }
5288
5289 if (left_level == 0 && right_level == 0) {
5290 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5291 if (cmp < 0) {
5292 ret = changed_cb(left_root, right_root,
5293 left_path, right_path,
5294 &left_key,
5295 BTRFS_COMPARE_TREE_NEW,
5296 ctx);
5297 if (ret < 0)
5298 goto out;
5299 advance_left = ADVANCE;
5300 } else if (cmp > 0) {
5301 ret = changed_cb(left_root, right_root,
5302 left_path, right_path,
5303 &right_key,
5304 BTRFS_COMPARE_TREE_DELETED,
5305 ctx);
5306 if (ret < 0)
5307 goto out;
5308 advance_right = ADVANCE;
5309 } else {
Chris Mason74dd17f2012-08-07 16:25:13 -04005310 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005311 ret = tree_compare_item(left_root, left_path,
5312 right_path, tmp_buf);
5313 if (ret) {
Chris Mason74dd17f2012-08-07 16:25:13 -04005314 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005315 ret = changed_cb(left_root, right_root,
5316 left_path, right_path,
5317 &left_key,
5318 BTRFS_COMPARE_TREE_CHANGED,
5319 ctx);
5320 if (ret < 0)
5321 goto out;
5322 }
5323 advance_left = ADVANCE;
5324 advance_right = ADVANCE;
5325 }
5326 } else if (left_level == right_level) {
5327 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5328 if (cmp < 0) {
5329 advance_left = ADVANCE;
5330 } else if (cmp > 0) {
5331 advance_right = ADVANCE;
5332 } else {
5333 left_blockptr = btrfs_node_blockptr(
5334 left_path->nodes[left_level],
5335 left_path->slots[left_level]);
5336 right_blockptr = btrfs_node_blockptr(
5337 right_path->nodes[right_level],
5338 right_path->slots[right_level]);
5339 if (left_blockptr == right_blockptr) {
5340 /*
5341 * As we're on a shared block, don't
5342 * allow to go deeper.
5343 */
5344 advance_left = ADVANCE_ONLY_NEXT;
5345 advance_right = ADVANCE_ONLY_NEXT;
5346 } else {
5347 advance_left = ADVANCE;
5348 advance_right = ADVANCE;
5349 }
5350 }
5351 } else if (left_level < right_level) {
5352 advance_right = ADVANCE;
5353 } else {
5354 advance_left = ADVANCE;
5355 }
5356 }
5357
5358out:
5359 btrfs_free_path(left_path);
5360 btrfs_free_path(right_path);
5361 kfree(tmp_buf);
5362
5363 if (trans) {
5364 if (!ret)
5365 ret = btrfs_end_transaction(trans, left_root);
5366 else
5367 btrfs_end_transaction(trans, left_root);
5368 }
5369
5370 return ret;
5371}
5372
Chris Mason3f157a22008-06-25 16:01:31 -04005373/*
5374 * this is similar to btrfs_next_leaf, but does not try to preserve
5375 * and fixup the path. It looks for and returns the next key in the
5376 * tree based on the current path and the cache_only and min_trans
5377 * parameters.
5378 *
5379 * 0 is returned if another key is found, < 0 if there are any errors
5380 * and 1 is returned if there are no higher keys in the tree
5381 *
5382 * path->keep_locks should be set to 1 on the search made before
5383 * calling this function.
5384 */
Chris Masone7a84562008-06-25 16:01:31 -04005385int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04005386 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04005387 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005388{
Chris Masone7a84562008-06-25 16:01:31 -04005389 int slot;
5390 struct extent_buffer *c;
5391
Chris Mason934d3752008-12-08 16:43:10 -05005392 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005393 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005394 if (!path->nodes[level])
5395 return 1;
5396
5397 slot = path->slots[level] + 1;
5398 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005399next:
Chris Masone7a84562008-06-25 16:01:31 -04005400 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005401 int ret;
5402 int orig_lowest;
5403 struct btrfs_key cur_key;
5404 if (level + 1 >= BTRFS_MAX_LEVEL ||
5405 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005406 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005407
5408 if (path->locks[level + 1]) {
5409 level++;
5410 continue;
5411 }
5412
5413 slot = btrfs_header_nritems(c) - 1;
5414 if (level == 0)
5415 btrfs_item_key_to_cpu(c, &cur_key, slot);
5416 else
5417 btrfs_node_key_to_cpu(c, &cur_key, slot);
5418
5419 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005420 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005421 path->lowest_level = level;
5422 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5423 0, 0);
5424 path->lowest_level = orig_lowest;
5425 if (ret < 0)
5426 return ret;
5427
5428 c = path->nodes[level];
5429 slot = path->slots[level];
5430 if (ret == 0)
5431 slot++;
5432 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005433 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005434
Chris Masone7a84562008-06-25 16:01:31 -04005435 if (level == 0)
5436 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005437 else {
5438 u64 blockptr = btrfs_node_blockptr(c, slot);
5439 u64 gen = btrfs_node_ptr_generation(c, slot);
5440
5441 if (cache_only) {
5442 struct extent_buffer *cur;
5443 cur = btrfs_find_tree_block(root, blockptr,
5444 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04005445 if (!cur ||
5446 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04005447 slot++;
5448 if (cur)
5449 free_extent_buffer(cur);
5450 goto next;
5451 }
5452 free_extent_buffer(cur);
5453 }
5454 if (gen < min_trans) {
5455 slot++;
5456 goto next;
5457 }
Chris Masone7a84562008-06-25 16:01:31 -04005458 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005459 }
Chris Masone7a84562008-06-25 16:01:31 -04005460 return 0;
5461 }
5462 return 1;
5463}
5464
Chris Mason7bb86312007-12-11 09:25:06 -05005465/*
Chris Mason925baed2008-06-25 16:01:30 -04005466 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005467 * returns 0 if it found something or 1 if there are no greater leaves.
5468 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005469 */
Chris Mason234b63a2007-03-13 10:46:10 -04005470int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005471{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005472 return btrfs_next_old_leaf(root, path, 0);
5473}
5474
5475int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5476 u64 time_seq)
5477{
Chris Masond97e63b2007-02-20 16:40:44 -05005478 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005479 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005480 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005481 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005482 struct btrfs_key key;
5483 u32 nritems;
5484 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005485 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005486 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005487
5488 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005489 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005490 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005491
Chris Mason8e73f272009-04-03 10:14:18 -04005492 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5493again:
5494 level = 1;
5495 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005496 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005497 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005498
Chris Masona2135012008-06-25 16:01:30 -04005499 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005500 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005501
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005502 if (time_seq)
5503 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5504 else
5505 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005506 path->keep_locks = 0;
5507
5508 if (ret < 0)
5509 return ret;
5510
Chris Masona2135012008-06-25 16:01:30 -04005511 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005512 /*
5513 * by releasing the path above we dropped all our locks. A balance
5514 * could have added more items next to the key that used to be
5515 * at the very end of the block. So, check again here and
5516 * advance the path if there are now more items available.
5517 */
Chris Masona2135012008-06-25 16:01:30 -04005518 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005519 if (ret == 0)
5520 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005521 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005522 goto done;
5523 }
Chris Masond97e63b2007-02-20 16:40:44 -05005524
Chris Masond3977122009-01-05 21:25:51 -05005525 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005526 if (!path->nodes[level]) {
5527 ret = 1;
5528 goto done;
5529 }
Chris Mason5f39d392007-10-15 16:14:19 -04005530
Chris Masond97e63b2007-02-20 16:40:44 -05005531 slot = path->slots[level] + 1;
5532 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005533 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005534 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005535 if (level == BTRFS_MAX_LEVEL) {
5536 ret = 1;
5537 goto done;
5538 }
Chris Masond97e63b2007-02-20 16:40:44 -05005539 continue;
5540 }
Chris Mason5f39d392007-10-15 16:14:19 -04005541
Chris Mason925baed2008-06-25 16:01:30 -04005542 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005543 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005544 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005545 }
Chris Mason5f39d392007-10-15 16:14:19 -04005546
Chris Mason8e73f272009-04-03 10:14:18 -04005547 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005548 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005549 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005550 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005551 if (ret == -EAGAIN)
5552 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005553
Chris Mason76a05b32009-05-14 13:24:30 -04005554 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005555 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005556 goto done;
5557 }
5558
Chris Mason5cd57b22008-06-25 16:01:30 -04005559 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005560 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005561 if (!ret && time_seq) {
5562 /*
5563 * If we don't get the lock, we may be racing
5564 * with push_leaf_left, holding that lock while
5565 * itself waiting for the leaf we've currently
5566 * locked. To solve this situation, we give up
5567 * on our lock and cycle.
5568 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005569 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005570 btrfs_release_path(path);
5571 cond_resched();
5572 goto again;
5573 }
Chris Mason8e73f272009-04-03 10:14:18 -04005574 if (!ret) {
5575 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005576 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005577 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005578 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005579 }
Chris Mason31533fb2011-07-26 16:01:59 -04005580 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005581 }
Chris Masond97e63b2007-02-20 16:40:44 -05005582 break;
5583 }
5584 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005585 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005586 level--;
5587 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005588 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005589 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005590
Chris Mason5f39d392007-10-15 16:14:19 -04005591 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005592 path->nodes[level] = next;
5593 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005594 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005595 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005596 if (!level)
5597 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005598
Chris Mason8e73f272009-04-03 10:14:18 -04005599 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005600 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005601 if (ret == -EAGAIN)
5602 goto again;
5603
Chris Mason76a05b32009-05-14 13:24:30 -04005604 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005605 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005606 goto done;
5607 }
5608
Chris Mason5cd57b22008-06-25 16:01:30 -04005609 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005610 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005611 if (!ret) {
5612 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005613 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005614 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005615 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005616 }
Chris Mason31533fb2011-07-26 16:01:59 -04005617 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005618 }
Chris Masond97e63b2007-02-20 16:40:44 -05005619 }
Chris Mason8e73f272009-04-03 10:14:18 -04005620 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005621done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005622 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005623 path->leave_spinning = old_spinning;
5624 if (!old_spinning)
5625 btrfs_set_path_blocking(path);
5626
5627 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005628}
Chris Mason0b86a832008-03-24 15:01:56 -04005629
Chris Mason3f157a22008-06-25 16:01:31 -04005630/*
5631 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5632 * searching until it gets past min_objectid or finds an item of 'type'
5633 *
5634 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5635 */
Chris Mason0b86a832008-03-24 15:01:56 -04005636int btrfs_previous_item(struct btrfs_root *root,
5637 struct btrfs_path *path, u64 min_objectid,
5638 int type)
5639{
5640 struct btrfs_key found_key;
5641 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005642 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005643 int ret;
5644
Chris Masond3977122009-01-05 21:25:51 -05005645 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005646 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005647 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005648 ret = btrfs_prev_leaf(root, path);
5649 if (ret != 0)
5650 return ret;
5651 } else {
5652 path->slots[0]--;
5653 }
5654 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005655 nritems = btrfs_header_nritems(leaf);
5656 if (nritems == 0)
5657 return 1;
5658 if (path->slots[0] == nritems)
5659 path->slots[0]--;
5660
Chris Mason0b86a832008-03-24 15:01:56 -04005661 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005662 if (found_key.objectid < min_objectid)
5663 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005664 if (found_key.type == type)
5665 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005666 if (found_key.objectid == min_objectid &&
5667 found_key.type < type)
5668 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005669 }
5670 return 1;
5671}