blob: 7f4cc2b88d097a7315ba876afb2ab791b70e4330 [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 Mason234b63a2007-03-13 10:46:10 -040063void 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 int keep = p->keep_locks;
Chris Mason5cd57b22008-06-25 16:01:30 -040067 int skip = p->skip_locking;
Chris Masona2135012008-06-25 16:01:30 -040068
Chris Mason234b63a2007-03-13 10:46:10 -040069 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050070 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040071 continue;
72 if (p->locks[i]) {
73 btrfs_tree_unlock(p->nodes[i]);
74 p->locks[i] = 0;
75 }
Chris Mason5f39d392007-10-15 16:14:19 -040076 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050077 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050078 memset(p, 0, sizeof(*p));
Chris Masona2135012008-06-25 16:01:30 -040079 p->keep_locks = keep;
Chris Mason5cd57b22008-06-25 16:01:30 -040080 p->skip_locking = skip;
Chris Masoneb60cea2007-02-02 09:18:22 -050081}
82
Chris Mason925baed2008-06-25 16:01:30 -040083struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
84{
85 struct extent_buffer *eb;
86 spin_lock(&root->node_lock);
87 eb = root->node;
88 extent_buffer_get(eb);
89 spin_unlock(&root->node_lock);
90 return eb;
91}
92
93struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
94{
95 struct extent_buffer *eb;
96
97 while(1) {
98 eb = btrfs_root_node(root);
99 btrfs_tree_lock(eb);
100
101 spin_lock(&root->node_lock);
102 if (eb == root->node) {
103 spin_unlock(&root->node_lock);
104 break;
105 }
106 spin_unlock(&root->node_lock);
107
108 btrfs_tree_unlock(eb);
109 free_extent_buffer(eb);
110 }
111 return eb;
112}
113
Chris Mason0b86a832008-03-24 15:01:56 -0400114static void add_root_to_dirty_list(struct btrfs_root *root)
115{
116 if (root->track_dirty && list_empty(&root->dirty_list)) {
117 list_add(&root->dirty_list,
118 &root->fs_info->dirty_cowonly_roots);
119 }
120}
121
Chris Masonbe20aa92007-12-17 20:14:01 -0500122int btrfs_copy_root(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
124 struct extent_buffer *buf,
125 struct extent_buffer **cow_ret, u64 new_root_objectid)
126{
127 struct extent_buffer *cow;
128 u32 nritems;
129 int ret = 0;
130 int level;
131 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -0500132 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500133
Chris Mason4aec2b52007-12-18 16:25:45 -0500134 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
135 if (!new_root)
136 return -ENOMEM;
137
138 memcpy(new_root, root, sizeof(*new_root));
139 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500140
141 WARN_ON(root->ref_cows && trans->transid !=
142 root->fs_info->running_transaction->transid);
143 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
144
145 level = btrfs_header_level(buf);
146 nritems = btrfs_header_nritems(buf);
147 if (nritems) {
148 if (level == 0)
149 btrfs_item_key_to_cpu(buf, &first_key, 0);
150 else
151 btrfs_node_key_to_cpu(buf, &first_key, 0);
152 } else {
153 first_key.objectid = 0;
154 }
Chris Mason925baed2008-06-25 16:01:30 -0400155 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500156 new_root_objectid,
157 trans->transid, first_key.objectid,
158 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500159 if (IS_ERR(cow)) {
160 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500161 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500162 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500163
164 copy_extent_buffer(cow, buf, 0, 0, cow->len);
165 btrfs_set_header_bytenr(cow, cow->start);
166 btrfs_set_header_generation(cow, trans->transid);
167 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400168 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500169
170 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500171 ret = btrfs_inc_ref(trans, new_root, buf);
172 kfree(new_root);
173
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 if (ret)
175 return ret;
176
177 btrfs_mark_buffer_dirty(cow);
178 *cow_ret = cow;
179 return 0;
180}
181
182int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400183 struct btrfs_root *root,
184 struct extent_buffer *buf,
185 struct extent_buffer *parent, int parent_slot,
186 struct extent_buffer **cow_ret,
187 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400188{
Chris Mason7bb86312007-12-11 09:25:06 -0500189 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400190 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500191 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400192 int ret = 0;
193 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500194 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400195 int unlock_orig = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500196 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400197
Chris Mason925baed2008-06-25 16:01:30 -0400198 if (*cow_ret == buf)
199 unlock_orig = 1;
200
201 WARN_ON(!btrfs_tree_locked(buf));
202
Chris Mason7bb86312007-12-11 09:25:06 -0500203 if (root->ref_cows) {
204 root_gen = trans->transid;
205 } else {
206 root_gen = 0;
207 }
Chris Mason7bb86312007-12-11 09:25:06 -0500208 WARN_ON(root->ref_cows && trans->transid !=
209 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400210 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400211
Chris Mason7bb86312007-12-11 09:25:06 -0500212 level = btrfs_header_level(buf);
213 nritems = btrfs_header_nritems(buf);
214 if (nritems) {
215 if (level == 0)
216 btrfs_item_key_to_cpu(buf, &first_key, 0);
217 else
218 btrfs_node_key_to_cpu(buf, &first_key, 0);
219 } else {
220 first_key.objectid = 0;
221 }
Chris Mason925baed2008-06-25 16:01:30 -0400222 cow = btrfs_alloc_free_block(trans, root, buf->len,
Chris Mason7bb86312007-12-11 09:25:06 -0500223 root->root_key.objectid,
224 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400225 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400226 if (IS_ERR(cow))
227 return PTR_ERR(cow);
228
Chris Mason5f39d392007-10-15 16:14:19 -0400229 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400230 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400231 btrfs_set_header_generation(cow, trans->transid);
232 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400233 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400234
Chris Mason5f39d392007-10-15 16:14:19 -0400235 WARN_ON(btrfs_header_generation(buf) > trans->transid);
236 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400237 different_trans = 1;
238 ret = btrfs_inc_ref(trans, root, buf);
239 if (ret)
240 return ret;
241 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400242 clean_tree_block(trans, root, buf);
243 }
244
245 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400246 WARN_ON(parent && parent != buf);
Chris Mason7bb86312007-12-11 09:25:06 -0500247 root_gen = btrfs_header_generation(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400248
249 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400250 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400251 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400252 spin_unlock(&root->node_lock);
253
Chris Mason6702ed42007-08-07 16:15:09 -0400254 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400255 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500256 buf->len, root->root_key.objectid,
257 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400258 }
Chris Mason5f39d392007-10-15 16:14:19 -0400259 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400260 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400261 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500262 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400263 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400264 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500265 WARN_ON(trans->transid == 0);
266 btrfs_set_node_ptr_generation(parent, parent_slot,
267 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400268 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400269 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500270 btrfs_free_extent(trans, root, buf->start, buf->len,
271 btrfs_header_owner(parent), root_gen,
272 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400273 }
Chris Mason925baed2008-06-25 16:01:30 -0400274 if (unlock_orig)
275 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400276 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400277 btrfs_mark_buffer_dirty(cow);
278 *cow_ret = cow;
279 return 0;
280}
281
Chris Mason5f39d392007-10-15 16:14:19 -0400282int btrfs_cow_block(struct btrfs_trans_handle *trans,
283 struct btrfs_root *root, struct extent_buffer *buf,
284 struct extent_buffer *parent, int parent_slot,
285 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500286{
Chris Mason6702ed42007-08-07 16:15:09 -0400287 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500288 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400289 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500290
Chris Masonccd467d2007-06-28 15:57:36 -0400291 if (trans->transaction != root->fs_info->running_transaction) {
292 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
293 root->fs_info->running_transaction->transid);
294 WARN_ON(1);
295 }
296 if (trans->transid != root->fs_info->generation) {
297 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
298 root->fs_info->generation);
299 WARN_ON(1);
300 }
Chris Masondc17ff82008-01-08 15:46:30 -0500301
302 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400303 spin_lock(&root->fs_info->hash_lock);
304 if (header_trans == trans->transid &&
305 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500306 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400307 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500308 return 0;
309 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400310 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400311 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400312 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400313 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400314 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400315}
316
Chris Mason6b800532007-10-15 16:17:34 -0400317static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400318{
Chris Mason6b800532007-10-15 16:17:34 -0400319 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400320 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400321 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400322 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500323 return 0;
324}
325
Chris Mason081e9572007-11-06 10:26:24 -0500326/*
327 * compare two keys in a memcmp fashion
328 */
329static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
330{
331 struct btrfs_key k1;
332
333 btrfs_disk_key_to_cpu(&k1, disk);
334
335 if (k1.objectid > k2->objectid)
336 return 1;
337 if (k1.objectid < k2->objectid)
338 return -1;
339 if (k1.type > k2->type)
340 return 1;
341 if (k1.type < k2->type)
342 return -1;
343 if (k1.offset > k2->offset)
344 return 1;
345 if (k1.offset < k2->offset)
346 return -1;
347 return 0;
348}
349
350
Chris Mason6702ed42007-08-07 16:15:09 -0400351int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400352 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400353 int start_slot, int cache_only, u64 *last_ret,
354 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400355{
Chris Mason6b800532007-10-15 16:17:34 -0400356 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400357 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400358 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400359 u64 search_start = *last_ret;
360 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400361 u64 other;
362 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400363 int end_slot;
364 int i;
365 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400366 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400367 int uptodate;
368 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500369 int progress_passed = 0;
370 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400371
Chris Mason5708b952007-10-25 15:43:18 -0400372 parent_level = btrfs_header_level(parent);
373 if (cache_only && parent_level != 1)
374 return 0;
375
Chris Mason6702ed42007-08-07 16:15:09 -0400376 if (trans->transaction != root->fs_info->running_transaction) {
377 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
378 root->fs_info->running_transaction->transid);
379 WARN_ON(1);
380 }
381 if (trans->transid != root->fs_info->generation) {
382 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
383 root->fs_info->generation);
384 WARN_ON(1);
385 }
Chris Mason86479a02007-09-10 19:58:16 -0400386
Chris Mason6b800532007-10-15 16:17:34 -0400387 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400388 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400389 end_slot = parent_nritems;
390
391 if (parent_nritems == 1)
392 return 0;
393
394 for (i = start_slot; i < end_slot; i++) {
395 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400396
Chris Mason5708b952007-10-25 15:43:18 -0400397 if (!parent->map_token) {
398 map_extent_buffer(parent,
399 btrfs_node_key_ptr_offset(i),
400 sizeof(struct btrfs_key_ptr),
401 &parent->map_token, &parent->kaddr,
402 &parent->map_start, &parent->map_len,
403 KM_USER1);
404 }
Chris Mason081e9572007-11-06 10:26:24 -0500405 btrfs_node_key(parent, &disk_key, i);
406 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
407 continue;
408
409 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400410 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400411 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400412 if (last_block == 0)
413 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400414
Chris Mason6702ed42007-08-07 16:15:09 -0400415 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400416 other = btrfs_node_blockptr(parent, i - 1);
417 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400418 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400419 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400420 other = btrfs_node_blockptr(parent, i + 1);
421 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400422 }
Chris Masone9d0b132007-08-10 14:06:19 -0400423 if (close) {
424 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400425 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400426 }
Chris Mason5708b952007-10-25 15:43:18 -0400427 if (parent->map_token) {
428 unmap_extent_buffer(parent, parent->map_token,
429 KM_USER1);
430 parent->map_token = NULL;
431 }
Chris Mason6702ed42007-08-07 16:15:09 -0400432
Chris Mason6b800532007-10-15 16:17:34 -0400433 cur = btrfs_find_tree_block(root, blocknr, blocksize);
434 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400435 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400436 else
437 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400438 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400439 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400440 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400441 continue;
442 }
Chris Mason6b800532007-10-15 16:17:34 -0400443 if (!cur) {
444 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400445 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400446 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400447 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400448 }
Chris Mason6702ed42007-08-07 16:15:09 -0400449 }
Chris Masone9d0b132007-08-10 14:06:19 -0400450 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400451 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400452
Chris Masone7a84562008-06-25 16:01:31 -0400453 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400454 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400455 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400456 min(16 * blocksize,
457 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400458 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400459 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400460 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400461 break;
Yan252c38f2007-08-29 09:11:44 -0400462 }
Chris Masone7a84562008-06-25 16:01:31 -0400463 search_start = cur->start;
464 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400465 *last_ret = search_start;
466 if (parent_level == 1)
Chris Masone7a84562008-06-25 16:01:31 -0400467 btrfs_clear_buffer_defrag(cur);
468 btrfs_tree_unlock(cur);
469 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 }
Chris Mason5708b952007-10-25 15:43:18 -0400471 if (parent->map_token) {
472 unmap_extent_buffer(parent, parent->map_token,
473 KM_USER1);
474 parent->map_token = NULL;
475 }
Chris Mason6702ed42007-08-07 16:15:09 -0400476 return err;
477}
478
Chris Mason74123bd2007-02-02 11:05:29 -0500479/*
480 * The leaf data grows from end-to-front in the node.
481 * this returns the address of the start of the last item,
482 * which is the stop of the leaf data stack
483 */
Chris Mason123abc82007-03-14 14:14:43 -0400484static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400485 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500486{
Chris Mason5f39d392007-10-15 16:14:19 -0400487 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500488 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400489 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400490 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500491}
492
Chris Mason123abc82007-03-14 14:14:43 -0400493static int check_node(struct btrfs_root *root, struct btrfs_path *path,
494 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500495{
Chris Mason5f39d392007-10-15 16:14:19 -0400496 struct extent_buffer *parent = NULL;
497 struct extent_buffer *node = path->nodes[level];
498 struct btrfs_disk_key parent_key;
499 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500500 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400501 int slot;
502 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400503 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500504
505 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400506 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400507
Chris Mason8d7be552007-05-10 11:24:42 -0400508 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400509 BUG_ON(nritems == 0);
510 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400511 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400512 btrfs_node_key(parent, &parent_key, parent_slot);
513 btrfs_node_key(node, &node_key, 0);
514 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400515 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400516 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400517 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500518 }
Chris Mason123abc82007-03-14 14:14:43 -0400519 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400520 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400521 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
522 btrfs_node_key(node, &node_key, slot);
523 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400524 }
525 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400526 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
527 btrfs_node_key(node, &node_key, slot);
528 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500529 }
530 return 0;
531}
532
Chris Mason123abc82007-03-14 14:14:43 -0400533static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
534 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500535{
Chris Mason5f39d392007-10-15 16:14:19 -0400536 struct extent_buffer *leaf = path->nodes[level];
537 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500538 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400539 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400540 struct btrfs_disk_key parent_key;
541 struct btrfs_disk_key leaf_key;
542 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400543
Chris Mason5f39d392007-10-15 16:14:19 -0400544 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500545
546 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400547 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400548
549 if (nritems == 0)
550 return 0;
551
552 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400553 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400554 btrfs_node_key(parent, &parent_key, parent_slot);
555 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400556
Chris Mason5f39d392007-10-15 16:14:19 -0400557 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400558 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400559 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400560 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500561 }
Chris Mason5f39d392007-10-15 16:14:19 -0400562#if 0
563 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
564 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
565 btrfs_item_key(leaf, &leaf_key, i);
566 if (comp_keys(&leaf_key, &cpukey) >= 0) {
567 btrfs_print_leaf(root, leaf);
568 printk("slot %d offset bad key\n", i);
569 BUG_ON(1);
570 }
571 if (btrfs_item_offset_nr(leaf, i) !=
572 btrfs_item_end_nr(leaf, i + 1)) {
573 btrfs_print_leaf(root, leaf);
574 printk("slot %d offset bad\n", i);
575 BUG_ON(1);
576 }
577 if (i == 0) {
578 if (btrfs_item_offset_nr(leaf, i) +
579 btrfs_item_size_nr(leaf, i) !=
580 BTRFS_LEAF_DATA_SIZE(root)) {
581 btrfs_print_leaf(root, leaf);
582 printk("slot %d first offset bad\n", i);
583 BUG_ON(1);
584 }
585 }
586 }
587 if (nritems > 0) {
588 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
589 btrfs_print_leaf(root, leaf);
590 printk("slot %d bad size \n", nritems - 1);
591 BUG_ON(1);
592 }
593 }
594#endif
595 if (slot != 0 && slot < nritems - 1) {
596 btrfs_item_key(leaf, &leaf_key, slot);
597 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
598 if (comp_keys(&leaf_key, &cpukey) <= 0) {
599 btrfs_print_leaf(root, leaf);
600 printk("slot %d offset bad key\n", slot);
601 BUG_ON(1);
602 }
603 if (btrfs_item_offset_nr(leaf, slot - 1) !=
604 btrfs_item_end_nr(leaf, slot)) {
605 btrfs_print_leaf(root, leaf);
606 printk("slot %d offset bad\n", slot);
607 BUG_ON(1);
608 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500609 }
Chris Mason8d7be552007-05-10 11:24:42 -0400610 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400611 btrfs_item_key(leaf, &leaf_key, slot);
612 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
613 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
614 if (btrfs_item_offset_nr(leaf, slot) !=
615 btrfs_item_end_nr(leaf, slot + 1)) {
616 btrfs_print_leaf(root, leaf);
617 printk("slot %d offset bad\n", slot);
618 BUG_ON(1);
619 }
Chris Mason8d7be552007-05-10 11:24:42 -0400620 }
Chris Mason5f39d392007-10-15 16:14:19 -0400621 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
622 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500623 return 0;
624}
625
Chris Mason98ed5172008-01-03 10:01:48 -0500626static int noinline check_block(struct btrfs_root *root,
627 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500628{
Chris Masonf1885912008-04-09 16:28:12 -0400629 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400630 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400631 if (btrfs_header_level(path->nodes[level]) != level)
632 printk("warning: bad level %Lu wanted %d found %d\n",
633 path->nodes[level]->start, level,
634 btrfs_header_level(path->nodes[level]));
635 found_start = btrfs_header_bytenr(path->nodes[level]);
636 if (found_start != path->nodes[level]->start) {
637 printk("warning: bad bytentr %Lu found %Lu\n",
638 path->nodes[level]->start, found_start);
639 }
Chris Masondb945352007-10-15 16:15:53 -0400640#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400641 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400642
Chris Mason479965d2007-10-15 16:14:27 -0400643 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
644 (unsigned long)btrfs_header_fsid(buf),
645 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400646 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400647 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400648 }
Chris Masondb945352007-10-15 16:15:53 -0400649#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500650 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400651 return check_leaf(root, path, level);
652 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500653}
654
Chris Mason74123bd2007-02-02 11:05:29 -0500655/*
Chris Mason5f39d392007-10-15 16:14:19 -0400656 * search for key in the extent_buffer. The items start at offset p,
657 * and they are item_size apart. There are 'max' items in p.
658 *
Chris Mason74123bd2007-02-02 11:05:29 -0500659 * the slot in the array is returned via slot, and it points to
660 * the place where you would insert key if it is not found in
661 * the array.
662 *
663 * slot may point to max if the key is bigger than all of the keys
664 */
Chris Mason5f39d392007-10-15 16:14:19 -0400665static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
666 int item_size, struct btrfs_key *key,
667 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500668{
669 int low = 0;
670 int high = max;
671 int mid;
672 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400673 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400674 struct btrfs_disk_key unaligned;
675 unsigned long offset;
676 char *map_token = NULL;
677 char *kaddr = NULL;
678 unsigned long map_start = 0;
679 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400680 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500681
682 while(low < high) {
683 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400684 offset = p + mid * item_size;
685
686 if (!map_token || offset < map_start ||
687 (offset + sizeof(struct btrfs_disk_key)) >
688 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400689 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400690 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400691 map_token = NULL;
692 }
693 err = map_extent_buffer(eb, offset,
694 sizeof(struct btrfs_disk_key),
695 &map_token, &kaddr,
696 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400697
Chris Mason479965d2007-10-15 16:14:27 -0400698 if (!err) {
699 tmp = (struct btrfs_disk_key *)(kaddr + offset -
700 map_start);
701 } else {
702 read_extent_buffer(eb, &unaligned,
703 offset, sizeof(unaligned));
704 tmp = &unaligned;
705 }
706
Chris Mason5f39d392007-10-15 16:14:19 -0400707 } else {
708 tmp = (struct btrfs_disk_key *)(kaddr + offset -
709 map_start);
710 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500711 ret = comp_keys(tmp, key);
712
713 if (ret < 0)
714 low = mid + 1;
715 else if (ret > 0)
716 high = mid;
717 else {
718 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400719 if (map_token)
720 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721 return 0;
722 }
723 }
724 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400725 if (map_token)
726 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727 return 1;
728}
729
Chris Mason97571fd2007-02-24 13:39:08 -0500730/*
731 * simple bin_search frontend that does the right thing for
732 * leaves vs nodes
733 */
Chris Mason5f39d392007-10-15 16:14:19 -0400734static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
735 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500736{
Chris Mason5f39d392007-10-15 16:14:19 -0400737 if (level == 0) {
738 return generic_bin_search(eb,
739 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400740 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400741 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400742 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400744 return generic_bin_search(eb,
745 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400746 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400747 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400748 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500749 }
750 return -1;
751}
752
Chris Mason5f39d392007-10-15 16:14:19 -0400753static struct extent_buffer *read_node_slot(struct btrfs_root *root,
754 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500755{
Chris Masonca7a79a2008-05-12 12:59:19 -0400756 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500757 if (slot < 0)
758 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400759 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500760 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400761
762 BUG_ON(level == 0);
763
Chris Masondb945352007-10-15 16:15:53 -0400764 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400765 btrfs_level_size(root, level - 1),
766 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500767}
768
Chris Mason98ed5172008-01-03 10:01:48 -0500769static int balance_level(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root,
771 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500772{
Chris Mason5f39d392007-10-15 16:14:19 -0400773 struct extent_buffer *right = NULL;
774 struct extent_buffer *mid;
775 struct extent_buffer *left = NULL;
776 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500777 int ret = 0;
778 int wret;
779 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500780 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400781 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500782 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500783
784 if (level == 0)
785 return 0;
786
Chris Mason5f39d392007-10-15 16:14:19 -0400787 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400788 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500789 WARN_ON(btrfs_header_generation(mid) != trans->transid);
790
Chris Mason1d4f8a02007-03-13 09:28:32 -0400791 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500792
Chris Mason234b63a2007-03-13 10:46:10 -0400793 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400794 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500795 pslot = path->slots[level + 1];
796
Chris Mason40689472007-03-17 14:29:23 -0400797 /*
798 * deal with the case where there is only one pointer in the root
799 * by promoting the node below to a root
800 */
Chris Mason5f39d392007-10-15 16:14:19 -0400801 if (!parent) {
802 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500803
Chris Mason5f39d392007-10-15 16:14:19 -0400804 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500805 return 0;
806
807 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400808 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400809 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500810 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500811 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
812 BUG_ON(ret);
813
Chris Mason925baed2008-06-25 16:01:30 -0400814 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500815 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400816 spin_unlock(&root->node_lock);
817
Chris Mason0b86a832008-03-24 15:01:56 -0400818 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400819 btrfs_tree_unlock(child);
820 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500821 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400822 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400823 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500824 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400825 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500826 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
827 root->root_key.objectid,
828 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500829 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400830 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400831 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500832 }
Chris Mason5f39d392007-10-15 16:14:19 -0400833 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400834 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500835 return 0;
836
Chris Mason5f39d392007-10-15 16:14:19 -0400837 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400838 err_on_enospc = 1;
839
Chris Mason5f39d392007-10-15 16:14:19 -0400840 left = read_node_slot(root, parent, pslot - 1);
841 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400842 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400843 wret = btrfs_cow_block(trans, root, left,
844 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 if (wret) {
846 ret = wret;
847 goto enospc;
848 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400849 }
Chris Mason5f39d392007-10-15 16:14:19 -0400850 right = read_node_slot(root, parent, pslot + 1);
851 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400852 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400853 wret = btrfs_cow_block(trans, root, right,
854 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400855 if (wret) {
856 ret = wret;
857 goto enospc;
858 }
859 }
860
861 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400862 if (left) {
863 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400864 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500865 if (wret < 0)
866 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400867 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400868 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500869 }
Chris Mason79f95c82007-03-01 15:16:26 -0500870
871 /*
872 * then try to empty the right most buffer into the middle
873 */
Chris Mason5f39d392007-10-15 16:14:19 -0400874 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400875 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400876 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500877 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400879 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500880 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400881 u32 blocksize = right->len;
882
Chris Mason5f39d392007-10-15 16:14:19 -0400883 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400884 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400885 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500886 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400887 wret = del_ptr(trans, root, path, level + 1, pslot +
888 1);
Chris Masonbb803952007-03-01 12:04:21 -0500889 if (wret)
890 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400891 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500892 blocksize,
893 btrfs_header_owner(parent),
894 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500895 if (wret)
896 ret = wret;
897 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400898 struct btrfs_disk_key right_key;
899 btrfs_node_key(right, &right_key, 0);
900 btrfs_set_node_key(parent, &right_key, pslot + 1);
901 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500902 }
903 }
Chris Mason5f39d392007-10-15 16:14:19 -0400904 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500905 /*
906 * we're not allowed to leave a node with one item in the
907 * tree during a delete. A deletion from lower in the tree
908 * could try to delete the only pointer in this node.
909 * So, pull some keys from the left.
910 * There has to be a left pointer at this point because
911 * otherwise we would have pulled some pointers from the
912 * right
913 */
Chris Mason5f39d392007-10-15 16:14:19 -0400914 BUG_ON(!left);
915 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400916 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500917 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400918 goto enospc;
919 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400920 if (wret == 1) {
921 wret = push_node_left(trans, root, left, mid, 1);
922 if (wret < 0)
923 ret = wret;
924 }
Chris Mason79f95c82007-03-01 15:16:26 -0500925 BUG_ON(wret == 1);
926 }
Chris Mason5f39d392007-10-15 16:14:19 -0400927 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500928 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500929 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400930 u64 bytenr = mid->start;
931 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400932
Chris Mason5f39d392007-10-15 16:14:19 -0400933 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400935 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500936 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400937 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500938 if (wret)
939 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500940 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
941 btrfs_header_owner(parent),
942 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500943 if (wret)
944 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500945 } else {
946 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400947 struct btrfs_disk_key mid_key;
948 btrfs_node_key(mid, &mid_key, 0);
949 btrfs_set_node_key(parent, &mid_key, pslot);
950 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500951 }
Chris Masonbb803952007-03-01 12:04:21 -0500952
Chris Mason79f95c82007-03-01 15:16:26 -0500953 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400954 if (left) {
955 if (btrfs_header_nritems(left) > orig_slot) {
956 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400957 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400958 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500959 path->slots[level + 1] -= 1;
960 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400961 if (mid) {
962 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400963 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400964 }
Chris Masonbb803952007-03-01 12:04:21 -0500965 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400966 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500967 path->slots[level] = orig_slot;
968 }
969 }
Chris Mason79f95c82007-03-01 15:16:26 -0500970 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400971 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400972 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400973 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500974 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400975enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400976 if (right) {
977 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400978 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400979 }
980 if (left) {
981 if (path->nodes[level] != left)
982 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400983 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400984 }
Chris Masonbb803952007-03-01 12:04:21 -0500985 return ret;
986}
987
Chris Masone66f7092007-04-20 13:16:02 -0400988/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500989static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
991 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400992{
Chris Mason5f39d392007-10-15 16:14:19 -0400993 struct extent_buffer *right = NULL;
994 struct extent_buffer *mid;
995 struct extent_buffer *left = NULL;
996 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400997 int ret = 0;
998 int wret;
999 int pslot;
1000 int orig_slot = path->slots[level];
1001 u64 orig_ptr;
1002
1003 if (level == 0)
1004 return 1;
1005
Chris Mason5f39d392007-10-15 16:14:19 -04001006 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001007 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001008 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1009
1010 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001011 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001012 pslot = path->slots[level + 1];
1013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001015 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001016
Chris Mason5f39d392007-10-15 16:14:19 -04001017 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001018
1019 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001020 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001021 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001022
1023 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001024 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001025 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1026 wret = 1;
1027 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001028 ret = btrfs_cow_block(trans, root, left, parent,
1029 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001030 if (ret)
1031 wret = 1;
1032 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001033 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001034 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001035 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001036 }
Chris Masone66f7092007-04-20 13:16:02 -04001037 if (wret < 0)
1038 ret = wret;
1039 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001040 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001041 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001042 btrfs_node_key(mid, &disk_key, 0);
1043 btrfs_set_node_key(parent, &disk_key, pslot);
1044 btrfs_mark_buffer_dirty(parent);
1045 if (btrfs_header_nritems(left) > orig_slot) {
1046 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001047 path->slots[level + 1] -= 1;
1048 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001049 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001050 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001051 } else {
1052 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001053 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001054 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001055 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001057 }
Chris Masone66f7092007-04-20 13:16:02 -04001058 return 0;
1059 }
Chris Mason925baed2008-06-25 16:01:30 -04001060 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001061 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001062 }
Chris Mason925baed2008-06-25 16:01:30 -04001063 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001064
1065 /*
1066 * then try to empty the right most buffer into the middle
1067 */
Chris Mason5f39d392007-10-15 16:14:19 -04001068 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001069 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001070 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001071 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001072 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1073 wret = 1;
1074 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001075 ret = btrfs_cow_block(trans, root, right,
1076 parent, pslot + 1,
1077 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001078 if (ret)
1079 wret = 1;
1080 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001081 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001082 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001083 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001084 }
Chris Masone66f7092007-04-20 13:16:02 -04001085 if (wret < 0)
1086 ret = wret;
1087 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001088 struct btrfs_disk_key disk_key;
1089
1090 btrfs_node_key(right, &disk_key, 0);
1091 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1092 btrfs_mark_buffer_dirty(parent);
1093
1094 if (btrfs_header_nritems(mid) <= orig_slot) {
1095 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001096 path->slots[level + 1] += 1;
1097 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001098 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001099 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001101 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001102 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001104 }
Chris Masone66f7092007-04-20 13:16:02 -04001105 return 0;
1106 }
Chris Mason925baed2008-06-25 16:01:30 -04001107 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001108 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001109 }
Chris Masone66f7092007-04-20 13:16:02 -04001110 return 1;
1111}
1112
Chris Mason74123bd2007-02-02 11:05:29 -05001113/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001114 * readahead one full node of leaves
1115 */
1116static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001117 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001118{
Chris Mason5f39d392007-10-15 16:14:19 -04001119 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001120 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001121 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001122 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001123 u64 lowest_read;
1124 u64 highest_read;
1125 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001126 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001127 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001128 u32 nr;
1129 u32 blocksize;
1130 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001131
Chris Masona6b6e752007-10-15 16:22:39 -04001132 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001133 return;
1134
Chris Mason6702ed42007-08-07 16:15:09 -04001135 if (!path->nodes[level])
1136 return;
1137
Chris Mason5f39d392007-10-15 16:14:19 -04001138 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001139
Chris Mason3c69fae2007-08-07 15:52:22 -04001140 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001141 blocksize = btrfs_level_size(root, level - 1);
1142 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001143 if (eb) {
1144 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001145 return;
1146 }
1147
Chris Mason6b800532007-10-15 16:17:34 -04001148 highest_read = search;
1149 lowest_read = search;
1150
Chris Mason5f39d392007-10-15 16:14:19 -04001151 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001152 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001153 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001154 if (direction < 0) {
1155 if (nr == 0)
1156 break;
1157 nr--;
1158 } else if (direction > 0) {
1159 nr++;
1160 if (nr >= nritems)
1161 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001162 }
Chris Mason01f46652007-12-21 16:24:26 -05001163 if (path->reada < 0 && objectid) {
1164 btrfs_node_key(node, &disk_key, nr);
1165 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1166 break;
1167 }
Chris Mason6b800532007-10-15 16:17:34 -04001168 search = btrfs_node_blockptr(node, nr);
1169 if ((search >= lowest_read && search <= highest_read) ||
1170 (search < lowest_read && lowest_read - search <= 32768) ||
1171 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001172 readahead_tree_block(root, search, blocksize,
1173 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001174 nread += blocksize;
1175 }
1176 nscan++;
1177 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1178 break;
1179 if(nread > (1024 * 1024) || nscan > 128)
1180 break;
1181
1182 if (search < lowest_read)
1183 lowest_read = search;
1184 if (search > highest_read)
1185 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001186 }
1187}
Chris Mason925baed2008-06-25 16:01:30 -04001188
1189static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1190{
1191 int i;
1192 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001193 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001194 struct extent_buffer *t;
1195
1196 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1197 if (!path->nodes[i])
1198 break;
1199 if (!path->locks[i])
1200 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001201 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001202 skip_level = i + 1;
1203 continue;
1204 }
Chris Mason051e1b92008-06-25 16:01:30 -04001205 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001206 u32 nritems;
1207 t = path->nodes[i];
1208 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001209 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001210 skip_level = i + 1;
1211 continue;
1212 }
1213 }
Chris Mason051e1b92008-06-25 16:01:30 -04001214 if (skip_level < i && i >= lowest_unlock)
1215 no_skips = 1;
1216
Chris Mason925baed2008-06-25 16:01:30 -04001217 t = path->nodes[i];
1218 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1219 btrfs_tree_unlock(t);
1220 path->locks[i] = 0;
1221 }
1222 }
1223}
1224
Chris Mason3c69fae2007-08-07 15:52:22 -04001225/*
Chris Mason74123bd2007-02-02 11:05:29 -05001226 * look for key in the tree. path is filled in with nodes along the way
1227 * if key is found, we return zero and you can find the item in the leaf
1228 * level of the path (level 0)
1229 *
1230 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001231 * be inserted, and 1 is returned. If there are other errors during the
1232 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001233 *
1234 * if ins_len > 0, nodes and leaves will be split as we walk down the
1235 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1236 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001237 */
Chris Masone089f052007-03-16 16:20:31 -04001238int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1239 *root, struct btrfs_key *key, struct btrfs_path *p, int
1240 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001241{
Chris Mason5f39d392007-10-15 16:14:19 -04001242 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001243 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001244 int slot;
1245 int ret;
1246 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001248 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001249 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001250 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001251 u64 blocknr;
1252 u64 gen;
Chris Mason9f3a7422007-08-07 15:52:19 -04001253
Chris Mason6702ed42007-08-07 16:15:09 -04001254 lowest_level = p->lowest_level;
1255 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001256 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001257 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001258 !mutex_is_locked(&root->fs_info->alloc_mutex));
1259 WARN_ON(root == root->fs_info->chunk_root &&
1260 !mutex_is_locked(&root->fs_info->chunk_mutex));
1261 WARN_ON(root == root->fs_info->dev_root &&
1262 !mutex_is_locked(&root->fs_info->chunk_mutex));
1263 if (ins_len < 0)
1264 lowest_unlock = 2;
Chris Masonbb803952007-03-01 12:04:21 -05001265again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001266 if (p->skip_locking)
1267 b = btrfs_root_node(root);
1268 else
1269 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001270
Chris Masoneb60cea2007-02-02 09:18:22 -05001271 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001272 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001273 if (cow) {
1274 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001275 wret = btrfs_cow_block(trans, root, b,
1276 p->nodes[level + 1],
1277 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001278 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001279 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001280 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001281 return wret;
1282 }
Chris Mason02217ed2007-03-02 16:08:05 -05001283 }
1284 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001285 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001286 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001287 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001288 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001289 if (!p->skip_locking)
1290 p->locks[level] = 1;
Chris Mason123abc82007-03-14 14:14:43 -04001291 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001292 if (ret)
1293 return -1;
Chris Mason925baed2008-06-25 16:01:30 -04001294
Chris Mason5f39d392007-10-15 16:14:19 -04001295 ret = bin_search(b, key, level, &slot);
1296 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001297 if (ret && slot > 0)
1298 slot -= 1;
1299 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001300 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001301 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001302 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001303 BUG_ON(sret > 0);
1304 if (sret)
1305 return sret;
1306 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001307 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001308 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001309 int sret = balance_level(trans, root, p,
1310 level);
Chris Masonbb803952007-03-01 12:04:21 -05001311 if (sret)
1312 return sret;
1313 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001314 if (!b) {
1315 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001316 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001317 }
Chris Masonbb803952007-03-01 12:04:21 -05001318 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001319 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001320 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001321 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001322 if (level == lowest_level) {
1323 unlock_up(p, level, lowest_unlock);
Chris Mason9f3a7422007-08-07 15:52:19 -04001324 break;
Chris Mason925baed2008-06-25 16:01:30 -04001325 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001326
Chris Mason6702ed42007-08-07 16:15:09 -04001327 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001328 reada_for_search(root, p, level, slot,
1329 key->objectid);
Chris Masonca7a79a2008-05-12 12:59:19 -04001330
Chris Mason594a24e2008-06-25 16:01:30 -04001331 blocknr = btrfs_node_blockptr(b, slot);
1332 gen = btrfs_node_ptr_generation(b, slot);
1333 blocksize = btrfs_level_size(root, level - 1);
1334
1335 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1336 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001337 b = tmp;
1338 } else {
1339 /*
1340 * reduce lock contention at high levels
1341 * of the btree by dropping locks before
1342 * we read.
1343 */
1344 if (level > 1) {
1345 btrfs_release_path(NULL, p);
1346 if (tmp)
1347 free_extent_buffer(tmp);
Chris Mason594a24e2008-06-25 16:01:30 -04001348 tmp = read_tree_block(root, blocknr,
1349 blocksize, gen);
1350 if (tmp)
1351 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001352 goto again;
1353 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001354 if (tmp)
1355 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001356 b = read_node_slot(root, b, slot);
1357 }
1358 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001359 if (!p->skip_locking)
1360 btrfs_tree_lock(b);
Chris Mason051e1b92008-06-25 16:01:30 -04001361 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001362 } else {
1363 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001364 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001365 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001366 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001367 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001368 BUG_ON(sret > 0);
1369 if (sret)
1370 return sret;
1371 }
Chris Mason925baed2008-06-25 16:01:30 -04001372 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001373 return ret;
1374 }
1375 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001376 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001377}
1378
Chris Mason74123bd2007-02-02 11:05:29 -05001379/*
1380 * adjust the pointers going up the tree, starting at level
1381 * making sure the right key of each node is points to 'key'.
1382 * This is used after shifting pointers to the left, so it stops
1383 * fixing up pointers when a given leaf/node is not in slot 0 of the
1384 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001385 *
1386 * If this fails to write a tree block, it returns -1, but continues
1387 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001388 */
Chris Mason5f39d392007-10-15 16:14:19 -04001389static int fixup_low_keys(struct btrfs_trans_handle *trans,
1390 struct btrfs_root *root, struct btrfs_path *path,
1391 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001392{
1393 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001394 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001395 struct extent_buffer *t;
1396
Chris Mason234b63a2007-03-13 10:46:10 -04001397 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001398 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001399 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001400 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001401 t = path->nodes[i];
1402 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001403 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001404 if (tslot != 0)
1405 break;
1406 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001407 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001408}
1409
Chris Mason74123bd2007-02-02 11:05:29 -05001410/*
1411 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001412 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001413 *
1414 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1415 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001416 */
Chris Mason98ed5172008-01-03 10:01:48 -05001417static int push_node_left(struct btrfs_trans_handle *trans,
1418 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001419 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001420{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001421 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001422 int src_nritems;
1423 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001424 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001425
Chris Mason5f39d392007-10-15 16:14:19 -04001426 src_nritems = btrfs_header_nritems(src);
1427 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001428 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001429 WARN_ON(btrfs_header_generation(src) != trans->transid);
1430 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001431
Chris Masonbce4eae2008-04-24 14:42:46 -04001432 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001433 return 1;
1434
Chris Masoneb60cea2007-02-02 09:18:22 -05001435 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001436 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001437 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001438
Chris Masonbce4eae2008-04-24 14:42:46 -04001439 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001440 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001441 if (push_items < src_nritems) {
1442 /* leave at least 8 pointers in the node if
1443 * we aren't going to empty it
1444 */
1445 if (src_nritems - push_items < 8) {
1446 if (push_items <= 8)
1447 return 1;
1448 push_items -= 8;
1449 }
1450 }
1451 } else
1452 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001453
Chris Mason5f39d392007-10-15 16:14:19 -04001454 copy_extent_buffer(dst, src,
1455 btrfs_node_key_ptr_offset(dst_nritems),
1456 btrfs_node_key_ptr_offset(0),
1457 push_items * sizeof(struct btrfs_key_ptr));
1458
Chris Masonbb803952007-03-01 12:04:21 -05001459 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001460 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1461 btrfs_node_key_ptr_offset(push_items),
1462 (src_nritems - push_items) *
1463 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001464 }
Chris Mason5f39d392007-10-15 16:14:19 -04001465 btrfs_set_header_nritems(src, src_nritems - push_items);
1466 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1467 btrfs_mark_buffer_dirty(src);
1468 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001469 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001470}
1471
Chris Mason97571fd2007-02-24 13:39:08 -05001472/*
Chris Mason79f95c82007-03-01 15:16:26 -05001473 * try to push data from one node into the next node right in the
1474 * tree.
1475 *
1476 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1477 * error, and > 0 if there was no room in the right hand block.
1478 *
1479 * this will only push up to 1/2 the contents of the left node over
1480 */
Chris Mason5f39d392007-10-15 16:14:19 -04001481static int balance_node_right(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct extent_buffer *dst,
1484 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001485{
Chris Mason79f95c82007-03-01 15:16:26 -05001486 int push_items = 0;
1487 int max_push;
1488 int src_nritems;
1489 int dst_nritems;
1490 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001491
Chris Mason7bb86312007-12-11 09:25:06 -05001492 WARN_ON(btrfs_header_generation(src) != trans->transid);
1493 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1494
Chris Mason5f39d392007-10-15 16:14:19 -04001495 src_nritems = btrfs_header_nritems(src);
1496 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001497 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001498 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001499 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001500 }
1501
1502 if (src_nritems < 4) {
1503 return 1;
1504 }
Chris Mason79f95c82007-03-01 15:16:26 -05001505
1506 max_push = src_nritems / 2 + 1;
1507 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001508 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001509 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001510 }
Yan252c38f2007-08-29 09:11:44 -04001511
Chris Mason79f95c82007-03-01 15:16:26 -05001512 if (max_push < push_items)
1513 push_items = max_push;
1514
Chris Mason5f39d392007-10-15 16:14:19 -04001515 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1516 btrfs_node_key_ptr_offset(0),
1517 (dst_nritems) *
1518 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001519
Chris Mason5f39d392007-10-15 16:14:19 -04001520 copy_extent_buffer(dst, src,
1521 btrfs_node_key_ptr_offset(0),
1522 btrfs_node_key_ptr_offset(src_nritems - push_items),
1523 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001524
Chris Mason5f39d392007-10-15 16:14:19 -04001525 btrfs_set_header_nritems(src, src_nritems - push_items);
1526 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001527
Chris Mason5f39d392007-10-15 16:14:19 -04001528 btrfs_mark_buffer_dirty(src);
1529 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001530 return ret;
1531}
1532
1533/*
Chris Mason97571fd2007-02-24 13:39:08 -05001534 * helper function to insert a new root level in the tree.
1535 * A new node is allocated, and a single item is inserted to
1536 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001537 *
1538 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001539 */
Chris Mason98ed5172008-01-03 10:01:48 -05001540static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001541 struct btrfs_root *root,
1542 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001543{
Chris Mason7bb86312007-12-11 09:25:06 -05001544 u64 root_gen;
1545 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001546 struct extent_buffer *lower;
1547 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001548 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001549 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001550
1551 BUG_ON(path->nodes[level]);
1552 BUG_ON(path->nodes[level-1] != root->node);
1553
Chris Mason7bb86312007-12-11 09:25:06 -05001554 if (root->ref_cows)
1555 root_gen = trans->transid;
1556 else
1557 root_gen = 0;
1558
1559 lower = path->nodes[level-1];
1560 if (level == 1)
1561 btrfs_item_key(lower, &lower_key, 0);
1562 else
1563 btrfs_node_key(lower, &lower_key, 0);
1564
Chris Mason925baed2008-06-25 16:01:30 -04001565 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001566 root->root_key.objectid,
1567 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001568 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001569 if (IS_ERR(c))
1570 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001571
Chris Mason5f39d392007-10-15 16:14:19 -04001572 memset_extent_buffer(c, 0, 0, root->nodesize);
1573 btrfs_set_header_nritems(c, 1);
1574 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001575 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001576 btrfs_set_header_generation(c, trans->transid);
1577 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001578
Chris Mason5f39d392007-10-15 16:14:19 -04001579 write_extent_buffer(c, root->fs_info->fsid,
1580 (unsigned long)btrfs_header_fsid(c),
1581 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001582
1583 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1584 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1585 BTRFS_UUID_SIZE);
1586
Chris Mason5f39d392007-10-15 16:14:19 -04001587 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001588 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001589 lower_gen = btrfs_header_generation(lower);
1590 WARN_ON(lower_gen == 0);
1591
1592 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001593
1594 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001595
Chris Mason925baed2008-06-25 16:01:30 -04001596 spin_lock(&root->node_lock);
1597 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001598 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001599 spin_unlock(&root->node_lock);
1600
1601 /* the super has an extra ref to root->node */
1602 free_extent_buffer(old);
1603
Chris Mason0b86a832008-03-24 15:01:56 -04001604 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001605 extent_buffer_get(c);
1606 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001607 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001608 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001609
1610 if (root->ref_cows && lower_gen != trans->transid) {
1611 struct btrfs_path *back_path = btrfs_alloc_path();
1612 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001613 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001614 ret = btrfs_insert_extent_backref(trans,
1615 root->fs_info->extent_root,
1616 path, lower->start,
1617 root->root_key.objectid,
1618 trans->transid, 0, 0);
1619 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001620 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001621 btrfs_free_path(back_path);
1622 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001623 return 0;
1624}
1625
Chris Mason74123bd2007-02-02 11:05:29 -05001626/*
1627 * worker function to insert a single pointer in a node.
1628 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001629 *
Chris Mason74123bd2007-02-02 11:05:29 -05001630 * slot and level indicate where you want the key to go, and
1631 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001632 *
1633 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001634 */
Chris Masone089f052007-03-16 16:20:31 -04001635static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1636 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001637 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001638{
Chris Mason5f39d392007-10-15 16:14:19 -04001639 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001640 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001641
1642 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001643 lower = path->nodes[level];
1644 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001645 if (slot > nritems)
1646 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001647 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001648 BUG();
1649 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001650 memmove_extent_buffer(lower,
1651 btrfs_node_key_ptr_offset(slot + 1),
1652 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001653 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001654 }
Chris Mason5f39d392007-10-15 16:14:19 -04001655 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001656 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001657 WARN_ON(trans->transid == 0);
1658 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001659 btrfs_set_header_nritems(lower, nritems + 1);
1660 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001661 return 0;
1662}
1663
Chris Mason97571fd2007-02-24 13:39:08 -05001664/*
1665 * split the node at the specified level in path in two.
1666 * The path is corrected to point to the appropriate node after the split
1667 *
1668 * Before splitting this tries to make some room in the node by pushing
1669 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001670 *
1671 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001672 */
Chris Masone089f052007-03-16 16:20:31 -04001673static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1674 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001675{
Chris Mason7bb86312007-12-11 09:25:06 -05001676 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001677 struct extent_buffer *c;
1678 struct extent_buffer *split;
1679 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001680 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001681 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001682 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001683 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001684
Chris Mason5f39d392007-10-15 16:14:19 -04001685 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001686 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001687 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001688 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001689 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001690 if (ret)
1691 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001692 } else {
1693 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001694 c = path->nodes[level];
1695 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001696 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001697 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001698 if (ret < 0)
1699 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001700 }
Chris Masone66f7092007-04-20 13:16:02 -04001701
Chris Mason5f39d392007-10-15 16:14:19 -04001702 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001703 if (root->ref_cows)
1704 root_gen = trans->transid;
1705 else
1706 root_gen = 0;
1707
1708 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001709 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001710 root->root_key.objectid,
1711 root_gen,
1712 btrfs_disk_key_objectid(&disk_key),
1713 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001714 if (IS_ERR(split))
1715 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001716
Chris Mason5f39d392007-10-15 16:14:19 -04001717 btrfs_set_header_flags(split, btrfs_header_flags(c));
1718 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001719 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001720 btrfs_set_header_generation(split, trans->transid);
1721 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001722 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001723 write_extent_buffer(split, root->fs_info->fsid,
1724 (unsigned long)btrfs_header_fsid(split),
1725 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001726 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1727 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1728 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001729
Chris Mason7518a232007-03-12 12:01:18 -04001730 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001731
1732 copy_extent_buffer(split, c,
1733 btrfs_node_key_ptr_offset(0),
1734 btrfs_node_key_ptr_offset(mid),
1735 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1736 btrfs_set_header_nritems(split, c_nritems - mid);
1737 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001738 ret = 0;
1739
Chris Mason5f39d392007-10-15 16:14:19 -04001740 btrfs_mark_buffer_dirty(c);
1741 btrfs_mark_buffer_dirty(split);
1742
1743 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001744 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001745 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001746 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001747 if (wret)
1748 ret = wret;
1749
Chris Mason5de08d72007-02-24 06:24:44 -05001750 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001751 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001752 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001753 free_extent_buffer(c);
1754 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001755 path->slots[level + 1] += 1;
1756 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001757 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001758 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001760 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001761}
1762
Chris Mason74123bd2007-02-02 11:05:29 -05001763/*
1764 * how many bytes are required to store the items in a leaf. start
1765 * and nr indicate which items in the leaf to check. This totals up the
1766 * space used both by the item structs and the item data
1767 */
Chris Mason5f39d392007-10-15 16:14:19 -04001768static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769{
1770 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001771 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001772 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773
1774 if (!nr)
1775 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001776 data_len = btrfs_item_end_nr(l, start);
1777 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001778 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001779 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001780 return data_len;
1781}
1782
Chris Mason74123bd2007-02-02 11:05:29 -05001783/*
Chris Masond4dbff92007-04-04 14:08:15 -04001784 * The space between the end of the leaf items and
1785 * the start of the leaf data. IOW, how much room
1786 * the leaf has left for both items and data
1787 */
Chris Mason5f39d392007-10-15 16:14:19 -04001788int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001789{
Chris Mason5f39d392007-10-15 16:14:19 -04001790 int nritems = btrfs_header_nritems(leaf);
1791 int ret;
1792 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1793 if (ret < 0) {
1794 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001795 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001796 leaf_space_used(leaf, 0, nritems), nritems);
1797 }
1798 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001799}
1800
1801/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001802 * push some data in the path leaf to the right, trying to free up at
1803 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001804 *
1805 * returns 1 if the push failed because the other node didn't have enough
1806 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001807 */
Chris Masone089f052007-03-16 16:20:31 -04001808static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001809 *root, struct btrfs_path *path, int data_size,
1810 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001811{
Chris Mason5f39d392007-10-15 16:14:19 -04001812 struct extent_buffer *left = path->nodes[0];
1813 struct extent_buffer *right;
1814 struct extent_buffer *upper;
1815 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001816 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001817 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001818 int free_space;
1819 int push_space = 0;
1820 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001821 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001822 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001823 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001824 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001825 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001826 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001827 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001828
1829 slot = path->slots[1];
1830 if (!path->nodes[1]) {
1831 return 1;
1832 }
1833 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001834 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001835 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001836
Chris Masona2135012008-06-25 16:01:30 -04001837 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1838
Chris Masonca7a79a2008-05-12 12:59:19 -04001839 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001840 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001841 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001842 if (free_space < data_size + sizeof(struct btrfs_item))
1843 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001844
Chris Mason5f39d392007-10-15 16:14:19 -04001845 /* cow and double check */
1846 ret = btrfs_cow_block(trans, root, right, upper,
1847 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04001848 if (ret)
1849 goto out_unlock;
1850
Chris Mason5f39d392007-10-15 16:14:19 -04001851 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001852 if (free_space < data_size + sizeof(struct btrfs_item))
1853 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001854
1855 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001856 if (left_nritems == 0)
1857 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001858
Chris Mason34a38212007-11-07 13:31:03 -05001859 if (empty)
1860 nr = 0;
1861 else
1862 nr = 1;
1863
1864 i = left_nritems - 1;
1865 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001866 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001867
Chris Mason00ec4c52007-02-24 12:47:20 -05001868 if (path->slots[0] == i)
1869 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001870
1871 if (!left->map_token) {
1872 map_extent_buffer(left, (unsigned long)item,
1873 sizeof(struct btrfs_item),
1874 &left->map_token, &left->kaddr,
1875 &left->map_start, &left->map_len,
1876 KM_USER1);
1877 }
1878
1879 this_item_size = btrfs_item_size(left, item);
1880 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001881 break;
1882 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001883 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001884 if (i == 0)
1885 break;
1886 i--;
Chris Masondb945352007-10-15 16:15:53 -04001887 }
1888 if (left->map_token) {
1889 unmap_extent_buffer(left, left->map_token, KM_USER1);
1890 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001891 }
Chris Mason5f39d392007-10-15 16:14:19 -04001892
Chris Mason925baed2008-06-25 16:01:30 -04001893 if (push_items == 0)
1894 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001895
Chris Mason34a38212007-11-07 13:31:03 -05001896 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001897 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001898
Chris Mason00ec4c52007-02-24 12:47:20 -05001899 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001900 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001901
Chris Mason5f39d392007-10-15 16:14:19 -04001902 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001903 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001904
Chris Mason00ec4c52007-02-24 12:47:20 -05001905 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001906 data_end = leaf_data_end(root, right);
1907 memmove_extent_buffer(right,
1908 btrfs_leaf_data(right) + data_end - push_space,
1909 btrfs_leaf_data(right) + data_end,
1910 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1911
Chris Mason00ec4c52007-02-24 12:47:20 -05001912 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001913 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001914 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1915 btrfs_leaf_data(left) + leaf_data_end(root, left),
1916 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001917
1918 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1919 btrfs_item_nr_offset(0),
1920 right_nritems * sizeof(struct btrfs_item));
1921
Chris Mason00ec4c52007-02-24 12:47:20 -05001922 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001923 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1924 btrfs_item_nr_offset(left_nritems - push_items),
1925 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001926
1927 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001928 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001930 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001931 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001932 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001933 if (!right->map_token) {
1934 map_extent_buffer(right, (unsigned long)item,
1935 sizeof(struct btrfs_item),
1936 &right->map_token, &right->kaddr,
1937 &right->map_start, &right->map_len,
1938 KM_USER1);
1939 }
1940 push_space -= btrfs_item_size(right, item);
1941 btrfs_set_item_offset(right, item, push_space);
1942 }
1943
1944 if (right->map_token) {
1945 unmap_extent_buffer(right, right->map_token, KM_USER1);
1946 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001947 }
Chris Mason7518a232007-03-12 12:01:18 -04001948 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001950
Chris Mason34a38212007-11-07 13:31:03 -05001951 if (left_nritems)
1952 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001953 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001954
Chris Mason5f39d392007-10-15 16:14:19 -04001955 btrfs_item_key(right, &disk_key, 0);
1956 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001957 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001958
Chris Mason00ec4c52007-02-24 12:47:20 -05001959 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001960 if (path->slots[0] >= left_nritems) {
1961 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04001962 if (btrfs_header_nritems(path->nodes[0]) == 0)
1963 clean_tree_block(trans, root, path->nodes[0]);
1964 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04001965 free_extent_buffer(path->nodes[0]);
1966 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001967 path->slots[1] += 1;
1968 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001969 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001971 }
1972 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04001973
1974out_unlock:
1975 btrfs_tree_unlock(right);
1976 free_extent_buffer(right);
1977 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05001978}
Chris Mason925baed2008-06-25 16:01:30 -04001979
Chris Mason00ec4c52007-02-24 12:47:20 -05001980/*
Chris Mason74123bd2007-02-02 11:05:29 -05001981 * push some data in the path leaf to the left, trying to free up at
1982 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1983 */
Chris Masone089f052007-03-16 16:20:31 -04001984static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001985 *root, struct btrfs_path *path, int data_size,
1986 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001987{
Chris Mason5f39d392007-10-15 16:14:19 -04001988 struct btrfs_disk_key disk_key;
1989 struct extent_buffer *right = path->nodes[0];
1990 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001991 int slot;
1992 int i;
1993 int free_space;
1994 int push_space = 0;
1995 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001996 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001997 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001998 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001999 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002000 int ret = 0;
2001 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002002 u32 this_item_size;
2003 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002004
2005 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002006 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002007 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002008 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002009 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason3685f792007-10-19 09:23:27 -04002011 right_nritems = btrfs_header_nritems(right);
2012 if (right_nritems == 0) {
2013 return 1;
2014 }
2015
Chris Masona2135012008-06-25 16:01:30 -04002016 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2017
Chris Masonca7a79a2008-05-12 12:59:19 -04002018 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002019 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002020 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002021 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002022 ret = 1;
2023 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002024 }
Chris Mason02217ed2007-03-02 16:08:05 -05002025
2026 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002027 ret = btrfs_cow_block(trans, root, left,
2028 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002029 if (ret) {
2030 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002031 ret = 1;
2032 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002033 }
Chris Mason3685f792007-10-19 09:23:27 -04002034
Chris Mason123abc82007-03-14 14:14:43 -04002035 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002036 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002037 ret = 1;
2038 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002039 }
2040
Chris Mason34a38212007-11-07 13:31:03 -05002041 if (empty)
2042 nr = right_nritems;
2043 else
2044 nr = right_nritems - 1;
2045
2046 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002047 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002048 if (!right->map_token) {
2049 map_extent_buffer(right, (unsigned long)item,
2050 sizeof(struct btrfs_item),
2051 &right->map_token, &right->kaddr,
2052 &right->map_start, &right->map_len,
2053 KM_USER1);
2054 }
2055
Chris Masonbe0e5c02007-01-26 15:51:26 -05002056 if (path->slots[0] == i)
2057 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002058
2059 this_item_size = btrfs_item_size(right, item);
2060 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002061 break;
Chris Masondb945352007-10-15 16:15:53 -04002062
Chris Masonbe0e5c02007-01-26 15:51:26 -05002063 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002064 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002065 }
Chris Masondb945352007-10-15 16:15:53 -04002066
2067 if (right->map_token) {
2068 unmap_extent_buffer(right, right->map_token, KM_USER1);
2069 right->map_token = NULL;
2070 }
2071
Chris Masonbe0e5c02007-01-26 15:51:26 -05002072 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002073 ret = 1;
2074 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002075 }
Chris Mason34a38212007-11-07 13:31:03 -05002076 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002077 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002078
Chris Masonbe0e5c02007-01-26 15:51:26 -05002079 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002080 copy_extent_buffer(left, right,
2081 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2082 btrfs_item_nr_offset(0),
2083 push_items * sizeof(struct btrfs_item));
2084
Chris Mason123abc82007-03-14 14:14:43 -04002085 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002086 btrfs_item_offset_nr(right, push_items -1);
2087
2088 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002089 leaf_data_end(root, left) - push_space,
2090 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002091 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002092 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002093 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002094 BUG_ON(old_left_nritems < 0);
2095
Chris Masondb945352007-10-15 16:15:53 -04002096 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002097 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002098 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002099
Chris Mason5f39d392007-10-15 16:14:19 -04002100 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002101 if (!left->map_token) {
2102 map_extent_buffer(left, (unsigned long)item,
2103 sizeof(struct btrfs_item),
2104 &left->map_token, &left->kaddr,
2105 &left->map_start, &left->map_len,
2106 KM_USER1);
2107 }
2108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 ioff = btrfs_item_offset(left, item);
2110 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002111 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002112 }
Chris Mason5f39d392007-10-15 16:14:19 -04002113 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002114 if (left->map_token) {
2115 unmap_extent_buffer(left, left->map_token, KM_USER1);
2116 left->map_token = NULL;
2117 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002118
2119 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002120 if (push_items > right_nritems) {
2121 printk("push items %d nr %u\n", push_items, right_nritems);
2122 WARN_ON(1);
2123 }
Chris Mason5f39d392007-10-15 16:14:19 -04002124
Chris Mason34a38212007-11-07 13:31:03 -05002125 if (push_items < right_nritems) {
2126 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2127 leaf_data_end(root, right);
2128 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2129 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2130 btrfs_leaf_data(right) +
2131 leaf_data_end(root, right), push_space);
2132
2133 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002134 btrfs_item_nr_offset(push_items),
2135 (btrfs_header_nritems(right) - push_items) *
2136 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002137 }
Yaneef1c492007-11-26 10:58:13 -05002138 right_nritems -= push_items;
2139 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002140 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 for (i = 0; i < right_nritems; i++) {
2142 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002143
2144 if (!right->map_token) {
2145 map_extent_buffer(right, (unsigned long)item,
2146 sizeof(struct btrfs_item),
2147 &right->map_token, &right->kaddr,
2148 &right->map_start, &right->map_len,
2149 KM_USER1);
2150 }
2151
2152 push_space = push_space - btrfs_item_size(right, item);
2153 btrfs_set_item_offset(right, item, push_space);
2154 }
2155 if (right->map_token) {
2156 unmap_extent_buffer(right, right->map_token, KM_USER1);
2157 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002158 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002159
Chris Mason5f39d392007-10-15 16:14:19 -04002160 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002161 if (right_nritems)
2162 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002163
Chris Mason5f39d392007-10-15 16:14:19 -04002164 btrfs_item_key(right, &disk_key, 0);
2165 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002166 if (wret)
2167 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168
2169 /* then fixup the leaf pointer in the path */
2170 if (path->slots[0] < push_items) {
2171 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002172 if (btrfs_header_nritems(path->nodes[0]) == 0)
2173 clean_tree_block(trans, root, path->nodes[0]);
2174 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002175 free_extent_buffer(path->nodes[0]);
2176 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002177 path->slots[1] -= 1;
2178 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002179 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002180 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002181 path->slots[0] -= push_items;
2182 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002183 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002184 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002185out:
2186 btrfs_tree_unlock(left);
2187 free_extent_buffer(left);
2188 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002189}
2190
Chris Mason74123bd2007-02-02 11:05:29 -05002191/*
2192 * split the path's leaf in two, making sure there is at least data_size
2193 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002194 *
2195 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002196 */
Chris Masone089f052007-03-16 16:20:31 -04002197static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002198 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002199 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002200{
Chris Mason7bb86312007-12-11 09:25:06 -05002201 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002202 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002203 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002204 int mid;
2205 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002206 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002207 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208 int data_copy_size;
2209 int rt_data_off;
2210 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002211 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002212 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002213 int double_split;
2214 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002215 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002216
Chris Masoncc0c5532007-10-25 15:42:57 -04002217 if (extend)
2218 space_needed = data_size;
2219
Chris Mason7bb86312007-12-11 09:25:06 -05002220 if (root->ref_cows)
2221 root_gen = trans->transid;
2222 else
2223 root_gen = 0;
2224
Chris Mason40689472007-03-17 14:29:23 -04002225 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002226 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002227 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002228 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002229 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002230 }
2231 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002232 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002233 if (wret < 0)
2234 return wret;
2235 }
2236 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002237
Chris Mason3685f792007-10-19 09:23:27 -04002238 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002239 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002240 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002241 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002242
Chris Mason5c680ed2007-02-22 11:39:13 -05002243 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002244 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002245 if (ret)
2246 return ret;
2247 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002248again:
2249 double_split = 0;
2250 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002251 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002252 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002253 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002254
Chris Mason7bb86312007-12-11 09:25:06 -05002255 btrfs_item_key(l, &disk_key, 0);
2256
Chris Mason925baed2008-06-25 16:01:30 -04002257 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002258 root->root_key.objectid,
2259 root_gen, disk_key.objectid, 0,
2260 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002261 if (IS_ERR(right)) {
2262 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002263 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002264 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002265
Chris Mason5f39d392007-10-15 16:14:19 -04002266 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002267 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002268 btrfs_set_header_generation(right, trans->transid);
2269 btrfs_set_header_owner(right, root->root_key.objectid);
2270 btrfs_set_header_level(right, 0);
2271 write_extent_buffer(right, root->fs_info->fsid,
2272 (unsigned long)btrfs_header_fsid(right),
2273 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002274
2275 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2276 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2277 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002278 if (mid <= slot) {
2279 if (nritems == 1 ||
2280 leaf_space_used(l, mid, nritems - mid) + space_needed >
2281 BTRFS_LEAF_DATA_SIZE(root)) {
2282 if (slot >= nritems) {
2283 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002284 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002285 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002286 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002287 path->slots[1] + 1, 1);
2288 if (wret)
2289 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002290
2291 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002292 free_extent_buffer(path->nodes[0]);
2293 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002294 path->slots[0] = 0;
2295 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002296 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002297 return ret;
2298 }
2299 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002300 if (mid != nritems &&
2301 leaf_space_used(l, mid, nritems - mid) +
2302 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2303 double_split = 1;
2304 }
Chris Masond4dbff92007-04-04 14:08:15 -04002305 }
2306 } else {
2307 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2308 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002309 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002310 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002311 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002312 wret = insert_ptr(trans, root, path,
2313 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002314 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002315 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002316 if (wret)
2317 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002318 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002319 free_extent_buffer(path->nodes[0]);
2320 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002321 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002322 if (path->slots[1] == 0) {
2323 wret = fixup_low_keys(trans, root,
2324 path, &disk_key, 1);
2325 if (wret)
2326 ret = wret;
2327 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002328 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002329 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002330 } else if (extend && slot == 0) {
2331 mid = 1;
2332 } else {
2333 mid = slot;
2334 if (mid != nritems &&
2335 leaf_space_used(l, mid, nritems - mid) +
2336 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2337 double_split = 1;
2338 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002339 }
Chris Masond4dbff92007-04-04 14:08:15 -04002340 }
2341 }
Chris Mason5f39d392007-10-15 16:14:19 -04002342 nritems = nritems - mid;
2343 btrfs_set_header_nritems(right, nritems);
2344 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2345
2346 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2347 btrfs_item_nr_offset(mid),
2348 nritems * sizeof(struct btrfs_item));
2349
2350 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002351 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2352 data_copy_size, btrfs_leaf_data(l) +
2353 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002354
Chris Mason5f39d392007-10-15 16:14:19 -04002355 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2356 btrfs_item_end_nr(l, mid);
2357
2358 for (i = 0; i < nritems; i++) {
2359 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002360 u32 ioff;
2361
2362 if (!right->map_token) {
2363 map_extent_buffer(right, (unsigned long)item,
2364 sizeof(struct btrfs_item),
2365 &right->map_token, &right->kaddr,
2366 &right->map_start, &right->map_len,
2367 KM_USER1);
2368 }
2369
2370 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002371 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002372 }
Chris Mason74123bd2007-02-02 11:05:29 -05002373
Chris Masondb945352007-10-15 16:15:53 -04002374 if (right->map_token) {
2375 unmap_extent_buffer(right, right->map_token, KM_USER1);
2376 right->map_token = NULL;
2377 }
2378
Chris Mason5f39d392007-10-15 16:14:19 -04002379 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002380 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002381 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002382 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2383 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002384 if (wret)
2385 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002386
2387 btrfs_mark_buffer_dirty(right);
2388 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002389 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002390
Chris Masonbe0e5c02007-01-26 15:51:26 -05002391 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002392 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002393 free_extent_buffer(path->nodes[0]);
2394 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002395 path->slots[0] -= mid;
2396 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002397 } else {
2398 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002399 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002400 }
Chris Mason5f39d392007-10-15 16:14:19 -04002401
Chris Masoneb60cea2007-02-02 09:18:22 -05002402 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002403
Chris Masoncc0c5532007-10-25 15:42:57 -04002404 if (double_split) {
2405 BUG_ON(num_doubles != 0);
2406 num_doubles++;
2407 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002408 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002409 return ret;
2410}
2411
Chris Masonb18c6682007-04-17 13:26:50 -04002412int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2413 struct btrfs_root *root,
2414 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002415 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002416{
2417 int ret = 0;
2418 int slot;
2419 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002420 struct extent_buffer *leaf;
2421 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002422 u32 nritems;
2423 unsigned int data_end;
2424 unsigned int old_data_start;
2425 unsigned int old_size;
2426 unsigned int size_diff;
2427 int i;
2428
2429 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002430 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002431 slot = path->slots[0];
2432
2433 old_size = btrfs_item_size_nr(leaf, slot);
2434 if (old_size == new_size)
2435 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002436
Chris Mason5f39d392007-10-15 16:14:19 -04002437 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002438 data_end = leaf_data_end(root, leaf);
2439
Chris Mason5f39d392007-10-15 16:14:19 -04002440 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002441
Chris Masonb18c6682007-04-17 13:26:50 -04002442 size_diff = old_size - new_size;
2443
2444 BUG_ON(slot < 0);
2445 BUG_ON(slot >= nritems);
2446
2447 /*
2448 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2449 */
2450 /* first correct the data pointers */
2451 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002452 u32 ioff;
2453 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002454
2455 if (!leaf->map_token) {
2456 map_extent_buffer(leaf, (unsigned long)item,
2457 sizeof(struct btrfs_item),
2458 &leaf->map_token, &leaf->kaddr,
2459 &leaf->map_start, &leaf->map_len,
2460 KM_USER1);
2461 }
2462
Chris Mason5f39d392007-10-15 16:14:19 -04002463 ioff = btrfs_item_offset(leaf, item);
2464 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002465 }
Chris Masondb945352007-10-15 16:15:53 -04002466
2467 if (leaf->map_token) {
2468 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2469 leaf->map_token = NULL;
2470 }
2471
Chris Masonb18c6682007-04-17 13:26:50 -04002472 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002473 if (from_end) {
2474 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2475 data_end + size_diff, btrfs_leaf_data(leaf) +
2476 data_end, old_data_start + new_size - data_end);
2477 } else {
2478 struct btrfs_disk_key disk_key;
2479 u64 offset;
2480
2481 btrfs_item_key(leaf, &disk_key, slot);
2482
2483 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2484 unsigned long ptr;
2485 struct btrfs_file_extent_item *fi;
2486
2487 fi = btrfs_item_ptr(leaf, slot,
2488 struct btrfs_file_extent_item);
2489 fi = (struct btrfs_file_extent_item *)(
2490 (unsigned long)fi - size_diff);
2491
2492 if (btrfs_file_extent_type(leaf, fi) ==
2493 BTRFS_FILE_EXTENT_INLINE) {
2494 ptr = btrfs_item_ptr_offset(leaf, slot);
2495 memmove_extent_buffer(leaf, ptr,
2496 (unsigned long)fi,
2497 offsetof(struct btrfs_file_extent_item,
2498 disk_bytenr));
2499 }
2500 }
2501
2502 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2503 data_end + size_diff, btrfs_leaf_data(leaf) +
2504 data_end, old_data_start - data_end);
2505
2506 offset = btrfs_disk_key_offset(&disk_key);
2507 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2508 btrfs_set_item_key(leaf, &disk_key, slot);
2509 if (slot == 0)
2510 fixup_low_keys(trans, root, path, &disk_key, 1);
2511 }
Chris Mason5f39d392007-10-15 16:14:19 -04002512
2513 item = btrfs_item_nr(leaf, slot);
2514 btrfs_set_item_size(leaf, item, new_size);
2515 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002516
2517 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002518 if (btrfs_leaf_free_space(root, leaf) < 0) {
2519 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002520 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002521 }
Chris Masonb18c6682007-04-17 13:26:50 -04002522 return ret;
2523}
2524
Chris Mason5f39d392007-10-15 16:14:19 -04002525int btrfs_extend_item(struct btrfs_trans_handle *trans,
2526 struct btrfs_root *root, struct btrfs_path *path,
2527 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002528{
2529 int ret = 0;
2530 int slot;
2531 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002532 struct extent_buffer *leaf;
2533 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002534 u32 nritems;
2535 unsigned int data_end;
2536 unsigned int old_data;
2537 unsigned int old_size;
2538 int i;
2539
2540 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002541 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002542
Chris Mason5f39d392007-10-15 16:14:19 -04002543 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002544 data_end = leaf_data_end(root, leaf);
2545
Chris Mason5f39d392007-10-15 16:14:19 -04002546 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2547 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002548 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002549 }
Chris Mason6567e832007-04-16 09:22:45 -04002550 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002551 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002552
2553 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002554 if (slot >= nritems) {
2555 btrfs_print_leaf(root, leaf);
2556 printk("slot %d too large, nritems %d\n", slot, nritems);
2557 BUG_ON(1);
2558 }
Chris Mason6567e832007-04-16 09:22:45 -04002559
2560 /*
2561 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2562 */
2563 /* first correct the data pointers */
2564 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002565 u32 ioff;
2566 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002567
2568 if (!leaf->map_token) {
2569 map_extent_buffer(leaf, (unsigned long)item,
2570 sizeof(struct btrfs_item),
2571 &leaf->map_token, &leaf->kaddr,
2572 &leaf->map_start, &leaf->map_len,
2573 KM_USER1);
2574 }
Chris Mason5f39d392007-10-15 16:14:19 -04002575 ioff = btrfs_item_offset(leaf, item);
2576 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002577 }
Chris Mason5f39d392007-10-15 16:14:19 -04002578
Chris Masondb945352007-10-15 16:15:53 -04002579 if (leaf->map_token) {
2580 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2581 leaf->map_token = NULL;
2582 }
2583
Chris Mason6567e832007-04-16 09:22:45 -04002584 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002585 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002586 data_end - data_size, btrfs_leaf_data(leaf) +
2587 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002588
Chris Mason6567e832007-04-16 09:22:45 -04002589 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002590 old_size = btrfs_item_size_nr(leaf, slot);
2591 item = btrfs_item_nr(leaf, slot);
2592 btrfs_set_item_size(leaf, item, old_size + data_size);
2593 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002594
2595 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002596 if (btrfs_leaf_free_space(root, leaf) < 0) {
2597 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002598 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002599 }
Chris Mason6567e832007-04-16 09:22:45 -04002600 return ret;
2601}
2602
Chris Mason74123bd2007-02-02 11:05:29 -05002603/*
2604 * Given a key and some data, insert an item into the tree.
2605 * This does all the path init required, making room in the tree if needed.
2606 */
Chris Mason9c583092008-01-29 15:15:18 -05002607int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002608 struct btrfs_root *root,
2609 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002610 struct btrfs_key *cpu_key, u32 *data_size,
2611 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002612{
Chris Mason5f39d392007-10-15 16:14:19 -04002613 struct extent_buffer *leaf;
2614 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002615 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002617 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002618 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002619 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002620 u32 total_size = 0;
2621 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002622 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002623 struct btrfs_disk_key disk_key;
2624
Chris Mason9c583092008-01-29 15:15:18 -05002625 for (i = 0; i < nr; i++) {
2626 total_data += data_size[i];
2627 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002628
Chris Mason9c583092008-01-29 15:15:18 -05002629 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2630 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002631 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002632 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002633 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002634 if (ret < 0)
2635 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002636
Chris Mason62e27492007-03-15 12:56:47 -04002637 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002638 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002639
Chris Mason5f39d392007-10-15 16:14:19 -04002640 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002641 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002642
Chris Mason123abc82007-03-14 14:14:43 -04002643 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002644 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002645 btrfs_print_leaf(root, leaf);
2646 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002647 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002649 }
Chris Mason5f39d392007-10-15 16:14:19 -04002650
Chris Mason62e27492007-03-15 12:56:47 -04002651 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002652 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002653
Chris Masonbe0e5c02007-01-26 15:51:26 -05002654 if (slot != nritems) {
2655 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002656 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002657
Chris Mason5f39d392007-10-15 16:14:19 -04002658 if (old_data < data_end) {
2659 btrfs_print_leaf(root, leaf);
2660 printk("slot %d old_data %d data_end %d\n",
2661 slot, old_data, data_end);
2662 BUG_ON(1);
2663 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002664 /*
2665 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2666 */
2667 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002668 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002669 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002670 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002673 if (!leaf->map_token) {
2674 map_extent_buffer(leaf, (unsigned long)item,
2675 sizeof(struct btrfs_item),
2676 &leaf->map_token, &leaf->kaddr,
2677 &leaf->map_start, &leaf->map_len,
2678 KM_USER1);
2679 }
2680
Chris Mason5f39d392007-10-15 16:14:19 -04002681 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002682 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002683 }
Chris Masondb945352007-10-15 16:15:53 -04002684 if (leaf->map_token) {
2685 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2686 leaf->map_token = NULL;
2687 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002688
2689 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002690 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002691 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002692 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002693
2694 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002695 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002696 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002697 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002698 data_end = old_data;
2699 }
Chris Mason5f39d392007-10-15 16:14:19 -04002700
Chris Mason62e27492007-03-15 12:56:47 -04002701 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002702 for (i = 0; i < nr; i++) {
2703 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2704 btrfs_set_item_key(leaf, &disk_key, slot + i);
2705 item = btrfs_item_nr(leaf, slot + i);
2706 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2707 data_end -= data_size[i];
2708 btrfs_set_item_size(leaf, item, data_size[i]);
2709 }
2710 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002711 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002712
2713 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002714 if (slot == 0) {
2715 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002716 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002717 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002718
Chris Mason5f39d392007-10-15 16:14:19 -04002719 if (btrfs_leaf_free_space(root, leaf) < 0) {
2720 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002721 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002722 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002723out:
Chris Mason62e27492007-03-15 12:56:47 -04002724 return ret;
2725}
2726
2727/*
2728 * Given a key and some data, insert an item into the tree.
2729 * This does all the path init required, making room in the tree if needed.
2730 */
Chris Masone089f052007-03-16 16:20:31 -04002731int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2732 *root, struct btrfs_key *cpu_key, void *data, u32
2733 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002734{
2735 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002736 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002737 struct extent_buffer *leaf;
2738 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002739
Chris Mason2c90e5d2007-04-02 10:50:19 -04002740 path = btrfs_alloc_path();
2741 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002742 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002743 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002744 leaf = path->nodes[0];
2745 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2746 write_extent_buffer(leaf, data, ptr, data_size);
2747 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002748 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002749 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002750 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002751}
2752
Chris Mason74123bd2007-02-02 11:05:29 -05002753/*
Chris Mason5de08d72007-02-24 06:24:44 -05002754 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002755 *
2756 * If the delete empties a node, the node is removed from the tree,
2757 * continuing all the way the root if required. The root is converted into
2758 * a leaf if all the nodes are emptied.
2759 */
Chris Masone089f052007-03-16 16:20:31 -04002760static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2761 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002762{
Chris Mason5f39d392007-10-15 16:14:19 -04002763 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002764 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002765 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002766 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002767
Chris Mason5f39d392007-10-15 16:14:19 -04002768 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002769 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002770 memmove_extent_buffer(parent,
2771 btrfs_node_key_ptr_offset(slot),
2772 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002773 sizeof(struct btrfs_key_ptr) *
2774 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002775 }
Chris Mason7518a232007-03-12 12:01:18 -04002776 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002777 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002778 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002779 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002780 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002781 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002782 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002783 struct btrfs_disk_key disk_key;
2784
2785 btrfs_node_key(parent, &disk_key, 0);
2786 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002787 if (wret)
2788 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002789 }
Chris Masond6025572007-03-30 14:27:56 -04002790 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002791 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002792}
2793
Chris Mason74123bd2007-02-02 11:05:29 -05002794/*
2795 * delete the item at the leaf level in path. If that empties
2796 * the leaf, remove it from the tree
2797 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002798int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2799 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002800{
Chris Mason5f39d392007-10-15 16:14:19 -04002801 struct extent_buffer *leaf;
2802 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002803 int last_off;
2804 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002805 int ret = 0;
2806 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002807 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002808 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002809
Chris Mason5f39d392007-10-15 16:14:19 -04002810 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002811 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2812
2813 for (i = 0; i < nr; i++)
2814 dsize += btrfs_item_size_nr(leaf, slot + i);
2815
Chris Mason5f39d392007-10-15 16:14:19 -04002816 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002817
Chris Mason85e21ba2008-01-29 15:11:36 -05002818 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002819 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002820 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002821
2822 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002823 data_end + dsize,
2824 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002825 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002826
Chris Mason85e21ba2008-01-29 15:11:36 -05002827 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002828 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002829
Chris Mason5f39d392007-10-15 16:14:19 -04002830 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002831 if (!leaf->map_token) {
2832 map_extent_buffer(leaf, (unsigned long)item,
2833 sizeof(struct btrfs_item),
2834 &leaf->map_token, &leaf->kaddr,
2835 &leaf->map_start, &leaf->map_len,
2836 KM_USER1);
2837 }
Chris Mason5f39d392007-10-15 16:14:19 -04002838 ioff = btrfs_item_offset(leaf, item);
2839 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002840 }
Chris Masondb945352007-10-15 16:15:53 -04002841
2842 if (leaf->map_token) {
2843 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2844 leaf->map_token = NULL;
2845 }
2846
Chris Mason5f39d392007-10-15 16:14:19 -04002847 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002848 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002849 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002850 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002851 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002852 btrfs_set_header_nritems(leaf, nritems - nr);
2853 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002854
Chris Mason74123bd2007-02-02 11:05:29 -05002855 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002856 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002857 if (leaf == root->node) {
2858 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002859 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002860 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002861 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002862 if (wret)
2863 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002864 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002865 leaf->start, leaf->len,
2866 btrfs_header_owner(path->nodes[1]),
2867 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002868 if (wret)
2869 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002870 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002871 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002872 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002873 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002874 struct btrfs_disk_key disk_key;
2875
2876 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002877 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002878 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002879 if (wret)
2880 ret = wret;
2881 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002882
Chris Mason74123bd2007-02-02 11:05:29 -05002883 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002884 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002885 /* push_leaf_left fixes the path.
2886 * make sure the path still points to our leaf
2887 * for possible call to del_ptr below
2888 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002889 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002890 extent_buffer_get(leaf);
2891
Chris Mason85e21ba2008-01-29 15:11:36 -05002892 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002893 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002894 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002895
2896 if (path->nodes[0] == leaf &&
2897 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002898 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002900 ret = wret;
2901 }
Chris Mason5f39d392007-10-15 16:14:19 -04002902
2903 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002904 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002905 u64 bytenr = leaf->start;
2906 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002907
Chris Mason7bb86312007-12-11 09:25:06 -05002908 root_gen = btrfs_header_generation(
2909 path->nodes[1]);
2910
Chris Masone089f052007-03-16 16:20:31 -04002911 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002912 if (wret)
2913 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002914
2915 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002916 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002917 blocksize,
2918 btrfs_header_owner(path->nodes[1]),
2919 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002920 if (wret)
2921 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002922 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002923 /* if we're still in the path, make sure
2924 * we're dirty. Otherwise, one of the
2925 * push_leaf functions must have already
2926 * dirtied this buffer
2927 */
2928 if (path->nodes[0] == leaf)
2929 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002930 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002931 }
Chris Masond5719762007-03-23 10:01:08 -04002932 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002933 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002934 }
2935 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002936 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002937}
2938
Chris Mason97571fd2007-02-24 13:39:08 -05002939/*
Chris Mason925baed2008-06-25 16:01:30 -04002940 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05002941 * returns 0 if it found something or 1 if there are no lesser leaves.
2942 * returns < 0 on io errors.
2943 */
2944int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2945{
Chris Mason925baed2008-06-25 16:01:30 -04002946 struct btrfs_key key;
2947 struct btrfs_disk_key found_key;
2948 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05002949
Chris Mason925baed2008-06-25 16:01:30 -04002950 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05002951
Chris Mason925baed2008-06-25 16:01:30 -04002952 if (key.offset > 0)
2953 key.offset--;
2954 else if (key.type > 0)
2955 key.type--;
2956 else if (key.objectid > 0)
2957 key.objectid--;
2958 else
2959 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002960
Chris Mason925baed2008-06-25 16:01:30 -04002961 btrfs_release_path(root, path);
2962 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2963 if (ret < 0)
2964 return ret;
2965 btrfs_item_key(path->nodes[0], &found_key, 0);
2966 ret = comp_keys(&found_key, &key);
2967 if (ret < 0)
2968 return 0;
2969 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002970}
2971
Chris Masone7a84562008-06-25 16:01:31 -04002972int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
2973 struct btrfs_key *key, int lowest_level)
2974{
2975 int level = lowest_level;
2976 int slot;
2977 struct extent_buffer *c;
2978
2979 while(level < BTRFS_MAX_LEVEL) {
2980 if (!path->nodes[level])
2981 return 1;
2982
2983 slot = path->slots[level] + 1;
2984 c = path->nodes[level];
2985 if (slot >= btrfs_header_nritems(c)) {
2986 level++;
2987 if (level == BTRFS_MAX_LEVEL) {
2988 return 1;
2989 }
2990 continue;
2991 }
2992 if (level == 0)
2993 btrfs_item_key_to_cpu(c, key, slot);
2994 else
2995 btrfs_node_key_to_cpu(c, key, slot);
2996 return 0;
2997 }
2998 return 1;
2999}
3000
Chris Mason7bb86312007-12-11 09:25:06 -05003001/*
Chris Mason925baed2008-06-25 16:01:30 -04003002 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003003 * returns 0 if it found something or 1 if there are no greater leaves.
3004 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003005 */
Chris Mason234b63a2007-03-13 10:46:10 -04003006int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003007{
3008 int slot;
3009 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003010 struct extent_buffer *c;
3011 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003012 struct btrfs_key key;
3013 u32 nritems;
3014 int ret;
3015
3016 nritems = btrfs_header_nritems(path->nodes[0]);
3017 if (nritems == 0) {
3018 return 1;
3019 }
3020
3021 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3022
Chris Mason925baed2008-06-25 16:01:30 -04003023 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003024 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003025 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3026 path->keep_locks = 0;
3027
3028 if (ret < 0)
3029 return ret;
3030
Chris Masona2135012008-06-25 16:01:30 -04003031 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003032 /*
3033 * by releasing the path above we dropped all our locks. A balance
3034 * could have added more items next to the key that used to be
3035 * at the very end of the block. So, check again here and
3036 * advance the path if there are now more items available.
3037 */
Chris Masona2135012008-06-25 16:01:30 -04003038 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003039 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003040 goto done;
3041 }
Chris Masond97e63b2007-02-20 16:40:44 -05003042
Chris Mason234b63a2007-03-13 10:46:10 -04003043 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003044 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003045 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003046
Chris Masond97e63b2007-02-20 16:40:44 -05003047 slot = path->slots[level] + 1;
3048 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003049 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003050 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003051 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003052 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003053 }
Chris Masond97e63b2007-02-20 16:40:44 -05003054 continue;
3055 }
Chris Mason5f39d392007-10-15 16:14:19 -04003056
Chris Mason925baed2008-06-25 16:01:30 -04003057 if (next) {
3058 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003059 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003060 }
Chris Mason5f39d392007-10-15 16:14:19 -04003061
Chris Mason925baed2008-06-25 16:01:30 -04003062 if (level == 1 && path->locks[1] && path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003063 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003064
Chris Masonca7a79a2008-05-12 12:59:19 -04003065 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003066 if (!path->skip_locking) {
3067 WARN_ON(!btrfs_tree_locked(c));
3068 btrfs_tree_lock(next);
3069 }
Chris Masond97e63b2007-02-20 16:40:44 -05003070 break;
3071 }
3072 path->slots[level] = slot;
3073 while(1) {
3074 level--;
3075 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003076 if (path->locks[level])
3077 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003078 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003079 path->nodes[level] = next;
3080 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003081 if (!path->skip_locking)
3082 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003083 if (!level)
3084 break;
Chris Mason925baed2008-06-25 16:01:30 -04003085 if (level == 1 && path->locks[1] && path->reada)
3086 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003087 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003088 if (!path->skip_locking) {
3089 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3090 btrfs_tree_lock(next);
3091 }
Chris Masond97e63b2007-02-20 16:40:44 -05003092 }
Chris Mason925baed2008-06-25 16:01:30 -04003093done:
3094 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003095 return 0;
3096}
Chris Mason0b86a832008-03-24 15:01:56 -04003097
3098int btrfs_previous_item(struct btrfs_root *root,
3099 struct btrfs_path *path, u64 min_objectid,
3100 int type)
3101{
3102 struct btrfs_key found_key;
3103 struct extent_buffer *leaf;
3104 int ret;
3105
3106 while(1) {
3107 if (path->slots[0] == 0) {
3108 ret = btrfs_prev_leaf(root, path);
3109 if (ret != 0)
3110 return ret;
3111 } else {
3112 path->slots[0]--;
3113 }
3114 leaf = path->nodes[0];
3115 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3116 if (found_key.type == type)
3117 return 0;
3118 }
3119 return 1;
3120}
3121