blob: f9cd40967d0485e54bb2a71513e32db8d500843f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
57void btrfs_free_path(struct btrfs_path *p)
58{
Chris Masondf24a2b2007-04-04 09:36:31 -040059 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040060 kmem_cache_free(btrfs_path_cachep, p);
61}
62
Chris Masone02119d2008-09-05 16:13:11 -040063void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050064{
65 int i;
Chris Masona2135012008-06-25 16:01:30 -040066
Chris Mason234b63a2007-03-13 10:46:10 -040067 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040068 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050069 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040070 continue;
71 if (p->locks[i]) {
72 btrfs_tree_unlock(p->nodes[i]);
73 p->locks[i] = 0;
74 }
Chris Mason5f39d392007-10-15 16:14:19 -040075 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040076 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050077 }
78}
79
Chris Mason925baed2008-06-25 16:01:30 -040080struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
81{
82 struct extent_buffer *eb;
83 spin_lock(&root->node_lock);
84 eb = root->node;
85 extent_buffer_get(eb);
86 spin_unlock(&root->node_lock);
87 return eb;
88}
89
90struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
91{
92 struct extent_buffer *eb;
93
94 while(1) {
95 eb = btrfs_root_node(root);
96 btrfs_tree_lock(eb);
97
98 spin_lock(&root->node_lock);
99 if (eb == root->node) {
100 spin_unlock(&root->node_lock);
101 break;
102 }
103 spin_unlock(&root->node_lock);
104
105 btrfs_tree_unlock(eb);
106 free_extent_buffer(eb);
107 }
108 return eb;
109}
110
Chris Mason0b86a832008-03-24 15:01:56 -0400111static void add_root_to_dirty_list(struct btrfs_root *root)
112{
113 if (root->track_dirty && list_empty(&root->dirty_list)) {
114 list_add(&root->dirty_list,
115 &root->fs_info->dirty_cowonly_roots);
116 }
117}
118
Chris Masonbe20aa92007-12-17 20:14:01 -0500119int btrfs_copy_root(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 struct extent_buffer *buf,
122 struct extent_buffer **cow_ret, u64 new_root_objectid)
123{
124 struct extent_buffer *cow;
125 u32 nritems;
126 int ret = 0;
127 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500128 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500129
Chris Mason4aec2b52007-12-18 16:25:45 -0500130 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
131 if (!new_root)
132 return -ENOMEM;
133
134 memcpy(new_root, root, sizeof(*new_root));
135 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500136
137 WARN_ON(root->ref_cows && trans->transid !=
138 root->fs_info->running_transaction->transid);
139 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
140
141 level = btrfs_header_level(buf);
142 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400143
144 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
145 new_root_objectid, trans->transid,
146 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500147 if (IS_ERR(cow)) {
148 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500149 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500150 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500151
152 copy_extent_buffer(cow, buf, 0, 0, cow->len);
153 btrfs_set_header_bytenr(cow, cow->start);
154 btrfs_set_header_generation(cow, trans->transid);
155 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400156 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500157
158 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400159 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 kfree(new_root);
161
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 if (ret)
163 return ret;
164
165 btrfs_mark_buffer_dirty(cow);
166 *cow_ret = cow;
167 return 0;
168}
169
Chris Masone02119d2008-09-05 16:13:11 -0400170int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400171 struct btrfs_root *root,
172 struct extent_buffer *buf,
173 struct extent_buffer *parent, int parent_slot,
174 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400175 u64 search_start, u64 empty_size,
176 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400177{
Zheng Yan31840ae2008-09-23 13:14:14 -0400178 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400179 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500180 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400181 int ret = 0;
182 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500183 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400184 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400185
Chris Mason925baed2008-06-25 16:01:30 -0400186 if (*cow_ret == buf)
187 unlock_orig = 1;
188
189 WARN_ON(!btrfs_tree_locked(buf));
190
Zheng Yan31840ae2008-09-23 13:14:14 -0400191 if (parent)
192 parent_start = parent->start;
193 else
194 parent_start = 0;
195
Chris Mason7bb86312007-12-11 09:25:06 -0500196 WARN_ON(root->ref_cows && trans->transid !=
197 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400198 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400199
Chris Mason7bb86312007-12-11 09:25:06 -0500200 level = btrfs_header_level(buf);
201 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400202
Chris Mason65b51a02008-08-01 15:11:20 -0400203 if (prealloc_dest) {
204 struct btrfs_key ins;
205
206 ins.objectid = prealloc_dest;
207 ins.offset = buf->len;
208 ins.type = BTRFS_EXTENT_ITEM_KEY;
209
Zheng Yan31840ae2008-09-23 13:14:14 -0400210 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400211 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400212 trans->transid, level, 0,
Chris Mason65b51a02008-08-01 15:11:20 -0400213 &ins);
214 BUG_ON(ret);
215 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
216 buf->len);
217 } else {
218 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400219 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400220 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400221 trans->transid, level,
222 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400223 }
Chris Mason6702ed42007-08-07 16:15:09 -0400224 if (IS_ERR(cow))
225 return PTR_ERR(cow);
226
Chris Mason5f39d392007-10-15 16:14:19 -0400227 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400228 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400229 btrfs_set_header_generation(cow, trans->transid);
230 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400231 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400232
Chris Mason5f39d392007-10-15 16:14:19 -0400233 WARN_ON(btrfs_header_generation(buf) > trans->transid);
234 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400235 u32 nr_extents;
Chris Mason6702ed42007-08-07 16:15:09 -0400236 different_trans = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400237 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400238 if (ret)
239 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400240
241 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
242 WARN_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400243 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400244 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
245 if (ret)
246 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400247 clean_tree_block(trans, root, buf);
248 }
249
250 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400251 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400252
253 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400254 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400255 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400256 spin_unlock(&root->node_lock);
257
Chris Mason6702ed42007-08-07 16:15:09 -0400258 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400259 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400260 buf->len, buf->start,
261 root->root_key.objectid,
262 btrfs_header_generation(buf),
263 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400264 }
Chris Mason5f39d392007-10-15 16:14:19 -0400265 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400266 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400267 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400268 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400269 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500270 WARN_ON(trans->transid == 0);
271 btrfs_set_node_ptr_generation(parent, parent_slot,
272 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400273 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400274 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500275 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400276 parent_start, btrfs_header_owner(parent),
277 btrfs_header_generation(parent), 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400278 }
Chris Mason925baed2008-06-25 16:01:30 -0400279 if (unlock_orig)
280 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400281 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400282 btrfs_mark_buffer_dirty(cow);
283 *cow_ret = cow;
284 return 0;
285}
286
Chris Masone02119d2008-09-05 16:13:11 -0400287int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400288 struct btrfs_root *root, struct extent_buffer *buf,
289 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400290 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500291{
Chris Mason6702ed42007-08-07 16:15:09 -0400292 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400293 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500294
Chris Masonccd467d2007-06-28 15:57:36 -0400295 if (trans->transaction != root->fs_info->running_transaction) {
296 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
297 root->fs_info->running_transaction->transid);
298 WARN_ON(1);
299 }
300 if (trans->transid != root->fs_info->generation) {
301 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
302 root->fs_info->generation);
303 WARN_ON(1);
304 }
Chris Masondc17ff82008-01-08 15:46:30 -0500305
Chris Mason63b10fc2008-04-01 11:21:32 -0400306 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400307 if (btrfs_header_generation(buf) == trans->transid &&
308 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400309 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500310 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400311 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400312 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500313 return 0;
314 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400315 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400316 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400317 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400318 parent_slot, cow_ret, search_start, 0,
319 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400320 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400321}
322
Chris Mason6b800532007-10-15 16:17:34 -0400323static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400324{
Chris Mason6b800532007-10-15 16:17:34 -0400325 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400326 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400327 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400328 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500329 return 0;
330}
331
Chris Mason081e9572007-11-06 10:26:24 -0500332/*
333 * compare two keys in a memcmp fashion
334 */
335static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
336{
337 struct btrfs_key k1;
338
339 btrfs_disk_key_to_cpu(&k1, disk);
340
341 if (k1.objectid > k2->objectid)
342 return 1;
343 if (k1.objectid < k2->objectid)
344 return -1;
345 if (k1.type > k2->type)
346 return 1;
347 if (k1.type < k2->type)
348 return -1;
349 if (k1.offset > k2->offset)
350 return 1;
351 if (k1.offset < k2->offset)
352 return -1;
353 return 0;
354}
355
356
Chris Mason6702ed42007-08-07 16:15:09 -0400357int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400358 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400359 int start_slot, int cache_only, u64 *last_ret,
360 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400361{
Chris Mason6b800532007-10-15 16:17:34 -0400362 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400363 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400364 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400365 u64 search_start = *last_ret;
366 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400367 u64 other;
368 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400369 int end_slot;
370 int i;
371 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400372 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400373 int uptodate;
374 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500375 int progress_passed = 0;
376 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400377
Chris Mason5708b952007-10-25 15:43:18 -0400378 parent_level = btrfs_header_level(parent);
379 if (cache_only && parent_level != 1)
380 return 0;
381
Chris Mason6702ed42007-08-07 16:15:09 -0400382 if (trans->transaction != root->fs_info->running_transaction) {
383 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
384 root->fs_info->running_transaction->transid);
385 WARN_ON(1);
386 }
387 if (trans->transid != root->fs_info->generation) {
388 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
389 root->fs_info->generation);
390 WARN_ON(1);
391 }
Chris Mason86479a02007-09-10 19:58:16 -0400392
Chris Mason6b800532007-10-15 16:17:34 -0400393 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400394 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400395 end_slot = parent_nritems;
396
397 if (parent_nritems == 1)
398 return 0;
399
400 for (i = start_slot; i < end_slot; i++) {
401 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400402
Chris Mason5708b952007-10-25 15:43:18 -0400403 if (!parent->map_token) {
404 map_extent_buffer(parent,
405 btrfs_node_key_ptr_offset(i),
406 sizeof(struct btrfs_key_ptr),
407 &parent->map_token, &parent->kaddr,
408 &parent->map_start, &parent->map_len,
409 KM_USER1);
410 }
Chris Mason081e9572007-11-06 10:26:24 -0500411 btrfs_node_key(parent, &disk_key, i);
412 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
413 continue;
414
415 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400416 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400417 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400418 if (last_block == 0)
419 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400420
Chris Mason6702ed42007-08-07 16:15:09 -0400421 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400422 other = btrfs_node_blockptr(parent, i - 1);
423 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400424 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400425 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400426 other = btrfs_node_blockptr(parent, i + 1);
427 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400428 }
Chris Masone9d0b132007-08-10 14:06:19 -0400429 if (close) {
430 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400431 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400432 }
Chris Mason5708b952007-10-25 15:43:18 -0400433 if (parent->map_token) {
434 unmap_extent_buffer(parent, parent->map_token,
435 KM_USER1);
436 parent->map_token = NULL;
437 }
Chris Mason6702ed42007-08-07 16:15:09 -0400438
Chris Mason6b800532007-10-15 16:17:34 -0400439 cur = btrfs_find_tree_block(root, blocknr, blocksize);
440 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400441 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400442 else
443 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400444 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400445 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400446 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400447 continue;
448 }
Chris Mason6b800532007-10-15 16:17:34 -0400449 if (!cur) {
450 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400451 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400452 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400453 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400454 }
Chris Mason6702ed42007-08-07 16:15:09 -0400455 }
Chris Masone9d0b132007-08-10 14:06:19 -0400456 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400457 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400458
Chris Masone7a84562008-06-25 16:01:31 -0400459 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400460 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400461 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400462 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400463 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400464 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400465 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400466 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400467 break;
Yan252c38f2007-08-29 09:11:44 -0400468 }
Chris Masone7a84562008-06-25 16:01:31 -0400469 search_start = cur->start;
470 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400471 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400472 btrfs_tree_unlock(cur);
473 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 }
Chris Mason5708b952007-10-25 15:43:18 -0400475 if (parent->map_token) {
476 unmap_extent_buffer(parent, parent->map_token,
477 KM_USER1);
478 parent->map_token = NULL;
479 }
Chris Mason6702ed42007-08-07 16:15:09 -0400480 return err;
481}
482
Chris Mason74123bd2007-02-02 11:05:29 -0500483/*
484 * The leaf data grows from end-to-front in the node.
485 * this returns the address of the start of the last item,
486 * which is the stop of the leaf data stack
487 */
Chris Mason123abc82007-03-14 14:14:43 -0400488static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400489 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500490{
Chris Mason5f39d392007-10-15 16:14:19 -0400491 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500492 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400493 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400494 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495}
496
Chris Mason123abc82007-03-14 14:14:43 -0400497static int check_node(struct btrfs_root *root, struct btrfs_path *path,
498 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500499{
Chris Mason5f39d392007-10-15 16:14:19 -0400500 struct extent_buffer *parent = NULL;
501 struct extent_buffer *node = path->nodes[level];
502 struct btrfs_disk_key parent_key;
503 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500504 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400505 int slot;
506 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400507 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500508
509 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400510 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400511
Chris Mason8d7be552007-05-10 11:24:42 -0400512 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400513 BUG_ON(nritems == 0);
514 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400515 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400516 btrfs_node_key(parent, &parent_key, parent_slot);
517 btrfs_node_key(node, &node_key, 0);
518 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400519 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400520 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400521 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500522 }
Chris Mason123abc82007-03-14 14:14:43 -0400523 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400524 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400525 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
526 btrfs_node_key(node, &node_key, slot);
527 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400528 }
529 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400530 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
531 btrfs_node_key(node, &node_key, slot);
532 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500533 }
534 return 0;
535}
536
Chris Mason123abc82007-03-14 14:14:43 -0400537static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
538 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500539{
Chris Mason5f39d392007-10-15 16:14:19 -0400540 struct extent_buffer *leaf = path->nodes[level];
541 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500542 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400543 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400544 struct btrfs_disk_key parent_key;
545 struct btrfs_disk_key leaf_key;
546 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400547
Chris Mason5f39d392007-10-15 16:14:19 -0400548 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500549
550 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400551 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400552
553 if (nritems == 0)
554 return 0;
555
556 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400557 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400558 btrfs_node_key(parent, &parent_key, parent_slot);
559 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400560
Chris Mason5f39d392007-10-15 16:14:19 -0400561 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400562 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400563 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400564 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500565 }
Chris Mason5f39d392007-10-15 16:14:19 -0400566#if 0
567 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
568 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
569 btrfs_item_key(leaf, &leaf_key, i);
570 if (comp_keys(&leaf_key, &cpukey) >= 0) {
571 btrfs_print_leaf(root, leaf);
572 printk("slot %d offset bad key\n", i);
573 BUG_ON(1);
574 }
575 if (btrfs_item_offset_nr(leaf, i) !=
576 btrfs_item_end_nr(leaf, i + 1)) {
577 btrfs_print_leaf(root, leaf);
578 printk("slot %d offset bad\n", i);
579 BUG_ON(1);
580 }
581 if (i == 0) {
582 if (btrfs_item_offset_nr(leaf, i) +
583 btrfs_item_size_nr(leaf, i) !=
584 BTRFS_LEAF_DATA_SIZE(root)) {
585 btrfs_print_leaf(root, leaf);
586 printk("slot %d first offset bad\n", i);
587 BUG_ON(1);
588 }
589 }
590 }
591 if (nritems > 0) {
592 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
593 btrfs_print_leaf(root, leaf);
594 printk("slot %d bad size \n", nritems - 1);
595 BUG_ON(1);
596 }
597 }
598#endif
599 if (slot != 0 && slot < nritems - 1) {
600 btrfs_item_key(leaf, &leaf_key, slot);
601 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
602 if (comp_keys(&leaf_key, &cpukey) <= 0) {
603 btrfs_print_leaf(root, leaf);
604 printk("slot %d offset bad key\n", slot);
605 BUG_ON(1);
606 }
607 if (btrfs_item_offset_nr(leaf, slot - 1) !=
608 btrfs_item_end_nr(leaf, slot)) {
609 btrfs_print_leaf(root, leaf);
610 printk("slot %d offset bad\n", slot);
611 BUG_ON(1);
612 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500613 }
Chris Mason8d7be552007-05-10 11:24:42 -0400614 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400615 btrfs_item_key(leaf, &leaf_key, slot);
616 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
617 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
618 if (btrfs_item_offset_nr(leaf, slot) !=
619 btrfs_item_end_nr(leaf, slot + 1)) {
620 btrfs_print_leaf(root, leaf);
621 printk("slot %d offset bad\n", slot);
622 BUG_ON(1);
623 }
Chris Mason8d7be552007-05-10 11:24:42 -0400624 }
Chris Mason5f39d392007-10-15 16:14:19 -0400625 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
626 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500627 return 0;
628}
629
Chris Mason98ed5172008-01-03 10:01:48 -0500630static int noinline check_block(struct btrfs_root *root,
631 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500632{
Chris Masonf1885912008-04-09 16:28:12 -0400633 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400634 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400635 if (btrfs_header_level(path->nodes[level]) != level)
636 printk("warning: bad level %Lu wanted %d found %d\n",
637 path->nodes[level]->start, level,
638 btrfs_header_level(path->nodes[level]));
639 found_start = btrfs_header_bytenr(path->nodes[level]);
640 if (found_start != path->nodes[level]->start) {
641 printk("warning: bad bytentr %Lu found %Lu\n",
642 path->nodes[level]->start, found_start);
643 }
Chris Masondb945352007-10-15 16:15:53 -0400644#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400645 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400646
Chris Mason479965d2007-10-15 16:14:27 -0400647 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
648 (unsigned long)btrfs_header_fsid(buf),
649 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400650 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400651 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400652 }
Chris Masondb945352007-10-15 16:15:53 -0400653#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500654 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400655 return check_leaf(root, path, level);
656 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500657}
658
Chris Mason74123bd2007-02-02 11:05:29 -0500659/*
Chris Mason5f39d392007-10-15 16:14:19 -0400660 * search for key in the extent_buffer. The items start at offset p,
661 * and they are item_size apart. There are 'max' items in p.
662 *
Chris Mason74123bd2007-02-02 11:05:29 -0500663 * the slot in the array is returned via slot, and it points to
664 * the place where you would insert key if it is not found in
665 * the array.
666 *
667 * slot may point to max if the key is bigger than all of the keys
668 */
Chris Masone02119d2008-09-05 16:13:11 -0400669static noinline int generic_bin_search(struct extent_buffer *eb,
670 unsigned long p,
671 int item_size, struct btrfs_key *key,
672 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500673{
674 int low = 0;
675 int high = max;
676 int mid;
677 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400678 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400679 struct btrfs_disk_key unaligned;
680 unsigned long offset;
681 char *map_token = NULL;
682 char *kaddr = NULL;
683 unsigned long map_start = 0;
684 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400685 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500686
687 while(low < high) {
688 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400689 offset = p + mid * item_size;
690
691 if (!map_token || offset < map_start ||
692 (offset + sizeof(struct btrfs_disk_key)) >
693 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400694 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400695 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400696 map_token = NULL;
697 }
698 err = map_extent_buffer(eb, offset,
699 sizeof(struct btrfs_disk_key),
700 &map_token, &kaddr,
701 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400702
Chris Mason479965d2007-10-15 16:14:27 -0400703 if (!err) {
704 tmp = (struct btrfs_disk_key *)(kaddr + offset -
705 map_start);
706 } else {
707 read_extent_buffer(eb, &unaligned,
708 offset, sizeof(unaligned));
709 tmp = &unaligned;
710 }
711
Chris Mason5f39d392007-10-15 16:14:19 -0400712 } else {
713 tmp = (struct btrfs_disk_key *)(kaddr + offset -
714 map_start);
715 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500716 ret = comp_keys(tmp, key);
717
718 if (ret < 0)
719 low = mid + 1;
720 else if (ret > 0)
721 high = mid;
722 else {
723 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400724 if (map_token)
725 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726 return 0;
727 }
728 }
729 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400730 if (map_token)
731 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732 return 1;
733}
734
Chris Mason97571fd2007-02-24 13:39:08 -0500735/*
736 * simple bin_search frontend that does the right thing for
737 * leaves vs nodes
738 */
Chris Mason5f39d392007-10-15 16:14:19 -0400739static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
740 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500741{
Chris Mason5f39d392007-10-15 16:14:19 -0400742 if (level == 0) {
743 return generic_bin_search(eb,
744 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400745 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400746 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400747 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400749 return generic_bin_search(eb,
750 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400751 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400752 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400753 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500754 }
755 return -1;
756}
757
Chris Masone02119d2008-09-05 16:13:11 -0400758static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400759 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500760{
Chris Masonca7a79a2008-05-12 12:59:19 -0400761 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500762 if (slot < 0)
763 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500765 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400766
767 BUG_ON(level == 0);
768
Chris Masondb945352007-10-15 16:15:53 -0400769 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400770 btrfs_level_size(root, level - 1),
771 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500772}
773
Chris Masone02119d2008-09-05 16:13:11 -0400774static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500775 struct btrfs_root *root,
776 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500777{
Chris Mason5f39d392007-10-15 16:14:19 -0400778 struct extent_buffer *right = NULL;
779 struct extent_buffer *mid;
780 struct extent_buffer *left = NULL;
781 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500782 int ret = 0;
783 int wret;
784 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500785 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400786 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500787 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500788
789 if (level == 0)
790 return 0;
791
Chris Mason5f39d392007-10-15 16:14:19 -0400792 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400793 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500794 WARN_ON(btrfs_header_generation(mid) != trans->transid);
795
Chris Mason1d4f8a02007-03-13 09:28:32 -0400796 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500797
Chris Mason234b63a2007-03-13 10:46:10 -0400798 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400799 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500800 pslot = path->slots[level + 1];
801
Chris Mason40689472007-03-17 14:29:23 -0400802 /*
803 * deal with the case where there is only one pointer in the root
804 * by promoting the node below to a root
805 */
Chris Mason5f39d392007-10-15 16:14:19 -0400806 if (!parent) {
807 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500808
Chris Mason5f39d392007-10-15 16:14:19 -0400809 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500810 return 0;
811
812 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400813 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400814 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500815 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400816 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500817 BUG_ON(ret);
818
Chris Mason925baed2008-06-25 16:01:30 -0400819 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500820 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400821 spin_unlock(&root->node_lock);
822
Zheng Yan31840ae2008-09-23 13:14:14 -0400823 ret = btrfs_update_extent_ref(trans, root, child->start,
824 mid->start, child->start,
825 root->root_key.objectid,
826 trans->transid, level - 1, 0);
827 BUG_ON(ret);
828
Chris Mason0b86a832008-03-24 15:01:56 -0400829 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400830 btrfs_tree_unlock(child);
831 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500832 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400833 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400834 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500835 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400836 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500837 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400838 mid->start, root->root_key.objectid,
Chris Mason7bb86312007-12-11 09:25:06 -0500839 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500840 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400841 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400842 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500843 }
Chris Mason5f39d392007-10-15 16:14:19 -0400844 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400845 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500846 return 0;
847
Chris Mason5f39d392007-10-15 16:14:19 -0400848 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400849 err_on_enospc = 1;
850
Chris Mason5f39d392007-10-15 16:14:19 -0400851 left = read_node_slot(root, parent, pslot - 1);
852 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400853 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400854 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400855 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 if (wret) {
857 ret = wret;
858 goto enospc;
859 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400860 }
Chris Mason5f39d392007-10-15 16:14:19 -0400861 right = read_node_slot(root, parent, pslot + 1);
862 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400863 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400864 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400865 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400866 if (wret) {
867 ret = wret;
868 goto enospc;
869 }
870 }
871
872 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400873 if (left) {
874 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400875 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500876 if (wret < 0)
877 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400879 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500880 }
Chris Mason79f95c82007-03-01 15:16:26 -0500881
882 /*
883 * then try to empty the right most buffer into the middle
884 */
Chris Mason5f39d392007-10-15 16:14:19 -0400885 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400886 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400887 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500888 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400889 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400890 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500891 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400892 u32 blocksize = right->len;
893
Chris Mason5f39d392007-10-15 16:14:19 -0400894 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400895 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400896 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500897 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400898 wret = del_ptr(trans, root, path, level + 1, pslot +
899 1);
Chris Masonbb803952007-03-01 12:04:21 -0500900 if (wret)
901 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400902 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -0400903 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500904 btrfs_header_owner(parent),
905 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500906 if (wret)
907 ret = wret;
908 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400909 struct btrfs_disk_key right_key;
910 btrfs_node_key(right, &right_key, 0);
911 btrfs_set_node_key(parent, &right_key, pslot + 1);
912 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500913 }
914 }
Chris Mason5f39d392007-10-15 16:14:19 -0400915 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500916 /*
917 * we're not allowed to leave a node with one item in the
918 * tree during a delete. A deletion from lower in the tree
919 * could try to delete the only pointer in this node.
920 * So, pull some keys from the left.
921 * There has to be a left pointer at this point because
922 * otherwise we would have pulled some pointers from the
923 * right
924 */
Chris Mason5f39d392007-10-15 16:14:19 -0400925 BUG_ON(!left);
926 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400927 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500928 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400929 goto enospc;
930 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400931 if (wret == 1) {
932 wret = push_node_left(trans, root, left, mid, 1);
933 if (wret < 0)
934 ret = wret;
935 }
Chris Mason79f95c82007-03-01 15:16:26 -0500936 BUG_ON(wret == 1);
937 }
Chris Mason5f39d392007-10-15 16:14:19 -0400938 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500939 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500940 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400941 u64 bytenr = mid->start;
942 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400943
Chris Mason5f39d392007-10-15 16:14:19 -0400944 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400945 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400946 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500947 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400948 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500949 if (wret)
950 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500951 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -0400952 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500953 btrfs_header_owner(parent),
954 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500955 if (wret)
956 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500957 } else {
958 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400959 struct btrfs_disk_key mid_key;
960 btrfs_node_key(mid, &mid_key, 0);
961 btrfs_set_node_key(parent, &mid_key, pslot);
962 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500963 }
Chris Masonbb803952007-03-01 12:04:21 -0500964
Chris Mason79f95c82007-03-01 15:16:26 -0500965 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400966 if (left) {
967 if (btrfs_header_nritems(left) > orig_slot) {
968 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400969 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400970 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500971 path->slots[level + 1] -= 1;
972 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400973 if (mid) {
974 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400975 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400976 }
Chris Masonbb803952007-03-01 12:04:21 -0500977 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400978 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500979 path->slots[level] = orig_slot;
980 }
981 }
Chris Mason79f95c82007-03-01 15:16:26 -0500982 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400983 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400984 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400985 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500986 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400987enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400988 if (right) {
989 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400990 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400991 }
992 if (left) {
993 if (path->nodes[level] != left)
994 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400995 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400996 }
Chris Masonbb803952007-03-01 12:04:21 -0500997 return ret;
998}
999
Chris Masone66f7092007-04-20 13:16:02 -04001000/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -05001001static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1002 struct btrfs_root *root,
1003 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001004{
Chris Mason5f39d392007-10-15 16:14:19 -04001005 struct extent_buffer *right = NULL;
1006 struct extent_buffer *mid;
1007 struct extent_buffer *left = NULL;
1008 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001009 int ret = 0;
1010 int wret;
1011 int pslot;
1012 int orig_slot = path->slots[level];
1013 u64 orig_ptr;
1014
1015 if (level == 0)
1016 return 1;
1017
Chris Mason5f39d392007-10-15 16:14:19 -04001018 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001019 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001020 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1021
1022 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001023 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001024 pslot = path->slots[level + 1];
1025
Chris Mason5f39d392007-10-15 16:14:19 -04001026 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001027 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001028
Chris Mason5f39d392007-10-15 16:14:19 -04001029 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001030
1031 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001033 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001034
1035 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001036 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001037 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1038 wret = 1;
1039 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001040 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001041 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001042 if (ret)
1043 wret = 1;
1044 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001045 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001046 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001047 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001048 }
Chris Masone66f7092007-04-20 13:16:02 -04001049 if (wret < 0)
1050 ret = wret;
1051 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001052 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001053 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001054 btrfs_node_key(mid, &disk_key, 0);
1055 btrfs_set_node_key(parent, &disk_key, pslot);
1056 btrfs_mark_buffer_dirty(parent);
1057 if (btrfs_header_nritems(left) > orig_slot) {
1058 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001059 path->slots[level + 1] -= 1;
1060 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001061 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001062 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001063 } else {
1064 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001065 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001066 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001067 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001068 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001069 }
Chris Masone66f7092007-04-20 13:16:02 -04001070 return 0;
1071 }
Chris Mason925baed2008-06-25 16:01:30 -04001072 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001073 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001074 }
Chris Mason925baed2008-06-25 16:01:30 -04001075 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001076
1077 /*
1078 * then try to empty the right most buffer into the middle
1079 */
Chris Mason5f39d392007-10-15 16:14:19 -04001080 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001081 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001082 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001083 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001084 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1085 wret = 1;
1086 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001087 ret = btrfs_cow_block(trans, root, right,
1088 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001089 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001090 if (ret)
1091 wret = 1;
1092 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001093 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001094 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001095 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001096 }
Chris Masone66f7092007-04-20 13:16:02 -04001097 if (wret < 0)
1098 ret = wret;
1099 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001100 struct btrfs_disk_key disk_key;
1101
1102 btrfs_node_key(right, &disk_key, 0);
1103 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1104 btrfs_mark_buffer_dirty(parent);
1105
1106 if (btrfs_header_nritems(mid) <= orig_slot) {
1107 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001108 path->slots[level + 1] += 1;
1109 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001110 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001111 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001112 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001113 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001114 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001115 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001116 }
Chris Masone66f7092007-04-20 13:16:02 -04001117 return 0;
1118 }
Chris Mason925baed2008-06-25 16:01:30 -04001119 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001120 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001121 }
Chris Masone66f7092007-04-20 13:16:02 -04001122 return 1;
1123}
1124
Chris Mason74123bd2007-02-02 11:05:29 -05001125/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001126 * readahead one full node of leaves
1127 */
Chris Masone02119d2008-09-05 16:13:11 -04001128static noinline void reada_for_search(struct btrfs_root *root,
1129 struct btrfs_path *path,
1130 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001131{
Chris Mason5f39d392007-10-15 16:14:19 -04001132 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001133 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001134 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001135 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001136 u64 lowest_read;
1137 u64 highest_read;
1138 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001139 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001141 u32 nr;
1142 u32 blocksize;
1143 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001144
Chris Masona6b6e752007-10-15 16:22:39 -04001145 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001146 return;
1147
Chris Mason6702ed42007-08-07 16:15:09 -04001148 if (!path->nodes[level])
1149 return;
1150
Chris Mason5f39d392007-10-15 16:14:19 -04001151 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001152
Chris Mason3c69fae2007-08-07 15:52:22 -04001153 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001154 blocksize = btrfs_level_size(root, level - 1);
1155 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 if (eb) {
1157 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001158 return;
1159 }
1160
Chris Mason6b800532007-10-15 16:17:34 -04001161 highest_read = search;
1162 lowest_read = search;
1163
Chris Mason5f39d392007-10-15 16:14:19 -04001164 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001165 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001166 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001167 if (direction < 0) {
1168 if (nr == 0)
1169 break;
1170 nr--;
1171 } else if (direction > 0) {
1172 nr++;
1173 if (nr >= nritems)
1174 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001175 }
Chris Mason01f46652007-12-21 16:24:26 -05001176 if (path->reada < 0 && objectid) {
1177 btrfs_node_key(node, &disk_key, nr);
1178 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1179 break;
1180 }
Chris Mason6b800532007-10-15 16:17:34 -04001181 search = btrfs_node_blockptr(node, nr);
1182 if ((search >= lowest_read && search <= highest_read) ||
1183 (search < lowest_read && lowest_read - search <= 32768) ||
1184 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001185 readahead_tree_block(root, search, blocksize,
1186 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001187 nread += blocksize;
1188 }
1189 nscan++;
1190 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1191 break;
1192 if(nread > (1024 * 1024) || nscan > 128)
1193 break;
1194
1195 if (search < lowest_read)
1196 lowest_read = search;
1197 if (search > highest_read)
1198 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001199 }
1200}
Chris Mason925baed2008-06-25 16:01:30 -04001201
Chris Masone02119d2008-09-05 16:13:11 -04001202static noinline void unlock_up(struct btrfs_path *path, int level,
1203 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001204{
1205 int i;
1206 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001207 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001208 struct extent_buffer *t;
1209
1210 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1211 if (!path->nodes[i])
1212 break;
1213 if (!path->locks[i])
1214 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001215 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001216 skip_level = i + 1;
1217 continue;
1218 }
Chris Mason051e1b92008-06-25 16:01:30 -04001219 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001220 u32 nritems;
1221 t = path->nodes[i];
1222 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001223 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001224 skip_level = i + 1;
1225 continue;
1226 }
1227 }
Chris Mason051e1b92008-06-25 16:01:30 -04001228 if (skip_level < i && i >= lowest_unlock)
1229 no_skips = 1;
1230
Chris Mason925baed2008-06-25 16:01:30 -04001231 t = path->nodes[i];
1232 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1233 btrfs_tree_unlock(t);
1234 path->locks[i] = 0;
1235 }
1236 }
1237}
1238
Chris Mason3c69fae2007-08-07 15:52:22 -04001239/*
Chris Mason74123bd2007-02-02 11:05:29 -05001240 * look for key in the tree. path is filled in with nodes along the way
1241 * if key is found, we return zero and you can find the item in the leaf
1242 * level of the path (level 0)
1243 *
1244 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001245 * be inserted, and 1 is returned. If there are other errors during the
1246 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001247 *
1248 * if ins_len > 0, nodes and leaves will be split as we walk down the
1249 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1250 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001251 */
Chris Masone089f052007-03-16 16:20:31 -04001252int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1253 *root, struct btrfs_key *key, struct btrfs_path *p, int
1254 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001255{
Chris Mason5f39d392007-10-15 16:14:19 -04001256 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001257 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001258 int slot;
1259 int ret;
1260 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001261 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001262 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001263 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001264 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001265 u64 blocknr;
1266 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001267 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001268
Chris Mason6702ed42007-08-07 16:15:09 -04001269 lowest_level = p->lowest_level;
1270 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001271 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001272 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001273 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001274 if (ins_len < 0)
1275 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001276
1277 prealloc_block.objectid = 0;
1278
Chris Masonbb803952007-03-01 12:04:21 -05001279again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001280 if (p->skip_locking)
1281 b = btrfs_root_node(root);
1282 else
1283 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001284
Chris Masoneb60cea2007-02-02 09:18:22 -05001285 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001286 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001287
1288 /*
1289 * setup the path here so we can release it under lock
1290 * contention with the cow code
1291 */
1292 p->nodes[level] = b;
1293 if (!p->skip_locking)
1294 p->locks[level] = 1;
1295
Chris Mason02217ed2007-03-02 16:08:05 -05001296 if (cow) {
1297 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001298
1299 /* is a cow on this block not required */
1300 spin_lock(&root->fs_info->hash_lock);
1301 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001302 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001303 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1304 spin_unlock(&root->fs_info->hash_lock);
1305 goto cow_done;
1306 }
1307 spin_unlock(&root->fs_info->hash_lock);
1308
1309 /* ok, we have to cow, is our old prealloc the right
1310 * size?
1311 */
1312 if (prealloc_block.objectid &&
1313 prealloc_block.offset != b->len) {
1314 btrfs_free_reserved_extent(root,
1315 prealloc_block.objectid,
1316 prealloc_block.offset);
1317 prealloc_block.objectid = 0;
1318 }
1319
1320 /*
1321 * for higher level blocks, try not to allocate blocks
1322 * with the block and the parent locks held.
1323 */
1324 if (level > 1 && !prealloc_block.objectid &&
1325 btrfs_path_lock_waiting(p, level)) {
1326 u32 size = b->len;
1327 u64 hint = b->start;
1328
1329 btrfs_release_path(root, p);
1330 ret = btrfs_reserve_extent(trans, root,
1331 size, size, 0,
1332 hint, (u64)-1,
1333 &prealloc_block, 0);
1334 BUG_ON(ret);
1335 goto again;
1336 }
1337
Chris Masone20d96d2007-03-22 12:13:20 -04001338 wret = btrfs_cow_block(trans, root, b,
1339 p->nodes[level + 1],
1340 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001341 &b, prealloc_block.objectid);
1342 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001343 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001344 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001345 ret = wret;
1346 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001347 }
Chris Mason02217ed2007-03-02 16:08:05 -05001348 }
Chris Mason65b51a02008-08-01 15:11:20 -04001349cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001350 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001351 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001352 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001353 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001354
Chris Masoneb60cea2007-02-02 09:18:22 -05001355 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001356 if (!p->skip_locking)
1357 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001358
Chris Mason123abc82007-03-14 14:14:43 -04001359 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001360 if (ret) {
1361 ret = -1;
1362 goto done;
1363 }
Chris Mason925baed2008-06-25 16:01:30 -04001364
Chris Mason5f39d392007-10-15 16:14:19 -04001365 ret = bin_search(b, key, level, &slot);
1366 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001367 if (ret && slot > 0)
1368 slot -= 1;
1369 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001371 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001372 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001373 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001374 if (sret) {
1375 ret = sret;
1376 goto done;
1377 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001378 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001379 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001380 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001381 int sret = balance_level(trans, root, p,
1382 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001383 if (sret) {
1384 ret = sret;
1385 goto done;
1386 }
Chris Masonbb803952007-03-01 12:04:21 -05001387 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001388 if (!b) {
1389 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001390 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001391 }
Chris Masonbb803952007-03-01 12:04:21 -05001392 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001393 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001394 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001395 unlock_up(p, level, lowest_unlock);
1396
Chris Mason9f3a7422007-08-07 15:52:19 -04001397 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001398 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001399 ret = 0;
1400 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001401 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001402
Chris Mason594a24e2008-06-25 16:01:30 -04001403 blocknr = btrfs_node_blockptr(b, slot);
1404 gen = btrfs_node_ptr_generation(b, slot);
1405 blocksize = btrfs_level_size(root, level - 1);
1406
1407 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1408 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001409 b = tmp;
1410 } else {
1411 /*
1412 * reduce lock contention at high levels
1413 * of the btree by dropping locks before
1414 * we read.
1415 */
1416 if (level > 1) {
1417 btrfs_release_path(NULL, p);
1418 if (tmp)
1419 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001420 if (should_reada)
1421 reada_for_search(root, p,
1422 level, slot,
1423 key->objectid);
1424
Chris Mason594a24e2008-06-25 16:01:30 -04001425 tmp = read_tree_block(root, blocknr,
1426 blocksize, gen);
1427 if (tmp)
1428 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001429 goto again;
1430 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001431 if (tmp)
1432 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001433 if (should_reada)
1434 reada_for_search(root, p,
1435 level, slot,
1436 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001437 b = read_node_slot(root, b, slot);
1438 }
1439 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001440 if (!p->skip_locking)
1441 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001442 } else {
1443 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001444 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001445 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001446 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001447 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001448 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001449 if (sret) {
1450 ret = sret;
1451 goto done;
1452 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001453 }
Chris Mason925baed2008-06-25 16:01:30 -04001454 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001455 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456 }
1457 }
Chris Mason65b51a02008-08-01 15:11:20 -04001458 ret = 1;
1459done:
1460 if (prealloc_block.objectid) {
1461 btrfs_free_reserved_extent(root,
1462 prealloc_block.objectid,
1463 prealloc_block.offset);
1464 }
1465
1466 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001467}
1468
Chris Mason74123bd2007-02-02 11:05:29 -05001469/*
1470 * adjust the pointers going up the tree, starting at level
1471 * making sure the right key of each node is points to 'key'.
1472 * This is used after shifting pointers to the left, so it stops
1473 * fixing up pointers when a given leaf/node is not in slot 0 of the
1474 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001475 *
1476 * If this fails to write a tree block, it returns -1, but continues
1477 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001478 */
Chris Mason5f39d392007-10-15 16:14:19 -04001479static int fixup_low_keys(struct btrfs_trans_handle *trans,
1480 struct btrfs_root *root, struct btrfs_path *path,
1481 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001482{
1483 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001484 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001485 struct extent_buffer *t;
1486
Chris Mason234b63a2007-03-13 10:46:10 -04001487 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001488 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001489 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001490 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001491 t = path->nodes[i];
1492 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001493 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001494 if (tslot != 0)
1495 break;
1496 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001497 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001498}
1499
Chris Mason74123bd2007-02-02 11:05:29 -05001500/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001501 * update item key.
1502 *
1503 * This function isn't completely safe. It's the caller's responsibility
1504 * that the new key won't break the order
1505 */
1506int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1507 struct btrfs_root *root, struct btrfs_path *path,
1508 struct btrfs_key *new_key)
1509{
1510 struct btrfs_disk_key disk_key;
1511 struct extent_buffer *eb;
1512 int slot;
1513
1514 eb = path->nodes[0];
1515 slot = path->slots[0];
1516 if (slot > 0) {
1517 btrfs_item_key(eb, &disk_key, slot - 1);
1518 if (comp_keys(&disk_key, new_key) >= 0)
1519 return -1;
1520 }
1521 if (slot < btrfs_header_nritems(eb) - 1) {
1522 btrfs_item_key(eb, &disk_key, slot + 1);
1523 if (comp_keys(&disk_key, new_key) <= 0)
1524 return -1;
1525 }
1526
1527 btrfs_cpu_key_to_disk(&disk_key, new_key);
1528 btrfs_set_item_key(eb, &disk_key, slot);
1529 btrfs_mark_buffer_dirty(eb);
1530 if (slot == 0)
1531 fixup_low_keys(trans, root, path, &disk_key, 1);
1532 return 0;
1533}
1534
1535/*
Chris Mason74123bd2007-02-02 11:05:29 -05001536 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001537 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001538 *
1539 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1540 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001541 */
Chris Mason98ed5172008-01-03 10:01:48 -05001542static int push_node_left(struct btrfs_trans_handle *trans,
1543 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001544 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001545{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001546 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001547 int src_nritems;
1548 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001549 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001550
Chris Mason5f39d392007-10-15 16:14:19 -04001551 src_nritems = btrfs_header_nritems(src);
1552 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001553 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001554 WARN_ON(btrfs_header_generation(src) != trans->transid);
1555 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001556
Chris Masonbce4eae2008-04-24 14:42:46 -04001557 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001558 return 1;
1559
Chris Masoneb60cea2007-02-02 09:18:22 -05001560 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001561 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001562 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563
Chris Masonbce4eae2008-04-24 14:42:46 -04001564 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001565 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001566 if (push_items < src_nritems) {
1567 /* leave at least 8 pointers in the node if
1568 * we aren't going to empty it
1569 */
1570 if (src_nritems - push_items < 8) {
1571 if (push_items <= 8)
1572 return 1;
1573 push_items -= 8;
1574 }
1575 }
1576 } else
1577 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001578
Chris Mason5f39d392007-10-15 16:14:19 -04001579 copy_extent_buffer(dst, src,
1580 btrfs_node_key_ptr_offset(dst_nritems),
1581 btrfs_node_key_ptr_offset(0),
1582 push_items * sizeof(struct btrfs_key_ptr));
1583
Chris Masonbb803952007-03-01 12:04:21 -05001584 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001585 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1586 btrfs_node_key_ptr_offset(push_items),
1587 (src_nritems - push_items) *
1588 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001589 }
Chris Mason5f39d392007-10-15 16:14:19 -04001590 btrfs_set_header_nritems(src, src_nritems - push_items);
1591 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1592 btrfs_mark_buffer_dirty(src);
1593 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001594
1595 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1596 BUG_ON(ret);
1597
Chris Masonbb803952007-03-01 12:04:21 -05001598 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001599}
1600
Chris Mason97571fd2007-02-24 13:39:08 -05001601/*
Chris Mason79f95c82007-03-01 15:16:26 -05001602 * try to push data from one node into the next node right in the
1603 * tree.
1604 *
1605 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1606 * error, and > 0 if there was no room in the right hand block.
1607 *
1608 * this will only push up to 1/2 the contents of the left node over
1609 */
Chris Mason5f39d392007-10-15 16:14:19 -04001610static int balance_node_right(struct btrfs_trans_handle *trans,
1611 struct btrfs_root *root,
1612 struct extent_buffer *dst,
1613 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001614{
Chris Mason79f95c82007-03-01 15:16:26 -05001615 int push_items = 0;
1616 int max_push;
1617 int src_nritems;
1618 int dst_nritems;
1619 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001620
Chris Mason7bb86312007-12-11 09:25:06 -05001621 WARN_ON(btrfs_header_generation(src) != trans->transid);
1622 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1623
Chris Mason5f39d392007-10-15 16:14:19 -04001624 src_nritems = btrfs_header_nritems(src);
1625 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001626 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001627 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001628 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001629 }
1630
1631 if (src_nritems < 4) {
1632 return 1;
1633 }
Chris Mason79f95c82007-03-01 15:16:26 -05001634
1635 max_push = src_nritems / 2 + 1;
1636 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001637 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001638 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001639 }
Yan252c38f2007-08-29 09:11:44 -04001640
Chris Mason79f95c82007-03-01 15:16:26 -05001641 if (max_push < push_items)
1642 push_items = max_push;
1643
Chris Mason5f39d392007-10-15 16:14:19 -04001644 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1645 btrfs_node_key_ptr_offset(0),
1646 (dst_nritems) *
1647 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001648
Chris Mason5f39d392007-10-15 16:14:19 -04001649 copy_extent_buffer(dst, src,
1650 btrfs_node_key_ptr_offset(0),
1651 btrfs_node_key_ptr_offset(src_nritems - push_items),
1652 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001653
Chris Mason5f39d392007-10-15 16:14:19 -04001654 btrfs_set_header_nritems(src, src_nritems - push_items);
1655 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001656
Chris Mason5f39d392007-10-15 16:14:19 -04001657 btrfs_mark_buffer_dirty(src);
1658 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001659
1660 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1661 BUG_ON(ret);
1662
Chris Mason79f95c82007-03-01 15:16:26 -05001663 return ret;
1664}
1665
1666/*
Chris Mason97571fd2007-02-24 13:39:08 -05001667 * helper function to insert a new root level in the tree.
1668 * A new node is allocated, and a single item is inserted to
1669 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001670 *
1671 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001672 */
Chris Mason98ed5172008-01-03 10:01:48 -05001673static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001674 struct btrfs_root *root,
1675 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001676{
Chris Mason7bb86312007-12-11 09:25:06 -05001677 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001678 struct extent_buffer *lower;
1679 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001680 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001681 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001682 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001683
1684 BUG_ON(path->nodes[level]);
1685 BUG_ON(path->nodes[level-1] != root->node);
1686
Chris Mason7bb86312007-12-11 09:25:06 -05001687 lower = path->nodes[level-1];
1688 if (level == 1)
1689 btrfs_item_key(lower, &lower_key, 0);
1690 else
1691 btrfs_node_key(lower, &lower_key, 0);
1692
Zheng Yan31840ae2008-09-23 13:14:14 -04001693 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1694 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001695 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001696 if (IS_ERR(c))
1697 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001698
Chris Mason5f39d392007-10-15 16:14:19 -04001699 memset_extent_buffer(c, 0, 0, root->nodesize);
1700 btrfs_set_header_nritems(c, 1);
1701 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001702 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001703 btrfs_set_header_generation(c, trans->transid);
1704 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001705
Chris Mason5f39d392007-10-15 16:14:19 -04001706 write_extent_buffer(c, root->fs_info->fsid,
1707 (unsigned long)btrfs_header_fsid(c),
1708 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001709
1710 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1711 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1712 BTRFS_UUID_SIZE);
1713
Chris Mason5f39d392007-10-15 16:14:19 -04001714 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001715 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001716 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001717 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001718
1719 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001720
1721 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001722
Chris Mason925baed2008-06-25 16:01:30 -04001723 spin_lock(&root->node_lock);
1724 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001725 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001726 spin_unlock(&root->node_lock);
1727
Zheng Yan31840ae2008-09-23 13:14:14 -04001728 ret = btrfs_update_extent_ref(trans, root, lower->start,
1729 lower->start, c->start,
1730 root->root_key.objectid,
1731 trans->transid, level - 1, 0);
1732 BUG_ON(ret);
1733
Chris Mason925baed2008-06-25 16:01:30 -04001734 /* the super has an extra ref to root->node */
1735 free_extent_buffer(old);
1736
Chris Mason0b86a832008-03-24 15:01:56 -04001737 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001738 extent_buffer_get(c);
1739 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001740 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001741 path->slots[level] = 0;
1742 return 0;
1743}
1744
Chris Mason74123bd2007-02-02 11:05:29 -05001745/*
1746 * worker function to insert a single pointer in a node.
1747 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001748 *
Chris Mason74123bd2007-02-02 11:05:29 -05001749 * slot and level indicate where you want the key to go, and
1750 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001751 *
1752 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001753 */
Chris Masone089f052007-03-16 16:20:31 -04001754static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1755 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001756 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001757{
Chris Mason5f39d392007-10-15 16:14:19 -04001758 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001759 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001760
1761 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001762 lower = path->nodes[level];
1763 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001764 if (slot > nritems)
1765 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001766 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001767 BUG();
1768 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001769 memmove_extent_buffer(lower,
1770 btrfs_node_key_ptr_offset(slot + 1),
1771 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001772 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001773 }
Chris Mason5f39d392007-10-15 16:14:19 -04001774 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001775 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001776 WARN_ON(trans->transid == 0);
1777 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001778 btrfs_set_header_nritems(lower, nritems + 1);
1779 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001780 return 0;
1781}
1782
Chris Mason97571fd2007-02-24 13:39:08 -05001783/*
1784 * split the node at the specified level in path in two.
1785 * The path is corrected to point to the appropriate node after the split
1786 *
1787 * Before splitting this tries to make some room in the node by pushing
1788 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001789 *
1790 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001791 */
Chris Masone02119d2008-09-05 16:13:11 -04001792static noinline int split_node(struct btrfs_trans_handle *trans,
1793 struct btrfs_root *root,
1794 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001795{
Chris Mason5f39d392007-10-15 16:14:19 -04001796 struct extent_buffer *c;
1797 struct extent_buffer *split;
1798 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001799 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001800 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001801 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001802 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001803
Chris Mason5f39d392007-10-15 16:14:19 -04001804 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001805 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001806 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001807 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001808 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001809 if (ret)
1810 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001811 } else {
1812 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001813 c = path->nodes[level];
1814 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001815 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001816 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001817 if (ret < 0)
1818 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001819 }
Chris Masone66f7092007-04-20 13:16:02 -04001820
Chris Mason5f39d392007-10-15 16:14:19 -04001821 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001822
Chris Mason925baed2008-06-25 16:01:30 -04001823 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001824 path->nodes[level + 1]->start,
1825 root->root_key.objectid,
1826 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001827 if (IS_ERR(split))
1828 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001829
Chris Mason5f39d392007-10-15 16:14:19 -04001830 btrfs_set_header_flags(split, btrfs_header_flags(c));
1831 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001832 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001833 btrfs_set_header_generation(split, trans->transid);
1834 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001835 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001836 write_extent_buffer(split, root->fs_info->fsid,
1837 (unsigned long)btrfs_header_fsid(split),
1838 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001839 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1840 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1841 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001842
Chris Mason7518a232007-03-12 12:01:18 -04001843 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001844
1845 copy_extent_buffer(split, c,
1846 btrfs_node_key_ptr_offset(0),
1847 btrfs_node_key_ptr_offset(mid),
1848 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1849 btrfs_set_header_nritems(split, c_nritems - mid);
1850 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001851 ret = 0;
1852
Chris Mason5f39d392007-10-15 16:14:19 -04001853 btrfs_mark_buffer_dirty(c);
1854 btrfs_mark_buffer_dirty(split);
1855
1856 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001857 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001858 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001859 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001860 if (wret)
1861 ret = wret;
1862
Zheng Yan31840ae2008-09-23 13:14:14 -04001863 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
1864 BUG_ON(ret);
1865
Chris Mason5de08d72007-02-24 06:24:44 -05001866 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001867 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001868 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001869 free_extent_buffer(c);
1870 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001871 path->slots[level + 1] += 1;
1872 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001873 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001874 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001876 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877}
1878
Chris Mason74123bd2007-02-02 11:05:29 -05001879/*
1880 * how many bytes are required to store the items in a leaf. start
1881 * and nr indicate which items in the leaf to check. This totals up the
1882 * space used both by the item structs and the item data
1883 */
Chris Mason5f39d392007-10-15 16:14:19 -04001884static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001885{
1886 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001887 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001888 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001889
1890 if (!nr)
1891 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 data_len = btrfs_item_end_nr(l, start);
1893 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001894 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001895 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001896 return data_len;
1897}
1898
Chris Mason74123bd2007-02-02 11:05:29 -05001899/*
Chris Masond4dbff92007-04-04 14:08:15 -04001900 * The space between the end of the leaf items and
1901 * the start of the leaf data. IOW, how much room
1902 * the leaf has left for both items and data
1903 */
Chris Masone02119d2008-09-05 16:13:11 -04001904int noinline btrfs_leaf_free_space(struct btrfs_root *root,
1905 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001906{
Chris Mason5f39d392007-10-15 16:14:19 -04001907 int nritems = btrfs_header_nritems(leaf);
1908 int ret;
1909 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1910 if (ret < 0) {
1911 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001912 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001913 leaf_space_used(leaf, 0, nritems), nritems);
1914 }
1915 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001916}
1917
1918/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001919 * push some data in the path leaf to the right, trying to free up at
1920 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001921 *
1922 * returns 1 if the push failed because the other node didn't have enough
1923 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001924 */
Chris Masone089f052007-03-16 16:20:31 -04001925static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001926 *root, struct btrfs_path *path, int data_size,
1927 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001928{
Chris Mason5f39d392007-10-15 16:14:19 -04001929 struct extent_buffer *left = path->nodes[0];
1930 struct extent_buffer *right;
1931 struct extent_buffer *upper;
1932 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001933 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001934 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001935 int free_space;
1936 int push_space = 0;
1937 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001938 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001939 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001940 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001941 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001942 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001943 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001944 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001945
1946 slot = path->slots[1];
1947 if (!path->nodes[1]) {
1948 return 1;
1949 }
1950 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001951 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001952 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001953
Chris Masona2135012008-06-25 16:01:30 -04001954 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1955
Chris Masonca7a79a2008-05-12 12:59:19 -04001956 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001957 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001958 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001959 if (free_space < data_size + sizeof(struct btrfs_item))
1960 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001961
Chris Mason5f39d392007-10-15 16:14:19 -04001962 /* cow and double check */
1963 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04001964 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001965 if (ret)
1966 goto out_unlock;
1967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001969 if (free_space < data_size + sizeof(struct btrfs_item))
1970 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001971
1972 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001973 if (left_nritems == 0)
1974 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001975
Chris Mason34a38212007-11-07 13:31:03 -05001976 if (empty)
1977 nr = 0;
1978 else
1979 nr = 1;
1980
Zheng Yan31840ae2008-09-23 13:14:14 -04001981 if (path->slots[0] >= left_nritems)
1982 push_space += data_size + sizeof(*item);
1983
Chris Mason34a38212007-11-07 13:31:03 -05001984 i = left_nritems - 1;
1985 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001986 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001987
Zheng Yan31840ae2008-09-23 13:14:14 -04001988 if (!empty && push_items > 0) {
1989 if (path->slots[0] > i)
1990 break;
1991 if (path->slots[0] == i) {
1992 int space = btrfs_leaf_free_space(root, left);
1993 if (space + push_space * 2 > free_space)
1994 break;
1995 }
1996 }
1997
Chris Mason00ec4c52007-02-24 12:47:20 -05001998 if (path->slots[0] == i)
1999 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002000
2001 if (!left->map_token) {
2002 map_extent_buffer(left, (unsigned long)item,
2003 sizeof(struct btrfs_item),
2004 &left->map_token, &left->kaddr,
2005 &left->map_start, &left->map_len,
2006 KM_USER1);
2007 }
2008
2009 this_item_size = btrfs_item_size(left, item);
2010 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002011 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002012
Chris Mason00ec4c52007-02-24 12:47:20 -05002013 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002014 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002015 if (i == 0)
2016 break;
2017 i--;
Chris Masondb945352007-10-15 16:15:53 -04002018 }
2019 if (left->map_token) {
2020 unmap_extent_buffer(left, left->map_token, KM_USER1);
2021 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002022 }
Chris Mason5f39d392007-10-15 16:14:19 -04002023
Chris Mason925baed2008-06-25 16:01:30 -04002024 if (push_items == 0)
2025 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002026
Chris Mason34a38212007-11-07 13:31:03 -05002027 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002028 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002029
Chris Mason00ec4c52007-02-24 12:47:20 -05002030 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002031 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002032
Chris Mason5f39d392007-10-15 16:14:19 -04002033 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002034 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002035
Chris Mason00ec4c52007-02-24 12:47:20 -05002036 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002037 data_end = leaf_data_end(root, right);
2038 memmove_extent_buffer(right,
2039 btrfs_leaf_data(right) + data_end - push_space,
2040 btrfs_leaf_data(right) + data_end,
2041 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2042
Chris Mason00ec4c52007-02-24 12:47:20 -05002043 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002044 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002045 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2046 btrfs_leaf_data(left) + leaf_data_end(root, left),
2047 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002048
2049 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2050 btrfs_item_nr_offset(0),
2051 right_nritems * sizeof(struct btrfs_item));
2052
Chris Mason00ec4c52007-02-24 12:47:20 -05002053 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002054 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2055 btrfs_item_nr_offset(left_nritems - push_items),
2056 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002057
2058 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002059 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002060 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002061 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002062 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002063 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002064 if (!right->map_token) {
2065 map_extent_buffer(right, (unsigned long)item,
2066 sizeof(struct btrfs_item),
2067 &right->map_token, &right->kaddr,
2068 &right->map_start, &right->map_len,
2069 KM_USER1);
2070 }
2071 push_space -= btrfs_item_size(right, item);
2072 btrfs_set_item_offset(right, item, push_space);
2073 }
2074
2075 if (right->map_token) {
2076 unmap_extent_buffer(right, right->map_token, KM_USER1);
2077 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002078 }
Chris Mason7518a232007-03-12 12:01:18 -04002079 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002080 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002081
Chris Mason34a38212007-11-07 13:31:03 -05002082 if (left_nritems)
2083 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002085
Zheng Yan31840ae2008-09-23 13:14:14 -04002086 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2087 BUG_ON(ret);
2088
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_item_key(right, &disk_key, 0);
2090 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002091 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002092
Chris Mason00ec4c52007-02-24 12:47:20 -05002093 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002094 if (path->slots[0] >= left_nritems) {
2095 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002096 if (btrfs_header_nritems(path->nodes[0]) == 0)
2097 clean_tree_block(trans, root, path->nodes[0]);
2098 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 free_extent_buffer(path->nodes[0]);
2100 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002101 path->slots[1] += 1;
2102 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002103 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002104 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002105 }
2106 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002107
2108out_unlock:
2109 btrfs_tree_unlock(right);
2110 free_extent_buffer(right);
2111 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002112}
Chris Mason925baed2008-06-25 16:01:30 -04002113
Chris Mason00ec4c52007-02-24 12:47:20 -05002114/*
Chris Mason74123bd2007-02-02 11:05:29 -05002115 * push some data in the path leaf to the left, trying to free up at
2116 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2117 */
Chris Masone089f052007-03-16 16:20:31 -04002118static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002119 *root, struct btrfs_path *path, int data_size,
2120 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002121{
Chris Mason5f39d392007-10-15 16:14:19 -04002122 struct btrfs_disk_key disk_key;
2123 struct extent_buffer *right = path->nodes[0];
2124 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002125 int slot;
2126 int i;
2127 int free_space;
2128 int push_space = 0;
2129 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002130 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002131 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002132 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002133 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002134 int ret = 0;
2135 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002136 u32 this_item_size;
2137 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002138
2139 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002140 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002141 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002142 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002143 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002144
Chris Mason3685f792007-10-19 09:23:27 -04002145 right_nritems = btrfs_header_nritems(right);
2146 if (right_nritems == 0) {
2147 return 1;
2148 }
2149
Chris Masona2135012008-06-25 16:01:30 -04002150 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2151
Chris Masonca7a79a2008-05-12 12:59:19 -04002152 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002153 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002154 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002155 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002156 ret = 1;
2157 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002158 }
Chris Mason02217ed2007-03-02 16:08:05 -05002159
2160 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002161 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002162 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002163 if (ret) {
2164 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002165 ret = 1;
2166 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002167 }
Chris Mason3685f792007-10-19 09:23:27 -04002168
Chris Mason123abc82007-03-14 14:14:43 -04002169 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002170 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002171 ret = 1;
2172 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002173 }
2174
Chris Mason34a38212007-11-07 13:31:03 -05002175 if (empty)
2176 nr = right_nritems;
2177 else
2178 nr = right_nritems - 1;
2179
2180 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002181 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002182 if (!right->map_token) {
2183 map_extent_buffer(right, (unsigned long)item,
2184 sizeof(struct btrfs_item),
2185 &right->map_token, &right->kaddr,
2186 &right->map_start, &right->map_len,
2187 KM_USER1);
2188 }
2189
Zheng Yan31840ae2008-09-23 13:14:14 -04002190 if (!empty && push_items > 0) {
2191 if (path->slots[0] < i)
2192 break;
2193 if (path->slots[0] == i) {
2194 int space = btrfs_leaf_free_space(root, right);
2195 if (space + push_space * 2 > free_space)
2196 break;
2197 }
2198 }
2199
Chris Masonbe0e5c02007-01-26 15:51:26 -05002200 if (path->slots[0] == i)
2201 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002202
2203 this_item_size = btrfs_item_size(right, item);
2204 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002205 break;
Chris Masondb945352007-10-15 16:15:53 -04002206
Chris Masonbe0e5c02007-01-26 15:51:26 -05002207 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002208 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002209 }
Chris Masondb945352007-10-15 16:15:53 -04002210
2211 if (right->map_token) {
2212 unmap_extent_buffer(right, right->map_token, KM_USER1);
2213 right->map_token = NULL;
2214 }
2215
Chris Masonbe0e5c02007-01-26 15:51:26 -05002216 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002217 ret = 1;
2218 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002219 }
Chris Mason34a38212007-11-07 13:31:03 -05002220 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002221 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002222
Chris Masonbe0e5c02007-01-26 15:51:26 -05002223 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002224 copy_extent_buffer(left, right,
2225 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2226 btrfs_item_nr_offset(0),
2227 push_items * sizeof(struct btrfs_item));
2228
Chris Mason123abc82007-03-14 14:14:43 -04002229 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002230 btrfs_item_offset_nr(right, push_items -1);
2231
2232 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002233 leaf_data_end(root, left) - push_space,
2234 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002235 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002236 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002238 BUG_ON(old_left_nritems < 0);
2239
Chris Masondb945352007-10-15 16:15:53 -04002240 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002241 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002242 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002243
Chris Mason5f39d392007-10-15 16:14:19 -04002244 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002245 if (!left->map_token) {
2246 map_extent_buffer(left, (unsigned long)item,
2247 sizeof(struct btrfs_item),
2248 &left->map_token, &left->kaddr,
2249 &left->map_start, &left->map_len,
2250 KM_USER1);
2251 }
2252
Chris Mason5f39d392007-10-15 16:14:19 -04002253 ioff = btrfs_item_offset(left, item);
2254 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002255 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002256 }
Chris Mason5f39d392007-10-15 16:14:19 -04002257 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002258 if (left->map_token) {
2259 unmap_extent_buffer(left, left->map_token, KM_USER1);
2260 left->map_token = NULL;
2261 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002262
2263 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002264 if (push_items > right_nritems) {
2265 printk("push items %d nr %u\n", push_items, right_nritems);
2266 WARN_ON(1);
2267 }
Chris Mason5f39d392007-10-15 16:14:19 -04002268
Chris Mason34a38212007-11-07 13:31:03 -05002269 if (push_items < right_nritems) {
2270 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2271 leaf_data_end(root, right);
2272 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2273 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2274 btrfs_leaf_data(right) +
2275 leaf_data_end(root, right), push_space);
2276
2277 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002278 btrfs_item_nr_offset(push_items),
2279 (btrfs_header_nritems(right) - push_items) *
2280 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002281 }
Yaneef1c492007-11-26 10:58:13 -05002282 right_nritems -= push_items;
2283 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002284 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002285 for (i = 0; i < right_nritems; i++) {
2286 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002287
2288 if (!right->map_token) {
2289 map_extent_buffer(right, (unsigned long)item,
2290 sizeof(struct btrfs_item),
2291 &right->map_token, &right->kaddr,
2292 &right->map_start, &right->map_len,
2293 KM_USER1);
2294 }
2295
2296 push_space = push_space - btrfs_item_size(right, item);
2297 btrfs_set_item_offset(right, item, push_space);
2298 }
2299 if (right->map_token) {
2300 unmap_extent_buffer(right, right->map_token, KM_USER1);
2301 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002302 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002303
Chris Mason5f39d392007-10-15 16:14:19 -04002304 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002305 if (right_nritems)
2306 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002307
Zheng Yan31840ae2008-09-23 13:14:14 -04002308 ret = btrfs_update_ref(trans, root, right, left,
2309 old_left_nritems, push_items);
2310 BUG_ON(ret);
2311
Chris Mason5f39d392007-10-15 16:14:19 -04002312 btrfs_item_key(right, &disk_key, 0);
2313 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002314 if (wret)
2315 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002316
2317 /* then fixup the leaf pointer in the path */
2318 if (path->slots[0] < push_items) {
2319 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002320 if (btrfs_header_nritems(path->nodes[0]) == 0)
2321 clean_tree_block(trans, root, path->nodes[0]);
2322 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002323 free_extent_buffer(path->nodes[0]);
2324 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002325 path->slots[1] -= 1;
2326 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002327 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002328 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002329 path->slots[0] -= push_items;
2330 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002331 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002332 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002333out:
2334 btrfs_tree_unlock(left);
2335 free_extent_buffer(left);
2336 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002337}
2338
Chris Mason74123bd2007-02-02 11:05:29 -05002339/*
2340 * split the path's leaf in two, making sure there is at least data_size
2341 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002342 *
2343 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002344 */
Chris Masone02119d2008-09-05 16:13:11 -04002345static noinline int split_leaf(struct btrfs_trans_handle *trans,
2346 struct btrfs_root *root,
2347 struct btrfs_key *ins_key,
2348 struct btrfs_path *path, int data_size,
2349 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002350{
Chris Mason5f39d392007-10-15 16:14:19 -04002351 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002352 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002353 int mid;
2354 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002355 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002356 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002357 int data_copy_size;
2358 int rt_data_off;
2359 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002360 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002361 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002362 int double_split;
2363 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002364 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002365
Chris Masoncc0c5532007-10-25 15:42:57 -04002366 if (extend)
2367 space_needed = data_size;
2368
Chris Mason40689472007-03-17 14:29:23 -04002369 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002370 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002371 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002372 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002373 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002374 }
2375 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002376 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002377 if (wret < 0)
2378 return wret;
2379 }
2380 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002381
Chris Mason3685f792007-10-19 09:23:27 -04002382 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002383 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002384 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002385 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002386
Chris Mason5c680ed2007-02-22 11:39:13 -05002387 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002388 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002389 if (ret)
2390 return ret;
2391 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002392again:
2393 double_split = 0;
2394 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002395 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002396 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002397 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002398
Chris Mason925baed2008-06-25 16:01:30 -04002399 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002400 path->nodes[1]->start,
2401 root->root_key.objectid,
2402 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002403 if (IS_ERR(right)) {
2404 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002405 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002406 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002407
Chris Mason5f39d392007-10-15 16:14:19 -04002408 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002409 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002410 btrfs_set_header_generation(right, trans->transid);
2411 btrfs_set_header_owner(right, root->root_key.objectid);
2412 btrfs_set_header_level(right, 0);
2413 write_extent_buffer(right, root->fs_info->fsid,
2414 (unsigned long)btrfs_header_fsid(right),
2415 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002416
2417 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2418 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2419 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002420 if (mid <= slot) {
2421 if (nritems == 1 ||
2422 leaf_space_used(l, mid, nritems - mid) + space_needed >
2423 BTRFS_LEAF_DATA_SIZE(root)) {
2424 if (slot >= nritems) {
2425 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002426 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002427 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002428 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002429 path->slots[1] + 1, 1);
2430 if (wret)
2431 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002432
2433 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002434 free_extent_buffer(path->nodes[0]);
2435 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002436 path->slots[0] = 0;
2437 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002438 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002439 return ret;
2440 }
2441 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002442 if (mid != nritems &&
2443 leaf_space_used(l, mid, nritems - mid) +
2444 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2445 double_split = 1;
2446 }
Chris Masond4dbff92007-04-04 14:08:15 -04002447 }
2448 } else {
2449 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2450 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002451 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002452 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002453 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002454 wret = insert_ptr(trans, root, path,
2455 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002456 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002457 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002458 if (wret)
2459 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002460 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002461 free_extent_buffer(path->nodes[0]);
2462 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002463 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002464 if (path->slots[1] == 0) {
2465 wret = fixup_low_keys(trans, root,
2466 path, &disk_key, 1);
2467 if (wret)
2468 ret = wret;
2469 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002470 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002471 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002472 } else if (extend && slot == 0) {
2473 mid = 1;
2474 } else {
2475 mid = slot;
2476 if (mid != nritems &&
2477 leaf_space_used(l, mid, nritems - mid) +
2478 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2479 double_split = 1;
2480 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002481 }
Chris Masond4dbff92007-04-04 14:08:15 -04002482 }
2483 }
Chris Mason5f39d392007-10-15 16:14:19 -04002484 nritems = nritems - mid;
2485 btrfs_set_header_nritems(right, nritems);
2486 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2487
2488 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2489 btrfs_item_nr_offset(mid),
2490 nritems * sizeof(struct btrfs_item));
2491
2492 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002493 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2494 data_copy_size, btrfs_leaf_data(l) +
2495 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002496
Chris Mason5f39d392007-10-15 16:14:19 -04002497 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2498 btrfs_item_end_nr(l, mid);
2499
2500 for (i = 0; i < nritems; i++) {
2501 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002502 u32 ioff;
2503
2504 if (!right->map_token) {
2505 map_extent_buffer(right, (unsigned long)item,
2506 sizeof(struct btrfs_item),
2507 &right->map_token, &right->kaddr,
2508 &right->map_start, &right->map_len,
2509 KM_USER1);
2510 }
2511
2512 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002513 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002514 }
Chris Mason74123bd2007-02-02 11:05:29 -05002515
Chris Masondb945352007-10-15 16:15:53 -04002516 if (right->map_token) {
2517 unmap_extent_buffer(right, right->map_token, KM_USER1);
2518 right->map_token = NULL;
2519 }
2520
Chris Mason5f39d392007-10-15 16:14:19 -04002521 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002522 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002523 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002524 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2525 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002526 if (wret)
2527 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002528
2529 btrfs_mark_buffer_dirty(right);
2530 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002531 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002532
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2534 BUG_ON(ret);
2535
Chris Masonbe0e5c02007-01-26 15:51:26 -05002536 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002537 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002538 free_extent_buffer(path->nodes[0]);
2539 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002540 path->slots[0] -= mid;
2541 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002542 } else {
2543 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002544 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002545 }
Chris Mason5f39d392007-10-15 16:14:19 -04002546
Chris Masoneb60cea2007-02-02 09:18:22 -05002547 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002548
Chris Masoncc0c5532007-10-25 15:42:57 -04002549 if (double_split) {
2550 BUG_ON(num_doubles != 0);
2551 num_doubles++;
2552 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002553 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 return ret;
2555}
2556
Chris Masonb18c6682007-04-17 13:26:50 -04002557int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2558 struct btrfs_root *root,
2559 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002560 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002561{
2562 int ret = 0;
2563 int slot;
2564 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002565 struct extent_buffer *leaf;
2566 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002567 u32 nritems;
2568 unsigned int data_end;
2569 unsigned int old_data_start;
2570 unsigned int old_size;
2571 unsigned int size_diff;
2572 int i;
2573
2574 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002575 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002576 slot = path->slots[0];
2577
2578 old_size = btrfs_item_size_nr(leaf, slot);
2579 if (old_size == new_size)
2580 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002581
Chris Mason5f39d392007-10-15 16:14:19 -04002582 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002583 data_end = leaf_data_end(root, leaf);
2584
Chris Mason5f39d392007-10-15 16:14:19 -04002585 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002586
Chris Masonb18c6682007-04-17 13:26:50 -04002587 size_diff = old_size - new_size;
2588
2589 BUG_ON(slot < 0);
2590 BUG_ON(slot >= nritems);
2591
2592 /*
2593 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2594 */
2595 /* first correct the data pointers */
2596 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002597 u32 ioff;
2598 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002599
2600 if (!leaf->map_token) {
2601 map_extent_buffer(leaf, (unsigned long)item,
2602 sizeof(struct btrfs_item),
2603 &leaf->map_token, &leaf->kaddr,
2604 &leaf->map_start, &leaf->map_len,
2605 KM_USER1);
2606 }
2607
Chris Mason5f39d392007-10-15 16:14:19 -04002608 ioff = btrfs_item_offset(leaf, item);
2609 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002610 }
Chris Masondb945352007-10-15 16:15:53 -04002611
2612 if (leaf->map_token) {
2613 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2614 leaf->map_token = NULL;
2615 }
2616
Chris Masonb18c6682007-04-17 13:26:50 -04002617 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002618 if (from_end) {
2619 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2620 data_end + size_diff, btrfs_leaf_data(leaf) +
2621 data_end, old_data_start + new_size - data_end);
2622 } else {
2623 struct btrfs_disk_key disk_key;
2624 u64 offset;
2625
2626 btrfs_item_key(leaf, &disk_key, slot);
2627
2628 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2629 unsigned long ptr;
2630 struct btrfs_file_extent_item *fi;
2631
2632 fi = btrfs_item_ptr(leaf, slot,
2633 struct btrfs_file_extent_item);
2634 fi = (struct btrfs_file_extent_item *)(
2635 (unsigned long)fi - size_diff);
2636
2637 if (btrfs_file_extent_type(leaf, fi) ==
2638 BTRFS_FILE_EXTENT_INLINE) {
2639 ptr = btrfs_item_ptr_offset(leaf, slot);
2640 memmove_extent_buffer(leaf, ptr,
2641 (unsigned long)fi,
2642 offsetof(struct btrfs_file_extent_item,
2643 disk_bytenr));
2644 }
2645 }
2646
2647 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2648 data_end + size_diff, btrfs_leaf_data(leaf) +
2649 data_end, old_data_start - data_end);
2650
2651 offset = btrfs_disk_key_offset(&disk_key);
2652 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2653 btrfs_set_item_key(leaf, &disk_key, slot);
2654 if (slot == 0)
2655 fixup_low_keys(trans, root, path, &disk_key, 1);
2656 }
Chris Mason5f39d392007-10-15 16:14:19 -04002657
2658 item = btrfs_item_nr(leaf, slot);
2659 btrfs_set_item_size(leaf, item, new_size);
2660 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002661
2662 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002663 if (btrfs_leaf_free_space(root, leaf) < 0) {
2664 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002665 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002666 }
Chris Masonb18c6682007-04-17 13:26:50 -04002667 return ret;
2668}
2669
Chris Mason5f39d392007-10-15 16:14:19 -04002670int btrfs_extend_item(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *root, struct btrfs_path *path,
2672 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002673{
2674 int ret = 0;
2675 int slot;
2676 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002677 struct extent_buffer *leaf;
2678 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002679 u32 nritems;
2680 unsigned int data_end;
2681 unsigned int old_data;
2682 unsigned int old_size;
2683 int i;
2684
2685 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002686 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002687
Chris Mason5f39d392007-10-15 16:14:19 -04002688 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002689 data_end = leaf_data_end(root, leaf);
2690
Chris Mason5f39d392007-10-15 16:14:19 -04002691 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2692 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002693 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002694 }
Chris Mason6567e832007-04-16 09:22:45 -04002695 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002696 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002697
2698 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002699 if (slot >= nritems) {
2700 btrfs_print_leaf(root, leaf);
2701 printk("slot %d too large, nritems %d\n", slot, nritems);
2702 BUG_ON(1);
2703 }
Chris Mason6567e832007-04-16 09:22:45 -04002704
2705 /*
2706 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2707 */
2708 /* first correct the data pointers */
2709 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002710 u32 ioff;
2711 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002712
2713 if (!leaf->map_token) {
2714 map_extent_buffer(leaf, (unsigned long)item,
2715 sizeof(struct btrfs_item),
2716 &leaf->map_token, &leaf->kaddr,
2717 &leaf->map_start, &leaf->map_len,
2718 KM_USER1);
2719 }
Chris Mason5f39d392007-10-15 16:14:19 -04002720 ioff = btrfs_item_offset(leaf, item);
2721 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002722 }
Chris Mason5f39d392007-10-15 16:14:19 -04002723
Chris Masondb945352007-10-15 16:15:53 -04002724 if (leaf->map_token) {
2725 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2726 leaf->map_token = NULL;
2727 }
2728
Chris Mason6567e832007-04-16 09:22:45 -04002729 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002730 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002731 data_end - data_size, btrfs_leaf_data(leaf) +
2732 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002733
Chris Mason6567e832007-04-16 09:22:45 -04002734 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002735 old_size = btrfs_item_size_nr(leaf, slot);
2736 item = btrfs_item_nr(leaf, slot);
2737 btrfs_set_item_size(leaf, item, old_size + data_size);
2738 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002739
2740 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002741 if (btrfs_leaf_free_space(root, leaf) < 0) {
2742 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002743 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002744 }
Chris Mason6567e832007-04-16 09:22:45 -04002745 return ret;
2746}
2747
Chris Mason74123bd2007-02-02 11:05:29 -05002748/*
2749 * Given a key and some data, insert an item into the tree.
2750 * This does all the path init required, making room in the tree if needed.
2751 */
Chris Mason9c583092008-01-29 15:15:18 -05002752int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002753 struct btrfs_root *root,
2754 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002755 struct btrfs_key *cpu_key, u32 *data_size,
2756 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002757{
Chris Mason5f39d392007-10-15 16:14:19 -04002758 struct extent_buffer *leaf;
2759 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002760 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002762 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002763 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002764 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002765 u32 total_size = 0;
2766 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002767 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002768 struct btrfs_disk_key disk_key;
2769
Chris Mason9c583092008-01-29 15:15:18 -05002770 for (i = 0; i < nr; i++) {
2771 total_data += data_size[i];
2772 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002773
Josef Bacik7b128762008-07-24 12:17:14 -04002774 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05002775 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002776 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05002777 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05002778 if (ret < 0)
2779 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002780
Chris Mason62e27492007-03-15 12:56:47 -04002781 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002782 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002783
Chris Mason5f39d392007-10-15 16:14:19 -04002784 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002785 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002786
Chris Masonf25956c2008-09-12 15:32:53 -04002787 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002788 btrfs_print_leaf(root, leaf);
2789 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002790 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002791 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002792 }
Chris Mason5f39d392007-10-15 16:14:19 -04002793
Chris Mason62e27492007-03-15 12:56:47 -04002794 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002795 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002796
Chris Masonbe0e5c02007-01-26 15:51:26 -05002797 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002798 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002799
Chris Mason5f39d392007-10-15 16:14:19 -04002800 if (old_data < data_end) {
2801 btrfs_print_leaf(root, leaf);
2802 printk("slot %d old_data %d data_end %d\n",
2803 slot, old_data, data_end);
2804 BUG_ON(1);
2805 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002806 /*
2807 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2808 */
2809 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002810 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002811 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002812 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002813
Chris Mason5f39d392007-10-15 16:14:19 -04002814 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002815 if (!leaf->map_token) {
2816 map_extent_buffer(leaf, (unsigned long)item,
2817 sizeof(struct btrfs_item),
2818 &leaf->map_token, &leaf->kaddr,
2819 &leaf->map_start, &leaf->map_len,
2820 KM_USER1);
2821 }
2822
Chris Mason5f39d392007-10-15 16:14:19 -04002823 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002824 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002825 }
Chris Masondb945352007-10-15 16:15:53 -04002826 if (leaf->map_token) {
2827 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2828 leaf->map_token = NULL;
2829 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002830
2831 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002832 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002833 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002834 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002835
2836 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002837 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002838 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002839 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002840 data_end = old_data;
2841 }
Chris Mason5f39d392007-10-15 16:14:19 -04002842
Chris Mason62e27492007-03-15 12:56:47 -04002843 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002844 for (i = 0; i < nr; i++) {
2845 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2846 btrfs_set_item_key(leaf, &disk_key, slot + i);
2847 item = btrfs_item_nr(leaf, slot + i);
2848 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2849 data_end -= data_size[i];
2850 btrfs_set_item_size(leaf, item, data_size[i]);
2851 }
2852 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002853 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002854
2855 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002856 if (slot == 0) {
2857 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002858 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002859 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002860
Chris Mason5f39d392007-10-15 16:14:19 -04002861 if (btrfs_leaf_free_space(root, leaf) < 0) {
2862 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002863 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002864 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002865out:
Chris Mason62e27492007-03-15 12:56:47 -04002866 return ret;
2867}
2868
2869/*
2870 * Given a key and some data, insert an item into the tree.
2871 * This does all the path init required, making room in the tree if needed.
2872 */
Chris Masone089f052007-03-16 16:20:31 -04002873int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2874 *root, struct btrfs_key *cpu_key, void *data, u32
2875 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002876{
2877 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002878 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002879 struct extent_buffer *leaf;
2880 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002881
Chris Mason2c90e5d2007-04-02 10:50:19 -04002882 path = btrfs_alloc_path();
2883 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002884 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002885 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002886 leaf = path->nodes[0];
2887 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2888 write_extent_buffer(leaf, data, ptr, data_size);
2889 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002890 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002891 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002892 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002893}
2894
Chris Mason74123bd2007-02-02 11:05:29 -05002895/*
Chris Mason5de08d72007-02-24 06:24:44 -05002896 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002897 *
2898 * If the delete empties a node, the node is removed from the tree,
2899 * continuing all the way the root if required. The root is converted into
2900 * a leaf if all the nodes are emptied.
2901 */
Chris Masone089f052007-03-16 16:20:31 -04002902static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2903 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002904{
Chris Mason5f39d392007-10-15 16:14:19 -04002905 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002906 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002907 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002908 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002909
Chris Mason5f39d392007-10-15 16:14:19 -04002910 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002911 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002912 memmove_extent_buffer(parent,
2913 btrfs_node_key_ptr_offset(slot),
2914 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002915 sizeof(struct btrfs_key_ptr) *
2916 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002917 }
Chris Mason7518a232007-03-12 12:01:18 -04002918 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002919 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002920 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002921 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002922 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002923 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002924 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002925 struct btrfs_disk_key disk_key;
2926
2927 btrfs_node_key(parent, &disk_key, 0);
2928 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002929 if (wret)
2930 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002931 }
Chris Masond6025572007-03-30 14:27:56 -04002932 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002933 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002934}
2935
Chris Mason74123bd2007-02-02 11:05:29 -05002936/*
2937 * delete the item at the leaf level in path. If that empties
2938 * the leaf, remove it from the tree
2939 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002940int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2941 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002942{
Chris Mason5f39d392007-10-15 16:14:19 -04002943 struct extent_buffer *leaf;
2944 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002945 int last_off;
2946 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002947 int ret = 0;
2948 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002949 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002950 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002951
Chris Mason5f39d392007-10-15 16:14:19 -04002952 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002953 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2954
2955 for (i = 0; i < nr; i++)
2956 dsize += btrfs_item_size_nr(leaf, slot + i);
2957
Chris Mason5f39d392007-10-15 16:14:19 -04002958 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002959
Chris Mason85e21ba2008-01-29 15:11:36 -05002960 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04002961 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002962
2963 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002964 data_end + dsize,
2965 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002966 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002967
Chris Mason85e21ba2008-01-29 15:11:36 -05002968 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002969 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002970
Chris Mason5f39d392007-10-15 16:14:19 -04002971 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002972 if (!leaf->map_token) {
2973 map_extent_buffer(leaf, (unsigned long)item,
2974 sizeof(struct btrfs_item),
2975 &leaf->map_token, &leaf->kaddr,
2976 &leaf->map_start, &leaf->map_len,
2977 KM_USER1);
2978 }
Chris Mason5f39d392007-10-15 16:14:19 -04002979 ioff = btrfs_item_offset(leaf, item);
2980 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002981 }
Chris Masondb945352007-10-15 16:15:53 -04002982
2983 if (leaf->map_token) {
2984 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2985 leaf->map_token = NULL;
2986 }
2987
Chris Mason5f39d392007-10-15 16:14:19 -04002988 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002989 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002990 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002991 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002992 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002993 btrfs_set_header_nritems(leaf, nritems - nr);
2994 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002995
Chris Mason74123bd2007-02-02 11:05:29 -05002996 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002997 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002998 if (leaf == root->node) {
2999 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003000 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05003001 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04003002 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003003 if (wret)
3004 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04003005 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05003006 leaf->start, leaf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003007 path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003008 btrfs_header_owner(path->nodes[1]),
3009 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003010 if (wret)
3011 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05003012 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003013 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003014 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003015 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003016 struct btrfs_disk_key disk_key;
3017
3018 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003019 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003020 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003021 if (wret)
3022 ret = wret;
3023 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003024
Chris Mason74123bd2007-02-02 11:05:29 -05003025 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003026 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003027 /* push_leaf_left fixes the path.
3028 * make sure the path still points to our leaf
3029 * for possible call to del_ptr below
3030 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003031 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003032 extent_buffer_get(leaf);
3033
Chris Mason85e21ba2008-01-29 15:11:36 -05003034 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003035 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003036 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003037
3038 if (path->nodes[0] == leaf &&
3039 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003040 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003041 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003042 ret = wret;
3043 }
Chris Mason5f39d392007-10-15 16:14:19 -04003044
3045 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05003046 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04003047 u64 bytenr = leaf->start;
3048 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Chris Mason7bb86312007-12-11 09:25:06 -05003050 root_gen = btrfs_header_generation(
3051 path->nodes[1]);
3052
Chris Masone089f052007-03-16 16:20:31 -04003053 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003054 if (wret)
3055 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003056
3057 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04003058 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003059 blocksize, path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003060 btrfs_header_owner(path->nodes[1]),
3061 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003062 if (wret)
3063 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05003064 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003065 /* if we're still in the path, make sure
3066 * we're dirty. Otherwise, one of the
3067 * push_leaf functions must have already
3068 * dirtied this buffer
3069 */
3070 if (path->nodes[0] == leaf)
3071 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003072 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003073 }
Chris Masond5719762007-03-23 10:01:08 -04003074 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003075 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003076 }
3077 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003078 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003079}
3080
Chris Mason97571fd2007-02-24 13:39:08 -05003081/*
Chris Mason925baed2008-06-25 16:01:30 -04003082 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003083 * returns 0 if it found something or 1 if there are no lesser leaves.
3084 * returns < 0 on io errors.
3085 */
3086int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3087{
Chris Mason925baed2008-06-25 16:01:30 -04003088 struct btrfs_key key;
3089 struct btrfs_disk_key found_key;
3090 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003091
Chris Mason925baed2008-06-25 16:01:30 -04003092 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003093
Chris Mason925baed2008-06-25 16:01:30 -04003094 if (key.offset > 0)
3095 key.offset--;
3096 else if (key.type > 0)
3097 key.type--;
3098 else if (key.objectid > 0)
3099 key.objectid--;
3100 else
3101 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003102
Chris Mason925baed2008-06-25 16:01:30 -04003103 btrfs_release_path(root, path);
3104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3105 if (ret < 0)
3106 return ret;
3107 btrfs_item_key(path->nodes[0], &found_key, 0);
3108 ret = comp_keys(&found_key, &key);
3109 if (ret < 0)
3110 return 0;
3111 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003112}
3113
Chris Mason3f157a22008-06-25 16:01:31 -04003114/*
3115 * A helper function to walk down the tree starting at min_key, and looking
3116 * for nodes or leaves that are either in cache or have a minimum
3117 * transaction id. This is used by the btree defrag code, but could
3118 * also be used to search for blocks that have changed since a given
3119 * transaction id.
3120 *
3121 * This does not cow, but it does stuff the starting key it finds back
3122 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3123 * key and get a writable path.
3124 *
3125 * This does lock as it descends, and path->keep_locks should be set
3126 * to 1 by the caller.
3127 *
3128 * This honors path->lowest_level to prevent descent past a given level
3129 * of the tree.
3130 *
3131 * returns zero if something useful was found, < 0 on error and 1 if there
3132 * was nothing in the tree that matched the search criteria.
3133 */
3134int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003135 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003136 struct btrfs_path *path, int cache_only,
3137 u64 min_trans)
3138{
3139 struct extent_buffer *cur;
3140 struct btrfs_key found_key;
3141 int slot;
Yan96524802008-07-24 12:19:49 -04003142 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003143 u32 nritems;
3144 int level;
3145 int ret = 1;
3146
3147again:
3148 cur = btrfs_lock_root_node(root);
3149 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003150 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003151 path->nodes[level] = cur;
3152 path->locks[level] = 1;
3153
3154 if (btrfs_header_generation(cur) < min_trans) {
3155 ret = 1;
3156 goto out;
3157 }
3158 while(1) {
3159 nritems = btrfs_header_nritems(cur);
3160 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003161 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003162
3163 /* at level = 0, we're done, setup the path and exit */
3164 if (level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003165 if (slot >= nritems)
3166 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003167 ret = 0;
3168 path->slots[level] = slot;
3169 btrfs_item_key_to_cpu(cur, &found_key, slot);
3170 goto out;
3171 }
Yan96524802008-07-24 12:19:49 -04003172 if (sret && slot > 0)
3173 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003174 /*
3175 * check this node pointer against the cache_only and
3176 * min_trans parameters. If it isn't in cache or is too
3177 * old, skip to the next one.
3178 */
3179 while(slot < nritems) {
3180 u64 blockptr;
3181 u64 gen;
3182 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003183 struct btrfs_disk_key disk_key;
3184
Chris Mason3f157a22008-06-25 16:01:31 -04003185 blockptr = btrfs_node_blockptr(cur, slot);
3186 gen = btrfs_node_ptr_generation(cur, slot);
3187 if (gen < min_trans) {
3188 slot++;
3189 continue;
3190 }
3191 if (!cache_only)
3192 break;
3193
Chris Masone02119d2008-09-05 16:13:11 -04003194 if (max_key) {
3195 btrfs_node_key(cur, &disk_key, slot);
3196 if (comp_keys(&disk_key, max_key) >= 0) {
3197 ret = 1;
3198 goto out;
3199 }
3200 }
3201
Chris Mason3f157a22008-06-25 16:01:31 -04003202 tmp = btrfs_find_tree_block(root, blockptr,
3203 btrfs_level_size(root, level - 1));
3204
3205 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3206 free_extent_buffer(tmp);
3207 break;
3208 }
3209 if (tmp)
3210 free_extent_buffer(tmp);
3211 slot++;
3212 }
Chris Masone02119d2008-09-05 16:13:11 -04003213find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003214 /*
3215 * we didn't find a candidate key in this node, walk forward
3216 * and find another one
3217 */
3218 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003219 path->slots[level] = slot;
3220 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003221 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003222 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003223 btrfs_release_path(root, path);
3224 goto again;
3225 } else {
3226 goto out;
3227 }
3228 }
3229 /* save our key for returning back */
3230 btrfs_node_key_to_cpu(cur, &found_key, slot);
3231 path->slots[level] = slot;
3232 if (level == path->lowest_level) {
3233 ret = 0;
3234 unlock_up(path, level, 1);
3235 goto out;
3236 }
3237 cur = read_node_slot(root, cur, slot);
3238
3239 btrfs_tree_lock(cur);
3240 path->locks[level - 1] = 1;
3241 path->nodes[level - 1] = cur;
3242 unlock_up(path, level, 1);
3243 }
3244out:
3245 if (ret == 0)
3246 memcpy(min_key, &found_key, sizeof(found_key));
3247 return ret;
3248}
3249
3250/*
3251 * this is similar to btrfs_next_leaf, but does not try to preserve
3252 * and fixup the path. It looks for and returns the next key in the
3253 * tree based on the current path and the cache_only and min_trans
3254 * parameters.
3255 *
3256 * 0 is returned if another key is found, < 0 if there are any errors
3257 * and 1 is returned if there are no higher keys in the tree
3258 *
3259 * path->keep_locks should be set to 1 on the search made before
3260 * calling this function.
3261 */
Chris Masone7a84562008-06-25 16:01:31 -04003262int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003263 struct btrfs_key *key, int lowest_level,
3264 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003265{
3266 int level = lowest_level;
3267 int slot;
3268 struct extent_buffer *c;
3269
3270 while(level < BTRFS_MAX_LEVEL) {
3271 if (!path->nodes[level])
3272 return 1;
3273
3274 slot = path->slots[level] + 1;
3275 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003276next:
Chris Masone7a84562008-06-25 16:01:31 -04003277 if (slot >= btrfs_header_nritems(c)) {
3278 level++;
3279 if (level == BTRFS_MAX_LEVEL) {
3280 return 1;
3281 }
3282 continue;
3283 }
3284 if (level == 0)
3285 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003286 else {
3287 u64 blockptr = btrfs_node_blockptr(c, slot);
3288 u64 gen = btrfs_node_ptr_generation(c, slot);
3289
3290 if (cache_only) {
3291 struct extent_buffer *cur;
3292 cur = btrfs_find_tree_block(root, blockptr,
3293 btrfs_level_size(root, level - 1));
3294 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3295 slot++;
3296 if (cur)
3297 free_extent_buffer(cur);
3298 goto next;
3299 }
3300 free_extent_buffer(cur);
3301 }
3302 if (gen < min_trans) {
3303 slot++;
3304 goto next;
3305 }
Chris Masone7a84562008-06-25 16:01:31 -04003306 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003307 }
Chris Masone7a84562008-06-25 16:01:31 -04003308 return 0;
3309 }
3310 return 1;
3311}
3312
Chris Mason7bb86312007-12-11 09:25:06 -05003313/*
Chris Mason925baed2008-06-25 16:01:30 -04003314 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003315 * returns 0 if it found something or 1 if there are no greater leaves.
3316 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003317 */
Chris Mason234b63a2007-03-13 10:46:10 -04003318int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003319{
3320 int slot;
3321 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003322 struct extent_buffer *c;
3323 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003324 struct btrfs_key key;
3325 u32 nritems;
3326 int ret;
3327
3328 nritems = btrfs_header_nritems(path->nodes[0]);
3329 if (nritems == 0) {
3330 return 1;
3331 }
3332
3333 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3334
Chris Mason925baed2008-06-25 16:01:30 -04003335 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003336 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3338 path->keep_locks = 0;
3339
3340 if (ret < 0)
3341 return ret;
3342
Chris Masona2135012008-06-25 16:01:30 -04003343 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003344 /*
3345 * by releasing the path above we dropped all our locks. A balance
3346 * could have added more items next to the key that used to be
3347 * at the very end of the block. So, check again here and
3348 * advance the path if there are now more items available.
3349 */
Chris Masona2135012008-06-25 16:01:30 -04003350 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003351 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003352 goto done;
3353 }
Chris Masond97e63b2007-02-20 16:40:44 -05003354
Chris Mason234b63a2007-03-13 10:46:10 -04003355 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003356 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003357 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003358
Chris Masond97e63b2007-02-20 16:40:44 -05003359 slot = path->slots[level] + 1;
3360 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003361 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003362 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003363 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003364 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003365 }
Chris Masond97e63b2007-02-20 16:40:44 -05003366 continue;
3367 }
Chris Mason5f39d392007-10-15 16:14:19 -04003368
Chris Mason925baed2008-06-25 16:01:30 -04003369 if (next) {
3370 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003372 }
Chris Mason5f39d392007-10-15 16:14:19 -04003373
Chris Mason0bd40a72008-07-17 12:54:43 -04003374 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3375 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003376 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003377
Chris Masonca7a79a2008-05-12 12:59:19 -04003378 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003379 if (!path->skip_locking) {
3380 WARN_ON(!btrfs_tree_locked(c));
3381 btrfs_tree_lock(next);
3382 }
Chris Masond97e63b2007-02-20 16:40:44 -05003383 break;
3384 }
3385 path->slots[level] = slot;
3386 while(1) {
3387 level--;
3388 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003389 if (path->locks[level])
3390 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003391 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003392 path->nodes[level] = next;
3393 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003394 if (!path->skip_locking)
3395 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003396 if (!level)
3397 break;
Chris Mason925baed2008-06-25 16:01:30 -04003398 if (level == 1 && path->locks[1] && path->reada)
3399 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003400 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003401 if (!path->skip_locking) {
3402 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3403 btrfs_tree_lock(next);
3404 }
Chris Masond97e63b2007-02-20 16:40:44 -05003405 }
Chris Mason925baed2008-06-25 16:01:30 -04003406done:
3407 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003408 return 0;
3409}
Chris Mason0b86a832008-03-24 15:01:56 -04003410
Chris Mason3f157a22008-06-25 16:01:31 -04003411/*
3412 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3413 * searching until it gets past min_objectid or finds an item of 'type'
3414 *
3415 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3416 */
Chris Mason0b86a832008-03-24 15:01:56 -04003417int btrfs_previous_item(struct btrfs_root *root,
3418 struct btrfs_path *path, u64 min_objectid,
3419 int type)
3420{
3421 struct btrfs_key found_key;
3422 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003423 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003424 int ret;
3425
3426 while(1) {
3427 if (path->slots[0] == 0) {
3428 ret = btrfs_prev_leaf(root, path);
3429 if (ret != 0)
3430 return ret;
3431 } else {
3432 path->slots[0]--;
3433 }
3434 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003435 nritems = btrfs_header_nritems(leaf);
3436 if (nritems == 0)
3437 return 1;
3438 if (path->slots[0] == nritems)
3439 path->slots[0]--;
3440
Chris Mason0b86a832008-03-24 15:01:56 -04003441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3442 if (found_key.type == type)
3443 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003444 if (found_key.objectid < min_objectid)
3445 break;
3446 if (found_key.objectid == min_objectid &&
3447 found_key.type < type)
3448 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003449 }
3450 return 1;
3451}