blob: 319f9f9bf8d457f1528c1186c96956c1bf1589dc [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,
Chris Mason971a1f62008-04-24 10:54:32 -040032 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040033static 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];
Aneesha1f396302007-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) {
Aneesha1f396302007-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) {
Aneesha1f396302007-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);
Chris Mason971a1f62008-04-24 10:54:32 -0400792 wret = push_node_left(trans, root, left, mid, 0);
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) {
Chris Mason971a1f62008-04-24 10:54:32 -0400803 wret = push_node_left(trans, root, mid, right, 1);
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 Mason971a1f62008-04-24 10:54:32 -0400944 left, mid, 0);
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 Mason971a1f62008-04-24 10:54:32 -04001242 struct extent_buffer *src, int empty)
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 Mason971a1f62008-04-24 10:54:32 -04001255 if (!empty && src_nritems <= 2)
1256 return 1;
1257
Chris Masoneb60cea2007-02-02 09:18:22 -05001258 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001260 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001261
Chris Mason971a1f62008-04-24 10:54:32 -04001262 if (empty)
1263 push_items = min(src_nritems, push_items);
1264 else
1265 push_items = min(src_nritems - 2, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001266
Chris Mason5f39d392007-10-15 16:14:19 -04001267 copy_extent_buffer(dst, src,
1268 btrfs_node_key_ptr_offset(dst_nritems),
1269 btrfs_node_key_ptr_offset(0),
1270 push_items * sizeof(struct btrfs_key_ptr));
1271
Chris Masonbb803952007-03-01 12:04:21 -05001272 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001273 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1274 btrfs_node_key_ptr_offset(push_items),
1275 (src_nritems - push_items) *
1276 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001277 }
Chris Mason5f39d392007-10-15 16:14:19 -04001278 btrfs_set_header_nritems(src, src_nritems - push_items);
1279 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1280 btrfs_mark_buffer_dirty(src);
1281 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001282 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001283}
1284
Chris Mason97571fd2007-02-24 13:39:08 -05001285/*
Chris Mason79f95c82007-03-01 15:16:26 -05001286 * try to push data from one node into the next node right in the
1287 * tree.
1288 *
1289 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1290 * error, and > 0 if there was no room in the right hand block.
1291 *
1292 * this will only push up to 1/2 the contents of the left node over
1293 */
Chris Mason5f39d392007-10-15 16:14:19 -04001294static int balance_node_right(struct btrfs_trans_handle *trans,
1295 struct btrfs_root *root,
1296 struct extent_buffer *dst,
1297 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001298{
Chris Mason79f95c82007-03-01 15:16:26 -05001299 int push_items = 0;
1300 int max_push;
1301 int src_nritems;
1302 int dst_nritems;
1303 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001304
Chris Mason7bb86312007-12-11 09:25:06 -05001305 WARN_ON(btrfs_header_generation(src) != trans->transid);
1306 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1307
Chris Mason5f39d392007-10-15 16:14:19 -04001308 src_nritems = btrfs_header_nritems(src);
1309 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001310 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001311 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001312 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001313
1314 max_push = src_nritems / 2 + 1;
1315 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001316 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001317 return 1;
Yan252c38f2007-08-29 09:11:44 -04001318
Chris Mason79f95c82007-03-01 15:16:26 -05001319 if (max_push < push_items)
1320 push_items = max_push;
1321
Chris Mason5f39d392007-10-15 16:14:19 -04001322 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1323 btrfs_node_key_ptr_offset(0),
1324 (dst_nritems) *
1325 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001326
Chris Mason5f39d392007-10-15 16:14:19 -04001327 copy_extent_buffer(dst, src,
1328 btrfs_node_key_ptr_offset(0),
1329 btrfs_node_key_ptr_offset(src_nritems - push_items),
1330 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001331
Chris Mason5f39d392007-10-15 16:14:19 -04001332 btrfs_set_header_nritems(src, src_nritems - push_items);
1333 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001334
Chris Mason5f39d392007-10-15 16:14:19 -04001335 btrfs_mark_buffer_dirty(src);
1336 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001337 return ret;
1338}
1339
1340/*
Chris Mason97571fd2007-02-24 13:39:08 -05001341 * helper function to insert a new root level in the tree.
1342 * A new node is allocated, and a single item is inserted to
1343 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001344 *
1345 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001346 */
Chris Mason98ed5172008-01-03 10:01:48 -05001347static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001348 struct btrfs_root *root,
1349 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001350{
Chris Mason7bb86312007-12-11 09:25:06 -05001351 u64 root_gen;
1352 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001353 struct extent_buffer *lower;
1354 struct extent_buffer *c;
1355 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001356
1357 BUG_ON(path->nodes[level]);
1358 BUG_ON(path->nodes[level-1] != root->node);
1359
Chris Mason7bb86312007-12-11 09:25:06 -05001360 if (root->ref_cows)
1361 root_gen = trans->transid;
1362 else
1363 root_gen = 0;
1364
1365 lower = path->nodes[level-1];
1366 if (level == 1)
1367 btrfs_item_key(lower, &lower_key, 0);
1368 else
1369 btrfs_node_key(lower, &lower_key, 0);
1370
1371 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1372 root->root_key.objectid,
1373 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001374 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001375 if (IS_ERR(c))
1376 return PTR_ERR(c);
1377 memset_extent_buffer(c, 0, 0, root->nodesize);
1378 btrfs_set_header_nritems(c, 1);
1379 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001380 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001381 btrfs_set_header_generation(c, trans->transid);
1382 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001383
Chris Mason5f39d392007-10-15 16:14:19 -04001384 write_extent_buffer(c, root->fs_info->fsid,
1385 (unsigned long)btrfs_header_fsid(c),
1386 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001387
1388 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1389 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1390 BTRFS_UUID_SIZE);
1391
Chris Mason5f39d392007-10-15 16:14:19 -04001392 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001393 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001394 lower_gen = btrfs_header_generation(lower);
1395 WARN_ON(lower_gen == 0);
1396
1397 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001398
1399 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001400
Chris Mason5c680ed2007-02-22 11:39:13 -05001401 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001402 free_extent_buffer(root->node);
1403 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001404 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001405 extent_buffer_get(c);
1406 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001407 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001408
1409 if (root->ref_cows && lower_gen != trans->transid) {
1410 struct btrfs_path *back_path = btrfs_alloc_path();
1411 int ret;
1412 ret = btrfs_insert_extent_backref(trans,
1413 root->fs_info->extent_root,
1414 path, lower->start,
1415 root->root_key.objectid,
1416 trans->transid, 0, 0);
1417 BUG_ON(ret);
1418 btrfs_free_path(back_path);
1419 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001420 return 0;
1421}
1422
Chris Mason74123bd2007-02-02 11:05:29 -05001423/*
1424 * worker function to insert a single pointer in a node.
1425 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001426 *
Chris Mason74123bd2007-02-02 11:05:29 -05001427 * slot and level indicate where you want the key to go, and
1428 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001429 *
1430 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001431 */
Chris Masone089f052007-03-16 16:20:31 -04001432static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1433 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001434 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001435{
Chris Mason5f39d392007-10-15 16:14:19 -04001436 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001437 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001438
1439 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001440 lower = path->nodes[level];
1441 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001442 if (slot > nritems)
1443 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001444 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001445 BUG();
1446 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001447 memmove_extent_buffer(lower,
1448 btrfs_node_key_ptr_offset(slot + 1),
1449 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001450 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001451 }
Chris Mason5f39d392007-10-15 16:14:19 -04001452 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001453 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001454 WARN_ON(trans->transid == 0);
1455 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001456 btrfs_set_header_nritems(lower, nritems + 1);
1457 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001458 return 0;
1459}
1460
Chris Mason97571fd2007-02-24 13:39:08 -05001461/*
1462 * split the node at the specified level in path in two.
1463 * The path is corrected to point to the appropriate node after the split
1464 *
1465 * Before splitting this tries to make some room in the node by pushing
1466 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001467 *
1468 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001469 */
Chris Masone089f052007-03-16 16:20:31 -04001470static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1471 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001472{
Chris Mason7bb86312007-12-11 09:25:06 -05001473 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001474 struct extent_buffer *c;
1475 struct extent_buffer *split;
1476 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001477 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001478 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001479 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001480 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001481
Chris Mason5f39d392007-10-15 16:14:19 -04001482 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001483 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001484 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001485 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001486 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001487 if (ret)
1488 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001489 } else {
1490 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001491 c = path->nodes[level];
1492 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001493 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001494 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001495 if (ret < 0)
1496 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001497 }
Chris Masone66f7092007-04-20 13:16:02 -04001498
Chris Mason5f39d392007-10-15 16:14:19 -04001499 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001500 if (root->ref_cows)
1501 root_gen = trans->transid;
1502 else
1503 root_gen = 0;
1504
1505 btrfs_node_key(c, &disk_key, 0);
1506 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1507 root->root_key.objectid,
1508 root_gen,
1509 btrfs_disk_key_objectid(&disk_key),
1510 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001511 if (IS_ERR(split))
1512 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001513
Chris Mason5f39d392007-10-15 16:14:19 -04001514 btrfs_set_header_flags(split, btrfs_header_flags(c));
1515 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001516 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001517 btrfs_set_header_generation(split, trans->transid);
1518 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001519 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001520 write_extent_buffer(split, root->fs_info->fsid,
1521 (unsigned long)btrfs_header_fsid(split),
1522 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001523 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1524 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1525 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001526
Chris Mason7518a232007-03-12 12:01:18 -04001527 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001528
1529 copy_extent_buffer(split, c,
1530 btrfs_node_key_ptr_offset(0),
1531 btrfs_node_key_ptr_offset(mid),
1532 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1533 btrfs_set_header_nritems(split, c_nritems - mid);
1534 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001535 ret = 0;
1536
Chris Mason5f39d392007-10-15 16:14:19 -04001537 btrfs_mark_buffer_dirty(c);
1538 btrfs_mark_buffer_dirty(split);
1539
1540 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001541 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001542 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001543 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001544 if (wret)
1545 ret = wret;
1546
Chris Mason5de08d72007-02-24 06:24:44 -05001547 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001548 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001549 free_extent_buffer(c);
1550 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001551 path->slots[level + 1] += 1;
1552 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001553 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001555 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001556}
1557
Chris Mason74123bd2007-02-02 11:05:29 -05001558/*
1559 * how many bytes are required to store the items in a leaf. start
1560 * and nr indicate which items in the leaf to check. This totals up the
1561 * space used both by the item structs and the item data
1562 */
Chris Mason5f39d392007-10-15 16:14:19 -04001563static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001564{
1565 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001566 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001567 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001568
1569 if (!nr)
1570 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001571 data_len = btrfs_item_end_nr(l, start);
1572 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001573 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001574 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001575 return data_len;
1576}
1577
Chris Mason74123bd2007-02-02 11:05:29 -05001578/*
Chris Masond4dbff92007-04-04 14:08:15 -04001579 * The space between the end of the leaf items and
1580 * the start of the leaf data. IOW, how much room
1581 * the leaf has left for both items and data
1582 */
Chris Mason5f39d392007-10-15 16:14:19 -04001583int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001584{
Chris Mason5f39d392007-10-15 16:14:19 -04001585 int nritems = btrfs_header_nritems(leaf);
1586 int ret;
1587 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1588 if (ret < 0) {
1589 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001590 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001591 leaf_space_used(leaf, 0, nritems), nritems);
1592 }
1593 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001594}
1595
1596/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001597 * push some data in the path leaf to the right, trying to free up at
1598 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001599 *
1600 * returns 1 if the push failed because the other node didn't have enough
1601 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001602 */
Chris Masone089f052007-03-16 16:20:31 -04001603static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001604 *root, struct btrfs_path *path, int data_size,
1605 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001606{
Chris Mason5f39d392007-10-15 16:14:19 -04001607 struct extent_buffer *left = path->nodes[0];
1608 struct extent_buffer *right;
1609 struct extent_buffer *upper;
1610 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001611 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001612 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001613 int free_space;
1614 int push_space = 0;
1615 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001616 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001617 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001618 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001619 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001620 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001621 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001622 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001623
1624 slot = path->slots[1];
1625 if (!path->nodes[1]) {
1626 return 1;
1627 }
1628 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001629 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001630 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001631
Chris Masondb945352007-10-15 16:15:53 -04001632 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1633 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001634 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001635 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001637 return 1;
1638 }
1639
Chris Mason5f39d392007-10-15 16:14:19 -04001640 /* cow and double check */
1641 ret = btrfs_cow_block(trans, root, right, upper,
1642 slot + 1, &right);
1643 if (ret) {
1644 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001645 return 1;
1646 }
Chris Mason5f39d392007-10-15 16:14:19 -04001647 free_space = btrfs_leaf_free_space(root, right);
1648 if (free_space < data_size + sizeof(struct btrfs_item)) {
1649 free_extent_buffer(right);
1650 return 1;
1651 }
1652
1653 left_nritems = btrfs_header_nritems(left);
1654 if (left_nritems == 0) {
1655 free_extent_buffer(right);
1656 return 1;
1657 }
1658
Chris Mason34a38212007-11-07 13:31:03 -05001659 if (empty)
1660 nr = 0;
1661 else
1662 nr = 1;
1663
1664 i = left_nritems - 1;
1665 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001666 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001667
Chris Mason00ec4c52007-02-24 12:47:20 -05001668 if (path->slots[0] == i)
1669 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001670
1671 if (!left->map_token) {
1672 map_extent_buffer(left, (unsigned long)item,
1673 sizeof(struct btrfs_item),
1674 &left->map_token, &left->kaddr,
1675 &left->map_start, &left->map_len,
1676 KM_USER1);
1677 }
1678
1679 this_item_size = btrfs_item_size(left, item);
1680 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001681 break;
1682 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001683 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001684 if (i == 0)
1685 break;
1686 i--;
Chris Masondb945352007-10-15 16:15:53 -04001687 }
1688 if (left->map_token) {
1689 unmap_extent_buffer(left, left->map_token, KM_USER1);
1690 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001691 }
Chris Mason5f39d392007-10-15 16:14:19 -04001692
Chris Mason00ec4c52007-02-24 12:47:20 -05001693 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001694 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001695 return 1;
1696 }
Chris Mason5f39d392007-10-15 16:14:19 -04001697
Chris Mason34a38212007-11-07 13:31:03 -05001698 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001699 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001700
Chris Mason00ec4c52007-02-24 12:47:20 -05001701 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001702 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001703
Chris Mason5f39d392007-10-15 16:14:19 -04001704 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001705 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001706
Chris Mason00ec4c52007-02-24 12:47:20 -05001707 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001708 data_end = leaf_data_end(root, right);
1709 memmove_extent_buffer(right,
1710 btrfs_leaf_data(right) + data_end - push_space,
1711 btrfs_leaf_data(right) + data_end,
1712 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1713
Chris Mason00ec4c52007-02-24 12:47:20 -05001714 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001715 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001716 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1717 btrfs_leaf_data(left) + leaf_data_end(root, left),
1718 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001719
1720 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1721 btrfs_item_nr_offset(0),
1722 right_nritems * sizeof(struct btrfs_item));
1723
Chris Mason00ec4c52007-02-24 12:47:20 -05001724 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001725 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1726 btrfs_item_nr_offset(left_nritems - push_items),
1727 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001728
1729 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001730 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001731 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001732 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001733 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001734 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001735 if (!right->map_token) {
1736 map_extent_buffer(right, (unsigned long)item,
1737 sizeof(struct btrfs_item),
1738 &right->map_token, &right->kaddr,
1739 &right->map_start, &right->map_len,
1740 KM_USER1);
1741 }
1742 push_space -= btrfs_item_size(right, item);
1743 btrfs_set_item_offset(right, item, push_space);
1744 }
1745
1746 if (right->map_token) {
1747 unmap_extent_buffer(right, right->map_token, KM_USER1);
1748 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001749 }
Chris Mason7518a232007-03-12 12:01:18 -04001750 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001751 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001752
Chris Mason34a38212007-11-07 13:31:03 -05001753 if (left_nritems)
1754 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001755 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001756
Chris Mason5f39d392007-10-15 16:14:19 -04001757 btrfs_item_key(right, &disk_key, 0);
1758 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001759 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001760
Chris Mason00ec4c52007-02-24 12:47:20 -05001761 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001762 if (path->slots[0] >= left_nritems) {
1763 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001764 free_extent_buffer(path->nodes[0]);
1765 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001766 path->slots[1] += 1;
1767 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001768 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001769 }
1770 return 0;
1771}
1772/*
Chris Mason74123bd2007-02-02 11:05:29 -05001773 * push some data in the path leaf to the left, trying to free up at
1774 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1775 */
Chris Masone089f052007-03-16 16:20:31 -04001776static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001777 *root, struct btrfs_path *path, int data_size,
1778 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001779{
Chris Mason5f39d392007-10-15 16:14:19 -04001780 struct btrfs_disk_key disk_key;
1781 struct extent_buffer *right = path->nodes[0];
1782 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001783 int slot;
1784 int i;
1785 int free_space;
1786 int push_space = 0;
1787 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001788 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001789 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001790 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001791 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001792 int ret = 0;
1793 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001794 u32 this_item_size;
1795 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796
1797 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001798 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001799 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001800 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001801 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001802
Chris Mason3685f792007-10-19 09:23:27 -04001803 right_nritems = btrfs_header_nritems(right);
1804 if (right_nritems == 0) {
1805 return 1;
1806 }
1807
Chris Mason5f39d392007-10-15 16:14:19 -04001808 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001809 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001810 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001811 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001812 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001813 return 1;
1814 }
Chris Mason02217ed2007-03-02 16:08:05 -05001815
1816 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001817 ret = btrfs_cow_block(trans, root, left,
1818 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001819 if (ret) {
1820 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001821 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001822 return 1;
1823 }
Chris Mason3685f792007-10-19 09:23:27 -04001824
Chris Mason123abc82007-03-14 14:14:43 -04001825 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001826 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001827 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001828 return 1;
1829 }
1830
Chris Mason34a38212007-11-07 13:31:03 -05001831 if (empty)
1832 nr = right_nritems;
1833 else
1834 nr = right_nritems - 1;
1835
1836 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001837 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001838 if (!right->map_token) {
1839 map_extent_buffer(right, (unsigned long)item,
1840 sizeof(struct btrfs_item),
1841 &right->map_token, &right->kaddr,
1842 &right->map_start, &right->map_len,
1843 KM_USER1);
1844 }
1845
Chris Masonbe0e5c02007-01-26 15:51:26 -05001846 if (path->slots[0] == i)
1847 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001848
1849 this_item_size = btrfs_item_size(right, item);
1850 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001851 break;
Chris Masondb945352007-10-15 16:15:53 -04001852
Chris Masonbe0e5c02007-01-26 15:51:26 -05001853 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001854 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001855 }
Chris Masondb945352007-10-15 16:15:53 -04001856
1857 if (right->map_token) {
1858 unmap_extent_buffer(right, right->map_token, KM_USER1);
1859 right->map_token = NULL;
1860 }
1861
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001863 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001864 return 1;
1865 }
Chris Mason34a38212007-11-07 13:31:03 -05001866 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001867 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001868
Chris Masonbe0e5c02007-01-26 15:51:26 -05001869 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001870 copy_extent_buffer(left, right,
1871 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1872 btrfs_item_nr_offset(0),
1873 push_items * sizeof(struct btrfs_item));
1874
Chris Mason123abc82007-03-14 14:14:43 -04001875 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001876 btrfs_item_offset_nr(right, push_items -1);
1877
1878 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001879 leaf_data_end(root, left) - push_space,
1880 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001881 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001882 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001883 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001884 BUG_ON(old_left_nritems < 0);
1885
Chris Masondb945352007-10-15 16:15:53 -04001886 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001887 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001888 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001889
Chris Mason5f39d392007-10-15 16:14:19 -04001890 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001891 if (!left->map_token) {
1892 map_extent_buffer(left, (unsigned long)item,
1893 sizeof(struct btrfs_item),
1894 &left->map_token, &left->kaddr,
1895 &left->map_start, &left->map_len,
1896 KM_USER1);
1897 }
1898
Chris Mason5f39d392007-10-15 16:14:19 -04001899 ioff = btrfs_item_offset(left, item);
1900 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001901 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001902 }
Chris Mason5f39d392007-10-15 16:14:19 -04001903 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001904 if (left->map_token) {
1905 unmap_extent_buffer(left, left->map_token, KM_USER1);
1906 left->map_token = NULL;
1907 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001908
1909 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001910 if (push_items > right_nritems) {
1911 printk("push items %d nr %u\n", push_items, right_nritems);
1912 WARN_ON(1);
1913 }
Chris Mason5f39d392007-10-15 16:14:19 -04001914
Chris Mason34a38212007-11-07 13:31:03 -05001915 if (push_items < right_nritems) {
1916 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1917 leaf_data_end(root, right);
1918 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1919 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1920 btrfs_leaf_data(right) +
1921 leaf_data_end(root, right), push_space);
1922
1923 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001924 btrfs_item_nr_offset(push_items),
1925 (btrfs_header_nritems(right) - push_items) *
1926 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001927 }
Yaneef1c492007-11-26 10:58:13 -05001928 right_nritems -= push_items;
1929 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001930 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001931 for (i = 0; i < right_nritems; i++) {
1932 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001933
1934 if (!right->map_token) {
1935 map_extent_buffer(right, (unsigned long)item,
1936 sizeof(struct btrfs_item),
1937 &right->map_token, &right->kaddr,
1938 &right->map_start, &right->map_len,
1939 KM_USER1);
1940 }
1941
1942 push_space = push_space - btrfs_item_size(right, item);
1943 btrfs_set_item_offset(right, item, push_space);
1944 }
1945 if (right->map_token) {
1946 unmap_extent_buffer(right, right->map_token, KM_USER1);
1947 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001948 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001949
Chris Mason5f39d392007-10-15 16:14:19 -04001950 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001951 if (right_nritems)
1952 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001953
Chris Mason5f39d392007-10-15 16:14:19 -04001954 btrfs_item_key(right, &disk_key, 0);
1955 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001956 if (wret)
1957 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001958
1959 /* then fixup the leaf pointer in the path */
1960 if (path->slots[0] < push_items) {
1961 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001962 free_extent_buffer(path->nodes[0]);
1963 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 path->slots[1] -= 1;
1965 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001966 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967 path->slots[0] -= push_items;
1968 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001969 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001970 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001971}
1972
Chris Mason74123bd2007-02-02 11:05:29 -05001973/*
1974 * split the path's leaf in two, making sure there is at least data_size
1975 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001976 *
1977 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001978 */
Chris Masone089f052007-03-16 16:20:31 -04001979static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001980 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001981 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001982{
Chris Mason7bb86312007-12-11 09:25:06 -05001983 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001984 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001985 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001986 int mid;
1987 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001988 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001989 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001990 int data_copy_size;
1991 int rt_data_off;
1992 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001993 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001994 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001995 int double_split;
1996 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001997 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998
Chris Masoncc0c5532007-10-25 15:42:57 -04001999 if (extend)
2000 space_needed = data_size;
2001
Chris Mason7bb86312007-12-11 09:25:06 -05002002 if (root->ref_cows)
2003 root_gen = trans->transid;
2004 else
2005 root_gen = 0;
2006
Chris Mason40689472007-03-17 14:29:23 -04002007 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002008 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002009 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002010 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002011 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002012 }
2013 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002014 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002015 if (wret < 0)
2016 return wret;
2017 }
2018 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002019
Chris Mason3685f792007-10-19 09:23:27 -04002020 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002021 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002022 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002023 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002024
Chris Mason5c680ed2007-02-22 11:39:13 -05002025 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002026 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002027 if (ret)
2028 return ret;
2029 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002030again:
2031 double_split = 0;
2032 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002033 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002034 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002035 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002036
Chris Mason7bb86312007-12-11 09:25:06 -05002037 btrfs_item_key(l, &disk_key, 0);
2038
2039 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2040 root->root_key.objectid,
2041 root_gen, disk_key.objectid, 0,
2042 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002043 if (IS_ERR(right)) {
2044 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002045 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002046 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002049 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002050 btrfs_set_header_generation(right, trans->transid);
2051 btrfs_set_header_owner(right, root->root_key.objectid);
2052 btrfs_set_header_level(right, 0);
2053 write_extent_buffer(right, root->fs_info->fsid,
2054 (unsigned long)btrfs_header_fsid(right),
2055 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002056
2057 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2058 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2059 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002060 if (mid <= slot) {
2061 if (nritems == 1 ||
2062 leaf_space_used(l, mid, nritems - mid) + space_needed >
2063 BTRFS_LEAF_DATA_SIZE(root)) {
2064 if (slot >= nritems) {
2065 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002067 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002068 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002069 path->slots[1] + 1, 1);
2070 if (wret)
2071 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002072 free_extent_buffer(path->nodes[0]);
2073 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002074 path->slots[0] = 0;
2075 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002076 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002077 return ret;
2078 }
2079 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002080 if (mid != nritems &&
2081 leaf_space_used(l, mid, nritems - mid) +
2082 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2083 double_split = 1;
2084 }
Chris Masond4dbff92007-04-04 14:08:15 -04002085 }
2086 } else {
2087 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2088 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002089 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002090 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002091 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002092 wret = insert_ptr(trans, root, path,
2093 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002094 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002095 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002096 if (wret)
2097 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002098 free_extent_buffer(path->nodes[0]);
2099 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002100 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002101 if (path->slots[1] == 0) {
2102 wret = fixup_low_keys(trans, root,
2103 path, &disk_key, 1);
2104 if (wret)
2105 ret = wret;
2106 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002107 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002108 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002109 } else if (extend && slot == 0) {
2110 mid = 1;
2111 } else {
2112 mid = slot;
2113 if (mid != nritems &&
2114 leaf_space_used(l, mid, nritems - mid) +
2115 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2116 double_split = 1;
2117 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002118 }
Chris Masond4dbff92007-04-04 14:08:15 -04002119 }
2120 }
Chris Mason5f39d392007-10-15 16:14:19 -04002121 nritems = nritems - mid;
2122 btrfs_set_header_nritems(right, nritems);
2123 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2124
2125 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2126 btrfs_item_nr_offset(mid),
2127 nritems * sizeof(struct btrfs_item));
2128
2129 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002130 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2131 data_copy_size, btrfs_leaf_data(l) +
2132 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002133
Chris Mason5f39d392007-10-15 16:14:19 -04002134 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2135 btrfs_item_end_nr(l, mid);
2136
2137 for (i = 0; i < nritems; i++) {
2138 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002139 u32 ioff;
2140
2141 if (!right->map_token) {
2142 map_extent_buffer(right, (unsigned long)item,
2143 sizeof(struct btrfs_item),
2144 &right->map_token, &right->kaddr,
2145 &right->map_start, &right->map_len,
2146 KM_USER1);
2147 }
2148
2149 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002150 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002151 }
Chris Mason74123bd2007-02-02 11:05:29 -05002152
Chris Masondb945352007-10-15 16:15:53 -04002153 if (right->map_token) {
2154 unmap_extent_buffer(right, right->map_token, KM_USER1);
2155 right->map_token = NULL;
2156 }
2157
Chris Mason5f39d392007-10-15 16:14:19 -04002158 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002159 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002160 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002161 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2162 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002163 if (wret)
2164 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002165
2166 btrfs_mark_buffer_dirty(right);
2167 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002168 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002169
Chris Masonbe0e5c02007-01-26 15:51:26 -05002170 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002171 free_extent_buffer(path->nodes[0]);
2172 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002173 path->slots[0] -= mid;
2174 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002175 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002176 free_extent_buffer(right);
2177
Chris Masoneb60cea2007-02-02 09:18:22 -05002178 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002179
Chris Masoncc0c5532007-10-25 15:42:57 -04002180 if (double_split) {
2181 BUG_ON(num_doubles != 0);
2182 num_doubles++;
2183 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002184 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002185 return ret;
2186}
2187
Chris Masonb18c6682007-04-17 13:26:50 -04002188int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root,
2190 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002191 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002192{
2193 int ret = 0;
2194 int slot;
2195 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002196 struct extent_buffer *leaf;
2197 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002198 u32 nritems;
2199 unsigned int data_end;
2200 unsigned int old_data_start;
2201 unsigned int old_size;
2202 unsigned int size_diff;
2203 int i;
2204
2205 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002206 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002207 slot = path->slots[0];
2208
2209 old_size = btrfs_item_size_nr(leaf, slot);
2210 if (old_size == new_size)
2211 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002212
Chris Mason5f39d392007-10-15 16:14:19 -04002213 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002214 data_end = leaf_data_end(root, leaf);
2215
Chris Mason5f39d392007-10-15 16:14:19 -04002216 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002217
Chris Masonb18c6682007-04-17 13:26:50 -04002218 size_diff = old_size - new_size;
2219
2220 BUG_ON(slot < 0);
2221 BUG_ON(slot >= nritems);
2222
2223 /*
2224 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2225 */
2226 /* first correct the data pointers */
2227 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002228 u32 ioff;
2229 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002230
2231 if (!leaf->map_token) {
2232 map_extent_buffer(leaf, (unsigned long)item,
2233 sizeof(struct btrfs_item),
2234 &leaf->map_token, &leaf->kaddr,
2235 &leaf->map_start, &leaf->map_len,
2236 KM_USER1);
2237 }
2238
Chris Mason5f39d392007-10-15 16:14:19 -04002239 ioff = btrfs_item_offset(leaf, item);
2240 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002241 }
Chris Masondb945352007-10-15 16:15:53 -04002242
2243 if (leaf->map_token) {
2244 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2245 leaf->map_token = NULL;
2246 }
2247
Chris Masonb18c6682007-04-17 13:26:50 -04002248 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002249 if (from_end) {
2250 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2251 data_end + size_diff, btrfs_leaf_data(leaf) +
2252 data_end, old_data_start + new_size - data_end);
2253 } else {
2254 struct btrfs_disk_key disk_key;
2255 u64 offset;
2256
2257 btrfs_item_key(leaf, &disk_key, slot);
2258
2259 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2260 unsigned long ptr;
2261 struct btrfs_file_extent_item *fi;
2262
2263 fi = btrfs_item_ptr(leaf, slot,
2264 struct btrfs_file_extent_item);
2265 fi = (struct btrfs_file_extent_item *)(
2266 (unsigned long)fi - size_diff);
2267
2268 if (btrfs_file_extent_type(leaf, fi) ==
2269 BTRFS_FILE_EXTENT_INLINE) {
2270 ptr = btrfs_item_ptr_offset(leaf, slot);
2271 memmove_extent_buffer(leaf, ptr,
2272 (unsigned long)fi,
2273 offsetof(struct btrfs_file_extent_item,
2274 disk_bytenr));
2275 }
2276 }
2277
2278 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2279 data_end + size_diff, btrfs_leaf_data(leaf) +
2280 data_end, old_data_start - data_end);
2281
2282 offset = btrfs_disk_key_offset(&disk_key);
2283 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2284 btrfs_set_item_key(leaf, &disk_key, slot);
2285 if (slot == 0)
2286 fixup_low_keys(trans, root, path, &disk_key, 1);
2287 }
Chris Mason5f39d392007-10-15 16:14:19 -04002288
2289 item = btrfs_item_nr(leaf, slot);
2290 btrfs_set_item_size(leaf, item, new_size);
2291 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002292
2293 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 if (btrfs_leaf_free_space(root, leaf) < 0) {
2295 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002296 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002297 }
Chris Masonb18c6682007-04-17 13:26:50 -04002298 return ret;
2299}
2300
Chris Mason5f39d392007-10-15 16:14:19 -04002301int btrfs_extend_item(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root, struct btrfs_path *path,
2303 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002304{
2305 int ret = 0;
2306 int slot;
2307 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 struct extent_buffer *leaf;
2309 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002310 u32 nritems;
2311 unsigned int data_end;
2312 unsigned int old_data;
2313 unsigned int old_size;
2314 int i;
2315
2316 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002317 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002318
Chris Mason5f39d392007-10-15 16:14:19 -04002319 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002320 data_end = leaf_data_end(root, leaf);
2321
Chris Mason5f39d392007-10-15 16:14:19 -04002322 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2323 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002324 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002325 }
Chris Mason6567e832007-04-16 09:22:45 -04002326 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002327 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002328
2329 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002330 if (slot >= nritems) {
2331 btrfs_print_leaf(root, leaf);
2332 printk("slot %d too large, nritems %d\n", slot, nritems);
2333 BUG_ON(1);
2334 }
Chris Mason6567e832007-04-16 09:22:45 -04002335
2336 /*
2337 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2338 */
2339 /* first correct the data pointers */
2340 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002341 u32 ioff;
2342 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002343
2344 if (!leaf->map_token) {
2345 map_extent_buffer(leaf, (unsigned long)item,
2346 sizeof(struct btrfs_item),
2347 &leaf->map_token, &leaf->kaddr,
2348 &leaf->map_start, &leaf->map_len,
2349 KM_USER1);
2350 }
Chris Mason5f39d392007-10-15 16:14:19 -04002351 ioff = btrfs_item_offset(leaf, item);
2352 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002353 }
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Chris Masondb945352007-10-15 16:15:53 -04002355 if (leaf->map_token) {
2356 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2357 leaf->map_token = NULL;
2358 }
2359
Chris Mason6567e832007-04-16 09:22:45 -04002360 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002361 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002362 data_end - data_size, btrfs_leaf_data(leaf) +
2363 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002364
Chris Mason6567e832007-04-16 09:22:45 -04002365 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002366 old_size = btrfs_item_size_nr(leaf, slot);
2367 item = btrfs_item_nr(leaf, slot);
2368 btrfs_set_item_size(leaf, item, old_size + data_size);
2369 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002370
2371 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002372 if (btrfs_leaf_free_space(root, leaf) < 0) {
2373 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002374 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002375 }
Chris Mason6567e832007-04-16 09:22:45 -04002376 return ret;
2377}
2378
Chris Mason74123bd2007-02-02 11:05:29 -05002379/*
2380 * Given a key and some data, insert an item into the tree.
2381 * This does all the path init required, making room in the tree if needed.
2382 */
Chris Mason9c583092008-01-29 15:15:18 -05002383int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002384 struct btrfs_root *root,
2385 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002386 struct btrfs_key *cpu_key, u32 *data_size,
2387 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002388{
Chris Mason5f39d392007-10-15 16:14:19 -04002389 struct extent_buffer *leaf;
2390 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002391 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002392 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002393 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002394 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002395 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002396 u32 total_size = 0;
2397 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002398 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002399 struct btrfs_disk_key disk_key;
2400
Chris Mason9c583092008-01-29 15:15:18 -05002401 for (i = 0; i < nr; i++) {
2402 total_data += data_size[i];
2403 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002404
Chris Mason74123bd2007-02-02 11:05:29 -05002405 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002406 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002407 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002408
Chris Mason9c583092008-01-29 15:15:18 -05002409 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2410 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002411 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002412 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002413 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002414 if (ret < 0)
2415 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416
Chris Mason62e27492007-03-15 12:56:47 -04002417 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002418 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002419
Chris Mason5f39d392007-10-15 16:14:19 -04002420 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002421 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002422
Chris Mason123abc82007-03-14 14:14:43 -04002423 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002424 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002425 btrfs_print_leaf(root, leaf);
2426 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002427 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002429 }
Chris Mason5f39d392007-10-15 16:14:19 -04002430
Chris Mason62e27492007-03-15 12:56:47 -04002431 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002432 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002433
Chris Masonbe0e5c02007-01-26 15:51:26 -05002434 if (slot != nritems) {
2435 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002436 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002437
Chris Mason5f39d392007-10-15 16:14:19 -04002438 if (old_data < data_end) {
2439 btrfs_print_leaf(root, leaf);
2440 printk("slot %d old_data %d data_end %d\n",
2441 slot, old_data, data_end);
2442 BUG_ON(1);
2443 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002444 /*
2445 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2446 */
2447 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002448 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002449 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002450 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002451
Chris Mason5f39d392007-10-15 16:14:19 -04002452 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002453 if (!leaf->map_token) {
2454 map_extent_buffer(leaf, (unsigned long)item,
2455 sizeof(struct btrfs_item),
2456 &leaf->map_token, &leaf->kaddr,
2457 &leaf->map_start, &leaf->map_len,
2458 KM_USER1);
2459 }
2460
Chris Mason5f39d392007-10-15 16:14:19 -04002461 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002462 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002463 }
Chris Masondb945352007-10-15 16:15:53 -04002464 if (leaf->map_token) {
2465 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2466 leaf->map_token = NULL;
2467 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002468
2469 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002470 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002471 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002472 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473
2474 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002475 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002476 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002477 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478 data_end = old_data;
2479 }
Chris Mason5f39d392007-10-15 16:14:19 -04002480
Chris Mason62e27492007-03-15 12:56:47 -04002481 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002482 for (i = 0; i < nr; i++) {
2483 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2484 btrfs_set_item_key(leaf, &disk_key, slot + i);
2485 item = btrfs_item_nr(leaf, slot + i);
2486 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2487 data_end -= data_size[i];
2488 btrfs_set_item_size(leaf, item, data_size[i]);
2489 }
2490 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002491 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002492
2493 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002494 if (slot == 0) {
2495 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002496 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002497 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002498
Chris Mason5f39d392007-10-15 16:14:19 -04002499 if (btrfs_leaf_free_space(root, leaf) < 0) {
2500 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002501 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002502 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002503
Chris Masoned2ff2c2007-03-01 18:59:40 -05002504out:
Chris Mason62e27492007-03-15 12:56:47 -04002505 return ret;
2506}
2507
2508/*
2509 * Given a key and some data, insert an item into the tree.
2510 * This does all the path init required, making room in the tree if needed.
2511 */
Chris Masone089f052007-03-16 16:20:31 -04002512int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2513 *root, struct btrfs_key *cpu_key, void *data, u32
2514 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002515{
2516 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002517 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002518 struct extent_buffer *leaf;
2519 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002520
Chris Mason2c90e5d2007-04-02 10:50:19 -04002521 path = btrfs_alloc_path();
2522 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002523 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002524 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002525 leaf = path->nodes[0];
2526 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2527 write_extent_buffer(leaf, data, ptr, data_size);
2528 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002529 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002530 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002531 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002532}
2533
Chris Mason74123bd2007-02-02 11:05:29 -05002534/*
Chris Mason5de08d72007-02-24 06:24:44 -05002535 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002536 *
2537 * If the delete empties a node, the node is removed from the tree,
2538 * continuing all the way the root if required. The root is converted into
2539 * a leaf if all the nodes are emptied.
2540 */
Chris Masone089f052007-03-16 16:20:31 -04002541static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2542 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002543{
Chris Mason5f39d392007-10-15 16:14:19 -04002544 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002545 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002546 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002547 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002548
Chris Mason5f39d392007-10-15 16:14:19 -04002549 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002550 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002551 memmove_extent_buffer(parent,
2552 btrfs_node_key_ptr_offset(slot),
2553 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002554 sizeof(struct btrfs_key_ptr) *
2555 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002556 }
Chris Mason7518a232007-03-12 12:01:18 -04002557 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002558 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002559 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002560 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002561 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002562 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002563 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002564 struct btrfs_disk_key disk_key;
2565
2566 btrfs_node_key(parent, &disk_key, 0);
2567 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002568 if (wret)
2569 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 }
Chris Masond6025572007-03-30 14:27:56 -04002571 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002572 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573}
2574
Chris Mason74123bd2007-02-02 11:05:29 -05002575/*
2576 * delete the item at the leaf level in path. If that empties
2577 * the leaf, remove it from the tree
2578 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002579int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2580 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002581{
Chris Mason5f39d392007-10-15 16:14:19 -04002582 struct extent_buffer *leaf;
2583 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002584 int last_off;
2585 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002586 int ret = 0;
2587 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002588 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002589 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002590
Chris Mason5f39d392007-10-15 16:14:19 -04002591 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002592 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2593
2594 for (i = 0; i < nr; i++)
2595 dsize += btrfs_item_size_nr(leaf, slot + i);
2596
Chris Mason5f39d392007-10-15 16:14:19 -04002597 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598
Chris Mason85e21ba2008-01-29 15:11:36 -05002599 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002601 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002602
2603 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002604 data_end + dsize,
2605 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002606 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002607
Chris Mason85e21ba2008-01-29 15:11:36 -05002608 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002609 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002610
Chris Mason5f39d392007-10-15 16:14:19 -04002611 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002612 if (!leaf->map_token) {
2613 map_extent_buffer(leaf, (unsigned long)item,
2614 sizeof(struct btrfs_item),
2615 &leaf->map_token, &leaf->kaddr,
2616 &leaf->map_start, &leaf->map_len,
2617 KM_USER1);
2618 }
Chris Mason5f39d392007-10-15 16:14:19 -04002619 ioff = btrfs_item_offset(leaf, item);
2620 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002621 }
Chris Masondb945352007-10-15 16:15:53 -04002622
2623 if (leaf->map_token) {
2624 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2625 leaf->map_token = NULL;
2626 }
2627
Chris Mason5f39d392007-10-15 16:14:19 -04002628 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002629 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002630 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002631 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002632 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002633 btrfs_set_header_nritems(leaf, nritems - nr);
2634 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002635
Chris Mason74123bd2007-02-02 11:05:29 -05002636 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002637 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002638 if (leaf == root->node) {
2639 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002640 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002641 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002642 clean_tree_block(trans, root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002643 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002644 if (wret)
2645 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002646 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002647 leaf->start, leaf->len,
2648 btrfs_header_owner(path->nodes[1]),
2649 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002650 if (wret)
2651 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002652 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002653 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002654 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002655 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002656 struct btrfs_disk_key disk_key;
2657
2658 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002659 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002660 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002661 if (wret)
2662 ret = wret;
2663 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002664
Chris Mason74123bd2007-02-02 11:05:29 -05002665 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002666 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002667 /* push_leaf_left fixes the path.
2668 * make sure the path still points to our leaf
2669 * for possible call to del_ptr below
2670 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002671 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002672 extent_buffer_get(leaf);
2673
Chris Mason85e21ba2008-01-29 15:11:36 -05002674 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002675 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002676 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002677
2678 if (path->nodes[0] == leaf &&
2679 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002680 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002681 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002682 ret = wret;
2683 }
Chris Mason5f39d392007-10-15 16:14:19 -04002684
2685 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002686 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002687 u64 bytenr = leaf->start;
2688 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002689
Chris Mason7bb86312007-12-11 09:25:06 -05002690 root_gen = btrfs_header_generation(
2691 path->nodes[1]);
2692
Chris Mason5f39d392007-10-15 16:14:19 -04002693 clean_tree_block(trans, root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002694
Chris Masone089f052007-03-16 16:20:31 -04002695 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002696 if (wret)
2697 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002698
2699 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002700 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002701 blocksize,
2702 btrfs_header_owner(path->nodes[1]),
2703 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002704 if (wret)
2705 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002706 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002707 btrfs_mark_buffer_dirty(leaf);
2708 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002709 }
Chris Masond5719762007-03-23 10:01:08 -04002710 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002711 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 }
2713 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002714 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002715}
2716
Chris Mason97571fd2007-02-24 13:39:08 -05002717/*
Chris Mason7bb86312007-12-11 09:25:06 -05002718 * walk up the tree as far as required to find the previous leaf.
2719 * returns 0 if it found something or 1 if there are no lesser leaves.
2720 * returns < 0 on io errors.
2721 */
2722int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2723{
Chris Mason8f662a72008-01-02 10:01:11 -05002724 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002725 int slot;
2726 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002727 struct extent_buffer *c;
2728 struct extent_buffer *next = NULL;
2729
2730 while(level < BTRFS_MAX_LEVEL) {
2731 if (!path->nodes[level])
2732 return 1;
2733
2734 slot = path->slots[level];
2735 c = path->nodes[level];
2736 if (slot == 0) {
2737 level++;
2738 if (level == BTRFS_MAX_LEVEL)
2739 return 1;
2740 continue;
2741 }
2742 slot--;
2743
2744 bytenr = btrfs_node_blockptr(c, slot);
2745 if (next)
2746 free_extent_buffer(next);
2747
Chris Mason7bb86312007-12-11 09:25:06 -05002748 next = read_tree_block(root, bytenr,
2749 btrfs_level_size(root, level - 1));
2750 break;
2751 }
2752 path->slots[level] = slot;
2753 while(1) {
2754 level--;
2755 c = path->nodes[level];
2756 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002757 slot = btrfs_header_nritems(next);
2758 if (slot != 0)
2759 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002760 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002761 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002762 if (!level)
2763 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002764 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002765 btrfs_level_size(root, level - 1));
2766 }
2767 return 0;
2768}
2769
2770/*
Chris Mason97571fd2007-02-24 13:39:08 -05002771 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002772 * returns 0 if it found something or 1 if there are no greater leaves.
2773 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002774 */
Chris Mason234b63a2007-03-13 10:46:10 -04002775int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002776{
2777 int slot;
2778 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002779 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002780 struct extent_buffer *c;
2781 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002782
Chris Mason234b63a2007-03-13 10:46:10 -04002783 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002784 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002785 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002786
Chris Masond97e63b2007-02-20 16:40:44 -05002787 slot = path->slots[level] + 1;
2788 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002789 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002790 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002791 if (level == BTRFS_MAX_LEVEL)
2792 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002793 continue;
2794 }
Chris Mason5f39d392007-10-15 16:14:19 -04002795
Chris Masondb945352007-10-15 16:15:53 -04002796 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002797 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002798 free_extent_buffer(next);
2799
Chris Mason6702ed42007-08-07 16:15:09 -04002800 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002801 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002802
Chris Masondb945352007-10-15 16:15:53 -04002803 next = read_tree_block(root, bytenr,
2804 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002805 break;
2806 }
2807 path->slots[level] = slot;
2808 while(1) {
2809 level--;
2810 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002811 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002812 path->nodes[level] = next;
2813 path->slots[level] = 0;
2814 if (!level)
2815 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002816 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002817 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002818 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2819 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002820 }
2821 return 0;
2822}
Chris Mason0b86a832008-03-24 15:01:56 -04002823
2824int btrfs_previous_item(struct btrfs_root *root,
2825 struct btrfs_path *path, u64 min_objectid,
2826 int type)
2827{
2828 struct btrfs_key found_key;
2829 struct extent_buffer *leaf;
2830 int ret;
2831
2832 while(1) {
2833 if (path->slots[0] == 0) {
2834 ret = btrfs_prev_leaf(root, path);
2835 if (ret != 0)
2836 return ret;
2837 } else {
2838 path->slots[0]--;
2839 }
2840 leaf = path->nodes[0];
2841 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2842 if (found_key.type == type)
2843 return 0;
2844 }
2845 return 1;
2846}
2847