blob: 579124043d9b4c1615eff4cc9126d40e914225f1 [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 Masone02119d2008-09-05 16:13:11 -040063void noinline 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
Chris Masone02119d2008-09-05 16:13:11 -0400179int noinline __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 Masone02119d2008-09-05 16:13:11 -0400297int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400298 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 Masone02119d2008-09-05 16:13:11 -0400680static noinline int generic_bin_search(struct extent_buffer *eb,
681 unsigned long p,
682 int item_size, struct btrfs_key *key,
683 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500684{
685 int low = 0;
686 int high = max;
687 int mid;
688 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400689 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400690 struct btrfs_disk_key unaligned;
691 unsigned long offset;
692 char *map_token = NULL;
693 char *kaddr = NULL;
694 unsigned long map_start = 0;
695 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400696 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500697
698 while(low < high) {
699 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400700 offset = p + mid * item_size;
701
702 if (!map_token || offset < map_start ||
703 (offset + sizeof(struct btrfs_disk_key)) >
704 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400705 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400706 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400707 map_token = NULL;
708 }
709 err = map_extent_buffer(eb, offset,
710 sizeof(struct btrfs_disk_key),
711 &map_token, &kaddr,
712 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400713
Chris Mason479965d2007-10-15 16:14:27 -0400714 if (!err) {
715 tmp = (struct btrfs_disk_key *)(kaddr + offset -
716 map_start);
717 } else {
718 read_extent_buffer(eb, &unaligned,
719 offset, sizeof(unaligned));
720 tmp = &unaligned;
721 }
722
Chris Mason5f39d392007-10-15 16:14:19 -0400723 } else {
724 tmp = (struct btrfs_disk_key *)(kaddr + offset -
725 map_start);
726 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727 ret = comp_keys(tmp, key);
728
729 if (ret < 0)
730 low = mid + 1;
731 else if (ret > 0)
732 high = mid;
733 else {
734 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400735 if (map_token)
736 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500737 return 0;
738 }
739 }
740 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400741 if (map_token)
742 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743 return 1;
744}
745
Chris Mason97571fd2007-02-24 13:39:08 -0500746/*
747 * simple bin_search frontend that does the right thing for
748 * leaves vs nodes
749 */
Chris Mason5f39d392007-10-15 16:14:19 -0400750static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
751 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500752{
Chris Mason5f39d392007-10-15 16:14:19 -0400753 if (level == 0) {
754 return generic_bin_search(eb,
755 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400756 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400757 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400758 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500759 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400760 return generic_bin_search(eb,
761 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400762 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400763 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400764 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500765 }
766 return -1;
767}
768
Chris Masone02119d2008-09-05 16:13:11 -0400769static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400770 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500771{
Chris Masonca7a79a2008-05-12 12:59:19 -0400772 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500773 if (slot < 0)
774 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400775 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500776 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400777
778 BUG_ON(level == 0);
779
Chris Masondb945352007-10-15 16:15:53 -0400780 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400781 btrfs_level_size(root, level - 1),
782 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500783}
784
Chris Masone02119d2008-09-05 16:13:11 -0400785static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500786 struct btrfs_root *root,
787 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500788{
Chris Mason5f39d392007-10-15 16:14:19 -0400789 struct extent_buffer *right = NULL;
790 struct extent_buffer *mid;
791 struct extent_buffer *left = NULL;
792 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500793 int ret = 0;
794 int wret;
795 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500796 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400797 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500798 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500799
800 if (level == 0)
801 return 0;
802
Chris Mason5f39d392007-10-15 16:14:19 -0400803 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400804 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500805 WARN_ON(btrfs_header_generation(mid) != trans->transid);
806
Chris Mason1d4f8a02007-03-13 09:28:32 -0400807 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500808
Chris Mason234b63a2007-03-13 10:46:10 -0400809 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400810 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500811 pslot = path->slots[level + 1];
812
Chris Mason40689472007-03-17 14:29:23 -0400813 /*
814 * deal with the case where there is only one pointer in the root
815 * by promoting the node below to a root
816 */
Chris Mason5f39d392007-10-15 16:14:19 -0400817 if (!parent) {
818 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500819
Chris Mason5f39d392007-10-15 16:14:19 -0400820 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500821 return 0;
822
823 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400824 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400825 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500826 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400827 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500828 BUG_ON(ret);
829
Chris Mason925baed2008-06-25 16:01:30 -0400830 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500831 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400832 spin_unlock(&root->node_lock);
833
Chris Mason0b86a832008-03-24 15:01:56 -0400834 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400835 btrfs_tree_unlock(child);
836 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500837 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400838 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400839 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500840 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400841 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500842 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
843 root->root_key.objectid,
844 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500845 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400846 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400847 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500848 }
Chris Mason5f39d392007-10-15 16:14:19 -0400849 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400850 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500851 return 0;
852
Chris Mason5f39d392007-10-15 16:14:19 -0400853 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400854 err_on_enospc = 1;
855
Chris Mason5f39d392007-10-15 16:14:19 -0400856 left = read_node_slot(root, parent, pslot - 1);
857 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400858 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400859 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400860 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400861 if (wret) {
862 ret = wret;
863 goto enospc;
864 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400865 }
Chris Mason5f39d392007-10-15 16:14:19 -0400866 right = read_node_slot(root, parent, pslot + 1);
867 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400868 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400869 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400870 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400871 if (wret) {
872 ret = wret;
873 goto enospc;
874 }
875 }
876
877 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (left) {
879 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400880 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500881 if (wret < 0)
882 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400883 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400884 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500885 }
Chris Mason79f95c82007-03-01 15:16:26 -0500886
887 /*
888 * then try to empty the right most buffer into the middle
889 */
Chris Mason5f39d392007-10-15 16:14:19 -0400890 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400891 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400892 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500893 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400894 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400895 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500896 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400897 u32 blocksize = right->len;
898
Chris Mason5f39d392007-10-15 16:14:19 -0400899 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400900 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400901 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500902 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400903 wret = del_ptr(trans, root, path, level + 1, pslot +
904 1);
Chris Masonbb803952007-03-01 12:04:21 -0500905 if (wret)
906 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400907 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500908 blocksize,
909 btrfs_header_owner(parent),
910 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500911 if (wret)
912 ret = wret;
913 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400914 struct btrfs_disk_key right_key;
915 btrfs_node_key(right, &right_key, 0);
916 btrfs_set_node_key(parent, &right_key, pslot + 1);
917 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500918 }
919 }
Chris Mason5f39d392007-10-15 16:14:19 -0400920 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500921 /*
922 * we're not allowed to leave a node with one item in the
923 * tree during a delete. A deletion from lower in the tree
924 * could try to delete the only pointer in this node.
925 * So, pull some keys from the left.
926 * There has to be a left pointer at this point because
927 * otherwise we would have pulled some pointers from the
928 * right
929 */
Chris Mason5f39d392007-10-15 16:14:19 -0400930 BUG_ON(!left);
931 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400932 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500933 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400934 goto enospc;
935 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400936 if (wret == 1) {
937 wret = push_node_left(trans, root, left, mid, 1);
938 if (wret < 0)
939 ret = wret;
940 }
Chris Mason79f95c82007-03-01 15:16:26 -0500941 BUG_ON(wret == 1);
942 }
Chris Mason5f39d392007-10-15 16:14:19 -0400943 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500944 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500945 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400946 u64 bytenr = mid->start;
947 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400948
Chris Mason5f39d392007-10-15 16:14:19 -0400949 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400950 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400951 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500952 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400953 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500954 if (wret)
955 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500956 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
957 btrfs_header_owner(parent),
958 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500959 if (wret)
960 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500961 } else {
962 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400963 struct btrfs_disk_key mid_key;
964 btrfs_node_key(mid, &mid_key, 0);
965 btrfs_set_node_key(parent, &mid_key, pslot);
966 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500967 }
Chris Masonbb803952007-03-01 12:04:21 -0500968
Chris Mason79f95c82007-03-01 15:16:26 -0500969 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400970 if (left) {
971 if (btrfs_header_nritems(left) > orig_slot) {
972 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400973 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400974 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500975 path->slots[level + 1] -= 1;
976 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400977 if (mid) {
978 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400979 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400980 }
Chris Masonbb803952007-03-01 12:04:21 -0500981 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400982 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500983 path->slots[level] = orig_slot;
984 }
985 }
Chris Mason79f95c82007-03-01 15:16:26 -0500986 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400987 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400988 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400989 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500990 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400991enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400992 if (right) {
993 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400994 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400995 }
996 if (left) {
997 if (path->nodes[level] != left)
998 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400999 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001000 }
Chris Masonbb803952007-03-01 12:04:21 -05001001 return ret;
1002}
1003
Chris Masone66f7092007-04-20 13:16:02 -04001004/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -05001005static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1006 struct btrfs_root *root,
1007 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001008{
Chris Mason5f39d392007-10-15 16:14:19 -04001009 struct extent_buffer *right = NULL;
1010 struct extent_buffer *mid;
1011 struct extent_buffer *left = NULL;
1012 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001013 int ret = 0;
1014 int wret;
1015 int pslot;
1016 int orig_slot = path->slots[level];
1017 u64 orig_ptr;
1018
1019 if (level == 0)
1020 return 1;
1021
Chris Mason5f39d392007-10-15 16:14:19 -04001022 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001023 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001024 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1025
1026 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001027 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001028 pslot = path->slots[level + 1];
1029
Chris Mason5f39d392007-10-15 16:14:19 -04001030 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001031 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001032
Chris Mason5f39d392007-10-15 16:14:19 -04001033 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001034
1035 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001037 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001038
1039 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001041 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1042 wret = 1;
1043 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001044 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001045 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001046 if (ret)
1047 wret = 1;
1048 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001049 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001050 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001051 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001052 }
Chris Masone66f7092007-04-20 13:16:02 -04001053 if (wret < 0)
1054 ret = wret;
1055 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001056 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001057 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001058 btrfs_node_key(mid, &disk_key, 0);
1059 btrfs_set_node_key(parent, &disk_key, pslot);
1060 btrfs_mark_buffer_dirty(parent);
1061 if (btrfs_header_nritems(left) > orig_slot) {
1062 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001063 path->slots[level + 1] -= 1;
1064 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001065 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001067 } else {
1068 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001069 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001070 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001071 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001072 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001073 }
Chris Masone66f7092007-04-20 13:16:02 -04001074 return 0;
1075 }
Chris Mason925baed2008-06-25 16:01:30 -04001076 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001078 }
Chris Mason925baed2008-06-25 16:01:30 -04001079 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001080
1081 /*
1082 * then try to empty the right most buffer into the middle
1083 */
Chris Mason5f39d392007-10-15 16:14:19 -04001084 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001085 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001086 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001087 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001088 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1089 wret = 1;
1090 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001091 ret = btrfs_cow_block(trans, root, right,
1092 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001093 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001094 if (ret)
1095 wret = 1;
1096 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001097 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001098 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001099 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001100 }
Chris Masone66f7092007-04-20 13:16:02 -04001101 if (wret < 0)
1102 ret = wret;
1103 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001104 struct btrfs_disk_key disk_key;
1105
1106 btrfs_node_key(right, &disk_key, 0);
1107 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1108 btrfs_mark_buffer_dirty(parent);
1109
1110 if (btrfs_header_nritems(mid) <= orig_slot) {
1111 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001112 path->slots[level + 1] += 1;
1113 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001114 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001115 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001116 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001117 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001118 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001119 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001120 }
Chris Masone66f7092007-04-20 13:16:02 -04001121 return 0;
1122 }
Chris Mason925baed2008-06-25 16:01:30 -04001123 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001124 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001125 }
Chris Masone66f7092007-04-20 13:16:02 -04001126 return 1;
1127}
1128
Chris Mason74123bd2007-02-02 11:05:29 -05001129/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001130 * readahead one full node of leaves
1131 */
Chris Masone02119d2008-09-05 16:13:11 -04001132static noinline void reada_for_search(struct btrfs_root *root,
1133 struct btrfs_path *path,
1134 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001135{
Chris Mason5f39d392007-10-15 16:14:19 -04001136 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001137 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001138 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001139 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001140 u64 lowest_read;
1141 u64 highest_read;
1142 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001143 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001144 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001145 u32 nr;
1146 u32 blocksize;
1147 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001148
Chris Masona6b6e752007-10-15 16:22:39 -04001149 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001150 return;
1151
Chris Mason6702ed42007-08-07 16:15:09 -04001152 if (!path->nodes[level])
1153 return;
1154
Chris Mason5f39d392007-10-15 16:14:19 -04001155 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001156
Chris Mason3c69fae2007-08-07 15:52:22 -04001157 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001158 blocksize = btrfs_level_size(root, level - 1);
1159 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001160 if (eb) {
1161 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001162 return;
1163 }
1164
Chris Mason6b800532007-10-15 16:17:34 -04001165 highest_read = search;
1166 lowest_read = search;
1167
Chris Mason5f39d392007-10-15 16:14:19 -04001168 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001169 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001170 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001171 if (direction < 0) {
1172 if (nr == 0)
1173 break;
1174 nr--;
1175 } else if (direction > 0) {
1176 nr++;
1177 if (nr >= nritems)
1178 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001179 }
Chris Mason01f46652007-12-21 16:24:26 -05001180 if (path->reada < 0 && objectid) {
1181 btrfs_node_key(node, &disk_key, nr);
1182 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1183 break;
1184 }
Chris Mason6b800532007-10-15 16:17:34 -04001185 search = btrfs_node_blockptr(node, nr);
1186 if ((search >= lowest_read && search <= highest_read) ||
1187 (search < lowest_read && lowest_read - search <= 32768) ||
1188 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001189 readahead_tree_block(root, search, blocksize,
1190 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001191 nread += blocksize;
1192 }
1193 nscan++;
1194 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1195 break;
1196 if(nread > (1024 * 1024) || nscan > 128)
1197 break;
1198
1199 if (search < lowest_read)
1200 lowest_read = search;
1201 if (search > highest_read)
1202 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001203 }
1204}
Chris Mason925baed2008-06-25 16:01:30 -04001205
Chris Masone02119d2008-09-05 16:13:11 -04001206static noinline void unlock_up(struct btrfs_path *path, int level,
1207 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001208{
1209 int i;
1210 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001211 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001212 struct extent_buffer *t;
1213
1214 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1215 if (!path->nodes[i])
1216 break;
1217 if (!path->locks[i])
1218 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001219 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001220 skip_level = i + 1;
1221 continue;
1222 }
Chris Mason051e1b92008-06-25 16:01:30 -04001223 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001224 u32 nritems;
1225 t = path->nodes[i];
1226 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001227 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001228 skip_level = i + 1;
1229 continue;
1230 }
1231 }
Chris Mason051e1b92008-06-25 16:01:30 -04001232 if (skip_level < i && i >= lowest_unlock)
1233 no_skips = 1;
1234
Chris Mason925baed2008-06-25 16:01:30 -04001235 t = path->nodes[i];
1236 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1237 btrfs_tree_unlock(t);
1238 path->locks[i] = 0;
1239 }
1240 }
1241}
1242
Chris Mason3c69fae2007-08-07 15:52:22 -04001243/*
Chris Mason74123bd2007-02-02 11:05:29 -05001244 * look for key in the tree. path is filled in with nodes along the way
1245 * if key is found, we return zero and you can find the item in the leaf
1246 * level of the path (level 0)
1247 *
1248 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001249 * be inserted, and 1 is returned. If there are other errors during the
1250 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001251 *
1252 * if ins_len > 0, nodes and leaves will be split as we walk down the
1253 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1254 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001255 */
Chris Masone089f052007-03-16 16:20:31 -04001256int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1257 *root, struct btrfs_key *key, struct btrfs_path *p, int
1258 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259{
Chris Mason5f39d392007-10-15 16:14:19 -04001260 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001261 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001262 int slot;
1263 int ret;
1264 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001266 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001267 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001268 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001269 u64 blocknr;
1270 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001271 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001272
Chris Mason6702ed42007-08-07 16:15:09 -04001273 lowest_level = p->lowest_level;
1274 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001275 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001276 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001277 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001278 if (ins_len < 0)
1279 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001280
1281 prealloc_block.objectid = 0;
1282
Chris Masonbb803952007-03-01 12:04:21 -05001283again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001284 if (p->skip_locking)
1285 b = btrfs_root_node(root);
1286 else
1287 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001288
Chris Masoneb60cea2007-02-02 09:18:22 -05001289 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001290 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001291
1292 /*
1293 * setup the path here so we can release it under lock
1294 * contention with the cow code
1295 */
1296 p->nodes[level] = b;
1297 if (!p->skip_locking)
1298 p->locks[level] = 1;
1299
Chris Mason02217ed2007-03-02 16:08:05 -05001300 if (cow) {
1301 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001302
1303 /* is a cow on this block not required */
1304 spin_lock(&root->fs_info->hash_lock);
1305 if (btrfs_header_generation(b) == trans->transid &&
1306 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1307 spin_unlock(&root->fs_info->hash_lock);
1308 goto cow_done;
1309 }
1310 spin_unlock(&root->fs_info->hash_lock);
1311
1312 /* ok, we have to cow, is our old prealloc the right
1313 * size?
1314 */
1315 if (prealloc_block.objectid &&
1316 prealloc_block.offset != b->len) {
1317 btrfs_free_reserved_extent(root,
1318 prealloc_block.objectid,
1319 prealloc_block.offset);
1320 prealloc_block.objectid = 0;
1321 }
1322
1323 /*
1324 * for higher level blocks, try not to allocate blocks
1325 * with the block and the parent locks held.
1326 */
1327 if (level > 1 && !prealloc_block.objectid &&
1328 btrfs_path_lock_waiting(p, level)) {
1329 u32 size = b->len;
1330 u64 hint = b->start;
1331
1332 btrfs_release_path(root, p);
1333 ret = btrfs_reserve_extent(trans, root,
1334 size, size, 0,
1335 hint, (u64)-1,
1336 &prealloc_block, 0);
1337 BUG_ON(ret);
1338 goto again;
1339 }
1340
Chris Masone20d96d2007-03-22 12:13:20 -04001341 wret = btrfs_cow_block(trans, root, b,
1342 p->nodes[level + 1],
1343 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001344 &b, prealloc_block.objectid);
1345 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001346 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001347 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001348 ret = wret;
1349 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001350 }
Chris Mason02217ed2007-03-02 16:08:05 -05001351 }
Chris Mason65b51a02008-08-01 15:11:20 -04001352cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001353 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001354 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001355 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001356 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001357
Chris Masoneb60cea2007-02-02 09:18:22 -05001358 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001359 if (!p->skip_locking)
1360 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001361
Chris Mason123abc82007-03-14 14:14:43 -04001362 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001363 if (ret) {
1364 ret = -1;
1365 goto done;
1366 }
Chris Mason925baed2008-06-25 16:01:30 -04001367
Chris Mason5f39d392007-10-15 16:14:19 -04001368 ret = bin_search(b, key, level, &slot);
1369 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001370 if (ret && slot > 0)
1371 slot -= 1;
1372 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001374 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001375 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001376 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001377 if (sret) {
1378 ret = sret;
1379 goto done;
1380 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001381 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001382 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001383 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001384 int sret = balance_level(trans, root, p,
1385 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001386 if (sret) {
1387 ret = sret;
1388 goto done;
1389 }
Chris Masonbb803952007-03-01 12:04:21 -05001390 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001391 if (!b) {
1392 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001393 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001394 }
Chris Masonbb803952007-03-01 12:04:21 -05001395 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001396 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001397 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001398 unlock_up(p, level, lowest_unlock);
1399
Chris Mason9f3a7422007-08-07 15:52:19 -04001400 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001401 if (level == lowest_level) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001402 break;
Chris Mason925baed2008-06-25 16:01:30 -04001403 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001404
Chris Mason594a24e2008-06-25 16:01:30 -04001405 blocknr = btrfs_node_blockptr(b, slot);
1406 gen = btrfs_node_ptr_generation(b, slot);
1407 blocksize = btrfs_level_size(root, level - 1);
1408
1409 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1410 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001411 b = tmp;
1412 } else {
1413 /*
1414 * reduce lock contention at high levels
1415 * of the btree by dropping locks before
1416 * we read.
1417 */
1418 if (level > 1) {
1419 btrfs_release_path(NULL, p);
1420 if (tmp)
1421 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001422 if (should_reada)
1423 reada_for_search(root, p,
1424 level, slot,
1425 key->objectid);
1426
Chris Mason594a24e2008-06-25 16:01:30 -04001427 tmp = read_tree_block(root, blocknr,
1428 blocksize, gen);
1429 if (tmp)
1430 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001431 goto again;
1432 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001433 if (tmp)
1434 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001435 if (should_reada)
1436 reada_for_search(root, p,
1437 level, slot,
1438 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001439 b = read_node_slot(root, b, slot);
1440 }
1441 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001442 if (!p->skip_locking)
1443 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001444 } else {
1445 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001446 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001447 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001448 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001449 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001450 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001451 if (sret) {
1452 ret = sret;
1453 goto done;
1454 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001455 }
Chris Mason925baed2008-06-25 16:01:30 -04001456 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001457 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001458 }
1459 }
Chris Mason65b51a02008-08-01 15:11:20 -04001460 ret = 1;
1461done:
1462 if (prealloc_block.objectid) {
1463 btrfs_free_reserved_extent(root,
1464 prealloc_block.objectid,
1465 prealloc_block.offset);
1466 }
1467
1468 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001469}
1470
Chris Mason74123bd2007-02-02 11:05:29 -05001471/*
1472 * adjust the pointers going up the tree, starting at level
1473 * making sure the right key of each node is points to 'key'.
1474 * This is used after shifting pointers to the left, so it stops
1475 * fixing up pointers when a given leaf/node is not in slot 0 of the
1476 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001477 *
1478 * If this fails to write a tree block, it returns -1, but continues
1479 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001480 */
Chris Mason5f39d392007-10-15 16:14:19 -04001481static int fixup_low_keys(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root, struct btrfs_path *path,
1483 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001484{
1485 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001486 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001487 struct extent_buffer *t;
1488
Chris Mason234b63a2007-03-13 10:46:10 -04001489 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001490 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001491 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001492 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001493 t = path->nodes[i];
1494 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001495 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001496 if (tslot != 0)
1497 break;
1498 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001499 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001500}
1501
Chris Mason74123bd2007-02-02 11:05:29 -05001502/*
1503 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001504 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001505 *
1506 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1507 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001508 */
Chris Mason98ed5172008-01-03 10:01:48 -05001509static int push_node_left(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001511 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001512{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001513 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001514 int src_nritems;
1515 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001516 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001517
Chris Mason5f39d392007-10-15 16:14:19 -04001518 src_nritems = btrfs_header_nritems(src);
1519 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001520 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001521 WARN_ON(btrfs_header_generation(src) != trans->transid);
1522 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001523
Chris Masonbce4eae2008-04-24 14:42:46 -04001524 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001525 return 1;
1526
Chris Masoneb60cea2007-02-02 09:18:22 -05001527 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001528 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001529 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001530
Chris Masonbce4eae2008-04-24 14:42:46 -04001531 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001532 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001533 if (push_items < src_nritems) {
1534 /* leave at least 8 pointers in the node if
1535 * we aren't going to empty it
1536 */
1537 if (src_nritems - push_items < 8) {
1538 if (push_items <= 8)
1539 return 1;
1540 push_items -= 8;
1541 }
1542 }
1543 } else
1544 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001545
Chris Mason5f39d392007-10-15 16:14:19 -04001546 copy_extent_buffer(dst, src,
1547 btrfs_node_key_ptr_offset(dst_nritems),
1548 btrfs_node_key_ptr_offset(0),
1549 push_items * sizeof(struct btrfs_key_ptr));
1550
Chris Masonbb803952007-03-01 12:04:21 -05001551 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001552 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1553 btrfs_node_key_ptr_offset(push_items),
1554 (src_nritems - push_items) *
1555 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001556 }
Chris Mason5f39d392007-10-15 16:14:19 -04001557 btrfs_set_header_nritems(src, src_nritems - push_items);
1558 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1559 btrfs_mark_buffer_dirty(src);
1560 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001561 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001562}
1563
Chris Mason97571fd2007-02-24 13:39:08 -05001564/*
Chris Mason79f95c82007-03-01 15:16:26 -05001565 * try to push data from one node into the next node right in the
1566 * tree.
1567 *
1568 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1569 * error, and > 0 if there was no room in the right hand block.
1570 *
1571 * this will only push up to 1/2 the contents of the left node over
1572 */
Chris Mason5f39d392007-10-15 16:14:19 -04001573static int balance_node_right(struct btrfs_trans_handle *trans,
1574 struct btrfs_root *root,
1575 struct extent_buffer *dst,
1576 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001577{
Chris Mason79f95c82007-03-01 15:16:26 -05001578 int push_items = 0;
1579 int max_push;
1580 int src_nritems;
1581 int dst_nritems;
1582 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001583
Chris Mason7bb86312007-12-11 09:25:06 -05001584 WARN_ON(btrfs_header_generation(src) != trans->transid);
1585 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1586
Chris Mason5f39d392007-10-15 16:14:19 -04001587 src_nritems = btrfs_header_nritems(src);
1588 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001589 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001590 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001591 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001592 }
1593
1594 if (src_nritems < 4) {
1595 return 1;
1596 }
Chris Mason79f95c82007-03-01 15:16:26 -05001597
1598 max_push = src_nritems / 2 + 1;
1599 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001600 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001601 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001602 }
Yan252c38f2007-08-29 09:11:44 -04001603
Chris Mason79f95c82007-03-01 15:16:26 -05001604 if (max_push < push_items)
1605 push_items = max_push;
1606
Chris Mason5f39d392007-10-15 16:14:19 -04001607 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1608 btrfs_node_key_ptr_offset(0),
1609 (dst_nritems) *
1610 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001611
Chris Mason5f39d392007-10-15 16:14:19 -04001612 copy_extent_buffer(dst, src,
1613 btrfs_node_key_ptr_offset(0),
1614 btrfs_node_key_ptr_offset(src_nritems - push_items),
1615 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 btrfs_set_header_nritems(src, src_nritems - push_items);
1618 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001619
Chris Mason5f39d392007-10-15 16:14:19 -04001620 btrfs_mark_buffer_dirty(src);
1621 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001622 return ret;
1623}
1624
1625/*
Chris Mason97571fd2007-02-24 13:39:08 -05001626 * helper function to insert a new root level in the tree.
1627 * A new node is allocated, and a single item is inserted to
1628 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001629 *
1630 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001631 */
Chris Mason98ed5172008-01-03 10:01:48 -05001632static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001633 struct btrfs_root *root,
1634 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001635{
Chris Mason7bb86312007-12-11 09:25:06 -05001636 u64 root_gen;
1637 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 struct extent_buffer *lower;
1639 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001640 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001641 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001642
1643 BUG_ON(path->nodes[level]);
1644 BUG_ON(path->nodes[level-1] != root->node);
1645
Chris Mason7bb86312007-12-11 09:25:06 -05001646 if (root->ref_cows)
1647 root_gen = trans->transid;
1648 else
1649 root_gen = 0;
1650
1651 lower = path->nodes[level-1];
1652 if (level == 1)
1653 btrfs_item_key(lower, &lower_key, 0);
1654 else
1655 btrfs_node_key(lower, &lower_key, 0);
1656
Chris Mason925baed2008-06-25 16:01:30 -04001657 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001658 root->root_key.objectid,
1659 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001660 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001661 if (IS_ERR(c))
1662 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001663
Chris Mason5f39d392007-10-15 16:14:19 -04001664 memset_extent_buffer(c, 0, 0, root->nodesize);
1665 btrfs_set_header_nritems(c, 1);
1666 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001667 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001668 btrfs_set_header_generation(c, trans->transid);
1669 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001670
Chris Mason5f39d392007-10-15 16:14:19 -04001671 write_extent_buffer(c, root->fs_info->fsid,
1672 (unsigned long)btrfs_header_fsid(c),
1673 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001674
1675 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1676 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1677 BTRFS_UUID_SIZE);
1678
Chris Mason5f39d392007-10-15 16:14:19 -04001679 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001680 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001681 lower_gen = btrfs_header_generation(lower);
1682 WARN_ON(lower_gen == 0);
1683
1684 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001685
1686 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001687
Chris Mason925baed2008-06-25 16:01:30 -04001688 spin_lock(&root->node_lock);
1689 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001690 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001691 spin_unlock(&root->node_lock);
1692
1693 /* the super has an extra ref to root->node */
1694 free_extent_buffer(old);
1695
Chris Mason0b86a832008-03-24 15:01:56 -04001696 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001697 extent_buffer_get(c);
1698 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001699 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001700 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001701
1702 if (root->ref_cows && lower_gen != trans->transid) {
1703 struct btrfs_path *back_path = btrfs_alloc_path();
1704 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001705 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001706 ret = btrfs_insert_extent_backref(trans,
1707 root->fs_info->extent_root,
1708 path, lower->start,
1709 root->root_key.objectid,
1710 trans->transid, 0, 0);
1711 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001712 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001713 btrfs_free_path(back_path);
1714 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001715 return 0;
1716}
1717
Chris Mason74123bd2007-02-02 11:05:29 -05001718/*
1719 * worker function to insert a single pointer in a node.
1720 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001721 *
Chris Mason74123bd2007-02-02 11:05:29 -05001722 * slot and level indicate where you want the key to go, and
1723 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001724 *
1725 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001726 */
Chris Masone089f052007-03-16 16:20:31 -04001727static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1728 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001729 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001730{
Chris Mason5f39d392007-10-15 16:14:19 -04001731 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001732 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001733
1734 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001735 lower = path->nodes[level];
1736 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001737 if (slot > nritems)
1738 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001739 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001740 BUG();
1741 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001742 memmove_extent_buffer(lower,
1743 btrfs_node_key_ptr_offset(slot + 1),
1744 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001745 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001746 }
Chris Mason5f39d392007-10-15 16:14:19 -04001747 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001748 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001749 WARN_ON(trans->transid == 0);
1750 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001751 btrfs_set_header_nritems(lower, nritems + 1);
1752 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001753 return 0;
1754}
1755
Chris Mason97571fd2007-02-24 13:39:08 -05001756/*
1757 * split the node at the specified level in path in two.
1758 * The path is corrected to point to the appropriate node after the split
1759 *
1760 * Before splitting this tries to make some room in the node by pushing
1761 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001762 *
1763 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001764 */
Chris Masone02119d2008-09-05 16:13:11 -04001765static noinline int split_node(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001768{
Chris Mason7bb86312007-12-11 09:25:06 -05001769 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001770 struct extent_buffer *c;
1771 struct extent_buffer *split;
1772 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001774 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001775 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001776 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001777
Chris Mason5f39d392007-10-15 16:14:19 -04001778 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001779 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001780 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001781 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001782 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001783 if (ret)
1784 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001785 } else {
1786 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001787 c = path->nodes[level];
1788 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001789 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001790 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001791 if (ret < 0)
1792 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001793 }
Chris Masone66f7092007-04-20 13:16:02 -04001794
Chris Mason5f39d392007-10-15 16:14:19 -04001795 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001796 if (root->ref_cows)
1797 root_gen = trans->transid;
1798 else
1799 root_gen = 0;
1800
1801 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001802 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001803 root->root_key.objectid,
1804 root_gen,
1805 btrfs_disk_key_objectid(&disk_key),
1806 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001807 if (IS_ERR(split))
1808 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001809
Chris Mason5f39d392007-10-15 16:14:19 -04001810 btrfs_set_header_flags(split, btrfs_header_flags(c));
1811 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001812 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001813 btrfs_set_header_generation(split, trans->transid);
1814 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001815 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001816 write_extent_buffer(split, root->fs_info->fsid,
1817 (unsigned long)btrfs_header_fsid(split),
1818 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001819 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1820 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1821 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001822
Chris Mason7518a232007-03-12 12:01:18 -04001823 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001824
1825 copy_extent_buffer(split, c,
1826 btrfs_node_key_ptr_offset(0),
1827 btrfs_node_key_ptr_offset(mid),
1828 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1829 btrfs_set_header_nritems(split, c_nritems - mid);
1830 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001831 ret = 0;
1832
Chris Mason5f39d392007-10-15 16:14:19 -04001833 btrfs_mark_buffer_dirty(c);
1834 btrfs_mark_buffer_dirty(split);
1835
1836 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001837 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001838 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001839 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001840 if (wret)
1841 ret = wret;
1842
Chris Mason5de08d72007-02-24 06:24:44 -05001843 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001844 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001845 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001846 free_extent_buffer(c);
1847 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001848 path->slots[level + 1] += 1;
1849 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001850 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001851 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001852 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001853 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001854}
1855
Chris Mason74123bd2007-02-02 11:05:29 -05001856/*
1857 * how many bytes are required to store the items in a leaf. start
1858 * and nr indicate which items in the leaf to check. This totals up the
1859 * space used both by the item structs and the item data
1860 */
Chris Mason5f39d392007-10-15 16:14:19 -04001861static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862{
1863 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001864 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001865 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001866
1867 if (!nr)
1868 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001869 data_len = btrfs_item_end_nr(l, start);
1870 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001871 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001872 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001873 return data_len;
1874}
1875
Chris Mason74123bd2007-02-02 11:05:29 -05001876/*
Chris Masond4dbff92007-04-04 14:08:15 -04001877 * The space between the end of the leaf items and
1878 * the start of the leaf data. IOW, how much room
1879 * the leaf has left for both items and data
1880 */
Chris Masone02119d2008-09-05 16:13:11 -04001881int noinline btrfs_leaf_free_space(struct btrfs_root *root,
1882 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001883{
Chris Mason5f39d392007-10-15 16:14:19 -04001884 int nritems = btrfs_header_nritems(leaf);
1885 int ret;
1886 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1887 if (ret < 0) {
1888 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001889 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001890 leaf_space_used(leaf, 0, nritems), nritems);
1891 }
1892 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001893}
1894
1895/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001896 * push some data in the path leaf to the right, trying to free up at
1897 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001898 *
1899 * returns 1 if the push failed because the other node didn't have enough
1900 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001901 */
Chris Masone089f052007-03-16 16:20:31 -04001902static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001903 *root, struct btrfs_path *path, int data_size,
1904 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001905{
Chris Mason5f39d392007-10-15 16:14:19 -04001906 struct extent_buffer *left = path->nodes[0];
1907 struct extent_buffer *right;
1908 struct extent_buffer *upper;
1909 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001910 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001911 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001912 int free_space;
1913 int push_space = 0;
1914 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001915 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001916 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001917 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001918 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001919 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001920 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001921 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001922
1923 slot = path->slots[1];
1924 if (!path->nodes[1]) {
1925 return 1;
1926 }
1927 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001928 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001929 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001930
Chris Masona2135012008-06-25 16:01:30 -04001931 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1932
Chris Masonca7a79a2008-05-12 12:59:19 -04001933 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001934 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001935 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001936 if (free_space < data_size + sizeof(struct btrfs_item))
1937 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 /* cow and double check */
1940 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04001941 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001942 if (ret)
1943 goto out_unlock;
1944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001946 if (free_space < data_size + sizeof(struct btrfs_item))
1947 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001948
1949 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001950 if (left_nritems == 0)
1951 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001952
Chris Mason34a38212007-11-07 13:31:03 -05001953 if (empty)
1954 nr = 0;
1955 else
1956 nr = 1;
1957
1958 i = left_nritems - 1;
1959 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001960 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001961
Chris Mason00ec4c52007-02-24 12:47:20 -05001962 if (path->slots[0] == i)
1963 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001964
1965 if (!left->map_token) {
1966 map_extent_buffer(left, (unsigned long)item,
1967 sizeof(struct btrfs_item),
1968 &left->map_token, &left->kaddr,
1969 &left->map_start, &left->map_len,
1970 KM_USER1);
1971 }
1972
1973 this_item_size = btrfs_item_size(left, item);
1974 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001975 break;
1976 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001977 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001978 if (i == 0)
1979 break;
1980 i--;
Chris Masondb945352007-10-15 16:15:53 -04001981 }
1982 if (left->map_token) {
1983 unmap_extent_buffer(left, left->map_token, KM_USER1);
1984 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001985 }
Chris Mason5f39d392007-10-15 16:14:19 -04001986
Chris Mason925baed2008-06-25 16:01:30 -04001987 if (push_items == 0)
1988 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001989
Chris Mason34a38212007-11-07 13:31:03 -05001990 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001991 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001992
Chris Mason00ec4c52007-02-24 12:47:20 -05001993 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001994 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001995
Chris Mason5f39d392007-10-15 16:14:19 -04001996 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001997 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001998
Chris Mason00ec4c52007-02-24 12:47:20 -05001999 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002000 data_end = leaf_data_end(root, right);
2001 memmove_extent_buffer(right,
2002 btrfs_leaf_data(right) + data_end - push_space,
2003 btrfs_leaf_data(right) + data_end,
2004 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2005
Chris Mason00ec4c52007-02-24 12:47:20 -05002006 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002007 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002008 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2009 btrfs_leaf_data(left) + leaf_data_end(root, left),
2010 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002011
2012 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2013 btrfs_item_nr_offset(0),
2014 right_nritems * sizeof(struct btrfs_item));
2015
Chris Mason00ec4c52007-02-24 12:47:20 -05002016 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002017 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2018 btrfs_item_nr_offset(left_nritems - push_items),
2019 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002020
2021 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002022 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002023 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002024 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002025 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002026 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002027 if (!right->map_token) {
2028 map_extent_buffer(right, (unsigned long)item,
2029 sizeof(struct btrfs_item),
2030 &right->map_token, &right->kaddr,
2031 &right->map_start, &right->map_len,
2032 KM_USER1);
2033 }
2034 push_space -= btrfs_item_size(right, item);
2035 btrfs_set_item_offset(right, item, push_space);
2036 }
2037
2038 if (right->map_token) {
2039 unmap_extent_buffer(right, right->map_token, KM_USER1);
2040 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002041 }
Chris Mason7518a232007-03-12 12:01:18 -04002042 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002043 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002044
Chris Mason34a38212007-11-07 13:31:03 -05002045 if (left_nritems)
2046 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_item_key(right, &disk_key, 0);
2050 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002051 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002052
Chris Mason00ec4c52007-02-24 12:47:20 -05002053 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002054 if (path->slots[0] >= left_nritems) {
2055 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002056 if (btrfs_header_nritems(path->nodes[0]) == 0)
2057 clean_tree_block(trans, root, path->nodes[0]);
2058 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002059 free_extent_buffer(path->nodes[0]);
2060 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002061 path->slots[1] += 1;
2062 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002063 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002064 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002065 }
2066 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002067
2068out_unlock:
2069 btrfs_tree_unlock(right);
2070 free_extent_buffer(right);
2071 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002072}
Chris Mason925baed2008-06-25 16:01:30 -04002073
Chris Mason00ec4c52007-02-24 12:47:20 -05002074/*
Chris Mason74123bd2007-02-02 11:05:29 -05002075 * push some data in the path leaf to the left, trying to free up at
2076 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2077 */
Chris Masone089f052007-03-16 16:20:31 -04002078static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002079 *root, struct btrfs_path *path, int data_size,
2080 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002081{
Chris Mason5f39d392007-10-15 16:14:19 -04002082 struct btrfs_disk_key disk_key;
2083 struct extent_buffer *right = path->nodes[0];
2084 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002085 int slot;
2086 int i;
2087 int free_space;
2088 int push_space = 0;
2089 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002090 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002091 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002092 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002093 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002094 int ret = 0;
2095 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002096 u32 this_item_size;
2097 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002098
2099 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002100 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002101 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002102 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002103 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002104
Chris Mason3685f792007-10-19 09:23:27 -04002105 right_nritems = btrfs_header_nritems(right);
2106 if (right_nritems == 0) {
2107 return 1;
2108 }
2109
Chris Masona2135012008-06-25 16:01:30 -04002110 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2111
Chris Masonca7a79a2008-05-12 12:59:19 -04002112 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002113 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002114 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002115 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002116 ret = 1;
2117 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002118 }
Chris Mason02217ed2007-03-02 16:08:05 -05002119
2120 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002121 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002122 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002123 if (ret) {
2124 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002125 ret = 1;
2126 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002127 }
Chris Mason3685f792007-10-19 09:23:27 -04002128
Chris Mason123abc82007-03-14 14:14:43 -04002129 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002130 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002131 ret = 1;
2132 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002133 }
2134
Chris Mason34a38212007-11-07 13:31:03 -05002135 if (empty)
2136 nr = right_nritems;
2137 else
2138 nr = right_nritems - 1;
2139
2140 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002141 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002142 if (!right->map_token) {
2143 map_extent_buffer(right, (unsigned long)item,
2144 sizeof(struct btrfs_item),
2145 &right->map_token, &right->kaddr,
2146 &right->map_start, &right->map_len,
2147 KM_USER1);
2148 }
2149
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150 if (path->slots[0] == i)
2151 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002152
2153 this_item_size = btrfs_item_size(right, item);
2154 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002155 break;
Chris Masondb945352007-10-15 16:15:53 -04002156
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002158 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002159 }
Chris Masondb945352007-10-15 16:15:53 -04002160
2161 if (right->map_token) {
2162 unmap_extent_buffer(right, right->map_token, KM_USER1);
2163 right->map_token = NULL;
2164 }
2165
Chris Masonbe0e5c02007-01-26 15:51:26 -05002166 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002167 ret = 1;
2168 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 }
Chris Mason34a38212007-11-07 13:31:03 -05002170 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002171 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002172
Chris Masonbe0e5c02007-01-26 15:51:26 -05002173 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002174 copy_extent_buffer(left, right,
2175 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2176 btrfs_item_nr_offset(0),
2177 push_items * sizeof(struct btrfs_item));
2178
Chris Mason123abc82007-03-14 14:14:43 -04002179 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_item_offset_nr(right, push_items -1);
2181
2182 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002183 leaf_data_end(root, left) - push_space,
2184 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002185 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002186 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002187 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002188 BUG_ON(old_left_nritems < 0);
2189
Chris Masondb945352007-10-15 16:15:53 -04002190 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002191 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002192 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002195 if (!left->map_token) {
2196 map_extent_buffer(left, (unsigned long)item,
2197 sizeof(struct btrfs_item),
2198 &left->map_token, &left->kaddr,
2199 &left->map_start, &left->map_len,
2200 KM_USER1);
2201 }
2202
Chris Mason5f39d392007-10-15 16:14:19 -04002203 ioff = btrfs_item_offset(left, item);
2204 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002205 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002206 }
Chris Mason5f39d392007-10-15 16:14:19 -04002207 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002208 if (left->map_token) {
2209 unmap_extent_buffer(left, left->map_token, KM_USER1);
2210 left->map_token = NULL;
2211 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002212
2213 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002214 if (push_items > right_nritems) {
2215 printk("push items %d nr %u\n", push_items, right_nritems);
2216 WARN_ON(1);
2217 }
Chris Mason5f39d392007-10-15 16:14:19 -04002218
Chris Mason34a38212007-11-07 13:31:03 -05002219 if (push_items < right_nritems) {
2220 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2221 leaf_data_end(root, right);
2222 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2223 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2224 btrfs_leaf_data(right) +
2225 leaf_data_end(root, right), push_space);
2226
2227 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002228 btrfs_item_nr_offset(push_items),
2229 (btrfs_header_nritems(right) - push_items) *
2230 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002231 }
Yaneef1c492007-11-26 10:58:13 -05002232 right_nritems -= push_items;
2233 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002234 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 for (i = 0; i < right_nritems; i++) {
2236 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002237
2238 if (!right->map_token) {
2239 map_extent_buffer(right, (unsigned long)item,
2240 sizeof(struct btrfs_item),
2241 &right->map_token, &right->kaddr,
2242 &right->map_start, &right->map_len,
2243 KM_USER1);
2244 }
2245
2246 push_space = push_space - btrfs_item_size(right, item);
2247 btrfs_set_item_offset(right, item, push_space);
2248 }
2249 if (right->map_token) {
2250 unmap_extent_buffer(right, right->map_token, KM_USER1);
2251 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002252 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002253
Chris Mason5f39d392007-10-15 16:14:19 -04002254 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002255 if (right_nritems)
2256 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002257
Chris Mason5f39d392007-10-15 16:14:19 -04002258 btrfs_item_key(right, &disk_key, 0);
2259 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002260 if (wret)
2261 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002262
2263 /* then fixup the leaf pointer in the path */
2264 if (path->slots[0] < push_items) {
2265 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002266 if (btrfs_header_nritems(path->nodes[0]) == 0)
2267 clean_tree_block(trans, root, path->nodes[0]);
2268 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002269 free_extent_buffer(path->nodes[0]);
2270 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002271 path->slots[1] -= 1;
2272 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002273 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002274 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002275 path->slots[0] -= push_items;
2276 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002277 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002278 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002279out:
2280 btrfs_tree_unlock(left);
2281 free_extent_buffer(left);
2282 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002283}
2284
Chris Mason74123bd2007-02-02 11:05:29 -05002285/*
2286 * split the path's leaf in two, making sure there is at least data_size
2287 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002288 *
2289 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002290 */
Chris Masone02119d2008-09-05 16:13:11 -04002291static noinline int split_leaf(struct btrfs_trans_handle *trans,
2292 struct btrfs_root *root,
2293 struct btrfs_key *ins_key,
2294 struct btrfs_path *path, int data_size,
2295 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002296{
Chris Mason7bb86312007-12-11 09:25:06 -05002297 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002298 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002299 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002300 int mid;
2301 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002302 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002303 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002304 int data_copy_size;
2305 int rt_data_off;
2306 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002307 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002308 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002309 int double_split;
2310 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002311 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002312
Chris Masoncc0c5532007-10-25 15:42:57 -04002313 if (extend)
2314 space_needed = data_size;
2315
Chris Mason7bb86312007-12-11 09:25:06 -05002316 if (root->ref_cows)
2317 root_gen = trans->transid;
2318 else
2319 root_gen = 0;
2320
Chris Mason40689472007-03-17 14:29:23 -04002321 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002322 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002323 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002324 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002325 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002326 }
2327 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002328 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002329 if (wret < 0)
2330 return wret;
2331 }
2332 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002333
Chris Mason3685f792007-10-19 09:23:27 -04002334 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002335 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002336 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002337 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002338
Chris Mason5c680ed2007-02-22 11:39:13 -05002339 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002340 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002341 if (ret)
2342 return ret;
2343 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002344again:
2345 double_split = 0;
2346 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002347 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002348 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002349 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002350
Chris Mason7bb86312007-12-11 09:25:06 -05002351 btrfs_item_key(l, &disk_key, 0);
2352
Chris Mason925baed2008-06-25 16:01:30 -04002353 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002354 root->root_key.objectid,
2355 root_gen, disk_key.objectid, 0,
2356 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002357 if (IS_ERR(right)) {
2358 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002359 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002360 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002361
Chris Mason5f39d392007-10-15 16:14:19 -04002362 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002363 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002364 btrfs_set_header_generation(right, trans->transid);
2365 btrfs_set_header_owner(right, root->root_key.objectid);
2366 btrfs_set_header_level(right, 0);
2367 write_extent_buffer(right, root->fs_info->fsid,
2368 (unsigned long)btrfs_header_fsid(right),
2369 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002370
2371 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2372 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2373 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002374 if (mid <= slot) {
2375 if (nritems == 1 ||
2376 leaf_space_used(l, mid, nritems - mid) + space_needed >
2377 BTRFS_LEAF_DATA_SIZE(root)) {
2378 if (slot >= nritems) {
2379 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002380 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002381 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002382 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002383 path->slots[1] + 1, 1);
2384 if (wret)
2385 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002386
2387 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002388 free_extent_buffer(path->nodes[0]);
2389 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002390 path->slots[0] = 0;
2391 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002392 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002393 return ret;
2394 }
2395 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002396 if (mid != nritems &&
2397 leaf_space_used(l, mid, nritems - mid) +
2398 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2399 double_split = 1;
2400 }
Chris Masond4dbff92007-04-04 14:08:15 -04002401 }
2402 } else {
2403 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2404 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002405 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002406 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002407 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002408 wret = insert_ptr(trans, root, path,
2409 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002410 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002411 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002412 if (wret)
2413 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002414 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002415 free_extent_buffer(path->nodes[0]);
2416 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002417 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002418 if (path->slots[1] == 0) {
2419 wret = fixup_low_keys(trans, root,
2420 path, &disk_key, 1);
2421 if (wret)
2422 ret = wret;
2423 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002424 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002425 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002426 } else if (extend && slot == 0) {
2427 mid = 1;
2428 } else {
2429 mid = slot;
2430 if (mid != nritems &&
2431 leaf_space_used(l, mid, nritems - mid) +
2432 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2433 double_split = 1;
2434 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002435 }
Chris Masond4dbff92007-04-04 14:08:15 -04002436 }
2437 }
Chris Mason5f39d392007-10-15 16:14:19 -04002438 nritems = nritems - mid;
2439 btrfs_set_header_nritems(right, nritems);
2440 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2441
2442 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2443 btrfs_item_nr_offset(mid),
2444 nritems * sizeof(struct btrfs_item));
2445
2446 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002447 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2448 data_copy_size, btrfs_leaf_data(l) +
2449 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002450
Chris Mason5f39d392007-10-15 16:14:19 -04002451 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2452 btrfs_item_end_nr(l, mid);
2453
2454 for (i = 0; i < nritems; i++) {
2455 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002456 u32 ioff;
2457
2458 if (!right->map_token) {
2459 map_extent_buffer(right, (unsigned long)item,
2460 sizeof(struct btrfs_item),
2461 &right->map_token, &right->kaddr,
2462 &right->map_start, &right->map_len,
2463 KM_USER1);
2464 }
2465
2466 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002467 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002468 }
Chris Mason74123bd2007-02-02 11:05:29 -05002469
Chris Masondb945352007-10-15 16:15:53 -04002470 if (right->map_token) {
2471 unmap_extent_buffer(right, right->map_token, KM_USER1);
2472 right->map_token = NULL;
2473 }
2474
Chris Mason5f39d392007-10-15 16:14:19 -04002475 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002476 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002477 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002478 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2479 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002480 if (wret)
2481 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002482
2483 btrfs_mark_buffer_dirty(right);
2484 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002485 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002486
Chris Masonbe0e5c02007-01-26 15:51:26 -05002487 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002488 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002489 free_extent_buffer(path->nodes[0]);
2490 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 path->slots[0] -= mid;
2492 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002493 } else {
2494 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002495 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002496 }
Chris Mason5f39d392007-10-15 16:14:19 -04002497
Chris Masoneb60cea2007-02-02 09:18:22 -05002498 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002499
Chris Masoncc0c5532007-10-25 15:42:57 -04002500 if (double_split) {
2501 BUG_ON(num_doubles != 0);
2502 num_doubles++;
2503 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002504 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002505 return ret;
2506}
2507
Chris Masonb18c6682007-04-17 13:26:50 -04002508int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2509 struct btrfs_root *root,
2510 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002511 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002512{
2513 int ret = 0;
2514 int slot;
2515 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct extent_buffer *leaf;
2517 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002518 u32 nritems;
2519 unsigned int data_end;
2520 unsigned int old_data_start;
2521 unsigned int old_size;
2522 unsigned int size_diff;
2523 int i;
2524
2525 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002526 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002527 slot = path->slots[0];
2528
2529 old_size = btrfs_item_size_nr(leaf, slot);
2530 if (old_size == new_size)
2531 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002532
Chris Mason5f39d392007-10-15 16:14:19 -04002533 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002534 data_end = leaf_data_end(root, leaf);
2535
Chris Mason5f39d392007-10-15 16:14:19 -04002536 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002537
Chris Masonb18c6682007-04-17 13:26:50 -04002538 size_diff = old_size - new_size;
2539
2540 BUG_ON(slot < 0);
2541 BUG_ON(slot >= nritems);
2542
2543 /*
2544 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2545 */
2546 /* first correct the data pointers */
2547 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002548 u32 ioff;
2549 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002550
2551 if (!leaf->map_token) {
2552 map_extent_buffer(leaf, (unsigned long)item,
2553 sizeof(struct btrfs_item),
2554 &leaf->map_token, &leaf->kaddr,
2555 &leaf->map_start, &leaf->map_len,
2556 KM_USER1);
2557 }
2558
Chris Mason5f39d392007-10-15 16:14:19 -04002559 ioff = btrfs_item_offset(leaf, item);
2560 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002561 }
Chris Masondb945352007-10-15 16:15:53 -04002562
2563 if (leaf->map_token) {
2564 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2565 leaf->map_token = NULL;
2566 }
2567
Chris Masonb18c6682007-04-17 13:26:50 -04002568 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002569 if (from_end) {
2570 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2571 data_end + size_diff, btrfs_leaf_data(leaf) +
2572 data_end, old_data_start + new_size - data_end);
2573 } else {
2574 struct btrfs_disk_key disk_key;
2575 u64 offset;
2576
2577 btrfs_item_key(leaf, &disk_key, slot);
2578
2579 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2580 unsigned long ptr;
2581 struct btrfs_file_extent_item *fi;
2582
2583 fi = btrfs_item_ptr(leaf, slot,
2584 struct btrfs_file_extent_item);
2585 fi = (struct btrfs_file_extent_item *)(
2586 (unsigned long)fi - size_diff);
2587
2588 if (btrfs_file_extent_type(leaf, fi) ==
2589 BTRFS_FILE_EXTENT_INLINE) {
2590 ptr = btrfs_item_ptr_offset(leaf, slot);
2591 memmove_extent_buffer(leaf, ptr,
2592 (unsigned long)fi,
2593 offsetof(struct btrfs_file_extent_item,
2594 disk_bytenr));
2595 }
2596 }
2597
2598 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2599 data_end + size_diff, btrfs_leaf_data(leaf) +
2600 data_end, old_data_start - data_end);
2601
2602 offset = btrfs_disk_key_offset(&disk_key);
2603 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2604 btrfs_set_item_key(leaf, &disk_key, slot);
2605 if (slot == 0)
2606 fixup_low_keys(trans, root, path, &disk_key, 1);
2607 }
Chris Mason5f39d392007-10-15 16:14:19 -04002608
2609 item = btrfs_item_nr(leaf, slot);
2610 btrfs_set_item_size(leaf, item, new_size);
2611 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002612
2613 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002614 if (btrfs_leaf_free_space(root, leaf) < 0) {
2615 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002616 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002617 }
Chris Masonb18c6682007-04-17 13:26:50 -04002618 return ret;
2619}
2620
Chris Mason5f39d392007-10-15 16:14:19 -04002621int btrfs_extend_item(struct btrfs_trans_handle *trans,
2622 struct btrfs_root *root, struct btrfs_path *path,
2623 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002624{
2625 int ret = 0;
2626 int slot;
2627 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002628 struct extent_buffer *leaf;
2629 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002630 u32 nritems;
2631 unsigned int data_end;
2632 unsigned int old_data;
2633 unsigned int old_size;
2634 int i;
2635
2636 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002637 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002638
Chris Mason5f39d392007-10-15 16:14:19 -04002639 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002640 data_end = leaf_data_end(root, leaf);
2641
Chris Mason5f39d392007-10-15 16:14:19 -04002642 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2643 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002644 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002645 }
Chris Mason6567e832007-04-16 09:22:45 -04002646 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002647 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002648
2649 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002650 if (slot >= nritems) {
2651 btrfs_print_leaf(root, leaf);
2652 printk("slot %d too large, nritems %d\n", slot, nritems);
2653 BUG_ON(1);
2654 }
Chris Mason6567e832007-04-16 09:22:45 -04002655
2656 /*
2657 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2658 */
2659 /* first correct the data pointers */
2660 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002661 u32 ioff;
2662 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002663
2664 if (!leaf->map_token) {
2665 map_extent_buffer(leaf, (unsigned long)item,
2666 sizeof(struct btrfs_item),
2667 &leaf->map_token, &leaf->kaddr,
2668 &leaf->map_start, &leaf->map_len,
2669 KM_USER1);
2670 }
Chris Mason5f39d392007-10-15 16:14:19 -04002671 ioff = btrfs_item_offset(leaf, item);
2672 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002673 }
Chris Mason5f39d392007-10-15 16:14:19 -04002674
Chris Masondb945352007-10-15 16:15:53 -04002675 if (leaf->map_token) {
2676 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2677 leaf->map_token = NULL;
2678 }
2679
Chris Mason6567e832007-04-16 09:22:45 -04002680 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002681 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002682 data_end - data_size, btrfs_leaf_data(leaf) +
2683 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002684
Chris Mason6567e832007-04-16 09:22:45 -04002685 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002686 old_size = btrfs_item_size_nr(leaf, slot);
2687 item = btrfs_item_nr(leaf, slot);
2688 btrfs_set_item_size(leaf, item, old_size + data_size);
2689 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002690
2691 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002692 if (btrfs_leaf_free_space(root, leaf) < 0) {
2693 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002694 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002695 }
Chris Mason6567e832007-04-16 09:22:45 -04002696 return ret;
2697}
2698
Chris Mason74123bd2007-02-02 11:05:29 -05002699/*
2700 * Given a key and some data, insert an item into the tree.
2701 * This does all the path init required, making room in the tree if needed.
2702 */
Chris Mason9c583092008-01-29 15:15:18 -05002703int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002704 struct btrfs_root *root,
2705 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002706 struct btrfs_key *cpu_key, u32 *data_size,
2707 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002708{
Chris Mason5f39d392007-10-15 16:14:19 -04002709 struct extent_buffer *leaf;
2710 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002711 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002713 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002714 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002715 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002716 u32 total_size = 0;
2717 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002718 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002719 struct btrfs_disk_key disk_key;
2720
Chris Mason9c583092008-01-29 15:15:18 -05002721 for (i = 0; i < nr; i++) {
2722 total_data += data_size[i];
2723 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002724
Josef Bacik7b128762008-07-24 12:17:14 -04002725 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05002726 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002727 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002728 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002729 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002730 if (ret < 0)
2731 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002732
Chris Mason62e27492007-03-15 12:56:47 -04002733 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002734 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002735
Chris Mason5f39d392007-10-15 16:14:19 -04002736 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002737 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002738
Chris Mason123abc82007-03-14 14:14:43 -04002739 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002740 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002741 btrfs_print_leaf(root, leaf);
2742 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002743 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002744 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002745 }
Chris Mason5f39d392007-10-15 16:14:19 -04002746
Chris Mason62e27492007-03-15 12:56:47 -04002747 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002748 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002749
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750 if (slot != nritems) {
2751 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002752 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002753
Chris Mason5f39d392007-10-15 16:14:19 -04002754 if (old_data < data_end) {
2755 btrfs_print_leaf(root, leaf);
2756 printk("slot %d old_data %d data_end %d\n",
2757 slot, old_data, data_end);
2758 BUG_ON(1);
2759 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002760 /*
2761 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2762 */
2763 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002764 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002765 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002766 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002767
Chris Mason5f39d392007-10-15 16:14:19 -04002768 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002769 if (!leaf->map_token) {
2770 map_extent_buffer(leaf, (unsigned long)item,
2771 sizeof(struct btrfs_item),
2772 &leaf->map_token, &leaf->kaddr,
2773 &leaf->map_start, &leaf->map_len,
2774 KM_USER1);
2775 }
2776
Chris Mason5f39d392007-10-15 16:14:19 -04002777 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002778 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002779 }
Chris Masondb945352007-10-15 16:15:53 -04002780 if (leaf->map_token) {
2781 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2782 leaf->map_token = NULL;
2783 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002784
2785 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002786 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002787 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002788 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002789
2790 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002791 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002792 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002793 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002794 data_end = old_data;
2795 }
Chris Mason5f39d392007-10-15 16:14:19 -04002796
Chris Mason62e27492007-03-15 12:56:47 -04002797 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002798 for (i = 0; i < nr; i++) {
2799 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2800 btrfs_set_item_key(leaf, &disk_key, slot + i);
2801 item = btrfs_item_nr(leaf, slot + i);
2802 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2803 data_end -= data_size[i];
2804 btrfs_set_item_size(leaf, item, data_size[i]);
2805 }
2806 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002808
2809 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002810 if (slot == 0) {
2811 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002812 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002813 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002814
Chris Mason5f39d392007-10-15 16:14:19 -04002815 if (btrfs_leaf_free_space(root, leaf) < 0) {
2816 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002817 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002818 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002819out:
Chris Mason62e27492007-03-15 12:56:47 -04002820 return ret;
2821}
2822
2823/*
2824 * Given a key and some data, insert an item into the tree.
2825 * This does all the path init required, making room in the tree if needed.
2826 */
Chris Masone089f052007-03-16 16:20:31 -04002827int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2828 *root, struct btrfs_key *cpu_key, void *data, u32
2829 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002830{
2831 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002832 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002833 struct extent_buffer *leaf;
2834 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002835
Chris Mason2c90e5d2007-04-02 10:50:19 -04002836 path = btrfs_alloc_path();
2837 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002838 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002839 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002840 leaf = path->nodes[0];
2841 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2842 write_extent_buffer(leaf, data, ptr, data_size);
2843 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002844 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002845 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002846 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002847}
2848
Chris Mason74123bd2007-02-02 11:05:29 -05002849/*
Chris Mason5de08d72007-02-24 06:24:44 -05002850 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002851 *
2852 * If the delete empties a node, the node is removed from the tree,
2853 * continuing all the way the root if required. The root is converted into
2854 * a leaf if all the nodes are emptied.
2855 */
Chris Masone089f052007-03-16 16:20:31 -04002856static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2857 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002858{
Chris Mason5f39d392007-10-15 16:14:19 -04002859 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002860 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002861 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002862 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002863
Chris Mason5f39d392007-10-15 16:14:19 -04002864 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002865 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002866 memmove_extent_buffer(parent,
2867 btrfs_node_key_ptr_offset(slot),
2868 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002869 sizeof(struct btrfs_key_ptr) *
2870 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002871 }
Chris Mason7518a232007-03-12 12:01:18 -04002872 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002874 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002875 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002876 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002877 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002878 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002879 struct btrfs_disk_key disk_key;
2880
2881 btrfs_node_key(parent, &disk_key, 0);
2882 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002883 if (wret)
2884 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002885 }
Chris Masond6025572007-03-30 14:27:56 -04002886 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002887 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002888}
2889
Chris Mason74123bd2007-02-02 11:05:29 -05002890/*
2891 * delete the item at the leaf level in path. If that empties
2892 * the leaf, remove it from the tree
2893 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002894int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2895 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002896{
Chris Mason5f39d392007-10-15 16:14:19 -04002897 struct extent_buffer *leaf;
2898 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002899 int last_off;
2900 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002901 int ret = 0;
2902 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002903 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002904 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002905
Chris Mason5f39d392007-10-15 16:14:19 -04002906 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002907 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2908
2909 for (i = 0; i < nr; i++)
2910 dsize += btrfs_item_size_nr(leaf, slot + i);
2911
Chris Mason5f39d392007-10-15 16:14:19 -04002912 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002913
Chris Mason85e21ba2008-01-29 15:11:36 -05002914 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002915 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002916 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002917
2918 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002919 data_end + dsize,
2920 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002921 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002922
Chris Mason85e21ba2008-01-29 15:11:36 -05002923 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002924 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002925
Chris Mason5f39d392007-10-15 16:14:19 -04002926 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002927 if (!leaf->map_token) {
2928 map_extent_buffer(leaf, (unsigned long)item,
2929 sizeof(struct btrfs_item),
2930 &leaf->map_token, &leaf->kaddr,
2931 &leaf->map_start, &leaf->map_len,
2932 KM_USER1);
2933 }
Chris Mason5f39d392007-10-15 16:14:19 -04002934 ioff = btrfs_item_offset(leaf, item);
2935 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002936 }
Chris Masondb945352007-10-15 16:15:53 -04002937
2938 if (leaf->map_token) {
2939 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2940 leaf->map_token = NULL;
2941 }
2942
Chris Mason5f39d392007-10-15 16:14:19 -04002943 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002944 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002945 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002946 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002947 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002948 btrfs_set_header_nritems(leaf, nritems - nr);
2949 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002950
Chris Mason74123bd2007-02-02 11:05:29 -05002951 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002952 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002953 if (leaf == root->node) {
2954 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002955 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002956 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002957 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002958 if (wret)
2959 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002960 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002961 leaf->start, leaf->len,
2962 btrfs_header_owner(path->nodes[1]),
2963 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002964 if (wret)
2965 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002966 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002967 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002968 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002969 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002970 struct btrfs_disk_key disk_key;
2971
2972 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002973 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002974 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002975 if (wret)
2976 ret = wret;
2977 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002978
Chris Mason74123bd2007-02-02 11:05:29 -05002979 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002980 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002981 /* push_leaf_left fixes the path.
2982 * make sure the path still points to our leaf
2983 * for possible call to del_ptr below
2984 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002985 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002986 extent_buffer_get(leaf);
2987
Chris Mason85e21ba2008-01-29 15:11:36 -05002988 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002989 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002990 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002991
2992 if (path->nodes[0] == leaf &&
2993 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002994 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002995 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002996 ret = wret;
2997 }
Chris Mason5f39d392007-10-15 16:14:19 -04002998
2999 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05003000 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04003001 u64 bytenr = leaf->start;
3002 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04003003
Chris Mason7bb86312007-12-11 09:25:06 -05003004 root_gen = btrfs_header_generation(
3005 path->nodes[1]);
3006
Chris Masone089f052007-03-16 16:20:31 -04003007 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003008 if (wret)
3009 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003010
3011 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04003012 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003013 blocksize,
3014 btrfs_header_owner(path->nodes[1]),
3015 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003016 if (wret)
3017 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05003018 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003019 /* if we're still in the path, make sure
3020 * we're dirty. Otherwise, one of the
3021 * push_leaf functions must have already
3022 * dirtied this buffer
3023 */
3024 if (path->nodes[0] == leaf)
3025 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003026 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003027 }
Chris Masond5719762007-03-23 10:01:08 -04003028 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003029 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003030 }
3031 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003032 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003033}
3034
Chris Mason97571fd2007-02-24 13:39:08 -05003035/*
Chris Mason925baed2008-06-25 16:01:30 -04003036 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003037 * returns 0 if it found something or 1 if there are no lesser leaves.
3038 * returns < 0 on io errors.
3039 */
3040int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3041{
Chris Mason925baed2008-06-25 16:01:30 -04003042 struct btrfs_key key;
3043 struct btrfs_disk_key found_key;
3044 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003045
Chris Mason925baed2008-06-25 16:01:30 -04003046 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003047
Chris Mason925baed2008-06-25 16:01:30 -04003048 if (key.offset > 0)
3049 key.offset--;
3050 else if (key.type > 0)
3051 key.type--;
3052 else if (key.objectid > 0)
3053 key.objectid--;
3054 else
3055 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003056
Chris Mason925baed2008-06-25 16:01:30 -04003057 btrfs_release_path(root, path);
3058 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3059 if (ret < 0)
3060 return ret;
3061 btrfs_item_key(path->nodes[0], &found_key, 0);
3062 ret = comp_keys(&found_key, &key);
3063 if (ret < 0)
3064 return 0;
3065 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003066}
3067
Chris Mason3f157a22008-06-25 16:01:31 -04003068/*
3069 * A helper function to walk down the tree starting at min_key, and looking
3070 * for nodes or leaves that are either in cache or have a minimum
3071 * transaction id. This is used by the btree defrag code, but could
3072 * also be used to search for blocks that have changed since a given
3073 * transaction id.
3074 *
3075 * This does not cow, but it does stuff the starting key it finds back
3076 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3077 * key and get a writable path.
3078 *
3079 * This does lock as it descends, and path->keep_locks should be set
3080 * to 1 by the caller.
3081 *
3082 * This honors path->lowest_level to prevent descent past a given level
3083 * of the tree.
3084 *
3085 * returns zero if something useful was found, < 0 on error and 1 if there
3086 * was nothing in the tree that matched the search criteria.
3087 */
3088int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003089 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003090 struct btrfs_path *path, int cache_only,
3091 u64 min_trans)
3092{
3093 struct extent_buffer *cur;
3094 struct btrfs_key found_key;
3095 int slot;
Yan96524802008-07-24 12:19:49 -04003096 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003097 u32 nritems;
3098 int level;
3099 int ret = 1;
3100
3101again:
3102 cur = btrfs_lock_root_node(root);
3103 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003104 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003105 path->nodes[level] = cur;
3106 path->locks[level] = 1;
3107
3108 if (btrfs_header_generation(cur) < min_trans) {
3109 ret = 1;
3110 goto out;
3111 }
3112 while(1) {
3113 nritems = btrfs_header_nritems(cur);
3114 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003115 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003116
3117 /* at level = 0, we're done, setup the path and exit */
3118 if (level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003119 if (slot >= nritems)
3120 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003121 ret = 0;
3122 path->slots[level] = slot;
3123 btrfs_item_key_to_cpu(cur, &found_key, slot);
3124 goto out;
3125 }
Yan96524802008-07-24 12:19:49 -04003126 if (sret && slot > 0)
3127 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003128 /*
3129 * check this node pointer against the cache_only and
3130 * min_trans parameters. If it isn't in cache or is too
3131 * old, skip to the next one.
3132 */
3133 while(slot < nritems) {
3134 u64 blockptr;
3135 u64 gen;
3136 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003137 struct btrfs_disk_key disk_key;
3138
Chris Mason3f157a22008-06-25 16:01:31 -04003139 blockptr = btrfs_node_blockptr(cur, slot);
3140 gen = btrfs_node_ptr_generation(cur, slot);
3141 if (gen < min_trans) {
3142 slot++;
3143 continue;
3144 }
3145 if (!cache_only)
3146 break;
3147
Chris Masone02119d2008-09-05 16:13:11 -04003148 if (max_key) {
3149 btrfs_node_key(cur, &disk_key, slot);
3150 if (comp_keys(&disk_key, max_key) >= 0) {
3151 ret = 1;
3152 goto out;
3153 }
3154 }
3155
Chris Mason3f157a22008-06-25 16:01:31 -04003156 tmp = btrfs_find_tree_block(root, blockptr,
3157 btrfs_level_size(root, level - 1));
3158
3159 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3160 free_extent_buffer(tmp);
3161 break;
3162 }
3163 if (tmp)
3164 free_extent_buffer(tmp);
3165 slot++;
3166 }
Chris Masone02119d2008-09-05 16:13:11 -04003167find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003168 /*
3169 * we didn't find a candidate key in this node, walk forward
3170 * and find another one
3171 */
3172 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003173 path->slots[level] = slot;
3174 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003175 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003176 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003177 btrfs_release_path(root, path);
3178 goto again;
3179 } else {
3180 goto out;
3181 }
3182 }
3183 /* save our key for returning back */
3184 btrfs_node_key_to_cpu(cur, &found_key, slot);
3185 path->slots[level] = slot;
3186 if (level == path->lowest_level) {
3187 ret = 0;
3188 unlock_up(path, level, 1);
3189 goto out;
3190 }
3191 cur = read_node_slot(root, cur, slot);
3192
3193 btrfs_tree_lock(cur);
3194 path->locks[level - 1] = 1;
3195 path->nodes[level - 1] = cur;
3196 unlock_up(path, level, 1);
3197 }
3198out:
3199 if (ret == 0)
3200 memcpy(min_key, &found_key, sizeof(found_key));
3201 return ret;
3202}
3203
3204/*
3205 * this is similar to btrfs_next_leaf, but does not try to preserve
3206 * and fixup the path. It looks for and returns the next key in the
3207 * tree based on the current path and the cache_only and min_trans
3208 * parameters.
3209 *
3210 * 0 is returned if another key is found, < 0 if there are any errors
3211 * and 1 is returned if there are no higher keys in the tree
3212 *
3213 * path->keep_locks should be set to 1 on the search made before
3214 * calling this function.
3215 */
Chris Masone7a84562008-06-25 16:01:31 -04003216int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003217 struct btrfs_key *key, int lowest_level,
3218 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003219{
3220 int level = lowest_level;
3221 int slot;
3222 struct extent_buffer *c;
3223
3224 while(level < BTRFS_MAX_LEVEL) {
3225 if (!path->nodes[level])
3226 return 1;
3227
3228 slot = path->slots[level] + 1;
3229 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003230next:
Chris Masone7a84562008-06-25 16:01:31 -04003231 if (slot >= btrfs_header_nritems(c)) {
3232 level++;
3233 if (level == BTRFS_MAX_LEVEL) {
3234 return 1;
3235 }
3236 continue;
3237 }
3238 if (level == 0)
3239 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003240 else {
3241 u64 blockptr = btrfs_node_blockptr(c, slot);
3242 u64 gen = btrfs_node_ptr_generation(c, slot);
3243
3244 if (cache_only) {
3245 struct extent_buffer *cur;
3246 cur = btrfs_find_tree_block(root, blockptr,
3247 btrfs_level_size(root, level - 1));
3248 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3249 slot++;
3250 if (cur)
3251 free_extent_buffer(cur);
3252 goto next;
3253 }
3254 free_extent_buffer(cur);
3255 }
3256 if (gen < min_trans) {
3257 slot++;
3258 goto next;
3259 }
Chris Masone7a84562008-06-25 16:01:31 -04003260 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003261 }
Chris Masone7a84562008-06-25 16:01:31 -04003262 return 0;
3263 }
3264 return 1;
3265}
3266
Chris Mason7bb86312007-12-11 09:25:06 -05003267/*
Chris Mason925baed2008-06-25 16:01:30 -04003268 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003269 * returns 0 if it found something or 1 if there are no greater leaves.
3270 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003271 */
Chris Mason234b63a2007-03-13 10:46:10 -04003272int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003273{
3274 int slot;
3275 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003276 struct extent_buffer *c;
3277 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003278 struct btrfs_key key;
3279 u32 nritems;
3280 int ret;
3281
3282 nritems = btrfs_header_nritems(path->nodes[0]);
3283 if (nritems == 0) {
3284 return 1;
3285 }
3286
3287 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3288
Chris Mason925baed2008-06-25 16:01:30 -04003289 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003290 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003291 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3292 path->keep_locks = 0;
3293
3294 if (ret < 0)
3295 return ret;
3296
Chris Masona2135012008-06-25 16:01:30 -04003297 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003298 /*
3299 * by releasing the path above we dropped all our locks. A balance
3300 * could have added more items next to the key that used to be
3301 * at the very end of the block. So, check again here and
3302 * advance the path if there are now more items available.
3303 */
Chris Masona2135012008-06-25 16:01:30 -04003304 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003305 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003306 goto done;
3307 }
Chris Masond97e63b2007-02-20 16:40:44 -05003308
Chris Mason234b63a2007-03-13 10:46:10 -04003309 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003310 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003311 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003312
Chris Masond97e63b2007-02-20 16:40:44 -05003313 slot = path->slots[level] + 1;
3314 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003315 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003316 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003317 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003318 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003319 }
Chris Masond97e63b2007-02-20 16:40:44 -05003320 continue;
3321 }
Chris Mason5f39d392007-10-15 16:14:19 -04003322
Chris Mason925baed2008-06-25 16:01:30 -04003323 if (next) {
3324 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003325 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003326 }
Chris Mason5f39d392007-10-15 16:14:19 -04003327
Chris Mason0bd40a72008-07-17 12:54:43 -04003328 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3329 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003330 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003331
Chris Masonca7a79a2008-05-12 12:59:19 -04003332 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003333 if (!path->skip_locking) {
3334 WARN_ON(!btrfs_tree_locked(c));
3335 btrfs_tree_lock(next);
3336 }
Chris Masond97e63b2007-02-20 16:40:44 -05003337 break;
3338 }
3339 path->slots[level] = slot;
3340 while(1) {
3341 level--;
3342 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003343 if (path->locks[level])
3344 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003345 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003346 path->nodes[level] = next;
3347 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003348 if (!path->skip_locking)
3349 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003350 if (!level)
3351 break;
Chris Mason925baed2008-06-25 16:01:30 -04003352 if (level == 1 && path->locks[1] && path->reada)
3353 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003354 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003355 if (!path->skip_locking) {
3356 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3357 btrfs_tree_lock(next);
3358 }
Chris Masond97e63b2007-02-20 16:40:44 -05003359 }
Chris Mason925baed2008-06-25 16:01:30 -04003360done:
3361 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003362 return 0;
3363}
Chris Mason0b86a832008-03-24 15:01:56 -04003364
Chris Mason3f157a22008-06-25 16:01:31 -04003365/*
3366 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3367 * searching until it gets past min_objectid or finds an item of 'type'
3368 *
3369 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3370 */
Chris Mason0b86a832008-03-24 15:01:56 -04003371int btrfs_previous_item(struct btrfs_root *root,
3372 struct btrfs_path *path, u64 min_objectid,
3373 int type)
3374{
3375 struct btrfs_key found_key;
3376 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003377 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003378 int ret;
3379
3380 while(1) {
3381 if (path->slots[0] == 0) {
3382 ret = btrfs_prev_leaf(root, path);
3383 if (ret != 0)
3384 return ret;
3385 } else {
3386 path->slots[0]--;
3387 }
3388 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003389 nritems = btrfs_header_nritems(leaf);
3390 if (nritems == 0)
3391 return 1;
3392 if (path->slots[0] == nritems)
3393 path->slots[0]--;
3394
Chris Mason0b86a832008-03-24 15:01:56 -04003395 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3396 if (found_key.type == type)
3397 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003398 if (found_key.objectid < min_objectid)
3399 break;
3400 if (found_key.objectid == min_objectid &&
3401 found_key.type < type)
3402 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003403 }
3404 return 1;
3405}