blob: 7114faafa9d48753887d81551b3c8d3761ace501 [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);
Yan Zheng31153d82008-07-28 15:32:19 -0400168 ret = btrfs_inc_ref(trans, new_root, buf, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500169 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,
Chris Mason65b51a02008-08-01 15:11:20 -0400184 u64 search_start, u64 empty_size,
185 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400186{
Chris Mason7bb86312007-12-11 09:25:06 -0500187 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400188 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500189 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400190 int ret = 0;
191 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500192 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400193 int unlock_orig = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500194 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400195
Chris Mason925baed2008-06-25 16:01:30 -0400196 if (*cow_ret == buf)
197 unlock_orig = 1;
198
199 WARN_ON(!btrfs_tree_locked(buf));
200
Chris Mason7bb86312007-12-11 09:25:06 -0500201 if (root->ref_cows) {
202 root_gen = trans->transid;
203 } else {
204 root_gen = 0;
205 }
Chris Mason7bb86312007-12-11 09:25:06 -0500206 WARN_ON(root->ref_cows && trans->transid !=
207 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400208 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400209
Chris Mason7bb86312007-12-11 09:25:06 -0500210 level = btrfs_header_level(buf);
211 nritems = btrfs_header_nritems(buf);
212 if (nritems) {
213 if (level == 0)
214 btrfs_item_key_to_cpu(buf, &first_key, 0);
215 else
216 btrfs_node_key_to_cpu(buf, &first_key, 0);
217 } else {
218 first_key.objectid = 0;
219 }
Chris Mason65b51a02008-08-01 15:11:20 -0400220 if (prealloc_dest) {
221 struct btrfs_key ins;
222
223 ins.objectid = prealloc_dest;
224 ins.offset = buf->len;
225 ins.type = BTRFS_EXTENT_ITEM_KEY;
226
227 ret = btrfs_alloc_reserved_extent(trans, root,
228 root->root_key.objectid,
229 root_gen, level,
230 first_key.objectid,
231 &ins);
232 BUG_ON(ret);
233 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
234 buf->len);
235 } else {
236 cow = btrfs_alloc_free_block(trans, root, buf->len,
237 root->root_key.objectid,
238 root_gen, first_key.objectid,
239 level, search_start, empty_size);
240 }
Chris Mason6702ed42007-08-07 16:15:09 -0400241 if (IS_ERR(cow))
242 return PTR_ERR(cow);
243
Chris Mason5f39d392007-10-15 16:14:19 -0400244 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400245 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400246 btrfs_set_header_generation(cow, trans->transid);
247 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400248 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400249
Chris Mason5f39d392007-10-15 16:14:19 -0400250 WARN_ON(btrfs_header_generation(buf) > trans->transid);
251 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400252 different_trans = 1;
Yan Zheng31153d82008-07-28 15:32:19 -0400253 ret = btrfs_inc_ref(trans, root, buf, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400254 if (ret)
255 return ret;
256 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400257 clean_tree_block(trans, root, buf);
258 }
259
260 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400261 WARN_ON(parent && parent != buf);
Chris Mason7bb86312007-12-11 09:25:06 -0500262 root_gen = btrfs_header_generation(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400263
264 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400265 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400266 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400267 spin_unlock(&root->node_lock);
268
Chris Mason6702ed42007-08-07 16:15:09 -0400269 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400270 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500271 buf->len, root->root_key.objectid,
272 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400273 }
Chris Mason5f39d392007-10-15 16:14:19 -0400274 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400275 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400276 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500277 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400278 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400279 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500280 WARN_ON(trans->transid == 0);
281 btrfs_set_node_ptr_generation(parent, parent_slot,
282 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400283 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400284 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500285 btrfs_free_extent(trans, root, buf->start, buf->len,
286 btrfs_header_owner(parent), root_gen,
287 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400288 }
Chris Mason925baed2008-06-25 16:01:30 -0400289 if (unlock_orig)
290 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400291 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400292 btrfs_mark_buffer_dirty(cow);
293 *cow_ret = cow;
294 return 0;
295}
296
Chris Mason5f39d392007-10-15 16:14:19 -0400297int btrfs_cow_block(struct btrfs_trans_handle *trans,
298 struct btrfs_root *root, struct extent_buffer *buf,
299 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400300 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500301{
Chris Mason6702ed42007-08-07 16:15:09 -0400302 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500303 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400304 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500305
Chris Masonccd467d2007-06-28 15:57:36 -0400306 if (trans->transaction != root->fs_info->running_transaction) {
307 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
308 root->fs_info->running_transaction->transid);
309 WARN_ON(1);
310 }
311 if (trans->transid != root->fs_info->generation) {
312 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
313 root->fs_info->generation);
314 WARN_ON(1);
315 }
Chris Masondc17ff82008-01-08 15:46:30 -0500316
317 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400318 spin_lock(&root->fs_info->hash_lock);
319 if (header_trans == trans->transid &&
320 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500321 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400322 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400323 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500324 return 0;
325 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400326 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400327 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400328 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400329 parent_slot, cow_ret, search_start, 0,
330 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400331 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400332}
333
Chris Mason6b800532007-10-15 16:17:34 -0400334static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400335{
Chris Mason6b800532007-10-15 16:17:34 -0400336 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400337 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400338 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400339 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500340 return 0;
341}
342
Chris Mason081e9572007-11-06 10:26:24 -0500343/*
344 * compare two keys in a memcmp fashion
345 */
346static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
347{
348 struct btrfs_key k1;
349
350 btrfs_disk_key_to_cpu(&k1, disk);
351
352 if (k1.objectid > k2->objectid)
353 return 1;
354 if (k1.objectid < k2->objectid)
355 return -1;
356 if (k1.type > k2->type)
357 return 1;
358 if (k1.type < k2->type)
359 return -1;
360 if (k1.offset > k2->offset)
361 return 1;
362 if (k1.offset < k2->offset)
363 return -1;
364 return 0;
365}
366
367
Chris Mason6702ed42007-08-07 16:15:09 -0400368int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400370 int start_slot, int cache_only, u64 *last_ret,
371 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400372{
Chris Mason6b800532007-10-15 16:17:34 -0400373 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400374 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400375 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400376 u64 search_start = *last_ret;
377 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400378 u64 other;
379 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400380 int end_slot;
381 int i;
382 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400383 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400384 int uptodate;
385 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500386 int progress_passed = 0;
387 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400388
Chris Mason5708b952007-10-25 15:43:18 -0400389 parent_level = btrfs_header_level(parent);
390 if (cache_only && parent_level != 1)
391 return 0;
392
Chris Mason6702ed42007-08-07 16:15:09 -0400393 if (trans->transaction != root->fs_info->running_transaction) {
394 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
395 root->fs_info->running_transaction->transid);
396 WARN_ON(1);
397 }
398 if (trans->transid != root->fs_info->generation) {
399 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
400 root->fs_info->generation);
401 WARN_ON(1);
402 }
Chris Mason86479a02007-09-10 19:58:16 -0400403
Chris Mason6b800532007-10-15 16:17:34 -0400404 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400405 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400406 end_slot = parent_nritems;
407
408 if (parent_nritems == 1)
409 return 0;
410
411 for (i = start_slot; i < end_slot; i++) {
412 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400413
Chris Mason5708b952007-10-25 15:43:18 -0400414 if (!parent->map_token) {
415 map_extent_buffer(parent,
416 btrfs_node_key_ptr_offset(i),
417 sizeof(struct btrfs_key_ptr),
418 &parent->map_token, &parent->kaddr,
419 &parent->map_start, &parent->map_len,
420 KM_USER1);
421 }
Chris Mason081e9572007-11-06 10:26:24 -0500422 btrfs_node_key(parent, &disk_key, i);
423 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
424 continue;
425
426 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400427 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400428 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400429 if (last_block == 0)
430 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400431
Chris Mason6702ed42007-08-07 16:15:09 -0400432 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400433 other = btrfs_node_blockptr(parent, i - 1);
434 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400435 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400436 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400437 other = btrfs_node_blockptr(parent, i + 1);
438 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400439 }
Chris Masone9d0b132007-08-10 14:06:19 -0400440 if (close) {
441 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400442 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400443 }
Chris Mason5708b952007-10-25 15:43:18 -0400444 if (parent->map_token) {
445 unmap_extent_buffer(parent, parent->map_token,
446 KM_USER1);
447 parent->map_token = NULL;
448 }
Chris Mason6702ed42007-08-07 16:15:09 -0400449
Chris Mason6b800532007-10-15 16:17:34 -0400450 cur = btrfs_find_tree_block(root, blocknr, blocksize);
451 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400452 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400453 else
454 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400455 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400456 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400457 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400458 continue;
459 }
Chris Mason6b800532007-10-15 16:17:34 -0400460 if (!cur) {
461 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400462 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400463 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400464 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400465 }
Chris Mason6702ed42007-08-07 16:15:09 -0400466 }
Chris Masone9d0b132007-08-10 14:06:19 -0400467 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400468 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400469
Chris Masone7a84562008-06-25 16:01:31 -0400470 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400471 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400472 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400473 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400474 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400475 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400476 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400477 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400478 break;
Yan252c38f2007-08-29 09:11:44 -0400479 }
Chris Masone7a84562008-06-25 16:01:31 -0400480 search_start = cur->start;
481 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400482 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400483 btrfs_tree_unlock(cur);
484 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400485 }
Chris Mason5708b952007-10-25 15:43:18 -0400486 if (parent->map_token) {
487 unmap_extent_buffer(parent, parent->map_token,
488 KM_USER1);
489 parent->map_token = NULL;
490 }
Chris Mason6702ed42007-08-07 16:15:09 -0400491 return err;
492}
493
Chris Mason74123bd2007-02-02 11:05:29 -0500494/*
495 * The leaf data grows from end-to-front in the node.
496 * this returns the address of the start of the last item,
497 * which is the stop of the leaf data stack
498 */
Chris Mason123abc82007-03-14 14:14:43 -0400499static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400500 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500501{
Chris Mason5f39d392007-10-15 16:14:19 -0400502 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500503 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400504 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400505 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500506}
507
Chris Mason123abc82007-03-14 14:14:43 -0400508static int check_node(struct btrfs_root *root, struct btrfs_path *path,
509 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500510{
Chris Mason5f39d392007-10-15 16:14:19 -0400511 struct extent_buffer *parent = NULL;
512 struct extent_buffer *node = path->nodes[level];
513 struct btrfs_disk_key parent_key;
514 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500515 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400516 int slot;
517 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400518 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500519
520 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400521 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400522
Chris Mason8d7be552007-05-10 11:24:42 -0400523 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400524 BUG_ON(nritems == 0);
525 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400526 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400527 btrfs_node_key(parent, &parent_key, parent_slot);
528 btrfs_node_key(node, &node_key, 0);
529 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400530 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400531 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400532 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500533 }
Chris Mason123abc82007-03-14 14:14:43 -0400534 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400535 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400536 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
537 btrfs_node_key(node, &node_key, slot);
538 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400539 }
540 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400541 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
542 btrfs_node_key(node, &node_key, slot);
543 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500544 }
545 return 0;
546}
547
Chris Mason123abc82007-03-14 14:14:43 -0400548static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
549 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500550{
Chris Mason5f39d392007-10-15 16:14:19 -0400551 struct extent_buffer *leaf = path->nodes[level];
552 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400554 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400555 struct btrfs_disk_key parent_key;
556 struct btrfs_disk_key leaf_key;
557 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400558
Chris Mason5f39d392007-10-15 16:14:19 -0400559 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500560
561 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400562 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400563
564 if (nritems == 0)
565 return 0;
566
567 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400568 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400569 btrfs_node_key(parent, &parent_key, parent_slot);
570 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400571
Chris Mason5f39d392007-10-15 16:14:19 -0400572 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400573 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400574 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400575 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500576 }
Chris Mason5f39d392007-10-15 16:14:19 -0400577#if 0
578 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
579 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
580 btrfs_item_key(leaf, &leaf_key, i);
581 if (comp_keys(&leaf_key, &cpukey) >= 0) {
582 btrfs_print_leaf(root, leaf);
583 printk("slot %d offset bad key\n", i);
584 BUG_ON(1);
585 }
586 if (btrfs_item_offset_nr(leaf, i) !=
587 btrfs_item_end_nr(leaf, i + 1)) {
588 btrfs_print_leaf(root, leaf);
589 printk("slot %d offset bad\n", i);
590 BUG_ON(1);
591 }
592 if (i == 0) {
593 if (btrfs_item_offset_nr(leaf, i) +
594 btrfs_item_size_nr(leaf, i) !=
595 BTRFS_LEAF_DATA_SIZE(root)) {
596 btrfs_print_leaf(root, leaf);
597 printk("slot %d first offset bad\n", i);
598 BUG_ON(1);
599 }
600 }
601 }
602 if (nritems > 0) {
603 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
604 btrfs_print_leaf(root, leaf);
605 printk("slot %d bad size \n", nritems - 1);
606 BUG_ON(1);
607 }
608 }
609#endif
610 if (slot != 0 && slot < nritems - 1) {
611 btrfs_item_key(leaf, &leaf_key, slot);
612 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
613 if (comp_keys(&leaf_key, &cpukey) <= 0) {
614 btrfs_print_leaf(root, leaf);
615 printk("slot %d offset bad key\n", slot);
616 BUG_ON(1);
617 }
618 if (btrfs_item_offset_nr(leaf, slot - 1) !=
619 btrfs_item_end_nr(leaf, slot)) {
620 btrfs_print_leaf(root, leaf);
621 printk("slot %d offset bad\n", slot);
622 BUG_ON(1);
623 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500624 }
Chris Mason8d7be552007-05-10 11:24:42 -0400625 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400626 btrfs_item_key(leaf, &leaf_key, slot);
627 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
628 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
629 if (btrfs_item_offset_nr(leaf, slot) !=
630 btrfs_item_end_nr(leaf, slot + 1)) {
631 btrfs_print_leaf(root, leaf);
632 printk("slot %d offset bad\n", slot);
633 BUG_ON(1);
634 }
Chris Mason8d7be552007-05-10 11:24:42 -0400635 }
Chris Mason5f39d392007-10-15 16:14:19 -0400636 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
637 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500638 return 0;
639}
640
Chris Mason98ed5172008-01-03 10:01:48 -0500641static int noinline check_block(struct btrfs_root *root,
642 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500643{
Chris Masonf1885912008-04-09 16:28:12 -0400644 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400645 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400646 if (btrfs_header_level(path->nodes[level]) != level)
647 printk("warning: bad level %Lu wanted %d found %d\n",
648 path->nodes[level]->start, level,
649 btrfs_header_level(path->nodes[level]));
650 found_start = btrfs_header_bytenr(path->nodes[level]);
651 if (found_start != path->nodes[level]->start) {
652 printk("warning: bad bytentr %Lu found %Lu\n",
653 path->nodes[level]->start, found_start);
654 }
Chris Masondb945352007-10-15 16:15:53 -0400655#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400656 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400657
Chris Mason479965d2007-10-15 16:14:27 -0400658 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
659 (unsigned long)btrfs_header_fsid(buf),
660 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400661 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400662 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400663 }
Chris Masondb945352007-10-15 16:15:53 -0400664#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500665 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400666 return check_leaf(root, path, level);
667 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500668}
669
Chris Mason74123bd2007-02-02 11:05:29 -0500670/*
Chris Mason5f39d392007-10-15 16:14:19 -0400671 * search for key in the extent_buffer. The items start at offset p,
672 * and they are item_size apart. There are 'max' items in p.
673 *
Chris Mason74123bd2007-02-02 11:05:29 -0500674 * the slot in the array is returned via slot, and it points to
675 * the place where you would insert key if it is not found in
676 * the array.
677 *
678 * slot may point to max if the key is bigger than all of the keys
679 */
Chris Mason5f39d392007-10-15 16:14:19 -0400680static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
681 int item_size, struct btrfs_key *key,
682 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500683{
684 int low = 0;
685 int high = max;
686 int mid;
687 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400688 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400689 struct btrfs_disk_key unaligned;
690 unsigned long offset;
691 char *map_token = NULL;
692 char *kaddr = NULL;
693 unsigned long map_start = 0;
694 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400695 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500696
697 while(low < high) {
698 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400699 offset = p + mid * item_size;
700
701 if (!map_token || offset < map_start ||
702 (offset + sizeof(struct btrfs_disk_key)) >
703 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400704 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400705 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400706 map_token = NULL;
707 }
708 err = map_extent_buffer(eb, offset,
709 sizeof(struct btrfs_disk_key),
710 &map_token, &kaddr,
711 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400712
Chris Mason479965d2007-10-15 16:14:27 -0400713 if (!err) {
714 tmp = (struct btrfs_disk_key *)(kaddr + offset -
715 map_start);
716 } else {
717 read_extent_buffer(eb, &unaligned,
718 offset, sizeof(unaligned));
719 tmp = &unaligned;
720 }
721
Chris Mason5f39d392007-10-15 16:14:19 -0400722 } else {
723 tmp = (struct btrfs_disk_key *)(kaddr + offset -
724 map_start);
725 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726 ret = comp_keys(tmp, key);
727
728 if (ret < 0)
729 low = mid + 1;
730 else if (ret > 0)
731 high = mid;
732 else {
733 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400734 if (map_token)
735 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500736 return 0;
737 }
738 }
739 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400740 if (map_token)
741 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500742 return 1;
743}
744
Chris Mason97571fd2007-02-24 13:39:08 -0500745/*
746 * simple bin_search frontend that does the right thing for
747 * leaves vs nodes
748 */
Chris Mason5f39d392007-10-15 16:14:19 -0400749static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
750 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500751{
Chris Mason5f39d392007-10-15 16:14:19 -0400752 if (level == 0) {
753 return generic_bin_search(eb,
754 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400755 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400756 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400757 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500758 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400759 return generic_bin_search(eb,
760 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400761 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400762 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400763 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500764 }
765 return -1;
766}
767
Chris Mason5f39d392007-10-15 16:14:19 -0400768static struct extent_buffer *read_node_slot(struct btrfs_root *root,
769 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500770{
Chris Masonca7a79a2008-05-12 12:59:19 -0400771 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500772 if (slot < 0)
773 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500775 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400776
777 BUG_ON(level == 0);
778
Chris Masondb945352007-10-15 16:15:53 -0400779 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400780 btrfs_level_size(root, level - 1),
781 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500782}
783
Chris Mason98ed5172008-01-03 10:01:48 -0500784static int balance_level(struct btrfs_trans_handle *trans,
785 struct btrfs_root *root,
786 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500787{
Chris Mason5f39d392007-10-15 16:14:19 -0400788 struct extent_buffer *right = NULL;
789 struct extent_buffer *mid;
790 struct extent_buffer *left = NULL;
791 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500792 int ret = 0;
793 int wret;
794 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500795 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400796 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500797 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500798
799 if (level == 0)
800 return 0;
801
Chris Mason5f39d392007-10-15 16:14:19 -0400802 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400803 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500804 WARN_ON(btrfs_header_generation(mid) != trans->transid);
805
Chris Mason1d4f8a02007-03-13 09:28:32 -0400806 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500807
Chris Mason234b63a2007-03-13 10:46:10 -0400808 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400809 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500810 pslot = path->slots[level + 1];
811
Chris Mason40689472007-03-17 14:29:23 -0400812 /*
813 * deal with the case where there is only one pointer in the root
814 * by promoting the node below to a root
815 */
Chris Mason5f39d392007-10-15 16:14:19 -0400816 if (!parent) {
817 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500818
Chris Mason5f39d392007-10-15 16:14:19 -0400819 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500820 return 0;
821
822 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400823 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400824 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500825 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400826 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500827 BUG_ON(ret);
828
Chris Mason925baed2008-06-25 16:01:30 -0400829 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500830 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400831 spin_unlock(&root->node_lock);
832
Chris Mason0b86a832008-03-24 15:01:56 -0400833 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400834 btrfs_tree_unlock(child);
835 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500836 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400837 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400838 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500839 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400840 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500841 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
842 root->root_key.objectid,
843 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500844 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400845 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400846 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500847 }
Chris Mason5f39d392007-10-15 16:14:19 -0400848 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400849 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500850 return 0;
851
Chris Mason5f39d392007-10-15 16:14:19 -0400852 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400853 err_on_enospc = 1;
854
Chris Mason5f39d392007-10-15 16:14:19 -0400855 left = read_node_slot(root, parent, pslot - 1);
856 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400857 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400858 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400859 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400860 if (wret) {
861 ret = wret;
862 goto enospc;
863 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400864 }
Chris Mason5f39d392007-10-15 16:14:19 -0400865 right = read_node_slot(root, parent, pslot + 1);
866 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400867 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400868 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400869 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400870 if (wret) {
871 ret = wret;
872 goto enospc;
873 }
874 }
875
876 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400877 if (left) {
878 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400879 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500880 if (wret < 0)
881 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400883 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500884 }
Chris Mason79f95c82007-03-01 15:16:26 -0500885
886 /*
887 * then try to empty the right most buffer into the middle
888 */
Chris Mason5f39d392007-10-15 16:14:19 -0400889 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400890 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400891 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500892 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400893 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400894 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500895 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400896 u32 blocksize = right->len;
897
Chris Mason5f39d392007-10-15 16:14:19 -0400898 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400899 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400900 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500901 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400902 wret = del_ptr(trans, root, path, level + 1, pslot +
903 1);
Chris Masonbb803952007-03-01 12:04:21 -0500904 if (wret)
905 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400906 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500907 blocksize,
908 btrfs_header_owner(parent),
909 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500910 if (wret)
911 ret = wret;
912 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400913 struct btrfs_disk_key right_key;
914 btrfs_node_key(right, &right_key, 0);
915 btrfs_set_node_key(parent, &right_key, pslot + 1);
916 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500917 }
918 }
Chris Mason5f39d392007-10-15 16:14:19 -0400919 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500920 /*
921 * we're not allowed to leave a node with one item in the
922 * tree during a delete. A deletion from lower in the tree
923 * could try to delete the only pointer in this node.
924 * So, pull some keys from the left.
925 * There has to be a left pointer at this point because
926 * otherwise we would have pulled some pointers from the
927 * right
928 */
Chris Mason5f39d392007-10-15 16:14:19 -0400929 BUG_ON(!left);
930 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400931 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500932 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400933 goto enospc;
934 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400935 if (wret == 1) {
936 wret = push_node_left(trans, root, left, mid, 1);
937 if (wret < 0)
938 ret = wret;
939 }
Chris Mason79f95c82007-03-01 15:16:26 -0500940 BUG_ON(wret == 1);
941 }
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500943 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500944 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400945 u64 bytenr = mid->start;
946 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400947
Chris Mason5f39d392007-10-15 16:14:19 -0400948 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400949 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400950 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500951 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400952 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500953 if (wret)
954 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500955 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
956 btrfs_header_owner(parent),
957 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500958 if (wret)
959 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500960 } else {
961 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400962 struct btrfs_disk_key mid_key;
963 btrfs_node_key(mid, &mid_key, 0);
964 btrfs_set_node_key(parent, &mid_key, pslot);
965 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500966 }
Chris Masonbb803952007-03-01 12:04:21 -0500967
Chris Mason79f95c82007-03-01 15:16:26 -0500968 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400969 if (left) {
970 if (btrfs_header_nritems(left) > orig_slot) {
971 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400972 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400973 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500974 path->slots[level + 1] -= 1;
975 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400976 if (mid) {
977 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400978 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400979 }
Chris Masonbb803952007-03-01 12:04:21 -0500980 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400981 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500982 path->slots[level] = orig_slot;
983 }
984 }
Chris Mason79f95c82007-03-01 15:16:26 -0500985 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400986 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400987 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400988 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500989 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400990enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400991 if (right) {
992 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400993 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400994 }
995 if (left) {
996 if (path->nodes[level] != left)
997 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400998 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400999 }
Chris Masonbb803952007-03-01 12:04:21 -05001000 return ret;
1001}
1002
Chris Masone66f7092007-04-20 13:16:02 -04001003/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -05001004static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001007{
Chris Mason5f39d392007-10-15 16:14:19 -04001008 struct extent_buffer *right = NULL;
1009 struct extent_buffer *mid;
1010 struct extent_buffer *left = NULL;
1011 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001012 int ret = 0;
1013 int wret;
1014 int pslot;
1015 int orig_slot = path->slots[level];
1016 u64 orig_ptr;
1017
1018 if (level == 0)
1019 return 1;
1020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001022 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001023 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1024
1025 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001026 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001027 pslot = path->slots[level + 1];
1028
Chris Mason5f39d392007-10-15 16:14:19 -04001029 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001030 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001031
Chris Mason5f39d392007-10-15 16:14:19 -04001032 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001033
1034 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001036 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001037
1038 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001039 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001040 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1041 wret = 1;
1042 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001043 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001044 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001045 if (ret)
1046 wret = 1;
1047 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001048 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001049 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001050 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001051 }
Chris Masone66f7092007-04-20 13:16:02 -04001052 if (wret < 0)
1053 ret = wret;
1054 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001055 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001056 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001057 btrfs_node_key(mid, &disk_key, 0);
1058 btrfs_set_node_key(parent, &disk_key, pslot);
1059 btrfs_mark_buffer_dirty(parent);
1060 if (btrfs_header_nritems(left) > orig_slot) {
1061 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001062 path->slots[level + 1] -= 1;
1063 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001064 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001065 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001066 } else {
1067 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001068 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001069 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001070 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001071 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001072 }
Chris Masone66f7092007-04-20 13:16:02 -04001073 return 0;
1074 }
Chris Mason925baed2008-06-25 16:01:30 -04001075 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001077 }
Chris Mason925baed2008-06-25 16:01:30 -04001078 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001079
1080 /*
1081 * then try to empty the right most buffer into the middle
1082 */
Chris Mason5f39d392007-10-15 16:14:19 -04001083 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001084 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001085 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001086 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001087 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1088 wret = 1;
1089 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001090 ret = btrfs_cow_block(trans, root, right,
1091 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001092 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001093 if (ret)
1094 wret = 1;
1095 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001096 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001097 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001098 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001099 }
Chris Masone66f7092007-04-20 13:16:02 -04001100 if (wret < 0)
1101 ret = wret;
1102 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001103 struct btrfs_disk_key disk_key;
1104
1105 btrfs_node_key(right, &disk_key, 0);
1106 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1107 btrfs_mark_buffer_dirty(parent);
1108
1109 if (btrfs_header_nritems(mid) <= orig_slot) {
1110 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001111 path->slots[level + 1] += 1;
1112 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001113 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001114 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001115 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001116 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001117 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001118 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001119 }
Chris Masone66f7092007-04-20 13:16:02 -04001120 return 0;
1121 }
Chris Mason925baed2008-06-25 16:01:30 -04001122 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001124 }
Chris Masone66f7092007-04-20 13:16:02 -04001125 return 1;
1126}
1127
Chris Mason74123bd2007-02-02 11:05:29 -05001128/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001129 * readahead one full node of leaves
1130 */
1131static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001132 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001133{
Chris Mason5f39d392007-10-15 16:14:19 -04001134 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001135 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001136 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001137 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001138 u64 lowest_read;
1139 u64 highest_read;
1140 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001141 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001142 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001143 u32 nr;
1144 u32 blocksize;
1145 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001146
Chris Masona6b6e752007-10-15 16:22:39 -04001147 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001148 return;
1149
Chris Mason6702ed42007-08-07 16:15:09 -04001150 if (!path->nodes[level])
1151 return;
1152
Chris Mason5f39d392007-10-15 16:14:19 -04001153 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001154
Chris Mason3c69fae2007-08-07 15:52:22 -04001155 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001156 blocksize = btrfs_level_size(root, level - 1);
1157 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (eb) {
1159 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001160 return;
1161 }
1162
Chris Mason6b800532007-10-15 16:17:34 -04001163 highest_read = search;
1164 lowest_read = search;
1165
Chris Mason5f39d392007-10-15 16:14:19 -04001166 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001167 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001168 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001169 if (direction < 0) {
1170 if (nr == 0)
1171 break;
1172 nr--;
1173 } else if (direction > 0) {
1174 nr++;
1175 if (nr >= nritems)
1176 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001177 }
Chris Mason01f46652007-12-21 16:24:26 -05001178 if (path->reada < 0 && objectid) {
1179 btrfs_node_key(node, &disk_key, nr);
1180 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1181 break;
1182 }
Chris Mason6b800532007-10-15 16:17:34 -04001183 search = btrfs_node_blockptr(node, nr);
1184 if ((search >= lowest_read && search <= highest_read) ||
1185 (search < lowest_read && lowest_read - search <= 32768) ||
1186 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001187 readahead_tree_block(root, search, blocksize,
1188 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001189 nread += blocksize;
1190 }
1191 nscan++;
1192 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1193 break;
1194 if(nread > (1024 * 1024) || nscan > 128)
1195 break;
1196
1197 if (search < lowest_read)
1198 lowest_read = search;
1199 if (search > highest_read)
1200 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001201 }
1202}
Chris Mason925baed2008-06-25 16:01:30 -04001203
1204static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1205{
1206 int i;
1207 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001208 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001209 struct extent_buffer *t;
1210
1211 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1212 if (!path->nodes[i])
1213 break;
1214 if (!path->locks[i])
1215 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001216 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001217 skip_level = i + 1;
1218 continue;
1219 }
Chris Mason051e1b92008-06-25 16:01:30 -04001220 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001221 u32 nritems;
1222 t = path->nodes[i];
1223 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001224 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001225 skip_level = i + 1;
1226 continue;
1227 }
1228 }
Chris Mason051e1b92008-06-25 16:01:30 -04001229 if (skip_level < i && i >= lowest_unlock)
1230 no_skips = 1;
1231
Chris Mason925baed2008-06-25 16:01:30 -04001232 t = path->nodes[i];
1233 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1234 btrfs_tree_unlock(t);
1235 path->locks[i] = 0;
1236 }
1237 }
1238}
1239
Chris Mason3c69fae2007-08-07 15:52:22 -04001240/*
Chris Mason74123bd2007-02-02 11:05:29 -05001241 * look for key in the tree. path is filled in with nodes along the way
1242 * if key is found, we return zero and you can find the item in the leaf
1243 * level of the path (level 0)
1244 *
1245 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001246 * be inserted, and 1 is returned. If there are other errors during the
1247 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001248 *
1249 * if ins_len > 0, nodes and leaves will be split as we walk down the
1250 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1251 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001252 */
Chris Masone089f052007-03-16 16:20:31 -04001253int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1254 *root, struct btrfs_key *key, struct btrfs_path *p, int
1255 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256{
Chris Mason5f39d392007-10-15 16:14:19 -04001257 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001258 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 int slot;
1260 int ret;
1261 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001262 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001263 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001264 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001265 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001266 u64 blocknr;
1267 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001268 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001269
Chris Mason6702ed42007-08-07 16:15:09 -04001270 lowest_level = p->lowest_level;
1271 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001272 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001273 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001274 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001275 if (ins_len < 0)
1276 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001277
1278 prealloc_block.objectid = 0;
1279
Chris Masonbb803952007-03-01 12:04:21 -05001280again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001281 if (p->skip_locking)
1282 b = btrfs_root_node(root);
1283 else
1284 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001285
Chris Masoneb60cea2007-02-02 09:18:22 -05001286 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001287 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001288
1289 /*
1290 * setup the path here so we can release it under lock
1291 * contention with the cow code
1292 */
1293 p->nodes[level] = b;
1294 if (!p->skip_locking)
1295 p->locks[level] = 1;
1296
Chris Mason02217ed2007-03-02 16:08:05 -05001297 if (cow) {
1298 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001299
1300 /* is a cow on this block not required */
1301 spin_lock(&root->fs_info->hash_lock);
1302 if (btrfs_header_generation(b) == trans->transid &&
1303 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1304 spin_unlock(&root->fs_info->hash_lock);
1305 goto cow_done;
1306 }
1307 spin_unlock(&root->fs_info->hash_lock);
1308
1309 /* ok, we have to cow, is our old prealloc the right
1310 * size?
1311 */
1312 if (prealloc_block.objectid &&
1313 prealloc_block.offset != b->len) {
1314 btrfs_free_reserved_extent(root,
1315 prealloc_block.objectid,
1316 prealloc_block.offset);
1317 prealloc_block.objectid = 0;
1318 }
1319
1320 /*
1321 * for higher level blocks, try not to allocate blocks
1322 * with the block and the parent locks held.
1323 */
1324 if (level > 1 && !prealloc_block.objectid &&
1325 btrfs_path_lock_waiting(p, level)) {
1326 u32 size = b->len;
1327 u64 hint = b->start;
1328
1329 btrfs_release_path(root, p);
1330 ret = btrfs_reserve_extent(trans, root,
1331 size, size, 0,
1332 hint, (u64)-1,
1333 &prealloc_block, 0);
1334 BUG_ON(ret);
1335 goto again;
1336 }
1337
Chris Masone20d96d2007-03-22 12:13:20 -04001338 wret = btrfs_cow_block(trans, root, b,
1339 p->nodes[level + 1],
1340 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001341 &b, prealloc_block.objectid);
1342 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001343 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001344 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001345 ret = wret;
1346 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001347 }
Chris Mason02217ed2007-03-02 16:08:05 -05001348 }
Chris Mason65b51a02008-08-01 15:11:20 -04001349cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001350 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001351 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001352 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001353 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001354
Chris Masoneb60cea2007-02-02 09:18:22 -05001355 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001356 if (!p->skip_locking)
1357 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001358
Chris Mason123abc82007-03-14 14:14:43 -04001359 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001360 if (ret) {
1361 ret = -1;
1362 goto done;
1363 }
Chris Mason925baed2008-06-25 16:01:30 -04001364
Chris Mason5f39d392007-10-15 16:14:19 -04001365 ret = bin_search(b, key, level, &slot);
1366 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001367 if (ret && slot > 0)
1368 slot -= 1;
1369 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001371 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001372 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001373 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001374 if (sret) {
1375 ret = sret;
1376 goto done;
1377 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001378 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001379 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001380 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001381 int sret = balance_level(trans, root, p,
1382 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001383 if (sret) {
1384 ret = sret;
1385 goto done;
1386 }
Chris Masonbb803952007-03-01 12:04:21 -05001387 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001388 if (!b) {
1389 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001390 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001391 }
Chris Masonbb803952007-03-01 12:04:21 -05001392 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001393 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001394 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001395 unlock_up(p, level, lowest_unlock);
1396
Chris Mason9f3a7422007-08-07 15:52:19 -04001397 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001398 if (level == lowest_level) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001399 break;
Chris Mason925baed2008-06-25 16:01:30 -04001400 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001401
Chris Mason594a24e2008-06-25 16:01:30 -04001402 blocknr = btrfs_node_blockptr(b, slot);
1403 gen = btrfs_node_ptr_generation(b, slot);
1404 blocksize = btrfs_level_size(root, level - 1);
1405
1406 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1407 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001408 b = tmp;
1409 } else {
1410 /*
1411 * reduce lock contention at high levels
1412 * of the btree by dropping locks before
1413 * we read.
1414 */
1415 if (level > 1) {
1416 btrfs_release_path(NULL, p);
1417 if (tmp)
1418 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001419 if (should_reada)
1420 reada_for_search(root, p,
1421 level, slot,
1422 key->objectid);
1423
Chris Mason594a24e2008-06-25 16:01:30 -04001424 tmp = read_tree_block(root, blocknr,
1425 blocksize, gen);
1426 if (tmp)
1427 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001428 goto again;
1429 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001430 if (tmp)
1431 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001432 if (should_reada)
1433 reada_for_search(root, p,
1434 level, slot,
1435 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001436 b = read_node_slot(root, b, slot);
1437 }
1438 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001439 if (!p->skip_locking)
1440 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001441 } else {
1442 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001443 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001444 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001445 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001446 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001447 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001448 if (sret) {
1449 ret = sret;
1450 goto done;
1451 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001452 }
Chris Mason925baed2008-06-25 16:01:30 -04001453 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001454 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001455 }
1456 }
Chris Mason65b51a02008-08-01 15:11:20 -04001457 ret = 1;
1458done:
1459 if (prealloc_block.objectid) {
1460 btrfs_free_reserved_extent(root,
1461 prealloc_block.objectid,
1462 prealloc_block.offset);
1463 }
1464
1465 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001466}
1467
Chris Mason74123bd2007-02-02 11:05:29 -05001468/*
1469 * adjust the pointers going up the tree, starting at level
1470 * making sure the right key of each node is points to 'key'.
1471 * This is used after shifting pointers to the left, so it stops
1472 * fixing up pointers when a given leaf/node is not in slot 0 of the
1473 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001474 *
1475 * If this fails to write a tree block, it returns -1, but continues
1476 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001477 */
Chris Mason5f39d392007-10-15 16:14:19 -04001478static int fixup_low_keys(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root, struct btrfs_path *path,
1480 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001481{
1482 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001483 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001484 struct extent_buffer *t;
1485
Chris Mason234b63a2007-03-13 10:46:10 -04001486 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001487 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001488 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001489 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001490 t = path->nodes[i];
1491 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001492 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001493 if (tslot != 0)
1494 break;
1495 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001496 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001497}
1498
Chris Mason74123bd2007-02-02 11:05:29 -05001499/*
1500 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001501 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001502 *
1503 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1504 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001505 */
Chris Mason98ed5172008-01-03 10:01:48 -05001506static int push_node_left(struct btrfs_trans_handle *trans,
1507 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001508 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001509{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001510 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001511 int src_nritems;
1512 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001513 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001514
Chris Mason5f39d392007-10-15 16:14:19 -04001515 src_nritems = btrfs_header_nritems(src);
1516 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001517 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001518 WARN_ON(btrfs_header_generation(src) != trans->transid);
1519 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001520
Chris Masonbce4eae2008-04-24 14:42:46 -04001521 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001522 return 1;
1523
Chris Masoneb60cea2007-02-02 09:18:22 -05001524 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001525 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001526 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001527
Chris Masonbce4eae2008-04-24 14:42:46 -04001528 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001529 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001530 if (push_items < src_nritems) {
1531 /* leave at least 8 pointers in the node if
1532 * we aren't going to empty it
1533 */
1534 if (src_nritems - push_items < 8) {
1535 if (push_items <= 8)
1536 return 1;
1537 push_items -= 8;
1538 }
1539 }
1540 } else
1541 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001542
Chris Mason5f39d392007-10-15 16:14:19 -04001543 copy_extent_buffer(dst, src,
1544 btrfs_node_key_ptr_offset(dst_nritems),
1545 btrfs_node_key_ptr_offset(0),
1546 push_items * sizeof(struct btrfs_key_ptr));
1547
Chris Masonbb803952007-03-01 12:04:21 -05001548 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001549 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1550 btrfs_node_key_ptr_offset(push_items),
1551 (src_nritems - push_items) *
1552 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001553 }
Chris Mason5f39d392007-10-15 16:14:19 -04001554 btrfs_set_header_nritems(src, src_nritems - push_items);
1555 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1556 btrfs_mark_buffer_dirty(src);
1557 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001558 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001559}
1560
Chris Mason97571fd2007-02-24 13:39:08 -05001561/*
Chris Mason79f95c82007-03-01 15:16:26 -05001562 * try to push data from one node into the next node right in the
1563 * tree.
1564 *
1565 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1566 * error, and > 0 if there was no room in the right hand block.
1567 *
1568 * this will only push up to 1/2 the contents of the left node over
1569 */
Chris Mason5f39d392007-10-15 16:14:19 -04001570static int balance_node_right(struct btrfs_trans_handle *trans,
1571 struct btrfs_root *root,
1572 struct extent_buffer *dst,
1573 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001574{
Chris Mason79f95c82007-03-01 15:16:26 -05001575 int push_items = 0;
1576 int max_push;
1577 int src_nritems;
1578 int dst_nritems;
1579 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001580
Chris Mason7bb86312007-12-11 09:25:06 -05001581 WARN_ON(btrfs_header_generation(src) != trans->transid);
1582 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1583
Chris Mason5f39d392007-10-15 16:14:19 -04001584 src_nritems = btrfs_header_nritems(src);
1585 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001586 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001587 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001588 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001589 }
1590
1591 if (src_nritems < 4) {
1592 return 1;
1593 }
Chris Mason79f95c82007-03-01 15:16:26 -05001594
1595 max_push = src_nritems / 2 + 1;
1596 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001597 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001598 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001599 }
Yan252c38f2007-08-29 09:11:44 -04001600
Chris Mason79f95c82007-03-01 15:16:26 -05001601 if (max_push < push_items)
1602 push_items = max_push;
1603
Chris Mason5f39d392007-10-15 16:14:19 -04001604 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1605 btrfs_node_key_ptr_offset(0),
1606 (dst_nritems) *
1607 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001608
Chris Mason5f39d392007-10-15 16:14:19 -04001609 copy_extent_buffer(dst, src,
1610 btrfs_node_key_ptr_offset(0),
1611 btrfs_node_key_ptr_offset(src_nritems - push_items),
1612 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001613
Chris Mason5f39d392007-10-15 16:14:19 -04001614 btrfs_set_header_nritems(src, src_nritems - push_items);
1615 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 btrfs_mark_buffer_dirty(src);
1618 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001619 return ret;
1620}
1621
1622/*
Chris Mason97571fd2007-02-24 13:39:08 -05001623 * helper function to insert a new root level in the tree.
1624 * A new node is allocated, and a single item is inserted to
1625 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001626 *
1627 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001628 */
Chris Mason98ed5172008-01-03 10:01:48 -05001629static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001630 struct btrfs_root *root,
1631 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001632{
Chris Mason7bb86312007-12-11 09:25:06 -05001633 u64 root_gen;
1634 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001635 struct extent_buffer *lower;
1636 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001637 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001639
1640 BUG_ON(path->nodes[level]);
1641 BUG_ON(path->nodes[level-1] != root->node);
1642
Chris Mason7bb86312007-12-11 09:25:06 -05001643 if (root->ref_cows)
1644 root_gen = trans->transid;
1645 else
1646 root_gen = 0;
1647
1648 lower = path->nodes[level-1];
1649 if (level == 1)
1650 btrfs_item_key(lower, &lower_key, 0);
1651 else
1652 btrfs_node_key(lower, &lower_key, 0);
1653
Chris Mason925baed2008-06-25 16:01:30 -04001654 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001655 root->root_key.objectid,
1656 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001657 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001658 if (IS_ERR(c))
1659 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001660
Chris Mason5f39d392007-10-15 16:14:19 -04001661 memset_extent_buffer(c, 0, 0, root->nodesize);
1662 btrfs_set_header_nritems(c, 1);
1663 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001664 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001665 btrfs_set_header_generation(c, trans->transid);
1666 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001667
Chris Mason5f39d392007-10-15 16:14:19 -04001668 write_extent_buffer(c, root->fs_info->fsid,
1669 (unsigned long)btrfs_header_fsid(c),
1670 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001671
1672 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1673 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1674 BTRFS_UUID_SIZE);
1675
Chris Mason5f39d392007-10-15 16:14:19 -04001676 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001677 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001678 lower_gen = btrfs_header_generation(lower);
1679 WARN_ON(lower_gen == 0);
1680
1681 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001682
1683 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001684
Chris Mason925baed2008-06-25 16:01:30 -04001685 spin_lock(&root->node_lock);
1686 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001687 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001688 spin_unlock(&root->node_lock);
1689
1690 /* the super has an extra ref to root->node */
1691 free_extent_buffer(old);
1692
Chris Mason0b86a832008-03-24 15:01:56 -04001693 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001694 extent_buffer_get(c);
1695 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001696 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001697 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001698
1699 if (root->ref_cows && lower_gen != trans->transid) {
1700 struct btrfs_path *back_path = btrfs_alloc_path();
1701 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001702 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001703 ret = btrfs_insert_extent_backref(trans,
1704 root->fs_info->extent_root,
1705 path, lower->start,
1706 root->root_key.objectid,
1707 trans->transid, 0, 0);
1708 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001709 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001710 btrfs_free_path(back_path);
1711 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001712 return 0;
1713}
1714
Chris Mason74123bd2007-02-02 11:05:29 -05001715/*
1716 * worker function to insert a single pointer in a node.
1717 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001718 *
Chris Mason74123bd2007-02-02 11:05:29 -05001719 * slot and level indicate where you want the key to go, and
1720 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001721 *
1722 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001723 */
Chris Masone089f052007-03-16 16:20:31 -04001724static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1725 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001726 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001727{
Chris Mason5f39d392007-10-15 16:14:19 -04001728 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001729 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001730
1731 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001732 lower = path->nodes[level];
1733 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001734 if (slot > nritems)
1735 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001736 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001737 BUG();
1738 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001739 memmove_extent_buffer(lower,
1740 btrfs_node_key_ptr_offset(slot + 1),
1741 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001742 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001743 }
Chris Mason5f39d392007-10-15 16:14:19 -04001744 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001745 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001746 WARN_ON(trans->transid == 0);
1747 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001748 btrfs_set_header_nritems(lower, nritems + 1);
1749 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001750 return 0;
1751}
1752
Chris Mason97571fd2007-02-24 13:39:08 -05001753/*
1754 * split the node at the specified level in path in two.
1755 * The path is corrected to point to the appropriate node after the split
1756 *
1757 * Before splitting this tries to make some room in the node by pushing
1758 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001759 *
1760 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001761 */
Chris Masone089f052007-03-16 16:20:31 -04001762static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1763 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001764{
Chris Mason7bb86312007-12-11 09:25:06 -05001765 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 struct extent_buffer *c;
1767 struct extent_buffer *split;
1768 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001770 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001771 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001772 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773
Chris Mason5f39d392007-10-15 16:14:19 -04001774 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001775 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001776 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001777 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001778 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001779 if (ret)
1780 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001781 } else {
1782 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001783 c = path->nodes[level];
1784 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001785 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001786 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001787 if (ret < 0)
1788 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001789 }
Chris Masone66f7092007-04-20 13:16:02 -04001790
Chris Mason5f39d392007-10-15 16:14:19 -04001791 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001792 if (root->ref_cows)
1793 root_gen = trans->transid;
1794 else
1795 root_gen = 0;
1796
1797 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001798 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001799 root->root_key.objectid,
1800 root_gen,
1801 btrfs_disk_key_objectid(&disk_key),
1802 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001803 if (IS_ERR(split))
1804 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001805
Chris Mason5f39d392007-10-15 16:14:19 -04001806 btrfs_set_header_flags(split, btrfs_header_flags(c));
1807 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001808 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001809 btrfs_set_header_generation(split, trans->transid);
1810 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001811 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001812 write_extent_buffer(split, root->fs_info->fsid,
1813 (unsigned long)btrfs_header_fsid(split),
1814 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001815 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1816 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1817 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001818
Chris Mason7518a232007-03-12 12:01:18 -04001819 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001820
1821 copy_extent_buffer(split, c,
1822 btrfs_node_key_ptr_offset(0),
1823 btrfs_node_key_ptr_offset(mid),
1824 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1825 btrfs_set_header_nritems(split, c_nritems - mid);
1826 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001827 ret = 0;
1828
Chris Mason5f39d392007-10-15 16:14:19 -04001829 btrfs_mark_buffer_dirty(c);
1830 btrfs_mark_buffer_dirty(split);
1831
1832 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001833 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001834 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001835 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001836 if (wret)
1837 ret = wret;
1838
Chris Mason5de08d72007-02-24 06:24:44 -05001839 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001840 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001841 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001842 free_extent_buffer(c);
1843 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001844 path->slots[level + 1] += 1;
1845 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001846 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001847 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001848 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001849 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850}
1851
Chris Mason74123bd2007-02-02 11:05:29 -05001852/*
1853 * how many bytes are required to store the items in a leaf. start
1854 * and nr indicate which items in the leaf to check. This totals up the
1855 * space used both by the item structs and the item data
1856 */
Chris Mason5f39d392007-10-15 16:14:19 -04001857static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001858{
1859 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001860 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001861 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862
1863 if (!nr)
1864 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001865 data_len = btrfs_item_end_nr(l, start);
1866 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001867 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001868 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001869 return data_len;
1870}
1871
Chris Mason74123bd2007-02-02 11:05:29 -05001872/*
Chris Masond4dbff92007-04-04 14:08:15 -04001873 * The space between the end of the leaf items and
1874 * the start of the leaf data. IOW, how much room
1875 * the leaf has left for both items and data
1876 */
Chris Mason5f39d392007-10-15 16:14:19 -04001877int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001878{
Chris Mason5f39d392007-10-15 16:14:19 -04001879 int nritems = btrfs_header_nritems(leaf);
1880 int ret;
1881 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1882 if (ret < 0) {
1883 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001884 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001885 leaf_space_used(leaf, 0, nritems), nritems);
1886 }
1887 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001888}
1889
1890/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001891 * push some data in the path leaf to the right, trying to free up at
1892 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001893 *
1894 * returns 1 if the push failed because the other node didn't have enough
1895 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001896 */
Chris Masone089f052007-03-16 16:20:31 -04001897static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001898 *root, struct btrfs_path *path, int data_size,
1899 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001900{
Chris Mason5f39d392007-10-15 16:14:19 -04001901 struct extent_buffer *left = path->nodes[0];
1902 struct extent_buffer *right;
1903 struct extent_buffer *upper;
1904 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001905 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001906 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001907 int free_space;
1908 int push_space = 0;
1909 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001910 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001911 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001912 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001913 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001914 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001915 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001916 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001917
1918 slot = path->slots[1];
1919 if (!path->nodes[1]) {
1920 return 1;
1921 }
1922 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001924 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001925
Chris Masona2135012008-06-25 16:01:30 -04001926 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1927
Chris Masonca7a79a2008-05-12 12:59:19 -04001928 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001929 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001930 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001931 if (free_space < data_size + sizeof(struct btrfs_item))
1932 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001933
Chris Mason5f39d392007-10-15 16:14:19 -04001934 /* cow and double check */
1935 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04001936 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001937 if (ret)
1938 goto out_unlock;
1939
Chris Mason5f39d392007-10-15 16:14:19 -04001940 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001941 if (free_space < data_size + sizeof(struct btrfs_item))
1942 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001943
1944 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001945 if (left_nritems == 0)
1946 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001947
Chris Mason34a38212007-11-07 13:31:03 -05001948 if (empty)
1949 nr = 0;
1950 else
1951 nr = 1;
1952
1953 i = left_nritems - 1;
1954 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001955 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001956
Chris Mason00ec4c52007-02-24 12:47:20 -05001957 if (path->slots[0] == i)
1958 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001959
1960 if (!left->map_token) {
1961 map_extent_buffer(left, (unsigned long)item,
1962 sizeof(struct btrfs_item),
1963 &left->map_token, &left->kaddr,
1964 &left->map_start, &left->map_len,
1965 KM_USER1);
1966 }
1967
1968 this_item_size = btrfs_item_size(left, item);
1969 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001970 break;
1971 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001972 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001973 if (i == 0)
1974 break;
1975 i--;
Chris Masondb945352007-10-15 16:15:53 -04001976 }
1977 if (left->map_token) {
1978 unmap_extent_buffer(left, left->map_token, KM_USER1);
1979 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001980 }
Chris Mason5f39d392007-10-15 16:14:19 -04001981
Chris Mason925baed2008-06-25 16:01:30 -04001982 if (push_items == 0)
1983 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001984
Chris Mason34a38212007-11-07 13:31:03 -05001985 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001986 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001987
Chris Mason00ec4c52007-02-24 12:47:20 -05001988 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001989 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001990
Chris Mason5f39d392007-10-15 16:14:19 -04001991 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001992 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001993
Chris Mason00ec4c52007-02-24 12:47:20 -05001994 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001995 data_end = leaf_data_end(root, right);
1996 memmove_extent_buffer(right,
1997 btrfs_leaf_data(right) + data_end - push_space,
1998 btrfs_leaf_data(right) + data_end,
1999 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2000
Chris Mason00ec4c52007-02-24 12:47:20 -05002001 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002002 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002003 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2004 btrfs_leaf_data(left) + leaf_data_end(root, left),
2005 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002006
2007 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2008 btrfs_item_nr_offset(0),
2009 right_nritems * sizeof(struct btrfs_item));
2010
Chris Mason00ec4c52007-02-24 12:47:20 -05002011 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002012 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2013 btrfs_item_nr_offset(left_nritems - push_items),
2014 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002015
2016 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002017 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002018 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002019 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002020 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002021 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002022 if (!right->map_token) {
2023 map_extent_buffer(right, (unsigned long)item,
2024 sizeof(struct btrfs_item),
2025 &right->map_token, &right->kaddr,
2026 &right->map_start, &right->map_len,
2027 KM_USER1);
2028 }
2029 push_space -= btrfs_item_size(right, item);
2030 btrfs_set_item_offset(right, item, push_space);
2031 }
2032
2033 if (right->map_token) {
2034 unmap_extent_buffer(right, right->map_token, KM_USER1);
2035 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002036 }
Chris Mason7518a232007-03-12 12:01:18 -04002037 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002039
Chris Mason34a38212007-11-07 13:31:03 -05002040 if (left_nritems)
2041 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002043
Chris Mason5f39d392007-10-15 16:14:19 -04002044 btrfs_item_key(right, &disk_key, 0);
2045 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002046 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002047
Chris Mason00ec4c52007-02-24 12:47:20 -05002048 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002049 if (path->slots[0] >= left_nritems) {
2050 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002051 if (btrfs_header_nritems(path->nodes[0]) == 0)
2052 clean_tree_block(trans, root, path->nodes[0]);
2053 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002054 free_extent_buffer(path->nodes[0]);
2055 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002056 path->slots[1] += 1;
2057 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002058 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002059 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002060 }
2061 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002062
2063out_unlock:
2064 btrfs_tree_unlock(right);
2065 free_extent_buffer(right);
2066 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002067}
Chris Mason925baed2008-06-25 16:01:30 -04002068
Chris Mason00ec4c52007-02-24 12:47:20 -05002069/*
Chris Mason74123bd2007-02-02 11:05:29 -05002070 * push some data in the path leaf to the left, trying to free up at
2071 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2072 */
Chris Masone089f052007-03-16 16:20:31 -04002073static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002074 *root, struct btrfs_path *path, int data_size,
2075 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002076{
Chris Mason5f39d392007-10-15 16:14:19 -04002077 struct btrfs_disk_key disk_key;
2078 struct extent_buffer *right = path->nodes[0];
2079 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002080 int slot;
2081 int i;
2082 int free_space;
2083 int push_space = 0;
2084 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002085 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002086 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002087 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002088 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002089 int ret = 0;
2090 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002091 u32 this_item_size;
2092 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002093
2094 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002095 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002096 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002097 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002098 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002099
Chris Mason3685f792007-10-19 09:23:27 -04002100 right_nritems = btrfs_header_nritems(right);
2101 if (right_nritems == 0) {
2102 return 1;
2103 }
2104
Chris Masona2135012008-06-25 16:01:30 -04002105 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2106
Chris Masonca7a79a2008-05-12 12:59:19 -04002107 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002108 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002109 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002110 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002111 ret = 1;
2112 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002113 }
Chris Mason02217ed2007-03-02 16:08:05 -05002114
2115 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002116 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002117 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002118 if (ret) {
2119 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002120 ret = 1;
2121 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002122 }
Chris Mason3685f792007-10-19 09:23:27 -04002123
Chris Mason123abc82007-03-14 14:14:43 -04002124 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002125 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002126 ret = 1;
2127 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002128 }
2129
Chris Mason34a38212007-11-07 13:31:03 -05002130 if (empty)
2131 nr = right_nritems;
2132 else
2133 nr = right_nritems - 1;
2134
2135 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002136 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002137 if (!right->map_token) {
2138 map_extent_buffer(right, (unsigned long)item,
2139 sizeof(struct btrfs_item),
2140 &right->map_token, &right->kaddr,
2141 &right->map_start, &right->map_len,
2142 KM_USER1);
2143 }
2144
Chris Masonbe0e5c02007-01-26 15:51:26 -05002145 if (path->slots[0] == i)
2146 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002147
2148 this_item_size = btrfs_item_size(right, item);
2149 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150 break;
Chris Masondb945352007-10-15 16:15:53 -04002151
Chris Masonbe0e5c02007-01-26 15:51:26 -05002152 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002153 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002154 }
Chris Masondb945352007-10-15 16:15:53 -04002155
2156 if (right->map_token) {
2157 unmap_extent_buffer(right, right->map_token, KM_USER1);
2158 right->map_token = NULL;
2159 }
2160
Chris Masonbe0e5c02007-01-26 15:51:26 -05002161 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002162 ret = 1;
2163 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002164 }
Chris Mason34a38212007-11-07 13:31:03 -05002165 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002166 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002167
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002169 copy_extent_buffer(left, right,
2170 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2171 btrfs_item_nr_offset(0),
2172 push_items * sizeof(struct btrfs_item));
2173
Chris Mason123abc82007-03-14 14:14:43 -04002174 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002175 btrfs_item_offset_nr(right, push_items -1);
2176
2177 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002178 leaf_data_end(root, left) - push_space,
2179 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002181 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002183 BUG_ON(old_left_nritems < 0);
2184
Chris Masondb945352007-10-15 16:15:53 -04002185 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002186 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002187 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002188
Chris Mason5f39d392007-10-15 16:14:19 -04002189 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002190 if (!left->map_token) {
2191 map_extent_buffer(left, (unsigned long)item,
2192 sizeof(struct btrfs_item),
2193 &left->map_token, &left->kaddr,
2194 &left->map_start, &left->map_len,
2195 KM_USER1);
2196 }
2197
Chris Mason5f39d392007-10-15 16:14:19 -04002198 ioff = btrfs_item_offset(left, item);
2199 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002200 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002201 }
Chris Mason5f39d392007-10-15 16:14:19 -04002202 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002203 if (left->map_token) {
2204 unmap_extent_buffer(left, left->map_token, KM_USER1);
2205 left->map_token = NULL;
2206 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002207
2208 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002209 if (push_items > right_nritems) {
2210 printk("push items %d nr %u\n", push_items, right_nritems);
2211 WARN_ON(1);
2212 }
Chris Mason5f39d392007-10-15 16:14:19 -04002213
Chris Mason34a38212007-11-07 13:31:03 -05002214 if (push_items < right_nritems) {
2215 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2216 leaf_data_end(root, right);
2217 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2218 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2219 btrfs_leaf_data(right) +
2220 leaf_data_end(root, right), push_space);
2221
2222 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002223 btrfs_item_nr_offset(push_items),
2224 (btrfs_header_nritems(right) - push_items) *
2225 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002226 }
Yaneef1c492007-11-26 10:58:13 -05002227 right_nritems -= push_items;
2228 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002229 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002230 for (i = 0; i < right_nritems; i++) {
2231 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002232
2233 if (!right->map_token) {
2234 map_extent_buffer(right, (unsigned long)item,
2235 sizeof(struct btrfs_item),
2236 &right->map_token, &right->kaddr,
2237 &right->map_start, &right->map_len,
2238 KM_USER1);
2239 }
2240
2241 push_space = push_space - btrfs_item_size(right, item);
2242 btrfs_set_item_offset(right, item, push_space);
2243 }
2244 if (right->map_token) {
2245 unmap_extent_buffer(right, right->map_token, KM_USER1);
2246 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002247 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002248
Chris Mason5f39d392007-10-15 16:14:19 -04002249 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002250 if (right_nritems)
2251 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002252
Chris Mason5f39d392007-10-15 16:14:19 -04002253 btrfs_item_key(right, &disk_key, 0);
2254 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002255 if (wret)
2256 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002257
2258 /* then fixup the leaf pointer in the path */
2259 if (path->slots[0] < push_items) {
2260 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002261 if (btrfs_header_nritems(path->nodes[0]) == 0)
2262 clean_tree_block(trans, root, path->nodes[0]);
2263 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 free_extent_buffer(path->nodes[0]);
2265 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002266 path->slots[1] -= 1;
2267 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002268 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002269 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002270 path->slots[0] -= push_items;
2271 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002272 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002273 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002274out:
2275 btrfs_tree_unlock(left);
2276 free_extent_buffer(left);
2277 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002278}
2279
Chris Mason74123bd2007-02-02 11:05:29 -05002280/*
2281 * split the path's leaf in two, making sure there is at least data_size
2282 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002283 *
2284 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002285 */
Chris Masone089f052007-03-16 16:20:31 -04002286static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002287 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002288 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002289{
Chris Mason7bb86312007-12-11 09:25:06 -05002290 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002291 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002292 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002293 int mid;
2294 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002295 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002296 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002297 int data_copy_size;
2298 int rt_data_off;
2299 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002300 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002301 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002302 int double_split;
2303 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002304 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002305
Chris Masoncc0c5532007-10-25 15:42:57 -04002306 if (extend)
2307 space_needed = data_size;
2308
Chris Mason7bb86312007-12-11 09:25:06 -05002309 if (root->ref_cows)
2310 root_gen = trans->transid;
2311 else
2312 root_gen = 0;
2313
Chris Mason40689472007-03-17 14:29:23 -04002314 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002315 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002316 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002317 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002318 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002319 }
2320 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002321 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002322 if (wret < 0)
2323 return wret;
2324 }
2325 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002326
Chris Mason3685f792007-10-19 09:23:27 -04002327 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002328 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002329 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002330 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002331
Chris Mason5c680ed2007-02-22 11:39:13 -05002332 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002333 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002334 if (ret)
2335 return ret;
2336 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002337again:
2338 double_split = 0;
2339 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002340 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002341 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002342 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002343
Chris Mason7bb86312007-12-11 09:25:06 -05002344 btrfs_item_key(l, &disk_key, 0);
2345
Chris Mason925baed2008-06-25 16:01:30 -04002346 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002347 root->root_key.objectid,
2348 root_gen, disk_key.objectid, 0,
2349 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002350 if (IS_ERR(right)) {
2351 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002352 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002353 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002354
Chris Mason5f39d392007-10-15 16:14:19 -04002355 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002356 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002357 btrfs_set_header_generation(right, trans->transid);
2358 btrfs_set_header_owner(right, root->root_key.objectid);
2359 btrfs_set_header_level(right, 0);
2360 write_extent_buffer(right, root->fs_info->fsid,
2361 (unsigned long)btrfs_header_fsid(right),
2362 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002363
2364 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2365 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2366 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002367 if (mid <= slot) {
2368 if (nritems == 1 ||
2369 leaf_space_used(l, mid, nritems - mid) + space_needed >
2370 BTRFS_LEAF_DATA_SIZE(root)) {
2371 if (slot >= nritems) {
2372 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002373 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002374 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002375 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002376 path->slots[1] + 1, 1);
2377 if (wret)
2378 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002379
2380 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002381 free_extent_buffer(path->nodes[0]);
2382 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002383 path->slots[0] = 0;
2384 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002385 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002386 return ret;
2387 }
2388 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002389 if (mid != nritems &&
2390 leaf_space_used(l, mid, nritems - mid) +
2391 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2392 double_split = 1;
2393 }
Chris Masond4dbff92007-04-04 14:08:15 -04002394 }
2395 } else {
2396 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2397 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002398 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002399 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002400 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002401 wret = insert_ptr(trans, root, path,
2402 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002403 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002404 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002405 if (wret)
2406 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002407 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002408 free_extent_buffer(path->nodes[0]);
2409 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002410 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002411 if (path->slots[1] == 0) {
2412 wret = fixup_low_keys(trans, root,
2413 path, &disk_key, 1);
2414 if (wret)
2415 ret = wret;
2416 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002417 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002418 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002419 } else if (extend && slot == 0) {
2420 mid = 1;
2421 } else {
2422 mid = slot;
2423 if (mid != nritems &&
2424 leaf_space_used(l, mid, nritems - mid) +
2425 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2426 double_split = 1;
2427 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002428 }
Chris Masond4dbff92007-04-04 14:08:15 -04002429 }
2430 }
Chris Mason5f39d392007-10-15 16:14:19 -04002431 nritems = nritems - mid;
2432 btrfs_set_header_nritems(right, nritems);
2433 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2434
2435 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2436 btrfs_item_nr_offset(mid),
2437 nritems * sizeof(struct btrfs_item));
2438
2439 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002440 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2441 data_copy_size, btrfs_leaf_data(l) +
2442 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002443
Chris Mason5f39d392007-10-15 16:14:19 -04002444 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2445 btrfs_item_end_nr(l, mid);
2446
2447 for (i = 0; i < nritems; i++) {
2448 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002449 u32 ioff;
2450
2451 if (!right->map_token) {
2452 map_extent_buffer(right, (unsigned long)item,
2453 sizeof(struct btrfs_item),
2454 &right->map_token, &right->kaddr,
2455 &right->map_start, &right->map_len,
2456 KM_USER1);
2457 }
2458
2459 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002460 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002461 }
Chris Mason74123bd2007-02-02 11:05:29 -05002462
Chris Masondb945352007-10-15 16:15:53 -04002463 if (right->map_token) {
2464 unmap_extent_buffer(right, right->map_token, KM_USER1);
2465 right->map_token = NULL;
2466 }
2467
Chris Mason5f39d392007-10-15 16:14:19 -04002468 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002469 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002470 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002471 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2472 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002473 if (wret)
2474 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002475
2476 btrfs_mark_buffer_dirty(right);
2477 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002478 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002479
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002481 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002482 free_extent_buffer(path->nodes[0]);
2483 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484 path->slots[0] -= mid;
2485 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002486 } else {
2487 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002488 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002489 }
Chris Mason5f39d392007-10-15 16:14:19 -04002490
Chris Masoneb60cea2007-02-02 09:18:22 -05002491 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002492
Chris Masoncc0c5532007-10-25 15:42:57 -04002493 if (double_split) {
2494 BUG_ON(num_doubles != 0);
2495 num_doubles++;
2496 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002497 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002498 return ret;
2499}
2500
Chris Masonb18c6682007-04-17 13:26:50 -04002501int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2502 struct btrfs_root *root,
2503 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002504 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002505{
2506 int ret = 0;
2507 int slot;
2508 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002509 struct extent_buffer *leaf;
2510 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002511 u32 nritems;
2512 unsigned int data_end;
2513 unsigned int old_data_start;
2514 unsigned int old_size;
2515 unsigned int size_diff;
2516 int i;
2517
2518 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002519 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002520 slot = path->slots[0];
2521
2522 old_size = btrfs_item_size_nr(leaf, slot);
2523 if (old_size == new_size)
2524 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002527 data_end = leaf_data_end(root, leaf);
2528
Chris Mason5f39d392007-10-15 16:14:19 -04002529 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002530
Chris Masonb18c6682007-04-17 13:26:50 -04002531 size_diff = old_size - new_size;
2532
2533 BUG_ON(slot < 0);
2534 BUG_ON(slot >= nritems);
2535
2536 /*
2537 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2538 */
2539 /* first correct the data pointers */
2540 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002541 u32 ioff;
2542 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002543
2544 if (!leaf->map_token) {
2545 map_extent_buffer(leaf, (unsigned long)item,
2546 sizeof(struct btrfs_item),
2547 &leaf->map_token, &leaf->kaddr,
2548 &leaf->map_start, &leaf->map_len,
2549 KM_USER1);
2550 }
2551
Chris Mason5f39d392007-10-15 16:14:19 -04002552 ioff = btrfs_item_offset(leaf, item);
2553 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002554 }
Chris Masondb945352007-10-15 16:15:53 -04002555
2556 if (leaf->map_token) {
2557 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2558 leaf->map_token = NULL;
2559 }
2560
Chris Masonb18c6682007-04-17 13:26:50 -04002561 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002562 if (from_end) {
2563 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2564 data_end + size_diff, btrfs_leaf_data(leaf) +
2565 data_end, old_data_start + new_size - data_end);
2566 } else {
2567 struct btrfs_disk_key disk_key;
2568 u64 offset;
2569
2570 btrfs_item_key(leaf, &disk_key, slot);
2571
2572 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2573 unsigned long ptr;
2574 struct btrfs_file_extent_item *fi;
2575
2576 fi = btrfs_item_ptr(leaf, slot,
2577 struct btrfs_file_extent_item);
2578 fi = (struct btrfs_file_extent_item *)(
2579 (unsigned long)fi - size_diff);
2580
2581 if (btrfs_file_extent_type(leaf, fi) ==
2582 BTRFS_FILE_EXTENT_INLINE) {
2583 ptr = btrfs_item_ptr_offset(leaf, slot);
2584 memmove_extent_buffer(leaf, ptr,
2585 (unsigned long)fi,
2586 offsetof(struct btrfs_file_extent_item,
2587 disk_bytenr));
2588 }
2589 }
2590
2591 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2592 data_end + size_diff, btrfs_leaf_data(leaf) +
2593 data_end, old_data_start - data_end);
2594
2595 offset = btrfs_disk_key_offset(&disk_key);
2596 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2597 btrfs_set_item_key(leaf, &disk_key, slot);
2598 if (slot == 0)
2599 fixup_low_keys(trans, root, path, &disk_key, 1);
2600 }
Chris Mason5f39d392007-10-15 16:14:19 -04002601
2602 item = btrfs_item_nr(leaf, slot);
2603 btrfs_set_item_size(leaf, item, new_size);
2604 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002605
2606 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002607 if (btrfs_leaf_free_space(root, leaf) < 0) {
2608 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002609 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002610 }
Chris Masonb18c6682007-04-17 13:26:50 -04002611 return ret;
2612}
2613
Chris Mason5f39d392007-10-15 16:14:19 -04002614int btrfs_extend_item(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root, struct btrfs_path *path,
2616 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002617{
2618 int ret = 0;
2619 int slot;
2620 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002621 struct extent_buffer *leaf;
2622 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002623 u32 nritems;
2624 unsigned int data_end;
2625 unsigned int old_data;
2626 unsigned int old_size;
2627 int i;
2628
2629 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002630 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002631
Chris Mason5f39d392007-10-15 16:14:19 -04002632 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002633 data_end = leaf_data_end(root, leaf);
2634
Chris Mason5f39d392007-10-15 16:14:19 -04002635 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2636 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002637 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002638 }
Chris Mason6567e832007-04-16 09:22:45 -04002639 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002640 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002641
2642 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002643 if (slot >= nritems) {
2644 btrfs_print_leaf(root, leaf);
2645 printk("slot %d too large, nritems %d\n", slot, nritems);
2646 BUG_ON(1);
2647 }
Chris Mason6567e832007-04-16 09:22:45 -04002648
2649 /*
2650 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2651 */
2652 /* first correct the data pointers */
2653 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002654 u32 ioff;
2655 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002656
2657 if (!leaf->map_token) {
2658 map_extent_buffer(leaf, (unsigned long)item,
2659 sizeof(struct btrfs_item),
2660 &leaf->map_token, &leaf->kaddr,
2661 &leaf->map_start, &leaf->map_len,
2662 KM_USER1);
2663 }
Chris Mason5f39d392007-10-15 16:14:19 -04002664 ioff = btrfs_item_offset(leaf, item);
2665 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002666 }
Chris Mason5f39d392007-10-15 16:14:19 -04002667
Chris Masondb945352007-10-15 16:15:53 -04002668 if (leaf->map_token) {
2669 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2670 leaf->map_token = NULL;
2671 }
2672
Chris Mason6567e832007-04-16 09:22:45 -04002673 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002674 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002675 data_end - data_size, btrfs_leaf_data(leaf) +
2676 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002677
Chris Mason6567e832007-04-16 09:22:45 -04002678 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002679 old_size = btrfs_item_size_nr(leaf, slot);
2680 item = btrfs_item_nr(leaf, slot);
2681 btrfs_set_item_size(leaf, item, old_size + data_size);
2682 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002683
2684 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002685 if (btrfs_leaf_free_space(root, leaf) < 0) {
2686 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002687 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002688 }
Chris Mason6567e832007-04-16 09:22:45 -04002689 return ret;
2690}
2691
Chris Mason74123bd2007-02-02 11:05:29 -05002692/*
2693 * Given a key and some data, insert an item into the tree.
2694 * This does all the path init required, making room in the tree if needed.
2695 */
Chris Mason9c583092008-01-29 15:15:18 -05002696int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002697 struct btrfs_root *root,
2698 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002699 struct btrfs_key *cpu_key, u32 *data_size,
2700 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002701{
Chris Mason5f39d392007-10-15 16:14:19 -04002702 struct extent_buffer *leaf;
2703 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002704 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002705 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002706 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002707 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002708 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002709 u32 total_size = 0;
2710 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002712 struct btrfs_disk_key disk_key;
2713
Chris Mason9c583092008-01-29 15:15:18 -05002714 for (i = 0; i < nr; i++) {
2715 total_data += data_size[i];
2716 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002717
Josef Bacik7b128762008-07-24 12:17:14 -04002718 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05002719 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002720 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002721 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002722 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002723 if (ret < 0)
2724 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725
Chris Mason62e27492007-03-15 12:56:47 -04002726 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002727 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002728
Chris Mason5f39d392007-10-15 16:14:19 -04002729 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002730 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002731
Chris Mason123abc82007-03-14 14:14:43 -04002732 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002733 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002734 btrfs_print_leaf(root, leaf);
2735 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002736 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002737 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002738 }
Chris Mason5f39d392007-10-15 16:14:19 -04002739
Chris Mason62e27492007-03-15 12:56:47 -04002740 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002741 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002742
Chris Masonbe0e5c02007-01-26 15:51:26 -05002743 if (slot != nritems) {
2744 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002745 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002746
Chris Mason5f39d392007-10-15 16:14:19 -04002747 if (old_data < data_end) {
2748 btrfs_print_leaf(root, leaf);
2749 printk("slot %d old_data %d data_end %d\n",
2750 slot, old_data, data_end);
2751 BUG_ON(1);
2752 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002753 /*
2754 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2755 */
2756 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002757 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002758 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002759 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002760
Chris Mason5f39d392007-10-15 16:14:19 -04002761 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002762 if (!leaf->map_token) {
2763 map_extent_buffer(leaf, (unsigned long)item,
2764 sizeof(struct btrfs_item),
2765 &leaf->map_token, &leaf->kaddr,
2766 &leaf->map_start, &leaf->map_len,
2767 KM_USER1);
2768 }
2769
Chris Mason5f39d392007-10-15 16:14:19 -04002770 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002771 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002772 }
Chris Masondb945352007-10-15 16:15:53 -04002773 if (leaf->map_token) {
2774 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2775 leaf->map_token = NULL;
2776 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002777
2778 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002779 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002780 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002781 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782
2783 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002784 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002785 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002786 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002787 data_end = old_data;
2788 }
Chris Mason5f39d392007-10-15 16:14:19 -04002789
Chris Mason62e27492007-03-15 12:56:47 -04002790 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002791 for (i = 0; i < nr; i++) {
2792 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2793 btrfs_set_item_key(leaf, &disk_key, slot + i);
2794 item = btrfs_item_nr(leaf, slot + i);
2795 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2796 data_end -= data_size[i];
2797 btrfs_set_item_size(leaf, item, data_size[i]);
2798 }
2799 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002800 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002801
2802 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002803 if (slot == 0) {
2804 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002805 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002806 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002807
Chris Mason5f39d392007-10-15 16:14:19 -04002808 if (btrfs_leaf_free_space(root, leaf) < 0) {
2809 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002810 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002811 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002812out:
Chris Mason62e27492007-03-15 12:56:47 -04002813 return ret;
2814}
2815
2816/*
2817 * Given a key and some data, insert an item into the tree.
2818 * This does all the path init required, making room in the tree if needed.
2819 */
Chris Masone089f052007-03-16 16:20:31 -04002820int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2821 *root, struct btrfs_key *cpu_key, void *data, u32
2822 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002823{
2824 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002825 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002826 struct extent_buffer *leaf;
2827 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002828
Chris Mason2c90e5d2007-04-02 10:50:19 -04002829 path = btrfs_alloc_path();
2830 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002831 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002832 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002833 leaf = path->nodes[0];
2834 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2835 write_extent_buffer(leaf, data, ptr, data_size);
2836 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002837 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002838 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002839 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002840}
2841
Chris Mason74123bd2007-02-02 11:05:29 -05002842/*
Chris Mason5de08d72007-02-24 06:24:44 -05002843 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002844 *
2845 * If the delete empties a node, the node is removed from the tree,
2846 * continuing all the way the root if required. The root is converted into
2847 * a leaf if all the nodes are emptied.
2848 */
Chris Masone089f052007-03-16 16:20:31 -04002849static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2850 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002851{
Chris Mason5f39d392007-10-15 16:14:19 -04002852 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002853 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002854 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002855 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002856
Chris Mason5f39d392007-10-15 16:14:19 -04002857 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002858 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002859 memmove_extent_buffer(parent,
2860 btrfs_node_key_ptr_offset(slot),
2861 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002862 sizeof(struct btrfs_key_ptr) *
2863 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002864 }
Chris Mason7518a232007-03-12 12:01:18 -04002865 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002866 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002867 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002868 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002869 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002870 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002871 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002872 struct btrfs_disk_key disk_key;
2873
2874 btrfs_node_key(parent, &disk_key, 0);
2875 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002876 if (wret)
2877 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002878 }
Chris Masond6025572007-03-30 14:27:56 -04002879 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002880 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002881}
2882
Chris Mason74123bd2007-02-02 11:05:29 -05002883/*
2884 * delete the item at the leaf level in path. If that empties
2885 * the leaf, remove it from the tree
2886 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002887int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2888 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002889{
Chris Mason5f39d392007-10-15 16:14:19 -04002890 struct extent_buffer *leaf;
2891 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002892 int last_off;
2893 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002894 int ret = 0;
2895 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002896 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002897 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002898
Chris Mason5f39d392007-10-15 16:14:19 -04002899 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002900 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2901
2902 for (i = 0; i < nr; i++)
2903 dsize += btrfs_item_size_nr(leaf, slot + i);
2904
Chris Mason5f39d392007-10-15 16:14:19 -04002905 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002906
Chris Mason85e21ba2008-01-29 15:11:36 -05002907 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002908 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002909 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002910
2911 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002912 data_end + dsize,
2913 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002914 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002915
Chris Mason85e21ba2008-01-29 15:11:36 -05002916 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002917 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002918
Chris Mason5f39d392007-10-15 16:14:19 -04002919 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002920 if (!leaf->map_token) {
2921 map_extent_buffer(leaf, (unsigned long)item,
2922 sizeof(struct btrfs_item),
2923 &leaf->map_token, &leaf->kaddr,
2924 &leaf->map_start, &leaf->map_len,
2925 KM_USER1);
2926 }
Chris Mason5f39d392007-10-15 16:14:19 -04002927 ioff = btrfs_item_offset(leaf, item);
2928 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002929 }
Chris Masondb945352007-10-15 16:15:53 -04002930
2931 if (leaf->map_token) {
2932 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2933 leaf->map_token = NULL;
2934 }
2935
Chris Mason5f39d392007-10-15 16:14:19 -04002936 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002937 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002938 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002939 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002940 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002941 btrfs_set_header_nritems(leaf, nritems - nr);
2942 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002943
Chris Mason74123bd2007-02-02 11:05:29 -05002944 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002945 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002946 if (leaf == root->node) {
2947 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002948 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002949 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002950 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002951 if (wret)
2952 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002953 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002954 leaf->start, leaf->len,
2955 btrfs_header_owner(path->nodes[1]),
2956 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002957 if (wret)
2958 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002959 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002960 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002961 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002962 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002963 struct btrfs_disk_key disk_key;
2964
2965 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002966 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002967 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002968 if (wret)
2969 ret = wret;
2970 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002971
Chris Mason74123bd2007-02-02 11:05:29 -05002972 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002973 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002974 /* push_leaf_left fixes the path.
2975 * make sure the path still points to our leaf
2976 * for possible call to del_ptr below
2977 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002978 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002979 extent_buffer_get(leaf);
2980
Chris Mason85e21ba2008-01-29 15:11:36 -05002981 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002982 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002983 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002984
2985 if (path->nodes[0] == leaf &&
2986 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002987 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002988 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002989 ret = wret;
2990 }
Chris Mason5f39d392007-10-15 16:14:19 -04002991
2992 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002993 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002994 u64 bytenr = leaf->start;
2995 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002996
Chris Mason7bb86312007-12-11 09:25:06 -05002997 root_gen = btrfs_header_generation(
2998 path->nodes[1]);
2999
Chris Masone089f052007-03-16 16:20:31 -04003000 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003001 if (wret)
3002 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003003
3004 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04003005 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003006 blocksize,
3007 btrfs_header_owner(path->nodes[1]),
3008 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003009 if (wret)
3010 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05003011 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003012 /* if we're still in the path, make sure
3013 * we're dirty. Otherwise, one of the
3014 * push_leaf functions must have already
3015 * dirtied this buffer
3016 */
3017 if (path->nodes[0] == leaf)
3018 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003019 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020 }
Chris Masond5719762007-03-23 10:01:08 -04003021 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003022 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003023 }
3024 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003025 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003026}
3027
Chris Mason97571fd2007-02-24 13:39:08 -05003028/*
Chris Mason925baed2008-06-25 16:01:30 -04003029 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003030 * returns 0 if it found something or 1 if there are no lesser leaves.
3031 * returns < 0 on io errors.
3032 */
3033int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3034{
Chris Mason925baed2008-06-25 16:01:30 -04003035 struct btrfs_key key;
3036 struct btrfs_disk_key found_key;
3037 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003038
Chris Mason925baed2008-06-25 16:01:30 -04003039 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003040
Chris Mason925baed2008-06-25 16:01:30 -04003041 if (key.offset > 0)
3042 key.offset--;
3043 else if (key.type > 0)
3044 key.type--;
3045 else if (key.objectid > 0)
3046 key.objectid--;
3047 else
3048 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003049
Chris Mason925baed2008-06-25 16:01:30 -04003050 btrfs_release_path(root, path);
3051 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3052 if (ret < 0)
3053 return ret;
3054 btrfs_item_key(path->nodes[0], &found_key, 0);
3055 ret = comp_keys(&found_key, &key);
3056 if (ret < 0)
3057 return 0;
3058 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003059}
3060
Chris Mason3f157a22008-06-25 16:01:31 -04003061/*
3062 * A helper function to walk down the tree starting at min_key, and looking
3063 * for nodes or leaves that are either in cache or have a minimum
3064 * transaction id. This is used by the btree defrag code, but could
3065 * also be used to search for blocks that have changed since a given
3066 * transaction id.
3067 *
3068 * This does not cow, but it does stuff the starting key it finds back
3069 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3070 * key and get a writable path.
3071 *
3072 * This does lock as it descends, and path->keep_locks should be set
3073 * to 1 by the caller.
3074 *
3075 * This honors path->lowest_level to prevent descent past a given level
3076 * of the tree.
3077 *
3078 * returns zero if something useful was found, < 0 on error and 1 if there
3079 * was nothing in the tree that matched the search criteria.
3080 */
3081int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3082 struct btrfs_path *path, int cache_only,
3083 u64 min_trans)
3084{
3085 struct extent_buffer *cur;
3086 struct btrfs_key found_key;
3087 int slot;
Yan96524802008-07-24 12:19:49 -04003088 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003089 u32 nritems;
3090 int level;
3091 int ret = 1;
3092
3093again:
3094 cur = btrfs_lock_root_node(root);
3095 level = btrfs_header_level(cur);
3096 path->nodes[level] = cur;
3097 path->locks[level] = 1;
3098
3099 if (btrfs_header_generation(cur) < min_trans) {
3100 ret = 1;
3101 goto out;
3102 }
3103 while(1) {
3104 nritems = btrfs_header_nritems(cur);
3105 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003106 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003107
3108 /* at level = 0, we're done, setup the path and exit */
3109 if (level == 0) {
3110 ret = 0;
3111 path->slots[level] = slot;
3112 btrfs_item_key_to_cpu(cur, &found_key, slot);
3113 goto out;
3114 }
Yan96524802008-07-24 12:19:49 -04003115 if (sret && slot > 0)
3116 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003117 /*
3118 * check this node pointer against the cache_only and
3119 * min_trans parameters. If it isn't in cache or is too
3120 * old, skip to the next one.
3121 */
3122 while(slot < nritems) {
3123 u64 blockptr;
3124 u64 gen;
3125 struct extent_buffer *tmp;
3126 blockptr = btrfs_node_blockptr(cur, slot);
3127 gen = btrfs_node_ptr_generation(cur, slot);
3128 if (gen < min_trans) {
3129 slot++;
3130 continue;
3131 }
3132 if (!cache_only)
3133 break;
3134
3135 tmp = btrfs_find_tree_block(root, blockptr,
3136 btrfs_level_size(root, level - 1));
3137
3138 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3139 free_extent_buffer(tmp);
3140 break;
3141 }
3142 if (tmp)
3143 free_extent_buffer(tmp);
3144 slot++;
3145 }
3146 /*
3147 * we didn't find a candidate key in this node, walk forward
3148 * and find another one
3149 */
3150 if (slot >= nritems) {
3151 ret = btrfs_find_next_key(root, path, min_key, level,
3152 cache_only, min_trans);
3153 if (ret == 0) {
3154 btrfs_release_path(root, path);
3155 goto again;
3156 } else {
3157 goto out;
3158 }
3159 }
3160 /* save our key for returning back */
3161 btrfs_node_key_to_cpu(cur, &found_key, slot);
3162 path->slots[level] = slot;
3163 if (level == path->lowest_level) {
3164 ret = 0;
3165 unlock_up(path, level, 1);
3166 goto out;
3167 }
3168 cur = read_node_slot(root, cur, slot);
3169
3170 btrfs_tree_lock(cur);
3171 path->locks[level - 1] = 1;
3172 path->nodes[level - 1] = cur;
3173 unlock_up(path, level, 1);
3174 }
3175out:
3176 if (ret == 0)
3177 memcpy(min_key, &found_key, sizeof(found_key));
3178 return ret;
3179}
3180
3181/*
3182 * this is similar to btrfs_next_leaf, but does not try to preserve
3183 * and fixup the path. It looks for and returns the next key in the
3184 * tree based on the current path and the cache_only and min_trans
3185 * parameters.
3186 *
3187 * 0 is returned if another key is found, < 0 if there are any errors
3188 * and 1 is returned if there are no higher keys in the tree
3189 *
3190 * path->keep_locks should be set to 1 on the search made before
3191 * calling this function.
3192 */
Chris Masone7a84562008-06-25 16:01:31 -04003193int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003194 struct btrfs_key *key, int lowest_level,
3195 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003196{
3197 int level = lowest_level;
3198 int slot;
3199 struct extent_buffer *c;
3200
3201 while(level < BTRFS_MAX_LEVEL) {
3202 if (!path->nodes[level])
3203 return 1;
3204
3205 slot = path->slots[level] + 1;
3206 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003207next:
Chris Masone7a84562008-06-25 16:01:31 -04003208 if (slot >= btrfs_header_nritems(c)) {
3209 level++;
3210 if (level == BTRFS_MAX_LEVEL) {
3211 return 1;
3212 }
3213 continue;
3214 }
3215 if (level == 0)
3216 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003217 else {
3218 u64 blockptr = btrfs_node_blockptr(c, slot);
3219 u64 gen = btrfs_node_ptr_generation(c, slot);
3220
3221 if (cache_only) {
3222 struct extent_buffer *cur;
3223 cur = btrfs_find_tree_block(root, blockptr,
3224 btrfs_level_size(root, level - 1));
3225 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3226 slot++;
3227 if (cur)
3228 free_extent_buffer(cur);
3229 goto next;
3230 }
3231 free_extent_buffer(cur);
3232 }
3233 if (gen < min_trans) {
3234 slot++;
3235 goto next;
3236 }
Chris Masone7a84562008-06-25 16:01:31 -04003237 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003238 }
Chris Masone7a84562008-06-25 16:01:31 -04003239 return 0;
3240 }
3241 return 1;
3242}
3243
Chris Mason7bb86312007-12-11 09:25:06 -05003244/*
Chris Mason925baed2008-06-25 16:01:30 -04003245 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003246 * returns 0 if it found something or 1 if there are no greater leaves.
3247 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003248 */
Chris Mason234b63a2007-03-13 10:46:10 -04003249int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003250{
3251 int slot;
3252 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003253 struct extent_buffer *c;
3254 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003255 struct btrfs_key key;
3256 u32 nritems;
3257 int ret;
3258
3259 nritems = btrfs_header_nritems(path->nodes[0]);
3260 if (nritems == 0) {
3261 return 1;
3262 }
3263
3264 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3265
Chris Mason925baed2008-06-25 16:01:30 -04003266 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003267 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003268 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3269 path->keep_locks = 0;
3270
3271 if (ret < 0)
3272 return ret;
3273
Chris Masona2135012008-06-25 16:01:30 -04003274 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003275 /*
3276 * by releasing the path above we dropped all our locks. A balance
3277 * could have added more items next to the key that used to be
3278 * at the very end of the block. So, check again here and
3279 * advance the path if there are now more items available.
3280 */
Chris Masona2135012008-06-25 16:01:30 -04003281 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003282 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003283 goto done;
3284 }
Chris Masond97e63b2007-02-20 16:40:44 -05003285
Chris Mason234b63a2007-03-13 10:46:10 -04003286 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003287 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003288 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003289
Chris Masond97e63b2007-02-20 16:40:44 -05003290 slot = path->slots[level] + 1;
3291 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003292 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003293 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003294 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003295 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003296 }
Chris Masond97e63b2007-02-20 16:40:44 -05003297 continue;
3298 }
Chris Mason5f39d392007-10-15 16:14:19 -04003299
Chris Mason925baed2008-06-25 16:01:30 -04003300 if (next) {
3301 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003302 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003303 }
Chris Mason5f39d392007-10-15 16:14:19 -04003304
Chris Mason0bd40a72008-07-17 12:54:43 -04003305 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3306 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003307 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003308
Chris Masonca7a79a2008-05-12 12:59:19 -04003309 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003310 if (!path->skip_locking) {
3311 WARN_ON(!btrfs_tree_locked(c));
3312 btrfs_tree_lock(next);
3313 }
Chris Masond97e63b2007-02-20 16:40:44 -05003314 break;
3315 }
3316 path->slots[level] = slot;
3317 while(1) {
3318 level--;
3319 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003320 if (path->locks[level])
3321 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003322 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003323 path->nodes[level] = next;
3324 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003325 if (!path->skip_locking)
3326 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003327 if (!level)
3328 break;
Chris Mason925baed2008-06-25 16:01:30 -04003329 if (level == 1 && path->locks[1] && path->reada)
3330 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003331 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003332 if (!path->skip_locking) {
3333 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3334 btrfs_tree_lock(next);
3335 }
Chris Masond97e63b2007-02-20 16:40:44 -05003336 }
Chris Mason925baed2008-06-25 16:01:30 -04003337done:
3338 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003339 return 0;
3340}
Chris Mason0b86a832008-03-24 15:01:56 -04003341
Chris Mason3f157a22008-06-25 16:01:31 -04003342/*
3343 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3344 * searching until it gets past min_objectid or finds an item of 'type'
3345 *
3346 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3347 */
Chris Mason0b86a832008-03-24 15:01:56 -04003348int btrfs_previous_item(struct btrfs_root *root,
3349 struct btrfs_path *path, u64 min_objectid,
3350 int type)
3351{
3352 struct btrfs_key found_key;
3353 struct extent_buffer *leaf;
3354 int ret;
3355
3356 while(1) {
3357 if (path->slots[0] == 0) {
3358 ret = btrfs_prev_leaf(root, path);
3359 if (ret != 0)
3360 return ret;
3361 } else {
3362 path->slots[0]--;
3363 }
3364 leaf = path->nodes[0];
3365 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3366 if (found_key.type == type)
3367 return 0;
3368 }
3369 return 1;
3370}