blob: fb21431fe4e045f507c0b5f0974c7f4afbfa2797 [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 /*
424 * we removed the lowest blocker from the blocker list, so there may be
425 * more processible delayed refs.
426 */
427 wake_up(&fs_info->tree_mod_seq_wait);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200428
429 /*
430 * anything that's lower than the lowest existing (read: blocked)
431 * sequence number can be removed from the tree.
432 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200433 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434 tm_root = &fs_info->tree_mod_log;
435 for (node = rb_first(tm_root); node; node = next) {
436 next = rb_next(node);
437 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200438 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439 continue;
440 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200441 kfree(tm);
442 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200443 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200444}
445
446/*
447 * key order of the log:
448 * index -> sequence
449 *
450 * the index is the shifted logical of the *new* root node for root replace
451 * operations, or the shifted logical of the affected block for all other
452 * operations.
453 */
454static noinline int
455__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
456{
457 struct rb_root *tm_root;
458 struct rb_node **new;
459 struct rb_node *parent = NULL;
460 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200461
Jan Schmidt097b8a72012-06-21 11:08:04 +0200462 BUG_ON(!tm || !tm->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200463
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200464 tm_root = &fs_info->tree_mod_log;
465 new = &tm_root->rb_node;
466 while (*new) {
467 cur = container_of(*new, struct tree_mod_elem, node);
468 parent = *new;
469 if (cur->index < tm->index)
470 new = &((*new)->rb_left);
471 else if (cur->index > tm->index)
472 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200473 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200474 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200475 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200476 new = &((*new)->rb_right);
477 else {
478 kfree(tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200479 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200480 }
481 }
482
483 rb_link_node(&tm->node, parent, new);
484 rb_insert_color(&tm->node, tm_root);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200485 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200486}
487
Jan Schmidt097b8a72012-06-21 11:08:04 +0200488/*
489 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
490 * returns zero with the tree_mod_log_lock acquired. The caller must hold
491 * this until all tree mod log insertions are recorded in the rb tree and then
492 * call tree_mod_log_write_unlock() to release.
493 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200494static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
495 struct extent_buffer *eb) {
496 smp_mb();
497 if (list_empty(&(fs_info)->tree_mod_seq_list))
498 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200499 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200500 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200501
502 tree_mod_log_write_lock(fs_info);
503 if (list_empty(&fs_info->tree_mod_seq_list)) {
504 /*
505 * someone emptied the list while we were waiting for the lock.
506 * we must not add to the list when no blocker exists.
507 */
508 tree_mod_log_write_unlock(fs_info);
509 return 1;
510 }
511
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200512 return 0;
513}
514
Jan Schmidt3310c362012-06-11 10:52:38 +0200515/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200516 * This allocates memory and gets a tree modification sequence number.
Jan Schmidt3310c362012-06-11 10:52:38 +0200517 *
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518 * Returns <0 on error.
519 * Returns >0 (the added sequence number) on success.
Jan Schmidt3310c362012-06-11 10:52:38 +0200520 */
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200521static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
522 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200523{
524 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200525
Jan Schmidt097b8a72012-06-21 11:08:04 +0200526 /*
527 * once we switch from spin locks to something different, we should
528 * honor the flags parameter here.
529 */
530 tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200531 if (!tm)
532 return -ENOMEM;
533
Jan Schmidt097b8a72012-06-21 11:08:04 +0200534 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
535 return tm->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200536}
537
Jan Schmidt097b8a72012-06-21 11:08:04 +0200538static inline int
539__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
540 struct extent_buffer *eb, int slot,
541 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200542{
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200543 int ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200544 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200545
546 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200547 if (ret < 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200548 return ret;
549
550 tm->index = eb->start >> PAGE_CACHE_SHIFT;
551 if (op != MOD_LOG_KEY_ADD) {
552 btrfs_node_key(eb, &tm->key, slot);
553 tm->blockptr = btrfs_node_blockptr(eb, slot);
554 }
555 tm->op = op;
556 tm->slot = slot;
557 tm->generation = btrfs_node_ptr_generation(eb, slot);
558
Jan Schmidt097b8a72012-06-21 11:08:04 +0200559 return __tree_mod_log_insert(fs_info, tm);
560}
561
562static noinline int
563tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
564 struct extent_buffer *eb, int slot,
565 enum mod_log_op op, gfp_t flags)
566{
567 int ret;
568
569 if (tree_mod_dont_log(fs_info, eb))
570 return 0;
571
572 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
573
574 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200575 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200576}
577
578static noinline int
579tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
580 int slot, enum mod_log_op op)
581{
582 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
583}
584
585static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200586tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info,
587 struct extent_buffer *eb, int slot,
588 enum mod_log_op op)
589{
590 return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS);
591}
592
593static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200594tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
595 struct extent_buffer *eb, int dst_slot, int src_slot,
596 int nr_items, gfp_t flags)
597{
598 struct tree_mod_elem *tm;
599 int ret;
600 int i;
601
Jan Schmidtf3956942012-05-31 15:02:32 +0200602 if (tree_mod_dont_log(fs_info, eb))
603 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200604
605 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200606 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200607 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
608 BUG_ON(ret < 0);
609 }
610
Jan Schmidtf3956942012-05-31 15:02:32 +0200611 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200612 if (ret < 0)
613 goto out;
Jan Schmidtf3956942012-05-31 15:02:32 +0200614
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200615 tm->index = eb->start >> PAGE_CACHE_SHIFT;
616 tm->slot = src_slot;
617 tm->move.dst_slot = dst_slot;
618 tm->move.nr_items = nr_items;
619 tm->op = MOD_LOG_MOVE_KEYS;
620
Jan Schmidt3310c362012-06-11 10:52:38 +0200621 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200622out:
623 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200624 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200625}
626
Jan Schmidt097b8a72012-06-21 11:08:04 +0200627static inline void
628__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
629{
630 int i;
631 u32 nritems;
632 int ret;
633
634 nritems = btrfs_header_nritems(eb);
635 for (i = nritems - 1; i >= 0; i--) {
636 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
637 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
638 BUG_ON(ret < 0);
639 }
640}
641
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200642static noinline int
643tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
644 struct extent_buffer *old_root,
645 struct extent_buffer *new_root, gfp_t flags)
646{
647 struct tree_mod_elem *tm;
648 int ret;
649
Jan Schmidt097b8a72012-06-21 11:08:04 +0200650 if (tree_mod_dont_log(fs_info, NULL))
651 return 0;
652
653 __tree_mod_log_free_eb(fs_info, old_root);
654
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200655 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200656 if (ret < 0)
657 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200658
659 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
660 tm->old_root.logical = old_root->start;
661 tm->old_root.level = btrfs_header_level(old_root);
662 tm->generation = btrfs_header_generation(old_root);
663 tm->op = MOD_LOG_ROOT_REPLACE;
664
Jan Schmidt3310c362012-06-11 10:52:38 +0200665 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200666out:
667 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200668 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200669}
670
671static struct tree_mod_elem *
672__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
673 int smallest)
674{
675 struct rb_root *tm_root;
676 struct rb_node *node;
677 struct tree_mod_elem *cur = NULL;
678 struct tree_mod_elem *found = NULL;
679 u64 index = start >> PAGE_CACHE_SHIFT;
680
Jan Schmidt097b8a72012-06-21 11:08:04 +0200681 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200682 tm_root = &fs_info->tree_mod_log;
683 node = tm_root->rb_node;
684 while (node) {
685 cur = container_of(node, struct tree_mod_elem, node);
686 if (cur->index < index) {
687 node = node->rb_left;
688 } else if (cur->index > index) {
689 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200690 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200691 node = node->rb_left;
692 } else if (!smallest) {
693 /* we want the node with the highest seq */
694 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200696 found = cur;
697 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200698 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200699 /* we want the node with the smallest seq */
700 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200701 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200702 found = cur;
703 node = node->rb_right;
704 } else {
705 found = cur;
706 break;
707 }
708 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200709 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200710
711 return found;
712}
713
714/*
715 * this returns the element from the log with the smallest time sequence
716 * value that's in the log (the oldest log item). any element with a time
717 * sequence lower than min_seq will be ignored.
718 */
719static struct tree_mod_elem *
720tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
721 u64 min_seq)
722{
723 return __tree_mod_log_search(fs_info, start, min_seq, 1);
724}
725
726/*
727 * this returns the element from the log with the largest time sequence
728 * value that's in the log (the most recent log item). any element with
729 * a time sequence lower than min_seq will be ignored.
730 */
731static struct tree_mod_elem *
732tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
733{
734 return __tree_mod_log_search(fs_info, start, min_seq, 0);
735}
736
Jan Schmidt097b8a72012-06-21 11:08:04 +0200737static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200738tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
739 struct extent_buffer *src, unsigned long dst_offset,
740 unsigned long src_offset, int nr_items)
741{
742 int ret;
743 int i;
744
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200745 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200746 return;
747
Jan Schmidt097b8a72012-06-21 11:08:04 +0200748 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
749 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200750 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200751 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200752
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200753 for (i = 0; i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200754 ret = tree_mod_log_insert_key_locked(fs_info, src,
755 i + src_offset,
756 MOD_LOG_KEY_REMOVE);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200758 ret = tree_mod_log_insert_key_locked(fs_info, dst,
759 i + dst_offset,
760 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200761 BUG_ON(ret < 0);
762 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200763
764 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200765}
766
767static inline void
768tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
769 int dst_offset, int src_offset, int nr_items)
770{
771 int ret;
772 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
773 nr_items, GFP_NOFS);
774 BUG_ON(ret < 0);
775}
776
Jan Schmidt097b8a72012-06-21 11:08:04 +0200777static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200778tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
779 struct extent_buffer *eb,
780 struct btrfs_disk_key *disk_key, int slot, int atomic)
781{
782 int ret;
783
784 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
785 MOD_LOG_KEY_REPLACE,
786 atomic ? GFP_ATOMIC : GFP_NOFS);
787 BUG_ON(ret < 0);
788}
789
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790static noinline void
791tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200793 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200794 return;
795
Jan Schmidt097b8a72012-06-21 11:08:04 +0200796 __tree_mod_log_free_eb(fs_info, eb);
797
798 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200799}
800
Jan Schmidt097b8a72012-06-21 11:08:04 +0200801static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200802tree_mod_log_set_root_pointer(struct btrfs_root *root,
803 struct extent_buffer *new_root_node)
804{
805 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200806 ret = tree_mod_log_insert_root(root->fs_info, root->node,
807 new_root_node, GFP_NOFS);
808 BUG_ON(ret < 0);
809}
810
Chris Masond352ac62008-09-29 15:18:18 -0400811/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400812 * check if the tree block can be shared by multiple trees
813 */
814int btrfs_block_can_be_shared(struct btrfs_root *root,
815 struct extent_buffer *buf)
816{
817 /*
818 * Tree blocks not in refernece counted trees and tree roots
819 * are never shared. If a block was allocated after the last
820 * snapshot and the block was not allocated by tree relocation,
821 * we know the block is not shared.
822 */
823 if (root->ref_cows &&
824 buf != root->node && buf != root->commit_root &&
825 (btrfs_header_generation(buf) <=
826 btrfs_root_last_snapshot(&root->root_item) ||
827 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
828 return 1;
829#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
830 if (root->ref_cows &&
831 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
832 return 1;
833#endif
834 return 0;
835}
836
837static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
838 struct btrfs_root *root,
839 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400840 struct extent_buffer *cow,
841 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842{
843 u64 refs;
844 u64 owner;
845 u64 flags;
846 u64 new_flags = 0;
847 int ret;
848
849 /*
850 * Backrefs update rules:
851 *
852 * Always use full backrefs for extent pointers in tree block
853 * allocated by tree relocation.
854 *
855 * If a shared tree block is no longer referenced by its owner
856 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
857 * use full backrefs for extent pointers in tree block.
858 *
859 * If a tree block is been relocating
860 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
861 * use full backrefs for extent pointers in tree block.
862 * The reason for this is some operations (such as drop tree)
863 * are only allowed for blocks use full backrefs.
864 */
865
866 if (btrfs_block_can_be_shared(root, buf)) {
867 ret = btrfs_lookup_extent_info(trans, root, buf->start,
868 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700869 if (ret)
870 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700871 if (refs == 0) {
872 ret = -EROFS;
873 btrfs_std_error(root->fs_info, ret);
874 return ret;
875 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400876 } else {
877 refs = 1;
878 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
879 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
880 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
881 else
882 flags = 0;
883 }
884
885 owner = btrfs_header_owner(buf);
886 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
887 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
888
889 if (refs > 1) {
890 if ((owner == root->root_key.objectid ||
891 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
892 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200893 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400895
896 if (root->root_key.objectid ==
897 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200898 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200900 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 }
903 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
904 } else {
905
906 if (root->root_key.objectid ==
907 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200908 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200910 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100911 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 }
913 if (new_flags != 0) {
914 ret = btrfs_set_disk_extent_flags(trans, root,
915 buf->start,
916 buf->len,
917 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700918 if (ret)
919 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 }
921 } else {
922 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
923 if (root->root_key.objectid ==
924 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200925 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200927 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200929 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100930 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200932 /*
933 * don't log freeing in case we're freeing the root node, this
934 * is done by tree_mod_log_set_root_pointer later
935 */
936 if (buf != root->node && btrfs_header_level(buf) != 0)
937 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400939 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400940 }
941 return 0;
942}
943
944/*
Chris Masond3977122009-01-05 21:25:51 -0500945 * does the dirty work in cow of a single block. The parent block (if
946 * supplied) is updated to point to the new cow copy. The new buffer is marked
947 * dirty and returned locked. If you modify the block it needs to be marked
948 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400949 *
950 * search_start -- an allocation hint for the new block
951 *
Chris Masond3977122009-01-05 21:25:51 -0500952 * empty_size -- a hint that you plan on doing more cow. This is the size in
953 * bytes the allocator should try to find free next to the block it returns.
954 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400955 */
Chris Masond3977122009-01-05 21:25:51 -0500956static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400957 struct btrfs_root *root,
958 struct extent_buffer *buf,
959 struct extent_buffer *parent, int parent_slot,
960 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400961 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400962{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400964 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700965 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400966 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400967 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400969
Chris Mason925baed2008-06-25 16:01:30 -0400970 if (*cow_ret == buf)
971 unlock_orig = 1;
972
Chris Masonb9447ef82009-03-09 11:45:38 -0400973 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400974
Chris Mason7bb86312007-12-11 09:25:06 -0500975 WARN_ON(root->ref_cows && trans->transid !=
976 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400977 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400978
Chris Mason7bb86312007-12-11 09:25:06 -0500979 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400980
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 if (level == 0)
982 btrfs_item_key(buf, &disk_key, 0);
983 else
984 btrfs_node_key(buf, &disk_key, 0);
985
986 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
987 if (parent)
988 parent_start = parent->start;
989 else
990 parent_start = 0;
991 } else
992 parent_start = 0;
993
994 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
995 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200996 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400997 if (IS_ERR(cow))
998 return PTR_ERR(cow);
999
Chris Masonb4ce94d2009-02-04 09:25:08 -05001000 /* cow is set to blocking by btrfs_init_new_buffer */
1001
Chris Mason5f39d392007-10-15 16:14:19 -04001002 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001003 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001004 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1006 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1007 BTRFS_HEADER_FLAG_RELOC);
1008 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1009 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1010 else
1011 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001012
Yan Zheng2b820322008-11-17 21:11:30 -05001013 write_extent_buffer(cow, root->fs_info->fsid,
1014 (unsigned long)btrfs_header_fsid(cow),
1015 BTRFS_FSID_SIZE);
1016
Mark Fashehbe1a5562011-08-08 13:20:18 -07001017 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001018 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001019 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001020 return ret;
1021 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001022
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001023 if (root->ref_cows)
1024 btrfs_reloc_cow_block(trans, root, buf, cow);
1025
Chris Mason6702ed42007-08-07 16:15:09 -04001026 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001027 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001028 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1029 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1030 parent_start = buf->start;
1031 else
1032 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001033
Chris Mason5f39d392007-10-15 16:14:19 -04001034 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +02001035 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -04001036 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001037
Yan, Zhengf0486c62010-05-16 10:46:25 -04001038 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001039 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001041 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001042 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001043 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1044 parent_start = parent->start;
1045 else
1046 parent_start = 0;
1047
1048 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001049 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1050 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001051 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001052 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001053 btrfs_set_node_ptr_generation(parent, parent_slot,
1054 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001055 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001056 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001057 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001058 }
Chris Mason925baed2008-06-25 16:01:30 -04001059 if (unlock_orig)
1060 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001061 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001062 btrfs_mark_buffer_dirty(cow);
1063 *cow_ret = cow;
1064 return 0;
1065}
1066
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001067/*
1068 * returns the logical address of the oldest predecessor of the given root.
1069 * entries older than time_seq are ignored.
1070 */
1071static struct tree_mod_elem *
1072__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
1073 struct btrfs_root *root, u64 time_seq)
1074{
1075 struct tree_mod_elem *tm;
1076 struct tree_mod_elem *found = NULL;
1077 u64 root_logical = root->node->start;
1078 int looped = 0;
1079
1080 if (!time_seq)
1081 return 0;
1082
1083 /*
1084 * the very last operation that's logged for a root is the replacement
1085 * operation (if it is replaced at all). this has the index of the *new*
1086 * root, making it the very first operation that's logged for this root.
1087 */
1088 while (1) {
1089 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1090 time_seq);
1091 if (!looped && !tm)
1092 return 0;
1093 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001094 * if there are no tree operation for the oldest root, we simply
1095 * return it. this should only happen if that (old) root is at
1096 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001097 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001098 if (!tm)
1099 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001100
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001101 /*
1102 * if there's an operation that's not a root replacement, we
1103 * found the oldest version of our root. normally, we'll find a
1104 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1105 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001106 if (tm->op != MOD_LOG_ROOT_REPLACE)
1107 break;
1108
1109 found = tm;
1110 root_logical = tm->old_root.logical;
1111 BUG_ON(root_logical == root->node->start);
1112 looped = 1;
1113 }
1114
Jan Schmidta95236d2012-06-05 16:41:24 +02001115 /* if there's no old root to return, return what we found instead */
1116 if (!found)
1117 found = tm;
1118
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001119 return found;
1120}
1121
1122/*
1123 * tm is a pointer to the first operation to rewind within eb. then, all
1124 * previous operations will be rewinded (until we reach something older than
1125 * time_seq).
1126 */
1127static void
1128__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1129 struct tree_mod_elem *first_tm)
1130{
1131 u32 n;
1132 struct rb_node *next;
1133 struct tree_mod_elem *tm = first_tm;
1134 unsigned long o_dst;
1135 unsigned long o_src;
1136 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1137
1138 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001139 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 /*
1141 * all the operations are recorded with the operator used for
1142 * the modification. as we're going backwards, we do the
1143 * opposite of each operation here.
1144 */
1145 switch (tm->op) {
1146 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1147 BUG_ON(tm->slot < n);
1148 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1149 case MOD_LOG_KEY_REMOVE:
1150 btrfs_set_node_key(eb, &tm->key, tm->slot);
1151 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1152 btrfs_set_node_ptr_generation(eb, tm->slot,
1153 tm->generation);
1154 n++;
1155 break;
1156 case MOD_LOG_KEY_REPLACE:
1157 BUG_ON(tm->slot >= n);
1158 btrfs_set_node_key(eb, &tm->key, tm->slot);
1159 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1160 btrfs_set_node_ptr_generation(eb, tm->slot,
1161 tm->generation);
1162 break;
1163 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001164 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001165 n--;
1166 break;
1167 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001168 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1169 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1170 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001171 tm->move.nr_items * p_size);
1172 break;
1173 case MOD_LOG_ROOT_REPLACE:
1174 /*
1175 * this operation is special. for roots, this must be
1176 * handled explicitly before rewinding.
1177 * for non-roots, this operation may exist if the node
1178 * was a root: root A -> child B; then A gets empty and
1179 * B is promoted to the new root. in the mod log, we'll
1180 * have a root-replace operation for B, a tree block
1181 * that is no root. we simply ignore that operation.
1182 */
1183 break;
1184 }
1185 next = rb_next(&tm->node);
1186 if (!next)
1187 break;
1188 tm = container_of(next, struct tree_mod_elem, node);
1189 if (tm->index != first_tm->index)
1190 break;
1191 }
1192 btrfs_set_header_nritems(eb, n);
1193}
1194
1195static struct extent_buffer *
1196tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1197 u64 time_seq)
1198{
1199 struct extent_buffer *eb_rewin;
1200 struct tree_mod_elem *tm;
1201
1202 if (!time_seq)
1203 return eb;
1204
1205 if (btrfs_header_level(eb) == 0)
1206 return eb;
1207
1208 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1209 if (!tm)
1210 return eb;
1211
1212 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1213 BUG_ON(tm->slot != 0);
1214 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1215 fs_info->tree_root->nodesize);
1216 BUG_ON(!eb_rewin);
1217 btrfs_set_header_bytenr(eb_rewin, eb->start);
1218 btrfs_set_header_backref_rev(eb_rewin,
1219 btrfs_header_backref_rev(eb));
1220 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001221 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001222 } else {
1223 eb_rewin = btrfs_clone_extent_buffer(eb);
1224 BUG_ON(!eb_rewin);
1225 }
1226
1227 extent_buffer_get(eb_rewin);
1228 free_extent_buffer(eb);
1229
1230 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1231
1232 return eb_rewin;
1233}
1234
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001235/*
1236 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1237 * value. If there are no changes, the current root->root_node is returned. If
1238 * anything changed in between, there's a fresh buffer allocated on which the
1239 * rewind operations are done. In any case, the returned buffer is read locked.
1240 * Returns NULL on error (with no locks held).
1241 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001242static inline struct extent_buffer *
1243get_old_root(struct btrfs_root *root, u64 time_seq)
1244{
1245 struct tree_mod_elem *tm;
1246 struct extent_buffer *eb;
Jan Schmidta95236d2012-06-05 16:41:24 +02001247 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001248 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001249 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001250
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001251 eb = btrfs_read_lock_root_node(root);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001252 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1253 if (!tm)
1254 return root->node;
1255
Jan Schmidta95236d2012-06-05 16:41:24 +02001256 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1257 old_root = &tm->old_root;
1258 old_generation = tm->generation;
1259 logical = old_root->logical;
1260 } else {
1261 logical = root->node->start;
1262 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001263
Jan Schmidta95236d2012-06-05 16:41:24 +02001264 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidta95236d2012-06-05 16:41:24 +02001265 if (old_root)
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001266 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidta95236d2012-06-05 16:41:24 +02001267 else
1268 eb = btrfs_clone_extent_buffer(root->node);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001269 btrfs_tree_read_unlock(root->node);
1270 free_extent_buffer(root->node);
1271 if (!eb)
1272 return NULL;
1273 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001274 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001275 btrfs_set_header_bytenr(eb, eb->start);
1276 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1277 btrfs_set_header_owner(eb, root->root_key.objectid);
Jan Schmidta95236d2012-06-05 16:41:24 +02001278 btrfs_set_header_level(eb, old_root->level);
1279 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001281 if (tm)
1282 __tree_mod_log_rewind(eb, time_seq, tm);
1283 else
1284 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001285 extent_buffer_get(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001286
1287 return eb;
1288}
1289
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290static inline int should_cow_block(struct btrfs_trans_handle *trans,
1291 struct btrfs_root *root,
1292 struct extent_buffer *buf)
1293{
Liu Bof1ebcc72011-11-14 20:48:06 -05001294 /* ensure we can see the force_cow */
1295 smp_rmb();
1296
1297 /*
1298 * We do not need to cow a block if
1299 * 1) this block is not created or changed in this transaction;
1300 * 2) this block does not belong to TREE_RELOC tree;
1301 * 3) the root is not forced COW.
1302 *
1303 * What is forced COW:
1304 * when we create snapshot during commiting the transaction,
1305 * after we've finished coping src root, we must COW the shared
1306 * block to ensure the metadata consistency.
1307 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 if (btrfs_header_generation(buf) == trans->transid &&
1309 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1310 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001311 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1312 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 return 0;
1314 return 1;
1315}
1316
Chris Masond352ac62008-09-29 15:18:18 -04001317/*
1318 * cows a single block, see __btrfs_cow_block for the real work.
1319 * This version of it has extra checks so that a block isn't cow'd more than
1320 * once per transaction, as long as it hasn't been written yet
1321 */
Chris Masond3977122009-01-05 21:25:51 -05001322noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001323 struct btrfs_root *root, struct extent_buffer *buf,
1324 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001325 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001326{
Chris Mason6702ed42007-08-07 16:15:09 -04001327 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001328 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001329
Chris Masonccd467d2007-06-28 15:57:36 -04001330 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001331 printk(KERN_CRIT "trans %llu running %llu\n",
1332 (unsigned long long)trans->transid,
1333 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001334 root->fs_info->running_transaction->transid);
1335 WARN_ON(1);
1336 }
1337 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001338 printk(KERN_CRIT "trans %llu running %llu\n",
1339 (unsigned long long)trans->transid,
1340 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001341 WARN_ON(1);
1342 }
Chris Masondc17ff82008-01-08 15:46:30 -05001343
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001344 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001345 *cow_ret = buf;
1346 return 0;
1347 }
Chris Masonc4876852009-02-04 09:24:25 -05001348
Chris Mason0b86a832008-03-24 15:01:56 -04001349 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001350
1351 if (parent)
1352 btrfs_set_lock_blocking(parent);
1353 btrfs_set_lock_blocking(buf);
1354
Chris Masonf510cfe2007-10-15 16:14:48 -04001355 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001356 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001357
1358 trace_btrfs_cow_block(root, buf, *cow_ret);
1359
Chris Masonf510cfe2007-10-15 16:14:48 -04001360 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001361}
1362
Chris Masond352ac62008-09-29 15:18:18 -04001363/*
1364 * helper function for defrag to decide if two blocks pointed to by a
1365 * node are actually close by
1366 */
Chris Mason6b800532007-10-15 16:17:34 -04001367static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001368{
Chris Mason6b800532007-10-15 16:17:34 -04001369 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001370 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001371 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001372 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001373 return 0;
1374}
1375
Chris Mason081e9572007-11-06 10:26:24 -05001376/*
1377 * compare two keys in a memcmp fashion
1378 */
1379static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1380{
1381 struct btrfs_key k1;
1382
1383 btrfs_disk_key_to_cpu(&k1, disk);
1384
Diego Calleja20736ab2009-07-24 11:06:52 -04001385 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001386}
1387
Josef Bacikf3465ca2008-11-12 14:19:50 -05001388/*
1389 * same as comp_keys only with two btrfs_key's
1390 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001392{
1393 if (k1->objectid > k2->objectid)
1394 return 1;
1395 if (k1->objectid < k2->objectid)
1396 return -1;
1397 if (k1->type > k2->type)
1398 return 1;
1399 if (k1->type < k2->type)
1400 return -1;
1401 if (k1->offset > k2->offset)
1402 return 1;
1403 if (k1->offset < k2->offset)
1404 return -1;
1405 return 0;
1406}
Chris Mason081e9572007-11-06 10:26:24 -05001407
Chris Masond352ac62008-09-29 15:18:18 -04001408/*
1409 * this is used by the defrag code to go through all the
1410 * leaves pointed to by a node and reallocate them so that
1411 * disk order is close to key order
1412 */
Chris Mason6702ed42007-08-07 16:15:09 -04001413int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001414 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001415 int start_slot, int cache_only, u64 *last_ret,
1416 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001417{
Chris Mason6b800532007-10-15 16:17:34 -04001418 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001419 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001420 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001421 u64 search_start = *last_ret;
1422 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001423 u64 other;
1424 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001425 int end_slot;
1426 int i;
1427 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001428 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001429 int uptodate;
1430 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001431 int progress_passed = 0;
1432 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001433
Chris Mason5708b952007-10-25 15:43:18 -04001434 parent_level = btrfs_header_level(parent);
1435 if (cache_only && parent_level != 1)
1436 return 0;
1437
Chris Masond3977122009-01-05 21:25:51 -05001438 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001439 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001440 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001441 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001442
Chris Mason6b800532007-10-15 16:17:34 -04001443 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001444 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001445 end_slot = parent_nritems;
1446
1447 if (parent_nritems == 1)
1448 return 0;
1449
Chris Masonb4ce94d2009-02-04 09:25:08 -05001450 btrfs_set_lock_blocking(parent);
1451
Chris Mason6702ed42007-08-07 16:15:09 -04001452 for (i = start_slot; i < end_slot; i++) {
1453 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001454
Chris Mason081e9572007-11-06 10:26:24 -05001455 btrfs_node_key(parent, &disk_key, i);
1456 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1457 continue;
1458
1459 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001460 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001461 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001462 if (last_block == 0)
1463 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001464
Chris Mason6702ed42007-08-07 16:15:09 -04001465 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001466 other = btrfs_node_blockptr(parent, i - 1);
1467 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001468 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001469 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001470 other = btrfs_node_blockptr(parent, i + 1);
1471 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001472 }
Chris Masone9d0b132007-08-10 14:06:19 -04001473 if (close) {
1474 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001475 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001476 }
Chris Mason6702ed42007-08-07 16:15:09 -04001477
Chris Mason6b800532007-10-15 16:17:34 -04001478 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1479 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001480 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001481 else
1482 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001483 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001484 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001485 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001486 continue;
1487 }
Chris Mason6b800532007-10-15 16:17:34 -04001488 if (!cur) {
1489 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001490 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001491 if (!cur)
1492 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001493 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001494 err = btrfs_read_buffer(cur, gen);
1495 if (err) {
1496 free_extent_buffer(cur);
1497 return err;
1498 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001499 }
Chris Mason6702ed42007-08-07 16:15:09 -04001500 }
Chris Masone9d0b132007-08-10 14:06:19 -04001501 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001502 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001503
Chris Masone7a84562008-06-25 16:01:31 -04001504 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001505 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001506 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001507 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001508 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001509 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001510 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001511 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001512 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001513 break;
Yan252c38f2007-08-29 09:11:44 -04001514 }
Chris Masone7a84562008-06-25 16:01:31 -04001515 search_start = cur->start;
1516 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001517 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001518 btrfs_tree_unlock(cur);
1519 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001520 }
1521 return err;
1522}
1523
Chris Mason74123bd2007-02-02 11:05:29 -05001524/*
1525 * The leaf data grows from end-to-front in the node.
1526 * this returns the address of the start of the last item,
1527 * which is the stop of the leaf data stack
1528 */
Chris Mason123abc82007-03-14 14:14:43 -04001529static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001530 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001531{
Chris Mason5f39d392007-10-15 16:14:19 -04001532 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001533 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001534 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001535 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001536}
1537
Chris Masonaa5d6be2007-02-28 16:35:06 -05001538
Chris Mason74123bd2007-02-02 11:05:29 -05001539/*
Chris Mason5f39d392007-10-15 16:14:19 -04001540 * search for key in the extent_buffer. The items start at offset p,
1541 * and they are item_size apart. There are 'max' items in p.
1542 *
Chris Mason74123bd2007-02-02 11:05:29 -05001543 * the slot in the array is returned via slot, and it points to
1544 * the place where you would insert key if it is not found in
1545 * the array.
1546 *
1547 * slot may point to max if the key is bigger than all of the keys
1548 */
Chris Masone02119d2008-09-05 16:13:11 -04001549static noinline int generic_bin_search(struct extent_buffer *eb,
1550 unsigned long p,
1551 int item_size, struct btrfs_key *key,
1552 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001553{
1554 int low = 0;
1555 int high = max;
1556 int mid;
1557 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001558 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001559 struct btrfs_disk_key unaligned;
1560 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001561 char *kaddr = NULL;
1562 unsigned long map_start = 0;
1563 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001564 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001565
Chris Masond3977122009-01-05 21:25:51 -05001566 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001567 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001568 offset = p + mid * item_size;
1569
Chris Masona6591712011-07-19 12:04:14 -04001570 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001571 (offset + sizeof(struct btrfs_disk_key)) >
1572 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001573
1574 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001575 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001576 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001577
Chris Mason479965d2007-10-15 16:14:27 -04001578 if (!err) {
1579 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1580 map_start);
1581 } else {
1582 read_extent_buffer(eb, &unaligned,
1583 offset, sizeof(unaligned));
1584 tmp = &unaligned;
1585 }
1586
Chris Mason5f39d392007-10-15 16:14:19 -04001587 } else {
1588 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1589 map_start);
1590 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001591 ret = comp_keys(tmp, key);
1592
1593 if (ret < 0)
1594 low = mid + 1;
1595 else if (ret > 0)
1596 high = mid;
1597 else {
1598 *slot = mid;
1599 return 0;
1600 }
1601 }
1602 *slot = low;
1603 return 1;
1604}
1605
Chris Mason97571fd2007-02-24 13:39:08 -05001606/*
1607 * simple bin_search frontend that does the right thing for
1608 * leaves vs nodes
1609 */
Chris Mason5f39d392007-10-15 16:14:19 -04001610static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1611 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001613 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001614 return generic_bin_search(eb,
1615 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001616 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001617 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001618 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001619 else
Chris Mason5f39d392007-10-15 16:14:19 -04001620 return generic_bin_search(eb,
1621 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001622 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001623 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001624 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001625}
1626
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001627int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1628 int level, int *slot)
1629{
1630 return bin_search(eb, key, level, slot);
1631}
1632
Yan, Zhengf0486c62010-05-16 10:46:25 -04001633static void root_add_used(struct btrfs_root *root, u32 size)
1634{
1635 spin_lock(&root->accounting_lock);
1636 btrfs_set_root_used(&root->root_item,
1637 btrfs_root_used(&root->root_item) + size);
1638 spin_unlock(&root->accounting_lock);
1639}
1640
1641static void root_sub_used(struct btrfs_root *root, u32 size)
1642{
1643 spin_lock(&root->accounting_lock);
1644 btrfs_set_root_used(&root->root_item,
1645 btrfs_root_used(&root->root_item) - size);
1646 spin_unlock(&root->accounting_lock);
1647}
1648
Chris Masond352ac62008-09-29 15:18:18 -04001649/* given a node and slot number, this reads the blocks it points to. The
1650 * extent buffer is returned with a reference taken (but unlocked).
1651 * NULL is returned on error.
1652 */
Chris Masone02119d2008-09-05 16:13:11 -04001653static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001654 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001655{
Chris Masonca7a79a2008-05-12 12:59:19 -04001656 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001657 if (slot < 0)
1658 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001659 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001660 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001661
1662 BUG_ON(level == 0);
1663
Chris Masondb945352007-10-15 16:15:53 -04001664 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001665 btrfs_level_size(root, level - 1),
1666 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001667}
1668
Chris Masond352ac62008-09-29 15:18:18 -04001669/*
1670 * node level balancing, used to make sure nodes are in proper order for
1671 * item deletion. We balance from the top down, so we have to make sure
1672 * that a deletion won't leave an node completely empty later on.
1673 */
Chris Masone02119d2008-09-05 16:13:11 -04001674static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001675 struct btrfs_root *root,
1676 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001677{
Chris Mason5f39d392007-10-15 16:14:19 -04001678 struct extent_buffer *right = NULL;
1679 struct extent_buffer *mid;
1680 struct extent_buffer *left = NULL;
1681 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001682 int ret = 0;
1683 int wret;
1684 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001685 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001686 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001687
1688 if (level == 0)
1689 return 0;
1690
Chris Mason5f39d392007-10-15 16:14:19 -04001691 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001692
Chris Masonbd681512011-07-16 15:23:14 -04001693 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1694 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001695 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1696
Chris Mason1d4f8a02007-03-13 09:28:32 -04001697 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001698
Li Zefana05a9bb2011-09-06 16:55:34 +08001699 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001700 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001701 pslot = path->slots[level + 1];
1702 }
Chris Masonbb803952007-03-01 12:04:21 -05001703
Chris Mason40689472007-03-17 14:29:23 -04001704 /*
1705 * deal with the case where there is only one pointer in the root
1706 * by promoting the node below to a root
1707 */
Chris Mason5f39d392007-10-15 16:14:19 -04001708 if (!parent) {
1709 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001710
Chris Mason5f39d392007-10-15 16:14:19 -04001711 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001712 return 0;
1713
1714 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001715 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001716 if (!child) {
1717 ret = -EROFS;
1718 btrfs_std_error(root->fs_info, ret);
1719 goto enospc;
1720 }
1721
Chris Mason925baed2008-06-25 16:01:30 -04001722 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001723 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001724 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001725 if (ret) {
1726 btrfs_tree_unlock(child);
1727 free_extent_buffer(child);
1728 goto enospc;
1729 }
Yan2f375ab2008-02-01 14:58:07 -05001730
Jan Schmidtf2304752012-05-26 11:43:17 +02001731 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001732 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001733
Chris Mason0b86a832008-03-24 15:01:56 -04001734 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001735 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001736
Chris Mason925baed2008-06-25 16:01:30 -04001737 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001738 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001739 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001740 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001741 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001742 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001743
1744 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001745 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001746 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001747 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001748 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001749 }
Chris Mason5f39d392007-10-15 16:14:19 -04001750 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001751 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001752 return 0;
1753
Chris Mason5f39d392007-10-15 16:14:19 -04001754 left = read_node_slot(root, parent, pslot - 1);
1755 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001756 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001757 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001758 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001759 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001760 if (wret) {
1761 ret = wret;
1762 goto enospc;
1763 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001764 }
Chris Mason5f39d392007-10-15 16:14:19 -04001765 right = read_node_slot(root, parent, pslot + 1);
1766 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001767 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001769 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001770 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001771 if (wret) {
1772 ret = wret;
1773 goto enospc;
1774 }
1775 }
1776
1777 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001778 if (left) {
1779 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001780 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001781 if (wret < 0)
1782 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001783 }
Chris Mason79f95c82007-03-01 15:16:26 -05001784
1785 /*
1786 * then try to empty the right most buffer into the middle
1787 */
Chris Mason5f39d392007-10-15 16:14:19 -04001788 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001789 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001790 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001791 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001792 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001793 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001794 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001795 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001796 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001797 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001798 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001799 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001800 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001801 struct btrfs_disk_key right_key;
1802 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001803 tree_mod_log_set_node_key(root->fs_info, parent,
1804 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001805 btrfs_set_node_key(parent, &right_key, pslot + 1);
1806 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001807 }
1808 }
Chris Mason5f39d392007-10-15 16:14:19 -04001809 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001810 /*
1811 * we're not allowed to leave a node with one item in the
1812 * tree during a delete. A deletion from lower in the tree
1813 * could try to delete the only pointer in this node.
1814 * So, pull some keys from the left.
1815 * There has to be a left pointer at this point because
1816 * otherwise we would have pulled some pointers from the
1817 * right
1818 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001819 if (!left) {
1820 ret = -EROFS;
1821 btrfs_std_error(root->fs_info, ret);
1822 goto enospc;
1823 }
Chris Mason5f39d392007-10-15 16:14:19 -04001824 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001825 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001826 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001827 goto enospc;
1828 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001829 if (wret == 1) {
1830 wret = push_node_left(trans, root, left, mid, 1);
1831 if (wret < 0)
1832 ret = wret;
1833 }
Chris Mason79f95c82007-03-01 15:16:26 -05001834 BUG_ON(wret == 1);
1835 }
Chris Mason5f39d392007-10-15 16:14:19 -04001836 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001837 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001838 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001839 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001840 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001841 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001842 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001843 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001844 } else {
1845 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001846 struct btrfs_disk_key mid_key;
1847 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001848 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1849 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001850 btrfs_set_node_key(parent, &mid_key, pslot);
1851 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001852 }
Chris Masonbb803952007-03-01 12:04:21 -05001853
Chris Mason79f95c82007-03-01 15:16:26 -05001854 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001855 if (left) {
1856 if (btrfs_header_nritems(left) > orig_slot) {
1857 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001858 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001859 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001860 path->slots[level + 1] -= 1;
1861 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001862 if (mid) {
1863 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001864 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001865 }
Chris Masonbb803952007-03-01 12:04:21 -05001866 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001867 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001868 path->slots[level] = orig_slot;
1869 }
1870 }
Chris Mason79f95c82007-03-01 15:16:26 -05001871 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001872 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001873 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001874 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001875enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001876 if (right) {
1877 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001878 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001879 }
1880 if (left) {
1881 if (path->nodes[level] != left)
1882 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001883 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001884 }
Chris Masonbb803952007-03-01 12:04:21 -05001885 return ret;
1886}
1887
Chris Masond352ac62008-09-29 15:18:18 -04001888/* Node balancing for insertion. Here we only split or push nodes around
1889 * when they are completely full. This is also done top down, so we
1890 * have to be pessimistic.
1891 */
Chris Masond3977122009-01-05 21:25:51 -05001892static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001893 struct btrfs_root *root,
1894 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001895{
Chris Mason5f39d392007-10-15 16:14:19 -04001896 struct extent_buffer *right = NULL;
1897 struct extent_buffer *mid;
1898 struct extent_buffer *left = NULL;
1899 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001900 int ret = 0;
1901 int wret;
1902 int pslot;
1903 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001904
1905 if (level == 0)
1906 return 1;
1907
Chris Mason5f39d392007-10-15 16:14:19 -04001908 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001909 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001910
Li Zefana05a9bb2011-09-06 16:55:34 +08001911 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001912 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001913 pslot = path->slots[level + 1];
1914 }
Chris Masone66f7092007-04-20 13:16:02 -04001915
Chris Mason5f39d392007-10-15 16:14:19 -04001916 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001917 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001918
Chris Mason5f39d392007-10-15 16:14:19 -04001919 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001920
1921 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001922 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001923 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001924
1925 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001926 btrfs_set_lock_blocking(left);
1927
Chris Mason5f39d392007-10-15 16:14:19 -04001928 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001929 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1930 wret = 1;
1931 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001932 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001933 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001934 if (ret)
1935 wret = 1;
1936 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001937 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001938 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001939 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001940 }
Chris Masone66f7092007-04-20 13:16:02 -04001941 if (wret < 0)
1942 ret = wret;
1943 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001944 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001945 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001946 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001947 tree_mod_log_set_node_key(root->fs_info, parent,
1948 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_set_node_key(parent, &disk_key, pslot);
1950 btrfs_mark_buffer_dirty(parent);
1951 if (btrfs_header_nritems(left) > orig_slot) {
1952 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001953 path->slots[level + 1] -= 1;
1954 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001955 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001956 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001957 } else {
1958 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001959 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001960 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001961 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001962 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001963 }
Chris Masone66f7092007-04-20 13:16:02 -04001964 return 0;
1965 }
Chris Mason925baed2008-06-25 16:01:30 -04001966 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001967 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001968 }
Chris Mason925baed2008-06-25 16:01:30 -04001969 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001970
1971 /*
1972 * then try to empty the right most buffer into the middle
1973 */
Chris Mason5f39d392007-10-15 16:14:19 -04001974 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001975 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001976
Chris Mason925baed2008-06-25 16:01:30 -04001977 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001978 btrfs_set_lock_blocking(right);
1979
Chris Mason5f39d392007-10-15 16:14:19 -04001980 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001981 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1982 wret = 1;
1983 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001984 ret = btrfs_cow_block(trans, root, right,
1985 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001986 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001987 if (ret)
1988 wret = 1;
1989 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001990 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001991 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001992 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001993 }
Chris Masone66f7092007-04-20 13:16:02 -04001994 if (wret < 0)
1995 ret = wret;
1996 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001997 struct btrfs_disk_key disk_key;
1998
1999 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002000 tree_mod_log_set_node_key(root->fs_info, parent,
2001 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002002 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2003 btrfs_mark_buffer_dirty(parent);
2004
2005 if (btrfs_header_nritems(mid) <= orig_slot) {
2006 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002007 path->slots[level + 1] += 1;
2008 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002009 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002010 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002012 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002013 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002014 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002015 }
Chris Masone66f7092007-04-20 13:16:02 -04002016 return 0;
2017 }
Chris Mason925baed2008-06-25 16:01:30 -04002018 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002019 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002020 }
Chris Masone66f7092007-04-20 13:16:02 -04002021 return 1;
2022}
2023
Chris Mason74123bd2007-02-02 11:05:29 -05002024/*
Chris Masond352ac62008-09-29 15:18:18 -04002025 * readahead one full node of leaves, finding things that are close
2026 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002027 */
Chris Masonc8c42862009-04-03 10:14:18 -04002028static void reada_for_search(struct btrfs_root *root,
2029 struct btrfs_path *path,
2030 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002031{
Chris Mason5f39d392007-10-15 16:14:19 -04002032 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002033 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002034 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002035 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002036 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002037 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002038 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002039 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002040 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002041 u32 nr;
2042 u32 blocksize;
2043 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002044
Chris Masona6b6e752007-10-15 16:22:39 -04002045 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002046 return;
2047
Chris Mason6702ed42007-08-07 16:15:09 -04002048 if (!path->nodes[level])
2049 return;
2050
Chris Mason5f39d392007-10-15 16:14:19 -04002051 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002052
Chris Mason3c69fae2007-08-07 15:52:22 -04002053 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002054 blocksize = btrfs_level_size(root, level - 1);
2055 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002056 if (eb) {
2057 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002058 return;
2059 }
2060
Chris Masona7175312009-01-22 09:23:10 -05002061 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002062
Chris Mason5f39d392007-10-15 16:14:19 -04002063 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002064 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002065
Chris Masond3977122009-01-05 21:25:51 -05002066 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002067 if (direction < 0) {
2068 if (nr == 0)
2069 break;
2070 nr--;
2071 } else if (direction > 0) {
2072 nr++;
2073 if (nr >= nritems)
2074 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002075 }
Chris Mason01f46652007-12-21 16:24:26 -05002076 if (path->reada < 0 && objectid) {
2077 btrfs_node_key(node, &disk_key, nr);
2078 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2079 break;
2080 }
Chris Mason6b800532007-10-15 16:17:34 -04002081 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002082 if ((search <= target && target - search <= 65536) ||
2083 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002084 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002085 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002086 nread += blocksize;
2087 }
2088 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002089 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002090 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002091 }
2092}
Chris Mason925baed2008-06-25 16:01:30 -04002093
Chris Masond352ac62008-09-29 15:18:18 -04002094/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002095 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2096 * cache
2097 */
2098static noinline int reada_for_balance(struct btrfs_root *root,
2099 struct btrfs_path *path, int level)
2100{
2101 int slot;
2102 int nritems;
2103 struct extent_buffer *parent;
2104 struct extent_buffer *eb;
2105 u64 gen;
2106 u64 block1 = 0;
2107 u64 block2 = 0;
2108 int ret = 0;
2109 int blocksize;
2110
Chris Mason8c594ea2009-04-20 15:50:10 -04002111 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002112 if (!parent)
2113 return 0;
2114
2115 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002116 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002117 blocksize = btrfs_level_size(root, level);
2118
2119 if (slot > 0) {
2120 block1 = btrfs_node_blockptr(parent, slot - 1);
2121 gen = btrfs_node_ptr_generation(parent, slot - 1);
2122 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002123 /*
2124 * if we get -eagain from btrfs_buffer_uptodate, we
2125 * don't want to return eagain here. That will loop
2126 * forever
2127 */
2128 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002129 block1 = 0;
2130 free_extent_buffer(eb);
2131 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002132 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002133 block2 = btrfs_node_blockptr(parent, slot + 1);
2134 gen = btrfs_node_ptr_generation(parent, slot + 1);
2135 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002136 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002137 block2 = 0;
2138 free_extent_buffer(eb);
2139 }
2140 if (block1 || block2) {
2141 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002142
2143 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002144 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002145
2146 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002147 if (block1)
2148 readahead_tree_block(root, block1, blocksize, 0);
2149 if (block2)
2150 readahead_tree_block(root, block2, blocksize, 0);
2151
2152 if (block1) {
2153 eb = read_tree_block(root, block1, blocksize, 0);
2154 free_extent_buffer(eb);
2155 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002156 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002157 eb = read_tree_block(root, block2, blocksize, 0);
2158 free_extent_buffer(eb);
2159 }
2160 }
2161 return ret;
2162}
2163
2164
2165/*
Chris Masond3977122009-01-05 21:25:51 -05002166 * when we walk down the tree, it is usually safe to unlock the higher layers
2167 * in the tree. The exceptions are when our path goes through slot 0, because
2168 * operations on the tree might require changing key pointers higher up in the
2169 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002170 *
Chris Masond3977122009-01-05 21:25:51 -05002171 * callers might also have set path->keep_locks, which tells this code to keep
2172 * the lock if the path points to the last slot in the block. This is part of
2173 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002174 *
Chris Masond3977122009-01-05 21:25:51 -05002175 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2176 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002177 */
Chris Masone02119d2008-09-05 16:13:11 -04002178static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002179 int lowest_unlock, int min_write_lock_level,
2180 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002181{
2182 int i;
2183 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002184 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002185 struct extent_buffer *t;
2186
2187 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2188 if (!path->nodes[i])
2189 break;
2190 if (!path->locks[i])
2191 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002192 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002193 skip_level = i + 1;
2194 continue;
2195 }
Chris Mason051e1b92008-06-25 16:01:30 -04002196 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002197 u32 nritems;
2198 t = path->nodes[i];
2199 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002200 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002201 skip_level = i + 1;
2202 continue;
2203 }
2204 }
Chris Mason051e1b92008-06-25 16:01:30 -04002205 if (skip_level < i && i >= lowest_unlock)
2206 no_skips = 1;
2207
Chris Mason925baed2008-06-25 16:01:30 -04002208 t = path->nodes[i];
2209 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002210 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002211 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002212 if (write_lock_level &&
2213 i > min_write_lock_level &&
2214 i <= *write_lock_level) {
2215 *write_lock_level = i - 1;
2216 }
Chris Mason925baed2008-06-25 16:01:30 -04002217 }
2218 }
2219}
2220
Chris Mason3c69fae2007-08-07 15:52:22 -04002221/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002222 * This releases any locks held in the path starting at level and
2223 * going all the way up to the root.
2224 *
2225 * btrfs_search_slot will keep the lock held on higher nodes in a few
2226 * corner cases, such as COW of the block at slot zero in the node. This
2227 * ignores those rules, and it should only be called when there are no
2228 * more updates to be done higher up in the tree.
2229 */
2230noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2231{
2232 int i;
2233
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002235 return;
2236
2237 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2238 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002239 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002240 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002241 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002242 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002243 path->locks[i] = 0;
2244 }
2245}
2246
2247/*
Chris Masonc8c42862009-04-03 10:14:18 -04002248 * helper function for btrfs_search_slot. The goal is to find a block
2249 * in cache without setting the path to blocking. If we find the block
2250 * we return zero and the path is unchanged.
2251 *
2252 * If we can't find the block, we set the path blocking and do some
2253 * reada. -EAGAIN is returned and the search must be repeated.
2254 */
2255static int
2256read_block_for_search(struct btrfs_trans_handle *trans,
2257 struct btrfs_root *root, struct btrfs_path *p,
2258 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002259 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002260{
2261 u64 blocknr;
2262 u64 gen;
2263 u32 blocksize;
2264 struct extent_buffer *b = *eb_ret;
2265 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002266 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002267
2268 blocknr = btrfs_node_blockptr(b, slot);
2269 gen = btrfs_node_ptr_generation(b, slot);
2270 blocksize = btrfs_level_size(root, level - 1);
2271
2272 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002273 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002274 /* first we do an atomic uptodate check */
2275 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2276 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002277 /*
2278 * we found an up to date block without
2279 * sleeping, return
2280 * right away
2281 */
2282 *eb_ret = tmp;
2283 return 0;
2284 }
2285 /* the pages were up to date, but we failed
2286 * the generation number check. Do a full
2287 * read for the generation number that is correct.
2288 * We must do this without dropping locks so
2289 * we can trust our generation number
2290 */
2291 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002292 btrfs_set_path_blocking(p);
2293
Chris Masonb9fab912012-05-06 07:23:47 -04002294 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002295 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002296 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002297 *eb_ret = tmp;
2298 return 0;
2299 }
2300 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002301 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002302 return -EIO;
2303 }
Chris Masonc8c42862009-04-03 10:14:18 -04002304 }
2305
2306 /*
2307 * reduce lock contention at high levels
2308 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002309 * we read. Don't release the lock on the current
2310 * level because we need to walk this node to figure
2311 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002312 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002313 btrfs_unlock_up_safe(p, level + 1);
2314 btrfs_set_path_blocking(p);
2315
Chris Masoncb449212010-10-24 11:01:27 -04002316 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002317 if (p->reada)
2318 reada_for_search(root, p, level, slot, key->objectid);
2319
David Sterbab3b4aa72011-04-21 01:20:15 +02002320 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002321
2322 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002323 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002324 if (tmp) {
2325 /*
2326 * If the read above didn't mark this buffer up to date,
2327 * it will never end up being up to date. Set ret to EIO now
2328 * and give up so that our caller doesn't loop forever
2329 * on our EAGAINs.
2330 */
Chris Masonb9fab912012-05-06 07:23:47 -04002331 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002332 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002333 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002334 }
2335 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002336}
2337
2338/*
2339 * helper function for btrfs_search_slot. This does all of the checks
2340 * for node-level blocks and does any balancing required based on
2341 * the ins_len.
2342 *
2343 * If no extra work was required, zero is returned. If we had to
2344 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2345 * start over
2346 */
2347static int
2348setup_nodes_for_search(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002350 struct extent_buffer *b, int level, int ins_len,
2351 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002352{
2353 int ret;
2354 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2355 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2356 int sret;
2357
Chris Masonbd681512011-07-16 15:23:14 -04002358 if (*write_lock_level < level + 1) {
2359 *write_lock_level = level + 1;
2360 btrfs_release_path(p);
2361 goto again;
2362 }
2363
Chris Masonc8c42862009-04-03 10:14:18 -04002364 sret = reada_for_balance(root, p, level);
2365 if (sret)
2366 goto again;
2367
2368 btrfs_set_path_blocking(p);
2369 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002370 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002371
2372 BUG_ON(sret > 0);
2373 if (sret) {
2374 ret = sret;
2375 goto done;
2376 }
2377 b = p->nodes[level];
2378 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002379 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002380 int sret;
2381
Chris Masonbd681512011-07-16 15:23:14 -04002382 if (*write_lock_level < level + 1) {
2383 *write_lock_level = level + 1;
2384 btrfs_release_path(p);
2385 goto again;
2386 }
2387
Chris Masonc8c42862009-04-03 10:14:18 -04002388 sret = reada_for_balance(root, p, level);
2389 if (sret)
2390 goto again;
2391
2392 btrfs_set_path_blocking(p);
2393 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002394 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002395
2396 if (sret) {
2397 ret = sret;
2398 goto done;
2399 }
2400 b = p->nodes[level];
2401 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002402 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002403 goto again;
2404 }
2405 BUG_ON(btrfs_header_nritems(b) == 1);
2406 }
2407 return 0;
2408
2409again:
2410 ret = -EAGAIN;
2411done:
2412 return ret;
2413}
2414
2415/*
Chris Mason74123bd2007-02-02 11:05:29 -05002416 * look for key in the tree. path is filled in with nodes along the way
2417 * if key is found, we return zero and you can find the item in the leaf
2418 * level of the path (level 0)
2419 *
2420 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002421 * be inserted, and 1 is returned. If there are other errors during the
2422 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002423 *
2424 * if ins_len > 0, nodes and leaves will be split as we walk down the
2425 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2426 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002427 */
Chris Masone089f052007-03-16 16:20:31 -04002428int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2429 *root, struct btrfs_key *key, struct btrfs_path *p, int
2430 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002431{
Chris Mason5f39d392007-10-15 16:14:19 -04002432 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002433 int slot;
2434 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002435 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002436 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002437 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002438 int root_lock;
2439 /* everything at write_lock_level or lower must be write locked */
2440 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002441 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002442 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002443
Chris Mason6702ed42007-08-07 16:15:09 -04002444 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002445 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002446 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002447
Chris Masonbd681512011-07-16 15:23:14 -04002448 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002449 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002450
Chris Masonbd681512011-07-16 15:23:14 -04002451 /* when we are removing items, we might have to go up to level
2452 * two as we update tree pointers Make sure we keep write
2453 * for those levels as well
2454 */
2455 write_lock_level = 2;
2456 } else if (ins_len > 0) {
2457 /*
2458 * for inserting items, make sure we have a write lock on
2459 * level 1 so we can update keys
2460 */
2461 write_lock_level = 1;
2462 }
2463
2464 if (!cow)
2465 write_lock_level = -1;
2466
2467 if (cow && (p->keep_locks || p->lowest_level))
2468 write_lock_level = BTRFS_MAX_LEVEL;
2469
Chris Masonf7c79f32012-03-19 15:54:38 -04002470 min_write_lock_level = write_lock_level;
2471
Chris Masonbb803952007-03-01 12:04:21 -05002472again:
Chris Masonbd681512011-07-16 15:23:14 -04002473 /*
2474 * we try very hard to do read locks on the root
2475 */
2476 root_lock = BTRFS_READ_LOCK;
2477 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002479 /*
2480 * the commit roots are read only
2481 * so we always do read locks
2482 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 b = root->commit_root;
2484 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002485 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002487 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002489 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002490 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002491 level = btrfs_header_level(b);
2492 } else {
2493 /* we don't know the level of the root node
2494 * until we actually have it read locked
2495 */
2496 b = btrfs_read_lock_root_node(root);
2497 level = btrfs_header_level(b);
2498 if (level <= write_lock_level) {
2499 /* whoops, must trade for write lock */
2500 btrfs_tree_read_unlock(b);
2501 free_extent_buffer(b);
2502 b = btrfs_lock_root_node(root);
2503 root_lock = BTRFS_WRITE_LOCK;
2504
2505 /* the level might have changed, check again */
2506 level = btrfs_header_level(b);
2507 }
2508 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509 }
Chris Masonbd681512011-07-16 15:23:14 -04002510 p->nodes[level] = b;
2511 if (!p->skip_locking)
2512 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002513
Chris Masoneb60cea2007-02-02 09:18:22 -05002514 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002515 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002516
2517 /*
2518 * setup the path here so we can release it under lock
2519 * contention with the cow code
2520 */
Chris Mason02217ed2007-03-02 16:08:05 -05002521 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002522 /*
2523 * if we don't really need to cow this block
2524 * then we don't want to set the path blocking,
2525 * so we test it here
2526 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002528 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002529
Chris Masonb4ce94d2009-02-04 09:25:08 -05002530 btrfs_set_path_blocking(p);
2531
Chris Masonbd681512011-07-16 15:23:14 -04002532 /*
2533 * must have write locks on this node and the
2534 * parent
2535 */
2536 if (level + 1 > write_lock_level) {
2537 write_lock_level = level + 1;
2538 btrfs_release_path(p);
2539 goto again;
2540 }
2541
Yan Zheng33c66f42009-07-22 09:59:00 -04002542 err = btrfs_cow_block(trans, root, b,
2543 p->nodes[level + 1],
2544 p->slots[level + 1], &b);
2545 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002546 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002547 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002548 }
Chris Mason02217ed2007-03-02 16:08:05 -05002549 }
Chris Mason65b51a02008-08-01 15:11:20 -04002550cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002551 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002552
Chris Masoneb60cea2007-02-02 09:18:22 -05002553 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002554 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002555
2556 /*
2557 * we have a lock on b and as long as we aren't changing
2558 * the tree, there is no way to for the items in b to change.
2559 * It is safe to drop the lock on our parent before we
2560 * go through the expensive btree search on b.
2561 *
2562 * If cow is true, then we might be changing slot zero,
2563 * which may require changing the parent. So, we can't
2564 * drop the lock until after we know which slot we're
2565 * operating on.
2566 */
2567 if (!cow)
2568 btrfs_unlock_up_safe(p, level + 1);
2569
Chris Mason5f39d392007-10-15 16:14:19 -04002570 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002571
Chris Mason5f39d392007-10-15 16:14:19 -04002572 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002573 int dec = 0;
2574 if (ret && slot > 0) {
2575 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002577 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002578 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002579 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002580 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002581 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002582 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002583 if (err) {
2584 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002585 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002586 }
Chris Masonc8c42862009-04-03 10:14:18 -04002587 b = p->nodes[level];
2588 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002589
Chris Masonbd681512011-07-16 15:23:14 -04002590 /*
2591 * slot 0 is special, if we change the key
2592 * we have to update the parent pointer
2593 * which means we must have a write lock
2594 * on the parent
2595 */
2596 if (slot == 0 && cow &&
2597 write_lock_level < level + 1) {
2598 write_lock_level = level + 1;
2599 btrfs_release_path(p);
2600 goto again;
2601 }
2602
Chris Masonf7c79f32012-03-19 15:54:38 -04002603 unlock_up(p, level, lowest_unlock,
2604 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002605
Chris Mason925baed2008-06-25 16:01:30 -04002606 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002607 if (dec)
2608 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002609 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002610 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002611
Yan Zheng33c66f42009-07-22 09:59:00 -04002612 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002613 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002614 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002615 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002616 if (err) {
2617 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002618 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002619 }
Chris Mason76a05b32009-05-14 13:24:30 -04002620
Chris Masonb4ce94d2009-02-04 09:25:08 -05002621 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002622 level = btrfs_header_level(b);
2623 if (level <= write_lock_level) {
2624 err = btrfs_try_tree_write_lock(b);
2625 if (!err) {
2626 btrfs_set_path_blocking(p);
2627 btrfs_tree_lock(b);
2628 btrfs_clear_path_blocking(p, b,
2629 BTRFS_WRITE_LOCK);
2630 }
2631 p->locks[level] = BTRFS_WRITE_LOCK;
2632 } else {
2633 err = btrfs_try_tree_read_lock(b);
2634 if (!err) {
2635 btrfs_set_path_blocking(p);
2636 btrfs_tree_read_lock(b);
2637 btrfs_clear_path_blocking(p, b,
2638 BTRFS_READ_LOCK);
2639 }
2640 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002641 }
Chris Masonbd681512011-07-16 15:23:14 -04002642 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002643 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002644 } else {
2645 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002646 if (ins_len > 0 &&
2647 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002648 if (write_lock_level < 1) {
2649 write_lock_level = 1;
2650 btrfs_release_path(p);
2651 goto again;
2652 }
2653
Chris Masonb4ce94d2009-02-04 09:25:08 -05002654 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002655 err = split_leaf(trans, root, key,
2656 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002657 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002658
Yan Zheng33c66f42009-07-22 09:59:00 -04002659 BUG_ON(err > 0);
2660 if (err) {
2661 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002662 goto done;
2663 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002664 }
Chris Mason459931e2008-12-10 09:10:46 -05002665 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002666 unlock_up(p, level, lowest_unlock,
2667 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002668 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002669 }
2670 }
Chris Mason65b51a02008-08-01 15:11:20 -04002671 ret = 1;
2672done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002673 /*
2674 * we don't really know what they plan on doing with the path
2675 * from here on, so for now just mark it as blocking
2676 */
Chris Masonb9473432009-03-13 11:00:37 -04002677 if (!p->leave_spinning)
2678 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002679 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002680 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002681 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002682}
2683
Chris Mason74123bd2007-02-02 11:05:29 -05002684/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002685 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2686 * current state of the tree together with the operations recorded in the tree
2687 * modification log to search for the key in a previous version of this tree, as
2688 * denoted by the time_seq parameter.
2689 *
2690 * Naturally, there is no support for insert, delete or cow operations.
2691 *
2692 * The resulting path and return value will be set up as if we called
2693 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2694 */
2695int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2696 struct btrfs_path *p, u64 time_seq)
2697{
2698 struct extent_buffer *b;
2699 int slot;
2700 int ret;
2701 int err;
2702 int level;
2703 int lowest_unlock = 1;
2704 u8 lowest_level = 0;
2705
2706 lowest_level = p->lowest_level;
2707 WARN_ON(p->nodes[0] != NULL);
2708
2709 if (p->search_commit_root) {
2710 BUG_ON(time_seq);
2711 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2712 }
2713
2714again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002715 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002716 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002717 p->locks[level] = BTRFS_READ_LOCK;
2718
2719 while (b) {
2720 level = btrfs_header_level(b);
2721 p->nodes[level] = b;
2722 btrfs_clear_path_blocking(p, NULL, 0);
2723
2724 /*
2725 * we have a lock on b and as long as we aren't changing
2726 * the tree, there is no way to for the items in b to change.
2727 * It is safe to drop the lock on our parent before we
2728 * go through the expensive btree search on b.
2729 */
2730 btrfs_unlock_up_safe(p, level + 1);
2731
2732 ret = bin_search(b, key, level, &slot);
2733
2734 if (level != 0) {
2735 int dec = 0;
2736 if (ret && slot > 0) {
2737 dec = 1;
2738 slot -= 1;
2739 }
2740 p->slots[level] = slot;
2741 unlock_up(p, level, lowest_unlock, 0, NULL);
2742
2743 if (level == lowest_level) {
2744 if (dec)
2745 p->slots[level]++;
2746 goto done;
2747 }
2748
2749 err = read_block_for_search(NULL, root, p, &b, level,
2750 slot, key, time_seq);
2751 if (err == -EAGAIN)
2752 goto again;
2753 if (err) {
2754 ret = err;
2755 goto done;
2756 }
2757
2758 level = btrfs_header_level(b);
2759 err = btrfs_try_tree_read_lock(b);
2760 if (!err) {
2761 btrfs_set_path_blocking(p);
2762 btrfs_tree_read_lock(b);
2763 btrfs_clear_path_blocking(p, b,
2764 BTRFS_READ_LOCK);
2765 }
2766 p->locks[level] = BTRFS_READ_LOCK;
2767 p->nodes[level] = b;
2768 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2769 if (b != p->nodes[level]) {
2770 btrfs_tree_unlock_rw(p->nodes[level],
2771 p->locks[level]);
2772 p->locks[level] = 0;
2773 p->nodes[level] = b;
2774 }
2775 } else {
2776 p->slots[level] = slot;
2777 unlock_up(p, level, lowest_unlock, 0, NULL);
2778 goto done;
2779 }
2780 }
2781 ret = 1;
2782done:
2783 if (!p->leave_spinning)
2784 btrfs_set_path_blocking(p);
2785 if (ret < 0)
2786 btrfs_release_path(p);
2787
2788 return ret;
2789}
2790
2791/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002792 * helper to use instead of search slot if no exact match is needed but
2793 * instead the next or previous item should be returned.
2794 * When find_higher is true, the next higher item is returned, the next lower
2795 * otherwise.
2796 * When return_any and find_higher are both true, and no higher item is found,
2797 * return the next lower instead.
2798 * When return_any is true and find_higher is false, and no lower item is found,
2799 * return the next higher instead.
2800 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2801 * < 0 on error
2802 */
2803int btrfs_search_slot_for_read(struct btrfs_root *root,
2804 struct btrfs_key *key, struct btrfs_path *p,
2805 int find_higher, int return_any)
2806{
2807 int ret;
2808 struct extent_buffer *leaf;
2809
2810again:
2811 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2812 if (ret <= 0)
2813 return ret;
2814 /*
2815 * a return value of 1 means the path is at the position where the
2816 * item should be inserted. Normally this is the next bigger item,
2817 * but in case the previous item is the last in a leaf, path points
2818 * to the first free slot in the previous leaf, i.e. at an invalid
2819 * item.
2820 */
2821 leaf = p->nodes[0];
2822
2823 if (find_higher) {
2824 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2825 ret = btrfs_next_leaf(root, p);
2826 if (ret <= 0)
2827 return ret;
2828 if (!return_any)
2829 return 1;
2830 /*
2831 * no higher item found, return the next
2832 * lower instead
2833 */
2834 return_any = 0;
2835 find_higher = 0;
2836 btrfs_release_path(p);
2837 goto again;
2838 }
2839 } else {
2840 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2841 /* we're sitting on an invalid slot */
2842 if (p->slots[0] == 0) {
2843 ret = btrfs_prev_leaf(root, p);
2844 if (ret <= 0)
2845 return ret;
2846 if (!return_any)
2847 return 1;
2848 /*
2849 * no lower item found, return the next
2850 * higher instead
2851 */
2852 return_any = 0;
2853 find_higher = 1;
2854 btrfs_release_path(p);
2855 goto again;
2856 }
2857 --p->slots[0];
2858 }
2859 }
2860 return 0;
2861}
2862
2863/*
Chris Mason74123bd2007-02-02 11:05:29 -05002864 * adjust the pointers going up the tree, starting at level
2865 * making sure the right key of each node is points to 'key'.
2866 * This is used after shifting pointers to the left, so it stops
2867 * fixing up pointers when a given leaf/node is not in slot 0 of the
2868 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002869 *
Chris Mason74123bd2007-02-02 11:05:29 -05002870 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002871static void fixup_low_keys(struct btrfs_trans_handle *trans,
2872 struct btrfs_root *root, struct btrfs_path *path,
2873 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002874{
2875 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002876 struct extent_buffer *t;
2877
Chris Mason234b63a2007-03-13 10:46:10 -04002878 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002879 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002880 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002881 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002882 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002883 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002884 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002885 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002886 if (tslot != 0)
2887 break;
2888 }
2889}
2890
Chris Mason74123bd2007-02-02 11:05:29 -05002891/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002892 * update item key.
2893 *
2894 * This function isn't completely safe. It's the caller's responsibility
2895 * that the new key won't break the order
2896 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002897void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2898 struct btrfs_root *root, struct btrfs_path *path,
2899 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002900{
2901 struct btrfs_disk_key disk_key;
2902 struct extent_buffer *eb;
2903 int slot;
2904
2905 eb = path->nodes[0];
2906 slot = path->slots[0];
2907 if (slot > 0) {
2908 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002909 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002910 }
2911 if (slot < btrfs_header_nritems(eb) - 1) {
2912 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002913 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002914 }
2915
2916 btrfs_cpu_key_to_disk(&disk_key, new_key);
2917 btrfs_set_item_key(eb, &disk_key, slot);
2918 btrfs_mark_buffer_dirty(eb);
2919 if (slot == 0)
2920 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002921}
2922
2923/*
Chris Mason74123bd2007-02-02 11:05:29 -05002924 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002925 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002926 *
2927 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2928 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002929 */
Chris Mason98ed5172008-01-03 10:01:48 -05002930static int push_node_left(struct btrfs_trans_handle *trans,
2931 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002932 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002933{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002934 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002935 int src_nritems;
2936 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002937 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002938
Chris Mason5f39d392007-10-15 16:14:19 -04002939 src_nritems = btrfs_header_nritems(src);
2940 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002941 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002942 WARN_ON(btrfs_header_generation(src) != trans->transid);
2943 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002944
Chris Masonbce4eae2008-04-24 14:42:46 -04002945 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002946 return 1;
2947
Chris Masond3977122009-01-05 21:25:51 -05002948 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002949 return 1;
2950
Chris Masonbce4eae2008-04-24 14:42:46 -04002951 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002952 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002953 if (push_items < src_nritems) {
2954 /* leave at least 8 pointers in the node if
2955 * we aren't going to empty it
2956 */
2957 if (src_nritems - push_items < 8) {
2958 if (push_items <= 8)
2959 return 1;
2960 push_items -= 8;
2961 }
2962 }
2963 } else
2964 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002965
Jan Schmidtf2304752012-05-26 11:43:17 +02002966 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2967 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002968 copy_extent_buffer(dst, src,
2969 btrfs_node_key_ptr_offset(dst_nritems),
2970 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002971 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002972
Chris Masonbb803952007-03-01 12:04:21 -05002973 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002974 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2975 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002976 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2977 btrfs_node_key_ptr_offset(push_items),
2978 (src_nritems - push_items) *
2979 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002980 }
Chris Mason5f39d392007-10-15 16:14:19 -04002981 btrfs_set_header_nritems(src, src_nritems - push_items);
2982 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2983 btrfs_mark_buffer_dirty(src);
2984 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002985
Chris Masonbb803952007-03-01 12:04:21 -05002986 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002987}
2988
Chris Mason97571fd2007-02-24 13:39:08 -05002989/*
Chris Mason79f95c82007-03-01 15:16:26 -05002990 * try to push data from one node into the next node right in the
2991 * tree.
2992 *
2993 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2994 * error, and > 0 if there was no room in the right hand block.
2995 *
2996 * this will only push up to 1/2 the contents of the left node over
2997 */
Chris Mason5f39d392007-10-15 16:14:19 -04002998static int balance_node_right(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *root,
3000 struct extent_buffer *dst,
3001 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003002{
Chris Mason79f95c82007-03-01 15:16:26 -05003003 int push_items = 0;
3004 int max_push;
3005 int src_nritems;
3006 int dst_nritems;
3007 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003008
Chris Mason7bb86312007-12-11 09:25:06 -05003009 WARN_ON(btrfs_header_generation(src) != trans->transid);
3010 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3011
Chris Mason5f39d392007-10-15 16:14:19 -04003012 src_nritems = btrfs_header_nritems(src);
3013 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003014 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003015 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003016 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003017
Chris Masond3977122009-01-05 21:25:51 -05003018 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003019 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003020
3021 max_push = src_nritems / 2 + 1;
3022 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003023 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003024 return 1;
Yan252c38f2007-08-29 09:11:44 -04003025
Chris Mason79f95c82007-03-01 15:16:26 -05003026 if (max_push < push_items)
3027 push_items = max_push;
3028
Jan Schmidtf2304752012-05-26 11:43:17 +02003029 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003030 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3031 btrfs_node_key_ptr_offset(0),
3032 (dst_nritems) *
3033 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003034
Jan Schmidtf2304752012-05-26 11:43:17 +02003035 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3036 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003037 copy_extent_buffer(dst, src,
3038 btrfs_node_key_ptr_offset(0),
3039 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003040 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003041
Chris Mason5f39d392007-10-15 16:14:19 -04003042 btrfs_set_header_nritems(src, src_nritems - push_items);
3043 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003044
Chris Mason5f39d392007-10-15 16:14:19 -04003045 btrfs_mark_buffer_dirty(src);
3046 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003047
Chris Mason79f95c82007-03-01 15:16:26 -05003048 return ret;
3049}
3050
3051/*
Chris Mason97571fd2007-02-24 13:39:08 -05003052 * helper function to insert a new root level in the tree.
3053 * A new node is allocated, and a single item is inserted to
3054 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003055 *
3056 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003057 */
Chris Masond3977122009-01-05 21:25:51 -05003058static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003059 struct btrfs_root *root,
3060 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003061{
Chris Mason7bb86312007-12-11 09:25:06 -05003062 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003063 struct extent_buffer *lower;
3064 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003065 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003066 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003067
3068 BUG_ON(path->nodes[level]);
3069 BUG_ON(path->nodes[level-1] != root->node);
3070
Chris Mason7bb86312007-12-11 09:25:06 -05003071 lower = path->nodes[level-1];
3072 if (level == 1)
3073 btrfs_item_key(lower, &lower_key, 0);
3074 else
3075 btrfs_node_key(lower, &lower_key, 0);
3076
Zheng Yan31840ae2008-09-23 13:14:14 -04003077 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003078 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003079 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003080 if (IS_ERR(c))
3081 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003082
Yan, Zhengf0486c62010-05-16 10:46:25 -04003083 root_add_used(root, root->nodesize);
3084
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003085 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003086 btrfs_set_header_nritems(c, 1);
3087 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003088 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003089 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003090 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003091 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003092
Chris Mason5f39d392007-10-15 16:14:19 -04003093 write_extent_buffer(c, root->fs_info->fsid,
3094 (unsigned long)btrfs_header_fsid(c),
3095 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003096
3097 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3098 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3099 BTRFS_UUID_SIZE);
3100
Chris Mason5f39d392007-10-15 16:14:19 -04003101 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003102 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003103 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003104 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003105
3106 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003107
3108 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003109
Chris Mason925baed2008-06-25 16:01:30 -04003110 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02003111 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04003112 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003113
3114 /* the super has an extra ref to root->node */
3115 free_extent_buffer(old);
3116
Chris Mason0b86a832008-03-24 15:01:56 -04003117 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003118 extent_buffer_get(c);
3119 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003120 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003121 path->slots[level] = 0;
3122 return 0;
3123}
3124
Chris Mason74123bd2007-02-02 11:05:29 -05003125/*
3126 * worker function to insert a single pointer in a node.
3127 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003128 *
Chris Mason74123bd2007-02-02 11:05:29 -05003129 * slot and level indicate where you want the key to go, and
3130 * blocknr is the block the key points to.
3131 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003132static void insert_ptr(struct btrfs_trans_handle *trans,
3133 struct btrfs_root *root, struct btrfs_path *path,
3134 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003135 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003136{
Chris Mason5f39d392007-10-15 16:14:19 -04003137 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003138 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003139 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003140
3141 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003142 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003143 lower = path->nodes[level];
3144 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003145 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003146 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003147 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003148 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003149 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3150 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003151 memmove_extent_buffer(lower,
3152 btrfs_node_key_ptr_offset(slot + 1),
3153 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003154 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003155 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003156 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003157 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3158 MOD_LOG_KEY_ADD);
3159 BUG_ON(ret < 0);
3160 }
Chris Mason5f39d392007-10-15 16:14:19 -04003161 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003162 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003163 WARN_ON(trans->transid == 0);
3164 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003165 btrfs_set_header_nritems(lower, nritems + 1);
3166 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003167}
3168
Chris Mason97571fd2007-02-24 13:39:08 -05003169/*
3170 * split the node at the specified level in path in two.
3171 * The path is corrected to point to the appropriate node after the split
3172 *
3173 * Before splitting this tries to make some room in the node by pushing
3174 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003175 *
3176 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003177 */
Chris Masone02119d2008-09-05 16:13:11 -04003178static noinline int split_node(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *root,
3180 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003181{
Chris Mason5f39d392007-10-15 16:14:19 -04003182 struct extent_buffer *c;
3183 struct extent_buffer *split;
3184 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003185 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003186 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003187 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003188
Chris Mason5f39d392007-10-15 16:14:19 -04003189 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003190 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003191 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003192 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003193 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003194 if (ret)
3195 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003196 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003197 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003198 c = path->nodes[level];
3199 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003200 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003201 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003202 if (ret < 0)
3203 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003204 }
Chris Masone66f7092007-04-20 13:16:02 -04003205
Chris Mason5f39d392007-10-15 16:14:19 -04003206 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003207 mid = (c_nritems + 1) / 2;
3208 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003210 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003211 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003212 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003213 if (IS_ERR(split))
3214 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003215
Yan, Zhengf0486c62010-05-16 10:46:25 -04003216 root_add_used(root, root->nodesize);
3217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003218 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003219 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003220 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003221 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003222 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003223 btrfs_set_header_owner(split, root->root_key.objectid);
3224 write_extent_buffer(split, root->fs_info->fsid,
3225 (unsigned long)btrfs_header_fsid(split),
3226 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003227 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3228 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3229 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003230
Jan Schmidtf2304752012-05-26 11:43:17 +02003231 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003232 copy_extent_buffer(split, c,
3233 btrfs_node_key_ptr_offset(0),
3234 btrfs_node_key_ptr_offset(mid),
3235 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3236 btrfs_set_header_nritems(split, c_nritems - mid);
3237 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003238 ret = 0;
3239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 btrfs_mark_buffer_dirty(c);
3241 btrfs_mark_buffer_dirty(split);
3242
Jeff Mahoney143bede2012-03-01 14:56:26 +01003243 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003244 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003245
Chris Mason5de08d72007-02-24 06:24:44 -05003246 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003247 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003248 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003249 free_extent_buffer(c);
3250 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003251 path->slots[level + 1] += 1;
3252 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003253 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003254 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003255 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003256 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003257}
3258
Chris Mason74123bd2007-02-02 11:05:29 -05003259/*
3260 * how many bytes are required to store the items in a leaf. start
3261 * and nr indicate which items in the leaf to check. This totals up the
3262 * space used both by the item structs and the item data
3263 */
Chris Mason5f39d392007-10-15 16:14:19 -04003264static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003265{
3266 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003267 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003268 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003269
3270 if (!nr)
3271 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003272 data_len = btrfs_item_end_nr(l, start);
3273 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003274 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003275 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003276 return data_len;
3277}
3278
Chris Mason74123bd2007-02-02 11:05:29 -05003279/*
Chris Masond4dbff92007-04-04 14:08:15 -04003280 * The space between the end of the leaf items and
3281 * the start of the leaf data. IOW, how much room
3282 * the leaf has left for both items and data
3283 */
Chris Masond3977122009-01-05 21:25:51 -05003284noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003285 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003286{
Chris Mason5f39d392007-10-15 16:14:19 -04003287 int nritems = btrfs_header_nritems(leaf);
3288 int ret;
3289 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3290 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003291 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3292 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003293 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003294 leaf_space_used(leaf, 0, nritems), nritems);
3295 }
3296 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003297}
3298
Chris Mason99d8f832010-07-07 10:51:48 -04003299/*
3300 * min slot controls the lowest index we're willing to push to the
3301 * right. We'll push up to and including min_slot, but no lower
3302 */
Chris Mason44871b12009-03-13 10:04:31 -04003303static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3304 struct btrfs_root *root,
3305 struct btrfs_path *path,
3306 int data_size, int empty,
3307 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003308 int free_space, u32 left_nritems,
3309 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003310{
Chris Mason5f39d392007-10-15 16:14:19 -04003311 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003312 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003313 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003314 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003315 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003316 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003317 int push_space = 0;
3318 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003319 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003320 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003321 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003322 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003323 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003324
Chris Masoncfed81a2012-03-03 07:40:03 -05003325 btrfs_init_map_token(&token);
3326
Chris Mason34a38212007-11-07 13:31:03 -05003327 if (empty)
3328 nr = 0;
3329 else
Chris Mason99d8f832010-07-07 10:51:48 -04003330 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003331
Zheng Yan31840ae2008-09-23 13:14:14 -04003332 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003333 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003334
Chris Mason44871b12009-03-13 10:04:31 -04003335 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003336 i = left_nritems - 1;
3337 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003338 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003339
Zheng Yan31840ae2008-09-23 13:14:14 -04003340 if (!empty && push_items > 0) {
3341 if (path->slots[0] > i)
3342 break;
3343 if (path->slots[0] == i) {
3344 int space = btrfs_leaf_free_space(root, left);
3345 if (space + push_space * 2 > free_space)
3346 break;
3347 }
3348 }
3349
Chris Mason00ec4c52007-02-24 12:47:20 -05003350 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003351 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003352
Chris Masondb945352007-10-15 16:15:53 -04003353 this_item_size = btrfs_item_size(left, item);
3354 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003355 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003356
Chris Mason00ec4c52007-02-24 12:47:20 -05003357 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003358 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003359 if (i == 0)
3360 break;
3361 i--;
Chris Masondb945352007-10-15 16:15:53 -04003362 }
Chris Mason5f39d392007-10-15 16:14:19 -04003363
Chris Mason925baed2008-06-25 16:01:30 -04003364 if (push_items == 0)
3365 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003366
Chris Mason34a38212007-11-07 13:31:03 -05003367 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003368 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003369
Chris Mason00ec4c52007-02-24 12:47:20 -05003370 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003371 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003372
Chris Mason5f39d392007-10-15 16:14:19 -04003373 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003374 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003375
Chris Mason00ec4c52007-02-24 12:47:20 -05003376 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003377 data_end = leaf_data_end(root, right);
3378 memmove_extent_buffer(right,
3379 btrfs_leaf_data(right) + data_end - push_space,
3380 btrfs_leaf_data(right) + data_end,
3381 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3382
Chris Mason00ec4c52007-02-24 12:47:20 -05003383 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003384 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003385 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3386 btrfs_leaf_data(left) + leaf_data_end(root, left),
3387 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003388
3389 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3390 btrfs_item_nr_offset(0),
3391 right_nritems * sizeof(struct btrfs_item));
3392
Chris Mason00ec4c52007-02-24 12:47:20 -05003393 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003394 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3395 btrfs_item_nr_offset(left_nritems - push_items),
3396 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003397
3398 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003399 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003400 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003401 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003402 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003403 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003404 push_space -= btrfs_token_item_size(right, item, &token);
3405 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003406 }
3407
Chris Mason7518a232007-03-12 12:01:18 -04003408 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003410
Chris Mason34a38212007-11-07 13:31:03 -05003411 if (left_nritems)
3412 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003413 else
3414 clean_tree_block(trans, root, left);
3415
Chris Mason5f39d392007-10-15 16:14:19 -04003416 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003417
Chris Mason5f39d392007-10-15 16:14:19 -04003418 btrfs_item_key(right, &disk_key, 0);
3419 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003420 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003421
Chris Mason00ec4c52007-02-24 12:47:20 -05003422 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003423 if (path->slots[0] >= left_nritems) {
3424 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003425 if (btrfs_header_nritems(path->nodes[0]) == 0)
3426 clean_tree_block(trans, root, path->nodes[0]);
3427 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003428 free_extent_buffer(path->nodes[0]);
3429 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003430 path->slots[1] += 1;
3431 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003432 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003433 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003434 }
3435 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003436
3437out_unlock:
3438 btrfs_tree_unlock(right);
3439 free_extent_buffer(right);
3440 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003441}
Chris Mason925baed2008-06-25 16:01:30 -04003442
Chris Mason00ec4c52007-02-24 12:47:20 -05003443/*
Chris Mason44871b12009-03-13 10:04:31 -04003444 * push some data in the path leaf to the right, trying to free up at
3445 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3446 *
3447 * returns 1 if the push failed because the other node didn't have enough
3448 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003449 *
3450 * this will push starting from min_slot to the end of the leaf. It won't
3451 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003452 */
3453static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003454 *root, struct btrfs_path *path,
3455 int min_data_size, int data_size,
3456 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003457{
3458 struct extent_buffer *left = path->nodes[0];
3459 struct extent_buffer *right;
3460 struct extent_buffer *upper;
3461 int slot;
3462 int free_space;
3463 u32 left_nritems;
3464 int ret;
3465
3466 if (!path->nodes[1])
3467 return 1;
3468
3469 slot = path->slots[1];
3470 upper = path->nodes[1];
3471 if (slot >= btrfs_header_nritems(upper) - 1)
3472 return 1;
3473
3474 btrfs_assert_tree_locked(path->nodes[1]);
3475
3476 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003477 if (right == NULL)
3478 return 1;
3479
Chris Mason44871b12009-03-13 10:04:31 -04003480 btrfs_tree_lock(right);
3481 btrfs_set_lock_blocking(right);
3482
3483 free_space = btrfs_leaf_free_space(root, right);
3484 if (free_space < data_size)
3485 goto out_unlock;
3486
3487 /* cow and double check */
3488 ret = btrfs_cow_block(trans, root, right, upper,
3489 slot + 1, &right);
3490 if (ret)
3491 goto out_unlock;
3492
3493 free_space = btrfs_leaf_free_space(root, right);
3494 if (free_space < data_size)
3495 goto out_unlock;
3496
3497 left_nritems = btrfs_header_nritems(left);
3498 if (left_nritems == 0)
3499 goto out_unlock;
3500
Chris Mason99d8f832010-07-07 10:51:48 -04003501 return __push_leaf_right(trans, root, path, min_data_size, empty,
3502 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003503out_unlock:
3504 btrfs_tree_unlock(right);
3505 free_extent_buffer(right);
3506 return 1;
3507}
3508
3509/*
Chris Mason74123bd2007-02-02 11:05:29 -05003510 * push some data in the path leaf to the left, trying to free up at
3511 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003512 *
3513 * max_slot can put a limit on how far into the leaf we'll push items. The
3514 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3515 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003516 */
Chris Mason44871b12009-03-13 10:04:31 -04003517static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3518 struct btrfs_root *root,
3519 struct btrfs_path *path, int data_size,
3520 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003521 int free_space, u32 right_nritems,
3522 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003523{
Chris Mason5f39d392007-10-15 16:14:19 -04003524 struct btrfs_disk_key disk_key;
3525 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003526 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003527 int push_space = 0;
3528 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003529 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003530 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003531 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003532 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003533 u32 this_item_size;
3534 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003535 struct btrfs_map_token token;
3536
3537 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538
Chris Mason34a38212007-11-07 13:31:03 -05003539 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003540 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003541 else
Chris Mason99d8f832010-07-07 10:51:48 -04003542 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003543
3544 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003545 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003546
Zheng Yan31840ae2008-09-23 13:14:14 -04003547 if (!empty && push_items > 0) {
3548 if (path->slots[0] < i)
3549 break;
3550 if (path->slots[0] == i) {
3551 int space = btrfs_leaf_free_space(root, right);
3552 if (space + push_space * 2 > free_space)
3553 break;
3554 }
3555 }
3556
Chris Masonbe0e5c02007-01-26 15:51:26 -05003557 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003558 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003559
3560 this_item_size = btrfs_item_size(right, item);
3561 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003562 break;
Chris Masondb945352007-10-15 16:15:53 -04003563
Chris Masonbe0e5c02007-01-26 15:51:26 -05003564 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003565 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003566 }
Chris Masondb945352007-10-15 16:15:53 -04003567
Chris Masonbe0e5c02007-01-26 15:51:26 -05003568 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003569 ret = 1;
3570 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571 }
Chris Mason34a38212007-11-07 13:31:03 -05003572 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003573 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003574
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003576 copy_extent_buffer(left, right,
3577 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3578 btrfs_item_nr_offset(0),
3579 push_items * sizeof(struct btrfs_item));
3580
Chris Mason123abc82007-03-14 14:14:43 -04003581 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003582 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003583
3584 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003585 leaf_data_end(root, left) - push_space,
3586 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003587 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003588 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003589 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003590 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003591
Chris Masondb945352007-10-15 16:15:53 -04003592 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003593 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003594 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003595
Chris Mason5f39d392007-10-15 16:14:19 -04003596 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003597
Chris Masoncfed81a2012-03-03 07:40:03 -05003598 ioff = btrfs_token_item_offset(left, item, &token);
3599 btrfs_set_token_item_offset(left, item,
3600 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3601 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003602 }
Chris Mason5f39d392007-10-15 16:14:19 -04003603 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604
3605 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003606 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003607 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3608 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003609 WARN_ON(1);
3610 }
Chris Mason5f39d392007-10-15 16:14:19 -04003611
Chris Mason34a38212007-11-07 13:31:03 -05003612 if (push_items < right_nritems) {
3613 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3614 leaf_data_end(root, right);
3615 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3616 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3617 btrfs_leaf_data(right) +
3618 leaf_data_end(root, right), push_space);
3619
3620 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003621 btrfs_item_nr_offset(push_items),
3622 (btrfs_header_nritems(right) - push_items) *
3623 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003624 }
Yaneef1c492007-11-26 10:58:13 -05003625 right_nritems -= push_items;
3626 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003627 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003628 for (i = 0; i < right_nritems; i++) {
3629 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003630
Chris Masoncfed81a2012-03-03 07:40:03 -05003631 push_space = push_space - btrfs_token_item_size(right,
3632 item, &token);
3633 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003634 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003635
Chris Mason5f39d392007-10-15 16:14:19 -04003636 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003637 if (right_nritems)
3638 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003639 else
3640 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003641
Chris Mason5f39d392007-10-15 16:14:19 -04003642 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003643 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003644
3645 /* then fixup the leaf pointer in the path */
3646 if (path->slots[0] < push_items) {
3647 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003648 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003649 free_extent_buffer(path->nodes[0]);
3650 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003651 path->slots[1] -= 1;
3652 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003653 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003654 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003655 path->slots[0] -= push_items;
3656 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003657 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003658 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003659out:
3660 btrfs_tree_unlock(left);
3661 free_extent_buffer(left);
3662 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003663}
3664
Chris Mason74123bd2007-02-02 11:05:29 -05003665/*
Chris Mason44871b12009-03-13 10:04:31 -04003666 * push some data in the path leaf to the left, trying to free up at
3667 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003668 *
3669 * max_slot can put a limit on how far into the leaf we'll push items. The
3670 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3671 * items
Chris Mason44871b12009-03-13 10:04:31 -04003672 */
3673static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003674 *root, struct btrfs_path *path, int min_data_size,
3675 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003676{
3677 struct extent_buffer *right = path->nodes[0];
3678 struct extent_buffer *left;
3679 int slot;
3680 int free_space;
3681 u32 right_nritems;
3682 int ret = 0;
3683
3684 slot = path->slots[1];
3685 if (slot == 0)
3686 return 1;
3687 if (!path->nodes[1])
3688 return 1;
3689
3690 right_nritems = btrfs_header_nritems(right);
3691 if (right_nritems == 0)
3692 return 1;
3693
3694 btrfs_assert_tree_locked(path->nodes[1]);
3695
3696 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003697 if (left == NULL)
3698 return 1;
3699
Chris Mason44871b12009-03-13 10:04:31 -04003700 btrfs_tree_lock(left);
3701 btrfs_set_lock_blocking(left);
3702
3703 free_space = btrfs_leaf_free_space(root, left);
3704 if (free_space < data_size) {
3705 ret = 1;
3706 goto out;
3707 }
3708
3709 /* cow and double check */
3710 ret = btrfs_cow_block(trans, root, left,
3711 path->nodes[1], slot - 1, &left);
3712 if (ret) {
3713 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003714 if (ret == -ENOSPC)
3715 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003716 goto out;
3717 }
3718
3719 free_space = btrfs_leaf_free_space(root, left);
3720 if (free_space < data_size) {
3721 ret = 1;
3722 goto out;
3723 }
3724
Chris Mason99d8f832010-07-07 10:51:48 -04003725 return __push_leaf_left(trans, root, path, min_data_size,
3726 empty, left, free_space, right_nritems,
3727 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003728out:
3729 btrfs_tree_unlock(left);
3730 free_extent_buffer(left);
3731 return ret;
3732}
3733
3734/*
Chris Mason74123bd2007-02-02 11:05:29 -05003735 * split the path's leaf in two, making sure there is at least data_size
3736 * available for the resulting leaf level of the path.
3737 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003738static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3739 struct btrfs_root *root,
3740 struct btrfs_path *path,
3741 struct extent_buffer *l,
3742 struct extent_buffer *right,
3743 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003745 int data_copy_size;
3746 int rt_data_off;
3747 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003748 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003749 struct btrfs_map_token token;
3750
3751 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003752
Chris Mason5f39d392007-10-15 16:14:19 -04003753 nritems = nritems - mid;
3754 btrfs_set_header_nritems(right, nritems);
3755 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3756
3757 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3758 btrfs_item_nr_offset(mid),
3759 nritems * sizeof(struct btrfs_item));
3760
3761 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003762 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3763 data_copy_size, btrfs_leaf_data(l) +
3764 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003765
Chris Mason5f39d392007-10-15 16:14:19 -04003766 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3767 btrfs_item_end_nr(l, mid);
3768
3769 for (i = 0; i < nritems; i++) {
3770 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003771 u32 ioff;
3772
Chris Masoncfed81a2012-03-03 07:40:03 -05003773 ioff = btrfs_token_item_offset(right, item, &token);
3774 btrfs_set_token_item_offset(right, item,
3775 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003776 }
Chris Mason74123bd2007-02-02 11:05:29 -05003777
Chris Mason5f39d392007-10-15 16:14:19 -04003778 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003779 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003780 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003781 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
3783 btrfs_mark_buffer_dirty(right);
3784 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003785 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003786
Chris Masonbe0e5c02007-01-26 15:51:26 -05003787 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003788 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003789 free_extent_buffer(path->nodes[0]);
3790 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003791 path->slots[0] -= mid;
3792 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003793 } else {
3794 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003795 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003796 }
Chris Mason5f39d392007-10-15 16:14:19 -04003797
Chris Masoneb60cea2007-02-02 09:18:22 -05003798 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003799}
3800
3801/*
Chris Mason99d8f832010-07-07 10:51:48 -04003802 * double splits happen when we need to insert a big item in the middle
3803 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3804 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3805 * A B C
3806 *
3807 * We avoid this by trying to push the items on either side of our target
3808 * into the adjacent leaves. If all goes well we can avoid the double split
3809 * completely.
3810 */
3811static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3812 struct btrfs_root *root,
3813 struct btrfs_path *path,
3814 int data_size)
3815{
3816 int ret;
3817 int progress = 0;
3818 int slot;
3819 u32 nritems;
3820
3821 slot = path->slots[0];
3822
3823 /*
3824 * try to push all the items after our slot into the
3825 * right leaf
3826 */
3827 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3828 if (ret < 0)
3829 return ret;
3830
3831 if (ret == 0)
3832 progress++;
3833
3834 nritems = btrfs_header_nritems(path->nodes[0]);
3835 /*
3836 * our goal is to get our slot at the start or end of a leaf. If
3837 * we've done so we're done
3838 */
3839 if (path->slots[0] == 0 || path->slots[0] == nritems)
3840 return 0;
3841
3842 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3843 return 0;
3844
3845 /* try to push all the items before our slot into the next leaf */
3846 slot = path->slots[0];
3847 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3848 if (ret < 0)
3849 return ret;
3850
3851 if (ret == 0)
3852 progress++;
3853
3854 if (progress)
3855 return 0;
3856 return 1;
3857}
3858
3859/*
Chris Mason44871b12009-03-13 10:04:31 -04003860 * split the path's leaf in two, making sure there is at least data_size
3861 * available for the resulting leaf level of the path.
3862 *
3863 * returns 0 if all went well and < 0 on failure.
3864 */
3865static noinline int split_leaf(struct btrfs_trans_handle *trans,
3866 struct btrfs_root *root,
3867 struct btrfs_key *ins_key,
3868 struct btrfs_path *path, int data_size,
3869 int extend)
3870{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003871 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003872 struct extent_buffer *l;
3873 u32 nritems;
3874 int mid;
3875 int slot;
3876 struct extent_buffer *right;
3877 int ret = 0;
3878 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003880 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003881 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003882
Yan, Zhenga5719522009-09-24 09:17:31 -04003883 l = path->nodes[0];
3884 slot = path->slots[0];
3885 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3886 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3887 return -EOVERFLOW;
3888
Chris Mason44871b12009-03-13 10:04:31 -04003889 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003890 if (data_size) {
3891 wret = push_leaf_right(trans, root, path, data_size,
3892 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003893 if (wret < 0)
3894 return wret;
3895 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003896 wret = push_leaf_left(trans, root, path, data_size,
3897 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003898 if (wret < 0)
3899 return wret;
3900 }
3901 l = path->nodes[0];
3902
3903 /* did the pushes work? */
3904 if (btrfs_leaf_free_space(root, l) >= data_size)
3905 return 0;
3906 }
3907
3908 if (!path->nodes[1]) {
3909 ret = insert_new_root(trans, root, path, 1);
3910 if (ret)
3911 return ret;
3912 }
3913again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003915 l = path->nodes[0];
3916 slot = path->slots[0];
3917 nritems = btrfs_header_nritems(l);
3918 mid = (nritems + 1) / 2;
3919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920 if (mid <= slot) {
3921 if (nritems == 1 ||
3922 leaf_space_used(l, mid, nritems - mid) + data_size >
3923 BTRFS_LEAF_DATA_SIZE(root)) {
3924 if (slot >= nritems) {
3925 split = 0;
3926 } else {
3927 mid = slot;
3928 if (mid != nritems &&
3929 leaf_space_used(l, mid, nritems - mid) +
3930 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003931 if (data_size && !tried_avoid_double)
3932 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003933 split = 2;
3934 }
3935 }
3936 }
3937 } else {
3938 if (leaf_space_used(l, 0, mid) + data_size >
3939 BTRFS_LEAF_DATA_SIZE(root)) {
3940 if (!extend && data_size && slot == 0) {
3941 split = 0;
3942 } else if ((extend || !data_size) && slot == 0) {
3943 mid = 1;
3944 } else {
3945 mid = slot;
3946 if (mid != nritems &&
3947 leaf_space_used(l, mid, nritems - mid) +
3948 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003949 if (data_size && !tried_avoid_double)
3950 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003951 split = 2 ;
3952 }
3953 }
3954 }
3955 }
3956
3957 if (split == 0)
3958 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3959 else
3960 btrfs_item_key(l, &disk_key, mid);
3961
3962 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003963 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003964 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003965 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003966 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003967
3968 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003969
3970 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3971 btrfs_set_header_bytenr(right, right->start);
3972 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003973 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003974 btrfs_set_header_owner(right, root->root_key.objectid);
3975 btrfs_set_header_level(right, 0);
3976 write_extent_buffer(right, root->fs_info->fsid,
3977 (unsigned long)btrfs_header_fsid(right),
3978 BTRFS_FSID_SIZE);
3979
3980 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3981 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3982 BTRFS_UUID_SIZE);
3983
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003984 if (split == 0) {
3985 if (mid <= slot) {
3986 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003987 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003988 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003989 btrfs_tree_unlock(path->nodes[0]);
3990 free_extent_buffer(path->nodes[0]);
3991 path->nodes[0] = right;
3992 path->slots[0] = 0;
3993 path->slots[1] += 1;
3994 } else {
3995 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003996 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003997 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003998 btrfs_tree_unlock(path->nodes[0]);
3999 free_extent_buffer(path->nodes[0]);
4000 path->nodes[0] = right;
4001 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004002 if (path->slots[1] == 0)
4003 fixup_low_keys(trans, root, path,
4004 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004005 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004006 btrfs_mark_buffer_dirty(right);
4007 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004008 }
4009
Jeff Mahoney143bede2012-03-01 14:56:26 +01004010 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004013 BUG_ON(num_doubles != 0);
4014 num_doubles++;
4015 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004016 }
Chris Mason44871b12009-03-13 10:04:31 -04004017
Jeff Mahoney143bede2012-03-01 14:56:26 +01004018 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004019
4020push_for_double:
4021 push_for_double_split(trans, root, path, data_size);
4022 tried_avoid_double = 1;
4023 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4024 return 0;
4025 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004026}
4027
Yan, Zhengad48fd752009-11-12 09:33:58 +00004028static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4029 struct btrfs_root *root,
4030 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004031{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004032 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004033 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004034 struct btrfs_file_extent_item *fi;
4035 u64 extent_len = 0;
4036 u32 item_size;
4037 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004038
4039 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004040 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4041
4042 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4043 key.type != BTRFS_EXTENT_CSUM_KEY);
4044
4045 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4046 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004047
4048 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004049 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4050 fi = btrfs_item_ptr(leaf, path->slots[0],
4051 struct btrfs_file_extent_item);
4052 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4053 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004054 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004055
Chris Mason459931e2008-12-10 09:10:46 -05004056 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004057 path->search_for_split = 1;
4058 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004059 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004060 if (ret < 0)
4061 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004062
Yan, Zhengad48fd752009-11-12 09:33:58 +00004063 ret = -EAGAIN;
4064 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004065 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004066 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4067 goto err;
4068
Chris Mason109f6ae2010-04-02 09:20:18 -04004069 /* the leaf has changed, it now has room. return now */
4070 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4071 goto err;
4072
Yan, Zhengad48fd752009-11-12 09:33:58 +00004073 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4074 fi = btrfs_item_ptr(leaf, path->slots[0],
4075 struct btrfs_file_extent_item);
4076 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4077 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004078 }
4079
Chris Masonb9473432009-03-13 11:00:37 -04004080 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004081 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004082 if (ret)
4083 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004084
Yan, Zhengad48fd752009-11-12 09:33:58 +00004085 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004086 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004087 return 0;
4088err:
4089 path->keep_locks = 0;
4090 return ret;
4091}
4092
4093static noinline int split_item(struct btrfs_trans_handle *trans,
4094 struct btrfs_root *root,
4095 struct btrfs_path *path,
4096 struct btrfs_key *new_key,
4097 unsigned long split_offset)
4098{
4099 struct extent_buffer *leaf;
4100 struct btrfs_item *item;
4101 struct btrfs_item *new_item;
4102 int slot;
4103 char *buf;
4104 u32 nritems;
4105 u32 item_size;
4106 u32 orig_offset;
4107 struct btrfs_disk_key disk_key;
4108
Chris Masonb9473432009-03-13 11:00:37 -04004109 leaf = path->nodes[0];
4110 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4111
Chris Masonb4ce94d2009-02-04 09:25:08 -05004112 btrfs_set_path_blocking(path);
4113
Chris Mason459931e2008-12-10 09:10:46 -05004114 item = btrfs_item_nr(leaf, path->slots[0]);
4115 orig_offset = btrfs_item_offset(leaf, item);
4116 item_size = btrfs_item_size(leaf, item);
4117
Chris Mason459931e2008-12-10 09:10:46 -05004118 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004119 if (!buf)
4120 return -ENOMEM;
4121
Chris Mason459931e2008-12-10 09:10:46 -05004122 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4123 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004124
Chris Mason459931e2008-12-10 09:10:46 -05004125 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004126 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004127 if (slot != nritems) {
4128 /* shift the items */
4129 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004130 btrfs_item_nr_offset(slot),
4131 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004132 }
4133
4134 btrfs_cpu_key_to_disk(&disk_key, new_key);
4135 btrfs_set_item_key(leaf, &disk_key, slot);
4136
4137 new_item = btrfs_item_nr(leaf, slot);
4138
4139 btrfs_set_item_offset(leaf, new_item, orig_offset);
4140 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4141
4142 btrfs_set_item_offset(leaf, item,
4143 orig_offset + item_size - split_offset);
4144 btrfs_set_item_size(leaf, item, split_offset);
4145
4146 btrfs_set_header_nritems(leaf, nritems + 1);
4147
4148 /* write the data for the start of the original item */
4149 write_extent_buffer(leaf, buf,
4150 btrfs_item_ptr_offset(leaf, path->slots[0]),
4151 split_offset);
4152
4153 /* write the data for the new item */
4154 write_extent_buffer(leaf, buf + split_offset,
4155 btrfs_item_ptr_offset(leaf, slot),
4156 item_size - split_offset);
4157 btrfs_mark_buffer_dirty(leaf);
4158
Yan, Zhengad48fd752009-11-12 09:33:58 +00004159 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004160 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004161 return 0;
4162}
4163
4164/*
4165 * This function splits a single item into two items,
4166 * giving 'new_key' to the new item and splitting the
4167 * old one at split_offset (from the start of the item).
4168 *
4169 * The path may be released by this operation. After
4170 * the split, the path is pointing to the old item. The
4171 * new item is going to be in the same node as the old one.
4172 *
4173 * Note, the item being split must be smaller enough to live alone on
4174 * a tree block with room for one extra struct btrfs_item
4175 *
4176 * This allows us to split the item in place, keeping a lock on the
4177 * leaf the entire time.
4178 */
4179int btrfs_split_item(struct btrfs_trans_handle *trans,
4180 struct btrfs_root *root,
4181 struct btrfs_path *path,
4182 struct btrfs_key *new_key,
4183 unsigned long split_offset)
4184{
4185 int ret;
4186 ret = setup_leaf_for_split(trans, root, path,
4187 sizeof(struct btrfs_item));
4188 if (ret)
4189 return ret;
4190
4191 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004192 return ret;
4193}
4194
4195/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004196 * This function duplicate a item, giving 'new_key' to the new item.
4197 * It guarantees both items live in the same tree leaf and the new item
4198 * is contiguous with the original item.
4199 *
4200 * This allows us to split file extent in place, keeping a lock on the
4201 * leaf the entire time.
4202 */
4203int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4204 struct btrfs_root *root,
4205 struct btrfs_path *path,
4206 struct btrfs_key *new_key)
4207{
4208 struct extent_buffer *leaf;
4209 int ret;
4210 u32 item_size;
4211
4212 leaf = path->nodes[0];
4213 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4214 ret = setup_leaf_for_split(trans, root, path,
4215 item_size + sizeof(struct btrfs_item));
4216 if (ret)
4217 return ret;
4218
4219 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004220 setup_items_for_insert(trans, root, path, new_key, &item_size,
4221 item_size, item_size +
4222 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004223 leaf = path->nodes[0];
4224 memcpy_extent_buffer(leaf,
4225 btrfs_item_ptr_offset(leaf, path->slots[0]),
4226 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4227 item_size);
4228 return 0;
4229}
4230
4231/*
Chris Masond352ac62008-09-29 15:18:18 -04004232 * make the item pointed to by the path smaller. new_size indicates
4233 * how small to make it, and from_end tells us if we just chop bytes
4234 * off the end of the item or if we shift the item to chop bytes off
4235 * the front.
4236 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004237void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4238 struct btrfs_root *root,
4239 struct btrfs_path *path,
4240 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004241{
Chris Masonb18c6682007-04-17 13:26:50 -04004242 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004243 struct extent_buffer *leaf;
4244 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004245 u32 nritems;
4246 unsigned int data_end;
4247 unsigned int old_data_start;
4248 unsigned int old_size;
4249 unsigned int size_diff;
4250 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004251 struct btrfs_map_token token;
4252
4253 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004254
Chris Mason5f39d392007-10-15 16:14:19 -04004255 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004256 slot = path->slots[0];
4257
4258 old_size = btrfs_item_size_nr(leaf, slot);
4259 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004260 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004261
Chris Mason5f39d392007-10-15 16:14:19 -04004262 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004263 data_end = leaf_data_end(root, leaf);
4264
Chris Mason5f39d392007-10-15 16:14:19 -04004265 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004266
Chris Masonb18c6682007-04-17 13:26:50 -04004267 size_diff = old_size - new_size;
4268
4269 BUG_ON(slot < 0);
4270 BUG_ON(slot >= nritems);
4271
4272 /*
4273 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4274 */
4275 /* first correct the data pointers */
4276 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004277 u32 ioff;
4278 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004279
Chris Masoncfed81a2012-03-03 07:40:03 -05004280 ioff = btrfs_token_item_offset(leaf, item, &token);
4281 btrfs_set_token_item_offset(leaf, item,
4282 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004283 }
Chris Masondb945352007-10-15 16:15:53 -04004284
Chris Masonb18c6682007-04-17 13:26:50 -04004285 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004286 if (from_end) {
4287 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4288 data_end + size_diff, btrfs_leaf_data(leaf) +
4289 data_end, old_data_start + new_size - data_end);
4290 } else {
4291 struct btrfs_disk_key disk_key;
4292 u64 offset;
4293
4294 btrfs_item_key(leaf, &disk_key, slot);
4295
4296 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4297 unsigned long ptr;
4298 struct btrfs_file_extent_item *fi;
4299
4300 fi = btrfs_item_ptr(leaf, slot,
4301 struct btrfs_file_extent_item);
4302 fi = (struct btrfs_file_extent_item *)(
4303 (unsigned long)fi - size_diff);
4304
4305 if (btrfs_file_extent_type(leaf, fi) ==
4306 BTRFS_FILE_EXTENT_INLINE) {
4307 ptr = btrfs_item_ptr_offset(leaf, slot);
4308 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004309 (unsigned long)fi,
4310 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004311 disk_bytenr));
4312 }
4313 }
4314
4315 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4316 data_end + size_diff, btrfs_leaf_data(leaf) +
4317 data_end, old_data_start - data_end);
4318
4319 offset = btrfs_disk_key_offset(&disk_key);
4320 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4321 btrfs_set_item_key(leaf, &disk_key, slot);
4322 if (slot == 0)
4323 fixup_low_keys(trans, root, path, &disk_key, 1);
4324 }
Chris Mason5f39d392007-10-15 16:14:19 -04004325
4326 item = btrfs_item_nr(leaf, slot);
4327 btrfs_set_item_size(leaf, item, new_size);
4328 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004329
Chris Mason5f39d392007-10-15 16:14:19 -04004330 if (btrfs_leaf_free_space(root, leaf) < 0) {
4331 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004332 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004333 }
Chris Masonb18c6682007-04-17 13:26:50 -04004334}
4335
Chris Masond352ac62008-09-29 15:18:18 -04004336/*
4337 * make the item pointed to by the path bigger, data_size is the new size.
4338 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004339void btrfs_extend_item(struct btrfs_trans_handle *trans,
4340 struct btrfs_root *root, struct btrfs_path *path,
4341 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004342{
Chris Mason6567e832007-04-16 09:22:45 -04004343 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004344 struct extent_buffer *leaf;
4345 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004346 u32 nritems;
4347 unsigned int data_end;
4348 unsigned int old_data;
4349 unsigned int old_size;
4350 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004351 struct btrfs_map_token token;
4352
4353 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004354
Chris Mason5f39d392007-10-15 16:14:19 -04004355 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004356
Chris Mason5f39d392007-10-15 16:14:19 -04004357 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004358 data_end = leaf_data_end(root, leaf);
4359
Chris Mason5f39d392007-10-15 16:14:19 -04004360 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4361 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004362 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004363 }
Chris Mason6567e832007-04-16 09:22:45 -04004364 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004365 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004366
4367 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004368 if (slot >= nritems) {
4369 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004370 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4371 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004372 BUG_ON(1);
4373 }
Chris Mason6567e832007-04-16 09:22:45 -04004374
4375 /*
4376 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4377 */
4378 /* first correct the data pointers */
4379 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004380 u32 ioff;
4381 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004382
Chris Masoncfed81a2012-03-03 07:40:03 -05004383 ioff = btrfs_token_item_offset(leaf, item, &token);
4384 btrfs_set_token_item_offset(leaf, item,
4385 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004386 }
Chris Mason5f39d392007-10-15 16:14:19 -04004387
Chris Mason6567e832007-04-16 09:22:45 -04004388 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004389 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004390 data_end - data_size, btrfs_leaf_data(leaf) +
4391 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004392
Chris Mason6567e832007-04-16 09:22:45 -04004393 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004394 old_size = btrfs_item_size_nr(leaf, slot);
4395 item = btrfs_item_nr(leaf, slot);
4396 btrfs_set_item_size(leaf, item, old_size + data_size);
4397 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004398
Chris Mason5f39d392007-10-15 16:14:19 -04004399 if (btrfs_leaf_free_space(root, leaf) < 0) {
4400 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004401 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004402 }
Chris Mason6567e832007-04-16 09:22:45 -04004403}
4404
Chris Mason74123bd2007-02-02 11:05:29 -05004405/*
Chris Masond352ac62008-09-29 15:18:18 -04004406 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004407 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004408 * Returns the number of keys that were inserted.
4409 */
4410int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4411 struct btrfs_root *root,
4412 struct btrfs_path *path,
4413 struct btrfs_key *cpu_key, u32 *data_size,
4414 int nr)
4415{
4416 struct extent_buffer *leaf;
4417 struct btrfs_item *item;
4418 int ret = 0;
4419 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004420 int i;
4421 u32 nritems;
4422 u32 total_data = 0;
4423 u32 total_size = 0;
4424 unsigned int data_end;
4425 struct btrfs_disk_key disk_key;
4426 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004427 struct btrfs_map_token token;
4428
4429 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004430
Yan Zheng87b29b22008-12-17 10:21:48 -05004431 for (i = 0; i < nr; i++) {
4432 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4433 BTRFS_LEAF_DATA_SIZE(root)) {
4434 break;
4435 nr = i;
4436 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004437 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004438 total_size += data_size[i] + sizeof(struct btrfs_item);
4439 }
4440 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004441
Josef Bacikf3465ca2008-11-12 14:19:50 -05004442 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4443 if (ret == 0)
4444 return -EEXIST;
4445 if (ret < 0)
4446 goto out;
4447
Josef Bacikf3465ca2008-11-12 14:19:50 -05004448 leaf = path->nodes[0];
4449
4450 nritems = btrfs_header_nritems(leaf);
4451 data_end = leaf_data_end(root, leaf);
4452
4453 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4454 for (i = nr; i >= 0; i--) {
4455 total_data -= data_size[i];
4456 total_size -= data_size[i] + sizeof(struct btrfs_item);
4457 if (total_size < btrfs_leaf_free_space(root, leaf))
4458 break;
4459 }
4460 nr = i;
4461 }
4462
4463 slot = path->slots[0];
4464 BUG_ON(slot < 0);
4465
4466 if (slot != nritems) {
4467 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4468
4469 item = btrfs_item_nr(leaf, slot);
4470 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4471
4472 /* figure out how many keys we can insert in here */
4473 total_data = data_size[0];
4474 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004475 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004476 break;
4477 total_data += data_size[i];
4478 }
4479 nr = i;
4480
4481 if (old_data < data_end) {
4482 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004483 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004484 slot, old_data, data_end);
4485 BUG_ON(1);
4486 }
4487 /*
4488 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4489 */
4490 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004491 for (i = slot; i < nritems; i++) {
4492 u32 ioff;
4493
4494 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004495 ioff = btrfs_token_item_offset(leaf, item, &token);
4496 btrfs_set_token_item_offset(leaf, item,
4497 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004498 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004499 /* shift the items */
4500 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4501 btrfs_item_nr_offset(slot),
4502 (nritems - slot) * sizeof(struct btrfs_item));
4503
4504 /* shift the data */
4505 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4506 data_end - total_data, btrfs_leaf_data(leaf) +
4507 data_end, old_data - data_end);
4508 data_end = old_data;
4509 } else {
4510 /*
4511 * this sucks but it has to be done, if we are inserting at
4512 * the end of the leaf only insert 1 of the items, since we
4513 * have no way of knowing whats on the next leaf and we'd have
4514 * to drop our current locks to figure it out
4515 */
4516 nr = 1;
4517 }
4518
4519 /* setup the item for the new data */
4520 for (i = 0; i < nr; i++) {
4521 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4522 btrfs_set_item_key(leaf, &disk_key, slot + i);
4523 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004524 btrfs_set_token_item_offset(leaf, item,
4525 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004526 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004527 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004528 }
4529 btrfs_set_header_nritems(leaf, nritems + nr);
4530 btrfs_mark_buffer_dirty(leaf);
4531
4532 ret = 0;
4533 if (slot == 0) {
4534 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004535 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004536 }
4537
4538 if (btrfs_leaf_free_space(root, leaf) < 0) {
4539 btrfs_print_leaf(root, leaf);
4540 BUG();
4541 }
4542out:
4543 if (!ret)
4544 ret = nr;
4545 return ret;
4546}
4547
4548/*
Chris Mason44871b12009-03-13 10:04:31 -04004549 * this is a helper for btrfs_insert_empty_items, the main goal here is
4550 * to save stack depth by doing the bulk of the work in a function
4551 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004552 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004553void setup_items_for_insert(struct btrfs_trans_handle *trans,
4554 struct btrfs_root *root, struct btrfs_path *path,
4555 struct btrfs_key *cpu_key, u32 *data_size,
4556 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004557{
Chris Mason5f39d392007-10-15 16:14:19 -04004558 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004559 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004560 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004561 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004562 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004563 struct extent_buffer *leaf;
4564 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004565 struct btrfs_map_token token;
4566
4567 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004568
Chris Mason5f39d392007-10-15 16:14:19 -04004569 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004570 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004571
Chris Mason5f39d392007-10-15 16:14:19 -04004572 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004573 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004574
Chris Masonf25956c2008-09-12 15:32:53 -04004575 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004576 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004577 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004578 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004579 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004580 }
Chris Mason5f39d392007-10-15 16:14:19 -04004581
Chris Masonbe0e5c02007-01-26 15:51:26 -05004582 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004583 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004584
Chris Mason5f39d392007-10-15 16:14:19 -04004585 if (old_data < data_end) {
4586 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004587 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004588 slot, old_data, data_end);
4589 BUG_ON(1);
4590 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004591 /*
4592 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4593 */
4594 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004595 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004596 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004597
Chris Mason5f39d392007-10-15 16:14:19 -04004598 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004599 ioff = btrfs_token_item_offset(leaf, item, &token);
4600 btrfs_set_token_item_offset(leaf, item,
4601 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004602 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004603 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004604 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004605 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004606 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004607
4608 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004609 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004610 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004611 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004612 data_end = old_data;
4613 }
Chris Mason5f39d392007-10-15 16:14:19 -04004614
Chris Mason62e27492007-03-15 12:56:47 -04004615 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004616 for (i = 0; i < nr; i++) {
4617 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4618 btrfs_set_item_key(leaf, &disk_key, slot + i);
4619 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004620 btrfs_set_token_item_offset(leaf, item,
4621 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004622 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004623 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004624 }
Chris Mason44871b12009-03-13 10:04:31 -04004625
Chris Mason9c583092008-01-29 15:15:18 -05004626 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004627
Chris Mason5a01a2e2008-01-30 11:43:54 -05004628 if (slot == 0) {
4629 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004630 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004631 }
Chris Masonb9473432009-03-13 11:00:37 -04004632 btrfs_unlock_up_safe(path, 1);
4633 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004634
Chris Mason5f39d392007-10-15 16:14:19 -04004635 if (btrfs_leaf_free_space(root, leaf) < 0) {
4636 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004637 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004638 }
Chris Mason44871b12009-03-13 10:04:31 -04004639}
4640
4641/*
4642 * Given a key and some data, insert items into the tree.
4643 * This does all the path init required, making room in the tree if needed.
4644 */
4645int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4646 struct btrfs_root *root,
4647 struct btrfs_path *path,
4648 struct btrfs_key *cpu_key, u32 *data_size,
4649 int nr)
4650{
Chris Mason44871b12009-03-13 10:04:31 -04004651 int ret = 0;
4652 int slot;
4653 int i;
4654 u32 total_size = 0;
4655 u32 total_data = 0;
4656
4657 for (i = 0; i < nr; i++)
4658 total_data += data_size[i];
4659
4660 total_size = total_data + (nr * sizeof(struct btrfs_item));
4661 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4662 if (ret == 0)
4663 return -EEXIST;
4664 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004665 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004666
Chris Mason44871b12009-03-13 10:04:31 -04004667 slot = path->slots[0];
4668 BUG_ON(slot < 0);
4669
Jeff Mahoney143bede2012-03-01 14:56:26 +01004670 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004671 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004672 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004673}
4674
4675/*
4676 * Given a key and some data, insert an item into the tree.
4677 * This does all the path init required, making room in the tree if needed.
4678 */
Chris Masone089f052007-03-16 16:20:31 -04004679int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4680 *root, struct btrfs_key *cpu_key, void *data, u32
4681 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004682{
4683 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004684 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004685 struct extent_buffer *leaf;
4686 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004687
Chris Mason2c90e5d2007-04-02 10:50:19 -04004688 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004689 if (!path)
4690 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004691 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004692 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004693 leaf = path->nodes[0];
4694 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4695 write_extent_buffer(leaf, data, ptr, data_size);
4696 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004697 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004698 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004699 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004700}
4701
Chris Mason74123bd2007-02-02 11:05:29 -05004702/*
Chris Mason5de08d72007-02-24 06:24:44 -05004703 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004704 *
Chris Masond352ac62008-09-29 15:18:18 -04004705 * the tree should have been previously balanced so the deletion does not
4706 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004707 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004708static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004709 struct btrfs_path *path, int level, int slot,
4710 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004711{
Chris Mason5f39d392007-10-15 16:14:19 -04004712 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004713 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004714 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715
Chris Mason5f39d392007-10-15 16:14:19 -04004716 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004717 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004718 if (tree_mod_log && level)
4719 tree_mod_log_eb_move(root->fs_info, parent, slot,
4720 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004721 memmove_extent_buffer(parent,
4722 btrfs_node_key_ptr_offset(slot),
4723 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004724 sizeof(struct btrfs_key_ptr) *
4725 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004726 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004727 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4728 MOD_LOG_KEY_REMOVE);
4729 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004730 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004731
Chris Mason7518a232007-03-12 12:01:18 -04004732 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004733 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004734 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004735 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004736 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004737 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004738 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004739 struct btrfs_disk_key disk_key;
4740
4741 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004742 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004743 }
Chris Masond6025572007-03-30 14:27:56 -04004744 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004745}
4746
Chris Mason74123bd2007-02-02 11:05:29 -05004747/*
Chris Mason323ac952008-10-01 19:05:46 -04004748 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004749 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004750 *
4751 * This deletes the pointer in path->nodes[1] and frees the leaf
4752 * block extent. zero is returned if it all worked out, < 0 otherwise.
4753 *
4754 * The path must have already been setup for deleting the leaf, including
4755 * all the proper balancing. path->nodes[1] must be locked.
4756 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004757static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4758 struct btrfs_root *root,
4759 struct btrfs_path *path,
4760 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004761{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004762 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004763 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004764
Chris Mason4d081c42009-02-04 09:31:28 -05004765 /*
4766 * btrfs_free_extent is expensive, we want to make sure we
4767 * aren't holding any locks when we call it
4768 */
4769 btrfs_unlock_up_safe(path, 0);
4770
Yan, Zhengf0486c62010-05-16 10:46:25 -04004771 root_sub_used(root, leaf->len);
4772
Josef Bacik3083ee22012-03-09 16:01:49 -05004773 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004774 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004775 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004776}
4777/*
Chris Mason74123bd2007-02-02 11:05:29 -05004778 * delete the item at the leaf level in path. If that empties
4779 * the leaf, remove it from the tree
4780 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004781int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4782 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004783{
Chris Mason5f39d392007-10-15 16:14:19 -04004784 struct extent_buffer *leaf;
4785 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004786 int last_off;
4787 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004788 int ret = 0;
4789 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004790 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004791 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004792 struct btrfs_map_token token;
4793
4794 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004795
Chris Mason5f39d392007-10-15 16:14:19 -04004796 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004797 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4798
4799 for (i = 0; i < nr; i++)
4800 dsize += btrfs_item_size_nr(leaf, slot + i);
4801
Chris Mason5f39d392007-10-15 16:14:19 -04004802 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004803
Chris Mason85e21ba2008-01-29 15:11:36 -05004804 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004805 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004806
4807 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004808 data_end + dsize,
4809 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004810 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004811
Chris Mason85e21ba2008-01-29 15:11:36 -05004812 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004813 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004814
Chris Mason5f39d392007-10-15 16:14:19 -04004815 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004816 ioff = btrfs_token_item_offset(leaf, item, &token);
4817 btrfs_set_token_item_offset(leaf, item,
4818 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004819 }
Chris Masondb945352007-10-15 16:15:53 -04004820
Chris Mason5f39d392007-10-15 16:14:19 -04004821 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004822 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004823 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004824 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004825 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004826 btrfs_set_header_nritems(leaf, nritems - nr);
4827 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004828
Chris Mason74123bd2007-02-02 11:05:29 -05004829 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004830 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004831 if (leaf == root->node) {
4832 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004833 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004834 btrfs_set_path_blocking(path);
4835 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004836 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004837 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004838 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004839 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004840 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004841 struct btrfs_disk_key disk_key;
4842
4843 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004844 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004845 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004846
Chris Mason74123bd2007-02-02 11:05:29 -05004847 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004848 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004849 /* push_leaf_left fixes the path.
4850 * make sure the path still points to our leaf
4851 * for possible call to del_ptr below
4852 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004853 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004854 extent_buffer_get(leaf);
4855
Chris Masonb9473432009-03-13 11:00:37 -04004856 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004857 wret = push_leaf_left(trans, root, path, 1, 1,
4858 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004859 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004860 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004861
4862 if (path->nodes[0] == leaf &&
4863 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004864 wret = push_leaf_right(trans, root, path, 1,
4865 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004866 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004867 ret = wret;
4868 }
Chris Mason5f39d392007-10-15 16:14:19 -04004869
4870 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004871 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004872 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004873 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004874 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004875 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004876 /* if we're still in the path, make sure
4877 * we're dirty. Otherwise, one of the
4878 * push_leaf functions must have already
4879 * dirtied this buffer
4880 */
4881 if (path->nodes[0] == leaf)
4882 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004883 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004884 }
Chris Masond5719762007-03-23 10:01:08 -04004885 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004886 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004887 }
4888 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004889 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004890}
4891
Chris Mason97571fd2007-02-24 13:39:08 -05004892/*
Chris Mason925baed2008-06-25 16:01:30 -04004893 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004894 * returns 0 if it found something or 1 if there are no lesser leaves.
4895 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004896 *
4897 * This may release the path, and so you may lose any locks held at the
4898 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004899 */
4900int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4901{
Chris Mason925baed2008-06-25 16:01:30 -04004902 struct btrfs_key key;
4903 struct btrfs_disk_key found_key;
4904 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004905
Chris Mason925baed2008-06-25 16:01:30 -04004906 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004907
Chris Mason925baed2008-06-25 16:01:30 -04004908 if (key.offset > 0)
4909 key.offset--;
4910 else if (key.type > 0)
4911 key.type--;
4912 else if (key.objectid > 0)
4913 key.objectid--;
4914 else
4915 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004916
David Sterbab3b4aa72011-04-21 01:20:15 +02004917 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4919 if (ret < 0)
4920 return ret;
4921 btrfs_item_key(path->nodes[0], &found_key, 0);
4922 ret = comp_keys(&found_key, &key);
4923 if (ret < 0)
4924 return 0;
4925 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004926}
4927
Chris Mason3f157a22008-06-25 16:01:31 -04004928/*
4929 * A helper function to walk down the tree starting at min_key, and looking
4930 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004931 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004932 *
4933 * This does not cow, but it does stuff the starting key it finds back
4934 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4935 * key and get a writable path.
4936 *
4937 * This does lock as it descends, and path->keep_locks should be set
4938 * to 1 by the caller.
4939 *
4940 * This honors path->lowest_level to prevent descent past a given level
4941 * of the tree.
4942 *
Chris Masond352ac62008-09-29 15:18:18 -04004943 * min_trans indicates the oldest transaction that you are interested
4944 * in walking through. Any nodes or leaves older than min_trans are
4945 * skipped over (without reading them).
4946 *
Chris Mason3f157a22008-06-25 16:01:31 -04004947 * returns zero if something useful was found, < 0 on error and 1 if there
4948 * was nothing in the tree that matched the search criteria.
4949 */
4950int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004951 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004952 struct btrfs_path *path, int cache_only,
4953 u64 min_trans)
4954{
4955 struct extent_buffer *cur;
4956 struct btrfs_key found_key;
4957 int slot;
Yan96524802008-07-24 12:19:49 -04004958 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004959 u32 nritems;
4960 int level;
4961 int ret = 1;
4962
Chris Mason934d3752008-12-08 16:43:10 -05004963 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004964again:
Chris Masonbd681512011-07-16 15:23:14 -04004965 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004966 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004967 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004968 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004969 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004970
4971 if (btrfs_header_generation(cur) < min_trans) {
4972 ret = 1;
4973 goto out;
4974 }
Chris Masond3977122009-01-05 21:25:51 -05004975 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004976 nritems = btrfs_header_nritems(cur);
4977 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004978 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004979
Chris Mason323ac952008-10-01 19:05:46 -04004980 /* at the lowest level, we're done, setup the path and exit */
4981 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004982 if (slot >= nritems)
4983 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004984 ret = 0;
4985 path->slots[level] = slot;
4986 btrfs_item_key_to_cpu(cur, &found_key, slot);
4987 goto out;
4988 }
Yan96524802008-07-24 12:19:49 -04004989 if (sret && slot > 0)
4990 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004991 /*
4992 * check this node pointer against the cache_only and
4993 * min_trans parameters. If it isn't in cache or is too
4994 * old, skip to the next one.
4995 */
Chris Masond3977122009-01-05 21:25:51 -05004996 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004997 u64 blockptr;
4998 u64 gen;
4999 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04005000 struct btrfs_disk_key disk_key;
5001
Chris Mason3f157a22008-06-25 16:01:31 -04005002 blockptr = btrfs_node_blockptr(cur, slot);
5003 gen = btrfs_node_ptr_generation(cur, slot);
5004 if (gen < min_trans) {
5005 slot++;
5006 continue;
5007 }
5008 if (!cache_only)
5009 break;
5010
Chris Masone02119d2008-09-05 16:13:11 -04005011 if (max_key) {
5012 btrfs_node_key(cur, &disk_key, slot);
5013 if (comp_keys(&disk_key, max_key) >= 0) {
5014 ret = 1;
5015 goto out;
5016 }
5017 }
5018
Chris Mason3f157a22008-06-25 16:01:31 -04005019 tmp = btrfs_find_tree_block(root, blockptr,
5020 btrfs_level_size(root, level - 1));
5021
Chris Masonb9fab912012-05-06 07:23:47 -04005022 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04005023 free_extent_buffer(tmp);
5024 break;
5025 }
5026 if (tmp)
5027 free_extent_buffer(tmp);
5028 slot++;
5029 }
Chris Masone02119d2008-09-05 16:13:11 -04005030find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005031 /*
5032 * we didn't find a candidate key in this node, walk forward
5033 * and find another one
5034 */
5035 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005036 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005037 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005038 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04005039 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005040 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005041 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005042 goto again;
5043 } else {
5044 goto out;
5045 }
5046 }
5047 /* save our key for returning back */
5048 btrfs_node_key_to_cpu(cur, &found_key, slot);
5049 path->slots[level] = slot;
5050 if (level == path->lowest_level) {
5051 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005052 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005053 goto out;
5054 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005055 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005056 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005057 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005058
Chris Masonbd681512011-07-16 15:23:14 -04005059 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005060
Chris Masonbd681512011-07-16 15:23:14 -04005061 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005062 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005063 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005064 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005065 }
5066out:
5067 if (ret == 0)
5068 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005069 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005070 return ret;
5071}
5072
5073/*
5074 * this is similar to btrfs_next_leaf, but does not try to preserve
5075 * and fixup the path. It looks for and returns the next key in the
5076 * tree based on the current path and the cache_only and min_trans
5077 * parameters.
5078 *
5079 * 0 is returned if another key is found, < 0 if there are any errors
5080 * and 1 is returned if there are no higher keys in the tree
5081 *
5082 * path->keep_locks should be set to 1 on the search made before
5083 * calling this function.
5084 */
Chris Masone7a84562008-06-25 16:01:31 -04005085int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04005086 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04005087 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005088{
Chris Masone7a84562008-06-25 16:01:31 -04005089 int slot;
5090 struct extent_buffer *c;
5091
Chris Mason934d3752008-12-08 16:43:10 -05005092 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005093 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005094 if (!path->nodes[level])
5095 return 1;
5096
5097 slot = path->slots[level] + 1;
5098 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005099next:
Chris Masone7a84562008-06-25 16:01:31 -04005100 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005101 int ret;
5102 int orig_lowest;
5103 struct btrfs_key cur_key;
5104 if (level + 1 >= BTRFS_MAX_LEVEL ||
5105 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005106 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005107
5108 if (path->locks[level + 1]) {
5109 level++;
5110 continue;
5111 }
5112
5113 slot = btrfs_header_nritems(c) - 1;
5114 if (level == 0)
5115 btrfs_item_key_to_cpu(c, &cur_key, slot);
5116 else
5117 btrfs_node_key_to_cpu(c, &cur_key, slot);
5118
5119 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005120 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005121 path->lowest_level = level;
5122 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5123 0, 0);
5124 path->lowest_level = orig_lowest;
5125 if (ret < 0)
5126 return ret;
5127
5128 c = path->nodes[level];
5129 slot = path->slots[level];
5130 if (ret == 0)
5131 slot++;
5132 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005133 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005134
Chris Masone7a84562008-06-25 16:01:31 -04005135 if (level == 0)
5136 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005137 else {
5138 u64 blockptr = btrfs_node_blockptr(c, slot);
5139 u64 gen = btrfs_node_ptr_generation(c, slot);
5140
5141 if (cache_only) {
5142 struct extent_buffer *cur;
5143 cur = btrfs_find_tree_block(root, blockptr,
5144 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04005145 if (!cur ||
5146 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04005147 slot++;
5148 if (cur)
5149 free_extent_buffer(cur);
5150 goto next;
5151 }
5152 free_extent_buffer(cur);
5153 }
5154 if (gen < min_trans) {
5155 slot++;
5156 goto next;
5157 }
Chris Masone7a84562008-06-25 16:01:31 -04005158 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005159 }
Chris Masone7a84562008-06-25 16:01:31 -04005160 return 0;
5161 }
5162 return 1;
5163}
5164
Chris Mason7bb86312007-12-11 09:25:06 -05005165/*
Chris Mason925baed2008-06-25 16:01:30 -04005166 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005167 * returns 0 if it found something or 1 if there are no greater leaves.
5168 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005169 */
Chris Mason234b63a2007-03-13 10:46:10 -04005170int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005171{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005172 return btrfs_next_old_leaf(root, path, 0);
5173}
5174
5175int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5176 u64 time_seq)
5177{
Chris Masond97e63b2007-02-20 16:40:44 -05005178 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005179 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005180 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005181 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005182 struct btrfs_key key;
5183 u32 nritems;
5184 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005185 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005186 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005187
5188 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005189 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005190 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005191
Chris Mason8e73f272009-04-03 10:14:18 -04005192 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5193again:
5194 level = 1;
5195 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005196 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005197 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005198
Chris Masona2135012008-06-25 16:01:30 -04005199 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005200 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005201
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005202 if (time_seq)
5203 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5204 else
5205 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005206 path->keep_locks = 0;
5207
5208 if (ret < 0)
5209 return ret;
5210
Chris Masona2135012008-06-25 16:01:30 -04005211 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005212 /*
5213 * by releasing the path above we dropped all our locks. A balance
5214 * could have added more items next to the key that used to be
5215 * at the very end of the block. So, check again here and
5216 * advance the path if there are now more items available.
5217 */
Chris Masona2135012008-06-25 16:01:30 -04005218 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005219 if (ret == 0)
5220 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005221 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005222 goto done;
5223 }
Chris Masond97e63b2007-02-20 16:40:44 -05005224
Chris Masond3977122009-01-05 21:25:51 -05005225 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005226 if (!path->nodes[level]) {
5227 ret = 1;
5228 goto done;
5229 }
Chris Mason5f39d392007-10-15 16:14:19 -04005230
Chris Masond97e63b2007-02-20 16:40:44 -05005231 slot = path->slots[level] + 1;
5232 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005233 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005234 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005235 if (level == BTRFS_MAX_LEVEL) {
5236 ret = 1;
5237 goto done;
5238 }
Chris Masond97e63b2007-02-20 16:40:44 -05005239 continue;
5240 }
Chris Mason5f39d392007-10-15 16:14:19 -04005241
Chris Mason925baed2008-06-25 16:01:30 -04005242 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005243 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005244 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005245 }
Chris Mason5f39d392007-10-15 16:14:19 -04005246
Chris Mason8e73f272009-04-03 10:14:18 -04005247 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005248 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005249 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005250 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005251 if (ret == -EAGAIN)
5252 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005253
Chris Mason76a05b32009-05-14 13:24:30 -04005254 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005255 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005256 goto done;
5257 }
5258
Chris Mason5cd57b22008-06-25 16:01:30 -04005259 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005260 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005261 if (!ret && time_seq) {
5262 /*
5263 * If we don't get the lock, we may be racing
5264 * with push_leaf_left, holding that lock while
5265 * itself waiting for the leaf we've currently
5266 * locked. To solve this situation, we give up
5267 * on our lock and cycle.
5268 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005269 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005270 btrfs_release_path(path);
5271 cond_resched();
5272 goto again;
5273 }
Chris Mason8e73f272009-04-03 10:14:18 -04005274 if (!ret) {
5275 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005276 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005277 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005278 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005279 }
Chris Mason31533fb2011-07-26 16:01:59 -04005280 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005281 }
Chris Masond97e63b2007-02-20 16:40:44 -05005282 break;
5283 }
5284 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005285 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005286 level--;
5287 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005288 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005289 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005290
Chris Mason5f39d392007-10-15 16:14:19 -04005291 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005292 path->nodes[level] = next;
5293 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005294 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005295 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005296 if (!level)
5297 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005298
Chris Mason8e73f272009-04-03 10:14:18 -04005299 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005300 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005301 if (ret == -EAGAIN)
5302 goto again;
5303
Chris Mason76a05b32009-05-14 13:24:30 -04005304 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005305 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005306 goto done;
5307 }
5308
Chris Mason5cd57b22008-06-25 16:01:30 -04005309 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005310 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005311 if (!ret) {
5312 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005313 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005314 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005315 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005316 }
Chris Mason31533fb2011-07-26 16:01:59 -04005317 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005318 }
Chris Masond97e63b2007-02-20 16:40:44 -05005319 }
Chris Mason8e73f272009-04-03 10:14:18 -04005320 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005321done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005322 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005323 path->leave_spinning = old_spinning;
5324 if (!old_spinning)
5325 btrfs_set_path_blocking(path);
5326
5327 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005328}
Chris Mason0b86a832008-03-24 15:01:56 -04005329
Chris Mason3f157a22008-06-25 16:01:31 -04005330/*
5331 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5332 * searching until it gets past min_objectid or finds an item of 'type'
5333 *
5334 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5335 */
Chris Mason0b86a832008-03-24 15:01:56 -04005336int btrfs_previous_item(struct btrfs_root *root,
5337 struct btrfs_path *path, u64 min_objectid,
5338 int type)
5339{
5340 struct btrfs_key found_key;
5341 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005342 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005343 int ret;
5344
Chris Masond3977122009-01-05 21:25:51 -05005345 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005346 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005347 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005348 ret = btrfs_prev_leaf(root, path);
5349 if (ret != 0)
5350 return ret;
5351 } else {
5352 path->slots[0]--;
5353 }
5354 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005355 nritems = btrfs_header_nritems(leaf);
5356 if (nritems == 0)
5357 return 1;
5358 if (path->slots[0] == nritems)
5359 path->slots[0]--;
5360
Chris Mason0b86a832008-03-24 15:01:56 -04005361 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005362 if (found_key.objectid < min_objectid)
5363 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005364 if (found_key.type == type)
5365 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005366 if (found_key.objectid == min_objectid &&
5367 found_key.type < type)
5368 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005369 }
5370 return 1;
5371}