blob: 1b756fae279932f79946972d368c3bde5c8a3421 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
57void btrfs_free_path(struct btrfs_path *p)
58{
Chris Masondf24a2b2007-04-04 09:36:31 -040059 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040060 kmem_cache_free(btrfs_path_cachep, p);
61}
62
Chris Mason234b63a2007-03-13 10:46:10 -040063void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050064{
65 int i;
Chris Masona2135012008-06-25 16:01:30 -040066 int keep = p->keep_locks;
67
Chris Mason234b63a2007-03-13 10:46:10 -040068 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050069 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040070 continue;
71 if (p->locks[i]) {
72 btrfs_tree_unlock(p->nodes[i]);
73 p->locks[i] = 0;
74 }
Chris Mason5f39d392007-10-15 16:14:19 -040075 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050076 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050077 memset(p, 0, sizeof(*p));
Chris Masona2135012008-06-25 16:01:30 -040078 p->keep_locks = keep;
Chris Masoneb60cea2007-02-02 09:18:22 -050079}
80
Chris Mason925baed2008-06-25 16:01:30 -040081struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
82{
83 struct extent_buffer *eb;
84 spin_lock(&root->node_lock);
85 eb = root->node;
86 extent_buffer_get(eb);
87 spin_unlock(&root->node_lock);
88 return eb;
89}
90
91struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
92{
93 struct extent_buffer *eb;
94
95 while(1) {
96 eb = btrfs_root_node(root);
97 btrfs_tree_lock(eb);
98
99 spin_lock(&root->node_lock);
100 if (eb == root->node) {
101 spin_unlock(&root->node_lock);
102 break;
103 }
104 spin_unlock(&root->node_lock);
105
106 btrfs_tree_unlock(eb);
107 free_extent_buffer(eb);
108 }
109 return eb;
110}
111
Chris Mason0b86a832008-03-24 15:01:56 -0400112static void add_root_to_dirty_list(struct btrfs_root *root)
113{
114 if (root->track_dirty && list_empty(&root->dirty_list)) {
115 list_add(&root->dirty_list,
116 &root->fs_info->dirty_cowonly_roots);
117 }
118}
119
Chris Masonbe20aa92007-12-17 20:14:01 -0500120int btrfs_copy_root(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct extent_buffer *buf,
123 struct extent_buffer **cow_ret, u64 new_root_objectid)
124{
125 struct extent_buffer *cow;
126 u32 nritems;
127 int ret = 0;
128 int level;
129 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -0500130 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500131
Chris Mason4aec2b52007-12-18 16:25:45 -0500132 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
133 if (!new_root)
134 return -ENOMEM;
135
136 memcpy(new_root, root, sizeof(*new_root));
137 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500138
139 WARN_ON(root->ref_cows && trans->transid !=
140 root->fs_info->running_transaction->transid);
141 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
142
143 level = btrfs_header_level(buf);
144 nritems = btrfs_header_nritems(buf);
145 if (nritems) {
146 if (level == 0)
147 btrfs_item_key_to_cpu(buf, &first_key, 0);
148 else
149 btrfs_node_key_to_cpu(buf, &first_key, 0);
150 } else {
151 first_key.objectid = 0;
152 }
Chris Mason925baed2008-06-25 16:01:30 -0400153 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500154 new_root_objectid,
155 trans->transid, first_key.objectid,
156 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500157 if (IS_ERR(cow)) {
158 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500159 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500161
162 copy_extent_buffer(cow, buf, 0, 0, cow->len);
163 btrfs_set_header_bytenr(cow, cow->start);
164 btrfs_set_header_generation(cow, trans->transid);
165 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400166 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500167
168 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500169 ret = btrfs_inc_ref(trans, new_root, buf);
170 kfree(new_root);
171
Chris Masonbe20aa92007-12-17 20:14:01 -0500172 if (ret)
173 return ret;
174
175 btrfs_mark_buffer_dirty(cow);
176 *cow_ret = cow;
177 return 0;
178}
179
180int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400181 struct btrfs_root *root,
182 struct extent_buffer *buf,
183 struct extent_buffer *parent, int parent_slot,
184 struct extent_buffer **cow_ret,
185 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400186{
Chris Mason7bb86312007-12-11 09:25:06 -0500187 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400188 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500189 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400190 int ret = 0;
191 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500192 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400193 int unlock_orig = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500194 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400195
Chris Mason925baed2008-06-25 16:01:30 -0400196 if (*cow_ret == buf)
197 unlock_orig = 1;
198
199 WARN_ON(!btrfs_tree_locked(buf));
200
Chris Mason7bb86312007-12-11 09:25:06 -0500201 if (root->ref_cows) {
202 root_gen = trans->transid;
203 } else {
204 root_gen = 0;
205 }
Chris Mason7bb86312007-12-11 09:25:06 -0500206 WARN_ON(root->ref_cows && trans->transid !=
207 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400208 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400209
Chris Mason7bb86312007-12-11 09:25:06 -0500210 level = btrfs_header_level(buf);
211 nritems = btrfs_header_nritems(buf);
212 if (nritems) {
213 if (level == 0)
214 btrfs_item_key_to_cpu(buf, &first_key, 0);
215 else
216 btrfs_node_key_to_cpu(buf, &first_key, 0);
217 } else {
218 first_key.objectid = 0;
219 }
Chris Mason925baed2008-06-25 16:01:30 -0400220 cow = btrfs_alloc_free_block(trans, root, buf->len,
Chris Mason7bb86312007-12-11 09:25:06 -0500221 root->root_key.objectid,
222 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400223 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400224 if (IS_ERR(cow))
225 return PTR_ERR(cow);
226
Chris Mason5f39d392007-10-15 16:14:19 -0400227 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400228 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400229 btrfs_set_header_generation(cow, trans->transid);
230 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400231 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400232
Chris Mason5f39d392007-10-15 16:14:19 -0400233 WARN_ON(btrfs_header_generation(buf) > trans->transid);
234 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400235 different_trans = 1;
236 ret = btrfs_inc_ref(trans, root, buf);
237 if (ret)
238 return ret;
239 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400240 clean_tree_block(trans, root, buf);
241 }
242
243 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400244 WARN_ON(parent && parent != buf);
Chris Mason7bb86312007-12-11 09:25:06 -0500245 root_gen = btrfs_header_generation(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400246
247 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400248 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400249 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400250 spin_unlock(&root->node_lock);
251
Chris Mason6702ed42007-08-07 16:15:09 -0400252 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400253 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500254 buf->len, root->root_key.objectid,
255 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400256 }
Chris Mason5f39d392007-10-15 16:14:19 -0400257 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400258 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400259 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500260 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400261 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400262 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500263 WARN_ON(trans->transid == 0);
264 btrfs_set_node_ptr_generation(parent, parent_slot,
265 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400266 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400267 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500268 btrfs_free_extent(trans, root, buf->start, buf->len,
269 btrfs_header_owner(parent), root_gen,
270 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400271 }
Chris Mason925baed2008-06-25 16:01:30 -0400272 if (unlock_orig)
273 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400274 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400275 btrfs_mark_buffer_dirty(cow);
276 *cow_ret = cow;
277 return 0;
278}
279
Chris Mason5f39d392007-10-15 16:14:19 -0400280int btrfs_cow_block(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root, struct extent_buffer *buf,
282 struct extent_buffer *parent, int parent_slot,
283 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500284{
Chris Mason6702ed42007-08-07 16:15:09 -0400285 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500286 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400287 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500288
Chris Masonccd467d2007-06-28 15:57:36 -0400289 if (trans->transaction != root->fs_info->running_transaction) {
290 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
291 root->fs_info->running_transaction->transid);
292 WARN_ON(1);
293 }
294 if (trans->transid != root->fs_info->generation) {
295 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
296 root->fs_info->generation);
297 WARN_ON(1);
298 }
Chris Masondc17ff82008-01-08 15:46:30 -0500299
300 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400301 spin_lock(&root->fs_info->hash_lock);
302 if (header_trans == trans->transid &&
303 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500304 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400305 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500306 return 0;
307 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400308 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400309 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400310 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400311 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400312 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400313}
314
Chris Mason6b800532007-10-15 16:17:34 -0400315static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400316{
Chris Mason6b800532007-10-15 16:17:34 -0400317 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400318 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400319 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400320 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500321 return 0;
322}
323
Chris Mason081e9572007-11-06 10:26:24 -0500324/*
325 * compare two keys in a memcmp fashion
326 */
327static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
328{
329 struct btrfs_key k1;
330
331 btrfs_disk_key_to_cpu(&k1, disk);
332
333 if (k1.objectid > k2->objectid)
334 return 1;
335 if (k1.objectid < k2->objectid)
336 return -1;
337 if (k1.type > k2->type)
338 return 1;
339 if (k1.type < k2->type)
340 return -1;
341 if (k1.offset > k2->offset)
342 return 1;
343 if (k1.offset < k2->offset)
344 return -1;
345 return 0;
346}
347
348
Chris Mason6702ed42007-08-07 16:15:09 -0400349int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400350 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400351 int start_slot, int cache_only, u64 *last_ret,
352 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400353{
Chris Mason6b800532007-10-15 16:17:34 -0400354 struct extent_buffer *cur;
355 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400356 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400357 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400358 u64 search_start = *last_ret;
359 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400360 u64 other;
361 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400362 int end_slot;
363 int i;
364 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400365 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400366 int uptodate;
367 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500368 int progress_passed = 0;
369 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400370
Chris Mason925baed2008-06-25 16:01:30 -0400371 /* FIXME this code needs locking */
372 return 0;
373
Chris Mason5708b952007-10-25 15:43:18 -0400374 parent_level = btrfs_header_level(parent);
375 if (cache_only && parent_level != 1)
376 return 0;
377
Chris Mason6702ed42007-08-07 16:15:09 -0400378 if (trans->transaction != root->fs_info->running_transaction) {
379 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
380 root->fs_info->running_transaction->transid);
381 WARN_ON(1);
382 }
383 if (trans->transid != root->fs_info->generation) {
384 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
385 root->fs_info->generation);
386 WARN_ON(1);
387 }
Chris Mason86479a02007-09-10 19:58:16 -0400388
Chris Mason6b800532007-10-15 16:17:34 -0400389 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400390 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400391 end_slot = parent_nritems;
392
393 if (parent_nritems == 1)
394 return 0;
395
396 for (i = start_slot; i < end_slot; i++) {
397 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400398
Chris Mason5708b952007-10-25 15:43:18 -0400399 if (!parent->map_token) {
400 map_extent_buffer(parent,
401 btrfs_node_key_ptr_offset(i),
402 sizeof(struct btrfs_key_ptr),
403 &parent->map_token, &parent->kaddr,
404 &parent->map_start, &parent->map_len,
405 KM_USER1);
406 }
Chris Mason081e9572007-11-06 10:26:24 -0500407 btrfs_node_key(parent, &disk_key, i);
408 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
409 continue;
410
411 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400412 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400413 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400414 if (last_block == 0)
415 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400416
Chris Mason6702ed42007-08-07 16:15:09 -0400417 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400418 other = btrfs_node_blockptr(parent, i - 1);
419 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400420 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400421 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400422 other = btrfs_node_blockptr(parent, i + 1);
423 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400424 }
Chris Masone9d0b132007-08-10 14:06:19 -0400425 if (close) {
426 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400427 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400428 }
Chris Mason5708b952007-10-25 15:43:18 -0400429 if (parent->map_token) {
430 unmap_extent_buffer(parent, parent->map_token,
431 KM_USER1);
432 parent->map_token = NULL;
433 }
Chris Mason6702ed42007-08-07 16:15:09 -0400434
Chris Mason6b800532007-10-15 16:17:34 -0400435 cur = btrfs_find_tree_block(root, blocknr, blocksize);
436 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400437 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400438 else
439 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400440 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400441 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400442 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400443 continue;
444 }
Chris Mason6b800532007-10-15 16:17:34 -0400445 if (!cur) {
446 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400447 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400448 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400449 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400450 }
Chris Mason6702ed42007-08-07 16:15:09 -0400451 }
Chris Masone9d0b132007-08-10 14:06:19 -0400452 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400453 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400454
Chris Mason6b800532007-10-15 16:17:34 -0400455 err = __btrfs_cow_block(trans, root, cur, parent, i,
456 &tmp, search_start,
457 min(16 * blocksize,
458 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400459 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400460 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400461 break;
Yan252c38f2007-08-29 09:11:44 -0400462 }
Chris Mason6b800532007-10-15 16:17:34 -0400463 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400464 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400465 *last_ret = search_start;
466 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400467 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400468 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400469 }
Chris Mason5708b952007-10-25 15:43:18 -0400470 if (parent->map_token) {
471 unmap_extent_buffer(parent, parent->map_token,
472 KM_USER1);
473 parent->map_token = NULL;
474 }
Chris Mason6702ed42007-08-07 16:15:09 -0400475 return err;
476}
477
Chris Mason74123bd2007-02-02 11:05:29 -0500478/*
479 * The leaf data grows from end-to-front in the node.
480 * this returns the address of the start of the last item,
481 * which is the stop of the leaf data stack
482 */
Chris Mason123abc82007-03-14 14:14:43 -0400483static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400484 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500485{
Chris Mason5f39d392007-10-15 16:14:19 -0400486 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500487 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400488 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400489 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500490}
491
Chris Mason123abc82007-03-14 14:14:43 -0400492static int check_node(struct btrfs_root *root, struct btrfs_path *path,
493 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500494{
Chris Mason5f39d392007-10-15 16:14:19 -0400495 struct extent_buffer *parent = NULL;
496 struct extent_buffer *node = path->nodes[level];
497 struct btrfs_disk_key parent_key;
498 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500499 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400500 int slot;
501 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400502 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500503
504 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400505 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400506
Chris Mason8d7be552007-05-10 11:24:42 -0400507 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400508 BUG_ON(nritems == 0);
509 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400510 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400511 btrfs_node_key(parent, &parent_key, parent_slot);
512 btrfs_node_key(node, &node_key, 0);
513 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400514 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400515 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400516 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500517 }
Chris Mason123abc82007-03-14 14:14:43 -0400518 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400519 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400520 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
521 btrfs_node_key(node, &node_key, slot);
522 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400523 }
524 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400525 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
526 btrfs_node_key(node, &node_key, slot);
527 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500528 }
529 return 0;
530}
531
Chris Mason123abc82007-03-14 14:14:43 -0400532static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
533 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500534{
Chris Mason5f39d392007-10-15 16:14:19 -0400535 struct extent_buffer *leaf = path->nodes[level];
536 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500537 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400538 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400539 struct btrfs_disk_key parent_key;
540 struct btrfs_disk_key leaf_key;
541 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400542
Chris Mason5f39d392007-10-15 16:14:19 -0400543 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500544
545 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400546 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400547
548 if (nritems == 0)
549 return 0;
550
551 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400552 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400553 btrfs_node_key(parent, &parent_key, parent_slot);
554 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400555
Chris Mason5f39d392007-10-15 16:14:19 -0400556 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400557 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400558 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400559 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500560 }
Chris Mason5f39d392007-10-15 16:14:19 -0400561#if 0
562 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
563 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
564 btrfs_item_key(leaf, &leaf_key, i);
565 if (comp_keys(&leaf_key, &cpukey) >= 0) {
566 btrfs_print_leaf(root, leaf);
567 printk("slot %d offset bad key\n", i);
568 BUG_ON(1);
569 }
570 if (btrfs_item_offset_nr(leaf, i) !=
571 btrfs_item_end_nr(leaf, i + 1)) {
572 btrfs_print_leaf(root, leaf);
573 printk("slot %d offset bad\n", i);
574 BUG_ON(1);
575 }
576 if (i == 0) {
577 if (btrfs_item_offset_nr(leaf, i) +
578 btrfs_item_size_nr(leaf, i) !=
579 BTRFS_LEAF_DATA_SIZE(root)) {
580 btrfs_print_leaf(root, leaf);
581 printk("slot %d first offset bad\n", i);
582 BUG_ON(1);
583 }
584 }
585 }
586 if (nritems > 0) {
587 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
588 btrfs_print_leaf(root, leaf);
589 printk("slot %d bad size \n", nritems - 1);
590 BUG_ON(1);
591 }
592 }
593#endif
594 if (slot != 0 && slot < nritems - 1) {
595 btrfs_item_key(leaf, &leaf_key, slot);
596 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
597 if (comp_keys(&leaf_key, &cpukey) <= 0) {
598 btrfs_print_leaf(root, leaf);
599 printk("slot %d offset bad key\n", slot);
600 BUG_ON(1);
601 }
602 if (btrfs_item_offset_nr(leaf, slot - 1) !=
603 btrfs_item_end_nr(leaf, slot)) {
604 btrfs_print_leaf(root, leaf);
605 printk("slot %d offset bad\n", slot);
606 BUG_ON(1);
607 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500608 }
Chris Mason8d7be552007-05-10 11:24:42 -0400609 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400610 btrfs_item_key(leaf, &leaf_key, slot);
611 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
612 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
613 if (btrfs_item_offset_nr(leaf, slot) !=
614 btrfs_item_end_nr(leaf, slot + 1)) {
615 btrfs_print_leaf(root, leaf);
616 printk("slot %d offset bad\n", slot);
617 BUG_ON(1);
618 }
Chris Mason8d7be552007-05-10 11:24:42 -0400619 }
Chris Mason5f39d392007-10-15 16:14:19 -0400620 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
621 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500622 return 0;
623}
624
Chris Mason98ed5172008-01-03 10:01:48 -0500625static int noinline check_block(struct btrfs_root *root,
626 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500627{
Chris Masonf1885912008-04-09 16:28:12 -0400628 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400629 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400630 if (btrfs_header_level(path->nodes[level]) != level)
631 printk("warning: bad level %Lu wanted %d found %d\n",
632 path->nodes[level]->start, level,
633 btrfs_header_level(path->nodes[level]));
634 found_start = btrfs_header_bytenr(path->nodes[level]);
635 if (found_start != path->nodes[level]->start) {
636 printk("warning: bad bytentr %Lu found %Lu\n",
637 path->nodes[level]->start, found_start);
638 }
Chris Masondb945352007-10-15 16:15:53 -0400639#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400640 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400641
Chris Mason479965d2007-10-15 16:14:27 -0400642 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
643 (unsigned long)btrfs_header_fsid(buf),
644 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400645 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400646 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400647 }
Chris Masondb945352007-10-15 16:15:53 -0400648#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500649 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400650 return check_leaf(root, path, level);
651 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500652}
653
Chris Mason74123bd2007-02-02 11:05:29 -0500654/*
Chris Mason5f39d392007-10-15 16:14:19 -0400655 * search for key in the extent_buffer. The items start at offset p,
656 * and they are item_size apart. There are 'max' items in p.
657 *
Chris Mason74123bd2007-02-02 11:05:29 -0500658 * the slot in the array is returned via slot, and it points to
659 * the place where you would insert key if it is not found in
660 * the array.
661 *
662 * slot may point to max if the key is bigger than all of the keys
663 */
Chris Mason5f39d392007-10-15 16:14:19 -0400664static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
665 int item_size, struct btrfs_key *key,
666 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500667{
668 int low = 0;
669 int high = max;
670 int mid;
671 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400672 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400673 struct btrfs_disk_key unaligned;
674 unsigned long offset;
675 char *map_token = NULL;
676 char *kaddr = NULL;
677 unsigned long map_start = 0;
678 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400679 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500680
681 while(low < high) {
682 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400683 offset = p + mid * item_size;
684
685 if (!map_token || offset < map_start ||
686 (offset + sizeof(struct btrfs_disk_key)) >
687 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400688 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400689 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400690 map_token = NULL;
691 }
692 err = map_extent_buffer(eb, offset,
693 sizeof(struct btrfs_disk_key),
694 &map_token, &kaddr,
695 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400696
Chris Mason479965d2007-10-15 16:14:27 -0400697 if (!err) {
698 tmp = (struct btrfs_disk_key *)(kaddr + offset -
699 map_start);
700 } else {
701 read_extent_buffer(eb, &unaligned,
702 offset, sizeof(unaligned));
703 tmp = &unaligned;
704 }
705
Chris Mason5f39d392007-10-15 16:14:19 -0400706 } else {
707 tmp = (struct btrfs_disk_key *)(kaddr + offset -
708 map_start);
709 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500710 ret = comp_keys(tmp, key);
711
712 if (ret < 0)
713 low = mid + 1;
714 else if (ret > 0)
715 high = mid;
716 else {
717 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400718 if (map_token)
719 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500720 return 0;
721 }
722 }
723 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400724 if (map_token)
725 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726 return 1;
727}
728
Chris Mason97571fd2007-02-24 13:39:08 -0500729/*
730 * simple bin_search frontend that does the right thing for
731 * leaves vs nodes
732 */
Chris Mason5f39d392007-10-15 16:14:19 -0400733static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
734 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735{
Chris Mason5f39d392007-10-15 16:14:19 -0400736 if (level == 0) {
737 return generic_bin_search(eb,
738 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400739 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400740 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400741 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500742 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400743 return generic_bin_search(eb,
744 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400745 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400746 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400747 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748 }
749 return -1;
750}
751
Chris Mason5f39d392007-10-15 16:14:19 -0400752static struct extent_buffer *read_node_slot(struct btrfs_root *root,
753 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500754{
Chris Masonca7a79a2008-05-12 12:59:19 -0400755 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500756 if (slot < 0)
757 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400758 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500759 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400760
761 BUG_ON(level == 0);
762
Chris Masondb945352007-10-15 16:15:53 -0400763 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400764 btrfs_level_size(root, level - 1),
765 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500766}
767
Chris Mason98ed5172008-01-03 10:01:48 -0500768static int balance_level(struct btrfs_trans_handle *trans,
769 struct btrfs_root *root,
770 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500771{
Chris Mason5f39d392007-10-15 16:14:19 -0400772 struct extent_buffer *right = NULL;
773 struct extent_buffer *mid;
774 struct extent_buffer *left = NULL;
775 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500776 int ret = 0;
777 int wret;
778 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500779 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400780 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500781 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500782
783 if (level == 0)
784 return 0;
785
Chris Mason5f39d392007-10-15 16:14:19 -0400786 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400787 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500788 WARN_ON(btrfs_header_generation(mid) != trans->transid);
789
Chris Mason1d4f8a02007-03-13 09:28:32 -0400790 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500791
Chris Mason234b63a2007-03-13 10:46:10 -0400792 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400793 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500794 pslot = path->slots[level + 1];
795
Chris Mason40689472007-03-17 14:29:23 -0400796 /*
797 * deal with the case where there is only one pointer in the root
798 * by promoting the node below to a root
799 */
Chris Mason5f39d392007-10-15 16:14:19 -0400800 if (!parent) {
801 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500802
Chris Mason5f39d392007-10-15 16:14:19 -0400803 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500804 return 0;
805
806 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400807 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400808 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500809 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500810 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
811 BUG_ON(ret);
812
Chris Mason925baed2008-06-25 16:01:30 -0400813 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500814 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400815 spin_unlock(&root->node_lock);
816
Chris Mason0b86a832008-03-24 15:01:56 -0400817 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400818 btrfs_tree_unlock(child);
819 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500820 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400821 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400822 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500823 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400824 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500825 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
826 root->root_key.objectid,
827 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500828 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400829 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400830 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500831 }
Chris Mason5f39d392007-10-15 16:14:19 -0400832 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400833 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500834 return 0;
835
Chris Mason5f39d392007-10-15 16:14:19 -0400836 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400837 err_on_enospc = 1;
838
Chris Mason5f39d392007-10-15 16:14:19 -0400839 left = read_node_slot(root, parent, pslot - 1);
840 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400841 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400842 wret = btrfs_cow_block(trans, root, left,
843 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400844 if (wret) {
845 ret = wret;
846 goto enospc;
847 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400848 }
Chris Mason5f39d392007-10-15 16:14:19 -0400849 right = read_node_slot(root, parent, pslot + 1);
850 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400851 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400852 wret = btrfs_cow_block(trans, root, right,
853 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400854 if (wret) {
855 ret = wret;
856 goto enospc;
857 }
858 }
859
860 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400861 if (left) {
862 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400863 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500864 if (wret < 0)
865 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400866 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400867 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500868 }
Chris Mason79f95c82007-03-01 15:16:26 -0500869
870 /*
871 * then try to empty the right most buffer into the middle
872 */
Chris Mason5f39d392007-10-15 16:14:19 -0400873 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400874 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400875 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500876 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400877 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400878 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500879 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400880 u32 blocksize = right->len;
881
Chris Mason5f39d392007-10-15 16:14:19 -0400882 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400883 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400884 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500885 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400886 wret = del_ptr(trans, root, path, level + 1, pslot +
887 1);
Chris Masonbb803952007-03-01 12:04:21 -0500888 if (wret)
889 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400890 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500891 blocksize,
892 btrfs_header_owner(parent),
893 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500894 if (wret)
895 ret = wret;
896 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400897 struct btrfs_disk_key right_key;
898 btrfs_node_key(right, &right_key, 0);
899 btrfs_set_node_key(parent, &right_key, pslot + 1);
900 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500901 }
902 }
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500904 /*
905 * we're not allowed to leave a node with one item in the
906 * tree during a delete. A deletion from lower in the tree
907 * could try to delete the only pointer in this node.
908 * So, pull some keys from the left.
909 * There has to be a left pointer at this point because
910 * otherwise we would have pulled some pointers from the
911 * right
912 */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 BUG_ON(!left);
914 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400915 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500916 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400917 goto enospc;
918 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400919 if (wret == 1) {
920 wret = push_node_left(trans, root, left, mid, 1);
921 if (wret < 0)
922 ret = wret;
923 }
Chris Mason79f95c82007-03-01 15:16:26 -0500924 BUG_ON(wret == 1);
925 }
Chris Mason5f39d392007-10-15 16:14:19 -0400926 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500927 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500928 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400929 u64 bytenr = mid->start;
930 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400931
Chris Mason5f39d392007-10-15 16:14:19 -0400932 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400933 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400934 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500935 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400936 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500937 if (wret)
938 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500939 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
940 btrfs_header_owner(parent),
941 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500942 if (wret)
943 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500944 } else {
945 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 struct btrfs_disk_key mid_key;
947 btrfs_node_key(mid, &mid_key, 0);
948 btrfs_set_node_key(parent, &mid_key, pslot);
949 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500950 }
Chris Masonbb803952007-03-01 12:04:21 -0500951
Chris Mason79f95c82007-03-01 15:16:26 -0500952 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400953 if (left) {
954 if (btrfs_header_nritems(left) > orig_slot) {
955 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400956 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400957 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500958 path->slots[level + 1] -= 1;
959 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400960 if (mid) {
961 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400962 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400963 }
Chris Masonbb803952007-03-01 12:04:21 -0500964 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400965 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500966 path->slots[level] = orig_slot;
967 }
968 }
Chris Mason79f95c82007-03-01 15:16:26 -0500969 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400970 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400971 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400972 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500973 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400974enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400975 if (right) {
976 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400977 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400978 }
979 if (left) {
980 if (path->nodes[level] != left)
981 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400983 }
Chris Masonbb803952007-03-01 12:04:21 -0500984 return ret;
985}
986
Chris Masone66f7092007-04-20 13:16:02 -0400987/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500988static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
989 struct btrfs_root *root,
990 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400991{
Chris Mason5f39d392007-10-15 16:14:19 -0400992 struct extent_buffer *right = NULL;
993 struct extent_buffer *mid;
994 struct extent_buffer *left = NULL;
995 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400996 int ret = 0;
997 int wret;
998 int pslot;
999 int orig_slot = path->slots[level];
1000 u64 orig_ptr;
1001
1002 if (level == 0)
1003 return 1;
1004
Chris Mason5f39d392007-10-15 16:14:19 -04001005 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001006 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001007 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1008
1009 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001010 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001011 pslot = path->slots[level + 1];
1012
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001014 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001015
Chris Mason5f39d392007-10-15 16:14:19 -04001016 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001017
1018 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001019 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001020 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001021
1022 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001023 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001024 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1025 wret = 1;
1026 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001027 ret = btrfs_cow_block(trans, root, left, parent,
1028 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001029 if (ret)
1030 wret = 1;
1031 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001032 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001033 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001034 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001035 }
Chris Masone66f7092007-04-20 13:16:02 -04001036 if (wret < 0)
1037 ret = wret;
1038 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001039 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001040 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001041 btrfs_node_key(mid, &disk_key, 0);
1042 btrfs_set_node_key(parent, &disk_key, pslot);
1043 btrfs_mark_buffer_dirty(parent);
1044 if (btrfs_header_nritems(left) > orig_slot) {
1045 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001046 path->slots[level + 1] -= 1;
1047 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001048 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001049 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001050 } else {
1051 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001052 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001053 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001054 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001055 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001056 }
Chris Masone66f7092007-04-20 13:16:02 -04001057 return 0;
1058 }
Chris Mason925baed2008-06-25 16:01:30 -04001059 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001060 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001061 }
Chris Mason925baed2008-06-25 16:01:30 -04001062 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001063
1064 /*
1065 * then try to empty the right most buffer into the middle
1066 */
Chris Mason5f39d392007-10-15 16:14:19 -04001067 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001068 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001069 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001070 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001071 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1072 wret = 1;
1073 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001074 ret = btrfs_cow_block(trans, root, right,
1075 parent, pslot + 1,
1076 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001077 if (ret)
1078 wret = 1;
1079 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001080 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001081 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001082 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001083 }
Chris Masone66f7092007-04-20 13:16:02 -04001084 if (wret < 0)
1085 ret = wret;
1086 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_disk_key disk_key;
1088
1089 btrfs_node_key(right, &disk_key, 0);
1090 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1091 btrfs_mark_buffer_dirty(parent);
1092
1093 if (btrfs_header_nritems(mid) <= orig_slot) {
1094 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001095 path->slots[level + 1] += 1;
1096 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001097 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001098 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001099 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001100 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001101 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001102 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001103 }
Chris Masone66f7092007-04-20 13:16:02 -04001104 return 0;
1105 }
Chris Mason925baed2008-06-25 16:01:30 -04001106 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001107 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001108 }
Chris Masone66f7092007-04-20 13:16:02 -04001109 return 1;
1110}
1111
Chris Mason74123bd2007-02-02 11:05:29 -05001112/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001113 * readahead one full node of leaves
1114 */
1115static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001116 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001117{
Chris Mason5f39d392007-10-15 16:14:19 -04001118 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001119 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001120 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001121 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001122 u64 lowest_read;
1123 u64 highest_read;
1124 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001125 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001126 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001127 u32 nr;
1128 u32 blocksize;
1129 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001130
Chris Masona6b6e752007-10-15 16:22:39 -04001131 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001132 return;
1133
Chris Mason6702ed42007-08-07 16:15:09 -04001134 if (!path->nodes[level])
1135 return;
1136
Chris Mason5f39d392007-10-15 16:14:19 -04001137 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001138
Chris Mason3c69fae2007-08-07 15:52:22 -04001139 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001140 blocksize = btrfs_level_size(root, level - 1);
1141 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001142 if (eb) {
1143 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001144 return;
1145 }
1146
Chris Mason6b800532007-10-15 16:17:34 -04001147 highest_read = search;
1148 lowest_read = search;
1149
Chris Mason5f39d392007-10-15 16:14:19 -04001150 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001151 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001152 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001153 if (direction < 0) {
1154 if (nr == 0)
1155 break;
1156 nr--;
1157 } else if (direction > 0) {
1158 nr++;
1159 if (nr >= nritems)
1160 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001161 }
Chris Mason01f46652007-12-21 16:24:26 -05001162 if (path->reada < 0 && objectid) {
1163 btrfs_node_key(node, &disk_key, nr);
1164 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1165 break;
1166 }
Chris Mason6b800532007-10-15 16:17:34 -04001167 search = btrfs_node_blockptr(node, nr);
1168 if ((search >= lowest_read && search <= highest_read) ||
1169 (search < lowest_read && lowest_read - search <= 32768) ||
1170 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001171 readahead_tree_block(root, search, blocksize,
1172 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001173 nread += blocksize;
1174 }
1175 nscan++;
1176 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1177 break;
1178 if(nread > (1024 * 1024) || nscan > 128)
1179 break;
1180
1181 if (search < lowest_read)
1182 lowest_read = search;
1183 if (search > highest_read)
1184 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001185 }
1186}
Chris Mason925baed2008-06-25 16:01:30 -04001187
1188static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1189{
1190 int i;
1191 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001192 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001193 struct extent_buffer *t;
1194
1195 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1196 if (!path->nodes[i])
1197 break;
1198 if (!path->locks[i])
1199 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001200 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001201 skip_level = i + 1;
1202 continue;
1203 }
Chris Mason051e1b92008-06-25 16:01:30 -04001204 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001205 u32 nritems;
1206 t = path->nodes[i];
1207 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001208 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001209 skip_level = i + 1;
1210 continue;
1211 }
1212 }
Chris Mason051e1b92008-06-25 16:01:30 -04001213 if (skip_level < i && i >= lowest_unlock)
1214 no_skips = 1;
1215
Chris Mason925baed2008-06-25 16:01:30 -04001216 t = path->nodes[i];
1217 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1218 btrfs_tree_unlock(t);
1219 path->locks[i] = 0;
1220 }
1221 }
1222}
1223
Chris Mason3c69fae2007-08-07 15:52:22 -04001224/*
Chris Mason74123bd2007-02-02 11:05:29 -05001225 * look for key in the tree. path is filled in with nodes along the way
1226 * if key is found, we return zero and you can find the item in the leaf
1227 * level of the path (level 0)
1228 *
1229 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001230 * be inserted, and 1 is returned. If there are other errors during the
1231 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001232 *
1233 * if ins_len > 0, nodes and leaves will be split as we walk down the
1234 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1235 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001236 */
Chris Masone089f052007-03-16 16:20:31 -04001237int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1238 *root, struct btrfs_key *key, struct btrfs_path *p, int
1239 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001240{
Chris Mason5f39d392007-10-15 16:14:19 -04001241 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001242 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001243 int slot;
1244 int ret;
1245 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001246 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001247 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001248 u8 lowest_level = 0;
1249
Chris Mason6702ed42007-08-07 16:15:09 -04001250 lowest_level = p->lowest_level;
1251 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001252 WARN_ON(p->nodes[0] != NULL);
Chris Mason925baed2008-06-25 16:01:30 -04001253 WARN_ON(root == root->fs_info->extent_root &&
1254 !mutex_is_locked(&root->fs_info->alloc_mutex));
1255 WARN_ON(root == root->fs_info->chunk_root &&
1256 !mutex_is_locked(&root->fs_info->chunk_mutex));
1257 WARN_ON(root == root->fs_info->dev_root &&
1258 !mutex_is_locked(&root->fs_info->chunk_mutex));
1259 if (ins_len < 0)
1260 lowest_unlock = 2;
Chris Masonbb803952007-03-01 12:04:21 -05001261again:
Chris Mason051e1b92008-06-25 16:01:30 -04001262 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001263
Chris Masoneb60cea2007-02-02 09:18:22 -05001264 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001265 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001266 if (cow) {
1267 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001268 wret = btrfs_cow_block(trans, root, b,
1269 p->nodes[level + 1],
1270 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001271 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001272 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001273 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001274 return wret;
1275 }
Chris Mason02217ed2007-03-02 16:08:05 -05001276 }
1277 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001278 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001279 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001280 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001281 p->nodes[level] = b;
Chris Mason051e1b92008-06-25 16:01:30 -04001282 p->locks[level] = 1;
Chris Mason123abc82007-03-14 14:14:43 -04001283 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001284 if (ret)
1285 return -1;
Chris Mason925baed2008-06-25 16:01:30 -04001286
Chris Mason5f39d392007-10-15 16:14:19 -04001287 ret = bin_search(b, key, level, &slot);
1288 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001289 if (ret && slot > 0)
1290 slot -= 1;
1291 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001292 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001293 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001294 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001295 BUG_ON(sret > 0);
1296 if (sret)
1297 return sret;
1298 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001299 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001300 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001301 int sret = balance_level(trans, root, p,
1302 level);
Chris Masonbb803952007-03-01 12:04:21 -05001303 if (sret)
1304 return sret;
1305 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001306 if (!b) {
1307 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001308 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001309 }
Chris Masonbb803952007-03-01 12:04:21 -05001310 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001311 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001312 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001313 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001314 if (level == lowest_level) {
1315 unlock_up(p, level, lowest_unlock);
Chris Mason9f3a7422007-08-07 15:52:19 -04001316 break;
Chris Mason925baed2008-06-25 16:01:30 -04001317 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001318
Chris Mason6702ed42007-08-07 16:15:09 -04001319 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001320 reada_for_search(root, p, level, slot,
1321 key->objectid);
Chris Masonca7a79a2008-05-12 12:59:19 -04001322
Chris Mason051e1b92008-06-25 16:01:30 -04001323 tmp = btrfs_find_tree_block(root,
1324 btrfs_node_blockptr(b, slot),
1325 btrfs_level_size(root, level - 1));
1326 if (tmp && btrfs_buffer_uptodate(tmp,
1327 btrfs_node_ptr_generation(b, slot))) {
1328 b = tmp;
1329 } else {
1330 /*
1331 * reduce lock contention at high levels
1332 * of the btree by dropping locks before
1333 * we read.
1334 */
1335 if (level > 1) {
1336 btrfs_release_path(NULL, p);
1337 if (tmp)
1338 free_extent_buffer(tmp);
1339 goto again;
1340 } else {
1341 b = read_node_slot(root, b, slot);
1342 }
1343 }
1344 btrfs_tree_lock(b);
1345 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001346 } else {
1347 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001349 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001350 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001351 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001352 BUG_ON(sret > 0);
1353 if (sret)
1354 return sret;
1355 }
Chris Mason925baed2008-06-25 16:01:30 -04001356 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001357 return ret;
1358 }
1359 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001360 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001361}
1362
Chris Mason74123bd2007-02-02 11:05:29 -05001363/*
1364 * adjust the pointers going up the tree, starting at level
1365 * making sure the right key of each node is points to 'key'.
1366 * This is used after shifting pointers to the left, so it stops
1367 * fixing up pointers when a given leaf/node is not in slot 0 of the
1368 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001369 *
1370 * If this fails to write a tree block, it returns -1, but continues
1371 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001372 */
Chris Mason5f39d392007-10-15 16:14:19 -04001373static int fixup_low_keys(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root, struct btrfs_path *path,
1375 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001376{
1377 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001378 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001379 struct extent_buffer *t;
1380
Chris Mason234b63a2007-03-13 10:46:10 -04001381 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001382 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001383 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001384 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001385 t = path->nodes[i];
1386 btrfs_set_node_key(t, key, tslot);
Chris Mason925baed2008-06-25 16:01:30 -04001387 if (!btrfs_tree_locked(path->nodes[i])) {
1388 int ii;
1389printk("fixup without lock on level %d\n", btrfs_header_level(path->nodes[i]));
1390 for (ii = 0; ii < BTRFS_MAX_LEVEL; ii++) {
1391printk("level %d slot %d\n", ii, path->slots[ii]);
1392 }
1393 }
Chris Masond6025572007-03-30 14:27:56 -04001394 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001395 if (tslot != 0)
1396 break;
1397 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001398 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001399}
1400
Chris Mason74123bd2007-02-02 11:05:29 -05001401/*
1402 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001403 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001404 *
1405 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1406 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001407 */
Chris Mason98ed5172008-01-03 10:01:48 -05001408static int push_node_left(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001410 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001411{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001412 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001413 int src_nritems;
1414 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001415 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001416
Chris Mason5f39d392007-10-15 16:14:19 -04001417 src_nritems = btrfs_header_nritems(src);
1418 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001419 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001420 WARN_ON(btrfs_header_generation(src) != trans->transid);
1421 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001422
Chris Masonbce4eae2008-04-24 14:42:46 -04001423 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001424 return 1;
1425
Chris Masoneb60cea2007-02-02 09:18:22 -05001426 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001427 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001428 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001429
Chris Masonbce4eae2008-04-24 14:42:46 -04001430 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001431 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001432 if (push_items < src_nritems) {
1433 /* leave at least 8 pointers in the node if
1434 * we aren't going to empty it
1435 */
1436 if (src_nritems - push_items < 8) {
1437 if (push_items <= 8)
1438 return 1;
1439 push_items -= 8;
1440 }
1441 }
1442 } else
1443 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001444
Chris Mason5f39d392007-10-15 16:14:19 -04001445 copy_extent_buffer(dst, src,
1446 btrfs_node_key_ptr_offset(dst_nritems),
1447 btrfs_node_key_ptr_offset(0),
1448 push_items * sizeof(struct btrfs_key_ptr));
1449
Chris Masonbb803952007-03-01 12:04:21 -05001450 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001451 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1452 btrfs_node_key_ptr_offset(push_items),
1453 (src_nritems - push_items) *
1454 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001455 }
Chris Mason5f39d392007-10-15 16:14:19 -04001456 btrfs_set_header_nritems(src, src_nritems - push_items);
1457 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1458 btrfs_mark_buffer_dirty(src);
1459 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001460 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001461}
1462
Chris Mason97571fd2007-02-24 13:39:08 -05001463/*
Chris Mason79f95c82007-03-01 15:16:26 -05001464 * try to push data from one node into the next node right in the
1465 * tree.
1466 *
1467 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1468 * error, and > 0 if there was no room in the right hand block.
1469 *
1470 * this will only push up to 1/2 the contents of the left node over
1471 */
Chris Mason5f39d392007-10-15 16:14:19 -04001472static int balance_node_right(struct btrfs_trans_handle *trans,
1473 struct btrfs_root *root,
1474 struct extent_buffer *dst,
1475 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001476{
Chris Mason79f95c82007-03-01 15:16:26 -05001477 int push_items = 0;
1478 int max_push;
1479 int src_nritems;
1480 int dst_nritems;
1481 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001482
Chris Mason7bb86312007-12-11 09:25:06 -05001483 WARN_ON(btrfs_header_generation(src) != trans->transid);
1484 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1485
Chris Mason5f39d392007-10-15 16:14:19 -04001486 src_nritems = btrfs_header_nritems(src);
1487 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001488 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001489 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001490 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001491 }
1492
1493 if (src_nritems < 4) {
1494 return 1;
1495 }
Chris Mason79f95c82007-03-01 15:16:26 -05001496
1497 max_push = src_nritems / 2 + 1;
1498 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001499 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001500 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001501 }
Yan252c38f2007-08-29 09:11:44 -04001502
Chris Mason79f95c82007-03-01 15:16:26 -05001503 if (max_push < push_items)
1504 push_items = max_push;
1505
Chris Mason5f39d392007-10-15 16:14:19 -04001506 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1507 btrfs_node_key_ptr_offset(0),
1508 (dst_nritems) *
1509 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001510
Chris Mason5f39d392007-10-15 16:14:19 -04001511 copy_extent_buffer(dst, src,
1512 btrfs_node_key_ptr_offset(0),
1513 btrfs_node_key_ptr_offset(src_nritems - push_items),
1514 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001515
Chris Mason5f39d392007-10-15 16:14:19 -04001516 btrfs_set_header_nritems(src, src_nritems - push_items);
1517 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001518
Chris Mason5f39d392007-10-15 16:14:19 -04001519 btrfs_mark_buffer_dirty(src);
1520 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001521 return ret;
1522}
1523
1524/*
Chris Mason97571fd2007-02-24 13:39:08 -05001525 * helper function to insert a new root level in the tree.
1526 * A new node is allocated, and a single item is inserted to
1527 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001528 *
1529 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001530 */
Chris Mason98ed5172008-01-03 10:01:48 -05001531static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001532 struct btrfs_root *root,
1533 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001534{
Chris Mason7bb86312007-12-11 09:25:06 -05001535 u64 root_gen;
1536 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001537 struct extent_buffer *lower;
1538 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001539 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001540 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001541
1542 BUG_ON(path->nodes[level]);
1543 BUG_ON(path->nodes[level-1] != root->node);
1544
Chris Mason7bb86312007-12-11 09:25:06 -05001545 if (root->ref_cows)
1546 root_gen = trans->transid;
1547 else
1548 root_gen = 0;
1549
1550 lower = path->nodes[level-1];
1551 if (level == 1)
1552 btrfs_item_key(lower, &lower_key, 0);
1553 else
1554 btrfs_node_key(lower, &lower_key, 0);
1555
Chris Mason925baed2008-06-25 16:01:30 -04001556 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001557 root->root_key.objectid,
1558 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001559 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001560 if (IS_ERR(c))
1561 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001562
Chris Mason5f39d392007-10-15 16:14:19 -04001563 memset_extent_buffer(c, 0, 0, root->nodesize);
1564 btrfs_set_header_nritems(c, 1);
1565 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001566 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001567 btrfs_set_header_generation(c, trans->transid);
1568 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001569
Chris Mason5f39d392007-10-15 16:14:19 -04001570 write_extent_buffer(c, root->fs_info->fsid,
1571 (unsigned long)btrfs_header_fsid(c),
1572 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001573
1574 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1575 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1576 BTRFS_UUID_SIZE);
1577
Chris Mason5f39d392007-10-15 16:14:19 -04001578 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001579 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001580 lower_gen = btrfs_header_generation(lower);
1581 WARN_ON(lower_gen == 0);
1582
1583 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001584
1585 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001586
Chris Mason925baed2008-06-25 16:01:30 -04001587 spin_lock(&root->node_lock);
1588 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001589 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001590 spin_unlock(&root->node_lock);
1591
1592 /* the super has an extra ref to root->node */
1593 free_extent_buffer(old);
1594
Chris Mason0b86a832008-03-24 15:01:56 -04001595 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001596 extent_buffer_get(c);
1597 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001598 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001599 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001600
1601 if (root->ref_cows && lower_gen != trans->transid) {
1602 struct btrfs_path *back_path = btrfs_alloc_path();
1603 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001604 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001605 ret = btrfs_insert_extent_backref(trans,
1606 root->fs_info->extent_root,
1607 path, lower->start,
1608 root->root_key.objectid,
1609 trans->transid, 0, 0);
1610 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001611 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001612 btrfs_free_path(back_path);
1613 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001614 return 0;
1615}
1616
Chris Mason74123bd2007-02-02 11:05:29 -05001617/*
1618 * worker function to insert a single pointer in a node.
1619 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001620 *
Chris Mason74123bd2007-02-02 11:05:29 -05001621 * slot and level indicate where you want the key to go, and
1622 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001623 *
1624 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001625 */
Chris Masone089f052007-03-16 16:20:31 -04001626static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1627 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001628 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001629{
Chris Mason5f39d392007-10-15 16:14:19 -04001630 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001631 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001632
1633 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001634 lower = path->nodes[level];
1635 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001636 if (slot > nritems)
1637 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001638 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001639 BUG();
1640 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001641 memmove_extent_buffer(lower,
1642 btrfs_node_key_ptr_offset(slot + 1),
1643 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001644 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001645 }
Chris Mason5f39d392007-10-15 16:14:19 -04001646 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001647 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001648 WARN_ON(trans->transid == 0);
1649 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001650 btrfs_set_header_nritems(lower, nritems + 1);
1651 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001652 return 0;
1653}
1654
Chris Mason97571fd2007-02-24 13:39:08 -05001655/*
1656 * split the node at the specified level in path in two.
1657 * The path is corrected to point to the appropriate node after the split
1658 *
1659 * Before splitting this tries to make some room in the node by pushing
1660 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001661 *
1662 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001663 */
Chris Masone089f052007-03-16 16:20:31 -04001664static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1665 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001666{
Chris Mason7bb86312007-12-11 09:25:06 -05001667 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001668 struct extent_buffer *c;
1669 struct extent_buffer *split;
1670 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001672 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001673 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001674 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001675
Chris Mason5f39d392007-10-15 16:14:19 -04001676 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001677 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001678 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001679 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001680 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001681 if (ret)
1682 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001683 } else {
1684 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001685 c = path->nodes[level];
1686 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001687 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001688 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001689 if (ret < 0)
1690 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001691 }
Chris Masone66f7092007-04-20 13:16:02 -04001692
Chris Mason5f39d392007-10-15 16:14:19 -04001693 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001694 if (root->ref_cows)
1695 root_gen = trans->transid;
1696 else
1697 root_gen = 0;
1698
1699 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001700 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001701 root->root_key.objectid,
1702 root_gen,
1703 btrfs_disk_key_objectid(&disk_key),
1704 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001705 if (IS_ERR(split))
1706 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001707
Chris Mason5f39d392007-10-15 16:14:19 -04001708 btrfs_set_header_flags(split, btrfs_header_flags(c));
1709 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001710 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001711 btrfs_set_header_generation(split, trans->transid);
1712 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001713 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001714 write_extent_buffer(split, root->fs_info->fsid,
1715 (unsigned long)btrfs_header_fsid(split),
1716 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001717 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1718 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1719 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001720
Chris Mason7518a232007-03-12 12:01:18 -04001721 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001722
1723 copy_extent_buffer(split, c,
1724 btrfs_node_key_ptr_offset(0),
1725 btrfs_node_key_ptr_offset(mid),
1726 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1727 btrfs_set_header_nritems(split, c_nritems - mid);
1728 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001729 ret = 0;
1730
Chris Mason5f39d392007-10-15 16:14:19 -04001731 btrfs_mark_buffer_dirty(c);
1732 btrfs_mark_buffer_dirty(split);
1733
1734 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001735 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001736 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001737 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001738 if (wret)
1739 ret = wret;
1740
Chris Mason5de08d72007-02-24 06:24:44 -05001741 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001742 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001743 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001744 free_extent_buffer(c);
1745 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001746 path->slots[level + 1] += 1;
1747 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001748 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001749 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001750 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001751 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001752}
1753
Chris Mason74123bd2007-02-02 11:05:29 -05001754/*
1755 * how many bytes are required to store the items in a leaf. start
1756 * and nr indicate which items in the leaf to check. This totals up the
1757 * space used both by the item structs and the item data
1758 */
Chris Mason5f39d392007-10-15 16:14:19 -04001759static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001760{
1761 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001762 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001763 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001764
1765 if (!nr)
1766 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001767 data_len = btrfs_item_end_nr(l, start);
1768 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001769 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001770 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771 return data_len;
1772}
1773
Chris Mason74123bd2007-02-02 11:05:29 -05001774/*
Chris Masond4dbff92007-04-04 14:08:15 -04001775 * The space between the end of the leaf items and
1776 * the start of the leaf data. IOW, how much room
1777 * the leaf has left for both items and data
1778 */
Chris Mason5f39d392007-10-15 16:14:19 -04001779int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001780{
Chris Mason5f39d392007-10-15 16:14:19 -04001781 int nritems = btrfs_header_nritems(leaf);
1782 int ret;
1783 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1784 if (ret < 0) {
1785 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001786 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001787 leaf_space_used(leaf, 0, nritems), nritems);
1788 }
1789 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001790}
1791
1792/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001793 * push some data in the path leaf to the right, trying to free up at
1794 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001795 *
1796 * returns 1 if the push failed because the other node didn't have enough
1797 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001798 */
Chris Masone089f052007-03-16 16:20:31 -04001799static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001800 *root, struct btrfs_path *path, int data_size,
1801 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001802{
Chris Mason5f39d392007-10-15 16:14:19 -04001803 struct extent_buffer *left = path->nodes[0];
1804 struct extent_buffer *right;
1805 struct extent_buffer *upper;
1806 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001807 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001808 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001809 int free_space;
1810 int push_space = 0;
1811 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001812 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001813 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001814 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001815 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001816 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001817 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001818 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001819
1820 slot = path->slots[1];
1821 if (!path->nodes[1]) {
1822 return 1;
1823 }
1824 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001825 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001826 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001827
Chris Masona2135012008-06-25 16:01:30 -04001828 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1829
Chris Masonca7a79a2008-05-12 12:59:19 -04001830 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001831 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001832 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001833 if (free_space < data_size + sizeof(struct btrfs_item))
1834 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001835
Chris Mason5f39d392007-10-15 16:14:19 -04001836 /* cow and double check */
1837 ret = btrfs_cow_block(trans, root, right, upper,
1838 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04001839 if (ret)
1840 goto out_unlock;
1841
Chris Mason5f39d392007-10-15 16:14:19 -04001842 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001843 if (free_space < data_size + sizeof(struct btrfs_item))
1844 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001845
1846 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001847 if (left_nritems == 0)
1848 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001849
Chris Mason34a38212007-11-07 13:31:03 -05001850 if (empty)
1851 nr = 0;
1852 else
1853 nr = 1;
1854
1855 i = left_nritems - 1;
1856 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001857 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001858
Chris Mason00ec4c52007-02-24 12:47:20 -05001859 if (path->slots[0] == i)
1860 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001861
1862 if (!left->map_token) {
1863 map_extent_buffer(left, (unsigned long)item,
1864 sizeof(struct btrfs_item),
1865 &left->map_token, &left->kaddr,
1866 &left->map_start, &left->map_len,
1867 KM_USER1);
1868 }
1869
1870 this_item_size = btrfs_item_size(left, item);
1871 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001872 break;
1873 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001874 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001875 if (i == 0)
1876 break;
1877 i--;
Chris Masondb945352007-10-15 16:15:53 -04001878 }
1879 if (left->map_token) {
1880 unmap_extent_buffer(left, left->map_token, KM_USER1);
1881 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001882 }
Chris Mason5f39d392007-10-15 16:14:19 -04001883
Chris Mason925baed2008-06-25 16:01:30 -04001884 if (push_items == 0)
1885 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001886
Chris Mason34a38212007-11-07 13:31:03 -05001887 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001888 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001889
Chris Mason00ec4c52007-02-24 12:47:20 -05001890 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001891 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001892
Chris Mason5f39d392007-10-15 16:14:19 -04001893 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001894 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001895
Chris Mason00ec4c52007-02-24 12:47:20 -05001896 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001897 data_end = leaf_data_end(root, right);
1898 memmove_extent_buffer(right,
1899 btrfs_leaf_data(right) + data_end - push_space,
1900 btrfs_leaf_data(right) + data_end,
1901 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1902
Chris Mason00ec4c52007-02-24 12:47:20 -05001903 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001904 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001905 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1906 btrfs_leaf_data(left) + leaf_data_end(root, left),
1907 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001908
1909 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1910 btrfs_item_nr_offset(0),
1911 right_nritems * sizeof(struct btrfs_item));
1912
Chris Mason00ec4c52007-02-24 12:47:20 -05001913 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001914 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1915 btrfs_item_nr_offset(left_nritems - push_items),
1916 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001917
1918 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001919 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001920 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001921 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001922 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001923 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001924 if (!right->map_token) {
1925 map_extent_buffer(right, (unsigned long)item,
1926 sizeof(struct btrfs_item),
1927 &right->map_token, &right->kaddr,
1928 &right->map_start, &right->map_len,
1929 KM_USER1);
1930 }
1931 push_space -= btrfs_item_size(right, item);
1932 btrfs_set_item_offset(right, item, push_space);
1933 }
1934
1935 if (right->map_token) {
1936 unmap_extent_buffer(right, right->map_token, KM_USER1);
1937 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001938 }
Chris Mason7518a232007-03-12 12:01:18 -04001939 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001940 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001941
Chris Mason34a38212007-11-07 13:31:03 -05001942 if (left_nritems)
1943 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001944 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001945
Chris Mason5f39d392007-10-15 16:14:19 -04001946 btrfs_item_key(right, &disk_key, 0);
1947 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001948 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001949
Chris Mason00ec4c52007-02-24 12:47:20 -05001950 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001951 if (path->slots[0] >= left_nritems) {
1952 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04001953 if (btrfs_header_nritems(path->nodes[0]) == 0)
1954 clean_tree_block(trans, root, path->nodes[0]);
1955 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04001956 free_extent_buffer(path->nodes[0]);
1957 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001958 path->slots[1] += 1;
1959 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001960 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001961 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001962 }
1963 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04001964
1965out_unlock:
1966 btrfs_tree_unlock(right);
1967 free_extent_buffer(right);
1968 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05001969}
Chris Mason925baed2008-06-25 16:01:30 -04001970
Chris Mason00ec4c52007-02-24 12:47:20 -05001971/*
Chris Mason74123bd2007-02-02 11:05:29 -05001972 * push some data in the path leaf to the left, trying to free up at
1973 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1974 */
Chris Masone089f052007-03-16 16:20:31 -04001975static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001976 *root, struct btrfs_path *path, int data_size,
1977 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001978{
Chris Mason5f39d392007-10-15 16:14:19 -04001979 struct btrfs_disk_key disk_key;
1980 struct extent_buffer *right = path->nodes[0];
1981 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001982 int slot;
1983 int i;
1984 int free_space;
1985 int push_space = 0;
1986 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001987 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001988 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001989 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001990 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001991 int ret = 0;
1992 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001993 u32 this_item_size;
1994 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001995
1996 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001997 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001999 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002000 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002001
Chris Mason3685f792007-10-19 09:23:27 -04002002 right_nritems = btrfs_header_nritems(right);
2003 if (right_nritems == 0) {
2004 return 1;
2005 }
2006
Chris Masona2135012008-06-25 16:01:30 -04002007 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2008
Chris Masonca7a79a2008-05-12 12:59:19 -04002009 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002010 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002011 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002012 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002013 ret = 1;
2014 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002015 }
Chris Mason02217ed2007-03-02 16:08:05 -05002016
2017 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002018 ret = btrfs_cow_block(trans, root, left,
2019 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002020 if (ret) {
2021 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002022 ret = 1;
2023 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002024 }
Chris Mason3685f792007-10-19 09:23:27 -04002025
Chris Mason123abc82007-03-14 14:14:43 -04002026 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002027 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002028 ret = 1;
2029 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002030 }
2031
Chris Mason34a38212007-11-07 13:31:03 -05002032 if (empty)
2033 nr = right_nritems;
2034 else
2035 nr = right_nritems - 1;
2036
2037 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002038 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002039 if (!right->map_token) {
2040 map_extent_buffer(right, (unsigned long)item,
2041 sizeof(struct btrfs_item),
2042 &right->map_token, &right->kaddr,
2043 &right->map_start, &right->map_len,
2044 KM_USER1);
2045 }
2046
Chris Masonbe0e5c02007-01-26 15:51:26 -05002047 if (path->slots[0] == i)
2048 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002049
2050 this_item_size = btrfs_item_size(right, item);
2051 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002052 break;
Chris Masondb945352007-10-15 16:15:53 -04002053
Chris Masonbe0e5c02007-01-26 15:51:26 -05002054 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002055 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002056 }
Chris Masondb945352007-10-15 16:15:53 -04002057
2058 if (right->map_token) {
2059 unmap_extent_buffer(right, right->map_token, KM_USER1);
2060 right->map_token = NULL;
2061 }
2062
Chris Masonbe0e5c02007-01-26 15:51:26 -05002063 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002064 ret = 1;
2065 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002066 }
Chris Mason34a38212007-11-07 13:31:03 -05002067 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002068 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002069
Chris Masonbe0e5c02007-01-26 15:51:26 -05002070 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002071 copy_extent_buffer(left, right,
2072 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2073 btrfs_item_nr_offset(0),
2074 push_items * sizeof(struct btrfs_item));
2075
Chris Mason123abc82007-03-14 14:14:43 -04002076 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002077 btrfs_item_offset_nr(right, push_items -1);
2078
2079 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002080 leaf_data_end(root, left) - push_space,
2081 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002083 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002085 BUG_ON(old_left_nritems < 0);
2086
Chris Masondb945352007-10-15 16:15:53 -04002087 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002088 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002089 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002092 if (!left->map_token) {
2093 map_extent_buffer(left, (unsigned long)item,
2094 sizeof(struct btrfs_item),
2095 &left->map_token, &left->kaddr,
2096 &left->map_start, &left->map_len,
2097 KM_USER1);
2098 }
2099
Chris Mason5f39d392007-10-15 16:14:19 -04002100 ioff = btrfs_item_offset(left, item);
2101 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002102 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002103 }
Chris Mason5f39d392007-10-15 16:14:19 -04002104 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002105 if (left->map_token) {
2106 unmap_extent_buffer(left, left->map_token, KM_USER1);
2107 left->map_token = NULL;
2108 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002109
2110 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002111 if (push_items > right_nritems) {
2112 printk("push items %d nr %u\n", push_items, right_nritems);
2113 WARN_ON(1);
2114 }
Chris Mason5f39d392007-10-15 16:14:19 -04002115
Chris Mason34a38212007-11-07 13:31:03 -05002116 if (push_items < right_nritems) {
2117 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2118 leaf_data_end(root, right);
2119 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2120 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2121 btrfs_leaf_data(right) +
2122 leaf_data_end(root, right), push_space);
2123
2124 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002125 btrfs_item_nr_offset(push_items),
2126 (btrfs_header_nritems(right) - push_items) *
2127 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002128 }
Yaneef1c492007-11-26 10:58:13 -05002129 right_nritems -= push_items;
2130 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002131 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002132 for (i = 0; i < right_nritems; i++) {
2133 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002134
2135 if (!right->map_token) {
2136 map_extent_buffer(right, (unsigned long)item,
2137 sizeof(struct btrfs_item),
2138 &right->map_token, &right->kaddr,
2139 &right->map_start, &right->map_len,
2140 KM_USER1);
2141 }
2142
2143 push_space = push_space - btrfs_item_size(right, item);
2144 btrfs_set_item_offset(right, item, push_space);
2145 }
2146 if (right->map_token) {
2147 unmap_extent_buffer(right, right->map_token, KM_USER1);
2148 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002149 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002150
Chris Mason5f39d392007-10-15 16:14:19 -04002151 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002152 if (right_nritems)
2153 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002154
Chris Mason5f39d392007-10-15 16:14:19 -04002155 btrfs_item_key(right, &disk_key, 0);
2156 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002157 if (wret)
2158 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002159
2160 /* then fixup the leaf pointer in the path */
2161 if (path->slots[0] < push_items) {
2162 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002163 if (btrfs_header_nritems(path->nodes[0]) == 0)
2164 clean_tree_block(trans, root, path->nodes[0]);
2165 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002166 free_extent_buffer(path->nodes[0]);
2167 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 path->slots[1] -= 1;
2169 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002170 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002171 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002172 path->slots[0] -= push_items;
2173 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002174 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002175 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002176out:
2177 btrfs_tree_unlock(left);
2178 free_extent_buffer(left);
2179 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002180}
2181
Chris Mason74123bd2007-02-02 11:05:29 -05002182/*
2183 * split the path's leaf in two, making sure there is at least data_size
2184 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002185 *
2186 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002187 */
Chris Masone089f052007-03-16 16:20:31 -04002188static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002189 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002190 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002191{
Chris Mason7bb86312007-12-11 09:25:06 -05002192 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002193 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002194 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002195 int mid;
2196 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002197 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002198 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002199 int data_copy_size;
2200 int rt_data_off;
2201 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002202 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002203 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002204 int double_split;
2205 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002206 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002207
Chris Masoncc0c5532007-10-25 15:42:57 -04002208 if (extend)
2209 space_needed = data_size;
2210
Chris Mason7bb86312007-12-11 09:25:06 -05002211 if (root->ref_cows)
2212 root_gen = trans->transid;
2213 else
2214 root_gen = 0;
2215
Chris Mason40689472007-03-17 14:29:23 -04002216 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002217 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002218 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002219 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002220 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002221 }
2222 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002223 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002224 if (wret < 0)
2225 return wret;
2226 }
2227 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002228
Chris Mason3685f792007-10-19 09:23:27 -04002229 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002230 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002231 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002232 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002233
Chris Mason5c680ed2007-02-22 11:39:13 -05002234 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002235 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002236 if (ret)
2237 return ret;
2238 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002239again:
2240 double_split = 0;
2241 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002242 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002243 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002244 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002245
Chris Mason7bb86312007-12-11 09:25:06 -05002246 btrfs_item_key(l, &disk_key, 0);
2247
Chris Mason925baed2008-06-25 16:01:30 -04002248 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002249 root->root_key.objectid,
2250 root_gen, disk_key.objectid, 0,
2251 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002252 if (IS_ERR(right)) {
2253 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002254 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002255 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002256
Chris Mason5f39d392007-10-15 16:14:19 -04002257 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002258 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002259 btrfs_set_header_generation(right, trans->transid);
2260 btrfs_set_header_owner(right, root->root_key.objectid);
2261 btrfs_set_header_level(right, 0);
2262 write_extent_buffer(right, root->fs_info->fsid,
2263 (unsigned long)btrfs_header_fsid(right),
2264 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002265
2266 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2267 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2268 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002269 if (mid <= slot) {
2270 if (nritems == 1 ||
2271 leaf_space_used(l, mid, nritems - mid) + space_needed >
2272 BTRFS_LEAF_DATA_SIZE(root)) {
2273 if (slot >= nritems) {
2274 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002275 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002276 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002277 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002278 path->slots[1] + 1, 1);
2279 if (wret)
2280 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002281
2282 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002283 free_extent_buffer(path->nodes[0]);
2284 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002285 path->slots[0] = 0;
2286 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002287 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002288 return ret;
2289 }
2290 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002291 if (mid != nritems &&
2292 leaf_space_used(l, mid, nritems - mid) +
2293 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2294 double_split = 1;
2295 }
Chris Masond4dbff92007-04-04 14:08:15 -04002296 }
2297 } else {
2298 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2299 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002300 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002301 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002302 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002303 wret = insert_ptr(trans, root, path,
2304 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002305 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002306 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002307 if (wret)
2308 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002309 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002310 free_extent_buffer(path->nodes[0]);
2311 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002312 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002313 if (path->slots[1] == 0) {
2314 wret = fixup_low_keys(trans, root,
2315 path, &disk_key, 1);
2316 if (wret)
2317 ret = wret;
2318 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002319 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002320 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002321 } else if (extend && slot == 0) {
2322 mid = 1;
2323 } else {
2324 mid = slot;
2325 if (mid != nritems &&
2326 leaf_space_used(l, mid, nritems - mid) +
2327 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2328 double_split = 1;
2329 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002330 }
Chris Masond4dbff92007-04-04 14:08:15 -04002331 }
2332 }
Chris Mason5f39d392007-10-15 16:14:19 -04002333 nritems = nritems - mid;
2334 btrfs_set_header_nritems(right, nritems);
2335 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2336
2337 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2338 btrfs_item_nr_offset(mid),
2339 nritems * sizeof(struct btrfs_item));
2340
2341 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002342 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2343 data_copy_size, btrfs_leaf_data(l) +
2344 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002345
Chris Mason5f39d392007-10-15 16:14:19 -04002346 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2347 btrfs_item_end_nr(l, mid);
2348
2349 for (i = 0; i < nritems; i++) {
2350 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002351 u32 ioff;
2352
2353 if (!right->map_token) {
2354 map_extent_buffer(right, (unsigned long)item,
2355 sizeof(struct btrfs_item),
2356 &right->map_token, &right->kaddr,
2357 &right->map_start, &right->map_len,
2358 KM_USER1);
2359 }
2360
2361 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002362 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002363 }
Chris Mason74123bd2007-02-02 11:05:29 -05002364
Chris Masondb945352007-10-15 16:15:53 -04002365 if (right->map_token) {
2366 unmap_extent_buffer(right, right->map_token, KM_USER1);
2367 right->map_token = NULL;
2368 }
2369
Chris Mason5f39d392007-10-15 16:14:19 -04002370 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002371 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002372 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002373 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2374 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002375 if (wret)
2376 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002377
2378 btrfs_mark_buffer_dirty(right);
2379 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002380 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Masonbe0e5c02007-01-26 15:51:26 -05002382 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002383 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002384 free_extent_buffer(path->nodes[0]);
2385 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002386 path->slots[0] -= mid;
2387 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002388 } else {
2389 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002390 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002391 }
Chris Mason5f39d392007-10-15 16:14:19 -04002392
Chris Masoneb60cea2007-02-02 09:18:22 -05002393 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002394
Chris Masoncc0c5532007-10-25 15:42:57 -04002395 if (double_split) {
2396 BUG_ON(num_doubles != 0);
2397 num_doubles++;
2398 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002399 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002400 return ret;
2401}
2402
Chris Masonb18c6682007-04-17 13:26:50 -04002403int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2404 struct btrfs_root *root,
2405 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002406 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002407{
2408 int ret = 0;
2409 int slot;
2410 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002411 struct extent_buffer *leaf;
2412 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002413 u32 nritems;
2414 unsigned int data_end;
2415 unsigned int old_data_start;
2416 unsigned int old_size;
2417 unsigned int size_diff;
2418 int i;
2419
2420 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002421 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002422 slot = path->slots[0];
2423
2424 old_size = btrfs_item_size_nr(leaf, slot);
2425 if (old_size == new_size)
2426 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002427
Chris Mason5f39d392007-10-15 16:14:19 -04002428 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002429 data_end = leaf_data_end(root, leaf);
2430
Chris Mason5f39d392007-10-15 16:14:19 -04002431 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002432
Chris Masonb18c6682007-04-17 13:26:50 -04002433 size_diff = old_size - new_size;
2434
2435 BUG_ON(slot < 0);
2436 BUG_ON(slot >= nritems);
2437
2438 /*
2439 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2440 */
2441 /* first correct the data pointers */
2442 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002443 u32 ioff;
2444 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002445
2446 if (!leaf->map_token) {
2447 map_extent_buffer(leaf, (unsigned long)item,
2448 sizeof(struct btrfs_item),
2449 &leaf->map_token, &leaf->kaddr,
2450 &leaf->map_start, &leaf->map_len,
2451 KM_USER1);
2452 }
2453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 ioff = btrfs_item_offset(leaf, item);
2455 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002456 }
Chris Masondb945352007-10-15 16:15:53 -04002457
2458 if (leaf->map_token) {
2459 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2460 leaf->map_token = NULL;
2461 }
2462
Chris Masonb18c6682007-04-17 13:26:50 -04002463 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002464 if (from_end) {
2465 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2466 data_end + size_diff, btrfs_leaf_data(leaf) +
2467 data_end, old_data_start + new_size - data_end);
2468 } else {
2469 struct btrfs_disk_key disk_key;
2470 u64 offset;
2471
2472 btrfs_item_key(leaf, &disk_key, slot);
2473
2474 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2475 unsigned long ptr;
2476 struct btrfs_file_extent_item *fi;
2477
2478 fi = btrfs_item_ptr(leaf, slot,
2479 struct btrfs_file_extent_item);
2480 fi = (struct btrfs_file_extent_item *)(
2481 (unsigned long)fi - size_diff);
2482
2483 if (btrfs_file_extent_type(leaf, fi) ==
2484 BTRFS_FILE_EXTENT_INLINE) {
2485 ptr = btrfs_item_ptr_offset(leaf, slot);
2486 memmove_extent_buffer(leaf, ptr,
2487 (unsigned long)fi,
2488 offsetof(struct btrfs_file_extent_item,
2489 disk_bytenr));
2490 }
2491 }
2492
2493 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2494 data_end + size_diff, btrfs_leaf_data(leaf) +
2495 data_end, old_data_start - data_end);
2496
2497 offset = btrfs_disk_key_offset(&disk_key);
2498 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2499 btrfs_set_item_key(leaf, &disk_key, slot);
2500 if (slot == 0)
2501 fixup_low_keys(trans, root, path, &disk_key, 1);
2502 }
Chris Mason5f39d392007-10-15 16:14:19 -04002503
2504 item = btrfs_item_nr(leaf, slot);
2505 btrfs_set_item_size(leaf, item, new_size);
2506 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002507
2508 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002509 if (btrfs_leaf_free_space(root, leaf) < 0) {
2510 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002511 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002512 }
Chris Masonb18c6682007-04-17 13:26:50 -04002513 return ret;
2514}
2515
Chris Mason5f39d392007-10-15 16:14:19 -04002516int btrfs_extend_item(struct btrfs_trans_handle *trans,
2517 struct btrfs_root *root, struct btrfs_path *path,
2518 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002519{
2520 int ret = 0;
2521 int slot;
2522 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002523 struct extent_buffer *leaf;
2524 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002525 u32 nritems;
2526 unsigned int data_end;
2527 unsigned int old_data;
2528 unsigned int old_size;
2529 int i;
2530
2531 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002532 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002533
Chris Mason5f39d392007-10-15 16:14:19 -04002534 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002535 data_end = leaf_data_end(root, leaf);
2536
Chris Mason5f39d392007-10-15 16:14:19 -04002537 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2538 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002539 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002540 }
Chris Mason6567e832007-04-16 09:22:45 -04002541 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002542 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002543
2544 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002545 if (slot >= nritems) {
2546 btrfs_print_leaf(root, leaf);
2547 printk("slot %d too large, nritems %d\n", slot, nritems);
2548 BUG_ON(1);
2549 }
Chris Mason6567e832007-04-16 09:22:45 -04002550
2551 /*
2552 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2553 */
2554 /* first correct the data pointers */
2555 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002556 u32 ioff;
2557 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002558
2559 if (!leaf->map_token) {
2560 map_extent_buffer(leaf, (unsigned long)item,
2561 sizeof(struct btrfs_item),
2562 &leaf->map_token, &leaf->kaddr,
2563 &leaf->map_start, &leaf->map_len,
2564 KM_USER1);
2565 }
Chris Mason5f39d392007-10-15 16:14:19 -04002566 ioff = btrfs_item_offset(leaf, item);
2567 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002568 }
Chris Mason5f39d392007-10-15 16:14:19 -04002569
Chris Masondb945352007-10-15 16:15:53 -04002570 if (leaf->map_token) {
2571 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2572 leaf->map_token = NULL;
2573 }
2574
Chris Mason6567e832007-04-16 09:22:45 -04002575 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002576 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002577 data_end - data_size, btrfs_leaf_data(leaf) +
2578 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
Chris Mason6567e832007-04-16 09:22:45 -04002580 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002581 old_size = btrfs_item_size_nr(leaf, slot);
2582 item = btrfs_item_nr(leaf, slot);
2583 btrfs_set_item_size(leaf, item, old_size + data_size);
2584 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002585
2586 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002587 if (btrfs_leaf_free_space(root, leaf) < 0) {
2588 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002589 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002590 }
Chris Mason6567e832007-04-16 09:22:45 -04002591 return ret;
2592}
2593
Chris Mason74123bd2007-02-02 11:05:29 -05002594/*
2595 * Given a key and some data, insert an item into the tree.
2596 * This does all the path init required, making room in the tree if needed.
2597 */
Chris Mason9c583092008-01-29 15:15:18 -05002598int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002599 struct btrfs_root *root,
2600 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002601 struct btrfs_key *cpu_key, u32 *data_size,
2602 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002603{
Chris Mason5f39d392007-10-15 16:14:19 -04002604 struct extent_buffer *leaf;
2605 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002606 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002608 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002609 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002610 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002611 u32 total_size = 0;
2612 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002613 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002614 struct btrfs_disk_key disk_key;
2615
Chris Mason9c583092008-01-29 15:15:18 -05002616 for (i = 0; i < nr; i++) {
2617 total_data += data_size[i];
2618 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002619
Chris Mason9c583092008-01-29 15:15:18 -05002620 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2621 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002622 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002623 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002624 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002625 if (ret < 0)
2626 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002627
Chris Mason62e27492007-03-15 12:56:47 -04002628 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002629 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002630
Chris Mason5f39d392007-10-15 16:14:19 -04002631 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002632 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002633
Chris Mason123abc82007-03-14 14:14:43 -04002634 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002635 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002636 btrfs_print_leaf(root, leaf);
2637 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002638 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002639 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002640 }
Chris Mason5f39d392007-10-15 16:14:19 -04002641
Chris Mason62e27492007-03-15 12:56:47 -04002642 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002643 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002644
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 if (slot != nritems) {
2646 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002647 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648
Chris Mason5f39d392007-10-15 16:14:19 -04002649 if (old_data < data_end) {
2650 btrfs_print_leaf(root, leaf);
2651 printk("slot %d old_data %d data_end %d\n",
2652 slot, old_data, data_end);
2653 BUG_ON(1);
2654 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002655 /*
2656 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2657 */
2658 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002659 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002660 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002661 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002662
Chris Mason5f39d392007-10-15 16:14:19 -04002663 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002664 if (!leaf->map_token) {
2665 map_extent_buffer(leaf, (unsigned long)item,
2666 sizeof(struct btrfs_item),
2667 &leaf->map_token, &leaf->kaddr,
2668 &leaf->map_start, &leaf->map_len,
2669 KM_USER1);
2670 }
2671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002673 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002674 }
Chris Masondb945352007-10-15 16:15:53 -04002675 if (leaf->map_token) {
2676 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2677 leaf->map_token = NULL;
2678 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002679
2680 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002681 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002682 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002683 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002684
2685 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002686 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002687 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002688 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689 data_end = old_data;
2690 }
Chris Mason5f39d392007-10-15 16:14:19 -04002691
Chris Mason62e27492007-03-15 12:56:47 -04002692 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002693 for (i = 0; i < nr; i++) {
2694 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2695 btrfs_set_item_key(leaf, &disk_key, slot + i);
2696 item = btrfs_item_nr(leaf, slot + i);
2697 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2698 data_end -= data_size[i];
2699 btrfs_set_item_size(leaf, item, data_size[i]);
2700 }
2701 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002702 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002703
2704 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002705 if (slot == 0) {
2706 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002707 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002708 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002709
Chris Mason5f39d392007-10-15 16:14:19 -04002710 if (btrfs_leaf_free_space(root, leaf) < 0) {
2711 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002713 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002714out:
Chris Mason62e27492007-03-15 12:56:47 -04002715 return ret;
2716}
2717
2718/*
2719 * Given a key and some data, insert an item into the tree.
2720 * This does all the path init required, making room in the tree if needed.
2721 */
Chris Masone089f052007-03-16 16:20:31 -04002722int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2723 *root, struct btrfs_key *cpu_key, void *data, u32
2724 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002725{
2726 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002727 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002728 struct extent_buffer *leaf;
2729 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002730
Chris Mason2c90e5d2007-04-02 10:50:19 -04002731 path = btrfs_alloc_path();
2732 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002733 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002734 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002735 leaf = path->nodes[0];
2736 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2737 write_extent_buffer(leaf, data, ptr, data_size);
2738 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002739 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002740 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002741 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002742}
2743
Chris Mason74123bd2007-02-02 11:05:29 -05002744/*
Chris Mason5de08d72007-02-24 06:24:44 -05002745 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002746 *
2747 * If the delete empties a node, the node is removed from the tree,
2748 * continuing all the way the root if required. The root is converted into
2749 * a leaf if all the nodes are emptied.
2750 */
Chris Masone089f052007-03-16 16:20:31 -04002751static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2752 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002753{
Chris Mason5f39d392007-10-15 16:14:19 -04002754 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002755 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002756 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002757 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758
Chris Mason5f39d392007-10-15 16:14:19 -04002759 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002760 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002761 memmove_extent_buffer(parent,
2762 btrfs_node_key_ptr_offset(slot),
2763 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002764 sizeof(struct btrfs_key_ptr) *
2765 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002766 }
Chris Mason7518a232007-03-12 12:01:18 -04002767 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002768 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002769 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002770 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002771 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002772 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002773 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002774 struct btrfs_disk_key disk_key;
2775
2776 btrfs_node_key(parent, &disk_key, 0);
2777 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002778 if (wret)
2779 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002780 }
Chris Masond6025572007-03-30 14:27:56 -04002781 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002782 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002783}
2784
Chris Mason74123bd2007-02-02 11:05:29 -05002785/*
2786 * delete the item at the leaf level in path. If that empties
2787 * the leaf, remove it from the tree
2788 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002789int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2790 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002791{
Chris Mason5f39d392007-10-15 16:14:19 -04002792 struct extent_buffer *leaf;
2793 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002794 int last_off;
2795 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002796 int ret = 0;
2797 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002798 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002799 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002800
Chris Mason5f39d392007-10-15 16:14:19 -04002801 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002802 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2803
2804 for (i = 0; i < nr; i++)
2805 dsize += btrfs_item_size_nr(leaf, slot + i);
2806
Chris Mason5f39d392007-10-15 16:14:19 -04002807 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002808
Chris Mason85e21ba2008-01-29 15:11:36 -05002809 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002810 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002811 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002812
2813 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002814 data_end + dsize,
2815 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002816 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002817
Chris Mason85e21ba2008-01-29 15:11:36 -05002818 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002819 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002820
Chris Mason5f39d392007-10-15 16:14:19 -04002821 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002822 if (!leaf->map_token) {
2823 map_extent_buffer(leaf, (unsigned long)item,
2824 sizeof(struct btrfs_item),
2825 &leaf->map_token, &leaf->kaddr,
2826 &leaf->map_start, &leaf->map_len,
2827 KM_USER1);
2828 }
Chris Mason5f39d392007-10-15 16:14:19 -04002829 ioff = btrfs_item_offset(leaf, item);
2830 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002831 }
Chris Masondb945352007-10-15 16:15:53 -04002832
2833 if (leaf->map_token) {
2834 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2835 leaf->map_token = NULL;
2836 }
2837
Chris Mason5f39d392007-10-15 16:14:19 -04002838 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002839 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002840 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002841 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002842 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002843 btrfs_set_header_nritems(leaf, nritems - nr);
2844 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002845
Chris Mason74123bd2007-02-02 11:05:29 -05002846 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002847 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002848 if (leaf == root->node) {
2849 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002850 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002851 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002852 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002853 if (wret)
2854 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002855 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002856 leaf->start, leaf->len,
2857 btrfs_header_owner(path->nodes[1]),
2858 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002859 if (wret)
2860 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002861 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002862 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002863 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002864 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002865 struct btrfs_disk_key disk_key;
2866
2867 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002868 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002869 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002870 if (wret)
2871 ret = wret;
2872 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002873
Chris Mason74123bd2007-02-02 11:05:29 -05002874 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002875 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002876 /* push_leaf_left fixes the path.
2877 * make sure the path still points to our leaf
2878 * for possible call to del_ptr below
2879 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002880 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002881 extent_buffer_get(leaf);
2882
Chris Mason85e21ba2008-01-29 15:11:36 -05002883 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002884 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002885 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002886
2887 if (path->nodes[0] == leaf &&
2888 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002889 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002890 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002891 ret = wret;
2892 }
Chris Mason5f39d392007-10-15 16:14:19 -04002893
2894 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002895 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002896 u64 bytenr = leaf->start;
2897 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002898
Chris Mason7bb86312007-12-11 09:25:06 -05002899 root_gen = btrfs_header_generation(
2900 path->nodes[1]);
2901
Chris Masone089f052007-03-16 16:20:31 -04002902 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002903 if (wret)
2904 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002905
2906 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002907 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002908 blocksize,
2909 btrfs_header_owner(path->nodes[1]),
2910 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002911 if (wret)
2912 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002913 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002914 /* if we're still in the path, make sure
2915 * we're dirty. Otherwise, one of the
2916 * push_leaf functions must have already
2917 * dirtied this buffer
2918 */
2919 if (path->nodes[0] == leaf)
2920 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002921 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002922 }
Chris Masond5719762007-03-23 10:01:08 -04002923 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002924 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002925 }
2926 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002927 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002928}
2929
Chris Mason97571fd2007-02-24 13:39:08 -05002930/*
Chris Mason925baed2008-06-25 16:01:30 -04002931 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05002932 * returns 0 if it found something or 1 if there are no lesser leaves.
2933 * returns < 0 on io errors.
2934 */
2935int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2936{
Chris Mason925baed2008-06-25 16:01:30 -04002937 struct btrfs_key key;
2938 struct btrfs_disk_key found_key;
2939 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05002940
Chris Mason925baed2008-06-25 16:01:30 -04002941 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05002942
Chris Mason925baed2008-06-25 16:01:30 -04002943 if (key.offset > 0)
2944 key.offset--;
2945 else if (key.type > 0)
2946 key.type--;
2947 else if (key.objectid > 0)
2948 key.objectid--;
2949 else
2950 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002951
Chris Mason925baed2008-06-25 16:01:30 -04002952 btrfs_release_path(root, path);
2953 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2954 if (ret < 0)
2955 return ret;
2956 btrfs_item_key(path->nodes[0], &found_key, 0);
2957 ret = comp_keys(&found_key, &key);
2958 if (ret < 0)
2959 return 0;
2960 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002961}
2962
2963/*
Chris Mason925baed2008-06-25 16:01:30 -04002964 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05002965 * returns 0 if it found something or 1 if there are no greater leaves.
2966 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002967 */
Chris Mason234b63a2007-03-13 10:46:10 -04002968int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002969{
2970 int slot;
2971 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002972 struct extent_buffer *c;
2973 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04002974 struct btrfs_key key;
2975 u32 nritems;
2976 int ret;
2977
2978 nritems = btrfs_header_nritems(path->nodes[0]);
2979 if (nritems == 0) {
2980 return 1;
2981 }
2982
2983 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
2984
Chris Mason925baed2008-06-25 16:01:30 -04002985 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04002986 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04002987 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2988 path->keep_locks = 0;
2989
2990 if (ret < 0)
2991 return ret;
2992
Chris Masona2135012008-06-25 16:01:30 -04002993 nritems = btrfs_header_nritems(path->nodes[0]);
2994 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002995 goto done;
2996 }
Chris Masond97e63b2007-02-20 16:40:44 -05002997
Chris Mason234b63a2007-03-13 10:46:10 -04002998 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002999 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003000 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003001
Chris Masond97e63b2007-02-20 16:40:44 -05003002 slot = path->slots[level] + 1;
3003 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003004 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003005 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003006 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003007 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003008 }
Chris Masond97e63b2007-02-20 16:40:44 -05003009 continue;
3010 }
Chris Mason5f39d392007-10-15 16:14:19 -04003011
Chris Mason925baed2008-06-25 16:01:30 -04003012 if (next) {
3013 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003014 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003015 }
Chris Mason5f39d392007-10-15 16:14:19 -04003016
Chris Mason925baed2008-06-25 16:01:30 -04003017 if (level == 1 && path->locks[1] && path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003018 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003019
Chris Masonca7a79a2008-05-12 12:59:19 -04003020 next = read_node_slot(root, c, slot);
Chris Mason051e1b92008-06-25 16:01:30 -04003021 WARN_ON(!btrfs_tree_locked(c));
3022 btrfs_tree_lock(next);
Chris Masond97e63b2007-02-20 16:40:44 -05003023 break;
3024 }
3025 path->slots[level] = slot;
3026 while(1) {
3027 level--;
3028 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003029 if (path->locks[level])
3030 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003031 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003032 path->nodes[level] = next;
3033 path->slots[level] = 0;
Chris Mason925baed2008-06-25 16:01:30 -04003034 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003035 if (!level)
3036 break;
Chris Mason925baed2008-06-25 16:01:30 -04003037 if (level == 1 && path->locks[1] && path->reada)
3038 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003039 next = read_node_slot(root, next, 0);
Chris Mason051e1b92008-06-25 16:01:30 -04003040 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3041 btrfs_tree_lock(next);
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