blob: 75625c68fdd8d867bbdd9c071a9449f17429876f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
57void btrfs_free_path(struct btrfs_path *p)
58{
Chris Masondf24a2b2007-04-04 09:36:31 -040059 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040060 kmem_cache_free(btrfs_path_cachep, p);
61}
62
Chris Mason234b63a2007-03-13 10:46:10 -040063void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050064{
65 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040066 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050067 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040068 continue;
69 if (p->locks[i]) {
70 btrfs_tree_unlock(p->nodes[i]);
71 p->locks[i] = 0;
72 }
Chris Mason5f39d392007-10-15 16:14:19 -040073 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050074 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050075 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050076}
77
Chris Mason925baed2008-06-25 16:01:30 -040078struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
79{
80 struct extent_buffer *eb;
81 spin_lock(&root->node_lock);
82 eb = root->node;
83 extent_buffer_get(eb);
84 spin_unlock(&root->node_lock);
85 return eb;
86}
87
88struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
89{
90 struct extent_buffer *eb;
91
92 while(1) {
93 eb = btrfs_root_node(root);
94 btrfs_tree_lock(eb);
95
96 spin_lock(&root->node_lock);
97 if (eb == root->node) {
98 spin_unlock(&root->node_lock);
99 break;
100 }
101 spin_unlock(&root->node_lock);
102
103 btrfs_tree_unlock(eb);
104 free_extent_buffer(eb);
105 }
106 return eb;
107}
108
Chris Mason0b86a832008-03-24 15:01:56 -0400109static void add_root_to_dirty_list(struct btrfs_root *root)
110{
111 if (root->track_dirty && list_empty(&root->dirty_list)) {
112 list_add(&root->dirty_list,
113 &root->fs_info->dirty_cowonly_roots);
114 }
115}
116
Chris Masonbe20aa92007-12-17 20:14:01 -0500117int btrfs_copy_root(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root,
119 struct extent_buffer *buf,
120 struct extent_buffer **cow_ret, u64 new_root_objectid)
121{
122 struct extent_buffer *cow;
123 u32 nritems;
124 int ret = 0;
125 int level;
126 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -0500127 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500128
Chris Mason4aec2b52007-12-18 16:25:45 -0500129 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
130 if (!new_root)
131 return -ENOMEM;
132
133 memcpy(new_root, root, sizeof(*new_root));
134 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500135
136 WARN_ON(root->ref_cows && trans->transid !=
137 root->fs_info->running_transaction->transid);
138 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
139
140 level = btrfs_header_level(buf);
141 nritems = btrfs_header_nritems(buf);
142 if (nritems) {
143 if (level == 0)
144 btrfs_item_key_to_cpu(buf, &first_key, 0);
145 else
146 btrfs_node_key_to_cpu(buf, &first_key, 0);
147 } else {
148 first_key.objectid = 0;
149 }
Chris Mason925baed2008-06-25 16:01:30 -0400150 cow = btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500151 new_root_objectid,
152 trans->transid, first_key.objectid,
153 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500154 if (IS_ERR(cow)) {
155 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500156 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500157 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500158
159 copy_extent_buffer(cow, buf, 0, 0, cow->len);
160 btrfs_set_header_bytenr(cow, cow->start);
161 btrfs_set_header_generation(cow, trans->transid);
162 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400163 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500164
165 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500166 ret = btrfs_inc_ref(trans, new_root, buf);
167 kfree(new_root);
168
Chris Masonbe20aa92007-12-17 20:14:01 -0500169 if (ret)
170 return ret;
171
172 btrfs_mark_buffer_dirty(cow);
173 *cow_ret = cow;
174 return 0;
175}
176
177int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400178 struct btrfs_root *root,
179 struct extent_buffer *buf,
180 struct extent_buffer *parent, int parent_slot,
181 struct extent_buffer **cow_ret,
182 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400183{
Chris Mason7bb86312007-12-11 09:25:06 -0500184 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400185 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500186 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400187 int ret = 0;
188 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500189 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400190 int unlock_orig = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500191 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400192
Chris Mason925baed2008-06-25 16:01:30 -0400193 if (*cow_ret == buf)
194 unlock_orig = 1;
195
196 WARN_ON(!btrfs_tree_locked(buf));
197
Chris Mason7bb86312007-12-11 09:25:06 -0500198 if (root->ref_cows) {
199 root_gen = trans->transid;
200 } else {
201 root_gen = 0;
202 }
Chris Mason7bb86312007-12-11 09:25:06 -0500203 WARN_ON(root->ref_cows && trans->transid !=
204 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400205 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400206
Chris Mason7bb86312007-12-11 09:25:06 -0500207 level = btrfs_header_level(buf);
208 nritems = btrfs_header_nritems(buf);
209 if (nritems) {
210 if (level == 0)
211 btrfs_item_key_to_cpu(buf, &first_key, 0);
212 else
213 btrfs_node_key_to_cpu(buf, &first_key, 0);
214 } else {
215 first_key.objectid = 0;
216 }
Chris Mason925baed2008-06-25 16:01:30 -0400217 cow = btrfs_alloc_free_block(trans, root, buf->len,
Chris Mason7bb86312007-12-11 09:25:06 -0500218 root->root_key.objectid,
219 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400220 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400221 if (IS_ERR(cow))
222 return PTR_ERR(cow);
223
Chris Mason5f39d392007-10-15 16:14:19 -0400224 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400225 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400226 btrfs_set_header_generation(cow, trans->transid);
227 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400228 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400229
Chris Mason5f39d392007-10-15 16:14:19 -0400230 WARN_ON(btrfs_header_generation(buf) > trans->transid);
231 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400232 different_trans = 1;
233 ret = btrfs_inc_ref(trans, root, buf);
234 if (ret)
235 return ret;
236 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400237 clean_tree_block(trans, root, buf);
238 }
239
240 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400241 WARN_ON(parent && parent != buf);
Chris Mason7bb86312007-12-11 09:25:06 -0500242 root_gen = btrfs_header_generation(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400243
244 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400245 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400246 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400247 spin_unlock(&root->node_lock);
248
Chris Mason6702ed42007-08-07 16:15:09 -0400249 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400250 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500251 buf->len, root->root_key.objectid,
252 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400253 }
Chris Mason5f39d392007-10-15 16:14:19 -0400254 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400255 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400256 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500257 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400258 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400259 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500260 WARN_ON(trans->transid == 0);
261 btrfs_set_node_ptr_generation(parent, parent_slot,
262 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400263 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400264 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500265 btrfs_free_extent(trans, root, buf->start, buf->len,
266 btrfs_header_owner(parent), root_gen,
267 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400268 }
Chris Mason925baed2008-06-25 16:01:30 -0400269 if (unlock_orig)
270 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400271 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400272 btrfs_mark_buffer_dirty(cow);
273 *cow_ret = cow;
274 return 0;
275}
276
Chris Mason5f39d392007-10-15 16:14:19 -0400277int btrfs_cow_block(struct btrfs_trans_handle *trans,
278 struct btrfs_root *root, struct extent_buffer *buf,
279 struct extent_buffer *parent, int parent_slot,
280 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500281{
Chris Mason6702ed42007-08-07 16:15:09 -0400282 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500283 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400284 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500285
Chris Masonccd467d2007-06-28 15:57:36 -0400286 if (trans->transaction != root->fs_info->running_transaction) {
287 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
288 root->fs_info->running_transaction->transid);
289 WARN_ON(1);
290 }
291 if (trans->transid != root->fs_info->generation) {
292 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
293 root->fs_info->generation);
294 WARN_ON(1);
295 }
Chris Masondc17ff82008-01-08 15:46:30 -0500296
297 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400298 spin_lock(&root->fs_info->hash_lock);
299 if (header_trans == trans->transid &&
300 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500301 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400302 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500303 return 0;
304 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400305 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400306 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400307 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400308 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400309 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400310}
311
Chris Mason6b800532007-10-15 16:17:34 -0400312static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400313{
Chris Mason6b800532007-10-15 16:17:34 -0400314 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400315 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400316 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400317 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500318 return 0;
319}
320
Chris Mason081e9572007-11-06 10:26:24 -0500321/*
322 * compare two keys in a memcmp fashion
323 */
324static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
325{
326 struct btrfs_key k1;
327
328 btrfs_disk_key_to_cpu(&k1, disk);
329
330 if (k1.objectid > k2->objectid)
331 return 1;
332 if (k1.objectid < k2->objectid)
333 return -1;
334 if (k1.type > k2->type)
335 return 1;
336 if (k1.type < k2->type)
337 return -1;
338 if (k1.offset > k2->offset)
339 return 1;
340 if (k1.offset < k2->offset)
341 return -1;
342 return 0;
343}
344
345
Chris Mason6702ed42007-08-07 16:15:09 -0400346int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400347 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400348 int start_slot, int cache_only, u64 *last_ret,
349 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400350{
Chris Mason6b800532007-10-15 16:17:34 -0400351 struct extent_buffer *cur;
352 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400353 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400354 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400355 u64 search_start = *last_ret;
356 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400357 u64 other;
358 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400359 int end_slot;
360 int i;
361 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400362 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400363 int uptodate;
364 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500365 int progress_passed = 0;
366 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400367
Chris Mason925baed2008-06-25 16:01:30 -0400368 /* FIXME this code needs locking */
369 return 0;
370
Chris Mason5708b952007-10-25 15:43:18 -0400371 parent_level = btrfs_header_level(parent);
372 if (cache_only && parent_level != 1)
373 return 0;
374
Chris Mason6702ed42007-08-07 16:15:09 -0400375 if (trans->transaction != root->fs_info->running_transaction) {
376 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
377 root->fs_info->running_transaction->transid);
378 WARN_ON(1);
379 }
380 if (trans->transid != root->fs_info->generation) {
381 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
382 root->fs_info->generation);
383 WARN_ON(1);
384 }
Chris Mason86479a02007-09-10 19:58:16 -0400385
Chris Mason6b800532007-10-15 16:17:34 -0400386 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400387 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400388 end_slot = parent_nritems;
389
390 if (parent_nritems == 1)
391 return 0;
392
393 for (i = start_slot; i < end_slot; i++) {
394 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400395
Chris Mason5708b952007-10-25 15:43:18 -0400396 if (!parent->map_token) {
397 map_extent_buffer(parent,
398 btrfs_node_key_ptr_offset(i),
399 sizeof(struct btrfs_key_ptr),
400 &parent->map_token, &parent->kaddr,
401 &parent->map_start, &parent->map_len,
402 KM_USER1);
403 }
Chris Mason081e9572007-11-06 10:26:24 -0500404 btrfs_node_key(parent, &disk_key, i);
405 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
406 continue;
407
408 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400409 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400410 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400411 if (last_block == 0)
412 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400413
Chris Mason6702ed42007-08-07 16:15:09 -0400414 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400415 other = btrfs_node_blockptr(parent, i - 1);
416 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400417 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400418 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400419 other = btrfs_node_blockptr(parent, i + 1);
420 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400421 }
Chris Masone9d0b132007-08-10 14:06:19 -0400422 if (close) {
423 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400424 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400425 }
Chris Mason5708b952007-10-25 15:43:18 -0400426 if (parent->map_token) {
427 unmap_extent_buffer(parent, parent->map_token,
428 KM_USER1);
429 parent->map_token = NULL;
430 }
Chris Mason6702ed42007-08-07 16:15:09 -0400431
Chris Mason6b800532007-10-15 16:17:34 -0400432 cur = btrfs_find_tree_block(root, blocknr, blocksize);
433 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400434 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400435 else
436 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400437 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400438 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400439 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400440 continue;
441 }
Chris Mason6b800532007-10-15 16:17:34 -0400442 if (!cur) {
443 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400444 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400445 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400446 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400447 }
Chris Mason6702ed42007-08-07 16:15:09 -0400448 }
Chris Masone9d0b132007-08-10 14:06:19 -0400449 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400450 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400451
Chris Mason6b800532007-10-15 16:17:34 -0400452 err = __btrfs_cow_block(trans, root, cur, parent, i,
453 &tmp, search_start,
454 min(16 * blocksize,
455 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400456 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400457 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400458 break;
Yan252c38f2007-08-29 09:11:44 -0400459 }
Chris Mason6b800532007-10-15 16:17:34 -0400460 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400461 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400462 *last_ret = search_start;
463 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400464 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400465 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400466 }
Chris Mason5708b952007-10-25 15:43:18 -0400467 if (parent->map_token) {
468 unmap_extent_buffer(parent, parent->map_token,
469 KM_USER1);
470 parent->map_token = NULL;
471 }
Chris Mason6702ed42007-08-07 16:15:09 -0400472 return err;
473}
474
Chris Mason74123bd2007-02-02 11:05:29 -0500475/*
476 * The leaf data grows from end-to-front in the node.
477 * this returns the address of the start of the last item,
478 * which is the stop of the leaf data stack
479 */
Chris Mason123abc82007-03-14 14:14:43 -0400480static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400481 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500482{
Chris Mason5f39d392007-10-15 16:14:19 -0400483 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500484 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400485 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400486 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500487}
488
Chris Mason123abc82007-03-14 14:14:43 -0400489static int check_node(struct btrfs_root *root, struct btrfs_path *path,
490 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500491{
Chris Mason5f39d392007-10-15 16:14:19 -0400492 struct extent_buffer *parent = NULL;
493 struct extent_buffer *node = path->nodes[level];
494 struct btrfs_disk_key parent_key;
495 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500496 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400497 int slot;
498 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400499 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500500
501 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400502 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400503
Chris Mason8d7be552007-05-10 11:24:42 -0400504 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400505 BUG_ON(nritems == 0);
506 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400507 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400508 btrfs_node_key(parent, &parent_key, parent_slot);
509 btrfs_node_key(node, &node_key, 0);
510 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400511 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400512 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400513 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500514 }
Chris Mason123abc82007-03-14 14:14:43 -0400515 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400516 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400517 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
518 btrfs_node_key(node, &node_key, slot);
519 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400520 }
521 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400522 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
523 btrfs_node_key(node, &node_key, slot);
524 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500525 }
526 return 0;
527}
528
Chris Mason123abc82007-03-14 14:14:43 -0400529static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
530 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500531{
Chris Mason5f39d392007-10-15 16:14:19 -0400532 struct extent_buffer *leaf = path->nodes[level];
533 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500534 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400535 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400536 struct btrfs_disk_key parent_key;
537 struct btrfs_disk_key leaf_key;
538 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400539
Chris Mason5f39d392007-10-15 16:14:19 -0400540 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500541
542 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400543 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400544
545 if (nritems == 0)
546 return 0;
547
548 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400549 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400550 btrfs_node_key(parent, &parent_key, parent_slot);
551 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400552
Chris Mason5f39d392007-10-15 16:14:19 -0400553 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400554 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400555 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400556 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500557 }
Chris Mason5f39d392007-10-15 16:14:19 -0400558#if 0
559 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
560 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
561 btrfs_item_key(leaf, &leaf_key, i);
562 if (comp_keys(&leaf_key, &cpukey) >= 0) {
563 btrfs_print_leaf(root, leaf);
564 printk("slot %d offset bad key\n", i);
565 BUG_ON(1);
566 }
567 if (btrfs_item_offset_nr(leaf, i) !=
568 btrfs_item_end_nr(leaf, i + 1)) {
569 btrfs_print_leaf(root, leaf);
570 printk("slot %d offset bad\n", i);
571 BUG_ON(1);
572 }
573 if (i == 0) {
574 if (btrfs_item_offset_nr(leaf, i) +
575 btrfs_item_size_nr(leaf, i) !=
576 BTRFS_LEAF_DATA_SIZE(root)) {
577 btrfs_print_leaf(root, leaf);
578 printk("slot %d first offset bad\n", i);
579 BUG_ON(1);
580 }
581 }
582 }
583 if (nritems > 0) {
584 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
585 btrfs_print_leaf(root, leaf);
586 printk("slot %d bad size \n", nritems - 1);
587 BUG_ON(1);
588 }
589 }
590#endif
591 if (slot != 0 && slot < nritems - 1) {
592 btrfs_item_key(leaf, &leaf_key, slot);
593 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
594 if (comp_keys(&leaf_key, &cpukey) <= 0) {
595 btrfs_print_leaf(root, leaf);
596 printk("slot %d offset bad key\n", slot);
597 BUG_ON(1);
598 }
599 if (btrfs_item_offset_nr(leaf, slot - 1) !=
600 btrfs_item_end_nr(leaf, slot)) {
601 btrfs_print_leaf(root, leaf);
602 printk("slot %d offset bad\n", slot);
603 BUG_ON(1);
604 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500605 }
Chris Mason8d7be552007-05-10 11:24:42 -0400606 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400607 btrfs_item_key(leaf, &leaf_key, slot);
608 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
609 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
610 if (btrfs_item_offset_nr(leaf, slot) !=
611 btrfs_item_end_nr(leaf, slot + 1)) {
612 btrfs_print_leaf(root, leaf);
613 printk("slot %d offset bad\n", slot);
614 BUG_ON(1);
615 }
Chris Mason8d7be552007-05-10 11:24:42 -0400616 }
Chris Mason5f39d392007-10-15 16:14:19 -0400617 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
618 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500619 return 0;
620}
621
Chris Mason98ed5172008-01-03 10:01:48 -0500622static int noinline check_block(struct btrfs_root *root,
623 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500624{
Chris Masonf1885912008-04-09 16:28:12 -0400625 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400626 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400627 if (btrfs_header_level(path->nodes[level]) != level)
628 printk("warning: bad level %Lu wanted %d found %d\n",
629 path->nodes[level]->start, level,
630 btrfs_header_level(path->nodes[level]));
631 found_start = btrfs_header_bytenr(path->nodes[level]);
632 if (found_start != path->nodes[level]->start) {
633 printk("warning: bad bytentr %Lu found %Lu\n",
634 path->nodes[level]->start, found_start);
635 }
Chris Masondb945352007-10-15 16:15:53 -0400636#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400637 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400638
Chris Mason479965d2007-10-15 16:14:27 -0400639 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
640 (unsigned long)btrfs_header_fsid(buf),
641 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400642 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400643 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400644 }
Chris Masondb945352007-10-15 16:15:53 -0400645#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500646 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400647 return check_leaf(root, path, level);
648 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500649}
650
Chris Mason74123bd2007-02-02 11:05:29 -0500651/*
Chris Mason5f39d392007-10-15 16:14:19 -0400652 * search for key in the extent_buffer. The items start at offset p,
653 * and they are item_size apart. There are 'max' items in p.
654 *
Chris Mason74123bd2007-02-02 11:05:29 -0500655 * the slot in the array is returned via slot, and it points to
656 * the place where you would insert key if it is not found in
657 * the array.
658 *
659 * slot may point to max if the key is bigger than all of the keys
660 */
Chris Mason5f39d392007-10-15 16:14:19 -0400661static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
662 int item_size, struct btrfs_key *key,
663 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500664{
665 int low = 0;
666 int high = max;
667 int mid;
668 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400669 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400670 struct btrfs_disk_key unaligned;
671 unsigned long offset;
672 char *map_token = NULL;
673 char *kaddr = NULL;
674 unsigned long map_start = 0;
675 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400676 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500677
678 while(low < high) {
679 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400680 offset = p + mid * item_size;
681
682 if (!map_token || offset < map_start ||
683 (offset + sizeof(struct btrfs_disk_key)) >
684 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400685 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400686 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400687 map_token = NULL;
688 }
689 err = map_extent_buffer(eb, offset,
690 sizeof(struct btrfs_disk_key),
691 &map_token, &kaddr,
692 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400693
Chris Mason479965d2007-10-15 16:14:27 -0400694 if (!err) {
695 tmp = (struct btrfs_disk_key *)(kaddr + offset -
696 map_start);
697 } else {
698 read_extent_buffer(eb, &unaligned,
699 offset, sizeof(unaligned));
700 tmp = &unaligned;
701 }
702
Chris Mason5f39d392007-10-15 16:14:19 -0400703 } else {
704 tmp = (struct btrfs_disk_key *)(kaddr + offset -
705 map_start);
706 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500707 ret = comp_keys(tmp, key);
708
709 if (ret < 0)
710 low = mid + 1;
711 else if (ret > 0)
712 high = mid;
713 else {
714 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400715 if (map_token)
716 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500717 return 0;
718 }
719 }
720 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400721 if (map_token)
722 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723 return 1;
724}
725
Chris Mason97571fd2007-02-24 13:39:08 -0500726/*
727 * simple bin_search frontend that does the right thing for
728 * leaves vs nodes
729 */
Chris Mason5f39d392007-10-15 16:14:19 -0400730static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
731 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732{
Chris Mason5f39d392007-10-15 16:14:19 -0400733 if (level == 0) {
734 return generic_bin_search(eb,
735 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400736 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400737 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400738 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500739 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400740 return generic_bin_search(eb,
741 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400742 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400743 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400744 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500745 }
746 return -1;
747}
748
Chris Mason5f39d392007-10-15 16:14:19 -0400749static struct extent_buffer *read_node_slot(struct btrfs_root *root,
750 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500751{
Chris Masonca7a79a2008-05-12 12:59:19 -0400752 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500753 if (slot < 0)
754 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400755 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500756 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400757
758 BUG_ON(level == 0);
759
Chris Masondb945352007-10-15 16:15:53 -0400760 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400761 btrfs_level_size(root, level - 1),
762 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500763}
764
Chris Mason98ed5172008-01-03 10:01:48 -0500765static int balance_level(struct btrfs_trans_handle *trans,
766 struct btrfs_root *root,
767 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500768{
Chris Mason5f39d392007-10-15 16:14:19 -0400769 struct extent_buffer *right = NULL;
770 struct extent_buffer *mid;
771 struct extent_buffer *left = NULL;
772 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500773 int ret = 0;
774 int wret;
775 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500776 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400777 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500778 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500779
780 if (level == 0)
781 return 0;
782
Chris Mason5f39d392007-10-15 16:14:19 -0400783 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400784 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500785 WARN_ON(btrfs_header_generation(mid) != trans->transid);
786
Chris Mason1d4f8a02007-03-13 09:28:32 -0400787 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500788
Chris Mason234b63a2007-03-13 10:46:10 -0400789 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400790 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500791 pslot = path->slots[level + 1];
792
Chris Mason40689472007-03-17 14:29:23 -0400793 /*
794 * deal with the case where there is only one pointer in the root
795 * by promoting the node below to a root
796 */
Chris Mason5f39d392007-10-15 16:14:19 -0400797 if (!parent) {
798 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500799
Chris Mason5f39d392007-10-15 16:14:19 -0400800 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500801 return 0;
802
803 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400804 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400805 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500806 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500807 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
808 BUG_ON(ret);
809
Chris Mason925baed2008-06-25 16:01:30 -0400810 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500811 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400812 spin_unlock(&root->node_lock);
813
Chris Mason0b86a832008-03-24 15:01:56 -0400814 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400815 btrfs_tree_unlock(child);
816 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500817 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400818 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400819 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500820 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400821 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500822 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
823 root->root_key.objectid,
824 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500825 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400826 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400827 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500828 }
Chris Mason5f39d392007-10-15 16:14:19 -0400829 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400830 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500831 return 0;
832
Chris Mason5f39d392007-10-15 16:14:19 -0400833 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400834 err_on_enospc = 1;
835
Chris Mason5f39d392007-10-15 16:14:19 -0400836 left = read_node_slot(root, parent, pslot - 1);
837 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400838 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400839 wret = btrfs_cow_block(trans, root, left,
840 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400841 if (wret) {
842 ret = wret;
843 goto enospc;
844 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400845 }
Chris Mason5f39d392007-10-15 16:14:19 -0400846 right = read_node_slot(root, parent, pslot + 1);
847 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400848 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400849 wret = btrfs_cow_block(trans, root, right,
850 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400851 if (wret) {
852 ret = wret;
853 goto enospc;
854 }
855 }
856
857 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400858 if (left) {
859 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400860 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500861 if (wret < 0)
862 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400863 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400864 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500865 }
Chris Mason79f95c82007-03-01 15:16:26 -0500866
867 /*
868 * then try to empty the right most buffer into the middle
869 */
Chris Mason5f39d392007-10-15 16:14:19 -0400870 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400871 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400872 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500873 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400874 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400875 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500876 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400877 u32 blocksize = right->len;
878
Chris Mason5f39d392007-10-15 16:14:19 -0400879 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400880 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400881 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500882 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400883 wret = del_ptr(trans, root, path, level + 1, pslot +
884 1);
Chris Masonbb803952007-03-01 12:04:21 -0500885 if (wret)
886 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400887 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500888 blocksize,
889 btrfs_header_owner(parent),
890 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500891 if (wret)
892 ret = wret;
893 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400894 struct btrfs_disk_key right_key;
895 btrfs_node_key(right, &right_key, 0);
896 btrfs_set_node_key(parent, &right_key, pslot + 1);
897 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500898 }
899 }
Chris Mason5f39d392007-10-15 16:14:19 -0400900 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500901 /*
902 * we're not allowed to leave a node with one item in the
903 * tree during a delete. A deletion from lower in the tree
904 * could try to delete the only pointer in this node.
905 * So, pull some keys from the left.
906 * There has to be a left pointer at this point because
907 * otherwise we would have pulled some pointers from the
908 * right
909 */
Chris Mason5f39d392007-10-15 16:14:19 -0400910 BUG_ON(!left);
911 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400912 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500913 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400914 goto enospc;
915 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400916 if (wret == 1) {
917 wret = push_node_left(trans, root, left, mid, 1);
918 if (wret < 0)
919 ret = wret;
920 }
Chris Mason79f95c82007-03-01 15:16:26 -0500921 BUG_ON(wret == 1);
922 }
Chris Mason5f39d392007-10-15 16:14:19 -0400923 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500924 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500925 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400926 u64 bytenr = mid->start;
927 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400928
Chris Mason5f39d392007-10-15 16:14:19 -0400929 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400930 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400931 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500932 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400933 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500934 if (wret)
935 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500936 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
937 btrfs_header_owner(parent),
938 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500939 if (wret)
940 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500941 } else {
942 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400943 struct btrfs_disk_key mid_key;
944 btrfs_node_key(mid, &mid_key, 0);
945 btrfs_set_node_key(parent, &mid_key, pslot);
946 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500947 }
Chris Masonbb803952007-03-01 12:04:21 -0500948
Chris Mason79f95c82007-03-01 15:16:26 -0500949 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (left) {
951 if (btrfs_header_nritems(left) > orig_slot) {
952 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400953 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400954 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500955 path->slots[level + 1] -= 1;
956 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -0400957 if (mid) {
958 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400959 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400960 }
Chris Masonbb803952007-03-01 12:04:21 -0500961 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400962 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500963 path->slots[level] = orig_slot;
964 }
965 }
Chris Mason79f95c82007-03-01 15:16:26 -0500966 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400967 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400968 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400969 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500970 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400971enospc:
Chris Mason925baed2008-06-25 16:01:30 -0400972 if (right) {
973 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400974 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -0400975 }
976 if (left) {
977 if (path->nodes[level] != left)
978 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400979 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -0400980 }
Chris Masonbb803952007-03-01 12:04:21 -0500981 return ret;
982}
983
Chris Masone66f7092007-04-20 13:16:02 -0400984/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500985static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400988{
Chris Mason5f39d392007-10-15 16:14:19 -0400989 struct extent_buffer *right = NULL;
990 struct extent_buffer *mid;
991 struct extent_buffer *left = NULL;
992 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400993 int ret = 0;
994 int wret;
995 int pslot;
996 int orig_slot = path->slots[level];
997 u64 orig_ptr;
998
999 if (level == 0)
1000 return 1;
1001
Chris Mason5f39d392007-10-15 16:14:19 -04001002 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001003 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001004 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1005
1006 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001007 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001008 pslot = path->slots[level + 1];
1009
Chris Mason5f39d392007-10-15 16:14:19 -04001010 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001011 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001012
Chris Mason5f39d392007-10-15 16:14:19 -04001013 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001014
1015 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001016 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001017 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001018
1019 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001020 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001021 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1022 wret = 1;
1023 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001024 ret = btrfs_cow_block(trans, root, left, parent,
1025 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001026 if (ret)
1027 wret = 1;
1028 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001029 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001030 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001031 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001032 }
Chris Masone66f7092007-04-20 13:16:02 -04001033 if (wret < 0)
1034 ret = wret;
1035 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001036 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001037 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001038 btrfs_node_key(mid, &disk_key, 0);
1039 btrfs_set_node_key(parent, &disk_key, pslot);
1040 btrfs_mark_buffer_dirty(parent);
1041 if (btrfs_header_nritems(left) > orig_slot) {
1042 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001043 path->slots[level + 1] -= 1;
1044 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001045 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001046 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001047 } else {
1048 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001049 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001050 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001051 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001052 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001053 }
Chris Masone66f7092007-04-20 13:16:02 -04001054 return 0;
1055 }
Chris Mason925baed2008-06-25 16:01:30 -04001056 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001057 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001058 }
Chris Mason925baed2008-06-25 16:01:30 -04001059 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001060
1061 /*
1062 * then try to empty the right most buffer into the middle
1063 */
Chris Mason5f39d392007-10-15 16:14:19 -04001064 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001065 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001066 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001067 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001068 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1069 wret = 1;
1070 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001071 ret = btrfs_cow_block(trans, root, right,
1072 parent, pslot + 1,
1073 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001074 if (ret)
1075 wret = 1;
1076 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001077 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001078 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001079 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001080 }
Chris Masone66f7092007-04-20 13:16:02 -04001081 if (wret < 0)
1082 ret = wret;
1083 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001084 struct btrfs_disk_key disk_key;
1085
1086 btrfs_node_key(right, &disk_key, 0);
1087 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1088 btrfs_mark_buffer_dirty(parent);
1089
1090 if (btrfs_header_nritems(mid) <= orig_slot) {
1091 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001092 path->slots[level + 1] += 1;
1093 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001094 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001095 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001096 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001097 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001098 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001099 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001100 }
Chris Masone66f7092007-04-20 13:16:02 -04001101 return 0;
1102 }
Chris Mason925baed2008-06-25 16:01:30 -04001103 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001104 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001105 }
Chris Masone66f7092007-04-20 13:16:02 -04001106 return 1;
1107}
1108
Chris Mason74123bd2007-02-02 11:05:29 -05001109/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001110 * readahead one full node of leaves
1111 */
1112static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001113 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001114{
Chris Mason5f39d392007-10-15 16:14:19 -04001115 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001116 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001117 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001118 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001119 u64 lowest_read;
1120 u64 highest_read;
1121 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001122 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001123 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001124 u32 nr;
1125 u32 blocksize;
1126 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001127
Chris Masona6b6e752007-10-15 16:22:39 -04001128 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001129 return;
1130
Chris Mason6702ed42007-08-07 16:15:09 -04001131 if (!path->nodes[level])
1132 return;
1133
Chris Mason5f39d392007-10-15 16:14:19 -04001134 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001135 WARN_ON(!path->skip_locking && !btrfs_tree_locked(node));
1136
Chris Mason3c69fae2007-08-07 15:52:22 -04001137 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001138 blocksize = btrfs_level_size(root, level - 1);
1139 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001140 if (eb) {
1141 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001142 return;
1143 }
1144
Chris Mason6b800532007-10-15 16:17:34 -04001145 highest_read = search;
1146 lowest_read = search;
1147
Chris Mason5f39d392007-10-15 16:14:19 -04001148 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001149 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001150 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001151 if (direction < 0) {
1152 if (nr == 0)
1153 break;
1154 nr--;
1155 } else if (direction > 0) {
1156 nr++;
1157 if (nr >= nritems)
1158 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001159 }
Chris Mason01f46652007-12-21 16:24:26 -05001160 if (path->reada < 0 && objectid) {
1161 btrfs_node_key(node, &disk_key, nr);
1162 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1163 break;
1164 }
Chris Mason6b800532007-10-15 16:17:34 -04001165 search = btrfs_node_blockptr(node, nr);
1166 if ((search >= lowest_read && search <= highest_read) ||
1167 (search < lowest_read && lowest_read - search <= 32768) ||
1168 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001169 readahead_tree_block(root, search, blocksize,
1170 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001171 nread += blocksize;
1172 }
1173 nscan++;
1174 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1175 break;
1176 if(nread > (1024 * 1024) || nscan > 128)
1177 break;
1178
1179 if (search < lowest_read)
1180 lowest_read = search;
1181 if (search > highest_read)
1182 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001183 }
1184}
Chris Mason925baed2008-06-25 16:01:30 -04001185
1186static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1187{
1188 int i;
1189 int skip_level = level;
1190 struct extent_buffer *t;
1191
1192 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1193 if (!path->nodes[i])
1194 break;
1195 if (!path->locks[i])
1196 break;
1197 if (path->slots[i] == 0) {
1198 skip_level = i + 1;
1199 continue;
1200 }
1201 if (path->keep_locks) {
1202 u32 nritems;
1203 t = path->nodes[i];
1204 nritems = btrfs_header_nritems(t);
1205 if (path->slots[i] >= nritems - 1) {
1206 skip_level = i + 1;
1207 continue;
1208 }
1209 }
1210 t = path->nodes[i];
1211 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1212 btrfs_tree_unlock(t);
1213 path->locks[i] = 0;
1214 }
1215 }
1216}
1217
Chris Mason3c69fae2007-08-07 15:52:22 -04001218/*
Chris Mason74123bd2007-02-02 11:05:29 -05001219 * look for key in the tree. path is filled in with nodes along the way
1220 * if key is found, we return zero and you can find the item in the leaf
1221 * level of the path (level 0)
1222 *
1223 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001224 * be inserted, and 1 is returned. If there are other errors during the
1225 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001226 *
1227 * if ins_len > 0, nodes and leaves will be split as we walk down the
1228 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1229 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001230 */
Chris Masone089f052007-03-16 16:20:31 -04001231int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1232 *root, struct btrfs_key *key, struct btrfs_path *p, int
1233 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001234{
Chris Mason5f39d392007-10-15 16:14:19 -04001235 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236 int slot;
1237 int ret;
1238 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001239 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001240 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001241 u8 lowest_level = 0;
1242
Chris Mason6702ed42007-08-07 16:15:09 -04001243 lowest_level = p->lowest_level;
1244 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001245 WARN_ON(p->nodes[0] != NULL);
Chris Mason925baed2008-06-25 16:01:30 -04001246 // WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1247 WARN_ON(root == root->fs_info->extent_root &&
1248 !mutex_is_locked(&root->fs_info->alloc_mutex));
1249 WARN_ON(root == root->fs_info->chunk_root &&
1250 !mutex_is_locked(&root->fs_info->chunk_mutex));
1251 WARN_ON(root == root->fs_info->dev_root &&
1252 !mutex_is_locked(&root->fs_info->chunk_mutex));
1253 if (ins_len < 0)
1254 lowest_unlock = 2;
Chris Masonbb803952007-03-01 12:04:21 -05001255again:
Chris Mason925baed2008-06-25 16:01:30 -04001256 if (!p->skip_locking)
1257 b = btrfs_lock_root_node(root);
1258 else
1259 b = btrfs_root_node(root);
1260
Chris Masoneb60cea2007-02-02 09:18:22 -05001261 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001262 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001263 if (cow) {
1264 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001265 wret = btrfs_cow_block(trans, root, b,
1266 p->nodes[level + 1],
1267 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001268 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001269 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001270 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001271 return wret;
1272 }
Chris Mason02217ed2007-03-02 16:08:05 -05001273 }
1274 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001275 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001276 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001277 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001278 p->nodes[level] = b;
Chris Mason925baed2008-06-25 16:01:30 -04001279 if (!p->skip_locking)
1280 p->locks[level] = 1;
Chris Mason123abc82007-03-14 14:14:43 -04001281 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001282 if (ret)
1283 return -1;
Chris Mason925baed2008-06-25 16:01:30 -04001284
Chris Mason5f39d392007-10-15 16:14:19 -04001285 ret = bin_search(b, key, level, &slot);
1286 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001287 if (ret && slot > 0)
1288 slot -= 1;
1289 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001290 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001291 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001292 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001293 BUG_ON(sret > 0);
1294 if (sret)
1295 return sret;
1296 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001297 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001298 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001299 int sret = balance_level(trans, root, p,
1300 level);
Chris Masonbb803952007-03-01 12:04:21 -05001301 if (sret)
1302 return sret;
1303 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001304 if (!b) {
1305 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001306 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001307 }
Chris Masonbb803952007-03-01 12:04:21 -05001308 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001309 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001310 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001311 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001312 if (level == lowest_level) {
1313 unlock_up(p, level, lowest_unlock);
Chris Mason9f3a7422007-08-07 15:52:19 -04001314 break;
Chris Mason925baed2008-06-25 16:01:30 -04001315 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001316
Chris Mason6702ed42007-08-07 16:15:09 -04001317 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001318 reada_for_search(root, p, level, slot,
1319 key->objectid);
Chris Masonca7a79a2008-05-12 12:59:19 -04001320
1321 b = read_node_slot(root, b, slot);
Chris Mason925baed2008-06-25 16:01:30 -04001322 if (!p->skip_locking)
1323 btrfs_tree_lock(b);
1324 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001325 } else {
1326 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001327 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001328 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001329 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001330 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001331 BUG_ON(sret > 0);
1332 if (sret)
1333 return sret;
1334 }
Chris Mason925baed2008-06-25 16:01:30 -04001335 unlock_up(p, level, lowest_unlock);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001336 return ret;
1337 }
1338 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001339 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001340}
1341
Chris Mason74123bd2007-02-02 11:05:29 -05001342/*
1343 * adjust the pointers going up the tree, starting at level
1344 * making sure the right key of each node is points to 'key'.
1345 * This is used after shifting pointers to the left, so it stops
1346 * fixing up pointers when a given leaf/node is not in slot 0 of the
1347 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001348 *
1349 * If this fails to write a tree block, it returns -1, but continues
1350 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001351 */
Chris Mason5f39d392007-10-15 16:14:19 -04001352static int fixup_low_keys(struct btrfs_trans_handle *trans,
1353 struct btrfs_root *root, struct btrfs_path *path,
1354 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001355{
1356 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001357 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001358 struct extent_buffer *t;
1359
Chris Mason234b63a2007-03-13 10:46:10 -04001360 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001361 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001362 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001363 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001364 t = path->nodes[i];
1365 btrfs_set_node_key(t, key, tslot);
Chris Mason925baed2008-06-25 16:01:30 -04001366 if (!btrfs_tree_locked(path->nodes[i])) {
1367 int ii;
1368printk("fixup without lock on level %d\n", btrfs_header_level(path->nodes[i]));
1369 for (ii = 0; ii < BTRFS_MAX_LEVEL; ii++) {
1370printk("level %d slot %d\n", ii, path->slots[ii]);
1371 }
1372 }
Chris Masond6025572007-03-30 14:27:56 -04001373 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001374 if (tslot != 0)
1375 break;
1376 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001377 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001378}
1379
Chris Mason74123bd2007-02-02 11:05:29 -05001380/*
1381 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001382 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001383 *
1384 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1385 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001386 */
Chris Mason98ed5172008-01-03 10:01:48 -05001387static int push_node_left(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001389 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001390{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001391 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001392 int src_nritems;
1393 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001394 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001395
Chris Mason5f39d392007-10-15 16:14:19 -04001396 src_nritems = btrfs_header_nritems(src);
1397 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001398 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001399 WARN_ON(btrfs_header_generation(src) != trans->transid);
1400 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001401
Chris Masonbce4eae2008-04-24 14:42:46 -04001402 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001403 return 1;
1404
Chris Masoneb60cea2007-02-02 09:18:22 -05001405 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001406 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001407 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001408
Chris Masonbce4eae2008-04-24 14:42:46 -04001409 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001410 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001411 if (push_items < src_nritems) {
1412 /* leave at least 8 pointers in the node if
1413 * we aren't going to empty it
1414 */
1415 if (src_nritems - push_items < 8) {
1416 if (push_items <= 8)
1417 return 1;
1418 push_items -= 8;
1419 }
1420 }
1421 } else
1422 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001423
Chris Mason5f39d392007-10-15 16:14:19 -04001424 copy_extent_buffer(dst, src,
1425 btrfs_node_key_ptr_offset(dst_nritems),
1426 btrfs_node_key_ptr_offset(0),
1427 push_items * sizeof(struct btrfs_key_ptr));
1428
Chris Masonbb803952007-03-01 12:04:21 -05001429 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001430 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1431 btrfs_node_key_ptr_offset(push_items),
1432 (src_nritems - push_items) *
1433 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001434 }
Chris Mason5f39d392007-10-15 16:14:19 -04001435 btrfs_set_header_nritems(src, src_nritems - push_items);
1436 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1437 btrfs_mark_buffer_dirty(src);
1438 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001439 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001440}
1441
Chris Mason97571fd2007-02-24 13:39:08 -05001442/*
Chris Mason79f95c82007-03-01 15:16:26 -05001443 * try to push data from one node into the next node right in the
1444 * tree.
1445 *
1446 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1447 * error, and > 0 if there was no room in the right hand block.
1448 *
1449 * this will only push up to 1/2 the contents of the left node over
1450 */
Chris Mason5f39d392007-10-15 16:14:19 -04001451static int balance_node_right(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root,
1453 struct extent_buffer *dst,
1454 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001455{
Chris Mason79f95c82007-03-01 15:16:26 -05001456 int push_items = 0;
1457 int max_push;
1458 int src_nritems;
1459 int dst_nritems;
1460 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001461
Chris Mason7bb86312007-12-11 09:25:06 -05001462 WARN_ON(btrfs_header_generation(src) != trans->transid);
1463 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1464
Chris Mason5f39d392007-10-15 16:14:19 -04001465 src_nritems = btrfs_header_nritems(src);
1466 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001467 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001468 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001469 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001470 }
1471
1472 if (src_nritems < 4) {
1473 return 1;
1474 }
Chris Mason79f95c82007-03-01 15:16:26 -05001475
1476 max_push = src_nritems / 2 + 1;
1477 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001478 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001479 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001480 }
Yan252c38f2007-08-29 09:11:44 -04001481
Chris Mason79f95c82007-03-01 15:16:26 -05001482 if (max_push < push_items)
1483 push_items = max_push;
1484
Chris Mason5f39d392007-10-15 16:14:19 -04001485 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1486 btrfs_node_key_ptr_offset(0),
1487 (dst_nritems) *
1488 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001489
Chris Mason5f39d392007-10-15 16:14:19 -04001490 copy_extent_buffer(dst, src,
1491 btrfs_node_key_ptr_offset(0),
1492 btrfs_node_key_ptr_offset(src_nritems - push_items),
1493 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001494
Chris Mason5f39d392007-10-15 16:14:19 -04001495 btrfs_set_header_nritems(src, src_nritems - push_items);
1496 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001497
Chris Mason5f39d392007-10-15 16:14:19 -04001498 btrfs_mark_buffer_dirty(src);
1499 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001500 return ret;
1501}
1502
1503/*
Chris Mason97571fd2007-02-24 13:39:08 -05001504 * helper function to insert a new root level in the tree.
1505 * A new node is allocated, and a single item is inserted to
1506 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001507 *
1508 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001509 */
Chris Mason98ed5172008-01-03 10:01:48 -05001510static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001511 struct btrfs_root *root,
1512 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001513{
Chris Mason7bb86312007-12-11 09:25:06 -05001514 u64 root_gen;
1515 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001516 struct extent_buffer *lower;
1517 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001518 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001519 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001520
1521 BUG_ON(path->nodes[level]);
1522 BUG_ON(path->nodes[level-1] != root->node);
1523
Chris Mason7bb86312007-12-11 09:25:06 -05001524 if (root->ref_cows)
1525 root_gen = trans->transid;
1526 else
1527 root_gen = 0;
1528
1529 lower = path->nodes[level-1];
1530 if (level == 1)
1531 btrfs_item_key(lower, &lower_key, 0);
1532 else
1533 btrfs_node_key(lower, &lower_key, 0);
1534
Chris Mason925baed2008-06-25 16:01:30 -04001535 c = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001536 root->root_key.objectid,
1537 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001538 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001539 if (IS_ERR(c))
1540 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001541
Chris Mason5f39d392007-10-15 16:14:19 -04001542 memset_extent_buffer(c, 0, 0, root->nodesize);
1543 btrfs_set_header_nritems(c, 1);
1544 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001545 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001546 btrfs_set_header_generation(c, trans->transid);
1547 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001548
Chris Mason5f39d392007-10-15 16:14:19 -04001549 write_extent_buffer(c, root->fs_info->fsid,
1550 (unsigned long)btrfs_header_fsid(c),
1551 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001552
1553 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1554 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1555 BTRFS_UUID_SIZE);
1556
Chris Mason5f39d392007-10-15 16:14:19 -04001557 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001558 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001559 lower_gen = btrfs_header_generation(lower);
1560 WARN_ON(lower_gen == 0);
1561
1562 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001563
1564 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001565
Chris Mason925baed2008-06-25 16:01:30 -04001566 spin_lock(&root->node_lock);
1567 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001568 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001569 spin_unlock(&root->node_lock);
1570
1571 /* the super has an extra ref to root->node */
1572 free_extent_buffer(old);
1573
Chris Mason0b86a832008-03-24 15:01:56 -04001574 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001575 extent_buffer_get(c);
1576 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001577 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001578 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001579
1580 if (root->ref_cows && lower_gen != trans->transid) {
1581 struct btrfs_path *back_path = btrfs_alloc_path();
1582 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04001583 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001584 ret = btrfs_insert_extent_backref(trans,
1585 root->fs_info->extent_root,
1586 path, lower->start,
1587 root->root_key.objectid,
1588 trans->transid, 0, 0);
1589 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001590 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason7bb86312007-12-11 09:25:06 -05001591 btrfs_free_path(back_path);
1592 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001593 return 0;
1594}
1595
Chris Mason74123bd2007-02-02 11:05:29 -05001596/*
1597 * worker function to insert a single pointer in a node.
1598 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001599 *
Chris Mason74123bd2007-02-02 11:05:29 -05001600 * slot and level indicate where you want the key to go, and
1601 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001602 *
1603 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001604 */
Chris Masone089f052007-03-16 16:20:31 -04001605static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1606 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001607 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001608{
Chris Mason5f39d392007-10-15 16:14:19 -04001609 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001610 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001611
1612 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001613 lower = path->nodes[level];
1614 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001615 if (slot > nritems)
1616 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001617 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001618 BUG();
1619 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001620 memmove_extent_buffer(lower,
1621 btrfs_node_key_ptr_offset(slot + 1),
1622 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001623 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001624 }
Chris Mason5f39d392007-10-15 16:14:19 -04001625 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001626 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001627 WARN_ON(trans->transid == 0);
1628 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001629 btrfs_set_header_nritems(lower, nritems + 1);
1630 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001631 return 0;
1632}
1633
Chris Mason97571fd2007-02-24 13:39:08 -05001634/*
1635 * split the node at the specified level in path in two.
1636 * The path is corrected to point to the appropriate node after the split
1637 *
1638 * Before splitting this tries to make some room in the node by pushing
1639 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001640 *
1641 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001642 */
Chris Masone089f052007-03-16 16:20:31 -04001643static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1644 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001645{
Chris Mason7bb86312007-12-11 09:25:06 -05001646 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001647 struct extent_buffer *c;
1648 struct extent_buffer *split;
1649 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001650 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001651 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001652 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001653 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001654
Chris Mason5f39d392007-10-15 16:14:19 -04001655 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001656 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001657 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001658 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001659 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001660 if (ret)
1661 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001662 } else {
1663 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001664 c = path->nodes[level];
1665 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001666 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001667 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001668 if (ret < 0)
1669 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001670 }
Chris Masone66f7092007-04-20 13:16:02 -04001671
Chris Mason5f39d392007-10-15 16:14:19 -04001672 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001673 if (root->ref_cows)
1674 root_gen = trans->transid;
1675 else
1676 root_gen = 0;
1677
1678 btrfs_node_key(c, &disk_key, 0);
Chris Mason925baed2008-06-25 16:01:30 -04001679 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Chris Mason7bb86312007-12-11 09:25:06 -05001680 root->root_key.objectid,
1681 root_gen,
1682 btrfs_disk_key_objectid(&disk_key),
1683 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001684 if (IS_ERR(split))
1685 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001686
Chris Mason5f39d392007-10-15 16:14:19 -04001687 btrfs_set_header_flags(split, btrfs_header_flags(c));
1688 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001689 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001690 btrfs_set_header_generation(split, trans->transid);
1691 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001692 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001693 write_extent_buffer(split, root->fs_info->fsid,
1694 (unsigned long)btrfs_header_fsid(split),
1695 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001696 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1697 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1698 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001699
Chris Mason7518a232007-03-12 12:01:18 -04001700 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001701
1702 copy_extent_buffer(split, c,
1703 btrfs_node_key_ptr_offset(0),
1704 btrfs_node_key_ptr_offset(mid),
1705 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1706 btrfs_set_header_nritems(split, c_nritems - mid);
1707 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001708 ret = 0;
1709
Chris Mason5f39d392007-10-15 16:14:19 -04001710 btrfs_mark_buffer_dirty(c);
1711 btrfs_mark_buffer_dirty(split);
1712
1713 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001714 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001715 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001716 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001717 if (wret)
1718 ret = wret;
1719
Chris Mason5de08d72007-02-24 06:24:44 -05001720 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001721 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04001722 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04001723 free_extent_buffer(c);
1724 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001725 path->slots[level + 1] += 1;
1726 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001727 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04001728 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001729 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001731}
1732
Chris Mason74123bd2007-02-02 11:05:29 -05001733/*
1734 * how many bytes are required to store the items in a leaf. start
1735 * and nr indicate which items in the leaf to check. This totals up the
1736 * space used both by the item structs and the item data
1737 */
Chris Mason5f39d392007-10-15 16:14:19 -04001738static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739{
1740 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001741 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001742 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001743
1744 if (!nr)
1745 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001746 data_len = btrfs_item_end_nr(l, start);
1747 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001748 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001749 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001750 return data_len;
1751}
1752
Chris Mason74123bd2007-02-02 11:05:29 -05001753/*
Chris Masond4dbff92007-04-04 14:08:15 -04001754 * The space between the end of the leaf items and
1755 * the start of the leaf data. IOW, how much room
1756 * the leaf has left for both items and data
1757 */
Chris Mason5f39d392007-10-15 16:14:19 -04001758int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001759{
Chris Mason5f39d392007-10-15 16:14:19 -04001760 int nritems = btrfs_header_nritems(leaf);
1761 int ret;
1762 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1763 if (ret < 0) {
1764 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001765 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001766 leaf_space_used(leaf, 0, nritems), nritems);
1767 }
1768 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001769}
1770
1771/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001772 * push some data in the path leaf to the right, trying to free up at
1773 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001774 *
1775 * returns 1 if the push failed because the other node didn't have enough
1776 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001777 */
Chris Masone089f052007-03-16 16:20:31 -04001778static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001779 *root, struct btrfs_path *path, int data_size,
1780 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001781{
Chris Mason5f39d392007-10-15 16:14:19 -04001782 struct extent_buffer *left = path->nodes[0];
1783 struct extent_buffer *right;
1784 struct extent_buffer *upper;
1785 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001786 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001787 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001788 int free_space;
1789 int push_space = 0;
1790 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001791 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001792 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001793 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001794 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001795 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001796 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001797 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001798
1799 slot = path->slots[1];
1800 if (!path->nodes[1]) {
1801 return 1;
1802 }
1803 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001804 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001805 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001806
Chris Masonca7a79a2008-05-12 12:59:19 -04001807 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04001808 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04001809 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001810 if (free_space < data_size + sizeof(struct btrfs_item))
1811 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05001812
Chris Mason5f39d392007-10-15 16:14:19 -04001813 /* cow and double check */
1814 ret = btrfs_cow_block(trans, root, right, upper,
1815 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04001816 if (ret)
1817 goto out_unlock;
1818
Chris Mason5f39d392007-10-15 16:14:19 -04001819 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001820 if (free_space < data_size + sizeof(struct btrfs_item))
1821 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001822
1823 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04001824 if (left_nritems == 0)
1825 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001826
Chris Mason34a38212007-11-07 13:31:03 -05001827 if (empty)
1828 nr = 0;
1829 else
1830 nr = 1;
1831
1832 i = left_nritems - 1;
1833 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001834 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001835
Chris Mason00ec4c52007-02-24 12:47:20 -05001836 if (path->slots[0] == i)
1837 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001838
1839 if (!left->map_token) {
1840 map_extent_buffer(left, (unsigned long)item,
1841 sizeof(struct btrfs_item),
1842 &left->map_token, &left->kaddr,
1843 &left->map_start, &left->map_len,
1844 KM_USER1);
1845 }
1846
1847 this_item_size = btrfs_item_size(left, item);
1848 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001849 break;
1850 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001851 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001852 if (i == 0)
1853 break;
1854 i--;
Chris Masondb945352007-10-15 16:15:53 -04001855 }
1856 if (left->map_token) {
1857 unmap_extent_buffer(left, left->map_token, KM_USER1);
1858 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001859 }
Chris Mason5f39d392007-10-15 16:14:19 -04001860
Chris Mason925baed2008-06-25 16:01:30 -04001861 if (push_items == 0)
1862 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04001863
Chris Mason34a38212007-11-07 13:31:03 -05001864 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001865 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001866
Chris Mason00ec4c52007-02-24 12:47:20 -05001867 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001868 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001869
Chris Mason5f39d392007-10-15 16:14:19 -04001870 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001871 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001872
Chris Mason00ec4c52007-02-24 12:47:20 -05001873 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001874 data_end = leaf_data_end(root, right);
1875 memmove_extent_buffer(right,
1876 btrfs_leaf_data(right) + data_end - push_space,
1877 btrfs_leaf_data(right) + data_end,
1878 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1879
Chris Mason00ec4c52007-02-24 12:47:20 -05001880 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001881 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001882 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1883 btrfs_leaf_data(left) + leaf_data_end(root, left),
1884 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001885
1886 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1887 btrfs_item_nr_offset(0),
1888 right_nritems * sizeof(struct btrfs_item));
1889
Chris Mason00ec4c52007-02-24 12:47:20 -05001890 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001891 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1892 btrfs_item_nr_offset(left_nritems - push_items),
1893 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001894
1895 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001896 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001897 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001898 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001899 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001900 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001901 if (!right->map_token) {
1902 map_extent_buffer(right, (unsigned long)item,
1903 sizeof(struct btrfs_item),
1904 &right->map_token, &right->kaddr,
1905 &right->map_start, &right->map_len,
1906 KM_USER1);
1907 }
1908 push_space -= btrfs_item_size(right, item);
1909 btrfs_set_item_offset(right, item, push_space);
1910 }
1911
1912 if (right->map_token) {
1913 unmap_extent_buffer(right, right->map_token, KM_USER1);
1914 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001915 }
Chris Mason7518a232007-03-12 12:01:18 -04001916 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001917 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001918
Chris Mason34a38212007-11-07 13:31:03 -05001919 if (left_nritems)
1920 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001921 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001922
Chris Mason5f39d392007-10-15 16:14:19 -04001923 btrfs_item_key(right, &disk_key, 0);
1924 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001925 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001926
Chris Mason00ec4c52007-02-24 12:47:20 -05001927 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001928 if (path->slots[0] >= left_nritems) {
1929 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04001930 if (btrfs_header_nritems(path->nodes[0]) == 0)
1931 clean_tree_block(trans, root, path->nodes[0]);
1932 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04001933 free_extent_buffer(path->nodes[0]);
1934 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001935 path->slots[1] += 1;
1936 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001937 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001938 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001939 }
1940 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04001941
1942out_unlock:
1943 btrfs_tree_unlock(right);
1944 free_extent_buffer(right);
1945 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05001946}
Chris Mason925baed2008-06-25 16:01:30 -04001947
Chris Mason00ec4c52007-02-24 12:47:20 -05001948/*
Chris Mason74123bd2007-02-02 11:05:29 -05001949 * push some data in the path leaf to the left, trying to free up at
1950 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1951 */
Chris Masone089f052007-03-16 16:20:31 -04001952static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001953 *root, struct btrfs_path *path, int data_size,
1954 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001955{
Chris Mason5f39d392007-10-15 16:14:19 -04001956 struct btrfs_disk_key disk_key;
1957 struct extent_buffer *right = path->nodes[0];
1958 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001959 int slot;
1960 int i;
1961 int free_space;
1962 int push_space = 0;
1963 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001964 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001965 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001966 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001967 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001968 int ret = 0;
1969 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001970 u32 this_item_size;
1971 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001972
1973 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001974 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001975 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001976 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001977 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001978
Chris Mason3685f792007-10-19 09:23:27 -04001979 right_nritems = btrfs_header_nritems(right);
1980 if (right_nritems == 0) {
1981 return 1;
1982 }
1983
Chris Masonca7a79a2008-05-12 12:59:19 -04001984 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04001985 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04001986 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001987 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04001988 ret = 1;
1989 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001990 }
Chris Mason02217ed2007-03-02 16:08:05 -05001991
1992 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001993 ret = btrfs_cow_block(trans, root, left,
1994 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001995 if (ret) {
1996 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04001997 ret = 1;
1998 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04001999 }
Chris Mason3685f792007-10-19 09:23:27 -04002000
Chris Mason123abc82007-03-14 14:14:43 -04002001 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002002 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002003 ret = 1;
2004 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002005 }
2006
Chris Mason34a38212007-11-07 13:31:03 -05002007 if (empty)
2008 nr = right_nritems;
2009 else
2010 nr = right_nritems - 1;
2011
2012 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002013 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002014 if (!right->map_token) {
2015 map_extent_buffer(right, (unsigned long)item,
2016 sizeof(struct btrfs_item),
2017 &right->map_token, &right->kaddr,
2018 &right->map_start, &right->map_len,
2019 KM_USER1);
2020 }
2021
Chris Masonbe0e5c02007-01-26 15:51:26 -05002022 if (path->slots[0] == i)
2023 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002024
2025 this_item_size = btrfs_item_size(right, item);
2026 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002027 break;
Chris Masondb945352007-10-15 16:15:53 -04002028
Chris Masonbe0e5c02007-01-26 15:51:26 -05002029 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002030 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002031 }
Chris Masondb945352007-10-15 16:15:53 -04002032
2033 if (right->map_token) {
2034 unmap_extent_buffer(right, right->map_token, KM_USER1);
2035 right->map_token = NULL;
2036 }
2037
Chris Masonbe0e5c02007-01-26 15:51:26 -05002038 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002039 ret = 1;
2040 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002041 }
Chris Mason34a38212007-11-07 13:31:03 -05002042 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002043 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002044
Chris Masonbe0e5c02007-01-26 15:51:26 -05002045 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002046 copy_extent_buffer(left, right,
2047 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2048 btrfs_item_nr_offset(0),
2049 push_items * sizeof(struct btrfs_item));
2050
Chris Mason123abc82007-03-14 14:14:43 -04002051 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_item_offset_nr(right, push_items -1);
2053
2054 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002055 leaf_data_end(root, left) - push_space,
2056 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002057 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002058 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002059 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002060 BUG_ON(old_left_nritems < 0);
2061
Chris Masondb945352007-10-15 16:15:53 -04002062 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002063 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002064 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002065
Chris Mason5f39d392007-10-15 16:14:19 -04002066 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002067 if (!left->map_token) {
2068 map_extent_buffer(left, (unsigned long)item,
2069 sizeof(struct btrfs_item),
2070 &left->map_token, &left->kaddr,
2071 &left->map_start, &left->map_len,
2072 KM_USER1);
2073 }
2074
Chris Mason5f39d392007-10-15 16:14:19 -04002075 ioff = btrfs_item_offset(left, item);
2076 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002077 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002078 }
Chris Mason5f39d392007-10-15 16:14:19 -04002079 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002080 if (left->map_token) {
2081 unmap_extent_buffer(left, left->map_token, KM_USER1);
2082 left->map_token = NULL;
2083 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002084
2085 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002086 if (push_items > right_nritems) {
2087 printk("push items %d nr %u\n", push_items, right_nritems);
2088 WARN_ON(1);
2089 }
Chris Mason5f39d392007-10-15 16:14:19 -04002090
Chris Mason34a38212007-11-07 13:31:03 -05002091 if (push_items < right_nritems) {
2092 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2093 leaf_data_end(root, right);
2094 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2095 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2096 btrfs_leaf_data(right) +
2097 leaf_data_end(root, right), push_space);
2098
2099 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002100 btrfs_item_nr_offset(push_items),
2101 (btrfs_header_nritems(right) - push_items) *
2102 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002103 }
Yaneef1c492007-11-26 10:58:13 -05002104 right_nritems -= push_items;
2105 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002106 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002107 for (i = 0; i < right_nritems; i++) {
2108 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002109
2110 if (!right->map_token) {
2111 map_extent_buffer(right, (unsigned long)item,
2112 sizeof(struct btrfs_item),
2113 &right->map_token, &right->kaddr,
2114 &right->map_start, &right->map_len,
2115 KM_USER1);
2116 }
2117
2118 push_space = push_space - btrfs_item_size(right, item);
2119 btrfs_set_item_offset(right, item, push_space);
2120 }
2121 if (right->map_token) {
2122 unmap_extent_buffer(right, right->map_token, KM_USER1);
2123 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002124 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002125
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002127 if (right_nritems)
2128 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002129
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_item_key(right, &disk_key, 0);
2131 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002132 if (wret)
2133 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002134
2135 /* then fixup the leaf pointer in the path */
2136 if (path->slots[0] < push_items) {
2137 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002138 if (btrfs_header_nritems(path->nodes[0]) == 0)
2139 clean_tree_block(trans, root, path->nodes[0]);
2140 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 free_extent_buffer(path->nodes[0]);
2142 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002143 path->slots[1] -= 1;
2144 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002145 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002147 path->slots[0] -= push_items;
2148 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002149 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002150 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002151out:
2152 btrfs_tree_unlock(left);
2153 free_extent_buffer(left);
2154 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002155}
2156
Chris Mason74123bd2007-02-02 11:05:29 -05002157/*
2158 * split the path's leaf in two, making sure there is at least data_size
2159 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002160 *
2161 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002162 */
Chris Masone089f052007-03-16 16:20:31 -04002163static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04002164 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04002165 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002166{
Chris Mason7bb86312007-12-11 09:25:06 -05002167 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002168 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002169 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002170 int mid;
2171 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002172 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002173 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002174 int data_copy_size;
2175 int rt_data_off;
2176 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002177 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002178 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002179 int double_split;
2180 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002181 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182
Chris Masoncc0c5532007-10-25 15:42:57 -04002183 if (extend)
2184 space_needed = data_size;
2185
Chris Mason7bb86312007-12-11 09:25:06 -05002186 if (root->ref_cows)
2187 root_gen = trans->transid;
2188 else
2189 root_gen = 0;
2190
Chris Mason40689472007-03-17 14:29:23 -04002191 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002192 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002193 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002194 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002195 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002196 }
2197 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002198 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002199 if (wret < 0)
2200 return wret;
2201 }
2202 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002203
Chris Mason3685f792007-10-19 09:23:27 -04002204 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002205 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002206 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002207 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002208
Chris Mason5c680ed2007-02-22 11:39:13 -05002209 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002210 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002211 if (ret)
2212 return ret;
2213 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002214again:
2215 double_split = 0;
2216 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002217 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002218 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002219 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002220
Chris Mason7bb86312007-12-11 09:25:06 -05002221 btrfs_item_key(l, &disk_key, 0);
2222
Chris Mason925baed2008-06-25 16:01:30 -04002223 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Chris Mason7bb86312007-12-11 09:25:06 -05002224 root->root_key.objectid,
2225 root_gen, disk_key.objectid, 0,
2226 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002227 if (IS_ERR(right)) {
2228 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002229 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002230 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002233 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002234 btrfs_set_header_generation(right, trans->transid);
2235 btrfs_set_header_owner(right, root->root_key.objectid);
2236 btrfs_set_header_level(right, 0);
2237 write_extent_buffer(right, root->fs_info->fsid,
2238 (unsigned long)btrfs_header_fsid(right),
2239 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002240
2241 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2242 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2243 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002244 if (mid <= slot) {
2245 if (nritems == 1 ||
2246 leaf_space_used(l, mid, nritems - mid) + space_needed >
2247 BTRFS_LEAF_DATA_SIZE(root)) {
2248 if (slot >= nritems) {
2249 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002250 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002251 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002252 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002253 path->slots[1] + 1, 1);
2254 if (wret)
2255 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002256
2257 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002258 free_extent_buffer(path->nodes[0]);
2259 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002260 path->slots[0] = 0;
2261 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002262 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002263 return ret;
2264 }
2265 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002266 if (mid != nritems &&
2267 leaf_space_used(l, mid, nritems - mid) +
2268 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2269 double_split = 1;
2270 }
Chris Masond4dbff92007-04-04 14:08:15 -04002271 }
2272 } else {
2273 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2274 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002275 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002276 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002277 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002278 wret = insert_ptr(trans, root, path,
2279 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002280 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002281 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002282 if (wret)
2283 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002284 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002285 free_extent_buffer(path->nodes[0]);
2286 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002287 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002288 if (path->slots[1] == 0) {
2289 wret = fixup_low_keys(trans, root,
2290 path, &disk_key, 1);
2291 if (wret)
2292 ret = wret;
2293 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002294 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002295 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002296 } else if (extend && slot == 0) {
2297 mid = 1;
2298 } else {
2299 mid = slot;
2300 if (mid != nritems &&
2301 leaf_space_used(l, mid, nritems - mid) +
2302 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2303 double_split = 1;
2304 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002305 }
Chris Masond4dbff92007-04-04 14:08:15 -04002306 }
2307 }
Chris Mason5f39d392007-10-15 16:14:19 -04002308 nritems = nritems - mid;
2309 btrfs_set_header_nritems(right, nritems);
2310 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2311
2312 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2313 btrfs_item_nr_offset(mid),
2314 nritems * sizeof(struct btrfs_item));
2315
2316 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002317 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2318 data_copy_size, btrfs_leaf_data(l) +
2319 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002320
Chris Mason5f39d392007-10-15 16:14:19 -04002321 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2322 btrfs_item_end_nr(l, mid);
2323
2324 for (i = 0; i < nritems; i++) {
2325 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002326 u32 ioff;
2327
2328 if (!right->map_token) {
2329 map_extent_buffer(right, (unsigned long)item,
2330 sizeof(struct btrfs_item),
2331 &right->map_token, &right->kaddr,
2332 &right->map_start, &right->map_len,
2333 KM_USER1);
2334 }
2335
2336 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002337 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002338 }
Chris Mason74123bd2007-02-02 11:05:29 -05002339
Chris Masondb945352007-10-15 16:15:53 -04002340 if (right->map_token) {
2341 unmap_extent_buffer(right, right->map_token, KM_USER1);
2342 right->map_token = NULL;
2343 }
2344
Chris Mason5f39d392007-10-15 16:14:19 -04002345 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002346 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002347 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002348 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2349 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002350 if (wret)
2351 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002352
2353 btrfs_mark_buffer_dirty(right);
2354 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002355 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002356
Chris Masonbe0e5c02007-01-26 15:51:26 -05002357 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002358 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002359 free_extent_buffer(path->nodes[0]);
2360 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002361 path->slots[0] -= mid;
2362 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002363 } else {
2364 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002365 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002366 }
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Masoneb60cea2007-02-02 09:18:22 -05002368 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002369
Chris Masoncc0c5532007-10-25 15:42:57 -04002370 if (double_split) {
2371 BUG_ON(num_doubles != 0);
2372 num_doubles++;
2373 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002374 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002375 return ret;
2376}
2377
Chris Masonb18c6682007-04-17 13:26:50 -04002378int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2379 struct btrfs_root *root,
2380 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002381 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002382{
2383 int ret = 0;
2384 int slot;
2385 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002386 struct extent_buffer *leaf;
2387 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002388 u32 nritems;
2389 unsigned int data_end;
2390 unsigned int old_data_start;
2391 unsigned int old_size;
2392 unsigned int size_diff;
2393 int i;
2394
2395 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002396 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002397 slot = path->slots[0];
2398
2399 old_size = btrfs_item_size_nr(leaf, slot);
2400 if (old_size == new_size)
2401 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002402
Chris Mason5f39d392007-10-15 16:14:19 -04002403 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002404 data_end = leaf_data_end(root, leaf);
2405
Chris Mason5f39d392007-10-15 16:14:19 -04002406 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002407
Chris Masonb18c6682007-04-17 13:26:50 -04002408 size_diff = old_size - new_size;
2409
2410 BUG_ON(slot < 0);
2411 BUG_ON(slot >= nritems);
2412
2413 /*
2414 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2415 */
2416 /* first correct the data pointers */
2417 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002418 u32 ioff;
2419 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002420
2421 if (!leaf->map_token) {
2422 map_extent_buffer(leaf, (unsigned long)item,
2423 sizeof(struct btrfs_item),
2424 &leaf->map_token, &leaf->kaddr,
2425 &leaf->map_start, &leaf->map_len,
2426 KM_USER1);
2427 }
2428
Chris Mason5f39d392007-10-15 16:14:19 -04002429 ioff = btrfs_item_offset(leaf, item);
2430 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002431 }
Chris Masondb945352007-10-15 16:15:53 -04002432
2433 if (leaf->map_token) {
2434 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2435 leaf->map_token = NULL;
2436 }
2437
Chris Masonb18c6682007-04-17 13:26:50 -04002438 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002439 if (from_end) {
2440 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2441 data_end + size_diff, btrfs_leaf_data(leaf) +
2442 data_end, old_data_start + new_size - data_end);
2443 } else {
2444 struct btrfs_disk_key disk_key;
2445 u64 offset;
2446
2447 btrfs_item_key(leaf, &disk_key, slot);
2448
2449 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2450 unsigned long ptr;
2451 struct btrfs_file_extent_item *fi;
2452
2453 fi = btrfs_item_ptr(leaf, slot,
2454 struct btrfs_file_extent_item);
2455 fi = (struct btrfs_file_extent_item *)(
2456 (unsigned long)fi - size_diff);
2457
2458 if (btrfs_file_extent_type(leaf, fi) ==
2459 BTRFS_FILE_EXTENT_INLINE) {
2460 ptr = btrfs_item_ptr_offset(leaf, slot);
2461 memmove_extent_buffer(leaf, ptr,
2462 (unsigned long)fi,
2463 offsetof(struct btrfs_file_extent_item,
2464 disk_bytenr));
2465 }
2466 }
2467
2468 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2469 data_end + size_diff, btrfs_leaf_data(leaf) +
2470 data_end, old_data_start - data_end);
2471
2472 offset = btrfs_disk_key_offset(&disk_key);
2473 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2474 btrfs_set_item_key(leaf, &disk_key, slot);
2475 if (slot == 0)
2476 fixup_low_keys(trans, root, path, &disk_key, 1);
2477 }
Chris Mason5f39d392007-10-15 16:14:19 -04002478
2479 item = btrfs_item_nr(leaf, slot);
2480 btrfs_set_item_size(leaf, item, new_size);
2481 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002482
2483 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002484 if (btrfs_leaf_free_space(root, leaf) < 0) {
2485 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002486 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002487 }
Chris Masonb18c6682007-04-17 13:26:50 -04002488 return ret;
2489}
2490
Chris Mason5f39d392007-10-15 16:14:19 -04002491int btrfs_extend_item(struct btrfs_trans_handle *trans,
2492 struct btrfs_root *root, struct btrfs_path *path,
2493 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002494{
2495 int ret = 0;
2496 int slot;
2497 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002498 struct extent_buffer *leaf;
2499 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002500 u32 nritems;
2501 unsigned int data_end;
2502 unsigned int old_data;
2503 unsigned int old_size;
2504 int i;
2505
2506 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002507 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002508
Chris Mason5f39d392007-10-15 16:14:19 -04002509 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002510 data_end = leaf_data_end(root, leaf);
2511
Chris Mason5f39d392007-10-15 16:14:19 -04002512 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2513 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002514 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002515 }
Chris Mason6567e832007-04-16 09:22:45 -04002516 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002517 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002518
2519 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002520 if (slot >= nritems) {
2521 btrfs_print_leaf(root, leaf);
2522 printk("slot %d too large, nritems %d\n", slot, nritems);
2523 BUG_ON(1);
2524 }
Chris Mason6567e832007-04-16 09:22:45 -04002525
2526 /*
2527 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2528 */
2529 /* first correct the data pointers */
2530 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002531 u32 ioff;
2532 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002533
2534 if (!leaf->map_token) {
2535 map_extent_buffer(leaf, (unsigned long)item,
2536 sizeof(struct btrfs_item),
2537 &leaf->map_token, &leaf->kaddr,
2538 &leaf->map_start, &leaf->map_len,
2539 KM_USER1);
2540 }
Chris Mason5f39d392007-10-15 16:14:19 -04002541 ioff = btrfs_item_offset(leaf, item);
2542 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002543 }
Chris Mason5f39d392007-10-15 16:14:19 -04002544
Chris Masondb945352007-10-15 16:15:53 -04002545 if (leaf->map_token) {
2546 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2547 leaf->map_token = NULL;
2548 }
2549
Chris Mason6567e832007-04-16 09:22:45 -04002550 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002551 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002552 data_end - data_size, btrfs_leaf_data(leaf) +
2553 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002554
Chris Mason6567e832007-04-16 09:22:45 -04002555 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002556 old_size = btrfs_item_size_nr(leaf, slot);
2557 item = btrfs_item_nr(leaf, slot);
2558 btrfs_set_item_size(leaf, item, old_size + data_size);
2559 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002560
2561 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002562 if (btrfs_leaf_free_space(root, leaf) < 0) {
2563 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002564 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002565 }
Chris Mason6567e832007-04-16 09:22:45 -04002566 return ret;
2567}
2568
Chris Mason74123bd2007-02-02 11:05:29 -05002569/*
2570 * Given a key and some data, insert an item into the tree.
2571 * This does all the path init required, making room in the tree if needed.
2572 */
Chris Mason9c583092008-01-29 15:15:18 -05002573int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002574 struct btrfs_root *root,
2575 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002576 struct btrfs_key *cpu_key, u32 *data_size,
2577 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002578{
Chris Mason5f39d392007-10-15 16:14:19 -04002579 struct extent_buffer *leaf;
2580 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002581 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002583 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002584 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002585 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002586 u32 total_size = 0;
2587 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002589 struct btrfs_disk_key disk_key;
2590
Chris Mason9c583092008-01-29 15:15:18 -05002591 for (i = 0; i < nr; i++) {
2592 total_data += data_size[i];
2593 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002594
Chris Mason9c583092008-01-29 15:15:18 -05002595 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2596 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002597 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002598 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002599 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002600 if (ret < 0)
2601 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602
Chris Mason62e27492007-03-15 12:56:47 -04002603 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002604 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002605
Chris Mason5f39d392007-10-15 16:14:19 -04002606 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002607 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002608
Chris Mason123abc82007-03-14 14:14:43 -04002609 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002610 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002611 btrfs_print_leaf(root, leaf);
2612 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002613 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002615 }
Chris Mason5f39d392007-10-15 16:14:19 -04002616
Chris Mason62e27492007-03-15 12:56:47 -04002617 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002618 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002619
Chris Masonbe0e5c02007-01-26 15:51:26 -05002620 if (slot != nritems) {
2621 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002622 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002623
Chris Mason5f39d392007-10-15 16:14:19 -04002624 if (old_data < data_end) {
2625 btrfs_print_leaf(root, leaf);
2626 printk("slot %d old_data %d data_end %d\n",
2627 slot, old_data, data_end);
2628 BUG_ON(1);
2629 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002630 /*
2631 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2632 */
2633 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002634 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002635 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002636 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002637
Chris Mason5f39d392007-10-15 16:14:19 -04002638 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002639 if (!leaf->map_token) {
2640 map_extent_buffer(leaf, (unsigned long)item,
2641 sizeof(struct btrfs_item),
2642 &leaf->map_token, &leaf->kaddr,
2643 &leaf->map_start, &leaf->map_len,
2644 KM_USER1);
2645 }
2646
Chris Mason5f39d392007-10-15 16:14:19 -04002647 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002648 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002649 }
Chris Masondb945352007-10-15 16:15:53 -04002650 if (leaf->map_token) {
2651 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2652 leaf->map_token = NULL;
2653 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002654
2655 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002656 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002657 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002658 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002659
2660 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002661 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002662 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002663 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002664 data_end = old_data;
2665 }
Chris Mason5f39d392007-10-15 16:14:19 -04002666
Chris Mason62e27492007-03-15 12:56:47 -04002667 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002668 for (i = 0; i < nr; i++) {
2669 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2670 btrfs_set_item_key(leaf, &disk_key, slot + i);
2671 item = btrfs_item_nr(leaf, slot + i);
2672 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2673 data_end -= data_size[i];
2674 btrfs_set_item_size(leaf, item, data_size[i]);
2675 }
2676 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002678
2679 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002680 if (slot == 0) {
2681 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002682 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002683 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002684
Chris Mason5f39d392007-10-15 16:14:19 -04002685 if (btrfs_leaf_free_space(root, leaf) < 0) {
2686 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002688 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002689out:
Chris Mason62e27492007-03-15 12:56:47 -04002690 return ret;
2691}
2692
2693/*
2694 * Given a key and some data, insert an item into the tree.
2695 * This does all the path init required, making room in the tree if needed.
2696 */
Chris Masone089f052007-03-16 16:20:31 -04002697int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2698 *root, struct btrfs_key *cpu_key, void *data, u32
2699 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002700{
2701 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002702 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002703 struct extent_buffer *leaf;
2704 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002705
Chris Mason2c90e5d2007-04-02 10:50:19 -04002706 path = btrfs_alloc_path();
2707 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002708 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002709 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002710 leaf = path->nodes[0];
2711 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2712 write_extent_buffer(leaf, data, ptr, data_size);
2713 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002714 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002715 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002716 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002717}
2718
Chris Mason74123bd2007-02-02 11:05:29 -05002719/*
Chris Mason5de08d72007-02-24 06:24:44 -05002720 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002721 *
2722 * If the delete empties a node, the node is removed from the tree,
2723 * continuing all the way the root if required. The root is converted into
2724 * a leaf if all the nodes are emptied.
2725 */
Chris Masone089f052007-03-16 16:20:31 -04002726static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2727 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002728{
Chris Mason5f39d392007-10-15 16:14:19 -04002729 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002730 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002731 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002732 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002733
Chris Mason5f39d392007-10-15 16:14:19 -04002734 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002735 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002736 memmove_extent_buffer(parent,
2737 btrfs_node_key_ptr_offset(slot),
2738 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002739 sizeof(struct btrfs_key_ptr) *
2740 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002741 }
Chris Mason7518a232007-03-12 12:01:18 -04002742 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002743 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002744 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002745 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002746 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002747 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002748 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002749 struct btrfs_disk_key disk_key;
2750
2751 btrfs_node_key(parent, &disk_key, 0);
2752 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002753 if (wret)
2754 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002755 }
Chris Masond6025572007-03-30 14:27:56 -04002756 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002757 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758}
2759
Chris Mason74123bd2007-02-02 11:05:29 -05002760/*
2761 * delete the item at the leaf level in path. If that empties
2762 * the leaf, remove it from the tree
2763 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002764int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2765 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766{
Chris Mason5f39d392007-10-15 16:14:19 -04002767 struct extent_buffer *leaf;
2768 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002769 int last_off;
2770 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002771 int ret = 0;
2772 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002773 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002774 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002775
Chris Mason5f39d392007-10-15 16:14:19 -04002776 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002777 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2778
2779 for (i = 0; i < nr; i++)
2780 dsize += btrfs_item_size_nr(leaf, slot + i);
2781
Chris Mason5f39d392007-10-15 16:14:19 -04002782 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002783
Chris Mason85e21ba2008-01-29 15:11:36 -05002784 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002785 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002786 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002787
2788 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002789 data_end + dsize,
2790 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002791 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002792
Chris Mason85e21ba2008-01-29 15:11:36 -05002793 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002794 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002795
Chris Mason5f39d392007-10-15 16:14:19 -04002796 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002797 if (!leaf->map_token) {
2798 map_extent_buffer(leaf, (unsigned long)item,
2799 sizeof(struct btrfs_item),
2800 &leaf->map_token, &leaf->kaddr,
2801 &leaf->map_start, &leaf->map_len,
2802 KM_USER1);
2803 }
Chris Mason5f39d392007-10-15 16:14:19 -04002804 ioff = btrfs_item_offset(leaf, item);
2805 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002806 }
Chris Masondb945352007-10-15 16:15:53 -04002807
2808 if (leaf->map_token) {
2809 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2810 leaf->map_token = NULL;
2811 }
2812
Chris Mason5f39d392007-10-15 16:14:19 -04002813 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002814 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002815 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002816 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002817 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002818 btrfs_set_header_nritems(leaf, nritems - nr);
2819 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002820
Chris Mason74123bd2007-02-02 11:05:29 -05002821 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002822 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002823 if (leaf == root->node) {
2824 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002825 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002826 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04002827 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002828 if (wret)
2829 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002830 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002831 leaf->start, leaf->len,
2832 btrfs_header_owner(path->nodes[1]),
2833 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002834 if (wret)
2835 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002836 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002837 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002838 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002839 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002840 struct btrfs_disk_key disk_key;
2841
2842 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002843 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002844 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002845 if (wret)
2846 ret = wret;
2847 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002848
Chris Mason74123bd2007-02-02 11:05:29 -05002849 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002850 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002851 /* push_leaf_left fixes the path.
2852 * make sure the path still points to our leaf
2853 * for possible call to del_ptr below
2854 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002855 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002856 extent_buffer_get(leaf);
2857
Chris Mason85e21ba2008-01-29 15:11:36 -05002858 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002859 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002860 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002861
2862 if (path->nodes[0] == leaf &&
2863 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002864 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002865 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002866 ret = wret;
2867 }
Chris Mason5f39d392007-10-15 16:14:19 -04002868
2869 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002870 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002871 u64 bytenr = leaf->start;
2872 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002873
Chris Mason7bb86312007-12-11 09:25:06 -05002874 root_gen = btrfs_header_generation(
2875 path->nodes[1]);
2876
Chris Masone089f052007-03-16 16:20:31 -04002877 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002878 if (wret)
2879 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002880
2881 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002882 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002883 blocksize,
2884 btrfs_header_owner(path->nodes[1]),
2885 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002886 if (wret)
2887 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002888 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002889 /* if we're still in the path, make sure
2890 * we're dirty. Otherwise, one of the
2891 * push_leaf functions must have already
2892 * dirtied this buffer
2893 */
2894 if (path->nodes[0] == leaf)
2895 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002896 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002897 }
Chris Masond5719762007-03-23 10:01:08 -04002898 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002899 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002900 }
2901 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002902 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002903}
2904
Chris Mason97571fd2007-02-24 13:39:08 -05002905/*
Chris Mason925baed2008-06-25 16:01:30 -04002906 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05002907 * returns 0 if it found something or 1 if there are no lesser leaves.
2908 * returns < 0 on io errors.
2909 */
2910int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2911{
Chris Mason925baed2008-06-25 16:01:30 -04002912 struct btrfs_key key;
2913 struct btrfs_disk_key found_key;
2914 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05002915
Chris Mason925baed2008-06-25 16:01:30 -04002916 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05002917
Chris Mason925baed2008-06-25 16:01:30 -04002918 if (key.offset > 0)
2919 key.offset--;
2920 else if (key.type > 0)
2921 key.type--;
2922 else if (key.objectid > 0)
2923 key.objectid--;
2924 else
2925 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002926
Chris Mason925baed2008-06-25 16:01:30 -04002927 btrfs_release_path(root, path);
2928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2929 if (ret < 0)
2930 return ret;
2931 btrfs_item_key(path->nodes[0], &found_key, 0);
2932 ret = comp_keys(&found_key, &key);
2933 if (ret < 0)
2934 return 0;
2935 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002936}
2937
2938/*
Chris Mason925baed2008-06-25 16:01:30 -04002939 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05002940 * returns 0 if it found something or 1 if there are no greater leaves.
2941 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002942 */
Chris Mason234b63a2007-03-13 10:46:10 -04002943int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002944{
2945 int slot;
2946 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002947 struct extent_buffer *c;
2948 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04002949 struct btrfs_key key;
2950 u32 nritems;
2951 int ret;
2952
2953 nritems = btrfs_header_nritems(path->nodes[0]);
2954 if (nritems == 0) {
2955 return 1;
2956 }
2957
2958 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
2959
2960 path->keep_locks = 1;
2961 btrfs_release_path(root, path);
2962 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2963 path->keep_locks = 0;
2964
2965 if (ret < 0)
2966 return ret;
2967
2968 if (path->slots[0] < nritems - 1) {
2969 goto done;
2970 }
Chris Masond97e63b2007-02-20 16:40:44 -05002971
Chris Mason234b63a2007-03-13 10:46:10 -04002972 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002973 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002974 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002975
Chris Masond97e63b2007-02-20 16:40:44 -05002976 slot = path->slots[level] + 1;
2977 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002978 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002979 level++;
Chris Mason925baed2008-06-25 16:01:30 -04002980 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05002981 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04002982 }
Chris Masond97e63b2007-02-20 16:40:44 -05002983 continue;
2984 }
Chris Mason5f39d392007-10-15 16:14:19 -04002985
Chris Mason925baed2008-06-25 16:01:30 -04002986 if (next) {
2987 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04002988 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04002989 }
Chris Mason5f39d392007-10-15 16:14:19 -04002990
Chris Mason925baed2008-06-25 16:01:30 -04002991 if (level == 1 && path->locks[1] && path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002992 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002993
Chris Masonca7a79a2008-05-12 12:59:19 -04002994 next = read_node_slot(root, c, slot);
Chris Mason925baed2008-06-25 16:01:30 -04002995 if (!path->skip_locking)
2996 btrfs_tree_lock(next);
Chris Masond97e63b2007-02-20 16:40:44 -05002997 break;
2998 }
2999 path->slots[level] = slot;
3000 while(1) {
3001 level--;
3002 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003003 if (path->locks[level])
3004 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003005 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003006 path->nodes[level] = next;
3007 path->slots[level] = 0;
Chris Mason925baed2008-06-25 16:01:30 -04003008 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003009 if (!level)
3010 break;
Chris Mason925baed2008-06-25 16:01:30 -04003011 if (level == 1 && path->locks[1] && path->reada)
3012 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003013 next = read_node_slot(root, next, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003014 if (!path->skip_locking)
3015 btrfs_tree_lock(next);
Chris Masond97e63b2007-02-20 16:40:44 -05003016 }
Chris Mason925baed2008-06-25 16:01:30 -04003017done:
3018 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003019 return 0;
3020}
Chris Mason0b86a832008-03-24 15:01:56 -04003021
3022int btrfs_previous_item(struct btrfs_root *root,
3023 struct btrfs_path *path, u64 min_objectid,
3024 int type)
3025{
3026 struct btrfs_key found_key;
3027 struct extent_buffer *leaf;
3028 int ret;
3029
3030 while(1) {
3031 if (path->slots[0] == 0) {
3032 ret = btrfs_prev_leaf(root, path);
3033 if (ret != 0)
3034 return ret;
3035 } else {
3036 path->slots[0]--;
3037 }
3038 leaf = path->nodes[0];
3039 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3040 if (found_key.type == type)
3041 return 0;
3042 }
3043 return 1;
3044}
3045