blob: 8ad8b5cc295ac3eb6144789161b95d56dae43aa6 [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 Mason9a8dd152007-02-23 08:38:36 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040028 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040029 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040030static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src);
33static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040037static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050039
Chris Masondf24a2b2007-04-04 09:36:31 -040040inline void btrfs_init_path(struct btrfs_path *p)
41{
42 memset(p, 0, sizeof(*p));
43}
44
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040049 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040050 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040051 path->reada = 1;
52 }
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
56void btrfs_free_path(struct btrfs_path *p)
57{
Chris Masondf24a2b2007-04-04 09:36:31 -040058 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040059 kmem_cache_free(btrfs_path_cachep, p);
60}
61
Chris Mason234b63a2007-03-13 10:46:10 -040062void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050063{
64 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040065 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050066 if (!p->nodes[i])
67 break;
Chris Mason5f39d392007-10-15 16:14:19 -040068 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050069 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050070 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050071}
72
Chris Mason0b86a832008-03-24 15:01:56 -040073static void add_root_to_dirty_list(struct btrfs_root *root)
74{
75 if (root->track_dirty && list_empty(&root->dirty_list)) {
76 list_add(&root->dirty_list,
77 &root->fs_info->dirty_cowonly_roots);
78 }
79}
80
Chris Masonbe20aa92007-12-17 20:14:01 -050081int btrfs_copy_root(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root,
83 struct extent_buffer *buf,
84 struct extent_buffer **cow_ret, u64 new_root_objectid)
85{
86 struct extent_buffer *cow;
87 u32 nritems;
88 int ret = 0;
89 int level;
90 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -050091 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -050092
Chris Mason4aec2b52007-12-18 16:25:45 -050093 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
94 if (!new_root)
95 return -ENOMEM;
96
97 memcpy(new_root, root, sizeof(*new_root));
98 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -050099
100 WARN_ON(root->ref_cows && trans->transid !=
101 root->fs_info->running_transaction->transid);
102 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
103
104 level = btrfs_header_level(buf);
105 nritems = btrfs_header_nritems(buf);
106 if (nritems) {
107 if (level == 0)
108 btrfs_item_key_to_cpu(buf, &first_key, 0);
109 else
110 btrfs_node_key_to_cpu(buf, &first_key, 0);
111 } else {
112 first_key.objectid = 0;
113 }
Chris Mason4aec2b52007-12-18 16:25:45 -0500114 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 new_root_objectid,
116 trans->transid, first_key.objectid,
117 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500118 if (IS_ERR(cow)) {
119 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500120 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500121 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500122
123 copy_extent_buffer(cow, buf, 0, 0, cow->len);
124 btrfs_set_header_bytenr(cow, cow->start);
125 btrfs_set_header_generation(cow, trans->transid);
126 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400127 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500128
129 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500130 ret = btrfs_inc_ref(trans, new_root, buf);
131 kfree(new_root);
132
Chris Masonbe20aa92007-12-17 20:14:01 -0500133 if (ret)
134 return ret;
135
136 btrfs_mark_buffer_dirty(cow);
137 *cow_ret = cow;
138 return 0;
139}
140
141int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400142 struct btrfs_root *root,
143 struct extent_buffer *buf,
144 struct extent_buffer *parent, int parent_slot,
145 struct extent_buffer **cow_ret,
146 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400147{
Chris Mason7bb86312007-12-11 09:25:06 -0500148 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400149 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500150 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400151 int ret = 0;
152 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500153 int level;
154 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400155
Chris Mason7bb86312007-12-11 09:25:06 -0500156 if (root->ref_cows) {
157 root_gen = trans->transid;
158 } else {
159 root_gen = 0;
160 }
Chris Mason7bb86312007-12-11 09:25:06 -0500161 WARN_ON(root->ref_cows && trans->transid !=
162 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400163 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400164
Chris Mason7bb86312007-12-11 09:25:06 -0500165 level = btrfs_header_level(buf);
166 nritems = btrfs_header_nritems(buf);
167 if (nritems) {
168 if (level == 0)
169 btrfs_item_key_to_cpu(buf, &first_key, 0);
170 else
171 btrfs_node_key_to_cpu(buf, &first_key, 0);
172 } else {
173 first_key.objectid = 0;
174 }
175 cow = __btrfs_alloc_free_block(trans, root, buf->len,
176 root->root_key.objectid,
177 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400178 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400179 if (IS_ERR(cow))
180 return PTR_ERR(cow);
181
Chris Mason5f39d392007-10-15 16:14:19 -0400182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400183 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400187
Chris Mason5f39d392007-10-15 16:14:19 -0400188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
189 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400190 different_trans = 1;
191 ret = btrfs_inc_ref(trans, root, buf);
192 if (ret)
193 return ret;
194 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400195 clean_tree_block(trans, root, buf);
196 }
197
198 if (buf == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -0500199 root_gen = btrfs_header_generation(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400200 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400201 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400202 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400203 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500204 buf->len, root->root_key.objectid,
205 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400206 }
Chris Mason5f39d392007-10-15 16:14:19 -0400207 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400208 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400209 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500210 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400211 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400212 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500213 WARN_ON(trans->transid == 0);
214 btrfs_set_node_ptr_generation(parent, parent_slot,
215 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400216 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400217 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500218 btrfs_free_extent(trans, root, buf->start, buf->len,
219 btrfs_header_owner(parent), root_gen,
220 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400221 }
Chris Mason5f39d392007-10-15 16:14:19 -0400222 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400223 btrfs_mark_buffer_dirty(cow);
224 *cow_ret = cow;
225 return 0;
226}
227
Chris Mason5f39d392007-10-15 16:14:19 -0400228int btrfs_cow_block(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root, struct extent_buffer *buf,
230 struct extent_buffer *parent, int parent_slot,
231 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500232{
Chris Mason6702ed42007-08-07 16:15:09 -0400233 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500234 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400235 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500236
Chris Masonccd467d2007-06-28 15:57:36 -0400237 if (trans->transaction != root->fs_info->running_transaction) {
238 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
239 root->fs_info->running_transaction->transid);
240 WARN_ON(1);
241 }
242 if (trans->transid != root->fs_info->generation) {
243 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
244 root->fs_info->generation);
245 WARN_ON(1);
246 }
Chris Masondc17ff82008-01-08 15:46:30 -0500247
248 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400249 spin_lock(&root->fs_info->hash_lock);
250 if (header_trans == trans->transid &&
251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500252 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400253 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500254 return 0;
255 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400256 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400257 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400258 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400259 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400260 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400261}
262
Chris Mason6b800532007-10-15 16:17:34 -0400263static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400264{
Chris Mason6b800532007-10-15 16:17:34 -0400265 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400266 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400267 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400268 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500269 return 0;
270}
271
Chris Mason081e9572007-11-06 10:26:24 -0500272/*
273 * compare two keys in a memcmp fashion
274 */
275static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
276{
277 struct btrfs_key k1;
278
279 btrfs_disk_key_to_cpu(&k1, disk);
280
281 if (k1.objectid > k2->objectid)
282 return 1;
283 if (k1.objectid < k2->objectid)
284 return -1;
285 if (k1.type > k2->type)
286 return 1;
287 if (k1.type < k2->type)
288 return -1;
289 if (k1.offset > k2->offset)
290 return 1;
291 if (k1.offset < k2->offset)
292 return -1;
293 return 0;
294}
295
296
Chris Mason6702ed42007-08-07 16:15:09 -0400297int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400298 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400299 int start_slot, int cache_only, u64 *last_ret,
300 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400301{
Chris Mason6b800532007-10-15 16:17:34 -0400302 struct extent_buffer *cur;
303 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400304 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400305 u64 search_start = *last_ret;
306 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400307 u64 other;
308 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400309 int end_slot;
310 int i;
311 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400312 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400313 int uptodate;
314 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500315 int progress_passed = 0;
316 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400317
Chris Mason5708b952007-10-25 15:43:18 -0400318 parent_level = btrfs_header_level(parent);
319 if (cache_only && parent_level != 1)
320 return 0;
321
Chris Mason6702ed42007-08-07 16:15:09 -0400322 if (trans->transaction != root->fs_info->running_transaction) {
323 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
324 root->fs_info->running_transaction->transid);
325 WARN_ON(1);
326 }
327 if (trans->transid != root->fs_info->generation) {
328 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
329 root->fs_info->generation);
330 WARN_ON(1);
331 }
Chris Mason86479a02007-09-10 19:58:16 -0400332
Chris Mason6b800532007-10-15 16:17:34 -0400333 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400334 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400335 end_slot = parent_nritems;
336
337 if (parent_nritems == 1)
338 return 0;
339
340 for (i = start_slot; i < end_slot; i++) {
341 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400342
Chris Mason5708b952007-10-25 15:43:18 -0400343 if (!parent->map_token) {
344 map_extent_buffer(parent,
345 btrfs_node_key_ptr_offset(i),
346 sizeof(struct btrfs_key_ptr),
347 &parent->map_token, &parent->kaddr,
348 &parent->map_start, &parent->map_len,
349 KM_USER1);
350 }
Chris Mason081e9572007-11-06 10:26:24 -0500351 btrfs_node_key(parent, &disk_key, i);
352 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
353 continue;
354
355 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400356 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400357 if (last_block == 0)
358 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400359
Chris Mason6702ed42007-08-07 16:15:09 -0400360 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400361 other = btrfs_node_blockptr(parent, i - 1);
362 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400363 }
Chris Mason5708b952007-10-25 15:43:18 -0400364 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400365 other = btrfs_node_blockptr(parent, i + 1);
366 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400367 }
Chris Masone9d0b132007-08-10 14:06:19 -0400368 if (close) {
369 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400370 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400371 }
Chris Mason5708b952007-10-25 15:43:18 -0400372 if (parent->map_token) {
373 unmap_extent_buffer(parent, parent->map_token,
374 KM_USER1);
375 parent->map_token = NULL;
376 }
Chris Mason6702ed42007-08-07 16:15:09 -0400377
Chris Mason6b800532007-10-15 16:17:34 -0400378 cur = btrfs_find_tree_block(root, blocknr, blocksize);
379 if (cur)
380 uptodate = btrfs_buffer_uptodate(cur);
381 else
382 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400383 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400384 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400385 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400386 continue;
387 }
Chris Mason6b800532007-10-15 16:17:34 -0400388 if (!cur) {
389 cur = read_tree_block(root, blocknr,
390 blocksize);
391 } else if (!uptodate) {
392 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400393 }
Chris Mason6702ed42007-08-07 16:15:09 -0400394 }
Chris Masone9d0b132007-08-10 14:06:19 -0400395 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400396 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400397
Chris Mason0999df52008-04-01 13:48:14 -0400398 btrfs_verify_block_csum(root, cur);
Chris Mason6b800532007-10-15 16:17:34 -0400399 err = __btrfs_cow_block(trans, root, cur, parent, i,
400 &tmp, search_start,
401 min(16 * blocksize,
402 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400403 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400404 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400405 break;
Yan252c38f2007-08-29 09:11:44 -0400406 }
Chris Mason6b800532007-10-15 16:17:34 -0400407 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400408 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400409 *last_ret = search_start;
410 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400411 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400412 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400413 }
Chris Mason5708b952007-10-25 15:43:18 -0400414 if (parent->map_token) {
415 unmap_extent_buffer(parent, parent->map_token,
416 KM_USER1);
417 parent->map_token = NULL;
418 }
Chris Mason6702ed42007-08-07 16:15:09 -0400419 return err;
420}
421
Chris Mason74123bd2007-02-02 11:05:29 -0500422/*
423 * The leaf data grows from end-to-front in the node.
424 * this returns the address of the start of the last item,
425 * which is the stop of the leaf data stack
426 */
Chris Mason123abc82007-03-14 14:14:43 -0400427static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400428 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500429{
Chris Mason5f39d392007-10-15 16:14:19 -0400430 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500431 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400432 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400433 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500434}
435
Chris Mason123abc82007-03-14 14:14:43 -0400436static int check_node(struct btrfs_root *root, struct btrfs_path *path,
437 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500438{
Chris Mason5f39d392007-10-15 16:14:19 -0400439 struct extent_buffer *parent = NULL;
440 struct extent_buffer *node = path->nodes[level];
441 struct btrfs_disk_key parent_key;
442 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500443 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400444 int slot;
445 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400446 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500447
448 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400449 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400450
Chris Mason8d7be552007-05-10 11:24:42 -0400451 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400452 BUG_ON(nritems == 0);
453 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400454 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400455 btrfs_node_key(parent, &parent_key, parent_slot);
456 btrfs_node_key(node, &node_key, 0);
457 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400458 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400459 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400460 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500461 }
Chris Mason123abc82007-03-14 14:14:43 -0400462 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400463 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400464 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
465 btrfs_node_key(node, &node_key, slot);
466 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400467 }
468 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400469 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
470 btrfs_node_key(node, &node_key, slot);
471 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500472 }
473 return 0;
474}
475
Chris Mason123abc82007-03-14 14:14:43 -0400476static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
477 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500478{
Chris Mason5f39d392007-10-15 16:14:19 -0400479 struct extent_buffer *leaf = path->nodes[level];
480 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500481 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400482 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400483 struct btrfs_disk_key parent_key;
484 struct btrfs_disk_key leaf_key;
485 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400486
Chris Mason5f39d392007-10-15 16:14:19 -0400487 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500488
489 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400490 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400491
492 if (nritems == 0)
493 return 0;
494
495 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400496 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_node_key(parent, &parent_key, parent_slot);
498 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400499
Chris Mason5f39d392007-10-15 16:14:19 -0400500 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400501 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400502 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400503 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500504 }
Chris Mason5f39d392007-10-15 16:14:19 -0400505#if 0
506 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
507 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
508 btrfs_item_key(leaf, &leaf_key, i);
509 if (comp_keys(&leaf_key, &cpukey) >= 0) {
510 btrfs_print_leaf(root, leaf);
511 printk("slot %d offset bad key\n", i);
512 BUG_ON(1);
513 }
514 if (btrfs_item_offset_nr(leaf, i) !=
515 btrfs_item_end_nr(leaf, i + 1)) {
516 btrfs_print_leaf(root, leaf);
517 printk("slot %d offset bad\n", i);
518 BUG_ON(1);
519 }
520 if (i == 0) {
521 if (btrfs_item_offset_nr(leaf, i) +
522 btrfs_item_size_nr(leaf, i) !=
523 BTRFS_LEAF_DATA_SIZE(root)) {
524 btrfs_print_leaf(root, leaf);
525 printk("slot %d first offset bad\n", i);
526 BUG_ON(1);
527 }
528 }
529 }
530 if (nritems > 0) {
531 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
532 btrfs_print_leaf(root, leaf);
533 printk("slot %d bad size \n", nritems - 1);
534 BUG_ON(1);
535 }
536 }
537#endif
538 if (slot != 0 && slot < nritems - 1) {
539 btrfs_item_key(leaf, &leaf_key, slot);
540 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
541 if (comp_keys(&leaf_key, &cpukey) <= 0) {
542 btrfs_print_leaf(root, leaf);
543 printk("slot %d offset bad key\n", slot);
544 BUG_ON(1);
545 }
546 if (btrfs_item_offset_nr(leaf, slot - 1) !=
547 btrfs_item_end_nr(leaf, slot)) {
548 btrfs_print_leaf(root, leaf);
549 printk("slot %d offset bad\n", slot);
550 BUG_ON(1);
551 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500552 }
Chris Mason8d7be552007-05-10 11:24:42 -0400553 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400554 btrfs_item_key(leaf, &leaf_key, slot);
555 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
556 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
557 if (btrfs_item_offset_nr(leaf, slot) !=
558 btrfs_item_end_nr(leaf, slot + 1)) {
559 btrfs_print_leaf(root, leaf);
560 printk("slot %d offset bad\n", slot);
561 BUG_ON(1);
562 }
Chris Mason8d7be552007-05-10 11:24:42 -0400563 }
Chris Mason5f39d392007-10-15 16:14:19 -0400564 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
565 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500566 return 0;
567}
568
Chris Mason98ed5172008-01-03 10:01:48 -0500569static int noinline check_block(struct btrfs_root *root,
570 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500571{
Chris Masonf1885912008-04-09 16:28:12 -0400572 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400573 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400574 if (btrfs_header_level(path->nodes[level]) != level)
575 printk("warning: bad level %Lu wanted %d found %d\n",
576 path->nodes[level]->start, level,
577 btrfs_header_level(path->nodes[level]));
578 found_start = btrfs_header_bytenr(path->nodes[level]);
579 if (found_start != path->nodes[level]->start) {
580 printk("warning: bad bytentr %Lu found %Lu\n",
581 path->nodes[level]->start, found_start);
582 }
Chris Masondb945352007-10-15 16:15:53 -0400583#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400584 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400585
Chris Mason479965d2007-10-15 16:14:27 -0400586 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
587 (unsigned long)btrfs_header_fsid(buf),
588 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400589 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400590 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400591 }
Chris Masondb945352007-10-15 16:15:53 -0400592#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500593 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400594 return check_leaf(root, path, level);
595 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500596}
597
Chris Mason74123bd2007-02-02 11:05:29 -0500598/*
Chris Mason5f39d392007-10-15 16:14:19 -0400599 * search for key in the extent_buffer. The items start at offset p,
600 * and they are item_size apart. There are 'max' items in p.
601 *
Chris Mason74123bd2007-02-02 11:05:29 -0500602 * the slot in the array is returned via slot, and it points to
603 * the place where you would insert key if it is not found in
604 * the array.
605 *
606 * slot may point to max if the key is bigger than all of the keys
607 */
Chris Mason5f39d392007-10-15 16:14:19 -0400608static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
609 int item_size, struct btrfs_key *key,
610 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500611{
612 int low = 0;
613 int high = max;
614 int mid;
615 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400616 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400617 struct btrfs_disk_key unaligned;
618 unsigned long offset;
619 char *map_token = NULL;
620 char *kaddr = NULL;
621 unsigned long map_start = 0;
622 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400623 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500624
625 while(low < high) {
626 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400627 offset = p + mid * item_size;
628
629 if (!map_token || offset < map_start ||
630 (offset + sizeof(struct btrfs_disk_key)) >
631 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400632 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400633 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400634 map_token = NULL;
635 }
636 err = map_extent_buffer(eb, offset,
637 sizeof(struct btrfs_disk_key),
638 &map_token, &kaddr,
639 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400640
Chris Mason479965d2007-10-15 16:14:27 -0400641 if (!err) {
642 tmp = (struct btrfs_disk_key *)(kaddr + offset -
643 map_start);
644 } else {
645 read_extent_buffer(eb, &unaligned,
646 offset, sizeof(unaligned));
647 tmp = &unaligned;
648 }
649
Chris Mason5f39d392007-10-15 16:14:19 -0400650 } else {
651 tmp = (struct btrfs_disk_key *)(kaddr + offset -
652 map_start);
653 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500654 ret = comp_keys(tmp, key);
655
656 if (ret < 0)
657 low = mid + 1;
658 else if (ret > 0)
659 high = mid;
660 else {
661 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400662 if (map_token)
663 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500664 return 0;
665 }
666 }
667 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400668 if (map_token)
669 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500670 return 1;
671}
672
Chris Mason97571fd2007-02-24 13:39:08 -0500673/*
674 * simple bin_search frontend that does the right thing for
675 * leaves vs nodes
676 */
Chris Mason5f39d392007-10-15 16:14:19 -0400677static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
678 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500679{
Chris Mason5f39d392007-10-15 16:14:19 -0400680 if (level == 0) {
681 return generic_bin_search(eb,
682 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400683 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400684 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400685 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500686 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400687 return generic_bin_search(eb,
688 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400689 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400690 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400691 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500692 }
693 return -1;
694}
695
Chris Mason5f39d392007-10-15 16:14:19 -0400696static struct extent_buffer *read_node_slot(struct btrfs_root *root,
697 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500698{
Chris Masonbb803952007-03-01 12:04:21 -0500699 if (slot < 0)
700 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400701 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500702 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400703 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
704 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500705}
706
Chris Mason98ed5172008-01-03 10:01:48 -0500707static int balance_level(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root,
709 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500710{
Chris Mason5f39d392007-10-15 16:14:19 -0400711 struct extent_buffer *right = NULL;
712 struct extent_buffer *mid;
713 struct extent_buffer *left = NULL;
714 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500715 int ret = 0;
716 int wret;
717 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500718 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400719 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500720 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500721
722 if (level == 0)
723 return 0;
724
Chris Mason5f39d392007-10-15 16:14:19 -0400725 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500726 WARN_ON(btrfs_header_generation(mid) != trans->transid);
727
Chris Mason1d4f8a02007-03-13 09:28:32 -0400728 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500729
Chris Mason234b63a2007-03-13 10:46:10 -0400730 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400731 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500732 pslot = path->slots[level + 1];
733
Chris Mason40689472007-03-17 14:29:23 -0400734 /*
735 * deal with the case where there is only one pointer in the root
736 * by promoting the node below to a root
737 */
Chris Mason5f39d392007-10-15 16:14:19 -0400738 if (!parent) {
739 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500740
Chris Mason5f39d392007-10-15 16:14:19 -0400741 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500742 return 0;
743
744 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400745 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500746 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500747 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
748 BUG_ON(ret);
749
Chris Masonbb803952007-03-01 12:04:21 -0500750 root->node = child;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 add_root_to_dirty_list(root);
Chris Masonbb803952007-03-01 12:04:21 -0500752 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400753 clean_tree_block(trans, root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500754 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400755 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500756 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
757 root->root_key.objectid,
758 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500759 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400760 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400761 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500762 }
Chris Mason5f39d392007-10-15 16:14:19 -0400763 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400764 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500765 return 0;
766
Chris Mason5f39d392007-10-15 16:14:19 -0400767 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400768 err_on_enospc = 1;
769
Chris Mason5f39d392007-10-15 16:14:19 -0400770 left = read_node_slot(root, parent, pslot - 1);
771 if (left) {
772 wret = btrfs_cow_block(trans, root, left,
773 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400774 if (wret) {
775 ret = wret;
776 goto enospc;
777 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400778 }
Chris Mason5f39d392007-10-15 16:14:19 -0400779 right = read_node_slot(root, parent, pslot + 1);
780 if (right) {
781 wret = btrfs_cow_block(trans, root, right,
782 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400783 if (wret) {
784 ret = wret;
785 goto enospc;
786 }
787 }
788
789 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400790 if (left) {
791 orig_slot += btrfs_header_nritems(left);
792 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500793 if (wret < 0)
794 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400795 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400796 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500797 }
Chris Mason79f95c82007-03-01 15:16:26 -0500798
799 /*
800 * then try to empty the right most buffer into the middle
801 */
Chris Mason5f39d392007-10-15 16:14:19 -0400802 if (right) {
803 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400804 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500805 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400806 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400807 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500808 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400809 u32 blocksize = right->len;
810
Chris Mason5f39d392007-10-15 16:14:19 -0400811 clean_tree_block(trans, root, right);
Chris Mason5f39d392007-10-15 16:14:19 -0400812 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500813 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400814 wret = del_ptr(trans, root, path, level + 1, pslot +
815 1);
Chris Masonbb803952007-03-01 12:04:21 -0500816 if (wret)
817 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400818 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500819 blocksize,
820 btrfs_header_owner(parent),
821 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500822 if (wret)
823 ret = wret;
824 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400825 struct btrfs_disk_key right_key;
826 btrfs_node_key(right, &right_key, 0);
827 btrfs_set_node_key(parent, &right_key, pslot + 1);
828 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500829 }
830 }
Chris Mason5f39d392007-10-15 16:14:19 -0400831 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500832 /*
833 * we're not allowed to leave a node with one item in the
834 * tree during a delete. A deletion from lower in the tree
835 * could try to delete the only pointer in this node.
836 * So, pull some keys from the left.
837 * There has to be a left pointer at this point because
838 * otherwise we would have pulled some pointers from the
839 * right
840 */
Chris Mason5f39d392007-10-15 16:14:19 -0400841 BUG_ON(!left);
842 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400843 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500844 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 goto enospc;
846 }
Chris Mason79f95c82007-03-01 15:16:26 -0500847 BUG_ON(wret == 1);
848 }
Chris Mason5f39d392007-10-15 16:14:19 -0400849 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500850 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500851 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400852 u64 bytenr = mid->start;
853 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400854 clean_tree_block(trans, root, mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400855 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500856 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400857 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500858 if (wret)
859 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500860 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
861 btrfs_header_owner(parent),
862 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500863 if (wret)
864 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500865 } else {
866 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400867 struct btrfs_disk_key mid_key;
868 btrfs_node_key(mid, &mid_key, 0);
869 btrfs_set_node_key(parent, &mid_key, pslot);
870 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500871 }
Chris Masonbb803952007-03-01 12:04:21 -0500872
Chris Mason79f95c82007-03-01 15:16:26 -0500873 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400874 if (left) {
875 if (btrfs_header_nritems(left) > orig_slot) {
876 extent_buffer_get(left);
877 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500878 path->slots[level + 1] -= 1;
879 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400880 if (mid)
881 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500882 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400883 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500884 path->slots[level] = orig_slot;
885 }
886 }
Chris Mason79f95c82007-03-01 15:16:26 -0500887 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400888 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400889 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400890 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500891 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400892enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400893 if (right)
894 free_extent_buffer(right);
895 if (left)
896 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500897 return ret;
898}
899
Chris Masone66f7092007-04-20 13:16:02 -0400900/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500901static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
902 struct btrfs_root *root,
903 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400904{
Chris Mason5f39d392007-10-15 16:14:19 -0400905 struct extent_buffer *right = NULL;
906 struct extent_buffer *mid;
907 struct extent_buffer *left = NULL;
908 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400909 int ret = 0;
910 int wret;
911 int pslot;
912 int orig_slot = path->slots[level];
913 u64 orig_ptr;
914
915 if (level == 0)
916 return 1;
917
Chris Mason5f39d392007-10-15 16:14:19 -0400918 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500919 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400920 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
921
922 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400923 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400924 pslot = path->slots[level + 1];
925
Chris Mason5f39d392007-10-15 16:14:19 -0400926 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400927 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400928
Chris Mason5f39d392007-10-15 16:14:19 -0400929 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400930
931 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400932 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400933 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400934 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400935 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
936 wret = 1;
937 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400938 ret = btrfs_cow_block(trans, root, left, parent,
939 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400940 if (ret)
941 wret = 1;
942 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400943 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400944 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400945 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400946 }
Chris Masone66f7092007-04-20 13:16:02 -0400947 if (wret < 0)
948 ret = wret;
949 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400950 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400951 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400952 btrfs_node_key(mid, &disk_key, 0);
953 btrfs_set_node_key(parent, &disk_key, pslot);
954 btrfs_mark_buffer_dirty(parent);
955 if (btrfs_header_nritems(left) > orig_slot) {
956 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400957 path->slots[level + 1] -= 1;
958 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400959 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400960 } else {
961 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400962 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400963 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400964 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400965 }
Chris Masone66f7092007-04-20 13:16:02 -0400966 return 0;
967 }
Chris Mason5f39d392007-10-15 16:14:19 -0400968 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400969 }
Chris Mason5f39d392007-10-15 16:14:19 -0400970 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400971
972 /*
973 * then try to empty the right most buffer into the middle
974 */
Chris Mason5f39d392007-10-15 16:14:19 -0400975 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400976 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400977 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400978 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
979 wret = 1;
980 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400981 ret = btrfs_cow_block(trans, root, right,
982 parent, pslot + 1,
983 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 if (ret)
985 wret = 1;
986 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400988 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400989 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400990 }
Chris Masone66f7092007-04-20 13:16:02 -0400991 if (wret < 0)
992 ret = wret;
993 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400994 struct btrfs_disk_key disk_key;
995
996 btrfs_node_key(right, &disk_key, 0);
997 btrfs_set_node_key(parent, &disk_key, pslot + 1);
998 btrfs_mark_buffer_dirty(parent);
999
1000 if (btrfs_header_nritems(mid) <= orig_slot) {
1001 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001002 path->slots[level + 1] += 1;
1003 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001004 btrfs_header_nritems(mid);
1005 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001006 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001007 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001008 }
Chris Masone66f7092007-04-20 13:16:02 -04001009 return 0;
1010 }
Chris Mason5f39d392007-10-15 16:14:19 -04001011 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001012 }
Chris Masone66f7092007-04-20 13:16:02 -04001013 return 1;
1014}
1015
Chris Mason74123bd2007-02-02 11:05:29 -05001016/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001017 * readahead one full node of leaves
1018 */
1019static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001020 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001021{
Chris Mason5f39d392007-10-15 16:14:19 -04001022 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001023 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001024 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001025 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001026 u64 lowest_read;
1027 u64 highest_read;
1028 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001029 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001030 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001031 u32 nr;
1032 u32 blocksize;
1033 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001034
Chris Masona6b6e752007-10-15 16:22:39 -04001035 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001036 return;
1037
Chris Mason6702ed42007-08-07 16:15:09 -04001038 if (!path->nodes[level])
1039 return;
1040
Chris Mason5f39d392007-10-15 16:14:19 -04001041 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001042 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001043 blocksize = btrfs_level_size(root, level - 1);
1044 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001045 if (eb) {
1046 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001047 return;
1048 }
1049
Chris Mason6b800532007-10-15 16:17:34 -04001050 highest_read = search;
1051 lowest_read = search;
1052
Chris Mason5f39d392007-10-15 16:14:19 -04001053 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001054 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001055 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001056 if (direction < 0) {
1057 if (nr == 0)
1058 break;
1059 nr--;
1060 } else if (direction > 0) {
1061 nr++;
1062 if (nr >= nritems)
1063 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001064 }
Chris Mason01f46652007-12-21 16:24:26 -05001065 if (path->reada < 0 && objectid) {
1066 btrfs_node_key(node, &disk_key, nr);
1067 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1068 break;
1069 }
Chris Mason6b800532007-10-15 16:17:34 -04001070 search = btrfs_node_blockptr(node, nr);
1071 if ((search >= lowest_read && search <= highest_read) ||
1072 (search < lowest_read && lowest_read - search <= 32768) ||
1073 (search > highest_read && search - highest_read <= 32768)) {
1074 readahead_tree_block(root, search, blocksize);
1075 nread += blocksize;
1076 }
1077 nscan++;
1078 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1079 break;
1080 if(nread > (1024 * 1024) || nscan > 128)
1081 break;
1082
1083 if (search < lowest_read)
1084 lowest_read = search;
1085 if (search > highest_read)
1086 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001087 }
1088}
1089/*
Chris Mason74123bd2007-02-02 11:05:29 -05001090 * look for key in the tree. path is filled in with nodes along the way
1091 * if key is found, we return zero and you can find the item in the leaf
1092 * level of the path (level 0)
1093 *
1094 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001095 * be inserted, and 1 is returned. If there are other errors during the
1096 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001097 *
1098 * if ins_len > 0, nodes and leaves will be split as we walk down the
1099 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1100 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001101 */
Chris Masone089f052007-03-16 16:20:31 -04001102int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1103 *root, struct btrfs_key *key, struct btrfs_path *p, int
1104 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001105{
Chris Mason5f39d392007-10-15 16:14:19 -04001106 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001107 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001108 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001109 int slot;
1110 int ret;
1111 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001112 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001113 u8 lowest_level = 0;
1114
Chris Mason6702ed42007-08-07 16:15:09 -04001115 lowest_level = p->lowest_level;
1116 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001117 WARN_ON(p->nodes[0] != NULL);
1118 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001119again:
1120 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001121 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001122 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001123 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001124 if (cow) {
1125 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001126 wret = btrfs_cow_block(trans, root, b,
1127 p->nodes[level + 1],
1128 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001129 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001131 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001132 return wret;
1133 }
Chris Mason02217ed2007-03-02 16:08:05 -05001134 }
1135 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001136 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001137 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001138 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001139 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001140 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001141 if (ret)
1142 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001143 ret = bin_search(b, key, level, &slot);
1144 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001145 if (ret && slot > 0)
1146 slot -= 1;
1147 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001148 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001149 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001150 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001151 BUG_ON(sret > 0);
1152 if (sret)
1153 return sret;
1154 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001155 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001156 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001157 int sret = balance_level(trans, root, p,
1158 level);
Chris Masonbb803952007-03-01 12:04:21 -05001159 if (sret)
1160 return sret;
1161 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001162 if (!b) {
1163 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001164 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001165 }
Chris Masonbb803952007-03-01 12:04:21 -05001166 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001167 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001168 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001169 /* this is only true while dropping a snapshot */
1170 if (level == lowest_level)
1171 break;
Chris Masondb945352007-10-15 16:15:53 -04001172 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001173 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001174 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001175 reada_for_search(root, p, level, slot,
1176 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001177 b = read_tree_block(root, bytenr,
1178 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001179 if (ptr_gen != btrfs_header_generation(b)) {
1180 printk("block %llu bad gen wanted %llu "
1181 "found %llu\n",
1182 (unsigned long long)b->start,
1183 (unsigned long long)ptr_gen,
1184 (unsigned long long)btrfs_header_generation(b));
1185 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001186 } else {
1187 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001189 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001190 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001191 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001192 BUG_ON(sret > 0);
1193 if (sret)
1194 return sret;
1195 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001196 return ret;
1197 }
1198 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001199 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001200}
1201
Chris Mason74123bd2007-02-02 11:05:29 -05001202/*
1203 * adjust the pointers going up the tree, starting at level
1204 * making sure the right key of each node is points to 'key'.
1205 * This is used after shifting pointers to the left, so it stops
1206 * fixing up pointers when a given leaf/node is not in slot 0 of the
1207 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001208 *
1209 * If this fails to write a tree block, it returns -1, but continues
1210 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001211 */
Chris Mason5f39d392007-10-15 16:14:19 -04001212static int fixup_low_keys(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root, struct btrfs_path *path,
1214 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001215{
1216 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001217 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001218 struct extent_buffer *t;
1219
Chris Mason234b63a2007-03-13 10:46:10 -04001220 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001221 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001222 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001223 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001224 t = path->nodes[i];
1225 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001226 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001227 if (tslot != 0)
1228 break;
1229 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001230 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001231}
1232
Chris Mason74123bd2007-02-02 11:05:29 -05001233/*
1234 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001235 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001236 *
1237 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1238 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001239 */
Chris Mason98ed5172008-01-03 10:01:48 -05001240static int push_node_left(struct btrfs_trans_handle *trans,
1241 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason5f39d392007-10-15 16:14:19 -04001242 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001243{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001244 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001245 int src_nritems;
1246 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001247 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001248
Chris Mason5f39d392007-10-15 16:14:19 -04001249 src_nritems = btrfs_header_nritems(src);
1250 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001251 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001252 WARN_ON(btrfs_header_generation(src) != trans->transid);
1253 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001254
Chris Masoneb60cea2007-02-02 09:18:22 -05001255 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001257 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001258
1259 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001260 push_items = src_nritems;
1261
Chris Mason5f39d392007-10-15 16:14:19 -04001262 copy_extent_buffer(dst, src,
1263 btrfs_node_key_ptr_offset(dst_nritems),
1264 btrfs_node_key_ptr_offset(0),
1265 push_items * sizeof(struct btrfs_key_ptr));
1266
Chris Masonbb803952007-03-01 12:04:21 -05001267 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001268 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1269 btrfs_node_key_ptr_offset(push_items),
1270 (src_nritems - push_items) *
1271 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001272 }
Chris Mason5f39d392007-10-15 16:14:19 -04001273 btrfs_set_header_nritems(src, src_nritems - push_items);
1274 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1275 btrfs_mark_buffer_dirty(src);
1276 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001277 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001278}
1279
Chris Mason97571fd2007-02-24 13:39:08 -05001280/*
Chris Mason79f95c82007-03-01 15:16:26 -05001281 * try to push data from one node into the next node right in the
1282 * tree.
1283 *
1284 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1285 * error, and > 0 if there was no room in the right hand block.
1286 *
1287 * this will only push up to 1/2 the contents of the left node over
1288 */
Chris Mason5f39d392007-10-15 16:14:19 -04001289static int balance_node_right(struct btrfs_trans_handle *trans,
1290 struct btrfs_root *root,
1291 struct extent_buffer *dst,
1292 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001293{
Chris Mason79f95c82007-03-01 15:16:26 -05001294 int push_items = 0;
1295 int max_push;
1296 int src_nritems;
1297 int dst_nritems;
1298 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001299
Chris Mason7bb86312007-12-11 09:25:06 -05001300 WARN_ON(btrfs_header_generation(src) != trans->transid);
1301 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1302
Chris Mason5f39d392007-10-15 16:14:19 -04001303 src_nritems = btrfs_header_nritems(src);
1304 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001305 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001306 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001307 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001308
1309 max_push = src_nritems / 2 + 1;
1310 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001311 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001312 return 1;
Yan252c38f2007-08-29 09:11:44 -04001313
Chris Mason79f95c82007-03-01 15:16:26 -05001314 if (max_push < push_items)
1315 push_items = max_push;
1316
Chris Mason5f39d392007-10-15 16:14:19 -04001317 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1318 btrfs_node_key_ptr_offset(0),
1319 (dst_nritems) *
1320 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001321
Chris Mason5f39d392007-10-15 16:14:19 -04001322 copy_extent_buffer(dst, src,
1323 btrfs_node_key_ptr_offset(0),
1324 btrfs_node_key_ptr_offset(src_nritems - push_items),
1325 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001326
Chris Mason5f39d392007-10-15 16:14:19 -04001327 btrfs_set_header_nritems(src, src_nritems - push_items);
1328 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001329
Chris Mason5f39d392007-10-15 16:14:19 -04001330 btrfs_mark_buffer_dirty(src);
1331 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001332 return ret;
1333}
1334
1335/*
Chris Mason97571fd2007-02-24 13:39:08 -05001336 * helper function to insert a new root level in the tree.
1337 * A new node is allocated, and a single item is inserted to
1338 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001339 *
1340 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001341 */
Chris Mason98ed5172008-01-03 10:01:48 -05001342static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001343 struct btrfs_root *root,
1344 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001345{
Chris Mason7bb86312007-12-11 09:25:06 -05001346 u64 root_gen;
1347 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 struct extent_buffer *lower;
1349 struct extent_buffer *c;
1350 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001351
1352 BUG_ON(path->nodes[level]);
1353 BUG_ON(path->nodes[level-1] != root->node);
1354
Chris Mason7bb86312007-12-11 09:25:06 -05001355 if (root->ref_cows)
1356 root_gen = trans->transid;
1357 else
1358 root_gen = 0;
1359
1360 lower = path->nodes[level-1];
1361 if (level == 1)
1362 btrfs_item_key(lower, &lower_key, 0);
1363 else
1364 btrfs_node_key(lower, &lower_key, 0);
1365
1366 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1367 root->root_key.objectid,
1368 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001369 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001370 if (IS_ERR(c))
1371 return PTR_ERR(c);
1372 memset_extent_buffer(c, 0, 0, root->nodesize);
1373 btrfs_set_header_nritems(c, 1);
1374 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001375 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001376 btrfs_set_header_generation(c, trans->transid);
1377 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001378
Chris Mason5f39d392007-10-15 16:14:19 -04001379 write_extent_buffer(c, root->fs_info->fsid,
1380 (unsigned long)btrfs_header_fsid(c),
1381 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001382
1383 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1384 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1385 BTRFS_UUID_SIZE);
1386
Chris Mason5f39d392007-10-15 16:14:19 -04001387 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001388 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001389 lower_gen = btrfs_header_generation(lower);
1390 WARN_ON(lower_gen == 0);
1391
1392 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001393
1394 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001395
Chris Mason5c680ed2007-02-22 11:39:13 -05001396 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001397 free_extent_buffer(root->node);
1398 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001399 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001400 extent_buffer_get(c);
1401 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001402 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001403
1404 if (root->ref_cows && lower_gen != trans->transid) {
1405 struct btrfs_path *back_path = btrfs_alloc_path();
1406 int ret;
1407 ret = btrfs_insert_extent_backref(trans,
1408 root->fs_info->extent_root,
1409 path, lower->start,
1410 root->root_key.objectid,
1411 trans->transid, 0, 0);
1412 BUG_ON(ret);
1413 btrfs_free_path(back_path);
1414 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001415 return 0;
1416}
1417
Chris Mason74123bd2007-02-02 11:05:29 -05001418/*
1419 * worker function to insert a single pointer in a node.
1420 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001421 *
Chris Mason74123bd2007-02-02 11:05:29 -05001422 * slot and level indicate where you want the key to go, and
1423 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001424 *
1425 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001426 */
Chris Masone089f052007-03-16 16:20:31 -04001427static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1428 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001429 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001430{
Chris Mason5f39d392007-10-15 16:14:19 -04001431 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001432 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001433
1434 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001435 lower = path->nodes[level];
1436 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001437 if (slot > nritems)
1438 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001439 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001440 BUG();
1441 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001442 memmove_extent_buffer(lower,
1443 btrfs_node_key_ptr_offset(slot + 1),
1444 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001445 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001446 }
Chris Mason5f39d392007-10-15 16:14:19 -04001447 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001448 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001449 WARN_ON(trans->transid == 0);
1450 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001451 btrfs_set_header_nritems(lower, nritems + 1);
1452 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001453 return 0;
1454}
1455
Chris Mason97571fd2007-02-24 13:39:08 -05001456/*
1457 * split the node at the specified level in path in two.
1458 * The path is corrected to point to the appropriate node after the split
1459 *
1460 * Before splitting this tries to make some room in the node by pushing
1461 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001462 *
1463 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001464 */
Chris Masone089f052007-03-16 16:20:31 -04001465static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1466 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001467{
Chris Mason7bb86312007-12-11 09:25:06 -05001468 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001469 struct extent_buffer *c;
1470 struct extent_buffer *split;
1471 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001472 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001473 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001474 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001475 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001476
Chris Mason5f39d392007-10-15 16:14:19 -04001477 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001478 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001479 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001480 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001481 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001482 if (ret)
1483 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001484 } else {
1485 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001486 c = path->nodes[level];
1487 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001488 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1489 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001490 if (ret < 0)
1491 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001492 }
Chris Masone66f7092007-04-20 13:16:02 -04001493
Chris Mason5f39d392007-10-15 16:14:19 -04001494 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001495 if (root->ref_cows)
1496 root_gen = trans->transid;
1497 else
1498 root_gen = 0;
1499
1500 btrfs_node_key(c, &disk_key, 0);
1501 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1502 root->root_key.objectid,
1503 root_gen,
1504 btrfs_disk_key_objectid(&disk_key),
1505 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001506 if (IS_ERR(split))
1507 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001508
Chris Mason5f39d392007-10-15 16:14:19 -04001509 btrfs_set_header_flags(split, btrfs_header_flags(c));
1510 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001511 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001512 btrfs_set_header_generation(split, trans->transid);
1513 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001514 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001515 write_extent_buffer(split, root->fs_info->fsid,
1516 (unsigned long)btrfs_header_fsid(split),
1517 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001518 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1519 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1520 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001521
Chris Mason7518a232007-03-12 12:01:18 -04001522 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001523
1524 copy_extent_buffer(split, c,
1525 btrfs_node_key_ptr_offset(0),
1526 btrfs_node_key_ptr_offset(mid),
1527 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1528 btrfs_set_header_nritems(split, c_nritems - mid);
1529 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001530 ret = 0;
1531
Chris Mason5f39d392007-10-15 16:14:19 -04001532 btrfs_mark_buffer_dirty(c);
1533 btrfs_mark_buffer_dirty(split);
1534
1535 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001536 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001537 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001538 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001539 if (wret)
1540 ret = wret;
1541
Chris Mason5de08d72007-02-24 06:24:44 -05001542 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001543 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001544 free_extent_buffer(c);
1545 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001546 path->slots[level + 1] += 1;
1547 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001548 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001549 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001550 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001551}
1552
Chris Mason74123bd2007-02-02 11:05:29 -05001553/*
1554 * how many bytes are required to store the items in a leaf. start
1555 * and nr indicate which items in the leaf to check. This totals up the
1556 * space used both by the item structs and the item data
1557 */
Chris Mason5f39d392007-10-15 16:14:19 -04001558static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001559{
1560 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001561 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001562 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563
1564 if (!nr)
1565 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001566 data_len = btrfs_item_end_nr(l, start);
1567 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001568 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001569 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001570 return data_len;
1571}
1572
Chris Mason74123bd2007-02-02 11:05:29 -05001573/*
Chris Masond4dbff92007-04-04 14:08:15 -04001574 * The space between the end of the leaf items and
1575 * the start of the leaf data. IOW, how much room
1576 * the leaf has left for both items and data
1577 */
Chris Mason5f39d392007-10-15 16:14:19 -04001578int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001579{
Chris Mason5f39d392007-10-15 16:14:19 -04001580 int nritems = btrfs_header_nritems(leaf);
1581 int ret;
1582 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1583 if (ret < 0) {
1584 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001585 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001586 leaf_space_used(leaf, 0, nritems), nritems);
1587 }
1588 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001589}
1590
1591/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001592 * push some data in the path leaf to the right, trying to free up at
1593 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001594 *
1595 * returns 1 if the push failed because the other node didn't have enough
1596 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001597 */
Chris Masone089f052007-03-16 16:20:31 -04001598static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001599 *root, struct btrfs_path *path, int data_size,
1600 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001601{
Chris Mason5f39d392007-10-15 16:14:19 -04001602 struct extent_buffer *left = path->nodes[0];
1603 struct extent_buffer *right;
1604 struct extent_buffer *upper;
1605 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001606 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001607 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001608 int free_space;
1609 int push_space = 0;
1610 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001611 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001612 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001613 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001614 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001615 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001616 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001617 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001618
1619 slot = path->slots[1];
1620 if (!path->nodes[1]) {
1621 return 1;
1622 }
1623 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001624 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001625 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001626
Chris Masondb945352007-10-15 16:15:53 -04001627 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1628 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001629 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001630 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001631 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001632 return 1;
1633 }
1634
Chris Mason5f39d392007-10-15 16:14:19 -04001635 /* cow and double check */
1636 ret = btrfs_cow_block(trans, root, right, upper,
1637 slot + 1, &right);
1638 if (ret) {
1639 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001640 return 1;
1641 }
Chris Mason5f39d392007-10-15 16:14:19 -04001642 free_space = btrfs_leaf_free_space(root, right);
1643 if (free_space < data_size + sizeof(struct btrfs_item)) {
1644 free_extent_buffer(right);
1645 return 1;
1646 }
1647
1648 left_nritems = btrfs_header_nritems(left);
1649 if (left_nritems == 0) {
1650 free_extent_buffer(right);
1651 return 1;
1652 }
1653
Chris Mason34a38212007-11-07 13:31:03 -05001654 if (empty)
1655 nr = 0;
1656 else
1657 nr = 1;
1658
1659 i = left_nritems - 1;
1660 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001661 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001662
Chris Mason00ec4c52007-02-24 12:47:20 -05001663 if (path->slots[0] == i)
1664 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001665
1666 if (!left->map_token) {
1667 map_extent_buffer(left, (unsigned long)item,
1668 sizeof(struct btrfs_item),
1669 &left->map_token, &left->kaddr,
1670 &left->map_start, &left->map_len,
1671 KM_USER1);
1672 }
1673
1674 this_item_size = btrfs_item_size(left, item);
1675 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001676 break;
1677 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001678 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001679 if (i == 0)
1680 break;
1681 i--;
Chris Masondb945352007-10-15 16:15:53 -04001682 }
1683 if (left->map_token) {
1684 unmap_extent_buffer(left, left->map_token, KM_USER1);
1685 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001686 }
Chris Mason5f39d392007-10-15 16:14:19 -04001687
Chris Mason00ec4c52007-02-24 12:47:20 -05001688 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001689 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001690 return 1;
1691 }
Chris Mason5f39d392007-10-15 16:14:19 -04001692
Chris Mason34a38212007-11-07 13:31:03 -05001693 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001694 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001695
Chris Mason00ec4c52007-02-24 12:47:20 -05001696 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001697 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001698
Chris Mason5f39d392007-10-15 16:14:19 -04001699 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001700 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001701
Chris Mason00ec4c52007-02-24 12:47:20 -05001702 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001703 data_end = leaf_data_end(root, right);
1704 memmove_extent_buffer(right,
1705 btrfs_leaf_data(right) + data_end - push_space,
1706 btrfs_leaf_data(right) + data_end,
1707 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1708
Chris Mason00ec4c52007-02-24 12:47:20 -05001709 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001710 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001711 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1712 btrfs_leaf_data(left) + leaf_data_end(root, left),
1713 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001714
1715 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1716 btrfs_item_nr_offset(0),
1717 right_nritems * sizeof(struct btrfs_item));
1718
Chris Mason00ec4c52007-02-24 12:47:20 -05001719 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001720 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1721 btrfs_item_nr_offset(left_nritems - push_items),
1722 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001723
1724 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001725 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001726 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001727 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001728 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001729 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001730 if (!right->map_token) {
1731 map_extent_buffer(right, (unsigned long)item,
1732 sizeof(struct btrfs_item),
1733 &right->map_token, &right->kaddr,
1734 &right->map_start, &right->map_len,
1735 KM_USER1);
1736 }
1737 push_space -= btrfs_item_size(right, item);
1738 btrfs_set_item_offset(right, item, push_space);
1739 }
1740
1741 if (right->map_token) {
1742 unmap_extent_buffer(right, right->map_token, KM_USER1);
1743 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001744 }
Chris Mason7518a232007-03-12 12:01:18 -04001745 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001746 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001747
Chris Mason34a38212007-11-07 13:31:03 -05001748 if (left_nritems)
1749 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001750 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001751
Chris Mason5f39d392007-10-15 16:14:19 -04001752 btrfs_item_key(right, &disk_key, 0);
1753 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001754 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001755
Chris Mason00ec4c52007-02-24 12:47:20 -05001756 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001757 if (path->slots[0] >= left_nritems) {
1758 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001759 free_extent_buffer(path->nodes[0]);
1760 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001761 path->slots[1] += 1;
1762 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001763 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001764 }
1765 return 0;
1766}
1767/*
Chris Mason74123bd2007-02-02 11:05:29 -05001768 * push some data in the path leaf to the left, trying to free up at
1769 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1770 */
Chris Masone089f052007-03-16 16:20:31 -04001771static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001772 *root, struct btrfs_path *path, int data_size,
1773 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001774{
Chris Mason5f39d392007-10-15 16:14:19 -04001775 struct btrfs_disk_key disk_key;
1776 struct extent_buffer *right = path->nodes[0];
1777 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001778 int slot;
1779 int i;
1780 int free_space;
1781 int push_space = 0;
1782 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001783 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001784 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001785 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001786 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001787 int ret = 0;
1788 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001789 u32 this_item_size;
1790 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791
1792 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001793 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001794 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001795 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001797
Chris Mason3685f792007-10-19 09:23:27 -04001798 right_nritems = btrfs_header_nritems(right);
1799 if (right_nritems == 0) {
1800 return 1;
1801 }
1802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001804 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001805 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001806 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001807 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001808 return 1;
1809 }
Chris Mason02217ed2007-03-02 16:08:05 -05001810
1811 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001812 ret = btrfs_cow_block(trans, root, left,
1813 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001814 if (ret) {
1815 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001816 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001817 return 1;
1818 }
Chris Mason3685f792007-10-19 09:23:27 -04001819
Chris Mason123abc82007-03-14 14:14:43 -04001820 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001821 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001822 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001823 return 1;
1824 }
1825
Chris Mason34a38212007-11-07 13:31:03 -05001826 if (empty)
1827 nr = right_nritems;
1828 else
1829 nr = right_nritems - 1;
1830
1831 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001832 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001833 if (!right->map_token) {
1834 map_extent_buffer(right, (unsigned long)item,
1835 sizeof(struct btrfs_item),
1836 &right->map_token, &right->kaddr,
1837 &right->map_start, &right->map_len,
1838 KM_USER1);
1839 }
1840
Chris Masonbe0e5c02007-01-26 15:51:26 -05001841 if (path->slots[0] == i)
1842 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001843
1844 this_item_size = btrfs_item_size(right, item);
1845 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001846 break;
Chris Masondb945352007-10-15 16:15:53 -04001847
Chris Masonbe0e5c02007-01-26 15:51:26 -05001848 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001849 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850 }
Chris Masondb945352007-10-15 16:15:53 -04001851
1852 if (right->map_token) {
1853 unmap_extent_buffer(right, right->map_token, KM_USER1);
1854 right->map_token = NULL;
1855 }
1856
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001858 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 return 1;
1860 }
Chris Mason34a38212007-11-07 13:31:03 -05001861 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001862 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001863
Chris Masonbe0e5c02007-01-26 15:51:26 -05001864 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001865 copy_extent_buffer(left, right,
1866 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1867 btrfs_item_nr_offset(0),
1868 push_items * sizeof(struct btrfs_item));
1869
Chris Mason123abc82007-03-14 14:14:43 -04001870 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001871 btrfs_item_offset_nr(right, push_items -1);
1872
1873 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001874 leaf_data_end(root, left) - push_space,
1875 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001876 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001877 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001878 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001879 BUG_ON(old_left_nritems < 0);
1880
Chris Masondb945352007-10-15 16:15:53 -04001881 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001882 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001883 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001884
Chris Mason5f39d392007-10-15 16:14:19 -04001885 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001886 if (!left->map_token) {
1887 map_extent_buffer(left, (unsigned long)item,
1888 sizeof(struct btrfs_item),
1889 &left->map_token, &left->kaddr,
1890 &left->map_start, &left->map_len,
1891 KM_USER1);
1892 }
1893
Chris Mason5f39d392007-10-15 16:14:19 -04001894 ioff = btrfs_item_offset(left, item);
1895 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001896 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 }
Chris Mason5f39d392007-10-15 16:14:19 -04001898 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001899 if (left->map_token) {
1900 unmap_extent_buffer(left, left->map_token, KM_USER1);
1901 left->map_token = NULL;
1902 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001903
1904 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001905 if (push_items > right_nritems) {
1906 printk("push items %d nr %u\n", push_items, right_nritems);
1907 WARN_ON(1);
1908 }
Chris Mason5f39d392007-10-15 16:14:19 -04001909
Chris Mason34a38212007-11-07 13:31:03 -05001910 if (push_items < right_nritems) {
1911 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1912 leaf_data_end(root, right);
1913 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1914 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1915 btrfs_leaf_data(right) +
1916 leaf_data_end(root, right), push_space);
1917
1918 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001919 btrfs_item_nr_offset(push_items),
1920 (btrfs_header_nritems(right) - push_items) *
1921 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001922 }
Yaneef1c492007-11-26 10:58:13 -05001923 right_nritems -= push_items;
1924 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001925 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001926 for (i = 0; i < right_nritems; i++) {
1927 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001928
1929 if (!right->map_token) {
1930 map_extent_buffer(right, (unsigned long)item,
1931 sizeof(struct btrfs_item),
1932 &right->map_token, &right->kaddr,
1933 &right->map_start, &right->map_len,
1934 KM_USER1);
1935 }
1936
1937 push_space = push_space - btrfs_item_size(right, item);
1938 btrfs_set_item_offset(right, item, push_space);
1939 }
1940 if (right->map_token) {
1941 unmap_extent_buffer(right, right->map_token, KM_USER1);
1942 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001943 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001946 if (right_nritems)
1947 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001948
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_item_key(right, &disk_key, 0);
1950 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001951 if (wret)
1952 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953
1954 /* then fixup the leaf pointer in the path */
1955 if (path->slots[0] < push_items) {
1956 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001957 free_extent_buffer(path->nodes[0]);
1958 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001959 path->slots[1] -= 1;
1960 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001961 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962 path->slots[0] -= push_items;
1963 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001964 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001965 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001966}
1967
Chris Mason74123bd2007-02-02 11:05:29 -05001968/*
1969 * split the path's leaf in two, making sure there is at least data_size
1970 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001971 *
1972 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001973 */
Chris Masone089f052007-03-16 16:20:31 -04001974static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001975 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001976 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001977{
Chris Mason7bb86312007-12-11 09:25:06 -05001978 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001979 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001980 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001981 int mid;
1982 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001983 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001984 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001985 int data_copy_size;
1986 int rt_data_off;
1987 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001988 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001989 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001990 int double_split;
1991 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001992 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001993
Chris Masoncc0c5532007-10-25 15:42:57 -04001994 if (extend)
1995 space_needed = data_size;
1996
Chris Mason7bb86312007-12-11 09:25:06 -05001997 if (root->ref_cows)
1998 root_gen = trans->transid;
1999 else
2000 root_gen = 0;
2001
Chris Mason40689472007-03-17 14:29:23 -04002002 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002003 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002004 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002005 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002006 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002007 }
2008 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002009 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002010 if (wret < 0)
2011 return wret;
2012 }
2013 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002014
Chris Mason3685f792007-10-19 09:23:27 -04002015 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002016 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002017 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002018 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002019
Chris Mason5c680ed2007-02-22 11:39:13 -05002020 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002021 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002022 if (ret)
2023 return ret;
2024 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002025again:
2026 double_split = 0;
2027 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002028 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002029 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002030 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002031
Chris Mason7bb86312007-12-11 09:25:06 -05002032 btrfs_item_key(l, &disk_key, 0);
2033
2034 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2035 root->root_key.objectid,
2036 root_gen, disk_key.objectid, 0,
2037 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002038 if (IS_ERR(right)) {
2039 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002040 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002041 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002044 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002045 btrfs_set_header_generation(right, trans->transid);
2046 btrfs_set_header_owner(right, root->root_key.objectid);
2047 btrfs_set_header_level(right, 0);
2048 write_extent_buffer(right, root->fs_info->fsid,
2049 (unsigned long)btrfs_header_fsid(right),
2050 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002051
2052 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2053 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2054 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002055 if (mid <= slot) {
2056 if (nritems == 1 ||
2057 leaf_space_used(l, mid, nritems - mid) + space_needed >
2058 BTRFS_LEAF_DATA_SIZE(root)) {
2059 if (slot >= nritems) {
2060 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002062 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002063 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002064 path->slots[1] + 1, 1);
2065 if (wret)
2066 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002067 free_extent_buffer(path->nodes[0]);
2068 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002069 path->slots[0] = 0;
2070 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002071 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002072 return ret;
2073 }
2074 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002075 if (mid != nritems &&
2076 leaf_space_used(l, mid, nritems - mid) +
2077 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2078 double_split = 1;
2079 }
Chris Masond4dbff92007-04-04 14:08:15 -04002080 }
2081 } else {
2082 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2083 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002084 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002085 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002087 wret = insert_ptr(trans, root, path,
2088 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002089 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002090 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002091 if (wret)
2092 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002093 free_extent_buffer(path->nodes[0]);
2094 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002095 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002096 if (path->slots[1] == 0) {
2097 wret = fixup_low_keys(trans, root,
2098 path, &disk_key, 1);
2099 if (wret)
2100 ret = wret;
2101 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002102 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002103 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002104 } else if (extend && slot == 0) {
2105 mid = 1;
2106 } else {
2107 mid = slot;
2108 if (mid != nritems &&
2109 leaf_space_used(l, mid, nritems - mid) +
2110 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2111 double_split = 1;
2112 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002113 }
Chris Masond4dbff92007-04-04 14:08:15 -04002114 }
2115 }
Chris Mason5f39d392007-10-15 16:14:19 -04002116 nritems = nritems - mid;
2117 btrfs_set_header_nritems(right, nritems);
2118 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2119
2120 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2121 btrfs_item_nr_offset(mid),
2122 nritems * sizeof(struct btrfs_item));
2123
2124 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002125 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2126 data_copy_size, btrfs_leaf_data(l) +
2127 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002128
Chris Mason5f39d392007-10-15 16:14:19 -04002129 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2130 btrfs_item_end_nr(l, mid);
2131
2132 for (i = 0; i < nritems; i++) {
2133 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002134 u32 ioff;
2135
2136 if (!right->map_token) {
2137 map_extent_buffer(right, (unsigned long)item,
2138 sizeof(struct btrfs_item),
2139 &right->map_token, &right->kaddr,
2140 &right->map_start, &right->map_len,
2141 KM_USER1);
2142 }
2143
2144 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002145 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002146 }
Chris Mason74123bd2007-02-02 11:05:29 -05002147
Chris Masondb945352007-10-15 16:15:53 -04002148 if (right->map_token) {
2149 unmap_extent_buffer(right, right->map_token, KM_USER1);
2150 right->map_token = NULL;
2151 }
2152
Chris Mason5f39d392007-10-15 16:14:19 -04002153 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002154 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002155 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002156 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2157 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002158 if (wret)
2159 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002160
2161 btrfs_mark_buffer_dirty(right);
2162 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002163 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002164
Chris Masonbe0e5c02007-01-26 15:51:26 -05002165 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002166 free_extent_buffer(path->nodes[0]);
2167 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 path->slots[0] -= mid;
2169 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002170 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002171 free_extent_buffer(right);
2172
Chris Masoneb60cea2007-02-02 09:18:22 -05002173 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002174
Chris Masoncc0c5532007-10-25 15:42:57 -04002175 if (double_split) {
2176 BUG_ON(num_doubles != 0);
2177 num_doubles++;
2178 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002179 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002180 return ret;
2181}
2182
Chris Masonb18c6682007-04-17 13:26:50 -04002183int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2184 struct btrfs_root *root,
2185 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002186 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002187{
2188 int ret = 0;
2189 int slot;
2190 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002191 struct extent_buffer *leaf;
2192 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002193 u32 nritems;
2194 unsigned int data_end;
2195 unsigned int old_data_start;
2196 unsigned int old_size;
2197 unsigned int size_diff;
2198 int i;
2199
2200 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002201 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002202 slot = path->slots[0];
2203
2204 old_size = btrfs_item_size_nr(leaf, slot);
2205 if (old_size == new_size)
2206 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002209 data_end = leaf_data_end(root, leaf);
2210
Chris Mason5f39d392007-10-15 16:14:19 -04002211 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002212
Chris Masonb18c6682007-04-17 13:26:50 -04002213 size_diff = old_size - new_size;
2214
2215 BUG_ON(slot < 0);
2216 BUG_ON(slot >= nritems);
2217
2218 /*
2219 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2220 */
2221 /* first correct the data pointers */
2222 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002223 u32 ioff;
2224 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002225
2226 if (!leaf->map_token) {
2227 map_extent_buffer(leaf, (unsigned long)item,
2228 sizeof(struct btrfs_item),
2229 &leaf->map_token, &leaf->kaddr,
2230 &leaf->map_start, &leaf->map_len,
2231 KM_USER1);
2232 }
2233
Chris Mason5f39d392007-10-15 16:14:19 -04002234 ioff = btrfs_item_offset(leaf, item);
2235 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002236 }
Chris Masondb945352007-10-15 16:15:53 -04002237
2238 if (leaf->map_token) {
2239 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2240 leaf->map_token = NULL;
2241 }
2242
Chris Masonb18c6682007-04-17 13:26:50 -04002243 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002244 if (from_end) {
2245 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2246 data_end + size_diff, btrfs_leaf_data(leaf) +
2247 data_end, old_data_start + new_size - data_end);
2248 } else {
2249 struct btrfs_disk_key disk_key;
2250 u64 offset;
2251
2252 btrfs_item_key(leaf, &disk_key, slot);
2253
2254 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2255 unsigned long ptr;
2256 struct btrfs_file_extent_item *fi;
2257
2258 fi = btrfs_item_ptr(leaf, slot,
2259 struct btrfs_file_extent_item);
2260 fi = (struct btrfs_file_extent_item *)(
2261 (unsigned long)fi - size_diff);
2262
2263 if (btrfs_file_extent_type(leaf, fi) ==
2264 BTRFS_FILE_EXTENT_INLINE) {
2265 ptr = btrfs_item_ptr_offset(leaf, slot);
2266 memmove_extent_buffer(leaf, ptr,
2267 (unsigned long)fi,
2268 offsetof(struct btrfs_file_extent_item,
2269 disk_bytenr));
2270 }
2271 }
2272
2273 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2274 data_end + size_diff, btrfs_leaf_data(leaf) +
2275 data_end, old_data_start - data_end);
2276
2277 offset = btrfs_disk_key_offset(&disk_key);
2278 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2279 btrfs_set_item_key(leaf, &disk_key, slot);
2280 if (slot == 0)
2281 fixup_low_keys(trans, root, path, &disk_key, 1);
2282 }
Chris Mason5f39d392007-10-15 16:14:19 -04002283
2284 item = btrfs_item_nr(leaf, slot);
2285 btrfs_set_item_size(leaf, item, new_size);
2286 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002287
2288 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 if (btrfs_leaf_free_space(root, leaf) < 0) {
2290 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002291 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002292 }
Chris Masonb18c6682007-04-17 13:26:50 -04002293 return ret;
2294}
2295
Chris Mason5f39d392007-10-15 16:14:19 -04002296int btrfs_extend_item(struct btrfs_trans_handle *trans,
2297 struct btrfs_root *root, struct btrfs_path *path,
2298 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002299{
2300 int ret = 0;
2301 int slot;
2302 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002303 struct extent_buffer *leaf;
2304 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002305 u32 nritems;
2306 unsigned int data_end;
2307 unsigned int old_data;
2308 unsigned int old_size;
2309 int i;
2310
2311 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002312 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002313
Chris Mason5f39d392007-10-15 16:14:19 -04002314 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002315 data_end = leaf_data_end(root, leaf);
2316
Chris Mason5f39d392007-10-15 16:14:19 -04002317 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2318 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002319 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002320 }
Chris Mason6567e832007-04-16 09:22:45 -04002321 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002322 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002323
2324 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002325 if (slot >= nritems) {
2326 btrfs_print_leaf(root, leaf);
2327 printk("slot %d too large, nritems %d\n", slot, nritems);
2328 BUG_ON(1);
2329 }
Chris Mason6567e832007-04-16 09:22:45 -04002330
2331 /*
2332 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2333 */
2334 /* first correct the data pointers */
2335 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002336 u32 ioff;
2337 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002338
2339 if (!leaf->map_token) {
2340 map_extent_buffer(leaf, (unsigned long)item,
2341 sizeof(struct btrfs_item),
2342 &leaf->map_token, &leaf->kaddr,
2343 &leaf->map_start, &leaf->map_len,
2344 KM_USER1);
2345 }
Chris Mason5f39d392007-10-15 16:14:19 -04002346 ioff = btrfs_item_offset(leaf, item);
2347 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002348 }
Chris Mason5f39d392007-10-15 16:14:19 -04002349
Chris Masondb945352007-10-15 16:15:53 -04002350 if (leaf->map_token) {
2351 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2352 leaf->map_token = NULL;
2353 }
2354
Chris Mason6567e832007-04-16 09:22:45 -04002355 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002356 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002357 data_end - data_size, btrfs_leaf_data(leaf) +
2358 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002359
Chris Mason6567e832007-04-16 09:22:45 -04002360 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002361 old_size = btrfs_item_size_nr(leaf, slot);
2362 item = btrfs_item_nr(leaf, slot);
2363 btrfs_set_item_size(leaf, item, old_size + data_size);
2364 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002365
2366 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002367 if (btrfs_leaf_free_space(root, leaf) < 0) {
2368 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002369 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002370 }
Chris Mason6567e832007-04-16 09:22:45 -04002371 return ret;
2372}
2373
Chris Mason74123bd2007-02-02 11:05:29 -05002374/*
2375 * Given a key and some data, insert an item into the tree.
2376 * This does all the path init required, making room in the tree if needed.
2377 */
Chris Mason9c583092008-01-29 15:15:18 -05002378int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002379 struct btrfs_root *root,
2380 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002381 struct btrfs_key *cpu_key, u32 *data_size,
2382 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002383{
Chris Mason5f39d392007-10-15 16:14:19 -04002384 struct extent_buffer *leaf;
2385 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002386 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002387 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002388 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002389 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002390 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002391 u32 total_size = 0;
2392 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002393 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002394 struct btrfs_disk_key disk_key;
2395
Chris Mason9c583092008-01-29 15:15:18 -05002396 for (i = 0; i < nr; i++) {
2397 total_data += data_size[i];
2398 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002399
Chris Mason74123bd2007-02-02 11:05:29 -05002400 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002401 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002402 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002403
Chris Mason9c583092008-01-29 15:15:18 -05002404 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2405 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002406 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002407 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002408 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002409 if (ret < 0)
2410 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002411
Chris Mason62e27492007-03-15 12:56:47 -04002412 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002413 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002414
Chris Mason5f39d392007-10-15 16:14:19 -04002415 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002416 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002417
Chris Mason123abc82007-03-14 14:14:43 -04002418 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002419 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002420 btrfs_print_leaf(root, leaf);
2421 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002422 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002424 }
Chris Mason5f39d392007-10-15 16:14:19 -04002425
Chris Mason62e27492007-03-15 12:56:47 -04002426 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002427 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002428
Chris Masonbe0e5c02007-01-26 15:51:26 -05002429 if (slot != nritems) {
2430 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002431 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 if (old_data < data_end) {
2434 btrfs_print_leaf(root, leaf);
2435 printk("slot %d old_data %d data_end %d\n",
2436 slot, old_data, data_end);
2437 BUG_ON(1);
2438 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002439 /*
2440 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2441 */
2442 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002443 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002444 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002445 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002446
Chris Mason5f39d392007-10-15 16:14:19 -04002447 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002448 if (!leaf->map_token) {
2449 map_extent_buffer(leaf, (unsigned long)item,
2450 sizeof(struct btrfs_item),
2451 &leaf->map_token, &leaf->kaddr,
2452 &leaf->map_start, &leaf->map_len,
2453 KM_USER1);
2454 }
2455
Chris Mason5f39d392007-10-15 16:14:19 -04002456 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002457 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002458 }
Chris Masondb945352007-10-15 16:15:53 -04002459 if (leaf->map_token) {
2460 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2461 leaf->map_token = NULL;
2462 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002463
2464 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002465 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002466 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002467 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002468
2469 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002470 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002471 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002472 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 data_end = old_data;
2474 }
Chris Mason5f39d392007-10-15 16:14:19 -04002475
Chris Mason62e27492007-03-15 12:56:47 -04002476 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002477 for (i = 0; i < nr; i++) {
2478 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2479 btrfs_set_item_key(leaf, &disk_key, slot + i);
2480 item = btrfs_item_nr(leaf, slot + i);
2481 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2482 data_end -= data_size[i];
2483 btrfs_set_item_size(leaf, item, data_size[i]);
2484 }
2485 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002486 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002487
2488 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002489 if (slot == 0) {
2490 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002491 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002492 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002493
Chris Mason5f39d392007-10-15 16:14:19 -04002494 if (btrfs_leaf_free_space(root, leaf) < 0) {
2495 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002497 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002498
Chris Masoned2ff2c2007-03-01 18:59:40 -05002499out:
Chris Mason62e27492007-03-15 12:56:47 -04002500 return ret;
2501}
2502
2503/*
2504 * Given a key and some data, insert an item into the tree.
2505 * This does all the path init required, making room in the tree if needed.
2506 */
Chris Masone089f052007-03-16 16:20:31 -04002507int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2508 *root, struct btrfs_key *cpu_key, void *data, u32
2509 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002510{
2511 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002512 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002513 struct extent_buffer *leaf;
2514 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002515
Chris Mason2c90e5d2007-04-02 10:50:19 -04002516 path = btrfs_alloc_path();
2517 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002518 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002519 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002520 leaf = path->nodes[0];
2521 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2522 write_extent_buffer(leaf, data, ptr, data_size);
2523 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002524 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002525 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002526 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002527}
2528
Chris Mason74123bd2007-02-02 11:05:29 -05002529/*
Chris Mason5de08d72007-02-24 06:24:44 -05002530 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002531 *
2532 * If the delete empties a node, the node is removed from the tree,
2533 * continuing all the way the root if required. The root is converted into
2534 * a leaf if all the nodes are emptied.
2535 */
Chris Masone089f052007-03-16 16:20:31 -04002536static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2537 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002538{
Chris Mason5f39d392007-10-15 16:14:19 -04002539 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002540 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002541 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002542 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002543
Chris Mason5f39d392007-10-15 16:14:19 -04002544 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002545 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002546 memmove_extent_buffer(parent,
2547 btrfs_node_key_ptr_offset(slot),
2548 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002549 sizeof(struct btrfs_key_ptr) *
2550 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002551 }
Chris Mason7518a232007-03-12 12:01:18 -04002552 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002553 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002554 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002555 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002556 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002557 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002558 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002559 struct btrfs_disk_key disk_key;
2560
2561 btrfs_node_key(parent, &disk_key, 0);
2562 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002563 if (wret)
2564 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002565 }
Chris Masond6025572007-03-30 14:27:56 -04002566 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002567 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002568}
2569
Chris Mason74123bd2007-02-02 11:05:29 -05002570/*
2571 * delete the item at the leaf level in path. If that empties
2572 * the leaf, remove it from the tree
2573 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002574int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2575 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576{
Chris Mason5f39d392007-10-15 16:14:19 -04002577 struct extent_buffer *leaf;
2578 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002579 int last_off;
2580 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002581 int ret = 0;
2582 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002583 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002584 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002585
Chris Mason5f39d392007-10-15 16:14:19 -04002586 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002587 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2588
2589 for (i = 0; i < nr; i++)
2590 dsize += btrfs_item_size_nr(leaf, slot + i);
2591
Chris Mason5f39d392007-10-15 16:14:19 -04002592 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593
Chris Mason85e21ba2008-01-29 15:11:36 -05002594 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002596 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002597
2598 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002599 data_end + dsize,
2600 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002601 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002602
Chris Mason85e21ba2008-01-29 15:11:36 -05002603 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002604 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002605
Chris Mason5f39d392007-10-15 16:14:19 -04002606 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002607 if (!leaf->map_token) {
2608 map_extent_buffer(leaf, (unsigned long)item,
2609 sizeof(struct btrfs_item),
2610 &leaf->map_token, &leaf->kaddr,
2611 &leaf->map_start, &leaf->map_len,
2612 KM_USER1);
2613 }
Chris Mason5f39d392007-10-15 16:14:19 -04002614 ioff = btrfs_item_offset(leaf, item);
2615 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002616 }
Chris Masondb945352007-10-15 16:15:53 -04002617
2618 if (leaf->map_token) {
2619 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2620 leaf->map_token = NULL;
2621 }
2622
Chris Mason5f39d392007-10-15 16:14:19 -04002623 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002624 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002625 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002626 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002627 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002628 btrfs_set_header_nritems(leaf, nritems - nr);
2629 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002630
Chris Mason74123bd2007-02-02 11:05:29 -05002631 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002632 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002633 if (leaf == root->node) {
2634 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002635 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002636 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002637 clean_tree_block(trans, root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002638 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002639 if (wret)
2640 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002641 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002642 leaf->start, leaf->len,
2643 btrfs_header_owner(path->nodes[1]),
2644 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002645 if (wret)
2646 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002647 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002649 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002650 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002651 struct btrfs_disk_key disk_key;
2652
2653 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002654 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002655 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002656 if (wret)
2657 ret = wret;
2658 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002659
Chris Mason74123bd2007-02-02 11:05:29 -05002660 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002661 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002662 /* push_leaf_left fixes the path.
2663 * make sure the path still points to our leaf
2664 * for possible call to del_ptr below
2665 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002666 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002667 extent_buffer_get(leaf);
2668
Chris Mason85e21ba2008-01-29 15:11:36 -05002669 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002670 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002671 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002672
2673 if (path->nodes[0] == leaf &&
2674 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002675 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002676 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002677 ret = wret;
2678 }
Chris Mason5f39d392007-10-15 16:14:19 -04002679
2680 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002681 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002682 u64 bytenr = leaf->start;
2683 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002684
Chris Mason7bb86312007-12-11 09:25:06 -05002685 root_gen = btrfs_header_generation(
2686 path->nodes[1]);
2687
Chris Mason5f39d392007-10-15 16:14:19 -04002688 clean_tree_block(trans, root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002689
Chris Masone089f052007-03-16 16:20:31 -04002690 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002691 if (wret)
2692 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002693
2694 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002695 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002696 blocksize,
2697 btrfs_header_owner(path->nodes[1]),
2698 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002699 if (wret)
2700 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002701 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002702 btrfs_mark_buffer_dirty(leaf);
2703 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 }
Chris Masond5719762007-03-23 10:01:08 -04002705 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002707 }
2708 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002709 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002710}
2711
Chris Mason97571fd2007-02-24 13:39:08 -05002712/*
Chris Mason7bb86312007-12-11 09:25:06 -05002713 * walk up the tree as far as required to find the previous leaf.
2714 * returns 0 if it found something or 1 if there are no lesser leaves.
2715 * returns < 0 on io errors.
2716 */
2717int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2718{
Chris Mason8f662a72008-01-02 10:01:11 -05002719 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002720 int slot;
2721 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002722 struct extent_buffer *c;
2723 struct extent_buffer *next = NULL;
2724
2725 while(level < BTRFS_MAX_LEVEL) {
2726 if (!path->nodes[level])
2727 return 1;
2728
2729 slot = path->slots[level];
2730 c = path->nodes[level];
2731 if (slot == 0) {
2732 level++;
2733 if (level == BTRFS_MAX_LEVEL)
2734 return 1;
2735 continue;
2736 }
2737 slot--;
2738
2739 bytenr = btrfs_node_blockptr(c, slot);
2740 if (next)
2741 free_extent_buffer(next);
2742
Chris Mason7bb86312007-12-11 09:25:06 -05002743 next = read_tree_block(root, bytenr,
2744 btrfs_level_size(root, level - 1));
2745 break;
2746 }
2747 path->slots[level] = slot;
2748 while(1) {
2749 level--;
2750 c = path->nodes[level];
2751 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002752 slot = btrfs_header_nritems(next);
2753 if (slot != 0)
2754 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002755 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002756 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002757 if (!level)
2758 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002759 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002760 btrfs_level_size(root, level - 1));
2761 }
2762 return 0;
2763}
2764
2765/*
Chris Mason97571fd2007-02-24 13:39:08 -05002766 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002767 * returns 0 if it found something or 1 if there are no greater leaves.
2768 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002769 */
Chris Mason234b63a2007-03-13 10:46:10 -04002770int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002771{
2772 int slot;
2773 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002774 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002775 struct extent_buffer *c;
2776 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002777
Chris Mason234b63a2007-03-13 10:46:10 -04002778 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002779 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002780 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002781
Chris Masond97e63b2007-02-20 16:40:44 -05002782 slot = path->slots[level] + 1;
2783 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002784 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002785 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002786 if (level == BTRFS_MAX_LEVEL)
2787 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002788 continue;
2789 }
Chris Mason5f39d392007-10-15 16:14:19 -04002790
Chris Masondb945352007-10-15 16:15:53 -04002791 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002792 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002793 free_extent_buffer(next);
2794
Chris Mason6702ed42007-08-07 16:15:09 -04002795 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002796 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002797
Chris Masondb945352007-10-15 16:15:53 -04002798 next = read_tree_block(root, bytenr,
2799 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002800 break;
2801 }
2802 path->slots[level] = slot;
2803 while(1) {
2804 level--;
2805 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002806 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002807 path->nodes[level] = next;
2808 path->slots[level] = 0;
2809 if (!level)
2810 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002811 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002812 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002813 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2814 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002815 }
2816 return 0;
2817}
Chris Mason0b86a832008-03-24 15:01:56 -04002818
2819int btrfs_previous_item(struct btrfs_root *root,
2820 struct btrfs_path *path, u64 min_objectid,
2821 int type)
2822{
2823 struct btrfs_key found_key;
2824 struct extent_buffer *leaf;
2825 int ret;
2826
2827 while(1) {
2828 if (path->slots[0] == 0) {
2829 ret = btrfs_prev_leaf(root, path);
2830 if (ret != 0)
2831 return ret;
2832 } else {
2833 path->slots[0]--;
2834 }
2835 leaf = path->nodes[0];
2836 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2837 if (found_key.type == type)
2838 return 0;
2839 }
2840 return 1;
2841}
2842