blob: ab4ac0365c7f58c46e986b3441b9ef6f865bcd24 [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
Chris Mason234b63a2007-03-13 10:46:10 -040067 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040068 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050069 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040070 continue;
71 if (p->locks[i]) {
72 btrfs_tree_unlock(p->nodes[i]);
73 p->locks[i] = 0;
74 }
Chris Mason5f39d392007-10-15 16:14:19 -040075 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040076 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050077 }
78}
79
Chris Mason925baed2008-06-25 16:01:30 -040080struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
81{
82 struct extent_buffer *eb;
83 spin_lock(&root->node_lock);
84 eb = root->node;
85 extent_buffer_get(eb);
86 spin_unlock(&root->node_lock);
87 return eb;
88}
89
90struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
91{
92 struct extent_buffer *eb;
93
94 while(1) {
95 eb = btrfs_root_node(root);
96 btrfs_tree_lock(eb);
97
98 spin_lock(&root->node_lock);
99 if (eb == root->node) {
100 spin_unlock(&root->node_lock);
101 break;
102 }
103 spin_unlock(&root->node_lock);
104
105 btrfs_tree_unlock(eb);
106 free_extent_buffer(eb);
107 }
108 return eb;
109}
110
Chris Mason0b86a832008-03-24 15:01:56 -0400111static void add_root_to_dirty_list(struct btrfs_root *root)
112{
113 if (root->track_dirty && list_empty(&root->dirty_list)) {
114 list_add(&root->dirty_list,
115 &root->fs_info->dirty_cowonly_roots);
116 }
117}
118
Chris Masonbe20aa92007-12-17 20:14:01 -0500119int btrfs_copy_root(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 struct extent_buffer *buf,
122 struct extent_buffer **cow_ret, u64 new_root_objectid)
123{
124 struct extent_buffer *cow;
125 u32 nritems;
126 int ret = 0;
127 int level;
128 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -0500129 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500130
Chris Mason4aec2b52007-12-18 16:25:45 -0500131 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
132 if (!new_root)
133 return -ENOMEM;
134
135 memcpy(new_root, root, sizeof(*new_root));
136 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500137
138 WARN_ON(root->ref_cows && trans->transid !=
139 root->fs_info->running_transaction->transid);
140 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
141
142 level = btrfs_header_level(buf);
143 nritems = btrfs_header_nritems(buf);
144 if (nritems) {
145 if (level == 0)
146 btrfs_item_key_to_cpu(buf, &first_key, 0);
147 else
148 btrfs_node_key_to_cpu(buf, &first_key, 0);
149 } else {
150 first_key.objectid = 0;
151 }
Chris Mason925baed2008-06-25 16:01:30 -0400152 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500153 new_root_objectid,
154 trans->transid, first_key.objectid,
155 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500156 if (IS_ERR(cow)) {
157 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500158 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500159 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500160
161 copy_extent_buffer(cow, buf, 0, 0, cow->len);
162 btrfs_set_header_bytenr(cow, cow->start);
163 btrfs_set_header_generation(cow, trans->transid);
164 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400165 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500166
167 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500168 ret = btrfs_inc_ref(trans, new_root, buf);
169 kfree(new_root);
170
Chris Masonbe20aa92007-12-17 20:14:01 -0500171 if (ret)
172 return ret;
173
174 btrfs_mark_buffer_dirty(cow);
175 *cow_ret = cow;
176 return 0;
177}
178
179int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400180 struct btrfs_root *root,
181 struct extent_buffer *buf,
182 struct extent_buffer *parent, int parent_slot,
183 struct extent_buffer **cow_ret,
184 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400185{
Chris Mason7bb86312007-12-11 09:25:06 -0500186 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400187 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500188 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400189 int ret = 0;
190 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500191 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400192 int unlock_orig = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500193 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400194
Chris Mason925baed2008-06-25 16:01:30 -0400195 if (*cow_ret == buf)
196 unlock_orig = 1;
197
198 WARN_ON(!btrfs_tree_locked(buf));
199
Chris Mason7bb86312007-12-11 09:25:06 -0500200 if (root->ref_cows) {
201 root_gen = trans->transid;
202 } else {
203 root_gen = 0;
204 }
Chris Mason7bb86312007-12-11 09:25:06 -0500205 WARN_ON(root->ref_cows && trans->transid !=
206 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400207 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400208
Chris Mason7bb86312007-12-11 09:25:06 -0500209 level = btrfs_header_level(buf);
210 nritems = btrfs_header_nritems(buf);
211 if (nritems) {
212 if (level == 0)
213 btrfs_item_key_to_cpu(buf, &first_key, 0);
214 else
215 btrfs_node_key_to_cpu(buf, &first_key, 0);
216 } else {
217 first_key.objectid = 0;
218 }
Chris Mason925baed2008-06-25 16:01:30 -0400219 cow = btrfs_alloc_free_block(trans, root, buf->len,
Chris Mason7bb86312007-12-11 09:25:06 -0500220 root->root_key.objectid,
221 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400222 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400223 if (IS_ERR(cow))
224 return PTR_ERR(cow);
225
Chris Mason5f39d392007-10-15 16:14:19 -0400226 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400227 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400228 btrfs_set_header_generation(cow, trans->transid);
229 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400230 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400231
Chris Mason5f39d392007-10-15 16:14:19 -0400232 WARN_ON(btrfs_header_generation(buf) > trans->transid);
233 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400234 different_trans = 1;
235 ret = btrfs_inc_ref(trans, root, buf);
236 if (ret)
237 return ret;
238 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400239 clean_tree_block(trans, root, buf);
240 }
241
242 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400243 WARN_ON(parent && parent != buf);
Chris Mason7bb86312007-12-11 09:25:06 -0500244 root_gen = btrfs_header_generation(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400245
246 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400247 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400248 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400249 spin_unlock(&root->node_lock);
250
Chris Mason6702ed42007-08-07 16:15:09 -0400251 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400252 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500253 buf->len, root->root_key.objectid,
254 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400255 }
Chris Mason5f39d392007-10-15 16:14:19 -0400256 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400257 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400258 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500259 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400260 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400261 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500262 WARN_ON(trans->transid == 0);
263 btrfs_set_node_ptr_generation(parent, parent_slot,
264 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400265 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400266 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500267 btrfs_free_extent(trans, root, buf->start, buf->len,
268 btrfs_header_owner(parent), root_gen,
269 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400270 }
Chris Mason925baed2008-06-25 16:01:30 -0400271 if (unlock_orig)
272 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400273 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400274 btrfs_mark_buffer_dirty(cow);
275 *cow_ret = cow;
276 return 0;
277}
278
Chris Mason5f39d392007-10-15 16:14:19 -0400279int btrfs_cow_block(struct btrfs_trans_handle *trans,
280 struct btrfs_root *root, struct extent_buffer *buf,
281 struct extent_buffer *parent, int parent_slot,
282 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500283{
Chris Mason6702ed42007-08-07 16:15:09 -0400284 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500285 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400286 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500287
Chris Masonccd467d2007-06-28 15:57:36 -0400288 if (trans->transaction != root->fs_info->running_transaction) {
289 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
290 root->fs_info->running_transaction->transid);
291 WARN_ON(1);
292 }
293 if (trans->transid != root->fs_info->generation) {
294 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
295 root->fs_info->generation);
296 WARN_ON(1);
297 }
Chris Masondc17ff82008-01-08 15:46:30 -0500298
299 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400300 spin_lock(&root->fs_info->hash_lock);
301 if (header_trans == trans->transid &&
302 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500303 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400304 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500305 return 0;
306 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400307 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400308 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400309 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400310 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400311 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400312}
313
Chris Mason6b800532007-10-15 16:17:34 -0400314static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400315{
Chris Mason6b800532007-10-15 16:17:34 -0400316 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400317 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400318 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400319 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500320 return 0;
321}
322
Chris Mason081e9572007-11-06 10:26:24 -0500323/*
324 * compare two keys in a memcmp fashion
325 */
326static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
327{
328 struct btrfs_key k1;
329
330 btrfs_disk_key_to_cpu(&k1, disk);
331
332 if (k1.objectid > k2->objectid)
333 return 1;
334 if (k1.objectid < k2->objectid)
335 return -1;
336 if (k1.type > k2->type)
337 return 1;
338 if (k1.type < k2->type)
339 return -1;
340 if (k1.offset > k2->offset)
341 return 1;
342 if (k1.offset < k2->offset)
343 return -1;
344 return 0;
345}
346
347
Chris Mason6702ed42007-08-07 16:15:09 -0400348int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400349 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400350 int start_slot, int cache_only, u64 *last_ret,
351 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400352{
Chris Mason6b800532007-10-15 16:17:34 -0400353 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400354 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400355 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400356 u64 search_start = *last_ret;
357 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400358 u64 other;
359 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400360 int end_slot;
361 int i;
362 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400363 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400364 int uptodate;
365 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500366 int progress_passed = 0;
367 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400368
Chris Mason5708b952007-10-25 15:43:18 -0400369 parent_level = btrfs_header_level(parent);
370 if (cache_only && parent_level != 1)
371 return 0;
372
Chris Mason6702ed42007-08-07 16:15:09 -0400373 if (trans->transaction != root->fs_info->running_transaction) {
374 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
375 root->fs_info->running_transaction->transid);
376 WARN_ON(1);
377 }
378 if (trans->transid != root->fs_info->generation) {
379 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
380 root->fs_info->generation);
381 WARN_ON(1);
382 }
Chris Mason86479a02007-09-10 19:58:16 -0400383
Chris Mason6b800532007-10-15 16:17:34 -0400384 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400385 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400386 end_slot = parent_nritems;
387
388 if (parent_nritems == 1)
389 return 0;
390
391 for (i = start_slot; i < end_slot; i++) {
392 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400393
Chris Mason5708b952007-10-25 15:43:18 -0400394 if (!parent->map_token) {
395 map_extent_buffer(parent,
396 btrfs_node_key_ptr_offset(i),
397 sizeof(struct btrfs_key_ptr),
398 &parent->map_token, &parent->kaddr,
399 &parent->map_start, &parent->map_len,
400 KM_USER1);
401 }
Chris Mason081e9572007-11-06 10:26:24 -0500402 btrfs_node_key(parent, &disk_key, i);
403 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
404 continue;
405
406 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400407 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400408 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400409 if (last_block == 0)
410 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400411
Chris Mason6702ed42007-08-07 16:15:09 -0400412 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400413 other = btrfs_node_blockptr(parent, i - 1);
414 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400415 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400416 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400417 other = btrfs_node_blockptr(parent, i + 1);
418 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400419 }
Chris Masone9d0b132007-08-10 14:06:19 -0400420 if (close) {
421 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400422 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400423 }
Chris Mason5708b952007-10-25 15:43:18 -0400424 if (parent->map_token) {
425 unmap_extent_buffer(parent, parent->map_token,
426 KM_USER1);
427 parent->map_token = NULL;
428 }
Chris Mason6702ed42007-08-07 16:15:09 -0400429
Chris Mason6b800532007-10-15 16:17:34 -0400430 cur = btrfs_find_tree_block(root, blocknr, blocksize);
431 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400432 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400433 else
434 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400435 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400436 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400437 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400438 continue;
439 }
Chris Mason6b800532007-10-15 16:17:34 -0400440 if (!cur) {
441 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400442 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400443 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400444 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400445 }
Chris Mason6702ed42007-08-07 16:15:09 -0400446 }
Chris Masone9d0b132007-08-10 14:06:19 -0400447 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400448 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400449
Chris Masone7a84562008-06-25 16:01:31 -0400450 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400451 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400452 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400453 min(16 * blocksize,
454 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400455 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400456 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400457 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400458 break;
Yan252c38f2007-08-29 09:11:44 -0400459 }
Chris Masone7a84562008-06-25 16:01:31 -0400460 search_start = cur->start;
461 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400462 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400463 btrfs_tree_unlock(cur);
464 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400465 }
Chris Mason5708b952007-10-25 15:43:18 -0400466 if (parent->map_token) {
467 unmap_extent_buffer(parent, parent->map_token,
468 KM_USER1);
469 parent->map_token = NULL;
470 }
Chris Mason6702ed42007-08-07 16:15:09 -0400471 return err;
472}
473
Chris Mason74123bd2007-02-02 11:05:29 -0500474/*
475 * The leaf data grows from end-to-front in the node.
476 * this returns the address of the start of the last item,
477 * which is the stop of the leaf data stack
478 */
Chris Mason123abc82007-03-14 14:14:43 -0400479static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400480 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500481{
Chris Mason5f39d392007-10-15 16:14:19 -0400482 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500483 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400484 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400485 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500486}
487
Chris Mason123abc82007-03-14 14:14:43 -0400488static int check_node(struct btrfs_root *root, struct btrfs_path *path,
489 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500490{
Chris Mason5f39d392007-10-15 16:14:19 -0400491 struct extent_buffer *parent = NULL;
492 struct extent_buffer *node = path->nodes[level];
493 struct btrfs_disk_key parent_key;
494 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500495 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400496 int slot;
497 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400498 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500499
500 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400501 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400502
Chris Mason8d7be552007-05-10 11:24:42 -0400503 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400504 BUG_ON(nritems == 0);
505 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400506 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400507 btrfs_node_key(parent, &parent_key, parent_slot);
508 btrfs_node_key(node, &node_key, 0);
509 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400510 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400511 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400512 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500513 }
Chris Mason123abc82007-03-14 14:14:43 -0400514 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400515 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400516 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
517 btrfs_node_key(node, &node_key, slot);
518 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400519 }
520 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400521 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
522 btrfs_node_key(node, &node_key, slot);
523 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500524 }
525 return 0;
526}
527
Chris Mason123abc82007-03-14 14:14:43 -0400528static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
529 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500530{
Chris Mason5f39d392007-10-15 16:14:19 -0400531 struct extent_buffer *leaf = path->nodes[level];
532 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500533 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400534 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400535 struct btrfs_disk_key parent_key;
536 struct btrfs_disk_key leaf_key;
537 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400538
Chris Mason5f39d392007-10-15 16:14:19 -0400539 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500540
541 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400542 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400543
544 if (nritems == 0)
545 return 0;
546
547 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400548 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400549 btrfs_node_key(parent, &parent_key, parent_slot);
550 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400551
Chris Mason5f39d392007-10-15 16:14:19 -0400552 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400553 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400554 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400555 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500556 }
Chris Mason5f39d392007-10-15 16:14:19 -0400557#if 0
558 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
559 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
560 btrfs_item_key(leaf, &leaf_key, i);
561 if (comp_keys(&leaf_key, &cpukey) >= 0) {
562 btrfs_print_leaf(root, leaf);
563 printk("slot %d offset bad key\n", i);
564 BUG_ON(1);
565 }
566 if (btrfs_item_offset_nr(leaf, i) !=
567 btrfs_item_end_nr(leaf, i + 1)) {
568 btrfs_print_leaf(root, leaf);
569 printk("slot %d offset bad\n", i);
570 BUG_ON(1);
571 }
572 if (i == 0) {
573 if (btrfs_item_offset_nr(leaf, i) +
574 btrfs_item_size_nr(leaf, i) !=
575 BTRFS_LEAF_DATA_SIZE(root)) {
576 btrfs_print_leaf(root, leaf);
577 printk("slot %d first offset bad\n", i);
578 BUG_ON(1);
579 }
580 }
581 }
582 if (nritems > 0) {
583 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
584 btrfs_print_leaf(root, leaf);
585 printk("slot %d bad size \n", nritems - 1);
586 BUG_ON(1);
587 }
588 }
589#endif
590 if (slot != 0 && slot < nritems - 1) {
591 btrfs_item_key(leaf, &leaf_key, slot);
592 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
593 if (comp_keys(&leaf_key, &cpukey) <= 0) {
594 btrfs_print_leaf(root, leaf);
595 printk("slot %d offset bad key\n", slot);
596 BUG_ON(1);
597 }
598 if (btrfs_item_offset_nr(leaf, slot - 1) !=
599 btrfs_item_end_nr(leaf, slot)) {
600 btrfs_print_leaf(root, leaf);
601 printk("slot %d offset bad\n", slot);
602 BUG_ON(1);
603 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500604 }
Chris Mason8d7be552007-05-10 11:24:42 -0400605 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400606 btrfs_item_key(leaf, &leaf_key, slot);
607 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
608 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
609 if (btrfs_item_offset_nr(leaf, slot) !=
610 btrfs_item_end_nr(leaf, slot + 1)) {
611 btrfs_print_leaf(root, leaf);
612 printk("slot %d offset bad\n", slot);
613 BUG_ON(1);
614 }
Chris Mason8d7be552007-05-10 11:24:42 -0400615 }
Chris Mason5f39d392007-10-15 16:14:19 -0400616 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
617 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500618 return 0;
619}
620
Chris Mason98ed5172008-01-03 10:01:48 -0500621static int noinline check_block(struct btrfs_root *root,
622 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500623{
Chris Masonf1885912008-04-09 16:28:12 -0400624 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400625 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400626 if (btrfs_header_level(path->nodes[level]) != level)
627 printk("warning: bad level %Lu wanted %d found %d\n",
628 path->nodes[level]->start, level,
629 btrfs_header_level(path->nodes[level]));
630 found_start = btrfs_header_bytenr(path->nodes[level]);
631 if (found_start != path->nodes[level]->start) {
632 printk("warning: bad bytentr %Lu found %Lu\n",
633 path->nodes[level]->start, found_start);
634 }
Chris Masondb945352007-10-15 16:15:53 -0400635#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400636 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400637
Chris Mason479965d2007-10-15 16:14:27 -0400638 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
639 (unsigned long)btrfs_header_fsid(buf),
640 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400641 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400642 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400643 }
Chris Masondb945352007-10-15 16:15:53 -0400644#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500645 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400646 return check_leaf(root, path, level);
647 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500648}
649
Chris Mason74123bd2007-02-02 11:05:29 -0500650/*
Chris Mason5f39d392007-10-15 16:14:19 -0400651 * search for key in the extent_buffer. The items start at offset p,
652 * and they are item_size apart. There are 'max' items in p.
653 *
Chris Mason74123bd2007-02-02 11:05:29 -0500654 * the slot in the array is returned via slot, and it points to
655 * the place where you would insert key if it is not found in
656 * the array.
657 *
658 * slot may point to max if the key is bigger than all of the keys
659 */
Chris Mason5f39d392007-10-15 16:14:19 -0400660static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
661 int item_size, struct btrfs_key *key,
662 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500663{
664 int low = 0;
665 int high = max;
666 int mid;
667 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400668 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400669 struct btrfs_disk_key unaligned;
670 unsigned long offset;
671 char *map_token = NULL;
672 char *kaddr = NULL;
673 unsigned long map_start = 0;
674 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400675 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500676
677 while(low < high) {
678 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400679 offset = p + mid * item_size;
680
681 if (!map_token || offset < map_start ||
682 (offset + sizeof(struct btrfs_disk_key)) >
683 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400684 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400685 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400686 map_token = NULL;
687 }
688 err = map_extent_buffer(eb, offset,
689 sizeof(struct btrfs_disk_key),
690 &map_token, &kaddr,
691 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400692
Chris Mason479965d2007-10-15 16:14:27 -0400693 if (!err) {
694 tmp = (struct btrfs_disk_key *)(kaddr + offset -
695 map_start);
696 } else {
697 read_extent_buffer(eb, &unaligned,
698 offset, sizeof(unaligned));
699 tmp = &unaligned;
700 }
701
Chris Mason5f39d392007-10-15 16:14:19 -0400702 } else {
703 tmp = (struct btrfs_disk_key *)(kaddr + offset -
704 map_start);
705 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500706 ret = comp_keys(tmp, key);
707
708 if (ret < 0)
709 low = mid + 1;
710 else if (ret > 0)
711 high = mid;
712 else {
713 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400714 if (map_token)
715 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500716 return 0;
717 }
718 }
719 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400720 if (map_token)
721 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500722 return 1;
723}
724
Chris Mason97571fd2007-02-24 13:39:08 -0500725/*
726 * simple bin_search frontend that does the right thing for
727 * leaves vs nodes
728 */
Chris Mason5f39d392007-10-15 16:14:19 -0400729static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
730 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731{
Chris Mason5f39d392007-10-15 16:14:19 -0400732 if (level == 0) {
733 return generic_bin_search(eb,
734 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400735 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400736 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400737 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500738 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400739 return generic_bin_search(eb,
740 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400741 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400742 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400743 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500744 }
745 return -1;
746}
747
Chris Mason5f39d392007-10-15 16:14:19 -0400748static struct extent_buffer *read_node_slot(struct btrfs_root *root,
749 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500750{
Chris Masonca7a79a2008-05-12 12:59:19 -0400751 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500752 if (slot < 0)
753 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400754 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500755 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400756
757 BUG_ON(level == 0);
758
Chris Masondb945352007-10-15 16:15:53 -0400759 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400760 btrfs_level_size(root, level - 1),
761 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500762}
763
Chris Mason98ed5172008-01-03 10:01:48 -0500764static int balance_level(struct btrfs_trans_handle *trans,
765 struct btrfs_root *root,
766 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500767{
Chris Mason5f39d392007-10-15 16:14:19 -0400768 struct extent_buffer *right = NULL;
769 struct extent_buffer *mid;
770 struct extent_buffer *left = NULL;
771 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500772 int ret = 0;
773 int wret;
774 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500775 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400776 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500777 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500778
779 if (level == 0)
780 return 0;
781
Chris Mason5f39d392007-10-15 16:14:19 -0400782 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400783 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500784 WARN_ON(btrfs_header_generation(mid) != trans->transid);
785
Chris Mason1d4f8a02007-03-13 09:28:32 -0400786 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500787
Chris Mason234b63a2007-03-13 10:46:10 -0400788 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400789 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500790 pslot = path->slots[level + 1];
791
Chris Mason40689472007-03-17 14:29:23 -0400792 /*
793 * deal with the case where there is only one pointer in the root
794 * by promoting the node below to a root
795 */
Chris Mason5f39d392007-10-15 16:14:19 -0400796 if (!parent) {
797 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500798
Chris Mason5f39d392007-10-15 16:14:19 -0400799 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500800 return 0;
801
802 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400803 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400804 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500805 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500806 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
807 BUG_ON(ret);
808
Chris Mason925baed2008-06-25 16:01:30 -0400809 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500810 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400811 spin_unlock(&root->node_lock);
812
Chris Mason0b86a832008-03-24 15:01:56 -0400813 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400814 btrfs_tree_unlock(child);
815 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500816 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400817 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400818 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500819 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400820 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500821 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
822 root->root_key.objectid,
823 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500824 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400825 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400826 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500827 }
Chris Mason5f39d392007-10-15 16:14:19 -0400828 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400829 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500830 return 0;
831
Chris Mason5f39d392007-10-15 16:14:19 -0400832 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400833 err_on_enospc = 1;
834
Chris Mason5f39d392007-10-15 16:14:19 -0400835 left = read_node_slot(root, parent, pslot - 1);
836 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400837 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400838 wret = btrfs_cow_block(trans, root, left,
839 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400840 if (wret) {
841 ret = wret;
842 goto enospc;
843 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400844 }
Chris Mason5f39d392007-10-15 16:14:19 -0400845 right = read_node_slot(root, parent, pslot + 1);
846 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400847 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400848 wret = btrfs_cow_block(trans, root, right,
849 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400850 if (wret) {
851 ret = wret;
852 goto enospc;
853 }
854 }
855
856 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400857 if (left) {
858 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400859 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500860 if (wret < 0)
861 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400862 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400863 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500864 }
Chris Mason79f95c82007-03-01 15:16:26 -0500865
866 /*
867 * then try to empty the right most buffer into the middle
868 */
Chris Mason5f39d392007-10-15 16:14:19 -0400869 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400870 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400871 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500872 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400873 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400874 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500875 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400876 u32 blocksize = right->len;
877
Chris Mason5f39d392007-10-15 16:14:19 -0400878 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400879 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400880 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500881 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400882 wret = del_ptr(trans, root, path, level + 1, pslot +
883 1);
Chris Masonbb803952007-03-01 12:04:21 -0500884 if (wret)
885 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400886 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500887 blocksize,
888 btrfs_header_owner(parent),
889 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500890 if (wret)
891 ret = wret;
892 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400893 struct btrfs_disk_key right_key;
894 btrfs_node_key(right, &right_key, 0);
895 btrfs_set_node_key(parent, &right_key, pslot + 1);
896 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500897 }
898 }
Chris Mason5f39d392007-10-15 16:14:19 -0400899 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500900 /*
901 * we're not allowed to leave a node with one item in the
902 * tree during a delete. A deletion from lower in the tree
903 * could try to delete the only pointer in this node.
904 * So, pull some keys from the left.
905 * There has to be a left pointer at this point because
906 * otherwise we would have pulled some pointers from the
907 * right
908 */
Chris Mason5f39d392007-10-15 16:14:19 -0400909 BUG_ON(!left);
910 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400911 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500912 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400913 goto enospc;
914 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400915 if (wret == 1) {
916 wret = push_node_left(trans, root, left, mid, 1);
917 if (wret < 0)
918 ret = wret;
919 }
Chris Mason79f95c82007-03-01 15:16:26 -0500920 BUG_ON(wret == 1);
921 }
Chris Mason5f39d392007-10-15 16:14:19 -0400922 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500923 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500924 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400925 u64 bytenr = mid->start;
926 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400927
Chris Mason5f39d392007-10-15 16:14:19 -0400928 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400929 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400930 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500931 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400932 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500933 if (wret)
934 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500935 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
936 btrfs_header_owner(parent),
937 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500938 if (wret)
939 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500940 } else {
941 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400942 struct btrfs_disk_key mid_key;
943 btrfs_node_key(mid, &mid_key, 0);
944 btrfs_set_node_key(parent, &mid_key, pslot);
945 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500946 }
Chris Masonbb803952007-03-01 12:04:21 -0500947
Chris Mason79f95c82007-03-01 15:16:26 -0500948 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (left) {
950 if (btrfs_header_nritems(left) > orig_slot) {
951 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400952 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400953 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500954 path->slots[level + 1] -= 1;
955 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400956 if (mid) {
957 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400959 }
Chris Masonbb803952007-03-01 12:04:21 -0500960 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400961 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500962 path->slots[level] = orig_slot;
963 }
964 }
Chris Mason79f95c82007-03-01 15:16:26 -0500965 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400966 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400967 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400968 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500969 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400970enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400971 if (right) {
972 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400973 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400974 }
975 if (left) {
976 if (path->nodes[level] != left)
977 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400978 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400979 }
Chris Masonbb803952007-03-01 12:04:21 -0500980 return ret;
981}
982
Chris Masone66f7092007-04-20 13:16:02 -0400983/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500984static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
985 struct btrfs_root *root,
986 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400987{
Chris Mason5f39d392007-10-15 16:14:19 -0400988 struct extent_buffer *right = NULL;
989 struct extent_buffer *mid;
990 struct extent_buffer *left = NULL;
991 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400992 int ret = 0;
993 int wret;
994 int pslot;
995 int orig_slot = path->slots[level];
996 u64 orig_ptr;
997
998 if (level == 0)
999 return 1;
1000
Chris Mason5f39d392007-10-15 16:14:19 -04001001 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001002 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001003 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1004
1005 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001006 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001007 pslot = path->slots[level + 1];
1008
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001010 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001011
Chris Mason5f39d392007-10-15 16:14:19 -04001012 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001013
1014 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001015 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001016 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001017
1018 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001019 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001020 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1021 wret = 1;
1022 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001023 ret = btrfs_cow_block(trans, root, left, parent,
1024 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001025 if (ret)
1026 wret = 1;
1027 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001028 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001029 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001030 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001031 }
Chris Masone66f7092007-04-20 13:16:02 -04001032 if (wret < 0)
1033 ret = wret;
1034 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001035 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001036 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001037 btrfs_node_key(mid, &disk_key, 0);
1038 btrfs_set_node_key(parent, &disk_key, pslot);
1039 btrfs_mark_buffer_dirty(parent);
1040 if (btrfs_header_nritems(left) > orig_slot) {
1041 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001042 path->slots[level + 1] -= 1;
1043 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001044 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001045 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001046 } else {
1047 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001048 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001049 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001050 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001051 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001052 }
Chris Masone66f7092007-04-20 13:16:02 -04001053 return 0;
1054 }
Chris Mason925baed2008-06-25 16:01:30 -04001055 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001057 }
Chris Mason925baed2008-06-25 16:01:30 -04001058 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001059
1060 /*
1061 * then try to empty the right most buffer into the middle
1062 */
Chris Mason5f39d392007-10-15 16:14:19 -04001063 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001064 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001065 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001067 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1068 wret = 1;
1069 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001070 ret = btrfs_cow_block(trans, root, right,
1071 parent, pslot + 1,
1072 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001073 if (ret)
1074 wret = 1;
1075 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001076 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001077 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001078 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001079 }
Chris Masone66f7092007-04-20 13:16:02 -04001080 if (wret < 0)
1081 ret = wret;
1082 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001083 struct btrfs_disk_key disk_key;
1084
1085 btrfs_node_key(right, &disk_key, 0);
1086 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1087 btrfs_mark_buffer_dirty(parent);
1088
1089 if (btrfs_header_nritems(mid) <= orig_slot) {
1090 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001091 path->slots[level + 1] += 1;
1092 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001093 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001094 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001096 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001097 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001098 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001099 }
Chris Masone66f7092007-04-20 13:16:02 -04001100 return 0;
1101 }
Chris Mason925baed2008-06-25 16:01:30 -04001102 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001104 }
Chris Masone66f7092007-04-20 13:16:02 -04001105 return 1;
1106}
1107
Chris Mason74123bd2007-02-02 11:05:29 -05001108/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001109 * readahead one full node of leaves
1110 */
1111static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001112 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001113{
Chris Mason5f39d392007-10-15 16:14:19 -04001114 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001115 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001116 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001117 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001118 u64 lowest_read;
1119 u64 highest_read;
1120 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001121 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001122 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001123 u32 nr;
1124 u32 blocksize;
1125 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001126
Chris Masona6b6e752007-10-15 16:22:39 -04001127 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001128 return;
1129
Chris Mason6702ed42007-08-07 16:15:09 -04001130 if (!path->nodes[level])
1131 return;
1132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001134
Chris Mason3c69fae2007-08-07 15:52:22 -04001135 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001136 blocksize = btrfs_level_size(root, level - 1);
1137 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001138 if (eb) {
1139 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001140 return;
1141 }
1142
Chris Mason6b800532007-10-15 16:17:34 -04001143 highest_read = search;
1144 lowest_read = search;
1145
Chris Mason5f39d392007-10-15 16:14:19 -04001146 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001147 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001148 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001149 if (direction < 0) {
1150 if (nr == 0)
1151 break;
1152 nr--;
1153 } else if (direction > 0) {
1154 nr++;
1155 if (nr >= nritems)
1156 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001157 }
Chris Mason01f46652007-12-21 16:24:26 -05001158 if (path->reada < 0 && objectid) {
1159 btrfs_node_key(node, &disk_key, nr);
1160 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1161 break;
1162 }
Chris Mason6b800532007-10-15 16:17:34 -04001163 search = btrfs_node_blockptr(node, nr);
1164 if ((search >= lowest_read && search <= highest_read) ||
1165 (search < lowest_read && lowest_read - search <= 32768) ||
1166 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001167 readahead_tree_block(root, search, blocksize,
1168 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001169 nread += blocksize;
1170 }
1171 nscan++;
1172 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1173 break;
1174 if(nread > (1024 * 1024) || nscan > 128)
1175 break;
1176
1177 if (search < lowest_read)
1178 lowest_read = search;
1179 if (search > highest_read)
1180 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001181 }
1182}
Chris Mason925baed2008-06-25 16:01:30 -04001183
1184static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1185{
1186 int i;
1187 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001188 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001189 struct extent_buffer *t;
1190
1191 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1192 if (!path->nodes[i])
1193 break;
1194 if (!path->locks[i])
1195 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001196 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001197 skip_level = i + 1;
1198 continue;
1199 }
Chris Mason051e1b92008-06-25 16:01:30 -04001200 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001201 u32 nritems;
1202 t = path->nodes[i];
1203 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001204 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001205 skip_level = i + 1;
1206 continue;
1207 }
1208 }
Chris Mason051e1b92008-06-25 16:01:30 -04001209 if (skip_level < i && i >= lowest_unlock)
1210 no_skips = 1;
1211
Chris Mason925baed2008-06-25 16:01:30 -04001212 t = path->nodes[i];
1213 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1214 btrfs_tree_unlock(t);
1215 path->locks[i] = 0;
1216 }
1217 }
1218}
1219
Chris Mason3c69fae2007-08-07 15:52:22 -04001220/*
Chris Mason74123bd2007-02-02 11:05:29 -05001221 * look for key in the tree. path is filled in with nodes along the way
1222 * if key is found, we return zero and you can find the item in the leaf
1223 * level of the path (level 0)
1224 *
1225 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001226 * be inserted, and 1 is returned. If there are other errors during the
1227 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001228 *
1229 * if ins_len > 0, nodes and leaves will be split as we walk down the
1230 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1231 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001232 */
Chris Masone089f052007-03-16 16:20:31 -04001233int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1234 *root, struct btrfs_key *key, struct btrfs_path *p, int
1235 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236{
Chris Mason5f39d392007-10-15 16:14:19 -04001237 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001238 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001239 int slot;
1240 int ret;
1241 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001242 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001243 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001244 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001245 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001246 u64 blocknr;
1247 u64 gen;
Chris Mason9f3a7422007-08-07 15:52:19 -04001248
Chris Mason6702ed42007-08-07 16:15:09 -04001249 lowest_level = p->lowest_level;
1250 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001251 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001252 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001253 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001254 if (ins_len < 0)
1255 lowest_unlock = 2;
Chris Masonbb803952007-03-01 12:04:21 -05001256again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001257 if (p->skip_locking)
1258 b = btrfs_root_node(root);
1259 else
1260 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001261
Chris Masoneb60cea2007-02-02 09:18:22 -05001262 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001263 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001264 if (cow) {
1265 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001266 wret = btrfs_cow_block(trans, root, b,
1267 p->nodes[level + 1],
1268 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001269 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001270 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001271 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001272 return wret;
1273 }
Chris Mason02217ed2007-03-02 16:08:05 -05001274 }
1275 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001276 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001277 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001278 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001279 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001280 if (!p->skip_locking)
1281 p->locks[level] = 1;
Chris Mason123abc82007-03-14 14:14:43 -04001282 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001283 if (ret)
1284 return -1;
Chris Mason925baed2008-06-25 16:01:30 -04001285
Chris Mason5f39d392007-10-15 16:14:19 -04001286 ret = bin_search(b, key, level, &slot);
1287 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288 if (ret && slot > 0)
1289 slot -= 1;
1290 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001291 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001292 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001293 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001294 BUG_ON(sret > 0);
1295 if (sret)
1296 return sret;
1297 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001298 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001299 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001300 int sret = balance_level(trans, root, p,
1301 level);
Chris Masonbb803952007-03-01 12:04:21 -05001302 if (sret)
1303 return sret;
1304 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001305 if (!b) {
1306 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001307 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001308 }
Chris Masonbb803952007-03-01 12:04:21 -05001309 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001310 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001311 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001312 unlock_up(p, level, lowest_unlock);
1313
Chris Mason9f3a7422007-08-07 15:52:19 -04001314 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001315 if (level == lowest_level) {
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 Mason594a24e2008-06-25 16:01:30 -04001319 blocknr = btrfs_node_blockptr(b, slot);
1320 gen = btrfs_node_ptr_generation(b, slot);
1321 blocksize = btrfs_level_size(root, level - 1);
1322
1323 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1324 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001325 b = tmp;
1326 } else {
1327 /*
1328 * reduce lock contention at high levels
1329 * of the btree by dropping locks before
1330 * we read.
1331 */
1332 if (level > 1) {
1333 btrfs_release_path(NULL, p);
1334 if (tmp)
1335 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001336 if (should_reada)
1337 reada_for_search(root, p,
1338 level, slot,
1339 key->objectid);
1340
Chris Mason594a24e2008-06-25 16:01:30 -04001341 tmp = read_tree_block(root, blocknr,
1342 blocksize, gen);
1343 if (tmp)
1344 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001345 goto again;
1346 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001347 if (tmp)
1348 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001349 if (should_reada)
1350 reada_for_search(root, p,
1351 level, slot,
1352 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001353 b = read_node_slot(root, b, slot);
1354 }
1355 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001356 if (!p->skip_locking)
1357 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001358 } else {
1359 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001360 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001361 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001362 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001363 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001364 BUG_ON(sret > 0);
1365 if (sret)
1366 return sret;
1367 }
Chris Mason925baed2008-06-25 16:01:30 -04001368 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001369 return ret;
1370 }
1371 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001372 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001373}
1374
Chris Mason74123bd2007-02-02 11:05:29 -05001375/*
1376 * adjust the pointers going up the tree, starting at level
1377 * making sure the right key of each node is points to 'key'.
1378 * This is used after shifting pointers to the left, so it stops
1379 * fixing up pointers when a given leaf/node is not in slot 0 of the
1380 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001381 *
1382 * If this fails to write a tree block, it returns -1, but continues
1383 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001384 */
Chris Mason5f39d392007-10-15 16:14:19 -04001385static int fixup_low_keys(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root, struct btrfs_path *path,
1387 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001388{
1389 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001390 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001391 struct extent_buffer *t;
1392
Chris Mason234b63a2007-03-13 10:46:10 -04001393 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001394 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001395 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001396 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001397 t = path->nodes[i];
1398 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001399 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001400 if (tslot != 0)
1401 break;
1402 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001403 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001404}
1405
Chris Mason74123bd2007-02-02 11:05:29 -05001406/*
1407 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001408 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001409 *
1410 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1411 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001412 */
Chris Mason98ed5172008-01-03 10:01:48 -05001413static int push_node_left(struct btrfs_trans_handle *trans,
1414 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001415 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001416{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001417 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001418 int src_nritems;
1419 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001420 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001421
Chris Mason5f39d392007-10-15 16:14:19 -04001422 src_nritems = btrfs_header_nritems(src);
1423 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001424 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001425 WARN_ON(btrfs_header_generation(src) != trans->transid);
1426 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001427
Chris Masonbce4eae2008-04-24 14:42:46 -04001428 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001429 return 1;
1430
Chris Masoneb60cea2007-02-02 09:18:22 -05001431 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001432 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001433 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001434
Chris Masonbce4eae2008-04-24 14:42:46 -04001435 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001436 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001437 if (push_items < src_nritems) {
1438 /* leave at least 8 pointers in the node if
1439 * we aren't going to empty it
1440 */
1441 if (src_nritems - push_items < 8) {
1442 if (push_items <= 8)
1443 return 1;
1444 push_items -= 8;
1445 }
1446 }
1447 } else
1448 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001449
Chris Mason5f39d392007-10-15 16:14:19 -04001450 copy_extent_buffer(dst, src,
1451 btrfs_node_key_ptr_offset(dst_nritems),
1452 btrfs_node_key_ptr_offset(0),
1453 push_items * sizeof(struct btrfs_key_ptr));
1454
Chris Masonbb803952007-03-01 12:04:21 -05001455 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001456 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1457 btrfs_node_key_ptr_offset(push_items),
1458 (src_nritems - push_items) *
1459 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001460 }
Chris Mason5f39d392007-10-15 16:14:19 -04001461 btrfs_set_header_nritems(src, src_nritems - push_items);
1462 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1463 btrfs_mark_buffer_dirty(src);
1464 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001465 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001466}
1467
Chris Mason97571fd2007-02-24 13:39:08 -05001468/*
Chris Mason79f95c82007-03-01 15:16:26 -05001469 * try to push data from one node into the next node right in the
1470 * tree.
1471 *
1472 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1473 * error, and > 0 if there was no room in the right hand block.
1474 *
1475 * this will only push up to 1/2 the contents of the left node over
1476 */
Chris Mason5f39d392007-10-15 16:14:19 -04001477static int balance_node_right(struct btrfs_trans_handle *trans,
1478 struct btrfs_root *root,
1479 struct extent_buffer *dst,
1480 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001481{
Chris Mason79f95c82007-03-01 15:16:26 -05001482 int push_items = 0;
1483 int max_push;
1484 int src_nritems;
1485 int dst_nritems;
1486 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001487
Chris Mason7bb86312007-12-11 09:25:06 -05001488 WARN_ON(btrfs_header_generation(src) != trans->transid);
1489 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1490
Chris Mason5f39d392007-10-15 16:14:19 -04001491 src_nritems = btrfs_header_nritems(src);
1492 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001493 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001494 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001495 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001496 }
1497
1498 if (src_nritems < 4) {
1499 return 1;
1500 }
Chris Mason79f95c82007-03-01 15:16:26 -05001501
1502 max_push = src_nritems / 2 + 1;
1503 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001504 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001505 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001506 }
Yan252c38f2007-08-29 09:11:44 -04001507
Chris Mason79f95c82007-03-01 15:16:26 -05001508 if (max_push < push_items)
1509 push_items = max_push;
1510
Chris Mason5f39d392007-10-15 16:14:19 -04001511 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1512 btrfs_node_key_ptr_offset(0),
1513 (dst_nritems) *
1514 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001515
Chris Mason5f39d392007-10-15 16:14:19 -04001516 copy_extent_buffer(dst, src,
1517 btrfs_node_key_ptr_offset(0),
1518 btrfs_node_key_ptr_offset(src_nritems - push_items),
1519 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001520
Chris Mason5f39d392007-10-15 16:14:19 -04001521 btrfs_set_header_nritems(src, src_nritems - push_items);
1522 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001523
Chris Mason5f39d392007-10-15 16:14:19 -04001524 btrfs_mark_buffer_dirty(src);
1525 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001526 return ret;
1527}
1528
1529/*
Chris Mason97571fd2007-02-24 13:39:08 -05001530 * helper function to insert a new root level in the tree.
1531 * A new node is allocated, and a single item is inserted to
1532 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001533 *
1534 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001535 */
Chris Mason98ed5172008-01-03 10:01:48 -05001536static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001537 struct btrfs_root *root,
1538 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001539{
Chris Mason7bb86312007-12-11 09:25:06 -05001540 u64 root_gen;
1541 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001542 struct extent_buffer *lower;
1543 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001544 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001545 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001546
1547 BUG_ON(path->nodes[level]);
1548 BUG_ON(path->nodes[level-1] != root->node);
1549
Chris Mason7bb86312007-12-11 09:25:06 -05001550 if (root->ref_cows)
1551 root_gen = trans->transid;
1552 else
1553 root_gen = 0;
1554
1555 lower = path->nodes[level-1];
1556 if (level == 1)
1557 btrfs_item_key(lower, &lower_key, 0);
1558 else
1559 btrfs_node_key(lower, &lower_key, 0);
1560
Chris Mason925baed2008-06-25 16:01:30 -04001561 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001562 root->root_key.objectid,
1563 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001564 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001565 if (IS_ERR(c))
1566 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001567
Chris Mason5f39d392007-10-15 16:14:19 -04001568 memset_extent_buffer(c, 0, 0, root->nodesize);
1569 btrfs_set_header_nritems(c, 1);
1570 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001571 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001572 btrfs_set_header_generation(c, trans->transid);
1573 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001574
Chris Mason5f39d392007-10-15 16:14:19 -04001575 write_extent_buffer(c, root->fs_info->fsid,
1576 (unsigned long)btrfs_header_fsid(c),
1577 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001578
1579 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1580 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1581 BTRFS_UUID_SIZE);
1582
Chris Mason5f39d392007-10-15 16:14:19 -04001583 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001584 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001585 lower_gen = btrfs_header_generation(lower);
1586 WARN_ON(lower_gen == 0);
1587
1588 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001589
1590 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001591
Chris Mason925baed2008-06-25 16:01:30 -04001592 spin_lock(&root->node_lock);
1593 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001594 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001595 spin_unlock(&root->node_lock);
1596
1597 /* the super has an extra ref to root->node */
1598 free_extent_buffer(old);
1599
Chris Mason0b86a832008-03-24 15:01:56 -04001600 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001601 extent_buffer_get(c);
1602 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001603 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001604 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001605
1606 if (root->ref_cows && lower_gen != trans->transid) {
1607 struct btrfs_path *back_path = btrfs_alloc_path();
1608 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001609 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001610 ret = btrfs_insert_extent_backref(trans,
1611 root->fs_info->extent_root,
1612 path, lower->start,
1613 root->root_key.objectid,
1614 trans->transid, 0, 0);
1615 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001616 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001617 btrfs_free_path(back_path);
1618 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001619 return 0;
1620}
1621
Chris Mason74123bd2007-02-02 11:05:29 -05001622/*
1623 * worker function to insert a single pointer in a node.
1624 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001625 *
Chris Mason74123bd2007-02-02 11:05:29 -05001626 * slot and level indicate where you want the key to go, and
1627 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001628 *
1629 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001630 */
Chris Masone089f052007-03-16 16:20:31 -04001631static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1632 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001633 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001634{
Chris Mason5f39d392007-10-15 16:14:19 -04001635 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001636 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001637
1638 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001639 lower = path->nodes[level];
1640 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001641 if (slot > nritems)
1642 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001643 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001644 BUG();
1645 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001646 memmove_extent_buffer(lower,
1647 btrfs_node_key_ptr_offset(slot + 1),
1648 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001649 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001650 }
Chris Mason5f39d392007-10-15 16:14:19 -04001651 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001652 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001653 WARN_ON(trans->transid == 0);
1654 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001655 btrfs_set_header_nritems(lower, nritems + 1);
1656 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001657 return 0;
1658}
1659
Chris Mason97571fd2007-02-24 13:39:08 -05001660/*
1661 * split the node at the specified level in path in two.
1662 * The path is corrected to point to the appropriate node after the split
1663 *
1664 * Before splitting this tries to make some room in the node by pushing
1665 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001666 *
1667 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001668 */
Chris Masone089f052007-03-16 16:20:31 -04001669static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1670 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671{
Chris Mason7bb86312007-12-11 09:25:06 -05001672 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001673 struct extent_buffer *c;
1674 struct extent_buffer *split;
1675 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001676 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001677 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001678 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001679 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001680
Chris Mason5f39d392007-10-15 16:14:19 -04001681 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001682 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001683 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001684 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001685 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001686 if (ret)
1687 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001688 } else {
1689 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001690 c = path->nodes[level];
1691 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001692 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001693 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001694 if (ret < 0)
1695 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001696 }
Chris Masone66f7092007-04-20 13:16:02 -04001697
Chris Mason5f39d392007-10-15 16:14:19 -04001698 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001699 if (root->ref_cows)
1700 root_gen = trans->transid;
1701 else
1702 root_gen = 0;
1703
1704 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001705 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001706 root->root_key.objectid,
1707 root_gen,
1708 btrfs_disk_key_objectid(&disk_key),
1709 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001710 if (IS_ERR(split))
1711 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001712
Chris Mason5f39d392007-10-15 16:14:19 -04001713 btrfs_set_header_flags(split, btrfs_header_flags(c));
1714 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001715 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001716 btrfs_set_header_generation(split, trans->transid);
1717 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001718 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001719 write_extent_buffer(split, root->fs_info->fsid,
1720 (unsigned long)btrfs_header_fsid(split),
1721 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001722 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1723 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1724 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001725
Chris Mason7518a232007-03-12 12:01:18 -04001726 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001727
1728 copy_extent_buffer(split, c,
1729 btrfs_node_key_ptr_offset(0),
1730 btrfs_node_key_ptr_offset(mid),
1731 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1732 btrfs_set_header_nritems(split, c_nritems - mid);
1733 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001734 ret = 0;
1735
Chris Mason5f39d392007-10-15 16:14:19 -04001736 btrfs_mark_buffer_dirty(c);
1737 btrfs_mark_buffer_dirty(split);
1738
1739 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001740 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001741 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001742 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001743 if (wret)
1744 ret = wret;
1745
Chris Mason5de08d72007-02-24 06:24:44 -05001746 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001747 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001748 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001749 free_extent_buffer(c);
1750 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001751 path->slots[level + 1] += 1;
1752 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001753 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001754 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001755 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001756 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757}
1758
Chris Mason74123bd2007-02-02 11:05:29 -05001759/*
1760 * how many bytes are required to store the items in a leaf. start
1761 * and nr indicate which items in the leaf to check. This totals up the
1762 * space used both by the item structs and the item data
1763 */
Chris Mason5f39d392007-10-15 16:14:19 -04001764static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001765{
1766 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001767 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001768 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769
1770 if (!nr)
1771 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001772 data_len = btrfs_item_end_nr(l, start);
1773 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001774 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001775 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776 return data_len;
1777}
1778
Chris Mason74123bd2007-02-02 11:05:29 -05001779/*
Chris Masond4dbff92007-04-04 14:08:15 -04001780 * The space between the end of the leaf items and
1781 * the start of the leaf data. IOW, how much room
1782 * the leaf has left for both items and data
1783 */
Chris Mason5f39d392007-10-15 16:14:19 -04001784int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001785{
Chris Mason5f39d392007-10-15 16:14:19 -04001786 int nritems = btrfs_header_nritems(leaf);
1787 int ret;
1788 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1789 if (ret < 0) {
1790 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001791 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001792 leaf_space_used(leaf, 0, nritems), nritems);
1793 }
1794 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001795}
1796
1797/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001798 * push some data in the path leaf to the right, trying to free up at
1799 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001800 *
1801 * returns 1 if the push failed because the other node didn't have enough
1802 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001803 */
Chris Masone089f052007-03-16 16:20:31 -04001804static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001805 *root, struct btrfs_path *path, int data_size,
1806 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001807{
Chris Mason5f39d392007-10-15 16:14:19 -04001808 struct extent_buffer *left = path->nodes[0];
1809 struct extent_buffer *right;
1810 struct extent_buffer *upper;
1811 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001812 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001813 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001814 int free_space;
1815 int push_space = 0;
1816 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001817 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001818 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001819 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001820 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001821 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001822 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001823 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001824
1825 slot = path->slots[1];
1826 if (!path->nodes[1]) {
1827 return 1;
1828 }
1829 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001830 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001831 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001832
Chris Masona2135012008-06-25 16:01:30 -04001833 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1834
Chris Masonca7a79a2008-05-12 12:59:19 -04001835 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001836 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001837 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001838 if (free_space < data_size + sizeof(struct btrfs_item))
1839 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 /* cow and double check */
1842 ret = btrfs_cow_block(trans, root, right, upper,
1843 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04001844 if (ret)
1845 goto out_unlock;
1846
Chris Mason5f39d392007-10-15 16:14:19 -04001847 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001848 if (free_space < data_size + sizeof(struct btrfs_item))
1849 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001850
1851 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001852 if (left_nritems == 0)
1853 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001854
Chris Mason34a38212007-11-07 13:31:03 -05001855 if (empty)
1856 nr = 0;
1857 else
1858 nr = 1;
1859
1860 i = left_nritems - 1;
1861 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001862 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001863
Chris Mason00ec4c52007-02-24 12:47:20 -05001864 if (path->slots[0] == i)
1865 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001866
1867 if (!left->map_token) {
1868 map_extent_buffer(left, (unsigned long)item,
1869 sizeof(struct btrfs_item),
1870 &left->map_token, &left->kaddr,
1871 &left->map_start, &left->map_len,
1872 KM_USER1);
1873 }
1874
1875 this_item_size = btrfs_item_size(left, item);
1876 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001877 break;
1878 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001879 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001880 if (i == 0)
1881 break;
1882 i--;
Chris Masondb945352007-10-15 16:15:53 -04001883 }
1884 if (left->map_token) {
1885 unmap_extent_buffer(left, left->map_token, KM_USER1);
1886 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001887 }
Chris Mason5f39d392007-10-15 16:14:19 -04001888
Chris Mason925baed2008-06-25 16:01:30 -04001889 if (push_items == 0)
1890 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001891
Chris Mason34a38212007-11-07 13:31:03 -05001892 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001893 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001894
Chris Mason00ec4c52007-02-24 12:47:20 -05001895 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001896 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001897
Chris Mason5f39d392007-10-15 16:14:19 -04001898 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001899 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001900
Chris Mason00ec4c52007-02-24 12:47:20 -05001901 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001902 data_end = leaf_data_end(root, right);
1903 memmove_extent_buffer(right,
1904 btrfs_leaf_data(right) + data_end - push_space,
1905 btrfs_leaf_data(right) + data_end,
1906 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1907
Chris Mason00ec4c52007-02-24 12:47:20 -05001908 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001909 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001910 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1911 btrfs_leaf_data(left) + leaf_data_end(root, left),
1912 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001913
1914 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1915 btrfs_item_nr_offset(0),
1916 right_nritems * sizeof(struct btrfs_item));
1917
Chris Mason00ec4c52007-02-24 12:47:20 -05001918 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001919 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1920 btrfs_item_nr_offset(left_nritems - push_items),
1921 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001922
1923 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001924 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001925 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001926 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001927 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001928 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001929 if (!right->map_token) {
1930 map_extent_buffer(right, (unsigned long)item,
1931 sizeof(struct btrfs_item),
1932 &right->map_token, &right->kaddr,
1933 &right->map_start, &right->map_len,
1934 KM_USER1);
1935 }
1936 push_space -= btrfs_item_size(right, item);
1937 btrfs_set_item_offset(right, item, push_space);
1938 }
1939
1940 if (right->map_token) {
1941 unmap_extent_buffer(right, right->map_token, KM_USER1);
1942 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001943 }
Chris Mason7518a232007-03-12 12:01:18 -04001944 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001945 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001946
Chris Mason34a38212007-11-07 13:31:03 -05001947 if (left_nritems)
1948 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 btrfs_item_key(right, &disk_key, 0);
1952 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001953 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001954
Chris Mason00ec4c52007-02-24 12:47:20 -05001955 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001956 if (path->slots[0] >= left_nritems) {
1957 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04001958 if (btrfs_header_nritems(path->nodes[0]) == 0)
1959 clean_tree_block(trans, root, path->nodes[0]);
1960 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04001961 free_extent_buffer(path->nodes[0]);
1962 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001963 path->slots[1] += 1;
1964 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001965 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001966 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001967 }
1968 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04001969
1970out_unlock:
1971 btrfs_tree_unlock(right);
1972 free_extent_buffer(right);
1973 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05001974}
Chris Mason925baed2008-06-25 16:01:30 -04001975
Chris Mason00ec4c52007-02-24 12:47:20 -05001976/*
Chris Mason74123bd2007-02-02 11:05:29 -05001977 * push some data in the path leaf to the left, trying to free up at
1978 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1979 */
Chris Masone089f052007-03-16 16:20:31 -04001980static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001981 *root, struct btrfs_path *path, int data_size,
1982 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001983{
Chris Mason5f39d392007-10-15 16:14:19 -04001984 struct btrfs_disk_key disk_key;
1985 struct extent_buffer *right = path->nodes[0];
1986 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001987 int slot;
1988 int i;
1989 int free_space;
1990 int push_space = 0;
1991 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001992 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001993 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001994 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001995 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001996 int ret = 0;
1997 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001998 u32 this_item_size;
1999 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002000
2001 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002002 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002003 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002004 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002005 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002006
Chris Mason3685f792007-10-19 09:23:27 -04002007 right_nritems = btrfs_header_nritems(right);
2008 if (right_nritems == 0) {
2009 return 1;
2010 }
2011
Chris Masona2135012008-06-25 16:01:30 -04002012 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2013
Chris Masonca7a79a2008-05-12 12:59:19 -04002014 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002015 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002016 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002017 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002018 ret = 1;
2019 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002020 }
Chris Mason02217ed2007-03-02 16:08:05 -05002021
2022 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002023 ret = btrfs_cow_block(trans, root, left,
2024 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002025 if (ret) {
2026 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002027 ret = 1;
2028 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002029 }
Chris Mason3685f792007-10-19 09:23:27 -04002030
Chris Mason123abc82007-03-14 14:14:43 -04002031 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002032 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002033 ret = 1;
2034 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002035 }
2036
Chris Mason34a38212007-11-07 13:31:03 -05002037 if (empty)
2038 nr = right_nritems;
2039 else
2040 nr = right_nritems - 1;
2041
2042 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002043 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002044 if (!right->map_token) {
2045 map_extent_buffer(right, (unsigned long)item,
2046 sizeof(struct btrfs_item),
2047 &right->map_token, &right->kaddr,
2048 &right->map_start, &right->map_len,
2049 KM_USER1);
2050 }
2051
Chris Masonbe0e5c02007-01-26 15:51:26 -05002052 if (path->slots[0] == i)
2053 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002054
2055 this_item_size = btrfs_item_size(right, item);
2056 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002057 break;
Chris Masondb945352007-10-15 16:15:53 -04002058
Chris Masonbe0e5c02007-01-26 15:51:26 -05002059 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002060 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002061 }
Chris Masondb945352007-10-15 16:15:53 -04002062
2063 if (right->map_token) {
2064 unmap_extent_buffer(right, right->map_token, KM_USER1);
2065 right->map_token = NULL;
2066 }
2067
Chris Masonbe0e5c02007-01-26 15:51:26 -05002068 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002069 ret = 1;
2070 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002071 }
Chris Mason34a38212007-11-07 13:31:03 -05002072 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002073 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002074
Chris Masonbe0e5c02007-01-26 15:51:26 -05002075 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002076 copy_extent_buffer(left, right,
2077 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2078 btrfs_item_nr_offset(0),
2079 push_items * sizeof(struct btrfs_item));
2080
Chris Mason123abc82007-03-14 14:14:43 -04002081 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_item_offset_nr(right, push_items -1);
2083
2084 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002085 leaf_data_end(root, left) - push_space,
2086 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002087 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002088 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002089 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002090 BUG_ON(old_left_nritems < 0);
2091
Chris Masondb945352007-10-15 16:15:53 -04002092 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002093 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002094 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002095
Chris Mason5f39d392007-10-15 16:14:19 -04002096 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002097 if (!left->map_token) {
2098 map_extent_buffer(left, (unsigned long)item,
2099 sizeof(struct btrfs_item),
2100 &left->map_token, &left->kaddr,
2101 &left->map_start, &left->map_len,
2102 KM_USER1);
2103 }
2104
Chris Mason5f39d392007-10-15 16:14:19 -04002105 ioff = btrfs_item_offset(left, item);
2106 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002107 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002108 }
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002110 if (left->map_token) {
2111 unmap_extent_buffer(left, left->map_token, KM_USER1);
2112 left->map_token = NULL;
2113 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002114
2115 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002116 if (push_items > right_nritems) {
2117 printk("push items %d nr %u\n", push_items, right_nritems);
2118 WARN_ON(1);
2119 }
Chris Mason5f39d392007-10-15 16:14:19 -04002120
Chris Mason34a38212007-11-07 13:31:03 -05002121 if (push_items < right_nritems) {
2122 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2123 leaf_data_end(root, right);
2124 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2125 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2126 btrfs_leaf_data(right) +
2127 leaf_data_end(root, right), push_space);
2128
2129 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_item_nr_offset(push_items),
2131 (btrfs_header_nritems(right) - push_items) *
2132 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002133 }
Yaneef1c492007-11-26 10:58:13 -05002134 right_nritems -= push_items;
2135 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002136 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 for (i = 0; i < right_nritems; i++) {
2138 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002139
2140 if (!right->map_token) {
2141 map_extent_buffer(right, (unsigned long)item,
2142 sizeof(struct btrfs_item),
2143 &right->map_token, &right->kaddr,
2144 &right->map_start, &right->map_len,
2145 KM_USER1);
2146 }
2147
2148 push_space = push_space - btrfs_item_size(right, item);
2149 btrfs_set_item_offset(right, item, push_space);
2150 }
2151 if (right->map_token) {
2152 unmap_extent_buffer(right, right->map_token, KM_USER1);
2153 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002154 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002155
Chris Mason5f39d392007-10-15 16:14:19 -04002156 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002157 if (right_nritems)
2158 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002159
Chris Mason5f39d392007-10-15 16:14:19 -04002160 btrfs_item_key(right, &disk_key, 0);
2161 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002162 if (wret)
2163 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002164
2165 /* then fixup the leaf pointer in the path */
2166 if (path->slots[0] < push_items) {
2167 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002168 if (btrfs_header_nritems(path->nodes[0]) == 0)
2169 clean_tree_block(trans, root, path->nodes[0]);
2170 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002171 free_extent_buffer(path->nodes[0]);
2172 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002173 path->slots[1] -= 1;
2174 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002175 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002176 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002177 path->slots[0] -= push_items;
2178 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002179 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002180 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002181out:
2182 btrfs_tree_unlock(left);
2183 free_extent_buffer(left);
2184 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002185}
2186
Chris Mason74123bd2007-02-02 11:05:29 -05002187/*
2188 * split the path's leaf in two, making sure there is at least data_size
2189 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002190 *
2191 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002192 */
Chris Masone089f052007-03-16 16:20:31 -04002193static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002194 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002195 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002196{
Chris Mason7bb86312007-12-11 09:25:06 -05002197 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002199 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002200 int mid;
2201 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002202 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002203 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002204 int data_copy_size;
2205 int rt_data_off;
2206 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002207 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002208 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002209 int double_split;
2210 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002211 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002212
Chris Masoncc0c5532007-10-25 15:42:57 -04002213 if (extend)
2214 space_needed = data_size;
2215
Chris Mason7bb86312007-12-11 09:25:06 -05002216 if (root->ref_cows)
2217 root_gen = trans->transid;
2218 else
2219 root_gen = 0;
2220
Chris Mason40689472007-03-17 14:29:23 -04002221 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002222 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002223 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002224 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002225 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002226 }
2227 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002228 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002229 if (wret < 0)
2230 return wret;
2231 }
2232 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002233
Chris Mason3685f792007-10-19 09:23:27 -04002234 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002235 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002236 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002237 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002238
Chris Mason5c680ed2007-02-22 11:39:13 -05002239 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002240 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002241 if (ret)
2242 return ret;
2243 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002244again:
2245 double_split = 0;
2246 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002247 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002248 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002249 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002250
Chris Mason7bb86312007-12-11 09:25:06 -05002251 btrfs_item_key(l, &disk_key, 0);
2252
Chris Mason925baed2008-06-25 16:01:30 -04002253 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002254 root->root_key.objectid,
2255 root_gen, disk_key.objectid, 0,
2256 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002257 if (IS_ERR(right)) {
2258 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002259 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002260 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002261
Chris Mason5f39d392007-10-15 16:14:19 -04002262 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002263 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 btrfs_set_header_generation(right, trans->transid);
2265 btrfs_set_header_owner(right, root->root_key.objectid);
2266 btrfs_set_header_level(right, 0);
2267 write_extent_buffer(right, root->fs_info->fsid,
2268 (unsigned long)btrfs_header_fsid(right),
2269 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002270
2271 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2272 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2273 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002274 if (mid <= slot) {
2275 if (nritems == 1 ||
2276 leaf_space_used(l, mid, nritems - mid) + space_needed >
2277 BTRFS_LEAF_DATA_SIZE(root)) {
2278 if (slot >= nritems) {
2279 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002280 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002281 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002282 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002283 path->slots[1] + 1, 1);
2284 if (wret)
2285 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002286
2287 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002288 free_extent_buffer(path->nodes[0]);
2289 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002290 path->slots[0] = 0;
2291 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002292 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002293 return ret;
2294 }
2295 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002296 if (mid != nritems &&
2297 leaf_space_used(l, mid, nritems - mid) +
2298 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2299 double_split = 1;
2300 }
Chris Masond4dbff92007-04-04 14:08:15 -04002301 }
2302 } else {
2303 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2304 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002305 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002306 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002307 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002308 wret = insert_ptr(trans, root, path,
2309 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002310 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002311 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002312 if (wret)
2313 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002314 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002315 free_extent_buffer(path->nodes[0]);
2316 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002317 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002318 if (path->slots[1] == 0) {
2319 wret = fixup_low_keys(trans, root,
2320 path, &disk_key, 1);
2321 if (wret)
2322 ret = wret;
2323 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002324 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002325 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002326 } else if (extend && slot == 0) {
2327 mid = 1;
2328 } else {
2329 mid = slot;
2330 if (mid != nritems &&
2331 leaf_space_used(l, mid, nritems - mid) +
2332 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2333 double_split = 1;
2334 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002335 }
Chris Masond4dbff92007-04-04 14:08:15 -04002336 }
2337 }
Chris Mason5f39d392007-10-15 16:14:19 -04002338 nritems = nritems - mid;
2339 btrfs_set_header_nritems(right, nritems);
2340 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2341
2342 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2343 btrfs_item_nr_offset(mid),
2344 nritems * sizeof(struct btrfs_item));
2345
2346 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002347 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2348 data_copy_size, btrfs_leaf_data(l) +
2349 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002350
Chris Mason5f39d392007-10-15 16:14:19 -04002351 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2352 btrfs_item_end_nr(l, mid);
2353
2354 for (i = 0; i < nritems; i++) {
2355 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002356 u32 ioff;
2357
2358 if (!right->map_token) {
2359 map_extent_buffer(right, (unsigned long)item,
2360 sizeof(struct btrfs_item),
2361 &right->map_token, &right->kaddr,
2362 &right->map_start, &right->map_len,
2363 KM_USER1);
2364 }
2365
2366 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002367 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002368 }
Chris Mason74123bd2007-02-02 11:05:29 -05002369
Chris Masondb945352007-10-15 16:15:53 -04002370 if (right->map_token) {
2371 unmap_extent_buffer(right, right->map_token, KM_USER1);
2372 right->map_token = NULL;
2373 }
2374
Chris Mason5f39d392007-10-15 16:14:19 -04002375 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002376 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002378 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2379 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002380 if (wret)
2381 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002382
2383 btrfs_mark_buffer_dirty(right);
2384 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002385 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002386
Chris Masonbe0e5c02007-01-26 15:51:26 -05002387 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002388 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002389 free_extent_buffer(path->nodes[0]);
2390 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002391 path->slots[0] -= mid;
2392 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002393 } else {
2394 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002395 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002396 }
Chris Mason5f39d392007-10-15 16:14:19 -04002397
Chris Masoneb60cea2007-02-02 09:18:22 -05002398 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002399
Chris Masoncc0c5532007-10-25 15:42:57 -04002400 if (double_split) {
2401 BUG_ON(num_doubles != 0);
2402 num_doubles++;
2403 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002404 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002405 return ret;
2406}
2407
Chris Masonb18c6682007-04-17 13:26:50 -04002408int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2409 struct btrfs_root *root,
2410 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002411 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002412{
2413 int ret = 0;
2414 int slot;
2415 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002416 struct extent_buffer *leaf;
2417 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002418 u32 nritems;
2419 unsigned int data_end;
2420 unsigned int old_data_start;
2421 unsigned int old_size;
2422 unsigned int size_diff;
2423 int i;
2424
2425 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002426 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002427 slot = path->slots[0];
2428
2429 old_size = btrfs_item_size_nr(leaf, slot);
2430 if (old_size == new_size)
2431 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002434 data_end = leaf_data_end(root, leaf);
2435
Chris Mason5f39d392007-10-15 16:14:19 -04002436 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002437
Chris Masonb18c6682007-04-17 13:26:50 -04002438 size_diff = old_size - new_size;
2439
2440 BUG_ON(slot < 0);
2441 BUG_ON(slot >= nritems);
2442
2443 /*
2444 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2445 */
2446 /* first correct the data pointers */
2447 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002448 u32 ioff;
2449 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002450
2451 if (!leaf->map_token) {
2452 map_extent_buffer(leaf, (unsigned long)item,
2453 sizeof(struct btrfs_item),
2454 &leaf->map_token, &leaf->kaddr,
2455 &leaf->map_start, &leaf->map_len,
2456 KM_USER1);
2457 }
2458
Chris Mason5f39d392007-10-15 16:14:19 -04002459 ioff = btrfs_item_offset(leaf, item);
2460 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002461 }
Chris Masondb945352007-10-15 16:15:53 -04002462
2463 if (leaf->map_token) {
2464 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2465 leaf->map_token = NULL;
2466 }
2467
Chris Masonb18c6682007-04-17 13:26:50 -04002468 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002469 if (from_end) {
2470 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2471 data_end + size_diff, btrfs_leaf_data(leaf) +
2472 data_end, old_data_start + new_size - data_end);
2473 } else {
2474 struct btrfs_disk_key disk_key;
2475 u64 offset;
2476
2477 btrfs_item_key(leaf, &disk_key, slot);
2478
2479 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2480 unsigned long ptr;
2481 struct btrfs_file_extent_item *fi;
2482
2483 fi = btrfs_item_ptr(leaf, slot,
2484 struct btrfs_file_extent_item);
2485 fi = (struct btrfs_file_extent_item *)(
2486 (unsigned long)fi - size_diff);
2487
2488 if (btrfs_file_extent_type(leaf, fi) ==
2489 BTRFS_FILE_EXTENT_INLINE) {
2490 ptr = btrfs_item_ptr_offset(leaf, slot);
2491 memmove_extent_buffer(leaf, ptr,
2492 (unsigned long)fi,
2493 offsetof(struct btrfs_file_extent_item,
2494 disk_bytenr));
2495 }
2496 }
2497
2498 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2499 data_end + size_diff, btrfs_leaf_data(leaf) +
2500 data_end, old_data_start - data_end);
2501
2502 offset = btrfs_disk_key_offset(&disk_key);
2503 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2504 btrfs_set_item_key(leaf, &disk_key, slot);
2505 if (slot == 0)
2506 fixup_low_keys(trans, root, path, &disk_key, 1);
2507 }
Chris Mason5f39d392007-10-15 16:14:19 -04002508
2509 item = btrfs_item_nr(leaf, slot);
2510 btrfs_set_item_size(leaf, item, new_size);
2511 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002512
2513 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002514 if (btrfs_leaf_free_space(root, leaf) < 0) {
2515 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002516 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002517 }
Chris Masonb18c6682007-04-17 13:26:50 -04002518 return ret;
2519}
2520
Chris Mason5f39d392007-10-15 16:14:19 -04002521int btrfs_extend_item(struct btrfs_trans_handle *trans,
2522 struct btrfs_root *root, struct btrfs_path *path,
2523 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002524{
2525 int ret = 0;
2526 int slot;
2527 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002528 struct extent_buffer *leaf;
2529 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002530 u32 nritems;
2531 unsigned int data_end;
2532 unsigned int old_data;
2533 unsigned int old_size;
2534 int i;
2535
2536 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002537 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002538
Chris Mason5f39d392007-10-15 16:14:19 -04002539 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002540 data_end = leaf_data_end(root, leaf);
2541
Chris Mason5f39d392007-10-15 16:14:19 -04002542 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2543 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002544 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002545 }
Chris Mason6567e832007-04-16 09:22:45 -04002546 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002547 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002548
2549 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002550 if (slot >= nritems) {
2551 btrfs_print_leaf(root, leaf);
2552 printk("slot %d too large, nritems %d\n", slot, nritems);
2553 BUG_ON(1);
2554 }
Chris Mason6567e832007-04-16 09:22:45 -04002555
2556 /*
2557 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2558 */
2559 /* first correct the data pointers */
2560 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002561 u32 ioff;
2562 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002563
2564 if (!leaf->map_token) {
2565 map_extent_buffer(leaf, (unsigned long)item,
2566 sizeof(struct btrfs_item),
2567 &leaf->map_token, &leaf->kaddr,
2568 &leaf->map_start, &leaf->map_len,
2569 KM_USER1);
2570 }
Chris Mason5f39d392007-10-15 16:14:19 -04002571 ioff = btrfs_item_offset(leaf, item);
2572 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002573 }
Chris Mason5f39d392007-10-15 16:14:19 -04002574
Chris Masondb945352007-10-15 16:15:53 -04002575 if (leaf->map_token) {
2576 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2577 leaf->map_token = NULL;
2578 }
2579
Chris Mason6567e832007-04-16 09:22:45 -04002580 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002581 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002582 data_end - data_size, btrfs_leaf_data(leaf) +
2583 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002584
Chris Mason6567e832007-04-16 09:22:45 -04002585 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002586 old_size = btrfs_item_size_nr(leaf, slot);
2587 item = btrfs_item_nr(leaf, slot);
2588 btrfs_set_item_size(leaf, item, old_size + data_size);
2589 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002590
2591 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002592 if (btrfs_leaf_free_space(root, leaf) < 0) {
2593 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002594 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002595 }
Chris Mason6567e832007-04-16 09:22:45 -04002596 return ret;
2597}
2598
Chris Mason74123bd2007-02-02 11:05:29 -05002599/*
2600 * Given a key and some data, insert an item into the tree.
2601 * This does all the path init required, making room in the tree if needed.
2602 */
Chris Mason9c583092008-01-29 15:15:18 -05002603int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002604 struct btrfs_root *root,
2605 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002606 struct btrfs_key *cpu_key, u32 *data_size,
2607 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608{
Chris Mason5f39d392007-10-15 16:14:19 -04002609 struct extent_buffer *leaf;
2610 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002611 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002612 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002613 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002614 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002615 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002616 u32 total_size = 0;
2617 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002619 struct btrfs_disk_key disk_key;
2620
Chris Mason9c583092008-01-29 15:15:18 -05002621 for (i = 0; i < nr; i++) {
2622 total_data += data_size[i];
2623 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002624
Josef Bacik7b128762008-07-24 12:17:14 -04002625 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05002626 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002627 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002628 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002629 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002630 if (ret < 0)
2631 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002632
Chris Mason62e27492007-03-15 12:56:47 -04002633 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002634 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002635
Chris Mason5f39d392007-10-15 16:14:19 -04002636 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002637 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002638
Chris Mason123abc82007-03-14 14:14:43 -04002639 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002640 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002641 btrfs_print_leaf(root, leaf);
2642 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002643 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002644 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002645 }
Chris Mason5f39d392007-10-15 16:14:19 -04002646
Chris Mason62e27492007-03-15 12:56:47 -04002647 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002648 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002649
Chris Masonbe0e5c02007-01-26 15:51:26 -05002650 if (slot != nritems) {
2651 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002652 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002653
Chris Mason5f39d392007-10-15 16:14:19 -04002654 if (old_data < data_end) {
2655 btrfs_print_leaf(root, leaf);
2656 printk("slot %d old_data %d data_end %d\n",
2657 slot, old_data, data_end);
2658 BUG_ON(1);
2659 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002660 /*
2661 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2662 */
2663 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002664 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002665 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002666 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002667
Chris Mason5f39d392007-10-15 16:14:19 -04002668 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002669 if (!leaf->map_token) {
2670 map_extent_buffer(leaf, (unsigned long)item,
2671 sizeof(struct btrfs_item),
2672 &leaf->map_token, &leaf->kaddr,
2673 &leaf->map_start, &leaf->map_len,
2674 KM_USER1);
2675 }
2676
Chris Mason5f39d392007-10-15 16:14:19 -04002677 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002678 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002679 }
Chris Masondb945352007-10-15 16:15:53 -04002680 if (leaf->map_token) {
2681 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2682 leaf->map_token = NULL;
2683 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002684
2685 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002686 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002687 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002688 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689
2690 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002691 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002692 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002693 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002694 data_end = old_data;
2695 }
Chris Mason5f39d392007-10-15 16:14:19 -04002696
Chris Mason62e27492007-03-15 12:56:47 -04002697 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002698 for (i = 0; i < nr; i++) {
2699 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2700 btrfs_set_item_key(leaf, &disk_key, slot + i);
2701 item = btrfs_item_nr(leaf, slot + i);
2702 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2703 data_end -= data_size[i];
2704 btrfs_set_item_size(leaf, item, data_size[i]);
2705 }
2706 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002707 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002708
2709 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002710 if (slot == 0) {
2711 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002712 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002713 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002714
Chris Mason5f39d392007-10-15 16:14:19 -04002715 if (btrfs_leaf_free_space(root, leaf) < 0) {
2716 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002717 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002718 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002719out:
Chris Mason62e27492007-03-15 12:56:47 -04002720 return ret;
2721}
2722
2723/*
2724 * Given a key and some data, insert an item into the tree.
2725 * This does all the path init required, making room in the tree if needed.
2726 */
Chris Masone089f052007-03-16 16:20:31 -04002727int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2728 *root, struct btrfs_key *cpu_key, void *data, u32
2729 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002730{
2731 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002732 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002733 struct extent_buffer *leaf;
2734 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002735
Chris Mason2c90e5d2007-04-02 10:50:19 -04002736 path = btrfs_alloc_path();
2737 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002738 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002739 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002740 leaf = path->nodes[0];
2741 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2742 write_extent_buffer(leaf, data, ptr, data_size);
2743 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002744 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002745 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002746 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002747}
2748
Chris Mason74123bd2007-02-02 11:05:29 -05002749/*
Chris Mason5de08d72007-02-24 06:24:44 -05002750 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002751 *
2752 * If the delete empties a node, the node is removed from the tree,
2753 * continuing all the way the root if required. The root is converted into
2754 * a leaf if all the nodes are emptied.
2755 */
Chris Masone089f052007-03-16 16:20:31 -04002756static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2757 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758{
Chris Mason5f39d392007-10-15 16:14:19 -04002759 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002760 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002761 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002762 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002765 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002766 memmove_extent_buffer(parent,
2767 btrfs_node_key_ptr_offset(slot),
2768 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002769 sizeof(struct btrfs_key_ptr) *
2770 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002771 }
Chris Mason7518a232007-03-12 12:01:18 -04002772 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002773 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002774 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002775 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002776 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002777 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002778 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002779 struct btrfs_disk_key disk_key;
2780
2781 btrfs_node_key(parent, &disk_key, 0);
2782 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002783 if (wret)
2784 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002785 }
Chris Masond6025572007-03-30 14:27:56 -04002786 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002787 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002788}
2789
Chris Mason74123bd2007-02-02 11:05:29 -05002790/*
2791 * delete the item at the leaf level in path. If that empties
2792 * the leaf, remove it from the tree
2793 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002794int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2795 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002796{
Chris Mason5f39d392007-10-15 16:14:19 -04002797 struct extent_buffer *leaf;
2798 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002799 int last_off;
2800 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002801 int ret = 0;
2802 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002803 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002804 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002805
Chris Mason5f39d392007-10-15 16:14:19 -04002806 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002807 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2808
2809 for (i = 0; i < nr; i++)
2810 dsize += btrfs_item_size_nr(leaf, slot + i);
2811
Chris Mason5f39d392007-10-15 16:14:19 -04002812 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002813
Chris Mason85e21ba2008-01-29 15:11:36 -05002814 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002815 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002816 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002817
2818 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002819 data_end + dsize,
2820 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002821 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002822
Chris Mason85e21ba2008-01-29 15:11:36 -05002823 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002824 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002825
Chris Mason5f39d392007-10-15 16:14:19 -04002826 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002827 if (!leaf->map_token) {
2828 map_extent_buffer(leaf, (unsigned long)item,
2829 sizeof(struct btrfs_item),
2830 &leaf->map_token, &leaf->kaddr,
2831 &leaf->map_start, &leaf->map_len,
2832 KM_USER1);
2833 }
Chris Mason5f39d392007-10-15 16:14:19 -04002834 ioff = btrfs_item_offset(leaf, item);
2835 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002836 }
Chris Masondb945352007-10-15 16:15:53 -04002837
2838 if (leaf->map_token) {
2839 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2840 leaf->map_token = NULL;
2841 }
2842
Chris Mason5f39d392007-10-15 16:14:19 -04002843 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002844 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002845 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002846 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002847 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002848 btrfs_set_header_nritems(leaf, nritems - nr);
2849 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002850
Chris Mason74123bd2007-02-02 11:05:29 -05002851 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002852 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002853 if (leaf == root->node) {
2854 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002855 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002856 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002857 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002858 if (wret)
2859 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002860 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002861 leaf->start, leaf->len,
2862 btrfs_header_owner(path->nodes[1]),
2863 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002864 if (wret)
2865 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002866 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002867 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002868 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002869 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002870 struct btrfs_disk_key disk_key;
2871
2872 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002873 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002874 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002875 if (wret)
2876 ret = wret;
2877 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002878
Chris Mason74123bd2007-02-02 11:05:29 -05002879 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002880 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002881 /* push_leaf_left fixes the path.
2882 * make sure the path still points to our leaf
2883 * for possible call to del_ptr below
2884 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002885 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002886 extent_buffer_get(leaf);
2887
Chris Mason85e21ba2008-01-29 15:11:36 -05002888 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002889 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002890 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002891
2892 if (path->nodes[0] == leaf &&
2893 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002894 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002895 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002896 ret = wret;
2897 }
Chris Mason5f39d392007-10-15 16:14:19 -04002898
2899 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002900 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002901 u64 bytenr = leaf->start;
2902 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002903
Chris Mason7bb86312007-12-11 09:25:06 -05002904 root_gen = btrfs_header_generation(
2905 path->nodes[1]);
2906
Chris Masone089f052007-03-16 16:20:31 -04002907 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002908 if (wret)
2909 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002910
2911 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002912 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002913 blocksize,
2914 btrfs_header_owner(path->nodes[1]),
2915 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002916 if (wret)
2917 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002918 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002919 /* if we're still in the path, make sure
2920 * we're dirty. Otherwise, one of the
2921 * push_leaf functions must have already
2922 * dirtied this buffer
2923 */
2924 if (path->nodes[0] == leaf)
2925 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002926 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002927 }
Chris Masond5719762007-03-23 10:01:08 -04002928 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002929 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002930 }
2931 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002932 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002933}
2934
Chris Mason97571fd2007-02-24 13:39:08 -05002935/*
Chris Mason925baed2008-06-25 16:01:30 -04002936 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05002937 * returns 0 if it found something or 1 if there are no lesser leaves.
2938 * returns < 0 on io errors.
2939 */
2940int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2941{
Chris Mason925baed2008-06-25 16:01:30 -04002942 struct btrfs_key key;
2943 struct btrfs_disk_key found_key;
2944 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05002945
Chris Mason925baed2008-06-25 16:01:30 -04002946 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05002947
Chris Mason925baed2008-06-25 16:01:30 -04002948 if (key.offset > 0)
2949 key.offset--;
2950 else if (key.type > 0)
2951 key.type--;
2952 else if (key.objectid > 0)
2953 key.objectid--;
2954 else
2955 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002956
Chris Mason925baed2008-06-25 16:01:30 -04002957 btrfs_release_path(root, path);
2958 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2959 if (ret < 0)
2960 return ret;
2961 btrfs_item_key(path->nodes[0], &found_key, 0);
2962 ret = comp_keys(&found_key, &key);
2963 if (ret < 0)
2964 return 0;
2965 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002966}
2967
Chris Mason3f157a22008-06-25 16:01:31 -04002968/*
2969 * A helper function to walk down the tree starting at min_key, and looking
2970 * for nodes or leaves that are either in cache or have a minimum
2971 * transaction id. This is used by the btree defrag code, but could
2972 * also be used to search for blocks that have changed since a given
2973 * transaction id.
2974 *
2975 * This does not cow, but it does stuff the starting key it finds back
2976 * into min_key, so you can call btrfs_search_slot with cow=1 on the
2977 * key and get a writable path.
2978 *
2979 * This does lock as it descends, and path->keep_locks should be set
2980 * to 1 by the caller.
2981 *
2982 * This honors path->lowest_level to prevent descent past a given level
2983 * of the tree.
2984 *
2985 * returns zero if something useful was found, < 0 on error and 1 if there
2986 * was nothing in the tree that matched the search criteria.
2987 */
2988int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
2989 struct btrfs_path *path, int cache_only,
2990 u64 min_trans)
2991{
2992 struct extent_buffer *cur;
2993 struct btrfs_key found_key;
2994 int slot;
Yan96524802008-07-24 12:19:49 -04002995 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04002996 u32 nritems;
2997 int level;
2998 int ret = 1;
2999
3000again:
3001 cur = btrfs_lock_root_node(root);
3002 level = btrfs_header_level(cur);
3003 path->nodes[level] = cur;
3004 path->locks[level] = 1;
3005
3006 if (btrfs_header_generation(cur) < min_trans) {
3007 ret = 1;
3008 goto out;
3009 }
3010 while(1) {
3011 nritems = btrfs_header_nritems(cur);
3012 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003013 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003014
3015 /* at level = 0, we're done, setup the path and exit */
3016 if (level == 0) {
3017 ret = 0;
3018 path->slots[level] = slot;
3019 btrfs_item_key_to_cpu(cur, &found_key, slot);
3020 goto out;
3021 }
Yan96524802008-07-24 12:19:49 -04003022 if (sret && slot > 0)
3023 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003024 /*
3025 * check this node pointer against the cache_only and
3026 * min_trans parameters. If it isn't in cache or is too
3027 * old, skip to the next one.
3028 */
3029 while(slot < nritems) {
3030 u64 blockptr;
3031 u64 gen;
3032 struct extent_buffer *tmp;
3033 blockptr = btrfs_node_blockptr(cur, slot);
3034 gen = btrfs_node_ptr_generation(cur, slot);
3035 if (gen < min_trans) {
3036 slot++;
3037 continue;
3038 }
3039 if (!cache_only)
3040 break;
3041
3042 tmp = btrfs_find_tree_block(root, blockptr,
3043 btrfs_level_size(root, level - 1));
3044
3045 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3046 free_extent_buffer(tmp);
3047 break;
3048 }
3049 if (tmp)
3050 free_extent_buffer(tmp);
3051 slot++;
3052 }
3053 /*
3054 * we didn't find a candidate key in this node, walk forward
3055 * and find another one
3056 */
3057 if (slot >= nritems) {
3058 ret = btrfs_find_next_key(root, path, min_key, level,
3059 cache_only, min_trans);
3060 if (ret == 0) {
3061 btrfs_release_path(root, path);
3062 goto again;
3063 } else {
3064 goto out;
3065 }
3066 }
3067 /* save our key for returning back */
3068 btrfs_node_key_to_cpu(cur, &found_key, slot);
3069 path->slots[level] = slot;
3070 if (level == path->lowest_level) {
3071 ret = 0;
3072 unlock_up(path, level, 1);
3073 goto out;
3074 }
3075 cur = read_node_slot(root, cur, slot);
3076
3077 btrfs_tree_lock(cur);
3078 path->locks[level - 1] = 1;
3079 path->nodes[level - 1] = cur;
3080 unlock_up(path, level, 1);
3081 }
3082out:
3083 if (ret == 0)
3084 memcpy(min_key, &found_key, sizeof(found_key));
3085 return ret;
3086}
3087
3088/*
3089 * this is similar to btrfs_next_leaf, but does not try to preserve
3090 * and fixup the path. It looks for and returns the next key in the
3091 * tree based on the current path and the cache_only and min_trans
3092 * parameters.
3093 *
3094 * 0 is returned if another key is found, < 0 if there are any errors
3095 * and 1 is returned if there are no higher keys in the tree
3096 *
3097 * path->keep_locks should be set to 1 on the search made before
3098 * calling this function.
3099 */
Chris Masone7a84562008-06-25 16:01:31 -04003100int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003101 struct btrfs_key *key, int lowest_level,
3102 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003103{
3104 int level = lowest_level;
3105 int slot;
3106 struct extent_buffer *c;
3107
3108 while(level < BTRFS_MAX_LEVEL) {
3109 if (!path->nodes[level])
3110 return 1;
3111
3112 slot = path->slots[level] + 1;
3113 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003114next:
Chris Masone7a84562008-06-25 16:01:31 -04003115 if (slot >= btrfs_header_nritems(c)) {
3116 level++;
3117 if (level == BTRFS_MAX_LEVEL) {
3118 return 1;
3119 }
3120 continue;
3121 }
3122 if (level == 0)
3123 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003124 else {
3125 u64 blockptr = btrfs_node_blockptr(c, slot);
3126 u64 gen = btrfs_node_ptr_generation(c, slot);
3127
3128 if (cache_only) {
3129 struct extent_buffer *cur;
3130 cur = btrfs_find_tree_block(root, blockptr,
3131 btrfs_level_size(root, level - 1));
3132 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3133 slot++;
3134 if (cur)
3135 free_extent_buffer(cur);
3136 goto next;
3137 }
3138 free_extent_buffer(cur);
3139 }
3140 if (gen < min_trans) {
3141 slot++;
3142 goto next;
3143 }
Chris Masone7a84562008-06-25 16:01:31 -04003144 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003145 }
Chris Masone7a84562008-06-25 16:01:31 -04003146 return 0;
3147 }
3148 return 1;
3149}
3150
Chris Mason7bb86312007-12-11 09:25:06 -05003151/*
Chris Mason925baed2008-06-25 16:01:30 -04003152 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003153 * returns 0 if it found something or 1 if there are no greater leaves.
3154 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003155 */
Chris Mason234b63a2007-03-13 10:46:10 -04003156int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003157{
3158 int slot;
3159 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003160 struct extent_buffer *c;
3161 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003162 struct btrfs_key key;
3163 u32 nritems;
3164 int ret;
3165
3166 nritems = btrfs_header_nritems(path->nodes[0]);
3167 if (nritems == 0) {
3168 return 1;
3169 }
3170
3171 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3172
Chris Mason925baed2008-06-25 16:01:30 -04003173 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003174 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3176 path->keep_locks = 0;
3177
3178 if (ret < 0)
3179 return ret;
3180
Chris Masona2135012008-06-25 16:01:30 -04003181 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003182 /*
3183 * by releasing the path above we dropped all our locks. A balance
3184 * could have added more items next to the key that used to be
3185 * at the very end of the block. So, check again here and
3186 * advance the path if there are now more items available.
3187 */
Chris Masona2135012008-06-25 16:01:30 -04003188 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003189 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003190 goto done;
3191 }
Chris Masond97e63b2007-02-20 16:40:44 -05003192
Chris Mason234b63a2007-03-13 10:46:10 -04003193 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003194 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003195 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003196
Chris Masond97e63b2007-02-20 16:40:44 -05003197 slot = path->slots[level] + 1;
3198 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003199 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003200 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003201 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003202 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003203 }
Chris Masond97e63b2007-02-20 16:40:44 -05003204 continue;
3205 }
Chris Mason5f39d392007-10-15 16:14:19 -04003206
Chris Mason925baed2008-06-25 16:01:30 -04003207 if (next) {
3208 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003209 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003210 }
Chris Mason5f39d392007-10-15 16:14:19 -04003211
Chris Mason0bd40a72008-07-17 12:54:43 -04003212 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3213 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003214 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003215
Chris Masonca7a79a2008-05-12 12:59:19 -04003216 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003217 if (!path->skip_locking) {
3218 WARN_ON(!btrfs_tree_locked(c));
3219 btrfs_tree_lock(next);
3220 }
Chris Masond97e63b2007-02-20 16:40:44 -05003221 break;
3222 }
3223 path->slots[level] = slot;
3224 while(1) {
3225 level--;
3226 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003227 if (path->locks[level])
3228 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003229 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003230 path->nodes[level] = next;
3231 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003232 if (!path->skip_locking)
3233 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003234 if (!level)
3235 break;
Chris Mason925baed2008-06-25 16:01:30 -04003236 if (level == 1 && path->locks[1] && path->reada)
3237 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003238 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003239 if (!path->skip_locking) {
3240 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3241 btrfs_tree_lock(next);
3242 }
Chris Masond97e63b2007-02-20 16:40:44 -05003243 }
Chris Mason925baed2008-06-25 16:01:30 -04003244done:
3245 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003246 return 0;
3247}
Chris Mason0b86a832008-03-24 15:01:56 -04003248
Chris Mason3f157a22008-06-25 16:01:31 -04003249/*
3250 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3251 * searching until it gets past min_objectid or finds an item of 'type'
3252 *
3253 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3254 */
Chris Mason0b86a832008-03-24 15:01:56 -04003255int btrfs_previous_item(struct btrfs_root *root,
3256 struct btrfs_path *path, u64 min_objectid,
3257 int type)
3258{
3259 struct btrfs_key found_key;
3260 struct extent_buffer *leaf;
3261 int ret;
3262
3263 while(1) {
3264 if (path->slots[0] == 0) {
3265 ret = btrfs_prev_leaf(root, path);
3266 if (ret != 0)
3267 return ret;
3268 } else {
3269 path->slots[0]--;
3270 }
3271 leaf = path->nodes[0];
3272 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3273 if (found_key.type == type)
3274 return 0;
3275 }
3276 return 1;
3277}
3278