blob: 1d404bde3cfa13fa70d1f474e136d4c8695091b7 [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 Masonca7a79a2008-05-12 12:59:19 -0400305 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400306 u64 search_start = *last_ret;
307 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400308 u64 other;
309 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400310 int end_slot;
311 int i;
312 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400313 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400314 int uptodate;
315 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500316 int progress_passed = 0;
317 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400318
Chris Mason5708b952007-10-25 15:43:18 -0400319 parent_level = btrfs_header_level(parent);
320 if (cache_only && parent_level != 1)
321 return 0;
322
Chris Mason6702ed42007-08-07 16:15:09 -0400323 if (trans->transaction != root->fs_info->running_transaction) {
324 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
325 root->fs_info->running_transaction->transid);
326 WARN_ON(1);
327 }
328 if (trans->transid != root->fs_info->generation) {
329 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
330 root->fs_info->generation);
331 WARN_ON(1);
332 }
Chris Mason86479a02007-09-10 19:58:16 -0400333
Chris Mason6b800532007-10-15 16:17:34 -0400334 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400335 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400336 end_slot = parent_nritems;
337
338 if (parent_nritems == 1)
339 return 0;
340
341 for (i = start_slot; i < end_slot; i++) {
342 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400343
Chris Mason5708b952007-10-25 15:43:18 -0400344 if (!parent->map_token) {
345 map_extent_buffer(parent,
346 btrfs_node_key_ptr_offset(i),
347 sizeof(struct btrfs_key_ptr),
348 &parent->map_token, &parent->kaddr,
349 &parent->map_start, &parent->map_len,
350 KM_USER1);
351 }
Chris Mason081e9572007-11-06 10:26:24 -0500352 btrfs_node_key(parent, &disk_key, i);
353 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
354 continue;
355
356 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400357 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400358 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400359 if (last_block == 0)
360 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400361
Chris Mason6702ed42007-08-07 16:15:09 -0400362 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400363 other = btrfs_node_blockptr(parent, i - 1);
364 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400365 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400366 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400367 other = btrfs_node_blockptr(parent, i + 1);
368 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400369 }
Chris Masone9d0b132007-08-10 14:06:19 -0400370 if (close) {
371 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400372 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400373 }
Chris Mason5708b952007-10-25 15:43:18 -0400374 if (parent->map_token) {
375 unmap_extent_buffer(parent, parent->map_token,
376 KM_USER1);
377 parent->map_token = NULL;
378 }
Chris Mason6702ed42007-08-07 16:15:09 -0400379
Chris Mason6b800532007-10-15 16:17:34 -0400380 cur = btrfs_find_tree_block(root, blocknr, blocksize);
381 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400382 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400383 else
384 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400385 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400386 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400387 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400388 continue;
389 }
Chris Mason6b800532007-10-15 16:17:34 -0400390 if (!cur) {
391 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400392 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400393 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400394 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400395 }
Chris Mason6702ed42007-08-07 16:15:09 -0400396 }
Chris Masone9d0b132007-08-10 14:06:19 -0400397 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400398 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400399
Chris Mason6b800532007-10-15 16:17:34 -0400400 err = __btrfs_cow_block(trans, root, cur, parent, i,
401 &tmp, search_start,
402 min(16 * blocksize,
403 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400404 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400405 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400406 break;
Yan252c38f2007-08-29 09:11:44 -0400407 }
Chris Mason6b800532007-10-15 16:17:34 -0400408 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400409 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400410 *last_ret = search_start;
411 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400412 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400413 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400414 }
Chris Mason5708b952007-10-25 15:43:18 -0400415 if (parent->map_token) {
416 unmap_extent_buffer(parent, parent->map_token,
417 KM_USER1);
418 parent->map_token = NULL;
419 }
Chris Mason6702ed42007-08-07 16:15:09 -0400420 return err;
421}
422
Chris Mason74123bd2007-02-02 11:05:29 -0500423/*
424 * The leaf data grows from end-to-front in the node.
425 * this returns the address of the start of the last item,
426 * which is the stop of the leaf data stack
427 */
Chris Mason123abc82007-03-14 14:14:43 -0400428static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400429 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500430{
Chris Mason5f39d392007-10-15 16:14:19 -0400431 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500432 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400433 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400434 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500435}
436
Chris Mason123abc82007-03-14 14:14:43 -0400437static int check_node(struct btrfs_root *root, struct btrfs_path *path,
438 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500439{
Chris Mason5f39d392007-10-15 16:14:19 -0400440 struct extent_buffer *parent = NULL;
441 struct extent_buffer *node = path->nodes[level];
442 struct btrfs_disk_key parent_key;
443 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500444 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400445 int slot;
446 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400447 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500448
449 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400450 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400451
Chris Mason8d7be552007-05-10 11:24:42 -0400452 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400453 BUG_ON(nritems == 0);
454 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400455 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400456 btrfs_node_key(parent, &parent_key, parent_slot);
457 btrfs_node_key(node, &node_key, 0);
458 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400459 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400460 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400461 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500462 }
Chris Mason123abc82007-03-14 14:14:43 -0400463 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400464 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400465 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
466 btrfs_node_key(node, &node_key, slot);
467 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400468 }
469 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
471 btrfs_node_key(node, &node_key, slot);
472 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500473 }
474 return 0;
475}
476
Chris Mason123abc82007-03-14 14:14:43 -0400477static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
478 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500479{
Chris Mason5f39d392007-10-15 16:14:19 -0400480 struct extent_buffer *leaf = path->nodes[level];
481 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500482 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400483 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400484 struct btrfs_disk_key parent_key;
485 struct btrfs_disk_key leaf_key;
486 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400487
Chris Mason5f39d392007-10-15 16:14:19 -0400488 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500489
490 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400491 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400492
493 if (nritems == 0)
494 return 0;
495
496 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400497 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400498 btrfs_node_key(parent, &parent_key, parent_slot);
499 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400500
Chris Mason5f39d392007-10-15 16:14:19 -0400501 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400502 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400503 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400504 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500505 }
Chris Mason5f39d392007-10-15 16:14:19 -0400506#if 0
507 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
508 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
509 btrfs_item_key(leaf, &leaf_key, i);
510 if (comp_keys(&leaf_key, &cpukey) >= 0) {
511 btrfs_print_leaf(root, leaf);
512 printk("slot %d offset bad key\n", i);
513 BUG_ON(1);
514 }
515 if (btrfs_item_offset_nr(leaf, i) !=
516 btrfs_item_end_nr(leaf, i + 1)) {
517 btrfs_print_leaf(root, leaf);
518 printk("slot %d offset bad\n", i);
519 BUG_ON(1);
520 }
521 if (i == 0) {
522 if (btrfs_item_offset_nr(leaf, i) +
523 btrfs_item_size_nr(leaf, i) !=
524 BTRFS_LEAF_DATA_SIZE(root)) {
525 btrfs_print_leaf(root, leaf);
526 printk("slot %d first offset bad\n", i);
527 BUG_ON(1);
528 }
529 }
530 }
531 if (nritems > 0) {
532 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
533 btrfs_print_leaf(root, leaf);
534 printk("slot %d bad size \n", nritems - 1);
535 BUG_ON(1);
536 }
537 }
538#endif
539 if (slot != 0 && slot < nritems - 1) {
540 btrfs_item_key(leaf, &leaf_key, slot);
541 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
542 if (comp_keys(&leaf_key, &cpukey) <= 0) {
543 btrfs_print_leaf(root, leaf);
544 printk("slot %d offset bad key\n", slot);
545 BUG_ON(1);
546 }
547 if (btrfs_item_offset_nr(leaf, slot - 1) !=
548 btrfs_item_end_nr(leaf, slot)) {
549 btrfs_print_leaf(root, leaf);
550 printk("slot %d offset bad\n", slot);
551 BUG_ON(1);
552 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553 }
Chris Mason8d7be552007-05-10 11:24:42 -0400554 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400555 btrfs_item_key(leaf, &leaf_key, slot);
556 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
557 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
558 if (btrfs_item_offset_nr(leaf, slot) !=
559 btrfs_item_end_nr(leaf, slot + 1)) {
560 btrfs_print_leaf(root, leaf);
561 printk("slot %d offset bad\n", slot);
562 BUG_ON(1);
563 }
Chris Mason8d7be552007-05-10 11:24:42 -0400564 }
Chris Mason5f39d392007-10-15 16:14:19 -0400565 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
566 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500567 return 0;
568}
569
Chris Mason98ed5172008-01-03 10:01:48 -0500570static int noinline check_block(struct btrfs_root *root,
571 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500572{
Chris Masonf1885912008-04-09 16:28:12 -0400573 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400574 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400575 if (btrfs_header_level(path->nodes[level]) != level)
576 printk("warning: bad level %Lu wanted %d found %d\n",
577 path->nodes[level]->start, level,
578 btrfs_header_level(path->nodes[level]));
579 found_start = btrfs_header_bytenr(path->nodes[level]);
580 if (found_start != path->nodes[level]->start) {
581 printk("warning: bad bytentr %Lu found %Lu\n",
582 path->nodes[level]->start, found_start);
583 }
Chris Masondb945352007-10-15 16:15:53 -0400584#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400585 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400586
Chris Mason479965d2007-10-15 16:14:27 -0400587 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
588 (unsigned long)btrfs_header_fsid(buf),
589 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400590 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400591 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400592 }
Chris Masondb945352007-10-15 16:15:53 -0400593#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500594 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400595 return check_leaf(root, path, level);
596 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500597}
598
Chris Mason74123bd2007-02-02 11:05:29 -0500599/*
Chris Mason5f39d392007-10-15 16:14:19 -0400600 * search for key in the extent_buffer. The items start at offset p,
601 * and they are item_size apart. There are 'max' items in p.
602 *
Chris Mason74123bd2007-02-02 11:05:29 -0500603 * the slot in the array is returned via slot, and it points to
604 * the place where you would insert key if it is not found in
605 * the array.
606 *
607 * slot may point to max if the key is bigger than all of the keys
608 */
Chris Mason5f39d392007-10-15 16:14:19 -0400609static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
610 int item_size, struct btrfs_key *key,
611 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500612{
613 int low = 0;
614 int high = max;
615 int mid;
616 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400617 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400618 struct btrfs_disk_key unaligned;
619 unsigned long offset;
620 char *map_token = NULL;
621 char *kaddr = NULL;
622 unsigned long map_start = 0;
623 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400624 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500625
626 while(low < high) {
627 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400628 offset = p + mid * item_size;
629
630 if (!map_token || offset < map_start ||
631 (offset + sizeof(struct btrfs_disk_key)) >
632 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400633 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400634 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400635 map_token = NULL;
636 }
637 err = map_extent_buffer(eb, offset,
638 sizeof(struct btrfs_disk_key),
639 &map_token, &kaddr,
640 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400641
Chris Mason479965d2007-10-15 16:14:27 -0400642 if (!err) {
643 tmp = (struct btrfs_disk_key *)(kaddr + offset -
644 map_start);
645 } else {
646 read_extent_buffer(eb, &unaligned,
647 offset, sizeof(unaligned));
648 tmp = &unaligned;
649 }
650
Chris Mason5f39d392007-10-15 16:14:19 -0400651 } else {
652 tmp = (struct btrfs_disk_key *)(kaddr + offset -
653 map_start);
654 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500655 ret = comp_keys(tmp, key);
656
657 if (ret < 0)
658 low = mid + 1;
659 else if (ret > 0)
660 high = mid;
661 else {
662 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400663 if (map_token)
664 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500665 return 0;
666 }
667 }
668 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400669 if (map_token)
670 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500671 return 1;
672}
673
Chris Mason97571fd2007-02-24 13:39:08 -0500674/*
675 * simple bin_search frontend that does the right thing for
676 * leaves vs nodes
677 */
Chris Mason5f39d392007-10-15 16:14:19 -0400678static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
679 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500680{
Chris Mason5f39d392007-10-15 16:14:19 -0400681 if (level == 0) {
682 return generic_bin_search(eb,
683 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400684 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400685 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400686 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500687 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400688 return generic_bin_search(eb,
689 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400690 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400691 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400692 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500693 }
694 return -1;
695}
696
Chris Mason5f39d392007-10-15 16:14:19 -0400697static struct extent_buffer *read_node_slot(struct btrfs_root *root,
698 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500699{
Chris Masonca7a79a2008-05-12 12:59:19 -0400700 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500701 if (slot < 0)
702 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400703 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500704 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400705
706 BUG_ON(level == 0);
707
Chris Masondb945352007-10-15 16:15:53 -0400708 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400709 btrfs_level_size(root, level - 1),
710 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500711}
712
Chris Mason98ed5172008-01-03 10:01:48 -0500713static int balance_level(struct btrfs_trans_handle *trans,
714 struct btrfs_root *root,
715 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500716{
Chris Mason5f39d392007-10-15 16:14:19 -0400717 struct extent_buffer *right = NULL;
718 struct extent_buffer *mid;
719 struct extent_buffer *left = NULL;
720 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500721 int ret = 0;
722 int wret;
723 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500724 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400725 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500726 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500727
728 if (level == 0)
729 return 0;
730
Chris Mason5f39d392007-10-15 16:14:19 -0400731 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500732 WARN_ON(btrfs_header_generation(mid) != trans->transid);
733
Chris Mason1d4f8a02007-03-13 09:28:32 -0400734 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500735
Chris Mason234b63a2007-03-13 10:46:10 -0400736 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400737 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500738 pslot = path->slots[level + 1];
739
Chris Mason40689472007-03-17 14:29:23 -0400740 /*
741 * deal with the case where there is only one pointer in the root
742 * by promoting the node below to a root
743 */
Chris Mason5f39d392007-10-15 16:14:19 -0400744 if (!parent) {
745 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500746
Chris Mason5f39d392007-10-15 16:14:19 -0400747 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500748 return 0;
749
750 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400751 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500752 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500753 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
754 BUG_ON(ret);
755
Chris Masonbb803952007-03-01 12:04:21 -0500756 root->node = child;
Chris Mason0b86a832008-03-24 15:01:56 -0400757 add_root_to_dirty_list(root);
Chris Masonbb803952007-03-01 12:04:21 -0500758 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400759 clean_tree_block(trans, root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500760 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400761 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500762 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
763 root->root_key.objectid,
764 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500765 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400766 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400767 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500768 }
Chris Mason5f39d392007-10-15 16:14:19 -0400769 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400770 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500771 return 0;
772
Chris Mason5f39d392007-10-15 16:14:19 -0400773 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400774 err_on_enospc = 1;
775
Chris Mason5f39d392007-10-15 16:14:19 -0400776 left = read_node_slot(root, parent, pslot - 1);
777 if (left) {
778 wret = btrfs_cow_block(trans, root, left,
779 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400780 if (wret) {
781 ret = wret;
782 goto enospc;
783 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400784 }
Chris Mason5f39d392007-10-15 16:14:19 -0400785 right = read_node_slot(root, parent, pslot + 1);
786 if (right) {
787 wret = btrfs_cow_block(trans, root, right,
788 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400789 if (wret) {
790 ret = wret;
791 goto enospc;
792 }
793 }
794
795 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400796 if (left) {
797 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400798 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500799 if (wret < 0)
800 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400801 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400802 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500803 }
Chris Mason79f95c82007-03-01 15:16:26 -0500804
805 /*
806 * then try to empty the right most buffer into the middle
807 */
Chris Mason5f39d392007-10-15 16:14:19 -0400808 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400809 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400810 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500811 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400813 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500814 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400815 u32 blocksize = right->len;
816
Chris Mason5f39d392007-10-15 16:14:19 -0400817 clean_tree_block(trans, root, right);
Chris Mason5f39d392007-10-15 16:14:19 -0400818 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500819 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400820 wret = del_ptr(trans, root, path, level + 1, pslot +
821 1);
Chris Masonbb803952007-03-01 12:04:21 -0500822 if (wret)
823 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400824 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500825 blocksize,
826 btrfs_header_owner(parent),
827 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500828 if (wret)
829 ret = wret;
830 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400831 struct btrfs_disk_key right_key;
832 btrfs_node_key(right, &right_key, 0);
833 btrfs_set_node_key(parent, &right_key, pslot + 1);
834 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500835 }
836 }
Chris Mason5f39d392007-10-15 16:14:19 -0400837 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500838 /*
839 * we're not allowed to leave a node with one item in the
840 * tree during a delete. A deletion from lower in the tree
841 * could try to delete the only pointer in this node.
842 * So, pull some keys from the left.
843 * There has to be a left pointer at this point because
844 * otherwise we would have pulled some pointers from the
845 * right
846 */
Chris Mason5f39d392007-10-15 16:14:19 -0400847 BUG_ON(!left);
848 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400849 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500850 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400851 goto enospc;
852 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400853 if (wret == 1) {
854 wret = push_node_left(trans, root, left, mid, 1);
855 if (wret < 0)
856 ret = wret;
857 }
Chris Mason79f95c82007-03-01 15:16:26 -0500858 BUG_ON(wret == 1);
859 }
Chris Mason5f39d392007-10-15 16:14:19 -0400860 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500861 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500862 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400863 u64 bytenr = mid->start;
864 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400865 clean_tree_block(trans, root, mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400866 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500867 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400868 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500869 if (wret)
870 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500871 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
872 btrfs_header_owner(parent),
873 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500874 if (wret)
875 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500876 } else {
877 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct btrfs_disk_key mid_key;
879 btrfs_node_key(mid, &mid_key, 0);
880 btrfs_set_node_key(parent, &mid_key, pslot);
881 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500882 }
Chris Masonbb803952007-03-01 12:04:21 -0500883
Chris Mason79f95c82007-03-01 15:16:26 -0500884 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400885 if (left) {
886 if (btrfs_header_nritems(left) > orig_slot) {
887 extent_buffer_get(left);
888 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500889 path->slots[level + 1] -= 1;
890 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400891 if (mid)
892 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500893 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400894 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500895 path->slots[level] = orig_slot;
896 }
897 }
Chris Mason79f95c82007-03-01 15:16:26 -0500898 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400899 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400900 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400901 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500902 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400903enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400904 if (right)
905 free_extent_buffer(right);
906 if (left)
907 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500908 return ret;
909}
910
Chris Masone66f7092007-04-20 13:16:02 -0400911/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500912static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400915{
Chris Mason5f39d392007-10-15 16:14:19 -0400916 struct extent_buffer *right = NULL;
917 struct extent_buffer *mid;
918 struct extent_buffer *left = NULL;
919 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400920 int ret = 0;
921 int wret;
922 int pslot;
923 int orig_slot = path->slots[level];
924 u64 orig_ptr;
925
926 if (level == 0)
927 return 1;
928
Chris Mason5f39d392007-10-15 16:14:19 -0400929 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500930 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400931 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
932
933 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400934 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400935 pslot = path->slots[level + 1];
936
Chris Mason5f39d392007-10-15 16:14:19 -0400937 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400938 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400939
Chris Mason5f39d392007-10-15 16:14:19 -0400940 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400941
942 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400943 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400944 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400945 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400946 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
947 wret = 1;
948 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400949 ret = btrfs_cow_block(trans, root, left, parent,
950 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400951 if (ret)
952 wret = 1;
953 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400954 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -0400955 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400956 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400957 }
Chris Masone66f7092007-04-20 13:16:02 -0400958 if (wret < 0)
959 ret = wret;
960 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400961 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400962 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400963 btrfs_node_key(mid, &disk_key, 0);
964 btrfs_set_node_key(parent, &disk_key, pslot);
965 btrfs_mark_buffer_dirty(parent);
966 if (btrfs_header_nritems(left) > orig_slot) {
967 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400968 path->slots[level + 1] -= 1;
969 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400970 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400971 } else {
972 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400973 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400974 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400975 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400976 }
Chris Masone66f7092007-04-20 13:16:02 -0400977 return 0;
978 }
Chris Mason5f39d392007-10-15 16:14:19 -0400979 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400980 }
Chris Mason5f39d392007-10-15 16:14:19 -0400981 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400982
983 /*
984 * then try to empty the right most buffer into the middle
985 */
Chris Mason5f39d392007-10-15 16:14:19 -0400986 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400987 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400988 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400989 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
990 wret = 1;
991 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400992 ret = btrfs_cow_block(trans, root, right,
993 parent, pslot + 1,
994 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400995 if (ret)
996 wret = 1;
997 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400998 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400999 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001001 }
Chris Masone66f7092007-04-20 13:16:02 -04001002 if (wret < 0)
1003 ret = wret;
1004 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001005 struct btrfs_disk_key disk_key;
1006
1007 btrfs_node_key(right, &disk_key, 0);
1008 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1009 btrfs_mark_buffer_dirty(parent);
1010
1011 if (btrfs_header_nritems(mid) <= orig_slot) {
1012 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001013 path->slots[level + 1] += 1;
1014 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001015 btrfs_header_nritems(mid);
1016 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001017 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001018 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001019 }
Chris Masone66f7092007-04-20 13:16:02 -04001020 return 0;
1021 }
Chris Mason5f39d392007-10-15 16:14:19 -04001022 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001023 }
Chris Masone66f7092007-04-20 13:16:02 -04001024 return 1;
1025}
1026
Chris Mason74123bd2007-02-02 11:05:29 -05001027/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001028 * readahead one full node of leaves
1029 */
1030static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001031 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001032{
Chris Mason5f39d392007-10-15 16:14:19 -04001033 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001034 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001035 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001036 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001037 u64 lowest_read;
1038 u64 highest_read;
1039 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001040 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001041 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001042 u32 nr;
1043 u32 blocksize;
1044 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001045
Chris Masona6b6e752007-10-15 16:22:39 -04001046 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001047 return;
1048
Chris Mason6702ed42007-08-07 16:15:09 -04001049 if (!path->nodes[level])
1050 return;
1051
Chris Mason5f39d392007-10-15 16:14:19 -04001052 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001053 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001054 blocksize = btrfs_level_size(root, level - 1);
1055 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 if (eb) {
1057 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001058 return;
1059 }
1060
Chris Mason6b800532007-10-15 16:17:34 -04001061 highest_read = search;
1062 lowest_read = search;
1063
Chris Mason5f39d392007-10-15 16:14:19 -04001064 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001065 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001066 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001067 if (direction < 0) {
1068 if (nr == 0)
1069 break;
1070 nr--;
1071 } else if (direction > 0) {
1072 nr++;
1073 if (nr >= nritems)
1074 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001075 }
Chris Mason01f46652007-12-21 16:24:26 -05001076 if (path->reada < 0 && objectid) {
1077 btrfs_node_key(node, &disk_key, nr);
1078 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1079 break;
1080 }
Chris Mason6b800532007-10-15 16:17:34 -04001081 search = btrfs_node_blockptr(node, nr);
1082 if ((search >= lowest_read && search <= highest_read) ||
1083 (search < lowest_read && lowest_read - search <= 32768) ||
1084 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001085 readahead_tree_block(root, search, blocksize,
1086 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001087 nread += blocksize;
1088 }
1089 nscan++;
1090 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1091 break;
1092 if(nread > (1024 * 1024) || nscan > 128)
1093 break;
1094
1095 if (search < lowest_read)
1096 lowest_read = search;
1097 if (search > highest_read)
1098 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001099 }
1100}
1101/*
Chris Mason74123bd2007-02-02 11:05:29 -05001102 * look for key in the tree. path is filled in with nodes along the way
1103 * if key is found, we return zero and you can find the item in the leaf
1104 * level of the path (level 0)
1105 *
1106 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001107 * be inserted, and 1 is returned. If there are other errors during the
1108 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001109 *
1110 * if ins_len > 0, nodes and leaves will be split as we walk down the
1111 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1112 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001113 */
Chris Masone089f052007-03-16 16:20:31 -04001114int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1115 *root, struct btrfs_key *key, struct btrfs_path *p, int
1116 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001117{
Chris Mason5f39d392007-10-15 16:14:19 -04001118 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001119 int slot;
1120 int ret;
1121 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001122 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001123 u8 lowest_level = 0;
1124
Chris Mason6702ed42007-08-07 16:15:09 -04001125 lowest_level = p->lowest_level;
1126 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001127 WARN_ON(p->nodes[0] != NULL);
1128 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001129again:
1130 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001131 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001132 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001133 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001134 if (cow) {
1135 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001136 wret = btrfs_cow_block(trans, root, b,
1137 p->nodes[level + 1],
1138 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001139 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001140 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001141 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001142 return wret;
1143 }
Chris Mason02217ed2007-03-02 16:08:05 -05001144 }
1145 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001146 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001147 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001148 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001149 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001150 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001151 if (ret)
1152 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001153 ret = bin_search(b, key, level, &slot);
1154 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001155 if (ret && slot > 0)
1156 slot -= 1;
1157 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001159 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001160 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001161 BUG_ON(sret > 0);
1162 if (sret)
1163 return sret;
1164 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001165 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001166 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001167 int sret = balance_level(trans, root, p,
1168 level);
Chris Masonbb803952007-03-01 12:04:21 -05001169 if (sret)
1170 return sret;
1171 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001172 if (!b) {
1173 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001174 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001175 }
Chris Masonbb803952007-03-01 12:04:21 -05001176 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001177 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001178 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001179 /* this is only true while dropping a snapshot */
1180 if (level == lowest_level)
1181 break;
Chris Masonca7a79a2008-05-12 12:59:19 -04001182
Chris Mason6702ed42007-08-07 16:15:09 -04001183 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001184 reada_for_search(root, p, level, slot,
1185 key->objectid);
Chris Masonca7a79a2008-05-12 12:59:19 -04001186
1187 b = read_node_slot(root, b, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001188 } else {
1189 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001191 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001192 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001193 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001194 BUG_ON(sret > 0);
1195 if (sret)
1196 return sret;
1197 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001198 return ret;
1199 }
1200 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001201 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001202}
1203
Chris Mason74123bd2007-02-02 11:05:29 -05001204/*
1205 * adjust the pointers going up the tree, starting at level
1206 * making sure the right key of each node is points to 'key'.
1207 * This is used after shifting pointers to the left, so it stops
1208 * fixing up pointers when a given leaf/node is not in slot 0 of the
1209 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001210 *
1211 * If this fails to write a tree block, it returns -1, but continues
1212 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001213 */
Chris Mason5f39d392007-10-15 16:14:19 -04001214static int fixup_low_keys(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root, struct btrfs_path *path,
1216 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001217{
1218 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001219 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001220 struct extent_buffer *t;
1221
Chris Mason234b63a2007-03-13 10:46:10 -04001222 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001223 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001224 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001225 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001226 t = path->nodes[i];
1227 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001228 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001229 if (tslot != 0)
1230 break;
1231 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001232 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001233}
1234
Chris Mason74123bd2007-02-02 11:05:29 -05001235/*
1236 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001237 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001238 *
1239 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1240 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001241 */
Chris Mason98ed5172008-01-03 10:01:48 -05001242static int push_node_left(struct btrfs_trans_handle *trans,
1243 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001244 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001245{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001246 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001247 int src_nritems;
1248 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001249 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001250
Chris Mason5f39d392007-10-15 16:14:19 -04001251 src_nritems = btrfs_header_nritems(src);
1252 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001253 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001254 WARN_ON(btrfs_header_generation(src) != trans->transid);
1255 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001256
Chris Masonbce4eae2008-04-24 14:42:46 -04001257 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001258 return 1;
1259
Chris Masoneb60cea2007-02-02 09:18:22 -05001260 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001261 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001262 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001263
Chris Masonbce4eae2008-04-24 14:42:46 -04001264 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001265 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001266 if (push_items < src_nritems) {
1267 /* leave at least 8 pointers in the node if
1268 * we aren't going to empty it
1269 */
1270 if (src_nritems - push_items < 8) {
1271 if (push_items <= 8)
1272 return 1;
1273 push_items -= 8;
1274 }
1275 }
1276 } else
1277 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001278
Chris Mason5f39d392007-10-15 16:14:19 -04001279 copy_extent_buffer(dst, src,
1280 btrfs_node_key_ptr_offset(dst_nritems),
1281 btrfs_node_key_ptr_offset(0),
1282 push_items * sizeof(struct btrfs_key_ptr));
1283
Chris Masonbb803952007-03-01 12:04:21 -05001284 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001285 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1286 btrfs_node_key_ptr_offset(push_items),
1287 (src_nritems - push_items) *
1288 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001289 }
Chris Mason5f39d392007-10-15 16:14:19 -04001290 btrfs_set_header_nritems(src, src_nritems - push_items);
1291 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1292 btrfs_mark_buffer_dirty(src);
1293 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001294 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001295}
1296
Chris Mason97571fd2007-02-24 13:39:08 -05001297/*
Chris Mason79f95c82007-03-01 15:16:26 -05001298 * try to push data from one node into the next node right in the
1299 * tree.
1300 *
1301 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1302 * error, and > 0 if there was no room in the right hand block.
1303 *
1304 * this will only push up to 1/2 the contents of the left node over
1305 */
Chris Mason5f39d392007-10-15 16:14:19 -04001306static int balance_node_right(struct btrfs_trans_handle *trans,
1307 struct btrfs_root *root,
1308 struct extent_buffer *dst,
1309 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001310{
Chris Mason79f95c82007-03-01 15:16:26 -05001311 int push_items = 0;
1312 int max_push;
1313 int src_nritems;
1314 int dst_nritems;
1315 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001316
Chris Mason7bb86312007-12-11 09:25:06 -05001317 WARN_ON(btrfs_header_generation(src) != trans->transid);
1318 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1319
Chris Mason5f39d392007-10-15 16:14:19 -04001320 src_nritems = btrfs_header_nritems(src);
1321 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001322 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001323 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001324 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001325 }
1326
1327 if (src_nritems < 4) {
1328 return 1;
1329 }
Chris Mason79f95c82007-03-01 15:16:26 -05001330
1331 max_push = src_nritems / 2 + 1;
1332 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001333 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001334 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001335 }
Yan252c38f2007-08-29 09:11:44 -04001336
Chris Mason79f95c82007-03-01 15:16:26 -05001337 if (max_push < push_items)
1338 push_items = max_push;
1339
Chris Mason5f39d392007-10-15 16:14:19 -04001340 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1341 btrfs_node_key_ptr_offset(0),
1342 (dst_nritems) *
1343 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001344
Chris Mason5f39d392007-10-15 16:14:19 -04001345 copy_extent_buffer(dst, src,
1346 btrfs_node_key_ptr_offset(0),
1347 btrfs_node_key_ptr_offset(src_nritems - push_items),
1348 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001349
Chris Mason5f39d392007-10-15 16:14:19 -04001350 btrfs_set_header_nritems(src, src_nritems - push_items);
1351 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001352
Chris Mason5f39d392007-10-15 16:14:19 -04001353 btrfs_mark_buffer_dirty(src);
1354 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001355 return ret;
1356}
1357
1358/*
Chris Mason97571fd2007-02-24 13:39:08 -05001359 * helper function to insert a new root level in the tree.
1360 * A new node is allocated, and a single item is inserted to
1361 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001362 *
1363 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001364 */
Chris Mason98ed5172008-01-03 10:01:48 -05001365static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001366 struct btrfs_root *root,
1367 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001368{
Chris Mason7bb86312007-12-11 09:25:06 -05001369 u64 root_gen;
1370 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001371 struct extent_buffer *lower;
1372 struct extent_buffer *c;
1373 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001374
1375 BUG_ON(path->nodes[level]);
1376 BUG_ON(path->nodes[level-1] != root->node);
1377
Chris Mason7bb86312007-12-11 09:25:06 -05001378 if (root->ref_cows)
1379 root_gen = trans->transid;
1380 else
1381 root_gen = 0;
1382
1383 lower = path->nodes[level-1];
1384 if (level == 1)
1385 btrfs_item_key(lower, &lower_key, 0);
1386 else
1387 btrfs_node_key(lower, &lower_key, 0);
1388
1389 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1390 root->root_key.objectid,
1391 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001392 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001393 if (IS_ERR(c))
1394 return PTR_ERR(c);
1395 memset_extent_buffer(c, 0, 0, root->nodesize);
1396 btrfs_set_header_nritems(c, 1);
1397 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001398 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001399 btrfs_set_header_generation(c, trans->transid);
1400 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001401
Chris Mason5f39d392007-10-15 16:14:19 -04001402 write_extent_buffer(c, root->fs_info->fsid,
1403 (unsigned long)btrfs_header_fsid(c),
1404 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001405
1406 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1407 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1408 BTRFS_UUID_SIZE);
1409
Chris Mason5f39d392007-10-15 16:14:19 -04001410 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001411 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001412 lower_gen = btrfs_header_generation(lower);
1413 WARN_ON(lower_gen == 0);
1414
1415 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001416
1417 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001418
Chris Mason5c680ed2007-02-22 11:39:13 -05001419 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001420 free_extent_buffer(root->node);
1421 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001422 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001423 extent_buffer_get(c);
1424 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001425 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001426
1427 if (root->ref_cows && lower_gen != trans->transid) {
1428 struct btrfs_path *back_path = btrfs_alloc_path();
1429 int ret;
1430 ret = btrfs_insert_extent_backref(trans,
1431 root->fs_info->extent_root,
1432 path, lower->start,
1433 root->root_key.objectid,
1434 trans->transid, 0, 0);
1435 BUG_ON(ret);
1436 btrfs_free_path(back_path);
1437 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001438 return 0;
1439}
1440
Chris Mason74123bd2007-02-02 11:05:29 -05001441/*
1442 * worker function to insert a single pointer in a node.
1443 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001444 *
Chris Mason74123bd2007-02-02 11:05:29 -05001445 * slot and level indicate where you want the key to go, and
1446 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001447 *
1448 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001449 */
Chris Masone089f052007-03-16 16:20:31 -04001450static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1451 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001452 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001453{
Chris Mason5f39d392007-10-15 16:14:19 -04001454 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001455 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001456
1457 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001458 lower = path->nodes[level];
1459 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001460 if (slot > nritems)
1461 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001462 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001463 BUG();
1464 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001465 memmove_extent_buffer(lower,
1466 btrfs_node_key_ptr_offset(slot + 1),
1467 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001468 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001469 }
Chris Mason5f39d392007-10-15 16:14:19 -04001470 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001471 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001472 WARN_ON(trans->transid == 0);
1473 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 btrfs_set_header_nritems(lower, nritems + 1);
1475 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001476 return 0;
1477}
1478
Chris Mason97571fd2007-02-24 13:39:08 -05001479/*
1480 * split the node at the specified level in path in two.
1481 * The path is corrected to point to the appropriate node after the split
1482 *
1483 * Before splitting this tries to make some room in the node by pushing
1484 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001485 *
1486 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001487 */
Chris Masone089f052007-03-16 16:20:31 -04001488static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1489 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001490{
Chris Mason7bb86312007-12-11 09:25:06 -05001491 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001492 struct extent_buffer *c;
1493 struct extent_buffer *split;
1494 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001495 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001496 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001497 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001498 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001499
Chris Mason5f39d392007-10-15 16:14:19 -04001500 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001501 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001502 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001503 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001504 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001505 if (ret)
1506 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001507 } else {
1508 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001509 c = path->nodes[level];
1510 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001511 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001512 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001513 if (ret < 0)
1514 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001515 }
Chris Masone66f7092007-04-20 13:16:02 -04001516
Chris Mason5f39d392007-10-15 16:14:19 -04001517 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001518 if (root->ref_cows)
1519 root_gen = trans->transid;
1520 else
1521 root_gen = 0;
1522
1523 btrfs_node_key(c, &disk_key, 0);
1524 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1525 root->root_key.objectid,
1526 root_gen,
1527 btrfs_disk_key_objectid(&disk_key),
1528 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001529 if (IS_ERR(split))
1530 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001531
Chris Mason5f39d392007-10-15 16:14:19 -04001532 btrfs_set_header_flags(split, btrfs_header_flags(c));
1533 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001534 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001535 btrfs_set_header_generation(split, trans->transid);
1536 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001537 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001538 write_extent_buffer(split, root->fs_info->fsid,
1539 (unsigned long)btrfs_header_fsid(split),
1540 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001541 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1542 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1543 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001544
Chris Mason7518a232007-03-12 12:01:18 -04001545 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001546
1547 copy_extent_buffer(split, c,
1548 btrfs_node_key_ptr_offset(0),
1549 btrfs_node_key_ptr_offset(mid),
1550 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1551 btrfs_set_header_nritems(split, c_nritems - mid);
1552 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001553 ret = 0;
1554
Chris Mason5f39d392007-10-15 16:14:19 -04001555 btrfs_mark_buffer_dirty(c);
1556 btrfs_mark_buffer_dirty(split);
1557
1558 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001559 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001560 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001561 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001562 if (wret)
1563 ret = wret;
1564
Chris Mason5de08d72007-02-24 06:24:44 -05001565 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001566 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001567 free_extent_buffer(c);
1568 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001569 path->slots[level + 1] += 1;
1570 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001571 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001572 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001573 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574}
1575
Chris Mason74123bd2007-02-02 11:05:29 -05001576/*
1577 * how many bytes are required to store the items in a leaf. start
1578 * and nr indicate which items in the leaf to check. This totals up the
1579 * space used both by the item structs and the item data
1580 */
Chris Mason5f39d392007-10-15 16:14:19 -04001581static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001582{
1583 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001584 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001585 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001586
1587 if (!nr)
1588 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001589 data_len = btrfs_item_end_nr(l, start);
1590 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001591 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001592 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001593 return data_len;
1594}
1595
Chris Mason74123bd2007-02-02 11:05:29 -05001596/*
Chris Masond4dbff92007-04-04 14:08:15 -04001597 * The space between the end of the leaf items and
1598 * the start of the leaf data. IOW, how much room
1599 * the leaf has left for both items and data
1600 */
Chris Mason5f39d392007-10-15 16:14:19 -04001601int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001602{
Chris Mason5f39d392007-10-15 16:14:19 -04001603 int nritems = btrfs_header_nritems(leaf);
1604 int ret;
1605 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1606 if (ret < 0) {
1607 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001608 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001609 leaf_space_used(leaf, 0, nritems), nritems);
1610 }
1611 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001612}
1613
1614/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001615 * push some data in the path leaf to the right, trying to free up at
1616 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001617 *
1618 * returns 1 if the push failed because the other node didn't have enough
1619 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001620 */
Chris Masone089f052007-03-16 16:20:31 -04001621static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001622 *root, struct btrfs_path *path, int data_size,
1623 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001624{
Chris Mason5f39d392007-10-15 16:14:19 -04001625 struct extent_buffer *left = path->nodes[0];
1626 struct extent_buffer *right;
1627 struct extent_buffer *upper;
1628 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001629 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001630 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001631 int free_space;
1632 int push_space = 0;
1633 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001634 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001635 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001636 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001637 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001639 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001640 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001641
1642 slot = path->slots[1];
1643 if (!path->nodes[1]) {
1644 return 1;
1645 }
1646 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001647 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001648 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001649
Chris Masonca7a79a2008-05-12 12:59:19 -04001650 right = read_node_slot(root, upper, slot + 1);
Chris Mason123abc82007-03-14 14:14:43 -04001651 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001652 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001653 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001654 return 1;
1655 }
1656
Chris Mason5f39d392007-10-15 16:14:19 -04001657 /* cow and double check */
1658 ret = btrfs_cow_block(trans, root, right, upper,
1659 slot + 1, &right);
1660 if (ret) {
1661 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001662 return 1;
1663 }
Chris Mason5f39d392007-10-15 16:14:19 -04001664 free_space = btrfs_leaf_free_space(root, right);
1665 if (free_space < data_size + sizeof(struct btrfs_item)) {
1666 free_extent_buffer(right);
1667 return 1;
1668 }
1669
1670 left_nritems = btrfs_header_nritems(left);
1671 if (left_nritems == 0) {
1672 free_extent_buffer(right);
1673 return 1;
1674 }
1675
Chris Mason34a38212007-11-07 13:31:03 -05001676 if (empty)
1677 nr = 0;
1678 else
1679 nr = 1;
1680
1681 i = left_nritems - 1;
1682 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001683 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001684
Chris Mason00ec4c52007-02-24 12:47:20 -05001685 if (path->slots[0] == i)
1686 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001687
1688 if (!left->map_token) {
1689 map_extent_buffer(left, (unsigned long)item,
1690 sizeof(struct btrfs_item),
1691 &left->map_token, &left->kaddr,
1692 &left->map_start, &left->map_len,
1693 KM_USER1);
1694 }
1695
1696 this_item_size = btrfs_item_size(left, item);
1697 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001698 break;
1699 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001700 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001701 if (i == 0)
1702 break;
1703 i--;
Chris Masondb945352007-10-15 16:15:53 -04001704 }
1705 if (left->map_token) {
1706 unmap_extent_buffer(left, left->map_token, KM_USER1);
1707 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001708 }
Chris Mason5f39d392007-10-15 16:14:19 -04001709
Chris Mason00ec4c52007-02-24 12:47:20 -05001710 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001711 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001712 return 1;
1713 }
Chris Mason5f39d392007-10-15 16:14:19 -04001714
Chris Mason34a38212007-11-07 13:31:03 -05001715 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001716 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001717
Chris Mason00ec4c52007-02-24 12:47:20 -05001718 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001719 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001720
Chris Mason5f39d392007-10-15 16:14:19 -04001721 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001722 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001723
Chris Mason00ec4c52007-02-24 12:47:20 -05001724 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001725 data_end = leaf_data_end(root, right);
1726 memmove_extent_buffer(right,
1727 btrfs_leaf_data(right) + data_end - push_space,
1728 btrfs_leaf_data(right) + data_end,
1729 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1730
Chris Mason00ec4c52007-02-24 12:47:20 -05001731 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001732 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001733 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1734 btrfs_leaf_data(left) + leaf_data_end(root, left),
1735 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001736
1737 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1738 btrfs_item_nr_offset(0),
1739 right_nritems * sizeof(struct btrfs_item));
1740
Chris Mason00ec4c52007-02-24 12:47:20 -05001741 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001742 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1743 btrfs_item_nr_offset(left_nritems - push_items),
1744 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001745
1746 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001747 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001748 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001749 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001750 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001751 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001752 if (!right->map_token) {
1753 map_extent_buffer(right, (unsigned long)item,
1754 sizeof(struct btrfs_item),
1755 &right->map_token, &right->kaddr,
1756 &right->map_start, &right->map_len,
1757 KM_USER1);
1758 }
1759 push_space -= btrfs_item_size(right, item);
1760 btrfs_set_item_offset(right, item, push_space);
1761 }
1762
1763 if (right->map_token) {
1764 unmap_extent_buffer(right, right->map_token, KM_USER1);
1765 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001766 }
Chris Mason7518a232007-03-12 12:01:18 -04001767 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001768 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001769
Chris Mason34a38212007-11-07 13:31:03 -05001770 if (left_nritems)
1771 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001772 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001773
Chris Mason5f39d392007-10-15 16:14:19 -04001774 btrfs_item_key(right, &disk_key, 0);
1775 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001776 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001777
Chris Mason00ec4c52007-02-24 12:47:20 -05001778 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001779 if (path->slots[0] >= left_nritems) {
1780 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001781 free_extent_buffer(path->nodes[0]);
1782 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001783 path->slots[1] += 1;
1784 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001785 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001786 }
1787 return 0;
1788}
1789/*
Chris Mason74123bd2007-02-02 11:05:29 -05001790 * push some data in the path leaf to the left, trying to free up at
1791 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1792 */
Chris Masone089f052007-03-16 16:20:31 -04001793static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001794 *root, struct btrfs_path *path, int data_size,
1795 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796{
Chris Mason5f39d392007-10-15 16:14:19 -04001797 struct btrfs_disk_key disk_key;
1798 struct extent_buffer *right = path->nodes[0];
1799 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001800 int slot;
1801 int i;
1802 int free_space;
1803 int push_space = 0;
1804 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001805 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001806 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001807 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001808 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001809 int ret = 0;
1810 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001811 u32 this_item_size;
1812 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001813
1814 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001815 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001817 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001819
Chris Mason3685f792007-10-19 09:23:27 -04001820 right_nritems = btrfs_header_nritems(right);
1821 if (right_nritems == 0) {
1822 return 1;
1823 }
1824
Chris Masonca7a79a2008-05-12 12:59:19 -04001825 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason123abc82007-03-14 14:14:43 -04001826 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001827 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001828 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001829 return 1;
1830 }
Chris Mason02217ed2007-03-02 16:08:05 -05001831
1832 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001833 ret = btrfs_cow_block(trans, root, left,
1834 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001835 if (ret) {
1836 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001837 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001838 return 1;
1839 }
Chris Mason3685f792007-10-19 09:23:27 -04001840
Chris Mason123abc82007-03-14 14:14:43 -04001841 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001842 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001843 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001844 return 1;
1845 }
1846
Chris Mason34a38212007-11-07 13:31:03 -05001847 if (empty)
1848 nr = right_nritems;
1849 else
1850 nr = right_nritems - 1;
1851
1852 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001853 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001854 if (!right->map_token) {
1855 map_extent_buffer(right, (unsigned long)item,
1856 sizeof(struct btrfs_item),
1857 &right->map_token, &right->kaddr,
1858 &right->map_start, &right->map_len,
1859 KM_USER1);
1860 }
1861
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862 if (path->slots[0] == i)
1863 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001864
1865 this_item_size = btrfs_item_size(right, item);
1866 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001867 break;
Chris Masondb945352007-10-15 16:15:53 -04001868
Chris Masonbe0e5c02007-01-26 15:51:26 -05001869 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001870 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001871 }
Chris Masondb945352007-10-15 16:15:53 -04001872
1873 if (right->map_token) {
1874 unmap_extent_buffer(right, right->map_token, KM_USER1);
1875 right->map_token = NULL;
1876 }
1877
Chris Masonbe0e5c02007-01-26 15:51:26 -05001878 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001879 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001880 return 1;
1881 }
Chris Mason34a38212007-11-07 13:31:03 -05001882 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001883 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001884
Chris Masonbe0e5c02007-01-26 15:51:26 -05001885 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001886 copy_extent_buffer(left, right,
1887 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1888 btrfs_item_nr_offset(0),
1889 push_items * sizeof(struct btrfs_item));
1890
Chris Mason123abc82007-03-14 14:14:43 -04001891 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001892 btrfs_item_offset_nr(right, push_items -1);
1893
1894 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001895 leaf_data_end(root, left) - push_space,
1896 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001897 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001898 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001899 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001900 BUG_ON(old_left_nritems < 0);
1901
Chris Masondb945352007-10-15 16:15:53 -04001902 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001903 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001904 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001905
Chris Mason5f39d392007-10-15 16:14:19 -04001906 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001907 if (!left->map_token) {
1908 map_extent_buffer(left, (unsigned long)item,
1909 sizeof(struct btrfs_item),
1910 &left->map_token, &left->kaddr,
1911 &left->map_start, &left->map_len,
1912 KM_USER1);
1913 }
1914
Chris Mason5f39d392007-10-15 16:14:19 -04001915 ioff = btrfs_item_offset(left, item);
1916 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001917 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001918 }
Chris Mason5f39d392007-10-15 16:14:19 -04001919 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001920 if (left->map_token) {
1921 unmap_extent_buffer(left, left->map_token, KM_USER1);
1922 left->map_token = NULL;
1923 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001924
1925 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001926 if (push_items > right_nritems) {
1927 printk("push items %d nr %u\n", push_items, right_nritems);
1928 WARN_ON(1);
1929 }
Chris Mason5f39d392007-10-15 16:14:19 -04001930
Chris Mason34a38212007-11-07 13:31:03 -05001931 if (push_items < right_nritems) {
1932 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1933 leaf_data_end(root, right);
1934 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1935 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1936 btrfs_leaf_data(right) +
1937 leaf_data_end(root, right), push_space);
1938
1939 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001940 btrfs_item_nr_offset(push_items),
1941 (btrfs_header_nritems(right) - push_items) *
1942 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001943 }
Yaneef1c492007-11-26 10:58:13 -05001944 right_nritems -= push_items;
1945 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001946 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001947 for (i = 0; i < right_nritems; i++) {
1948 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001949
1950 if (!right->map_token) {
1951 map_extent_buffer(right, (unsigned long)item,
1952 sizeof(struct btrfs_item),
1953 &right->map_token, &right->kaddr,
1954 &right->map_start, &right->map_len,
1955 KM_USER1);
1956 }
1957
1958 push_space = push_space - btrfs_item_size(right, item);
1959 btrfs_set_item_offset(right, item, push_space);
1960 }
1961 if (right->map_token) {
1962 unmap_extent_buffer(right, right->map_token, KM_USER1);
1963 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001967 if (right_nritems)
1968 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001969
Chris Mason5f39d392007-10-15 16:14:19 -04001970 btrfs_item_key(right, &disk_key, 0);
1971 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001972 if (wret)
1973 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001974
1975 /* then fixup the leaf pointer in the path */
1976 if (path->slots[0] < push_items) {
1977 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001978 free_extent_buffer(path->nodes[0]);
1979 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980 path->slots[1] -= 1;
1981 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001982 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001983 path->slots[0] -= push_items;
1984 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001985 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001986 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001987}
1988
Chris Mason74123bd2007-02-02 11:05:29 -05001989/*
1990 * split the path's leaf in two, making sure there is at least data_size
1991 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001992 *
1993 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001994 */
Chris Masone089f052007-03-16 16:20:31 -04001995static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001996 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001997 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998{
Chris Mason7bb86312007-12-11 09:25:06 -05001999 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002000 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002001 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002002 int mid;
2003 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002004 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002005 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002006 int data_copy_size;
2007 int rt_data_off;
2008 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002009 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002010 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002011 int double_split;
2012 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002013 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002014
Chris Masoncc0c5532007-10-25 15:42:57 -04002015 if (extend)
2016 space_needed = data_size;
2017
Chris Mason7bb86312007-12-11 09:25:06 -05002018 if (root->ref_cows)
2019 root_gen = trans->transid;
2020 else
2021 root_gen = 0;
2022
Chris Mason40689472007-03-17 14:29:23 -04002023 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002024 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002025 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002026 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002027 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002028 }
2029 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002030 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002031 if (wret < 0)
2032 return wret;
2033 }
2034 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002035
Chris Mason3685f792007-10-19 09:23:27 -04002036 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002037 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002038 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002039 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002040
Chris Mason5c680ed2007-02-22 11:39:13 -05002041 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002042 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002043 if (ret)
2044 return ret;
2045 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002046again:
2047 double_split = 0;
2048 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002049 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002050 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002051 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002052
Chris Mason7bb86312007-12-11 09:25:06 -05002053 btrfs_item_key(l, &disk_key, 0);
2054
2055 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2056 root->root_key.objectid,
2057 root_gen, disk_key.objectid, 0,
2058 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002059 if (IS_ERR(right)) {
2060 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002062 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002063
Chris Mason5f39d392007-10-15 16:14:19 -04002064 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002065 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_header_generation(right, trans->transid);
2067 btrfs_set_header_owner(right, root->root_key.objectid);
2068 btrfs_set_header_level(right, 0);
2069 write_extent_buffer(right, root->fs_info->fsid,
2070 (unsigned long)btrfs_header_fsid(right),
2071 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002072
2073 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2074 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2075 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002076 if (mid <= slot) {
2077 if (nritems == 1 ||
2078 leaf_space_used(l, mid, nritems - mid) + space_needed >
2079 BTRFS_LEAF_DATA_SIZE(root)) {
2080 if (slot >= nritems) {
2081 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002083 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002084 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002085 path->slots[1] + 1, 1);
2086 if (wret)
2087 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002088 free_extent_buffer(path->nodes[0]);
2089 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002090 path->slots[0] = 0;
2091 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002092 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002093 return ret;
2094 }
2095 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002096 if (mid != nritems &&
2097 leaf_space_used(l, mid, nritems - mid) +
2098 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2099 double_split = 1;
2100 }
Chris Masond4dbff92007-04-04 14:08:15 -04002101 }
2102 } else {
2103 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2104 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002105 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002106 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002108 wret = insert_ptr(trans, root, path,
2109 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002110 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002111 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002112 if (wret)
2113 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002114 free_extent_buffer(path->nodes[0]);
2115 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002116 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002117 if (path->slots[1] == 0) {
2118 wret = fixup_low_keys(trans, root,
2119 path, &disk_key, 1);
2120 if (wret)
2121 ret = wret;
2122 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002123 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002124 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002125 } else if (extend && slot == 0) {
2126 mid = 1;
2127 } else {
2128 mid = slot;
2129 if (mid != nritems &&
2130 leaf_space_used(l, mid, nritems - mid) +
2131 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2132 double_split = 1;
2133 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002134 }
Chris Masond4dbff92007-04-04 14:08:15 -04002135 }
2136 }
Chris Mason5f39d392007-10-15 16:14:19 -04002137 nritems = nritems - mid;
2138 btrfs_set_header_nritems(right, nritems);
2139 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2140
2141 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2142 btrfs_item_nr_offset(mid),
2143 nritems * sizeof(struct btrfs_item));
2144
2145 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002146 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2147 data_copy_size, btrfs_leaf_data(l) +
2148 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002149
Chris Mason5f39d392007-10-15 16:14:19 -04002150 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2151 btrfs_item_end_nr(l, mid);
2152
2153 for (i = 0; i < nritems; i++) {
2154 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002155 u32 ioff;
2156
2157 if (!right->map_token) {
2158 map_extent_buffer(right, (unsigned long)item,
2159 sizeof(struct btrfs_item),
2160 &right->map_token, &right->kaddr,
2161 &right->map_start, &right->map_len,
2162 KM_USER1);
2163 }
2164
2165 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002166 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002167 }
Chris Mason74123bd2007-02-02 11:05:29 -05002168
Chris Masondb945352007-10-15 16:15:53 -04002169 if (right->map_token) {
2170 unmap_extent_buffer(right, right->map_token, KM_USER1);
2171 right->map_token = NULL;
2172 }
2173
Chris Mason5f39d392007-10-15 16:14:19 -04002174 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002175 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002176 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002177 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2178 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002179 if (wret)
2180 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002181
2182 btrfs_mark_buffer_dirty(right);
2183 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002184 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002185
Chris Masonbe0e5c02007-01-26 15:51:26 -05002186 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002187 free_extent_buffer(path->nodes[0]);
2188 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002189 path->slots[0] -= mid;
2190 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002191 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002192 free_extent_buffer(right);
2193
Chris Masoneb60cea2007-02-02 09:18:22 -05002194 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002195
Chris Masoncc0c5532007-10-25 15:42:57 -04002196 if (double_split) {
2197 BUG_ON(num_doubles != 0);
2198 num_doubles++;
2199 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002200 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002201 return ret;
2202}
2203
Chris Masonb18c6682007-04-17 13:26:50 -04002204int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2205 struct btrfs_root *root,
2206 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002207 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002208{
2209 int ret = 0;
2210 int slot;
2211 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002212 struct extent_buffer *leaf;
2213 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002214 u32 nritems;
2215 unsigned int data_end;
2216 unsigned int old_data_start;
2217 unsigned int old_size;
2218 unsigned int size_diff;
2219 int i;
2220
2221 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002222 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002223 slot = path->slots[0];
2224
2225 old_size = btrfs_item_size_nr(leaf, slot);
2226 if (old_size == new_size)
2227 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002228
Chris Mason5f39d392007-10-15 16:14:19 -04002229 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002230 data_end = leaf_data_end(root, leaf);
2231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002233
Chris Masonb18c6682007-04-17 13:26:50 -04002234 size_diff = old_size - new_size;
2235
2236 BUG_ON(slot < 0);
2237 BUG_ON(slot >= nritems);
2238
2239 /*
2240 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2241 */
2242 /* first correct the data pointers */
2243 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002244 u32 ioff;
2245 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002246
2247 if (!leaf->map_token) {
2248 map_extent_buffer(leaf, (unsigned long)item,
2249 sizeof(struct btrfs_item),
2250 &leaf->map_token, &leaf->kaddr,
2251 &leaf->map_start, &leaf->map_len,
2252 KM_USER1);
2253 }
2254
Chris Mason5f39d392007-10-15 16:14:19 -04002255 ioff = btrfs_item_offset(leaf, item);
2256 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002257 }
Chris Masondb945352007-10-15 16:15:53 -04002258
2259 if (leaf->map_token) {
2260 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2261 leaf->map_token = NULL;
2262 }
2263
Chris Masonb18c6682007-04-17 13:26:50 -04002264 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002265 if (from_end) {
2266 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2267 data_end + size_diff, btrfs_leaf_data(leaf) +
2268 data_end, old_data_start + new_size - data_end);
2269 } else {
2270 struct btrfs_disk_key disk_key;
2271 u64 offset;
2272
2273 btrfs_item_key(leaf, &disk_key, slot);
2274
2275 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2276 unsigned long ptr;
2277 struct btrfs_file_extent_item *fi;
2278
2279 fi = btrfs_item_ptr(leaf, slot,
2280 struct btrfs_file_extent_item);
2281 fi = (struct btrfs_file_extent_item *)(
2282 (unsigned long)fi - size_diff);
2283
2284 if (btrfs_file_extent_type(leaf, fi) ==
2285 BTRFS_FILE_EXTENT_INLINE) {
2286 ptr = btrfs_item_ptr_offset(leaf, slot);
2287 memmove_extent_buffer(leaf, ptr,
2288 (unsigned long)fi,
2289 offsetof(struct btrfs_file_extent_item,
2290 disk_bytenr));
2291 }
2292 }
2293
2294 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2295 data_end + size_diff, btrfs_leaf_data(leaf) +
2296 data_end, old_data_start - data_end);
2297
2298 offset = btrfs_disk_key_offset(&disk_key);
2299 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2300 btrfs_set_item_key(leaf, &disk_key, slot);
2301 if (slot == 0)
2302 fixup_low_keys(trans, root, path, &disk_key, 1);
2303 }
Chris Mason5f39d392007-10-15 16:14:19 -04002304
2305 item = btrfs_item_nr(leaf, slot);
2306 btrfs_set_item_size(leaf, item, new_size);
2307 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002308
2309 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002310 if (btrfs_leaf_free_space(root, leaf) < 0) {
2311 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002312 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002313 }
Chris Masonb18c6682007-04-17 13:26:50 -04002314 return ret;
2315}
2316
Chris Mason5f39d392007-10-15 16:14:19 -04002317int btrfs_extend_item(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root, struct btrfs_path *path,
2319 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002320{
2321 int ret = 0;
2322 int slot;
2323 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002324 struct extent_buffer *leaf;
2325 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002326 u32 nritems;
2327 unsigned int data_end;
2328 unsigned int old_data;
2329 unsigned int old_size;
2330 int i;
2331
2332 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002333 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002334
Chris Mason5f39d392007-10-15 16:14:19 -04002335 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002336 data_end = leaf_data_end(root, leaf);
2337
Chris Mason5f39d392007-10-15 16:14:19 -04002338 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2339 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002340 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002341 }
Chris Mason6567e832007-04-16 09:22:45 -04002342 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002343 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002344
2345 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002346 if (slot >= nritems) {
2347 btrfs_print_leaf(root, leaf);
2348 printk("slot %d too large, nritems %d\n", slot, nritems);
2349 BUG_ON(1);
2350 }
Chris Mason6567e832007-04-16 09:22:45 -04002351
2352 /*
2353 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2354 */
2355 /* first correct the data pointers */
2356 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002357 u32 ioff;
2358 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002359
2360 if (!leaf->map_token) {
2361 map_extent_buffer(leaf, (unsigned long)item,
2362 sizeof(struct btrfs_item),
2363 &leaf->map_token, &leaf->kaddr,
2364 &leaf->map_start, &leaf->map_len,
2365 KM_USER1);
2366 }
Chris Mason5f39d392007-10-15 16:14:19 -04002367 ioff = btrfs_item_offset(leaf, item);
2368 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002369 }
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Masondb945352007-10-15 16:15:53 -04002371 if (leaf->map_token) {
2372 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2373 leaf->map_token = NULL;
2374 }
2375
Chris Mason6567e832007-04-16 09:22:45 -04002376 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002377 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002378 data_end - data_size, btrfs_leaf_data(leaf) +
2379 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002380
Chris Mason6567e832007-04-16 09:22:45 -04002381 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002382 old_size = btrfs_item_size_nr(leaf, slot);
2383 item = btrfs_item_nr(leaf, slot);
2384 btrfs_set_item_size(leaf, item, old_size + data_size);
2385 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002386
2387 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002388 if (btrfs_leaf_free_space(root, leaf) < 0) {
2389 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002390 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002391 }
Chris Mason6567e832007-04-16 09:22:45 -04002392 return ret;
2393}
2394
Chris Mason74123bd2007-02-02 11:05:29 -05002395/*
2396 * Given a key and some data, insert an item into the tree.
2397 * This does all the path init required, making room in the tree if needed.
2398 */
Chris Mason9c583092008-01-29 15:15:18 -05002399int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002400 struct btrfs_root *root,
2401 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002402 struct btrfs_key *cpu_key, u32 *data_size,
2403 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002404{
Chris Mason5f39d392007-10-15 16:14:19 -04002405 struct extent_buffer *leaf;
2406 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002407 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002408 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002409 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002410 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002411 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002412 u32 total_size = 0;
2413 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002415 struct btrfs_disk_key disk_key;
2416
Chris Mason9c583092008-01-29 15:15:18 -05002417 for (i = 0; i < nr; i++) {
2418 total_data += data_size[i];
2419 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002420
Chris Mason74123bd2007-02-02 11:05:29 -05002421 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002422 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002423 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002424
Chris Mason9c583092008-01-29 15:15:18 -05002425 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2426 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002427 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002428 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002429 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002430 if (ret < 0)
2431 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002432
Chris Mason62e27492007-03-15 12:56:47 -04002433 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002434 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002435
Chris Mason5f39d392007-10-15 16:14:19 -04002436 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002437 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002438
Chris Mason123abc82007-03-14 14:14:43 -04002439 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002440 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002441 btrfs_print_leaf(root, leaf);
2442 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002443 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002444 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002445 }
Chris Mason5f39d392007-10-15 16:14:19 -04002446
Chris Mason62e27492007-03-15 12:56:47 -04002447 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002448 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002449
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450 if (slot != nritems) {
2451 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 if (old_data < data_end) {
2455 btrfs_print_leaf(root, leaf);
2456 printk("slot %d old_data %d data_end %d\n",
2457 slot, old_data, data_end);
2458 BUG_ON(1);
2459 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002460 /*
2461 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2462 */
2463 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002464 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002465 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002466 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002467
Chris Mason5f39d392007-10-15 16:14:19 -04002468 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002469 if (!leaf->map_token) {
2470 map_extent_buffer(leaf, (unsigned long)item,
2471 sizeof(struct btrfs_item),
2472 &leaf->map_token, &leaf->kaddr,
2473 &leaf->map_start, &leaf->map_len,
2474 KM_USER1);
2475 }
2476
Chris Mason5f39d392007-10-15 16:14:19 -04002477 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002478 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002479 }
Chris Masondb945352007-10-15 16:15:53 -04002480 if (leaf->map_token) {
2481 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2482 leaf->map_token = NULL;
2483 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484
2485 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002486 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002487 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002488 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489
2490 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002491 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002492 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002493 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002494 data_end = old_data;
2495 }
Chris Mason5f39d392007-10-15 16:14:19 -04002496
Chris Mason62e27492007-03-15 12:56:47 -04002497 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002498 for (i = 0; i < nr; i++) {
2499 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2500 btrfs_set_item_key(leaf, &disk_key, slot + i);
2501 item = btrfs_item_nr(leaf, slot + i);
2502 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2503 data_end -= data_size[i];
2504 btrfs_set_item_size(leaf, item, data_size[i]);
2505 }
2506 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002507 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002508
2509 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002510 if (slot == 0) {
2511 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002512 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002513 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002514
Chris Mason5f39d392007-10-15 16:14:19 -04002515 if (btrfs_leaf_free_space(root, leaf) < 0) {
2516 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002518 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002519
Chris Masoned2ff2c2007-03-01 18:59:40 -05002520out:
Chris Mason62e27492007-03-15 12:56:47 -04002521 return ret;
2522}
2523
2524/*
2525 * Given a key and some data, insert an item into the tree.
2526 * This does all the path init required, making room in the tree if needed.
2527 */
Chris Masone089f052007-03-16 16:20:31 -04002528int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2529 *root, struct btrfs_key *cpu_key, void *data, u32
2530 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002531{
2532 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002533 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002534 struct extent_buffer *leaf;
2535 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002536
Chris Mason2c90e5d2007-04-02 10:50:19 -04002537 path = btrfs_alloc_path();
2538 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002539 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002540 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002541 leaf = path->nodes[0];
2542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2543 write_extent_buffer(leaf, data, ptr, data_size);
2544 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002545 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002546 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002547 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002548}
2549
Chris Mason74123bd2007-02-02 11:05:29 -05002550/*
Chris Mason5de08d72007-02-24 06:24:44 -05002551 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002552 *
2553 * If the delete empties a node, the node is removed from the tree,
2554 * continuing all the way the root if required. The root is converted into
2555 * a leaf if all the nodes are emptied.
2556 */
Chris Masone089f052007-03-16 16:20:31 -04002557static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2558 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559{
Chris Mason5f39d392007-10-15 16:14:19 -04002560 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002561 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002562 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002563 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564
Chris Mason5f39d392007-10-15 16:14:19 -04002565 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002566 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002567 memmove_extent_buffer(parent,
2568 btrfs_node_key_ptr_offset(slot),
2569 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002570 sizeof(struct btrfs_key_ptr) *
2571 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002572 }
Chris Mason7518a232007-03-12 12:01:18 -04002573 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002574 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002575 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002576 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002577 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002578 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002579 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002580 struct btrfs_disk_key disk_key;
2581
2582 btrfs_node_key(parent, &disk_key, 0);
2583 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002584 if (wret)
2585 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 }
Chris Masond6025572007-03-30 14:27:56 -04002587 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002588 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589}
2590
Chris Mason74123bd2007-02-02 11:05:29 -05002591/*
2592 * delete the item at the leaf level in path. If that empties
2593 * the leaf, remove it from the tree
2594 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002595int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2596 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597{
Chris Mason5f39d392007-10-15 16:14:19 -04002598 struct extent_buffer *leaf;
2599 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002600 int last_off;
2601 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002602 int ret = 0;
2603 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002604 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002605 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606
Chris Mason5f39d392007-10-15 16:14:19 -04002607 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002608 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2609
2610 for (i = 0; i < nr; i++)
2611 dsize += btrfs_item_size_nr(leaf, slot + i);
2612
Chris Mason5f39d392007-10-15 16:14:19 -04002613 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614
Chris Mason85e21ba2008-01-29 15:11:36 -05002615 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002617 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002618
2619 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002620 data_end + dsize,
2621 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002622 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002623
Chris Mason85e21ba2008-01-29 15:11:36 -05002624 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002625 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002626
Chris Mason5f39d392007-10-15 16:14:19 -04002627 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002628 if (!leaf->map_token) {
2629 map_extent_buffer(leaf, (unsigned long)item,
2630 sizeof(struct btrfs_item),
2631 &leaf->map_token, &leaf->kaddr,
2632 &leaf->map_start, &leaf->map_len,
2633 KM_USER1);
2634 }
Chris Mason5f39d392007-10-15 16:14:19 -04002635 ioff = btrfs_item_offset(leaf, item);
2636 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002637 }
Chris Masondb945352007-10-15 16:15:53 -04002638
2639 if (leaf->map_token) {
2640 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2641 leaf->map_token = NULL;
2642 }
2643
Chris Mason5f39d392007-10-15 16:14:19 -04002644 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002645 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002646 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002647 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002649 btrfs_set_header_nritems(leaf, nritems - nr);
2650 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002651
Chris Mason74123bd2007-02-02 11:05:29 -05002652 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002653 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002654 if (leaf == root->node) {
2655 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002656 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002657 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002658 clean_tree_block(trans, root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002659 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002660 if (wret)
2661 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002662 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002663 leaf->start, leaf->len,
2664 btrfs_header_owner(path->nodes[1]),
2665 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002666 if (wret)
2667 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002668 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002669 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002670 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002671 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002672 struct btrfs_disk_key disk_key;
2673
2674 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002675 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002676 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002677 if (wret)
2678 ret = wret;
2679 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002680
Chris Mason74123bd2007-02-02 11:05:29 -05002681 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002682 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683 /* push_leaf_left fixes the path.
2684 * make sure the path still points to our leaf
2685 * for possible call to del_ptr below
2686 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002687 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002688 extent_buffer_get(leaf);
2689
Chris Mason85e21ba2008-01-29 15:11:36 -05002690 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002691 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002692 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002693
2694 if (path->nodes[0] == leaf &&
2695 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002696 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002697 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002698 ret = wret;
2699 }
Chris Mason5f39d392007-10-15 16:14:19 -04002700
2701 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002702 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002703 u64 bytenr = leaf->start;
2704 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002705
Chris Mason7bb86312007-12-11 09:25:06 -05002706 root_gen = btrfs_header_generation(
2707 path->nodes[1]);
2708
Chris Mason5f39d392007-10-15 16:14:19 -04002709 clean_tree_block(trans, root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002710
Chris Masone089f052007-03-16 16:20:31 -04002711 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002712 if (wret)
2713 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002714
2715 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002716 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002717 blocksize,
2718 btrfs_header_owner(path->nodes[1]),
2719 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002720 if (wret)
2721 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002722 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002723 btrfs_mark_buffer_dirty(leaf);
2724 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725 }
Chris Masond5719762007-03-23 10:01:08 -04002726 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002727 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002728 }
2729 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731}
2732
Chris Mason97571fd2007-02-24 13:39:08 -05002733/*
Chris Mason7bb86312007-12-11 09:25:06 -05002734 * walk up the tree as far as required to find the previous leaf.
2735 * returns 0 if it found something or 1 if there are no lesser leaves.
2736 * returns < 0 on io errors.
2737 */
2738int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2739{
2740 int slot;
2741 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002742 struct extent_buffer *c;
2743 struct extent_buffer *next = NULL;
2744
2745 while(level < BTRFS_MAX_LEVEL) {
2746 if (!path->nodes[level])
2747 return 1;
2748
2749 slot = path->slots[level];
2750 c = path->nodes[level];
2751 if (slot == 0) {
2752 level++;
2753 if (level == BTRFS_MAX_LEVEL)
2754 return 1;
2755 continue;
2756 }
2757 slot--;
2758
Chris Mason7bb86312007-12-11 09:25:06 -05002759 if (next)
2760 free_extent_buffer(next);
2761
Chris Masonca7a79a2008-05-12 12:59:19 -04002762 next = read_node_slot(root, c, slot);
Chris Mason7bb86312007-12-11 09:25:06 -05002763 break;
2764 }
2765 path->slots[level] = slot;
2766 while(1) {
2767 level--;
2768 c = path->nodes[level];
2769 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002770 slot = btrfs_header_nritems(next);
2771 if (slot != 0)
2772 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002773 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002774 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002775 if (!level)
2776 break;
Chris Masonca7a79a2008-05-12 12:59:19 -04002777 next = read_node_slot(root, next, slot);
Chris Mason7bb86312007-12-11 09:25:06 -05002778 }
2779 return 0;
2780}
2781
2782/*
Chris Mason97571fd2007-02-24 13:39:08 -05002783 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002784 * returns 0 if it found something or 1 if there are no greater leaves.
2785 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002786 */
Chris Mason234b63a2007-03-13 10:46:10 -04002787int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002788{
2789 int slot;
2790 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002791 struct extent_buffer *c;
2792 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002793
Chris Mason234b63a2007-03-13 10:46:10 -04002794 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002795 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002796 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002797
Chris Masond97e63b2007-02-20 16:40:44 -05002798 slot = path->slots[level] + 1;
2799 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002800 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002801 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002802 if (level == BTRFS_MAX_LEVEL)
2803 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002804 continue;
2805 }
Chris Mason5f39d392007-10-15 16:14:19 -04002806
Chris Masoncfaa7292007-02-21 17:04:57 -05002807 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002808 free_extent_buffer(next);
2809
Chris Mason6702ed42007-08-07 16:15:09 -04002810 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002811 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002812
Chris Masonca7a79a2008-05-12 12:59:19 -04002813 next = read_node_slot(root, c, slot);
Chris Masond97e63b2007-02-20 16:40:44 -05002814 break;
2815 }
2816 path->slots[level] = slot;
2817 while(1) {
2818 level--;
2819 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002820 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002821 path->nodes[level] = next;
2822 path->slots[level] = 0;
2823 if (!level)
2824 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002825 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002826 reada_for_search(root, path, level, 0, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04002827 next = read_node_slot(root, next, 0);
Chris Masond97e63b2007-02-20 16:40:44 -05002828 }
2829 return 0;
2830}
Chris Mason0b86a832008-03-24 15:01:56 -04002831
2832int btrfs_previous_item(struct btrfs_root *root,
2833 struct btrfs_path *path, u64 min_objectid,
2834 int type)
2835{
2836 struct btrfs_key found_key;
2837 struct extent_buffer *leaf;
2838 int ret;
2839
2840 while(1) {
2841 if (path->slots[0] == 0) {
2842 ret = btrfs_prev_leaf(root, path);
2843 if (ret != 0)
2844 return ret;
2845 } else {
2846 path->slots[0]--;
2847 }
2848 leaf = path->nodes[0];
2849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2850 if (found_key.type == type)
2851 return 0;
2852 }
2853 return 1;
2854}
2855