blob: dff4da082d06bcae80550d9f595033db20f4c1d4 [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 skip = p->skip_locking;
67 int keep = p->keep_locks;
68
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->skip_locking = skip;
80 p->keep_locks = keep;
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;
357 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400358 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400359 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400360 u64 search_start = *last_ret;
361 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400362 u64 other;
363 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400364 int end_slot;
365 int i;
366 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400367 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400368 int uptodate;
369 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500370 int progress_passed = 0;
371 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400372
Chris Mason925baed2008-06-25 16:01:30 -0400373 /* FIXME this code needs locking */
374 return 0;
375
Chris Mason5708b952007-10-25 15:43:18 -0400376 parent_level = btrfs_header_level(parent);
377 if (cache_only && parent_level != 1)
378 return 0;
379
Chris Mason6702ed42007-08-07 16:15:09 -0400380 if (trans->transaction != root->fs_info->running_transaction) {
381 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
382 root->fs_info->running_transaction->transid);
383 WARN_ON(1);
384 }
385 if (trans->transid != root->fs_info->generation) {
386 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
387 root->fs_info->generation);
388 WARN_ON(1);
389 }
Chris Mason86479a02007-09-10 19:58:16 -0400390
Chris Mason6b800532007-10-15 16:17:34 -0400391 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400392 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400393 end_slot = parent_nritems;
394
395 if (parent_nritems == 1)
396 return 0;
397
398 for (i = start_slot; i < end_slot; i++) {
399 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400400
Chris Mason5708b952007-10-25 15:43:18 -0400401 if (!parent->map_token) {
402 map_extent_buffer(parent,
403 btrfs_node_key_ptr_offset(i),
404 sizeof(struct btrfs_key_ptr),
405 &parent->map_token, &parent->kaddr,
406 &parent->map_start, &parent->map_len,
407 KM_USER1);
408 }
Chris Mason081e9572007-11-06 10:26:24 -0500409 btrfs_node_key(parent, &disk_key, i);
410 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
411 continue;
412
413 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400414 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400415 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400416 if (last_block == 0)
417 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400418
Chris Mason6702ed42007-08-07 16:15:09 -0400419 if (i > 0) {
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 Mason0ef3e662008-05-24 14:04:53 -0400423 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400424 other = btrfs_node_blockptr(parent, i + 1);
425 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400426 }
Chris Masone9d0b132007-08-10 14:06:19 -0400427 if (close) {
428 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400429 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400430 }
Chris Mason5708b952007-10-25 15:43:18 -0400431 if (parent->map_token) {
432 unmap_extent_buffer(parent, parent->map_token,
433 KM_USER1);
434 parent->map_token = NULL;
435 }
Chris Mason6702ed42007-08-07 16:15:09 -0400436
Chris Mason6b800532007-10-15 16:17:34 -0400437 cur = btrfs_find_tree_block(root, blocknr, blocksize);
438 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400439 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400440 else
441 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400442 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400443 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400444 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400445 continue;
446 }
Chris Mason6b800532007-10-15 16:17:34 -0400447 if (!cur) {
448 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400449 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400450 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400451 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400452 }
Chris Mason6702ed42007-08-07 16:15:09 -0400453 }
Chris Masone9d0b132007-08-10 14:06:19 -0400454 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400455 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400456
Chris Mason6b800532007-10-15 16:17:34 -0400457 err = __btrfs_cow_block(trans, root, cur, parent, i,
458 &tmp, search_start,
459 min(16 * blocksize,
460 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400461 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400462 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400463 break;
Yan252c38f2007-08-29 09:11:44 -0400464 }
Chris Mason6b800532007-10-15 16:17:34 -0400465 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400466 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400467 *last_ret = search_start;
468 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400469 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400470 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400471 }
Chris Mason5708b952007-10-25 15:43:18 -0400472 if (parent->map_token) {
473 unmap_extent_buffer(parent, parent->map_token,
474 KM_USER1);
475 parent->map_token = NULL;
476 }
Chris Mason6702ed42007-08-07 16:15:09 -0400477 return err;
478}
479
Chris Mason74123bd2007-02-02 11:05:29 -0500480/*
481 * The leaf data grows from end-to-front in the node.
482 * this returns the address of the start of the last item,
483 * which is the stop of the leaf data stack
484 */
Chris Mason123abc82007-03-14 14:14:43 -0400485static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400486 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500487{
Chris Mason5f39d392007-10-15 16:14:19 -0400488 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500489 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400490 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400491 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500492}
493
Chris Mason123abc82007-03-14 14:14:43 -0400494static int check_node(struct btrfs_root *root, struct btrfs_path *path,
495 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500496{
Chris Mason5f39d392007-10-15 16:14:19 -0400497 struct extent_buffer *parent = NULL;
498 struct extent_buffer *node = path->nodes[level];
499 struct btrfs_disk_key parent_key;
500 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500501 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400502 int slot;
503 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400504 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500505
506 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400507 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400508
Chris Mason8d7be552007-05-10 11:24:42 -0400509 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400510 BUG_ON(nritems == 0);
511 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400512 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400513 btrfs_node_key(parent, &parent_key, parent_slot);
514 btrfs_node_key(node, &node_key, 0);
515 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400516 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400517 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400518 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500519 }
Chris Mason123abc82007-03-14 14:14:43 -0400520 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400521 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400522 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
523 btrfs_node_key(node, &node_key, slot);
524 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400525 }
526 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400527 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
528 btrfs_node_key(node, &node_key, slot);
529 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500530 }
531 return 0;
532}
533
Chris Mason123abc82007-03-14 14:14:43 -0400534static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
535 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500536{
Chris Mason5f39d392007-10-15 16:14:19 -0400537 struct extent_buffer *leaf = path->nodes[level];
538 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500539 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400540 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400541 struct btrfs_disk_key parent_key;
542 struct btrfs_disk_key leaf_key;
543 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400544
Chris Mason5f39d392007-10-15 16:14:19 -0400545 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500546
547 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400548 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400549
550 if (nritems == 0)
551 return 0;
552
553 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400554 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400555 btrfs_node_key(parent, &parent_key, parent_slot);
556 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400557
Chris Mason5f39d392007-10-15 16:14:19 -0400558 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400559 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400560 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400561 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500562 }
Chris Mason5f39d392007-10-15 16:14:19 -0400563#if 0
564 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
565 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
566 btrfs_item_key(leaf, &leaf_key, i);
567 if (comp_keys(&leaf_key, &cpukey) >= 0) {
568 btrfs_print_leaf(root, leaf);
569 printk("slot %d offset bad key\n", i);
570 BUG_ON(1);
571 }
572 if (btrfs_item_offset_nr(leaf, i) !=
573 btrfs_item_end_nr(leaf, i + 1)) {
574 btrfs_print_leaf(root, leaf);
575 printk("slot %d offset bad\n", i);
576 BUG_ON(1);
577 }
578 if (i == 0) {
579 if (btrfs_item_offset_nr(leaf, i) +
580 btrfs_item_size_nr(leaf, i) !=
581 BTRFS_LEAF_DATA_SIZE(root)) {
582 btrfs_print_leaf(root, leaf);
583 printk("slot %d first offset bad\n", i);
584 BUG_ON(1);
585 }
586 }
587 }
588 if (nritems > 0) {
589 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
590 btrfs_print_leaf(root, leaf);
591 printk("slot %d bad size \n", nritems - 1);
592 BUG_ON(1);
593 }
594 }
595#endif
596 if (slot != 0 && slot < nritems - 1) {
597 btrfs_item_key(leaf, &leaf_key, slot);
598 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
599 if (comp_keys(&leaf_key, &cpukey) <= 0) {
600 btrfs_print_leaf(root, leaf);
601 printk("slot %d offset bad key\n", slot);
602 BUG_ON(1);
603 }
604 if (btrfs_item_offset_nr(leaf, slot - 1) !=
605 btrfs_item_end_nr(leaf, slot)) {
606 btrfs_print_leaf(root, leaf);
607 printk("slot %d offset bad\n", slot);
608 BUG_ON(1);
609 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500610 }
Chris Mason8d7be552007-05-10 11:24:42 -0400611 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400612 btrfs_item_key(leaf, &leaf_key, slot);
613 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
614 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
615 if (btrfs_item_offset_nr(leaf, slot) !=
616 btrfs_item_end_nr(leaf, slot + 1)) {
617 btrfs_print_leaf(root, leaf);
618 printk("slot %d offset bad\n", slot);
619 BUG_ON(1);
620 }
Chris Mason8d7be552007-05-10 11:24:42 -0400621 }
Chris Mason5f39d392007-10-15 16:14:19 -0400622 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
623 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500624 return 0;
625}
626
Chris Mason98ed5172008-01-03 10:01:48 -0500627static int noinline check_block(struct btrfs_root *root,
628 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500629{
Chris Masonf1885912008-04-09 16:28:12 -0400630 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400631 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400632 if (btrfs_header_level(path->nodes[level]) != level)
633 printk("warning: bad level %Lu wanted %d found %d\n",
634 path->nodes[level]->start, level,
635 btrfs_header_level(path->nodes[level]));
636 found_start = btrfs_header_bytenr(path->nodes[level]);
637 if (found_start != path->nodes[level]->start) {
638 printk("warning: bad bytentr %Lu found %Lu\n",
639 path->nodes[level]->start, found_start);
640 }
Chris Masondb945352007-10-15 16:15:53 -0400641#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400642 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400643
Chris Mason479965d2007-10-15 16:14:27 -0400644 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
645 (unsigned long)btrfs_header_fsid(buf),
646 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400647 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400648 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400649 }
Chris Masondb945352007-10-15 16:15:53 -0400650#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500651 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400652 return check_leaf(root, path, level);
653 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500654}
655
Chris Mason74123bd2007-02-02 11:05:29 -0500656/*
Chris Mason5f39d392007-10-15 16:14:19 -0400657 * search for key in the extent_buffer. The items start at offset p,
658 * and they are item_size apart. There are 'max' items in p.
659 *
Chris Mason74123bd2007-02-02 11:05:29 -0500660 * the slot in the array is returned via slot, and it points to
661 * the place where you would insert key if it is not found in
662 * the array.
663 *
664 * slot may point to max if the key is bigger than all of the keys
665 */
Chris Mason5f39d392007-10-15 16:14:19 -0400666static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
667 int item_size, struct btrfs_key *key,
668 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500669{
670 int low = 0;
671 int high = max;
672 int mid;
673 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400674 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400675 struct btrfs_disk_key unaligned;
676 unsigned long offset;
677 char *map_token = NULL;
678 char *kaddr = NULL;
679 unsigned long map_start = 0;
680 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400681 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500682
683 while(low < high) {
684 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400685 offset = p + mid * item_size;
686
687 if (!map_token || offset < map_start ||
688 (offset + sizeof(struct btrfs_disk_key)) >
689 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400690 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400691 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400692 map_token = NULL;
693 }
694 err = map_extent_buffer(eb, offset,
695 sizeof(struct btrfs_disk_key),
696 &map_token, &kaddr,
697 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400698
Chris Mason479965d2007-10-15 16:14:27 -0400699 if (!err) {
700 tmp = (struct btrfs_disk_key *)(kaddr + offset -
701 map_start);
702 } else {
703 read_extent_buffer(eb, &unaligned,
704 offset, sizeof(unaligned));
705 tmp = &unaligned;
706 }
707
Chris Mason5f39d392007-10-15 16:14:19 -0400708 } else {
709 tmp = (struct btrfs_disk_key *)(kaddr + offset -
710 map_start);
711 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500712 ret = comp_keys(tmp, key);
713
714 if (ret < 0)
715 low = mid + 1;
716 else if (ret > 0)
717 high = mid;
718 else {
719 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400720 if (map_token)
721 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500722 return 0;
723 }
724 }
725 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400726 if (map_token)
727 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 return 1;
729}
730
Chris Mason97571fd2007-02-24 13:39:08 -0500731/*
732 * simple bin_search frontend that does the right thing for
733 * leaves vs nodes
734 */
Chris Mason5f39d392007-10-15 16:14:19 -0400735static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
736 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500737{
Chris Mason5f39d392007-10-15 16:14:19 -0400738 if (level == 0) {
739 return generic_bin_search(eb,
740 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400741 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400742 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400743 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500744 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400745 return generic_bin_search(eb,
746 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400747 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400748 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400749 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500750 }
751 return -1;
752}
753
Chris Mason5f39d392007-10-15 16:14:19 -0400754static struct extent_buffer *read_node_slot(struct btrfs_root *root,
755 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500756{
Chris Masonca7a79a2008-05-12 12:59:19 -0400757 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500758 if (slot < 0)
759 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400760 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500761 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400762
763 BUG_ON(level == 0);
764
Chris Masondb945352007-10-15 16:15:53 -0400765 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400766 btrfs_level_size(root, level - 1),
767 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500768}
769
Chris Mason98ed5172008-01-03 10:01:48 -0500770static int balance_level(struct btrfs_trans_handle *trans,
771 struct btrfs_root *root,
772 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500773{
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct extent_buffer *right = NULL;
775 struct extent_buffer *mid;
776 struct extent_buffer *left = NULL;
777 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500778 int ret = 0;
779 int wret;
780 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500781 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400782 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500783 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500784
785 if (level == 0)
786 return 0;
787
Chris Mason5f39d392007-10-15 16:14:19 -0400788 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400789 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500790 WARN_ON(btrfs_header_generation(mid) != trans->transid);
791
Chris Mason1d4f8a02007-03-13 09:28:32 -0400792 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500793
Chris Mason234b63a2007-03-13 10:46:10 -0400794 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400795 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500796 pslot = path->slots[level + 1];
797
Chris Mason40689472007-03-17 14:29:23 -0400798 /*
799 * deal with the case where there is only one pointer in the root
800 * by promoting the node below to a root
801 */
Chris Mason5f39d392007-10-15 16:14:19 -0400802 if (!parent) {
803 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500804
Chris Mason5f39d392007-10-15 16:14:19 -0400805 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500806 return 0;
807
808 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400809 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400810 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500811 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500812 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
813 BUG_ON(ret);
814
Chris Mason925baed2008-06-25 16:01:30 -0400815 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500816 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400817 spin_unlock(&root->node_lock);
818
Chris Mason0b86a832008-03-24 15:01:56 -0400819 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400820 btrfs_tree_unlock(child);
821 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500822 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400824 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500825 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400826 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500827 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
828 root->root_key.objectid,
829 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500830 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400831 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400832 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500833 }
Chris Mason5f39d392007-10-15 16:14:19 -0400834 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400835 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500836 return 0;
837
Chris Mason5f39d392007-10-15 16:14:19 -0400838 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400839 err_on_enospc = 1;
840
Chris Mason5f39d392007-10-15 16:14:19 -0400841 left = read_node_slot(root, parent, pslot - 1);
842 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400843 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400844 wret = btrfs_cow_block(trans, root, left,
845 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400846 if (wret) {
847 ret = wret;
848 goto enospc;
849 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400850 }
Chris Mason5f39d392007-10-15 16:14:19 -0400851 right = read_node_slot(root, parent, pslot + 1);
852 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400853 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400854 wret = btrfs_cow_block(trans, root, right,
855 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400856 if (wret) {
857 ret = wret;
858 goto enospc;
859 }
860 }
861
862 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400863 if (left) {
864 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400865 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500866 if (wret < 0)
867 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400868 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500870 }
Chris Mason79f95c82007-03-01 15:16:26 -0500871
872 /*
873 * then try to empty the right most buffer into the middle
874 */
Chris Mason5f39d392007-10-15 16:14:19 -0400875 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400876 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400877 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500878 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400879 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400880 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500881 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400882 u32 blocksize = right->len;
883
Chris Mason5f39d392007-10-15 16:14:19 -0400884 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400885 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400886 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500887 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400888 wret = del_ptr(trans, root, path, level + 1, pslot +
889 1);
Chris Masonbb803952007-03-01 12:04:21 -0500890 if (wret)
891 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400892 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500893 blocksize,
894 btrfs_header_owner(parent),
895 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500896 if (wret)
897 ret = wret;
898 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400899 struct btrfs_disk_key right_key;
900 btrfs_node_key(right, &right_key, 0);
901 btrfs_set_node_key(parent, &right_key, pslot + 1);
902 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500903 }
904 }
Chris Mason5f39d392007-10-15 16:14:19 -0400905 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500906 /*
907 * we're not allowed to leave a node with one item in the
908 * tree during a delete. A deletion from lower in the tree
909 * could try to delete the only pointer in this node.
910 * So, pull some keys from the left.
911 * There has to be a left pointer at this point because
912 * otherwise we would have pulled some pointers from the
913 * right
914 */
Chris Mason5f39d392007-10-15 16:14:19 -0400915 BUG_ON(!left);
916 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400917 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500918 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400919 goto enospc;
920 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400921 if (wret == 1) {
922 wret = push_node_left(trans, root, left, mid, 1);
923 if (wret < 0)
924 ret = wret;
925 }
Chris Mason79f95c82007-03-01 15:16:26 -0500926 BUG_ON(wret == 1);
927 }
Chris Mason5f39d392007-10-15 16:14:19 -0400928 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500929 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500930 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400931 u64 bytenr = mid->start;
932 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400933
Chris Mason5f39d392007-10-15 16:14:19 -0400934 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400935 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400936 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500937 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400938 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500939 if (wret)
940 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500941 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
942 btrfs_header_owner(parent),
943 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500944 if (wret)
945 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500946 } else {
947 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400948 struct btrfs_disk_key mid_key;
949 btrfs_node_key(mid, &mid_key, 0);
950 btrfs_set_node_key(parent, &mid_key, pslot);
951 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500952 }
Chris Masonbb803952007-03-01 12:04:21 -0500953
Chris Mason79f95c82007-03-01 15:16:26 -0500954 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (left) {
956 if (btrfs_header_nritems(left) > orig_slot) {
957 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400958 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400959 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500960 path->slots[level + 1] -= 1;
961 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400962 if (mid) {
963 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400964 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400965 }
Chris Masonbb803952007-03-01 12:04:21 -0500966 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400967 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500968 path->slots[level] = orig_slot;
969 }
970 }
Chris Mason79f95c82007-03-01 15:16:26 -0500971 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400972 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400973 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400974 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500975 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400976enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400977 if (right) {
978 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400979 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400980 }
981 if (left) {
982 if (path->nodes[level] != left)
983 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400984 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400985 }
Chris Masonbb803952007-03-01 12:04:21 -0500986 return ret;
987}
988
Chris Masone66f7092007-04-20 13:16:02 -0400989/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500990static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
991 struct btrfs_root *root,
992 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400993{
Chris Mason5f39d392007-10-15 16:14:19 -0400994 struct extent_buffer *right = NULL;
995 struct extent_buffer *mid;
996 struct extent_buffer *left = NULL;
997 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400998 int ret = 0;
999 int wret;
1000 int pslot;
1001 int orig_slot = path->slots[level];
1002 u64 orig_ptr;
1003
1004 if (level == 0)
1005 return 1;
1006
Chris Mason5f39d392007-10-15 16:14:19 -04001007 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001008 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001009 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1010
1011 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001012 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001013 pslot = path->slots[level + 1];
1014
Chris Mason5f39d392007-10-15 16:14:19 -04001015 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001016 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001017
Chris Mason5f39d392007-10-15 16:14:19 -04001018 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001019
1020 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001021 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001022 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001023
1024 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001025 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001026 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1027 wret = 1;
1028 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001029 ret = btrfs_cow_block(trans, root, left, parent,
1030 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001031 if (ret)
1032 wret = 1;
1033 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001034 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001035 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001036 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001037 }
Chris Masone66f7092007-04-20 13:16:02 -04001038 if (wret < 0)
1039 ret = wret;
1040 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001041 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001042 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001043 btrfs_node_key(mid, &disk_key, 0);
1044 btrfs_set_node_key(parent, &disk_key, pslot);
1045 btrfs_mark_buffer_dirty(parent);
1046 if (btrfs_header_nritems(left) > orig_slot) {
1047 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001048 path->slots[level + 1] -= 1;
1049 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001050 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001051 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001052 } else {
1053 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001054 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001055 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001056 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001057 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001058 }
Chris Masone66f7092007-04-20 13:16:02 -04001059 return 0;
1060 }
Chris Mason925baed2008-06-25 16:01:30 -04001061 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001062 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001063 }
Chris Mason925baed2008-06-25 16:01:30 -04001064 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001065
1066 /*
1067 * then try to empty the right most buffer into the middle
1068 */
Chris Mason5f39d392007-10-15 16:14:19 -04001069 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001070 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001071 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001072 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001073 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1074 wret = 1;
1075 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001076 ret = btrfs_cow_block(trans, root, right,
1077 parent, pslot + 1,
1078 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001079 if (ret)
1080 wret = 1;
1081 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001082 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001083 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001084 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001085 }
Chris Masone66f7092007-04-20 13:16:02 -04001086 if (wret < 0)
1087 ret = wret;
1088 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001089 struct btrfs_disk_key disk_key;
1090
1091 btrfs_node_key(right, &disk_key, 0);
1092 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1093 btrfs_mark_buffer_dirty(parent);
1094
1095 if (btrfs_header_nritems(mid) <= orig_slot) {
1096 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001097 path->slots[level + 1] += 1;
1098 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001099 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001100 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001101 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001102 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001103 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001104 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001105 }
Chris Masone66f7092007-04-20 13:16:02 -04001106 return 0;
1107 }
Chris Mason925baed2008-06-25 16:01:30 -04001108 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001109 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001110 }
Chris Masone66f7092007-04-20 13:16:02 -04001111 return 1;
1112}
1113
Chris Mason74123bd2007-02-02 11:05:29 -05001114/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001115 * readahead one full node of leaves
1116 */
1117static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001118 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001119{
Chris Mason5f39d392007-10-15 16:14:19 -04001120 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001121 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001122 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001123 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001124 u64 lowest_read;
1125 u64 highest_read;
1126 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001127 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001128 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001129 u32 nr;
1130 u32 blocksize;
1131 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001132
Chris Masona6b6e752007-10-15 16:22:39 -04001133 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001134 return;
1135
Chris Mason6702ed42007-08-07 16:15:09 -04001136 if (!path->nodes[level])
1137 return;
1138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001140 WARN_ON(!path->skip_locking && !btrfs_tree_locked(node));
1141
Chris Mason3c69fae2007-08-07 15:52:22 -04001142 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001143 blocksize = btrfs_level_size(root, level - 1);
1144 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001145 if (eb) {
1146 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001147 return;
1148 }
1149
Chris Mason6b800532007-10-15 16:17:34 -04001150 highest_read = search;
1151 lowest_read = search;
1152
Chris Mason5f39d392007-10-15 16:14:19 -04001153 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001154 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001155 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001156 if (direction < 0) {
1157 if (nr == 0)
1158 break;
1159 nr--;
1160 } else if (direction > 0) {
1161 nr++;
1162 if (nr >= nritems)
1163 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001164 }
Chris Mason01f46652007-12-21 16:24:26 -05001165 if (path->reada < 0 && objectid) {
1166 btrfs_node_key(node, &disk_key, nr);
1167 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1168 break;
1169 }
Chris Mason6b800532007-10-15 16:17:34 -04001170 search = btrfs_node_blockptr(node, nr);
1171 if ((search >= lowest_read && search <= highest_read) ||
1172 (search < lowest_read && lowest_read - search <= 32768) ||
1173 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001174 readahead_tree_block(root, search, blocksize,
1175 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001176 nread += blocksize;
1177 }
1178 nscan++;
1179 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1180 break;
1181 if(nread > (1024 * 1024) || nscan > 128)
1182 break;
1183
1184 if (search < lowest_read)
1185 lowest_read = search;
1186 if (search > highest_read)
1187 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001188 }
1189}
Chris Mason925baed2008-06-25 16:01:30 -04001190
1191static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1192{
1193 int i;
1194 int skip_level = level;
1195 struct extent_buffer *t;
1196
1197 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1198 if (!path->nodes[i])
1199 break;
1200 if (!path->locks[i])
1201 break;
1202 if (path->slots[i] == 0) {
1203 skip_level = i + 1;
1204 continue;
1205 }
1206 if (path->keep_locks) {
1207 u32 nritems;
1208 t = path->nodes[i];
1209 nritems = btrfs_header_nritems(t);
Chris Masona2135012008-06-25 16:01:30 -04001210 if (nritems < 2 || path->slots[i] >= nritems - 2) {
1211if (path->keep_locks) {
1212//printk("path %p skip level now %d\n", path, skip_level);
1213}
Chris Mason925baed2008-06-25 16:01:30 -04001214 skip_level = i + 1;
1215 continue;
1216 }
1217 }
1218 t = path->nodes[i];
1219 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masona2135012008-06-25 16:01:30 -04001220if (path->keep_locks) {
1221//printk("path %p unlocking level %d slot %d nritems %d skip_level %d\n", path, i, path->slots[i], btrfs_header_nritems(t), skip_level);
1222}
Chris Mason925baed2008-06-25 16:01:30 -04001223 btrfs_tree_unlock(t);
1224 path->locks[i] = 0;
1225 }
1226 }
1227}
1228
Chris Mason3c69fae2007-08-07 15:52:22 -04001229/*
Chris Mason74123bd2007-02-02 11:05:29 -05001230 * look for key in the tree. path is filled in with nodes along the way
1231 * if key is found, we return zero and you can find the item in the leaf
1232 * level of the path (level 0)
1233 *
1234 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001235 * be inserted, and 1 is returned. If there are other errors during the
1236 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001237 *
1238 * if ins_len > 0, nodes and leaves will be split as we walk down the
1239 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1240 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001241 */
Chris Masone089f052007-03-16 16:20:31 -04001242int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1243 *root, struct btrfs_key *key, struct btrfs_path *p, int
1244 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001245{
Chris Mason5f39d392007-10-15 16:14:19 -04001246 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247 int slot;
1248 int ret;
1249 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001250 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001251 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001252 u8 lowest_level = 0;
1253
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 Mason925baed2008-06-25 16:01:30 -04001257 WARN_ON(root == root->fs_info->extent_root &&
1258 !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 Mason925baed2008-06-25 16:01:30 -04001266 if (!p->skip_locking)
1267 b = btrfs_lock_root_node(root);
1268 else
1269 b = btrfs_root_node(root);
1270
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 Mason925baed2008-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
1331 b = read_node_slot(root, b, slot);
Chris Mason925baed2008-06-25 16:01:30 -04001332 if (!p->skip_locking)
1333 btrfs_tree_lock(b);
Chris Masona2135012008-06-25 16:01:30 -04001334 unlock_up(p, level + 1, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001335 } else {
1336 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001337 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001338 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001339 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001340 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001341 BUG_ON(sret > 0);
1342 if (sret)
1343 return sret;
1344 }
Chris Mason925baed2008-06-25 16:01:30 -04001345 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001346 return ret;
1347 }
1348 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001349 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001350}
1351
Chris Mason74123bd2007-02-02 11:05:29 -05001352/*
1353 * adjust the pointers going up the tree, starting at level
1354 * making sure the right key of each node is points to 'key'.
1355 * This is used after shifting pointers to the left, so it stops
1356 * fixing up pointers when a given leaf/node is not in slot 0 of the
1357 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001358 *
1359 * If this fails to write a tree block, it returns -1, but continues
1360 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001361 */
Chris Mason5f39d392007-10-15 16:14:19 -04001362static int fixup_low_keys(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root, struct btrfs_path *path,
1364 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001365{
1366 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001367 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001368 struct extent_buffer *t;
1369
Chris Mason234b63a2007-03-13 10:46:10 -04001370 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001371 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001372 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001373 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001374 t = path->nodes[i];
1375 btrfs_set_node_key(t, key, tslot);
Chris Mason925baed2008-06-25 16:01:30 -04001376 if (!btrfs_tree_locked(path->nodes[i])) {
1377 int ii;
1378printk("fixup without lock on level %d\n", btrfs_header_level(path->nodes[i]));
1379 for (ii = 0; ii < BTRFS_MAX_LEVEL; ii++) {
1380printk("level %d slot %d\n", ii, path->slots[ii]);
1381 }
1382 }
Chris Masond6025572007-03-30 14:27:56 -04001383 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001384 if (tslot != 0)
1385 break;
1386 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001387 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001388}
1389
Chris Mason74123bd2007-02-02 11:05:29 -05001390/*
1391 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001392 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001393 *
1394 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1395 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001396 */
Chris Mason98ed5172008-01-03 10:01:48 -05001397static int push_node_left(struct btrfs_trans_handle *trans,
1398 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001399 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001400{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001401 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001402 int src_nritems;
1403 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001404 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001405
Chris Mason5f39d392007-10-15 16:14:19 -04001406 src_nritems = btrfs_header_nritems(src);
1407 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001408 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001409 WARN_ON(btrfs_header_generation(src) != trans->transid);
1410 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001411
Chris Masonbce4eae2008-04-24 14:42:46 -04001412 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001413 return 1;
1414
Chris Masoneb60cea2007-02-02 09:18:22 -05001415 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001416 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001417 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001418
Chris Masonbce4eae2008-04-24 14:42:46 -04001419 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001420 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001421 if (push_items < src_nritems) {
1422 /* leave at least 8 pointers in the node if
1423 * we aren't going to empty it
1424 */
1425 if (src_nritems - push_items < 8) {
1426 if (push_items <= 8)
1427 return 1;
1428 push_items -= 8;
1429 }
1430 }
1431 } else
1432 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001433
Chris Mason5f39d392007-10-15 16:14:19 -04001434 copy_extent_buffer(dst, src,
1435 btrfs_node_key_ptr_offset(dst_nritems),
1436 btrfs_node_key_ptr_offset(0),
1437 push_items * sizeof(struct btrfs_key_ptr));
1438
Chris Masonbb803952007-03-01 12:04:21 -05001439 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001440 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1441 btrfs_node_key_ptr_offset(push_items),
1442 (src_nritems - push_items) *
1443 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001444 }
Chris Mason5f39d392007-10-15 16:14:19 -04001445 btrfs_set_header_nritems(src, src_nritems - push_items);
1446 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1447 btrfs_mark_buffer_dirty(src);
1448 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001449 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001450}
1451
Chris Mason97571fd2007-02-24 13:39:08 -05001452/*
Chris Mason79f95c82007-03-01 15:16:26 -05001453 * try to push data from one node into the next node right in the
1454 * tree.
1455 *
1456 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1457 * error, and > 0 if there was no room in the right hand block.
1458 *
1459 * this will only push up to 1/2 the contents of the left node over
1460 */
Chris Mason5f39d392007-10-15 16:14:19 -04001461static int balance_node_right(struct btrfs_trans_handle *trans,
1462 struct btrfs_root *root,
1463 struct extent_buffer *dst,
1464 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001465{
Chris Mason79f95c82007-03-01 15:16:26 -05001466 int push_items = 0;
1467 int max_push;
1468 int src_nritems;
1469 int dst_nritems;
1470 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001471
Chris Mason7bb86312007-12-11 09:25:06 -05001472 WARN_ON(btrfs_header_generation(src) != trans->transid);
1473 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1474
Chris Mason5f39d392007-10-15 16:14:19 -04001475 src_nritems = btrfs_header_nritems(src);
1476 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001477 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001478 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001479 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001480 }
1481
1482 if (src_nritems < 4) {
1483 return 1;
1484 }
Chris Mason79f95c82007-03-01 15:16:26 -05001485
1486 max_push = src_nritems / 2 + 1;
1487 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001488 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001489 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001490 }
Yan252c38f2007-08-29 09:11:44 -04001491
Chris Mason79f95c82007-03-01 15:16:26 -05001492 if (max_push < push_items)
1493 push_items = max_push;
1494
Chris Mason5f39d392007-10-15 16:14:19 -04001495 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1496 btrfs_node_key_ptr_offset(0),
1497 (dst_nritems) *
1498 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001499
Chris Mason5f39d392007-10-15 16:14:19 -04001500 copy_extent_buffer(dst, src,
1501 btrfs_node_key_ptr_offset(0),
1502 btrfs_node_key_ptr_offset(src_nritems - push_items),
1503 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001504
Chris Mason5f39d392007-10-15 16:14:19 -04001505 btrfs_set_header_nritems(src, src_nritems - push_items);
1506 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001507
Chris Mason5f39d392007-10-15 16:14:19 -04001508 btrfs_mark_buffer_dirty(src);
1509 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001510 return ret;
1511}
1512
1513/*
Chris Mason97571fd2007-02-24 13:39:08 -05001514 * helper function to insert a new root level in the tree.
1515 * A new node is allocated, and a single item is inserted to
1516 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001517 *
1518 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001519 */
Chris Mason98ed5172008-01-03 10:01:48 -05001520static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001521 struct btrfs_root *root,
1522 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001523{
Chris Mason7bb86312007-12-11 09:25:06 -05001524 u64 root_gen;
1525 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001526 struct extent_buffer *lower;
1527 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001528 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001529 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001530
1531 BUG_ON(path->nodes[level]);
1532 BUG_ON(path->nodes[level-1] != root->node);
1533
Chris Mason7bb86312007-12-11 09:25:06 -05001534 if (root->ref_cows)
1535 root_gen = trans->transid;
1536 else
1537 root_gen = 0;
1538
1539 lower = path->nodes[level-1];
1540 if (level == 1)
1541 btrfs_item_key(lower, &lower_key, 0);
1542 else
1543 btrfs_node_key(lower, &lower_key, 0);
1544
Chris Mason925baed2008-06-25 16:01:30 -04001545 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001546 root->root_key.objectid,
1547 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001548 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001549 if (IS_ERR(c))
1550 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001551
Chris Mason5f39d392007-10-15 16:14:19 -04001552 memset_extent_buffer(c, 0, 0, root->nodesize);
1553 btrfs_set_header_nritems(c, 1);
1554 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001555 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001556 btrfs_set_header_generation(c, trans->transid);
1557 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001558
Chris Mason5f39d392007-10-15 16:14:19 -04001559 write_extent_buffer(c, root->fs_info->fsid,
1560 (unsigned long)btrfs_header_fsid(c),
1561 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001562
1563 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1564 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1565 BTRFS_UUID_SIZE);
1566
Chris Mason5f39d392007-10-15 16:14:19 -04001567 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001568 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001569 lower_gen = btrfs_header_generation(lower);
1570 WARN_ON(lower_gen == 0);
1571
1572 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001573
1574 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001575
Chris Mason925baed2008-06-25 16:01:30 -04001576 spin_lock(&root->node_lock);
1577 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001579 spin_unlock(&root->node_lock);
1580
1581 /* the super has an extra ref to root->node */
1582 free_extent_buffer(old);
1583
Chris Mason0b86a832008-03-24 15:01:56 -04001584 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001585 extent_buffer_get(c);
1586 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001587 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001588 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001589
1590 if (root->ref_cows && lower_gen != trans->transid) {
1591 struct btrfs_path *back_path = btrfs_alloc_path();
1592 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001593 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001594 ret = btrfs_insert_extent_backref(trans,
1595 root->fs_info->extent_root,
1596 path, lower->start,
1597 root->root_key.objectid,
1598 trans->transid, 0, 0);
1599 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001600 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001601 btrfs_free_path(back_path);
1602 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001603 return 0;
1604}
1605
Chris Mason74123bd2007-02-02 11:05:29 -05001606/*
1607 * worker function to insert a single pointer in a node.
1608 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001609 *
Chris Mason74123bd2007-02-02 11:05:29 -05001610 * slot and level indicate where you want the key to go, and
1611 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001612 *
1613 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001614 */
Chris Masone089f052007-03-16 16:20:31 -04001615static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1616 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001617 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001618{
Chris Mason5f39d392007-10-15 16:14:19 -04001619 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001620 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001621
1622 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001623 lower = path->nodes[level];
1624 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001625 if (slot > nritems)
1626 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001627 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001628 BUG();
1629 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001630 memmove_extent_buffer(lower,
1631 btrfs_node_key_ptr_offset(slot + 1),
1632 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001633 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001634 }
Chris Mason5f39d392007-10-15 16:14:19 -04001635 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001636 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001637 WARN_ON(trans->transid == 0);
1638 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001639 btrfs_set_header_nritems(lower, nritems + 1);
1640 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001641 return 0;
1642}
1643
Chris Mason97571fd2007-02-24 13:39:08 -05001644/*
1645 * split the node at the specified level in path in two.
1646 * The path is corrected to point to the appropriate node after the split
1647 *
1648 * Before splitting this tries to make some room in the node by pushing
1649 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001650 *
1651 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001652 */
Chris Masone089f052007-03-16 16:20:31 -04001653static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1654 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001655{
Chris Mason7bb86312007-12-11 09:25:06 -05001656 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001657 struct extent_buffer *c;
1658 struct extent_buffer *split;
1659 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001660 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001661 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001662 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001663 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664
Chris Mason5f39d392007-10-15 16:14:19 -04001665 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001666 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001667 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001668 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001669 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001670 if (ret)
1671 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001672 } else {
1673 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001674 c = path->nodes[level];
1675 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001676 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001677 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001678 if (ret < 0)
1679 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001680 }
Chris Masone66f7092007-04-20 13:16:02 -04001681
Chris Mason5f39d392007-10-15 16:14:19 -04001682 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001683 if (root->ref_cows)
1684 root_gen = trans->transid;
1685 else
1686 root_gen = 0;
1687
1688 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001689 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001690 root->root_key.objectid,
1691 root_gen,
1692 btrfs_disk_key_objectid(&disk_key),
1693 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001694 if (IS_ERR(split))
1695 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001696
Chris Mason5f39d392007-10-15 16:14:19 -04001697 btrfs_set_header_flags(split, btrfs_header_flags(c));
1698 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001699 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001700 btrfs_set_header_generation(split, trans->transid);
1701 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001702 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001703 write_extent_buffer(split, root->fs_info->fsid,
1704 (unsigned long)btrfs_header_fsid(split),
1705 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001706 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1707 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1708 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001709
Chris Mason7518a232007-03-12 12:01:18 -04001710 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001711
1712 copy_extent_buffer(split, c,
1713 btrfs_node_key_ptr_offset(0),
1714 btrfs_node_key_ptr_offset(mid),
1715 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1716 btrfs_set_header_nritems(split, c_nritems - mid);
1717 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001718 ret = 0;
1719
Chris Mason5f39d392007-10-15 16:14:19 -04001720 btrfs_mark_buffer_dirty(c);
1721 btrfs_mark_buffer_dirty(split);
1722
1723 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001724 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001725 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001726 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001727 if (wret)
1728 ret = wret;
1729
Chris Mason5de08d72007-02-24 06:24:44 -05001730 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001731 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001732 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001733 free_extent_buffer(c);
1734 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001735 path->slots[level + 1] += 1;
1736 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001737 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001738 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001740 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001741}
1742
Chris Mason74123bd2007-02-02 11:05:29 -05001743/*
1744 * how many bytes are required to store the items in a leaf. start
1745 * and nr indicate which items in the leaf to check. This totals up the
1746 * space used both by the item structs and the item data
1747 */
Chris Mason5f39d392007-10-15 16:14:19 -04001748static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749{
1750 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001751 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001752 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001753
1754 if (!nr)
1755 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001756 data_len = btrfs_item_end_nr(l, start);
1757 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001758 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001759 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001760 return data_len;
1761}
1762
Chris Mason74123bd2007-02-02 11:05:29 -05001763/*
Chris Masond4dbff92007-04-04 14:08:15 -04001764 * The space between the end of the leaf items and
1765 * the start of the leaf data. IOW, how much room
1766 * the leaf has left for both items and data
1767 */
Chris Mason5f39d392007-10-15 16:14:19 -04001768int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001769{
Chris Mason5f39d392007-10-15 16:14:19 -04001770 int nritems = btrfs_header_nritems(leaf);
1771 int ret;
1772 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1773 if (ret < 0) {
1774 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001775 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001776 leaf_space_used(leaf, 0, nritems), nritems);
1777 }
1778 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001779}
1780
1781/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001782 * push some data in the path leaf to the right, trying to free up at
1783 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001784 *
1785 * returns 1 if the push failed because the other node didn't have enough
1786 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001787 */
Chris Masone089f052007-03-16 16:20:31 -04001788static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001789 *root, struct btrfs_path *path, int data_size,
1790 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001791{
Chris Mason5f39d392007-10-15 16:14:19 -04001792 struct extent_buffer *left = path->nodes[0];
1793 struct extent_buffer *right;
1794 struct extent_buffer *upper;
1795 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001796 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001797 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001798 int free_space;
1799 int push_space = 0;
1800 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001801 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001802 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001803 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001804 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001805 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001806 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001807 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001808
1809 slot = path->slots[1];
1810 if (!path->nodes[1]) {
1811 return 1;
1812 }
1813 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001814 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001815 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001816
Chris Masona2135012008-06-25 16:01:30 -04001817 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1818
Chris Masonca7a79a2008-05-12 12:59:19 -04001819 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001820 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001821 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001822 if (free_space < data_size + sizeof(struct btrfs_item))
1823 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001824
Chris Mason5f39d392007-10-15 16:14:19 -04001825 /* cow and double check */
1826 ret = btrfs_cow_block(trans, root, right, upper,
1827 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04001828 if (ret)
1829 goto out_unlock;
1830
Chris Mason5f39d392007-10-15 16:14:19 -04001831 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001832 if (free_space < data_size + sizeof(struct btrfs_item))
1833 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001834
1835 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001836 if (left_nritems == 0)
1837 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001838
Chris Mason34a38212007-11-07 13:31:03 -05001839 if (empty)
1840 nr = 0;
1841 else
1842 nr = 1;
1843
1844 i = left_nritems - 1;
1845 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001846 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001847
Chris Mason00ec4c52007-02-24 12:47:20 -05001848 if (path->slots[0] == i)
1849 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001850
1851 if (!left->map_token) {
1852 map_extent_buffer(left, (unsigned long)item,
1853 sizeof(struct btrfs_item),
1854 &left->map_token, &left->kaddr,
1855 &left->map_start, &left->map_len,
1856 KM_USER1);
1857 }
1858
1859 this_item_size = btrfs_item_size(left, item);
1860 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001861 break;
1862 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001863 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001864 if (i == 0)
1865 break;
1866 i--;
Chris Masondb945352007-10-15 16:15:53 -04001867 }
1868 if (left->map_token) {
1869 unmap_extent_buffer(left, left->map_token, KM_USER1);
1870 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001871 }
Chris Mason5f39d392007-10-15 16:14:19 -04001872
Chris Mason925baed2008-06-25 16:01:30 -04001873 if (push_items == 0)
1874 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001875
Chris Mason34a38212007-11-07 13:31:03 -05001876 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001877 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001878
Chris Mason00ec4c52007-02-24 12:47:20 -05001879 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001880 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001881
Chris Mason5f39d392007-10-15 16:14:19 -04001882 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001883 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001884
Chris Mason00ec4c52007-02-24 12:47:20 -05001885 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001886 data_end = leaf_data_end(root, right);
1887 memmove_extent_buffer(right,
1888 btrfs_leaf_data(right) + data_end - push_space,
1889 btrfs_leaf_data(right) + data_end,
1890 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1891
Chris Mason00ec4c52007-02-24 12:47:20 -05001892 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001893 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001894 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1895 btrfs_leaf_data(left) + leaf_data_end(root, left),
1896 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001897
1898 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1899 btrfs_item_nr_offset(0),
1900 right_nritems * sizeof(struct btrfs_item));
1901
Chris Mason00ec4c52007-02-24 12:47:20 -05001902 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001903 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1904 btrfs_item_nr_offset(left_nritems - push_items),
1905 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001906
1907 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001908 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001909 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001910 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001911 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001912 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001913 if (!right->map_token) {
1914 map_extent_buffer(right, (unsigned long)item,
1915 sizeof(struct btrfs_item),
1916 &right->map_token, &right->kaddr,
1917 &right->map_start, &right->map_len,
1918 KM_USER1);
1919 }
1920 push_space -= btrfs_item_size(right, item);
1921 btrfs_set_item_offset(right, item, push_space);
1922 }
1923
1924 if (right->map_token) {
1925 unmap_extent_buffer(right, right->map_token, KM_USER1);
1926 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001927 }
Chris Mason7518a232007-03-12 12:01:18 -04001928 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001930
Chris Mason34a38212007-11-07 13:31:03 -05001931 if (left_nritems)
1932 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001933 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001934
Chris Mason5f39d392007-10-15 16:14:19 -04001935 btrfs_item_key(right, &disk_key, 0);
1936 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001937 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001938
Chris Mason00ec4c52007-02-24 12:47:20 -05001939 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001940 if (path->slots[0] >= left_nritems) {
1941 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04001942 if (btrfs_header_nritems(path->nodes[0]) == 0)
1943 clean_tree_block(trans, root, path->nodes[0]);
1944 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04001945 free_extent_buffer(path->nodes[0]);
1946 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001947 path->slots[1] += 1;
1948 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001949 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001950 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001951 }
1952 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04001953
1954out_unlock:
1955 btrfs_tree_unlock(right);
1956 free_extent_buffer(right);
1957 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05001958}
Chris Mason925baed2008-06-25 16:01:30 -04001959
Chris Mason00ec4c52007-02-24 12:47:20 -05001960/*
Chris Mason74123bd2007-02-02 11:05:29 -05001961 * push some data in the path leaf to the left, trying to free up at
1962 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1963 */
Chris Masone089f052007-03-16 16:20:31 -04001964static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001965 *root, struct btrfs_path *path, int data_size,
1966 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967{
Chris Mason5f39d392007-10-15 16:14:19 -04001968 struct btrfs_disk_key disk_key;
1969 struct extent_buffer *right = path->nodes[0];
1970 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001971 int slot;
1972 int i;
1973 int free_space;
1974 int push_space = 0;
1975 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001976 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001977 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001978 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001979 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001980 int ret = 0;
1981 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001982 u32 this_item_size;
1983 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001984
1985 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001986 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001987 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001988 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001989 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001990
Chris Mason3685f792007-10-19 09:23:27 -04001991 right_nritems = btrfs_header_nritems(right);
1992 if (right_nritems == 0) {
1993 return 1;
1994 }
1995
Chris Masona2135012008-06-25 16:01:30 -04001996 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1997
Chris Masonca7a79a2008-05-12 12:59:19 -04001998 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04001999 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002000 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002001 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002002 ret = 1;
2003 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002004 }
Chris Mason02217ed2007-03-02 16:08:05 -05002005
2006 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002007 ret = btrfs_cow_block(trans, root, left,
2008 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 if (ret) {
2010 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002011 ret = 1;
2012 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002013 }
Chris Mason3685f792007-10-19 09:23:27 -04002014
Chris Mason123abc82007-03-14 14:14:43 -04002015 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002016 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002017 ret = 1;
2018 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002019 }
2020
Chris Mason34a38212007-11-07 13:31:03 -05002021 if (empty)
2022 nr = right_nritems;
2023 else
2024 nr = right_nritems - 1;
2025
2026 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002027 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002028 if (!right->map_token) {
2029 map_extent_buffer(right, (unsigned long)item,
2030 sizeof(struct btrfs_item),
2031 &right->map_token, &right->kaddr,
2032 &right->map_start, &right->map_len,
2033 KM_USER1);
2034 }
2035
Chris Masonbe0e5c02007-01-26 15:51:26 -05002036 if (path->slots[0] == i)
2037 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002038
2039 this_item_size = btrfs_item_size(right, item);
2040 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002041 break;
Chris Masondb945352007-10-15 16:15:53 -04002042
Chris Masonbe0e5c02007-01-26 15:51:26 -05002043 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002044 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002045 }
Chris Masondb945352007-10-15 16:15:53 -04002046
2047 if (right->map_token) {
2048 unmap_extent_buffer(right, right->map_token, KM_USER1);
2049 right->map_token = NULL;
2050 }
2051
Chris Masonbe0e5c02007-01-26 15:51:26 -05002052 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002053 ret = 1;
2054 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002055 }
Chris Mason34a38212007-11-07 13:31:03 -05002056 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002057 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002058
Chris Masonbe0e5c02007-01-26 15:51:26 -05002059 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002060 copy_extent_buffer(left, right,
2061 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2062 btrfs_item_nr_offset(0),
2063 push_items * sizeof(struct btrfs_item));
2064
Chris Mason123abc82007-03-14 14:14:43 -04002065 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_item_offset_nr(right, push_items -1);
2067
2068 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002069 leaf_data_end(root, left) - push_space,
2070 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002071 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002072 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002073 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002074 BUG_ON(old_left_nritems < 0);
2075
Chris Masondb945352007-10-15 16:15:53 -04002076 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002077 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002078 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002079
Chris Mason5f39d392007-10-15 16:14:19 -04002080 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002081 if (!left->map_token) {
2082 map_extent_buffer(left, (unsigned long)item,
2083 sizeof(struct btrfs_item),
2084 &left->map_token, &left->kaddr,
2085 &left->map_start, &left->map_len,
2086 KM_USER1);
2087 }
2088
Chris Mason5f39d392007-10-15 16:14:19 -04002089 ioff = btrfs_item_offset(left, item);
2090 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002091 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002092 }
Chris Mason5f39d392007-10-15 16:14:19 -04002093 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002094 if (left->map_token) {
2095 unmap_extent_buffer(left, left->map_token, KM_USER1);
2096 left->map_token = NULL;
2097 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002098
2099 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002100 if (push_items > right_nritems) {
2101 printk("push items %d nr %u\n", push_items, right_nritems);
2102 WARN_ON(1);
2103 }
Chris Mason5f39d392007-10-15 16:14:19 -04002104
Chris Mason34a38212007-11-07 13:31:03 -05002105 if (push_items < right_nritems) {
2106 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2107 leaf_data_end(root, right);
2108 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2109 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2110 btrfs_leaf_data(right) +
2111 leaf_data_end(root, right), push_space);
2112
2113 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002114 btrfs_item_nr_offset(push_items),
2115 (btrfs_header_nritems(right) - push_items) *
2116 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002117 }
Yaneef1c492007-11-26 10:58:13 -05002118 right_nritems -= push_items;
2119 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002120 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002121 for (i = 0; i < right_nritems; i++) {
2122 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002123
2124 if (!right->map_token) {
2125 map_extent_buffer(right, (unsigned long)item,
2126 sizeof(struct btrfs_item),
2127 &right->map_token, &right->kaddr,
2128 &right->map_start, &right->map_len,
2129 KM_USER1);
2130 }
2131
2132 push_space = push_space - btrfs_item_size(right, item);
2133 btrfs_set_item_offset(right, item, push_space);
2134 }
2135 if (right->map_token) {
2136 unmap_extent_buffer(right, right->map_token, KM_USER1);
2137 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002138 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002139
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002141 if (right_nritems)
2142 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002143
Chris Mason5f39d392007-10-15 16:14:19 -04002144 btrfs_item_key(right, &disk_key, 0);
2145 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002146 if (wret)
2147 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148
2149 /* then fixup the leaf pointer in the path */
2150 if (path->slots[0] < push_items) {
2151 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002152 if (btrfs_header_nritems(path->nodes[0]) == 0)
2153 clean_tree_block(trans, root, path->nodes[0]);
2154 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002155 free_extent_buffer(path->nodes[0]);
2156 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157 path->slots[1] -= 1;
2158 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002159 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002160 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002161 path->slots[0] -= push_items;
2162 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002163 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002164 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002165out:
2166 btrfs_tree_unlock(left);
2167 free_extent_buffer(left);
2168 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169}
2170
Chris Mason74123bd2007-02-02 11:05:29 -05002171/*
2172 * split the path's leaf in two, making sure there is at least data_size
2173 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002174 *
2175 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002176 */
Chris Masone089f052007-03-16 16:20:31 -04002177static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002178 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002179 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002180{
Chris Mason7bb86312007-12-11 09:25:06 -05002181 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002182 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002183 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002184 int mid;
2185 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002186 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002187 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002188 int data_copy_size;
2189 int rt_data_off;
2190 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002191 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002192 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002193 int double_split;
2194 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002195 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002196
Chris Masoncc0c5532007-10-25 15:42:57 -04002197 if (extend)
2198 space_needed = data_size;
2199
Chris Mason7bb86312007-12-11 09:25:06 -05002200 if (root->ref_cows)
2201 root_gen = trans->transid;
2202 else
2203 root_gen = 0;
2204
Chris Mason40689472007-03-17 14:29:23 -04002205 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002206 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002207 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002208 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002209 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002210 }
2211 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002212 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002213 if (wret < 0)
2214 return wret;
2215 }
2216 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002217
Chris Mason3685f792007-10-19 09:23:27 -04002218 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002219 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002220 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002221 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002222
Chris Mason5c680ed2007-02-22 11:39:13 -05002223 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002224 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002225 if (ret)
2226 return ret;
2227 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002228again:
2229 double_split = 0;
2230 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002231 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002232 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002233 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002234
Chris Mason7bb86312007-12-11 09:25:06 -05002235 btrfs_item_key(l, &disk_key, 0);
2236
Chris Mason925baed2008-06-25 16:01:30 -04002237 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002238 root->root_key.objectid,
2239 root_gen, disk_key.objectid, 0,
2240 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002241 if (IS_ERR(right)) {
2242 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002244 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002245
Chris Mason5f39d392007-10-15 16:14:19 -04002246 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002247 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002248 btrfs_set_header_generation(right, trans->transid);
2249 btrfs_set_header_owner(right, root->root_key.objectid);
2250 btrfs_set_header_level(right, 0);
2251 write_extent_buffer(right, root->fs_info->fsid,
2252 (unsigned long)btrfs_header_fsid(right),
2253 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002254
2255 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2256 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2257 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002258 if (mid <= slot) {
2259 if (nritems == 1 ||
2260 leaf_space_used(l, mid, nritems - mid) + space_needed >
2261 BTRFS_LEAF_DATA_SIZE(root)) {
2262 if (slot >= nritems) {
2263 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002265 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002266 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002267 path->slots[1] + 1, 1);
2268 if (wret)
2269 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002270
2271 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002272 free_extent_buffer(path->nodes[0]);
2273 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002274 path->slots[0] = 0;
2275 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002276 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002277 return ret;
2278 }
2279 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002280 if (mid != nritems &&
2281 leaf_space_used(l, mid, nritems - mid) +
2282 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2283 double_split = 1;
2284 }
Chris Masond4dbff92007-04-04 14:08:15 -04002285 }
2286 } else {
2287 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2288 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002289 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002290 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002291 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002292 wret = insert_ptr(trans, root, path,
2293 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002294 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002295 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002296 if (wret)
2297 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002298 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002299 free_extent_buffer(path->nodes[0]);
2300 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002301 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002302 if (path->slots[1] == 0) {
2303 wret = fixup_low_keys(trans, root,
2304 path, &disk_key, 1);
2305 if (wret)
2306 ret = wret;
2307 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002308 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002309 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002310 } else if (extend && slot == 0) {
2311 mid = 1;
2312 } else {
2313 mid = slot;
2314 if (mid != nritems &&
2315 leaf_space_used(l, mid, nritems - mid) +
2316 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2317 double_split = 1;
2318 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002319 }
Chris Masond4dbff92007-04-04 14:08:15 -04002320 }
2321 }
Chris Mason5f39d392007-10-15 16:14:19 -04002322 nritems = nritems - mid;
2323 btrfs_set_header_nritems(right, nritems);
2324 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2325
2326 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2327 btrfs_item_nr_offset(mid),
2328 nritems * sizeof(struct btrfs_item));
2329
2330 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002331 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2332 data_copy_size, btrfs_leaf_data(l) +
2333 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002334
Chris Mason5f39d392007-10-15 16:14:19 -04002335 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2336 btrfs_item_end_nr(l, mid);
2337
2338 for (i = 0; i < nritems; i++) {
2339 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002340 u32 ioff;
2341
2342 if (!right->map_token) {
2343 map_extent_buffer(right, (unsigned long)item,
2344 sizeof(struct btrfs_item),
2345 &right->map_token, &right->kaddr,
2346 &right->map_start, &right->map_len,
2347 KM_USER1);
2348 }
2349
2350 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002351 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002352 }
Chris Mason74123bd2007-02-02 11:05:29 -05002353
Chris Masondb945352007-10-15 16:15:53 -04002354 if (right->map_token) {
2355 unmap_extent_buffer(right, right->map_token, KM_USER1);
2356 right->map_token = NULL;
2357 }
2358
Chris Mason5f39d392007-10-15 16:14:19 -04002359 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002360 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002361 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002362 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2363 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002364 if (wret)
2365 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002366
2367 btrfs_mark_buffer_dirty(right);
2368 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002369 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Masonbe0e5c02007-01-26 15:51:26 -05002371 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002372 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002373 free_extent_buffer(path->nodes[0]);
2374 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002375 path->slots[0] -= mid;
2376 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002377 } else {
2378 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002379 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002380 }
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Masoneb60cea2007-02-02 09:18:22 -05002382 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002383
Chris Masoncc0c5532007-10-25 15:42:57 -04002384 if (double_split) {
2385 BUG_ON(num_doubles != 0);
2386 num_doubles++;
2387 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002388 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002389 return ret;
2390}
2391
Chris Masonb18c6682007-04-17 13:26:50 -04002392int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2393 struct btrfs_root *root,
2394 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002395 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002396{
2397 int ret = 0;
2398 int slot;
2399 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002400 struct extent_buffer *leaf;
2401 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002402 u32 nritems;
2403 unsigned int data_end;
2404 unsigned int old_data_start;
2405 unsigned int old_size;
2406 unsigned int size_diff;
2407 int i;
2408
2409 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002410 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002411 slot = path->slots[0];
2412
2413 old_size = btrfs_item_size_nr(leaf, slot);
2414 if (old_size == new_size)
2415 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002416
Chris Mason5f39d392007-10-15 16:14:19 -04002417 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002418 data_end = leaf_data_end(root, leaf);
2419
Chris Mason5f39d392007-10-15 16:14:19 -04002420 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002421
Chris Masonb18c6682007-04-17 13:26:50 -04002422 size_diff = old_size - new_size;
2423
2424 BUG_ON(slot < 0);
2425 BUG_ON(slot >= nritems);
2426
2427 /*
2428 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2429 */
2430 /* first correct the data pointers */
2431 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002432 u32 ioff;
2433 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002434
2435 if (!leaf->map_token) {
2436 map_extent_buffer(leaf, (unsigned long)item,
2437 sizeof(struct btrfs_item),
2438 &leaf->map_token, &leaf->kaddr,
2439 &leaf->map_start, &leaf->map_len,
2440 KM_USER1);
2441 }
2442
Chris Mason5f39d392007-10-15 16:14:19 -04002443 ioff = btrfs_item_offset(leaf, item);
2444 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002445 }
Chris Masondb945352007-10-15 16:15:53 -04002446
2447 if (leaf->map_token) {
2448 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2449 leaf->map_token = NULL;
2450 }
2451
Chris Masonb18c6682007-04-17 13:26:50 -04002452 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002453 if (from_end) {
2454 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2455 data_end + size_diff, btrfs_leaf_data(leaf) +
2456 data_end, old_data_start + new_size - data_end);
2457 } else {
2458 struct btrfs_disk_key disk_key;
2459 u64 offset;
2460
2461 btrfs_item_key(leaf, &disk_key, slot);
2462
2463 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2464 unsigned long ptr;
2465 struct btrfs_file_extent_item *fi;
2466
2467 fi = btrfs_item_ptr(leaf, slot,
2468 struct btrfs_file_extent_item);
2469 fi = (struct btrfs_file_extent_item *)(
2470 (unsigned long)fi - size_diff);
2471
2472 if (btrfs_file_extent_type(leaf, fi) ==
2473 BTRFS_FILE_EXTENT_INLINE) {
2474 ptr = btrfs_item_ptr_offset(leaf, slot);
2475 memmove_extent_buffer(leaf, ptr,
2476 (unsigned long)fi,
2477 offsetof(struct btrfs_file_extent_item,
2478 disk_bytenr));
2479 }
2480 }
2481
2482 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2483 data_end + size_diff, btrfs_leaf_data(leaf) +
2484 data_end, old_data_start - data_end);
2485
2486 offset = btrfs_disk_key_offset(&disk_key);
2487 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2488 btrfs_set_item_key(leaf, &disk_key, slot);
2489 if (slot == 0)
2490 fixup_low_keys(trans, root, path, &disk_key, 1);
2491 }
Chris Mason5f39d392007-10-15 16:14:19 -04002492
2493 item = btrfs_item_nr(leaf, slot);
2494 btrfs_set_item_size(leaf, item, new_size);
2495 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002496
2497 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002498 if (btrfs_leaf_free_space(root, leaf) < 0) {
2499 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002500 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002501 }
Chris Masonb18c6682007-04-17 13:26:50 -04002502 return ret;
2503}
2504
Chris Mason5f39d392007-10-15 16:14:19 -04002505int btrfs_extend_item(struct btrfs_trans_handle *trans,
2506 struct btrfs_root *root, struct btrfs_path *path,
2507 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002508{
2509 int ret = 0;
2510 int slot;
2511 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002512 struct extent_buffer *leaf;
2513 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002514 u32 nritems;
2515 unsigned int data_end;
2516 unsigned int old_data;
2517 unsigned int old_size;
2518 int i;
2519
2520 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002521 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002522
Chris Mason5f39d392007-10-15 16:14:19 -04002523 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002524 data_end = leaf_data_end(root, leaf);
2525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2527 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002528 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002529 }
Chris Mason6567e832007-04-16 09:22:45 -04002530 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002531 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002532
2533 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002534 if (slot >= nritems) {
2535 btrfs_print_leaf(root, leaf);
2536 printk("slot %d too large, nritems %d\n", slot, nritems);
2537 BUG_ON(1);
2538 }
Chris Mason6567e832007-04-16 09:22:45 -04002539
2540 /*
2541 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2542 */
2543 /* first correct the data pointers */
2544 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002545 u32 ioff;
2546 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002547
2548 if (!leaf->map_token) {
2549 map_extent_buffer(leaf, (unsigned long)item,
2550 sizeof(struct btrfs_item),
2551 &leaf->map_token, &leaf->kaddr,
2552 &leaf->map_start, &leaf->map_len,
2553 KM_USER1);
2554 }
Chris Mason5f39d392007-10-15 16:14:19 -04002555 ioff = btrfs_item_offset(leaf, item);
2556 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002557 }
Chris Mason5f39d392007-10-15 16:14:19 -04002558
Chris Masondb945352007-10-15 16:15:53 -04002559 if (leaf->map_token) {
2560 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2561 leaf->map_token = NULL;
2562 }
2563
Chris Mason6567e832007-04-16 09:22:45 -04002564 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002565 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002566 data_end - data_size, btrfs_leaf_data(leaf) +
2567 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002568
Chris Mason6567e832007-04-16 09:22:45 -04002569 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002570 old_size = btrfs_item_size_nr(leaf, slot);
2571 item = btrfs_item_nr(leaf, slot);
2572 btrfs_set_item_size(leaf, item, old_size + data_size);
2573 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002574
2575 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002576 if (btrfs_leaf_free_space(root, leaf) < 0) {
2577 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002578 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002579 }
Chris Mason6567e832007-04-16 09:22:45 -04002580 return ret;
2581}
2582
Chris Mason74123bd2007-02-02 11:05:29 -05002583/*
2584 * Given a key and some data, insert an item into the tree.
2585 * This does all the path init required, making room in the tree if needed.
2586 */
Chris Mason9c583092008-01-29 15:15:18 -05002587int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002588 struct btrfs_root *root,
2589 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002590 struct btrfs_key *cpu_key, u32 *data_size,
2591 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002592{
Chris Mason5f39d392007-10-15 16:14:19 -04002593 struct extent_buffer *leaf;
2594 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002595 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002597 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002598 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002599 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002600 u32 total_size = 0;
2601 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002603 struct btrfs_disk_key disk_key;
2604
Chris Mason9c583092008-01-29 15:15:18 -05002605 for (i = 0; i < nr; i++) {
2606 total_data += data_size[i];
2607 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608
Chris Mason9c583092008-01-29 15:15:18 -05002609 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2610 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002611 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002612 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002613 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002614 if (ret < 0)
2615 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616
Chris Mason62e27492007-03-15 12:56:47 -04002617 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002618 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002619
Chris Mason5f39d392007-10-15 16:14:19 -04002620 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002621 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002622
Chris Mason123abc82007-03-14 14:14:43 -04002623 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002624 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002625 btrfs_print_leaf(root, leaf);
2626 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002627 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002628 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002629 }
Chris Mason5f39d392007-10-15 16:14:19 -04002630
Chris Mason62e27492007-03-15 12:56:47 -04002631 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002632 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002633
Chris Masonbe0e5c02007-01-26 15:51:26 -05002634 if (slot != nritems) {
2635 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002636 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002637
Chris Mason5f39d392007-10-15 16:14:19 -04002638 if (old_data < data_end) {
2639 btrfs_print_leaf(root, leaf);
2640 printk("slot %d old_data %d data_end %d\n",
2641 slot, old_data, data_end);
2642 BUG_ON(1);
2643 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002644 /*
2645 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2646 */
2647 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002648 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002649 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002650 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002651
Chris Mason5f39d392007-10-15 16:14:19 -04002652 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002653 if (!leaf->map_token) {
2654 map_extent_buffer(leaf, (unsigned long)item,
2655 sizeof(struct btrfs_item),
2656 &leaf->map_token, &leaf->kaddr,
2657 &leaf->map_start, &leaf->map_len,
2658 KM_USER1);
2659 }
2660
Chris Mason5f39d392007-10-15 16:14:19 -04002661 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002662 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002663 }
Chris Masondb945352007-10-15 16:15:53 -04002664 if (leaf->map_token) {
2665 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2666 leaf->map_token = NULL;
2667 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002668
2669 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002670 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002672 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002673
2674 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002675 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002676 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002677 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002678 data_end = old_data;
2679 }
Chris Mason5f39d392007-10-15 16:14:19 -04002680
Chris Mason62e27492007-03-15 12:56:47 -04002681 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002682 for (i = 0; i < nr; i++) {
2683 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2684 btrfs_set_item_key(leaf, &disk_key, slot + i);
2685 item = btrfs_item_nr(leaf, slot + i);
2686 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2687 data_end -= data_size[i];
2688 btrfs_set_item_size(leaf, item, data_size[i]);
2689 }
2690 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002691 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002692
2693 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002694 if (slot == 0) {
2695 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002696 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002697 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002698
Chris Mason5f39d392007-10-15 16:14:19 -04002699 if (btrfs_leaf_free_space(root, leaf) < 0) {
2700 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002701 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002702 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002703out:
Chris Mason62e27492007-03-15 12:56:47 -04002704 return ret;
2705}
2706
2707/*
2708 * Given a key and some data, insert an item into the tree.
2709 * This does all the path init required, making room in the tree if needed.
2710 */
Chris Masone089f052007-03-16 16:20:31 -04002711int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2712 *root, struct btrfs_key *cpu_key, void *data, u32
2713 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002714{
2715 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002716 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002717 struct extent_buffer *leaf;
2718 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002719
Chris Mason2c90e5d2007-04-02 10:50:19 -04002720 path = btrfs_alloc_path();
2721 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002722 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002723 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002724 leaf = path->nodes[0];
2725 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2726 write_extent_buffer(leaf, data, ptr, data_size);
2727 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002728 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002729 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731}
2732
Chris Mason74123bd2007-02-02 11:05:29 -05002733/*
Chris Mason5de08d72007-02-24 06:24:44 -05002734 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002735 *
2736 * If the delete empties a node, the node is removed from the tree,
2737 * continuing all the way the root if required. The root is converted into
2738 * a leaf if all the nodes are emptied.
2739 */
Chris Masone089f052007-03-16 16:20:31 -04002740static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2741 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002742{
Chris Mason5f39d392007-10-15 16:14:19 -04002743 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002744 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002745 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002746 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002747
Chris Mason5f39d392007-10-15 16:14:19 -04002748 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002749 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002750 memmove_extent_buffer(parent,
2751 btrfs_node_key_ptr_offset(slot),
2752 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002753 sizeof(struct btrfs_key_ptr) *
2754 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002755 }
Chris Mason7518a232007-03-12 12:01:18 -04002756 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002758 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002759 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002760 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002761 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002762 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002763 struct btrfs_disk_key disk_key;
2764
2765 btrfs_node_key(parent, &disk_key, 0);
2766 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002767 if (wret)
2768 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002769 }
Chris Masond6025572007-03-30 14:27:56 -04002770 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002771 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002772}
2773
Chris Mason74123bd2007-02-02 11:05:29 -05002774/*
2775 * delete the item at the leaf level in path. If that empties
2776 * the leaf, remove it from the tree
2777 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002778int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2779 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002780{
Chris Mason5f39d392007-10-15 16:14:19 -04002781 struct extent_buffer *leaf;
2782 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002783 int last_off;
2784 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002785 int ret = 0;
2786 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002787 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002788 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002789
Chris Mason5f39d392007-10-15 16:14:19 -04002790 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002791 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2792
2793 for (i = 0; i < nr; i++)
2794 dsize += btrfs_item_size_nr(leaf, slot + i);
2795
Chris Mason5f39d392007-10-15 16:14:19 -04002796 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002797
Chris Mason85e21ba2008-01-29 15:11:36 -05002798 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002799 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002800 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002801
2802 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002803 data_end + dsize,
2804 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002805 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002806
Chris Mason85e21ba2008-01-29 15:11:36 -05002807 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002808 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002809
Chris Mason5f39d392007-10-15 16:14:19 -04002810 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002811 if (!leaf->map_token) {
2812 map_extent_buffer(leaf, (unsigned long)item,
2813 sizeof(struct btrfs_item),
2814 &leaf->map_token, &leaf->kaddr,
2815 &leaf->map_start, &leaf->map_len,
2816 KM_USER1);
2817 }
Chris Mason5f39d392007-10-15 16:14:19 -04002818 ioff = btrfs_item_offset(leaf, item);
2819 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002820 }
Chris Masondb945352007-10-15 16:15:53 -04002821
2822 if (leaf->map_token) {
2823 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2824 leaf->map_token = NULL;
2825 }
2826
Chris Mason5f39d392007-10-15 16:14:19 -04002827 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002828 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002829 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002830 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002831 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002832 btrfs_set_header_nritems(leaf, nritems - nr);
2833 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002834
Chris Mason74123bd2007-02-02 11:05:29 -05002835 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002836 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002837 if (leaf == root->node) {
2838 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002839 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002840 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002841 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002842 if (wret)
2843 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002844 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002845 leaf->start, leaf->len,
2846 btrfs_header_owner(path->nodes[1]),
2847 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002848 if (wret)
2849 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002850 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002851 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002852 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002853 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002854 struct btrfs_disk_key disk_key;
2855
2856 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002857 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002858 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002859 if (wret)
2860 ret = wret;
2861 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002862
Chris Mason74123bd2007-02-02 11:05:29 -05002863 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002864 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002865 /* push_leaf_left fixes the path.
2866 * make sure the path still points to our leaf
2867 * for possible call to del_ptr below
2868 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002869 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002870 extent_buffer_get(leaf);
2871
Chris Mason85e21ba2008-01-29 15:11:36 -05002872 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002873 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002874 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002875
2876 if (path->nodes[0] == leaf &&
2877 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002878 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002879 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002880 ret = wret;
2881 }
Chris Mason5f39d392007-10-15 16:14:19 -04002882
2883 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002884 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002885 u64 bytenr = leaf->start;
2886 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002887
Chris Mason7bb86312007-12-11 09:25:06 -05002888 root_gen = btrfs_header_generation(
2889 path->nodes[1]);
2890
Chris Masone089f052007-03-16 16:20:31 -04002891 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002892 if (wret)
2893 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002894
2895 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002896 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002897 blocksize,
2898 btrfs_header_owner(path->nodes[1]),
2899 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002900 if (wret)
2901 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002902 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002903 /* if we're still in the path, make sure
2904 * we're dirty. Otherwise, one of the
2905 * push_leaf functions must have already
2906 * dirtied this buffer
2907 */
2908 if (path->nodes[0] == leaf)
2909 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002910 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002911 }
Chris Masond5719762007-03-23 10:01:08 -04002912 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002913 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002914 }
2915 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002916 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002917}
2918
Chris Mason97571fd2007-02-24 13:39:08 -05002919/*
Chris Mason925baed2008-06-25 16:01:30 -04002920 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05002921 * returns 0 if it found something or 1 if there are no lesser leaves.
2922 * returns < 0 on io errors.
2923 */
2924int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2925{
Chris Mason925baed2008-06-25 16:01:30 -04002926 struct btrfs_key key;
2927 struct btrfs_disk_key found_key;
2928 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05002929
Chris Mason925baed2008-06-25 16:01:30 -04002930 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05002931
Chris Mason925baed2008-06-25 16:01:30 -04002932 if (key.offset > 0)
2933 key.offset--;
2934 else if (key.type > 0)
2935 key.type--;
2936 else if (key.objectid > 0)
2937 key.objectid--;
2938 else
2939 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002940
Chris Mason925baed2008-06-25 16:01:30 -04002941 btrfs_release_path(root, path);
2942 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2943 if (ret < 0)
2944 return ret;
2945 btrfs_item_key(path->nodes[0], &found_key, 0);
2946 ret = comp_keys(&found_key, &key);
2947 if (ret < 0)
2948 return 0;
2949 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002950}
2951
2952/*
Chris Mason925baed2008-06-25 16:01:30 -04002953 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05002954 * returns 0 if it found something or 1 if there are no greater leaves.
2955 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002956 */
Chris Mason234b63a2007-03-13 10:46:10 -04002957int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002958{
2959 int slot;
2960 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002961 struct extent_buffer *c;
2962 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04002963 struct btrfs_key key;
2964 u32 nritems;
2965 int ret;
2966
2967 nritems = btrfs_header_nritems(path->nodes[0]);
2968 if (nritems == 0) {
2969 return 1;
2970 }
2971
2972 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
2973
Chris Mason925baed2008-06-25 16:01:30 -04002974 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04002975 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04002976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2977 path->keep_locks = 0;
2978
2979 if (ret < 0)
2980 return ret;
2981
Chris Masona2135012008-06-25 16:01:30 -04002982 nritems = btrfs_header_nritems(path->nodes[0]);
2983 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002984 goto done;
2985 }
Chris Masond97e63b2007-02-20 16:40:44 -05002986
Chris Mason234b63a2007-03-13 10:46:10 -04002987 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002988 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002989 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002990
Chris Masond97e63b2007-02-20 16:40:44 -05002991 slot = path->slots[level] + 1;
2992 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002993 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002994 level++;
Chris Mason925baed2008-06-25 16:01:30 -04002995 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05002996 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04002997 }
Chris Masond97e63b2007-02-20 16:40:44 -05002998 continue;
2999 }
Chris Mason5f39d392007-10-15 16:14:19 -04003000
Chris Mason925baed2008-06-25 16:01:30 -04003001 if (next) {
3002 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003003 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003004 }
Chris Mason5f39d392007-10-15 16:14:19 -04003005
Chris Mason925baed2008-06-25 16:01:30 -04003006 if (level == 1 && path->locks[1] && path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003007 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003008
Chris Masonca7a79a2008-05-12 12:59:19 -04003009 next = read_node_slot(root, c, slot);
Chris Masona2135012008-06-25 16:01:30 -04003010 if (!path->skip_locking) {
3011 if (!btrfs_tree_locked(c)) {
3012 int i;
3013 WARN_ON(1);
3014printk("path %p no lock on level %d\n", path, level);
3015for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3016printk("path %p level %d slot %d nritems %d\n", path, i, path->slots[i], btrfs_header_nritems(path->nodes[i]));
3017}
3018 }
Chris Mason925baed2008-06-25 16:01:30 -04003019 btrfs_tree_lock(next);
Chris Masona2135012008-06-25 16:01:30 -04003020 }
Chris Masond97e63b2007-02-20 16:40:44 -05003021 break;
3022 }
3023 path->slots[level] = slot;
3024 while(1) {
3025 level--;
3026 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003027 if (path->locks[level])
3028 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003029 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003030 path->nodes[level] = next;
3031 path->slots[level] = 0;
Chris Mason925baed2008-06-25 16:01:30 -04003032 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003033 if (!level)
3034 break;
Chris Mason925baed2008-06-25 16:01:30 -04003035 if (level == 1 && path->locks[1] && path->reada)
3036 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003037 next = read_node_slot(root, next, 0);
Chris Masona2135012008-06-25 16:01:30 -04003038 if (!path->skip_locking) {
3039 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
Chris Mason925baed2008-06-25 16:01:30 -04003040 btrfs_tree_lock(next);
Chris Masona2135012008-06-25 16:01:30 -04003041 }
Chris Masond97e63b2007-02-20 16:40:44 -05003042 }
Chris Mason925baed2008-06-25 16:01:30 -04003043done:
3044 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003045 return 0;
3046}
Chris Mason0b86a832008-03-24 15:01:56 -04003047
3048int btrfs_previous_item(struct btrfs_root *root,
3049 struct btrfs_path *path, u64 min_objectid,
3050 int type)
3051{
3052 struct btrfs_key found_key;
3053 struct extent_buffer *leaf;
3054 int ret;
3055
3056 while(1) {
3057 if (path->slots[0] == 0) {
3058 ret = btrfs_prev_leaf(root, path);
3059 if (ret != 0)
3060 return ret;
3061 } else {
3062 path->slots[0]--;
3063 }
3064 leaf = path->nodes[0];
3065 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3066 if (found_key.type == type)
3067 return 0;
3068 }
3069 return 1;
3070}
3071