blob: 5697705f7530d2eb2360e718eba9b8a92908fc81 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040028 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040029 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040030static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src);
33static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040037static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050039
Chris Masondf24a2b2007-04-04 09:36:31 -040040inline void btrfs_init_path(struct btrfs_path *p)
41{
42 memset(p, 0, sizeof(*p));
43}
44
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040049 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040050 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040051 path->reada = 1;
52 }
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
56void btrfs_free_path(struct btrfs_path *p)
57{
Chris Masondf24a2b2007-04-04 09:36:31 -040058 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040059 kmem_cache_free(btrfs_path_cachep, p);
60}
61
Chris Mason234b63a2007-03-13 10:46:10 -040062void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050063{
64 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040065 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050066 if (!p->nodes[i])
67 break;
Chris Mason5f39d392007-10-15 16:14:19 -040068 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050069 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050070 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050071}
72
Chris Mason5f39d392007-10-15 16:14:19 -040073static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 struct extent_buffer *buf,
76 struct extent_buffer *parent, int parent_slot,
77 struct extent_buffer **cow_ret,
78 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -040079{
Chris Mason5f39d392007-10-15 16:14:19 -040080 struct extent_buffer *cow;
Chris Mason6702ed42007-08-07 16:15:09 -040081 int ret = 0;
82 int different_trans = 0;
83
84 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -040085
Chris Masondb945352007-10-15 16:15:53 -040086 cow = btrfs_alloc_free_block(trans, root, buf->len,
87 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -040088 if (IS_ERR(cow))
89 return PTR_ERR(cow);
90
Chris Mason5f39d392007-10-15 16:14:19 -040091 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -040092 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -040093 btrfs_set_header_generation(cow, trans->transid);
94 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -040095
Chris Mason5f39d392007-10-15 16:14:19 -040096 WARN_ON(btrfs_header_generation(buf) > trans->transid);
97 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -040098 different_trans = 1;
99 ret = btrfs_inc_ref(trans, root, buf);
100 if (ret)
101 return ret;
102 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400103 clean_tree_block(trans, root, buf);
104 }
105
106 if (buf == root->node) {
107 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400108 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400109 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400110 btrfs_free_extent(trans, root, buf->start,
111 buf->len, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400112 }
Chris Mason5f39d392007-10-15 16:14:19 -0400113 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400114 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400115 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400116 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500117 WARN_ON(trans->transid == 0);
118 btrfs_set_node_ptr_generation(parent, parent_slot,
119 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400120 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400121 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Masondb945352007-10-15 16:15:53 -0400122 btrfs_free_extent(trans, root, buf->start, buf->len, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400123 }
Chris Mason5f39d392007-10-15 16:14:19 -0400124 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400125 btrfs_mark_buffer_dirty(cow);
126 *cow_ret = cow;
127 return 0;
128}
129
Chris Mason5f39d392007-10-15 16:14:19 -0400130int btrfs_cow_block(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root, struct extent_buffer *buf,
132 struct extent_buffer *parent, int parent_slot,
133 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500134{
Chris Mason6702ed42007-08-07 16:15:09 -0400135 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400136 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400137 if (trans->transaction != root->fs_info->running_transaction) {
138 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
139 root->fs_info->running_transaction->transid);
140 WARN_ON(1);
141 }
142 if (trans->transid != root->fs_info->generation) {
143 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
144 root->fs_info->generation);
145 WARN_ON(1);
146 }
Chris Mason5f39d392007-10-15 16:14:19 -0400147 if (btrfs_header_generation(buf) == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500148 *cow_ret = buf;
149 return 0;
150 }
Chris Mason6702ed42007-08-07 16:15:09 -0400151
Chris Masondb945352007-10-15 16:15:53 -0400152 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400153 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400154 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400155 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400156}
157
Chris Mason6b800532007-10-15 16:17:34 -0400158static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400159{
Chris Mason6b800532007-10-15 16:17:34 -0400160 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400161 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400162 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400163 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500164 return 0;
165}
166
Chris Mason081e9572007-11-06 10:26:24 -0500167/*
168 * compare two keys in a memcmp fashion
169 */
170static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
171{
172 struct btrfs_key k1;
173
174 btrfs_disk_key_to_cpu(&k1, disk);
175
176 if (k1.objectid > k2->objectid)
177 return 1;
178 if (k1.objectid < k2->objectid)
179 return -1;
180 if (k1.type > k2->type)
181 return 1;
182 if (k1.type < k2->type)
183 return -1;
184 if (k1.offset > k2->offset)
185 return 1;
186 if (k1.offset < k2->offset)
187 return -1;
188 return 0;
189}
190
191
Chris Mason6702ed42007-08-07 16:15:09 -0400192int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400193 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400194 int start_slot, int cache_only, u64 *last_ret,
195 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400196{
Chris Mason6b800532007-10-15 16:17:34 -0400197 struct extent_buffer *cur;
198 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400199 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400200 u64 search_start = *last_ret;
201 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400202 u64 other;
203 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400204 int end_slot;
205 int i;
206 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400207 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400208 int uptodate;
209 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500210 int progress_passed = 0;
211 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400212
Chris Mason5708b952007-10-25 15:43:18 -0400213 parent_level = btrfs_header_level(parent);
214 if (cache_only && parent_level != 1)
215 return 0;
216
Chris Mason6702ed42007-08-07 16:15:09 -0400217 if (trans->transaction != root->fs_info->running_transaction) {
218 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
219 root->fs_info->running_transaction->transid);
220 WARN_ON(1);
221 }
222 if (trans->transid != root->fs_info->generation) {
223 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
224 root->fs_info->generation);
225 WARN_ON(1);
226 }
Chris Mason86479a02007-09-10 19:58:16 -0400227
Chris Mason6b800532007-10-15 16:17:34 -0400228 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400229 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400230 end_slot = parent_nritems;
231
232 if (parent_nritems == 1)
233 return 0;
234
235 for (i = start_slot; i < end_slot; i++) {
236 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400237
Chris Mason5708b952007-10-25 15:43:18 -0400238 if (!parent->map_token) {
239 map_extent_buffer(parent,
240 btrfs_node_key_ptr_offset(i),
241 sizeof(struct btrfs_key_ptr),
242 &parent->map_token, &parent->kaddr,
243 &parent->map_start, &parent->map_len,
244 KM_USER1);
245 }
Chris Mason081e9572007-11-06 10:26:24 -0500246 btrfs_node_key(parent, &disk_key, i);
247 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
248 continue;
249
250 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400251 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400252 if (last_block == 0)
253 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400254
Chris Mason6702ed42007-08-07 16:15:09 -0400255 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400256 other = btrfs_node_blockptr(parent, i - 1);
257 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400258 }
Chris Mason5708b952007-10-25 15:43:18 -0400259 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400260 other = btrfs_node_blockptr(parent, i + 1);
261 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400262 }
Chris Masone9d0b132007-08-10 14:06:19 -0400263 if (close) {
264 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400265 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400266 }
Chris Mason5708b952007-10-25 15:43:18 -0400267 if (parent->map_token) {
268 unmap_extent_buffer(parent, parent->map_token,
269 KM_USER1);
270 parent->map_token = NULL;
271 }
Chris Mason6702ed42007-08-07 16:15:09 -0400272
Chris Mason6b800532007-10-15 16:17:34 -0400273 cur = btrfs_find_tree_block(root, blocknr, blocksize);
274 if (cur)
275 uptodate = btrfs_buffer_uptodate(cur);
276 else
277 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400278 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400279 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400280 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400281 continue;
282 }
Chris Mason6b800532007-10-15 16:17:34 -0400283 if (!cur) {
284 cur = read_tree_block(root, blocknr,
285 blocksize);
286 } else if (!uptodate) {
287 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400288 }
Chris Mason6702ed42007-08-07 16:15:09 -0400289 }
Chris Masone9d0b132007-08-10 14:06:19 -0400290 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400291 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400292
Chris Mason6b800532007-10-15 16:17:34 -0400293 err = __btrfs_cow_block(trans, root, cur, parent, i,
294 &tmp, search_start,
295 min(16 * blocksize,
296 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400297 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400298 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400299 break;
Yan252c38f2007-08-29 09:11:44 -0400300 }
Chris Mason6b800532007-10-15 16:17:34 -0400301 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400302 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400303 *last_ret = search_start;
304 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400305 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400306 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400307 }
Chris Mason5708b952007-10-25 15:43:18 -0400308 if (parent->map_token) {
309 unmap_extent_buffer(parent, parent->map_token,
310 KM_USER1);
311 parent->map_token = NULL;
312 }
Chris Mason6702ed42007-08-07 16:15:09 -0400313 return err;
314}
315
Chris Mason74123bd2007-02-02 11:05:29 -0500316/*
317 * The leaf data grows from end-to-front in the node.
318 * this returns the address of the start of the last item,
319 * which is the stop of the leaf data stack
320 */
Chris Mason123abc82007-03-14 14:14:43 -0400321static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400322 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500323{
Chris Mason5f39d392007-10-15 16:14:19 -0400324 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500325 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400326 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400327 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500328}
329
Chris Mason123abc82007-03-14 14:14:43 -0400330static int check_node(struct btrfs_root *root, struct btrfs_path *path,
331 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500332{
Chris Mason5f39d392007-10-15 16:14:19 -0400333 struct extent_buffer *parent = NULL;
334 struct extent_buffer *node = path->nodes[level];
335 struct btrfs_disk_key parent_key;
336 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500337 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400338 int slot;
339 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400340 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500341
342 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400343 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400344
Chris Mason8d7be552007-05-10 11:24:42 -0400345 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400346 BUG_ON(nritems == 0);
347 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400348 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400349 btrfs_node_key(parent, &parent_key, parent_slot);
350 btrfs_node_key(node, &node_key, 0);
351 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400352 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400353 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400354 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500355 }
Chris Mason123abc82007-03-14 14:14:43 -0400356 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400357 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400358 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
359 btrfs_node_key(node, &node_key, slot);
360 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400361 }
362 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400363 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
364 btrfs_node_key(node, &node_key, slot);
365 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500366 }
367 return 0;
368}
369
Chris Mason123abc82007-03-14 14:14:43 -0400370static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
371 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500372{
Chris Mason5f39d392007-10-15 16:14:19 -0400373 struct extent_buffer *leaf = path->nodes[level];
374 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500375 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400376 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400377 struct btrfs_disk_key parent_key;
378 struct btrfs_disk_key leaf_key;
379 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400380
Chris Mason5f39d392007-10-15 16:14:19 -0400381 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500382
383 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400384 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400385
386 if (nritems == 0)
387 return 0;
388
389 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400390 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400391 btrfs_node_key(parent, &parent_key, parent_slot);
392 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400393
Chris Mason5f39d392007-10-15 16:14:19 -0400394 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400395 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400396 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400397 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500398 }
Chris Mason5f39d392007-10-15 16:14:19 -0400399#if 0
400 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
401 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
402 btrfs_item_key(leaf, &leaf_key, i);
403 if (comp_keys(&leaf_key, &cpukey) >= 0) {
404 btrfs_print_leaf(root, leaf);
405 printk("slot %d offset bad key\n", i);
406 BUG_ON(1);
407 }
408 if (btrfs_item_offset_nr(leaf, i) !=
409 btrfs_item_end_nr(leaf, i + 1)) {
410 btrfs_print_leaf(root, leaf);
411 printk("slot %d offset bad\n", i);
412 BUG_ON(1);
413 }
414 if (i == 0) {
415 if (btrfs_item_offset_nr(leaf, i) +
416 btrfs_item_size_nr(leaf, i) !=
417 BTRFS_LEAF_DATA_SIZE(root)) {
418 btrfs_print_leaf(root, leaf);
419 printk("slot %d first offset bad\n", i);
420 BUG_ON(1);
421 }
422 }
423 }
424 if (nritems > 0) {
425 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
426 btrfs_print_leaf(root, leaf);
427 printk("slot %d bad size \n", nritems - 1);
428 BUG_ON(1);
429 }
430 }
431#endif
432 if (slot != 0 && slot < nritems - 1) {
433 btrfs_item_key(leaf, &leaf_key, slot);
434 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
435 if (comp_keys(&leaf_key, &cpukey) <= 0) {
436 btrfs_print_leaf(root, leaf);
437 printk("slot %d offset bad key\n", slot);
438 BUG_ON(1);
439 }
440 if (btrfs_item_offset_nr(leaf, slot - 1) !=
441 btrfs_item_end_nr(leaf, slot)) {
442 btrfs_print_leaf(root, leaf);
443 printk("slot %d offset bad\n", slot);
444 BUG_ON(1);
445 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500446 }
Chris Mason8d7be552007-05-10 11:24:42 -0400447 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400448 btrfs_item_key(leaf, &leaf_key, slot);
449 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
450 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
451 if (btrfs_item_offset_nr(leaf, slot) !=
452 btrfs_item_end_nr(leaf, slot + 1)) {
453 btrfs_print_leaf(root, leaf);
454 printk("slot %d offset bad\n", slot);
455 BUG_ON(1);
456 }
Chris Mason8d7be552007-05-10 11:24:42 -0400457 }
Chris Mason5f39d392007-10-15 16:14:19 -0400458 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
459 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500460 return 0;
461}
462
Chris Mason123abc82007-03-14 14:14:43 -0400463static int check_block(struct btrfs_root *root, struct btrfs_path *path,
464 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500465{
Chris Mason810191f2007-10-15 16:18:55 -0400466 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400467#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400468 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400469
Chris Mason479965d2007-10-15 16:14:27 -0400470 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
471 (unsigned long)btrfs_header_fsid(buf),
472 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400473 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400474 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400475 }
Chris Masondb945352007-10-15 16:15:53 -0400476#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500477 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400478 return check_leaf(root, path, level);
479 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500480}
481
Chris Mason74123bd2007-02-02 11:05:29 -0500482/*
Chris Mason5f39d392007-10-15 16:14:19 -0400483 * search for key in the extent_buffer. The items start at offset p,
484 * and they are item_size apart. There are 'max' items in p.
485 *
Chris Mason74123bd2007-02-02 11:05:29 -0500486 * the slot in the array is returned via slot, and it points to
487 * the place where you would insert key if it is not found in
488 * the array.
489 *
490 * slot may point to max if the key is bigger than all of the keys
491 */
Chris Mason5f39d392007-10-15 16:14:19 -0400492static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
493 int item_size, struct btrfs_key *key,
494 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495{
496 int low = 0;
497 int high = max;
498 int mid;
499 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400500 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400501 struct btrfs_disk_key unaligned;
502 unsigned long offset;
503 char *map_token = NULL;
504 char *kaddr = NULL;
505 unsigned long map_start = 0;
506 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400507 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500508
509 while(low < high) {
510 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400511 offset = p + mid * item_size;
512
513 if (!map_token || offset < map_start ||
514 (offset + sizeof(struct btrfs_disk_key)) >
515 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400516 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400517 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400518 map_token = NULL;
519 }
520 err = map_extent_buffer(eb, offset,
521 sizeof(struct btrfs_disk_key),
522 &map_token, &kaddr,
523 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400524
Chris Mason479965d2007-10-15 16:14:27 -0400525 if (!err) {
526 tmp = (struct btrfs_disk_key *)(kaddr + offset -
527 map_start);
528 } else {
529 read_extent_buffer(eb, &unaligned,
530 offset, sizeof(unaligned));
531 tmp = &unaligned;
532 }
533
Chris Mason5f39d392007-10-15 16:14:19 -0400534 } else {
535 tmp = (struct btrfs_disk_key *)(kaddr + offset -
536 map_start);
537 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500538 ret = comp_keys(tmp, key);
539
540 if (ret < 0)
541 low = mid + 1;
542 else if (ret > 0)
543 high = mid;
544 else {
545 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400546 if (map_token)
547 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548 return 0;
549 }
550 }
551 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400552 if (map_token)
553 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500554 return 1;
555}
556
Chris Mason97571fd2007-02-24 13:39:08 -0500557/*
558 * simple bin_search frontend that does the right thing for
559 * leaves vs nodes
560 */
Chris Mason5f39d392007-10-15 16:14:19 -0400561static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
562 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563{
Chris Mason5f39d392007-10-15 16:14:19 -0400564 if (level == 0) {
565 return generic_bin_search(eb,
566 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400567 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400568 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400569 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500570 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400571 return generic_bin_search(eb,
572 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400573 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400574 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400575 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500576 }
577 return -1;
578}
579
Chris Mason5f39d392007-10-15 16:14:19 -0400580static struct extent_buffer *read_node_slot(struct btrfs_root *root,
581 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500582{
Chris Masonbb803952007-03-01 12:04:21 -0500583 if (slot < 0)
584 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400585 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500586 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400587 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
588 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500589}
590
Chris Masone089f052007-03-16 16:20:31 -0400591static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
592 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500593{
Chris Mason5f39d392007-10-15 16:14:19 -0400594 struct extent_buffer *right = NULL;
595 struct extent_buffer *mid;
596 struct extent_buffer *left = NULL;
597 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500598 int ret = 0;
599 int wret;
600 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500601 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400602 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500603 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500604
605 if (level == 0)
606 return 0;
607
Chris Mason5f39d392007-10-15 16:14:19 -0400608 mid = path->nodes[level];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400609 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500610
Chris Mason234b63a2007-03-13 10:46:10 -0400611 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400612 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500613 pslot = path->slots[level + 1];
614
Chris Mason40689472007-03-17 14:29:23 -0400615 /*
616 * deal with the case where there is only one pointer in the root
617 * by promoting the node below to a root
618 */
Chris Mason5f39d392007-10-15 16:14:19 -0400619 if (!parent) {
620 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500621
Chris Mason5f39d392007-10-15 16:14:19 -0400622 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500623 return 0;
624
625 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400626 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500627 BUG_ON(!child);
628 root->node = child;
629 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400630 clean_tree_block(trans, root, mid);
631 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500632 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400633 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400634 ret = btrfs_free_extent(trans, root, mid->start, mid->len, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500635 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400636 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400637 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500638 }
Chris Mason5f39d392007-10-15 16:14:19 -0400639 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400640 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500641 return 0;
642
Chris Mason5f39d392007-10-15 16:14:19 -0400643 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400644 err_on_enospc = 1;
645
Chris Mason5f39d392007-10-15 16:14:19 -0400646 left = read_node_slot(root, parent, pslot - 1);
647 if (left) {
648 wret = btrfs_cow_block(trans, root, left,
649 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400650 if (wret) {
651 ret = wret;
652 goto enospc;
653 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400654 }
Chris Mason5f39d392007-10-15 16:14:19 -0400655 right = read_node_slot(root, parent, pslot + 1);
656 if (right) {
657 wret = btrfs_cow_block(trans, root, right,
658 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400659 if (wret) {
660 ret = wret;
661 goto enospc;
662 }
663 }
664
665 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400666 if (left) {
667 orig_slot += btrfs_header_nritems(left);
668 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500669 if (wret < 0)
670 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400671 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400672 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500673 }
Chris Mason79f95c82007-03-01 15:16:26 -0500674
675 /*
676 * then try to empty the right most buffer into the middle
677 */
Chris Mason5f39d392007-10-15 16:14:19 -0400678 if (right) {
679 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400680 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500681 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400682 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400683 u64 bytenr = right->start;
684 u32 blocksize = right->len;
685
Chris Mason5f39d392007-10-15 16:14:19 -0400686 clean_tree_block(trans, root, right);
687 wait_on_tree_block_writeback(root, right);
688 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500689 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400690 wret = del_ptr(trans, root, path, level + 1, pslot +
691 1);
Chris Masonbb803952007-03-01 12:04:21 -0500692 if (wret)
693 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400694 wret = btrfs_free_extent(trans, root, bytenr,
695 blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500696 if (wret)
697 ret = wret;
698 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400699 struct btrfs_disk_key right_key;
700 btrfs_node_key(right, &right_key, 0);
701 btrfs_set_node_key(parent, &right_key, pslot + 1);
702 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500703 }
704 }
Chris Mason5f39d392007-10-15 16:14:19 -0400705 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500706 /*
707 * we're not allowed to leave a node with one item in the
708 * tree during a delete. A deletion from lower in the tree
709 * could try to delete the only pointer in this node.
710 * So, pull some keys from the left.
711 * There has to be a left pointer at this point because
712 * otherwise we would have pulled some pointers from the
713 * right
714 */
Chris Mason5f39d392007-10-15 16:14:19 -0400715 BUG_ON(!left);
716 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400717 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500718 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400719 goto enospc;
720 }
Chris Mason79f95c82007-03-01 15:16:26 -0500721 BUG_ON(wret == 1);
722 }
Chris Mason5f39d392007-10-15 16:14:19 -0400723 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500724 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -0400725 u64 bytenr = mid->start;
726 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400727 clean_tree_block(trans, root, mid);
728 wait_on_tree_block_writeback(root, mid);
729 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500730 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400731 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500732 if (wret)
733 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400734 wret = btrfs_free_extent(trans, root, bytenr, blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500735 if (wret)
736 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500737 } else {
738 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400739 struct btrfs_disk_key mid_key;
740 btrfs_node_key(mid, &mid_key, 0);
741 btrfs_set_node_key(parent, &mid_key, pslot);
742 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500743 }
Chris Masonbb803952007-03-01 12:04:21 -0500744
Chris Mason79f95c82007-03-01 15:16:26 -0500745 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400746 if (left) {
747 if (btrfs_header_nritems(left) > orig_slot) {
748 extent_buffer_get(left);
749 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500750 path->slots[level + 1] -= 1;
751 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400752 if (mid)
753 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500754 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400755 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500756 path->slots[level] = orig_slot;
757 }
758 }
Chris Mason79f95c82007-03-01 15:16:26 -0500759 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400760 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400761 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400762 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500763 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400764enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400765 if (right)
766 free_extent_buffer(right);
767 if (left)
768 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500769 return ret;
770}
771
Chris Masone66f7092007-04-20 13:16:02 -0400772/* returns zero if the push worked, non-zero otherwise */
773static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
774 struct btrfs_root *root,
775 struct btrfs_path *path, int level)
776{
Chris Mason5f39d392007-10-15 16:14:19 -0400777 struct extent_buffer *right = NULL;
778 struct extent_buffer *mid;
779 struct extent_buffer *left = NULL;
780 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400781 int ret = 0;
782 int wret;
783 int pslot;
784 int orig_slot = path->slots[level];
785 u64 orig_ptr;
786
787 if (level == 0)
788 return 1;
789
Chris Mason5f39d392007-10-15 16:14:19 -0400790 mid = path->nodes[level];
Chris Masone66f7092007-04-20 13:16:02 -0400791 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
792
793 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400794 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400795 pslot = path->slots[level + 1];
796
Chris Mason5f39d392007-10-15 16:14:19 -0400797 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400798 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400799
Chris Mason5f39d392007-10-15 16:14:19 -0400800 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400801
802 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400803 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400804 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400805 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400806 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
807 wret = 1;
808 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400809 ret = btrfs_cow_block(trans, root, left, parent,
810 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400811 if (ret)
812 wret = 1;
813 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400814 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400815 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400816 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400817 }
Chris Masone66f7092007-04-20 13:16:02 -0400818 if (wret < 0)
819 ret = wret;
820 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400821 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400822 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 btrfs_node_key(mid, &disk_key, 0);
824 btrfs_set_node_key(parent, &disk_key, pslot);
825 btrfs_mark_buffer_dirty(parent);
826 if (btrfs_header_nritems(left) > orig_slot) {
827 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400828 path->slots[level + 1] -= 1;
829 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400830 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400831 } else {
832 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400833 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400834 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400835 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400836 }
Chris Masone66f7092007-04-20 13:16:02 -0400837 return 0;
838 }
Chris Mason5f39d392007-10-15 16:14:19 -0400839 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400840 }
Chris Mason5f39d392007-10-15 16:14:19 -0400841 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400842
843 /*
844 * then try to empty the right most buffer into the middle
845 */
Chris Mason5f39d392007-10-15 16:14:19 -0400846 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400847 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400848 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400849 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
850 wret = 1;
851 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400852 ret = btrfs_cow_block(trans, root, right,
853 parent, pslot + 1,
854 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400855 if (ret)
856 wret = 1;
857 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400858 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400859 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400860 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400861 }
Chris Masone66f7092007-04-20 13:16:02 -0400862 if (wret < 0)
863 ret = wret;
864 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400865 struct btrfs_disk_key disk_key;
866
867 btrfs_node_key(right, &disk_key, 0);
868 btrfs_set_node_key(parent, &disk_key, pslot + 1);
869 btrfs_mark_buffer_dirty(parent);
870
871 if (btrfs_header_nritems(mid) <= orig_slot) {
872 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400873 path->slots[level + 1] += 1;
874 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400875 btrfs_header_nritems(mid);
876 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400877 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400878 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400879 }
Chris Masone66f7092007-04-20 13:16:02 -0400880 return 0;
881 }
Chris Mason5f39d392007-10-15 16:14:19 -0400882 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400883 }
Chris Masone66f7092007-04-20 13:16:02 -0400884 return 1;
885}
886
Chris Mason74123bd2007-02-02 11:05:29 -0500887/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400888 * readahead one full node of leaves
889 */
890static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400891 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400892{
Chris Mason5f39d392007-10-15 16:14:19 -0400893 struct extent_buffer *node;
Chris Mason3c69fae2007-08-07 15:52:22 -0400894 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400895 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -0400896 u64 lowest_read;
897 u64 highest_read;
898 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -0400899 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -0400900 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -0400901 u32 nr;
902 u32 blocksize;
903 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -0400904
Chris Masona6b6e752007-10-15 16:22:39 -0400905 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -0400906 return;
907
Chris Mason6702ed42007-08-07 16:15:09 -0400908 if (!path->nodes[level])
909 return;
910
Chris Mason5f39d392007-10-15 16:14:19 -0400911 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -0400912 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -0400913 blocksize = btrfs_level_size(root, level - 1);
914 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400915 if (eb) {
916 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -0400917 return;
918 }
919
Chris Mason6b800532007-10-15 16:17:34 -0400920 highest_read = search;
921 lowest_read = search;
922
Chris Mason5f39d392007-10-15 16:14:19 -0400923 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -0400924 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -0400925 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -0400926 if (direction < 0) {
927 if (nr == 0)
928 break;
929 nr--;
930 } else if (direction > 0) {
931 nr++;
932 if (nr >= nritems)
933 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400934 }
Chris Mason6b800532007-10-15 16:17:34 -0400935 search = btrfs_node_blockptr(node, nr);
936 if ((search >= lowest_read && search <= highest_read) ||
937 (search < lowest_read && lowest_read - search <= 32768) ||
938 (search > highest_read && search - highest_read <= 32768)) {
939 readahead_tree_block(root, search, blocksize);
940 nread += blocksize;
941 }
942 nscan++;
943 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
944 break;
945 if(nread > (1024 * 1024) || nscan > 128)
946 break;
947
948 if (search < lowest_read)
949 lowest_read = search;
950 if (search > highest_read)
951 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -0400952 }
953}
954/*
Chris Mason74123bd2007-02-02 11:05:29 -0500955 * look for key in the tree. path is filled in with nodes along the way
956 * if key is found, we return zero and you can find the item in the leaf
957 * level of the path (level 0)
958 *
959 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500960 * be inserted, and 1 is returned. If there are other errors during the
961 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500962 *
963 * if ins_len > 0, nodes and leaves will be split as we walk down the
964 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
965 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500966 */
Chris Masone089f052007-03-16 16:20:31 -0400967int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
968 *root, struct btrfs_key *key, struct btrfs_path *p, int
969 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500970{
Chris Mason5f39d392007-10-15 16:14:19 -0400971 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -0400972 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -0500973 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500974 int slot;
975 int ret;
976 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400977 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400978 u8 lowest_level = 0;
979
Chris Mason6702ed42007-08-07 16:15:09 -0400980 lowest_level = p->lowest_level;
981 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400982 WARN_ON(p->nodes[0] != NULL);
983 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500984again:
985 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -0400986 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500987 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -0400988 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500989 if (cow) {
990 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400991 wret = btrfs_cow_block(trans, root, b,
992 p->nodes[level + 1],
993 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -0400994 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400995 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400996 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400997 return wret;
998 }
Chris Mason02217ed2007-03-02 16:08:05 -0500999 }
1000 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001001 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001002 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001003 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001004 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001005 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001006 if (ret)
1007 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001008 ret = bin_search(b, key, level, &slot);
1009 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001010 if (ret && slot > 0)
1011 slot -= 1;
1012 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001014 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001015 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001016 BUG_ON(sret > 0);
1017 if (sret)
1018 return sret;
1019 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001020 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001021 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001022 int sret = balance_level(trans, root, p,
1023 level);
Chris Masonbb803952007-03-01 12:04:21 -05001024 if (sret)
1025 return sret;
1026 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001027 if (!b) {
1028 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001029 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001030 }
Chris Masonbb803952007-03-01 12:04:21 -05001031 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001032 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001033 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001034 /* this is only true while dropping a snapshot */
1035 if (level == lowest_level)
1036 break;
Chris Masondb945352007-10-15 16:15:53 -04001037 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001038 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001039 if (should_reada)
1040 reada_for_search(root, p, level, slot);
Chris Masondb945352007-10-15 16:15:53 -04001041 b = read_tree_block(root, bytenr,
1042 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001043 if (ptr_gen != btrfs_header_generation(b)) {
1044 printk("block %llu bad gen wanted %llu "
1045 "found %llu\n",
1046 (unsigned long long)b->start,
1047 (unsigned long long)ptr_gen,
1048 (unsigned long long)btrfs_header_generation(b));
1049 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001050 } else {
1051 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001052 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001053 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001054 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001055 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001056 BUG_ON(sret > 0);
1057 if (sret)
1058 return sret;
1059 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001060 return ret;
1061 }
1062 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001063 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001064}
1065
Chris Mason74123bd2007-02-02 11:05:29 -05001066/*
1067 * adjust the pointers going up the tree, starting at level
1068 * making sure the right key of each node is points to 'key'.
1069 * This is used after shifting pointers to the left, so it stops
1070 * fixing up pointers when a given leaf/node is not in slot 0 of the
1071 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001072 *
1073 * If this fails to write a tree block, it returns -1, but continues
1074 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001075 */
Chris Mason5f39d392007-10-15 16:14:19 -04001076static int fixup_low_keys(struct btrfs_trans_handle *trans,
1077 struct btrfs_root *root, struct btrfs_path *path,
1078 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001079{
1080 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001081 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001082 struct extent_buffer *t;
1083
Chris Mason234b63a2007-03-13 10:46:10 -04001084 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001085 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001086 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001087 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001088 t = path->nodes[i];
1089 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001090 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001091 if (tslot != 0)
1092 break;
1093 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001094 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001095}
1096
Chris Mason74123bd2007-02-02 11:05:29 -05001097/*
1098 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001099 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001100 *
1101 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1102 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001103 */
Chris Masone089f052007-03-16 16:20:31 -04001104static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason5f39d392007-10-15 16:14:19 -04001105 *root, struct extent_buffer *dst,
1106 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001107{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001108 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001109 int src_nritems;
1110 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001111 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001112
Chris Mason5f39d392007-10-15 16:14:19 -04001113 src_nritems = btrfs_header_nritems(src);
1114 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001115 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001116
Chris Masoneb60cea2007-02-02 09:18:22 -05001117 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001118 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001119 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001120
1121 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001122 push_items = src_nritems;
1123
Chris Mason5f39d392007-10-15 16:14:19 -04001124 copy_extent_buffer(dst, src,
1125 btrfs_node_key_ptr_offset(dst_nritems),
1126 btrfs_node_key_ptr_offset(0),
1127 push_items * sizeof(struct btrfs_key_ptr));
1128
Chris Masonbb803952007-03-01 12:04:21 -05001129 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001130 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1131 btrfs_node_key_ptr_offset(push_items),
1132 (src_nritems - push_items) *
1133 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001134 }
Chris Mason5f39d392007-10-15 16:14:19 -04001135 btrfs_set_header_nritems(src, src_nritems - push_items);
1136 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1137 btrfs_mark_buffer_dirty(src);
1138 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001139 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001140}
1141
Chris Mason97571fd2007-02-24 13:39:08 -05001142/*
Chris Mason79f95c82007-03-01 15:16:26 -05001143 * try to push data from one node into the next node right in the
1144 * tree.
1145 *
1146 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1147 * error, and > 0 if there was no room in the right hand block.
1148 *
1149 * this will only push up to 1/2 the contents of the left node over
1150 */
Chris Mason5f39d392007-10-15 16:14:19 -04001151static int balance_node_right(struct btrfs_trans_handle *trans,
1152 struct btrfs_root *root,
1153 struct extent_buffer *dst,
1154 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001155{
Chris Mason79f95c82007-03-01 15:16:26 -05001156 int push_items = 0;
1157 int max_push;
1158 int src_nritems;
1159 int dst_nritems;
1160 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001161
Chris Mason5f39d392007-10-15 16:14:19 -04001162 src_nritems = btrfs_header_nritems(src);
1163 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001164 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001165 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001166 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001167
1168 max_push = src_nritems / 2 + 1;
1169 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001170 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001171 return 1;
Yan252c38f2007-08-29 09:11:44 -04001172
Chris Mason79f95c82007-03-01 15:16:26 -05001173 if (max_push < push_items)
1174 push_items = max_push;
1175
Chris Mason5f39d392007-10-15 16:14:19 -04001176 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1177 btrfs_node_key_ptr_offset(0),
1178 (dst_nritems) *
1179 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001180
Chris Mason5f39d392007-10-15 16:14:19 -04001181 copy_extent_buffer(dst, src,
1182 btrfs_node_key_ptr_offset(0),
1183 btrfs_node_key_ptr_offset(src_nritems - push_items),
1184 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001185
Chris Mason5f39d392007-10-15 16:14:19 -04001186 btrfs_set_header_nritems(src, src_nritems - push_items);
1187 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001188
Chris Mason5f39d392007-10-15 16:14:19 -04001189 btrfs_mark_buffer_dirty(src);
1190 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001191 return ret;
1192}
1193
1194/*
Chris Mason97571fd2007-02-24 13:39:08 -05001195 * helper function to insert a new root level in the tree.
1196 * A new node is allocated, and a single item is inserted to
1197 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001198 *
1199 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001200 */
Chris Mason5f39d392007-10-15 16:14:19 -04001201static int insert_new_root(struct btrfs_trans_handle *trans,
1202 struct btrfs_root *root,
1203 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001204{
Chris Mason5f39d392007-10-15 16:14:19 -04001205 struct extent_buffer *lower;
1206 struct extent_buffer *c;
1207 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001208
1209 BUG_ON(path->nodes[level]);
1210 BUG_ON(path->nodes[level-1] != root->node);
1211
Chris Masondb945352007-10-15 16:15:53 -04001212 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1213 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001214 if (IS_ERR(c))
1215 return PTR_ERR(c);
1216 memset_extent_buffer(c, 0, 0, root->nodesize);
1217 btrfs_set_header_nritems(c, 1);
1218 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001219 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001220 btrfs_set_header_generation(c, trans->transid);
1221 btrfs_set_header_owner(c, root->root_key.objectid);
1222 lower = path->nodes[level-1];
Chris Masond5719762007-03-23 10:01:08 -04001223
Chris Mason5f39d392007-10-15 16:14:19 -04001224 write_extent_buffer(c, root->fs_info->fsid,
1225 (unsigned long)btrfs_header_fsid(c),
1226 BTRFS_FSID_SIZE);
1227 if (level == 1)
1228 btrfs_item_key(lower, &lower_key, 0);
1229 else
1230 btrfs_node_key(lower, &lower_key, 0);
1231 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001232 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason74493f72007-12-11 09:25:06 -05001233 WARN_ON(btrfs_header_generation(lower) == 0);
1234 btrfs_set_node_ptr_generation(c, 0, btrfs_header_generation(lower));
Chris Mason5f39d392007-10-15 16:14:19 -04001235
1236 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001237
Chris Mason5c680ed2007-02-22 11:39:13 -05001238 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001239 free_extent_buffer(root->node);
1240 root->node = c;
1241 extent_buffer_get(c);
1242 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001243 path->slots[level] = 0;
1244 return 0;
1245}
1246
Chris Mason74123bd2007-02-02 11:05:29 -05001247/*
1248 * worker function to insert a single pointer in a node.
1249 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001250 *
Chris Mason74123bd2007-02-02 11:05:29 -05001251 * slot and level indicate where you want the key to go, and
1252 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001253 *
1254 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001255 */
Chris Masone089f052007-03-16 16:20:31 -04001256static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1257 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001258 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001259{
Chris Mason5f39d392007-10-15 16:14:19 -04001260 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001261 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001262
1263 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 lower = path->nodes[level];
1265 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001266 if (slot > nritems)
1267 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001268 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001269 BUG();
1270 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001271 memmove_extent_buffer(lower,
1272 btrfs_node_key_ptr_offset(slot + 1),
1273 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001274 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001275 }
Chris Mason5f39d392007-10-15 16:14:19 -04001276 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001277 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001278 WARN_ON(trans->transid == 0);
1279 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001280 btrfs_set_header_nritems(lower, nritems + 1);
1281 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001282 return 0;
1283}
1284
Chris Mason97571fd2007-02-24 13:39:08 -05001285/*
1286 * split the node at the specified level in path in two.
1287 * The path is corrected to point to the appropriate node after the split
1288 *
1289 * Before splitting this tries to make some room in the node by pushing
1290 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001291 *
1292 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001293 */
Chris Masone089f052007-03-16 16:20:31 -04001294static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1295 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001296{
Chris Mason5f39d392007-10-15 16:14:19 -04001297 struct extent_buffer *c;
1298 struct extent_buffer *split;
1299 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001300 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001301 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001302 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001303 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001304
Chris Mason5f39d392007-10-15 16:14:19 -04001305 c = path->nodes[level];
1306 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001307 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001308 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001309 if (ret)
1310 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001311 } else {
1312 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001313 c = path->nodes[level];
1314 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001315 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1316 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001317 if (ret < 0)
1318 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001319 }
Chris Masone66f7092007-04-20 13:16:02 -04001320
Chris Mason5f39d392007-10-15 16:14:19 -04001321 c_nritems = btrfs_header_nritems(c);
Chris Masondb945352007-10-15 16:15:53 -04001322 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1323 c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001324 if (IS_ERR(split))
1325 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001326
Chris Mason5f39d392007-10-15 16:14:19 -04001327 btrfs_set_header_flags(split, btrfs_header_flags(c));
1328 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001329 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001330 btrfs_set_header_generation(split, trans->transid);
1331 btrfs_set_header_owner(split, root->root_key.objectid);
1332 write_extent_buffer(split, root->fs_info->fsid,
1333 (unsigned long)btrfs_header_fsid(split),
1334 BTRFS_FSID_SIZE);
1335
Chris Mason7518a232007-03-12 12:01:18 -04001336 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001337
1338 copy_extent_buffer(split, c,
1339 btrfs_node_key_ptr_offset(0),
1340 btrfs_node_key_ptr_offset(mid),
1341 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1342 btrfs_set_header_nritems(split, c_nritems - mid);
1343 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001344 ret = 0;
1345
Chris Mason5f39d392007-10-15 16:14:19 -04001346 btrfs_mark_buffer_dirty(c);
1347 btrfs_mark_buffer_dirty(split);
1348
1349 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001350 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001351 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001352 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001353 if (wret)
1354 ret = wret;
1355
Chris Mason5de08d72007-02-24 06:24:44 -05001356 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001357 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001358 free_extent_buffer(c);
1359 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001360 path->slots[level + 1] += 1;
1361 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001362 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001363 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001364 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001365}
1366
Chris Mason74123bd2007-02-02 11:05:29 -05001367/*
1368 * how many bytes are required to store the items in a leaf. start
1369 * and nr indicate which items in the leaf to check. This totals up the
1370 * space used both by the item structs and the item data
1371 */
Chris Mason5f39d392007-10-15 16:14:19 -04001372static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001373{
1374 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001375 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001376 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001377
1378 if (!nr)
1379 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001380 data_len = btrfs_item_end_nr(l, start);
1381 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001382 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001383 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001384 return data_len;
1385}
1386
Chris Mason74123bd2007-02-02 11:05:29 -05001387/*
Chris Masond4dbff92007-04-04 14:08:15 -04001388 * The space between the end of the leaf items and
1389 * the start of the leaf data. IOW, how much room
1390 * the leaf has left for both items and data
1391 */
Chris Mason5f39d392007-10-15 16:14:19 -04001392int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001393{
Chris Mason5f39d392007-10-15 16:14:19 -04001394 int nritems = btrfs_header_nritems(leaf);
1395 int ret;
1396 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1397 if (ret < 0) {
1398 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001399 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001400 leaf_space_used(leaf, 0, nritems), nritems);
1401 }
1402 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001403}
1404
1405/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001406 * push some data in the path leaf to the right, trying to free up at
1407 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001408 *
1409 * returns 1 if the push failed because the other node didn't have enough
1410 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001411 */
Chris Masone089f052007-03-16 16:20:31 -04001412static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001413 *root, struct btrfs_path *path, int data_size,
1414 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001415{
Chris Mason5f39d392007-10-15 16:14:19 -04001416 struct extent_buffer *left = path->nodes[0];
1417 struct extent_buffer *right;
1418 struct extent_buffer *upper;
1419 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001420 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001421 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001422 int free_space;
1423 int push_space = 0;
1424 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001425 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001426 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001427 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001428 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001429 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001430 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001431 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001432
1433 slot = path->slots[1];
1434 if (!path->nodes[1]) {
1435 return 1;
1436 }
1437 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001438 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001439 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001440
Chris Masondb945352007-10-15 16:15:53 -04001441 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1442 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001443 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001444 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001445 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001446 return 1;
1447 }
1448
Chris Mason5f39d392007-10-15 16:14:19 -04001449 /* cow and double check */
1450 ret = btrfs_cow_block(trans, root, right, upper,
1451 slot + 1, &right);
1452 if (ret) {
1453 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001454 return 1;
1455 }
Chris Mason5f39d392007-10-15 16:14:19 -04001456 free_space = btrfs_leaf_free_space(root, right);
1457 if (free_space < data_size + sizeof(struct btrfs_item)) {
1458 free_extent_buffer(right);
1459 return 1;
1460 }
1461
1462 left_nritems = btrfs_header_nritems(left);
1463 if (left_nritems == 0) {
1464 free_extent_buffer(right);
1465 return 1;
1466 }
1467
Chris Mason34a38212007-11-07 13:31:03 -05001468 if (empty)
1469 nr = 0;
1470 else
1471 nr = 1;
1472
1473 i = left_nritems - 1;
1474 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001475 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001476
Chris Mason00ec4c52007-02-24 12:47:20 -05001477 if (path->slots[0] == i)
1478 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001479
1480 if (!left->map_token) {
1481 map_extent_buffer(left, (unsigned long)item,
1482 sizeof(struct btrfs_item),
1483 &left->map_token, &left->kaddr,
1484 &left->map_start, &left->map_len,
1485 KM_USER1);
1486 }
1487
1488 this_item_size = btrfs_item_size(left, item);
1489 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001490 break;
1491 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001492 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001493 if (i == 0)
1494 break;
1495 i--;
Chris Masondb945352007-10-15 16:15:53 -04001496 }
1497 if (left->map_token) {
1498 unmap_extent_buffer(left, left->map_token, KM_USER1);
1499 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001500 }
Chris Mason5f39d392007-10-15 16:14:19 -04001501
Chris Mason00ec4c52007-02-24 12:47:20 -05001502 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001503 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001504 return 1;
1505 }
Chris Mason5f39d392007-10-15 16:14:19 -04001506
Chris Mason34a38212007-11-07 13:31:03 -05001507 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001508 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001509
Chris Mason00ec4c52007-02-24 12:47:20 -05001510 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001511 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001512
Chris Mason5f39d392007-10-15 16:14:19 -04001513 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001514 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001515
Chris Mason00ec4c52007-02-24 12:47:20 -05001516 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001517 data_end = leaf_data_end(root, right);
1518 memmove_extent_buffer(right,
1519 btrfs_leaf_data(right) + data_end - push_space,
1520 btrfs_leaf_data(right) + data_end,
1521 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1522
Chris Mason00ec4c52007-02-24 12:47:20 -05001523 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001524 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001525 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1526 btrfs_leaf_data(left) + leaf_data_end(root, left),
1527 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001528
1529 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1530 btrfs_item_nr_offset(0),
1531 right_nritems * sizeof(struct btrfs_item));
1532
Chris Mason00ec4c52007-02-24 12:47:20 -05001533 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001534 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1535 btrfs_item_nr_offset(left_nritems - push_items),
1536 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001537
1538 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001539 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001540 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001541 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001542 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001543 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001544 if (!right->map_token) {
1545 map_extent_buffer(right, (unsigned long)item,
1546 sizeof(struct btrfs_item),
1547 &right->map_token, &right->kaddr,
1548 &right->map_start, &right->map_len,
1549 KM_USER1);
1550 }
1551 push_space -= btrfs_item_size(right, item);
1552 btrfs_set_item_offset(right, item, push_space);
1553 }
1554
1555 if (right->map_token) {
1556 unmap_extent_buffer(right, right->map_token, KM_USER1);
1557 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001558 }
Chris Mason7518a232007-03-12 12:01:18 -04001559 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001560 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001561
Chris Mason34a38212007-11-07 13:31:03 -05001562 if (left_nritems)
1563 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001564 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001565
Chris Mason5f39d392007-10-15 16:14:19 -04001566 btrfs_item_key(right, &disk_key, 0);
1567 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001568 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001569
Chris Mason00ec4c52007-02-24 12:47:20 -05001570 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001571 if (path->slots[0] >= left_nritems) {
1572 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001573 free_extent_buffer(path->nodes[0]);
1574 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001575 path->slots[1] += 1;
1576 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001577 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001578 }
1579 return 0;
1580}
1581/*
Chris Mason74123bd2007-02-02 11:05:29 -05001582 * push some data in the path leaf to the left, trying to free up at
1583 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1584 */
Chris Masone089f052007-03-16 16:20:31 -04001585static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001586 *root, struct btrfs_path *path, int data_size,
1587 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001588{
Chris Mason5f39d392007-10-15 16:14:19 -04001589 struct btrfs_disk_key disk_key;
1590 struct extent_buffer *right = path->nodes[0];
1591 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001592 int slot;
1593 int i;
1594 int free_space;
1595 int push_space = 0;
1596 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001597 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001598 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001599 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001600 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001601 int ret = 0;
1602 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001603 u32 this_item_size;
1604 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001605
1606 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001607 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001608 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001609 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001611
Chris Mason3685f792007-10-19 09:23:27 -04001612 right_nritems = btrfs_header_nritems(right);
1613 if (right_nritems == 0) {
1614 return 1;
1615 }
1616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001618 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001619 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001620 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001621 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001622 return 1;
1623 }
Chris Mason02217ed2007-03-02 16:08:05 -05001624
1625 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001626 ret = btrfs_cow_block(trans, root, left,
1627 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001628 if (ret) {
1629 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001630 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001631 return 1;
1632 }
Chris Mason3685f792007-10-19 09:23:27 -04001633
Chris Mason123abc82007-03-14 14:14:43 -04001634 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001635 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001637 return 1;
1638 }
1639
Chris Mason34a38212007-11-07 13:31:03 -05001640 if (empty)
1641 nr = right_nritems;
1642 else
1643 nr = right_nritems - 1;
1644
1645 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001646 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001647 if (!right->map_token) {
1648 map_extent_buffer(right, (unsigned long)item,
1649 sizeof(struct btrfs_item),
1650 &right->map_token, &right->kaddr,
1651 &right->map_start, &right->map_len,
1652 KM_USER1);
1653 }
1654
Chris Masonbe0e5c02007-01-26 15:51:26 -05001655 if (path->slots[0] == i)
1656 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001657
1658 this_item_size = btrfs_item_size(right, item);
1659 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001660 break;
Chris Masondb945352007-10-15 16:15:53 -04001661
Chris Masonbe0e5c02007-01-26 15:51:26 -05001662 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001663 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664 }
Chris Masondb945352007-10-15 16:15:53 -04001665
1666 if (right->map_token) {
1667 unmap_extent_buffer(right, right->map_token, KM_USER1);
1668 right->map_token = NULL;
1669 }
1670
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001672 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001673 return 1;
1674 }
Chris Mason34a38212007-11-07 13:31:03 -05001675 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001676 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001677
Chris Masonbe0e5c02007-01-26 15:51:26 -05001678 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001679 copy_extent_buffer(left, right,
1680 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1681 btrfs_item_nr_offset(0),
1682 push_items * sizeof(struct btrfs_item));
1683
Chris Mason123abc82007-03-14 14:14:43 -04001684 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001685 btrfs_item_offset_nr(right, push_items -1);
1686
1687 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001688 leaf_data_end(root, left) - push_space,
1689 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001690 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001691 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001692 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001693 BUG_ON(old_left_nritems < 0);
1694
Chris Masondb945352007-10-15 16:15:53 -04001695 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001696 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001697 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001698
Chris Mason5f39d392007-10-15 16:14:19 -04001699 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001700 if (!left->map_token) {
1701 map_extent_buffer(left, (unsigned long)item,
1702 sizeof(struct btrfs_item),
1703 &left->map_token, &left->kaddr,
1704 &left->map_start, &left->map_len,
1705 KM_USER1);
1706 }
1707
Chris Mason5f39d392007-10-15 16:14:19 -04001708 ioff = btrfs_item_offset(left, item);
1709 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001710 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001711 }
Chris Mason5f39d392007-10-15 16:14:19 -04001712 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001713 if (left->map_token) {
1714 unmap_extent_buffer(left, left->map_token, KM_USER1);
1715 left->map_token = NULL;
1716 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001717
1718 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001719 if (push_items > right_nritems) {
1720 printk("push items %d nr %u\n", push_items, right_nritems);
1721 WARN_ON(1);
1722 }
Chris Mason5f39d392007-10-15 16:14:19 -04001723
Chris Mason34a38212007-11-07 13:31:03 -05001724 if (push_items < right_nritems) {
1725 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1726 leaf_data_end(root, right);
1727 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1728 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1729 btrfs_leaf_data(right) +
1730 leaf_data_end(root, right), push_space);
1731
1732 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001733 btrfs_item_nr_offset(push_items),
1734 (btrfs_header_nritems(right) - push_items) *
1735 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001736 }
Yaneef1c492007-11-26 10:58:13 -05001737 right_nritems -= push_items;
1738 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001739 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001740 for (i = 0; i < right_nritems; i++) {
1741 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001742
1743 if (!right->map_token) {
1744 map_extent_buffer(right, (unsigned long)item,
1745 sizeof(struct btrfs_item),
1746 &right->map_token, &right->kaddr,
1747 &right->map_start, &right->map_len,
1748 KM_USER1);
1749 }
1750
1751 push_space = push_space - btrfs_item_size(right, item);
1752 btrfs_set_item_offset(right, item, push_space);
1753 }
1754 if (right->map_token) {
1755 unmap_extent_buffer(right, right->map_token, KM_USER1);
1756 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001758
Chris Mason5f39d392007-10-15 16:14:19 -04001759 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001760 if (right_nritems)
1761 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 btrfs_item_key(right, &disk_key, 0);
1764 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001765 if (wret)
1766 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001767
1768 /* then fixup the leaf pointer in the path */
1769 if (path->slots[0] < push_items) {
1770 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001771 free_extent_buffer(path->nodes[0]);
1772 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773 path->slots[1] -= 1;
1774 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001775 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776 path->slots[0] -= push_items;
1777 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001778 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001779 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001780}
1781
Chris Mason74123bd2007-02-02 11:05:29 -05001782/*
1783 * split the path's leaf in two, making sure there is at least data_size
1784 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001785 *
1786 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001787 */
Chris Masone089f052007-03-16 16:20:31 -04001788static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001789 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001790 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791{
Chris Mason5f39d392007-10-15 16:14:19 -04001792 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001793 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001794 int mid;
1795 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001796 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001797 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798 int data_copy_size;
1799 int rt_data_off;
1800 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001801 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001802 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001803 int double_split;
1804 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001805 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806
Chris Masoncc0c5532007-10-25 15:42:57 -04001807 if (extend)
1808 space_needed = data_size;
1809
Chris Mason40689472007-03-17 14:29:23 -04001810 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001811 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001812 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001813 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001814 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001815 }
1816 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05001817 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001818 if (wret < 0)
1819 return wret;
1820 }
1821 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001822
Chris Mason3685f792007-10-19 09:23:27 -04001823 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04001824 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04001825 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001826 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001827
Chris Mason5c680ed2007-02-22 11:39:13 -05001828 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001829 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001830 if (ret)
1831 return ret;
1832 }
Chris Masoncc0c5532007-10-25 15:42:57 -04001833again:
1834 double_split = 0;
1835 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001836 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001837 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001838 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001839
Chris Masondb945352007-10-15 16:15:53 -04001840 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1841 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001842 if (IS_ERR(right))
1843 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001844
Chris Mason5f39d392007-10-15 16:14:19 -04001845 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04001846 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001847 btrfs_set_header_generation(right, trans->transid);
1848 btrfs_set_header_owner(right, root->root_key.objectid);
1849 btrfs_set_header_level(right, 0);
1850 write_extent_buffer(right, root->fs_info->fsid,
1851 (unsigned long)btrfs_header_fsid(right),
1852 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04001853 if (mid <= slot) {
1854 if (nritems == 1 ||
1855 leaf_space_used(l, mid, nritems - mid) + space_needed >
1856 BTRFS_LEAF_DATA_SIZE(root)) {
1857 if (slot >= nritems) {
1858 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001859 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001860 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04001861 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04001862 path->slots[1] + 1, 1);
1863 if (wret)
1864 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001865 free_extent_buffer(path->nodes[0]);
1866 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001867 path->slots[0] = 0;
1868 path->slots[1] += 1;
1869 return ret;
1870 }
1871 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04001872 if (mid != nritems &&
1873 leaf_space_used(l, mid, nritems - mid) +
1874 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1875 double_split = 1;
1876 }
Chris Masond4dbff92007-04-04 14:08:15 -04001877 }
1878 } else {
1879 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1880 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04001881 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04001882 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001883 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001884 wret = insert_ptr(trans, root, path,
1885 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04001886 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04001887 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001888 if (wret)
1889 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001890 free_extent_buffer(path->nodes[0]);
1891 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001892 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001893 if (path->slots[1] == 0) {
1894 wret = fixup_low_keys(trans, root,
1895 path, &disk_key, 1);
1896 if (wret)
1897 ret = wret;
1898 }
Chris Masond4dbff92007-04-04 14:08:15 -04001899 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001900 } else if (extend && slot == 0) {
1901 mid = 1;
1902 } else {
1903 mid = slot;
1904 if (mid != nritems &&
1905 leaf_space_used(l, mid, nritems - mid) +
1906 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1907 double_split = 1;
1908 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04001909 }
Chris Masond4dbff92007-04-04 14:08:15 -04001910 }
1911 }
Chris Mason5f39d392007-10-15 16:14:19 -04001912 nritems = nritems - mid;
1913 btrfs_set_header_nritems(right, nritems);
1914 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1915
1916 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1917 btrfs_item_nr_offset(mid),
1918 nritems * sizeof(struct btrfs_item));
1919
1920 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04001921 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1922 data_copy_size, btrfs_leaf_data(l) +
1923 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05001924
Chris Mason5f39d392007-10-15 16:14:19 -04001925 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1926 btrfs_item_end_nr(l, mid);
1927
1928 for (i = 0; i < nritems; i++) {
1929 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001930 u32 ioff;
1931
1932 if (!right->map_token) {
1933 map_extent_buffer(right, (unsigned long)item,
1934 sizeof(struct btrfs_item),
1935 &right->map_token, &right->kaddr,
1936 &right->map_start, &right->map_len,
1937 KM_USER1);
1938 }
1939
1940 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04001941 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04001942 }
Chris Mason74123bd2007-02-02 11:05:29 -05001943
Chris Masondb945352007-10-15 16:15:53 -04001944 if (right->map_token) {
1945 unmap_extent_buffer(right, right->map_token, KM_USER1);
1946 right->map_token = NULL;
1947 }
1948
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001950 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001951 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001952 wret = insert_ptr(trans, root, path, &disk_key, right->start,
1953 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001954 if (wret)
1955 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001956
1957 btrfs_mark_buffer_dirty(right);
1958 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001959 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04001960
Chris Masonbe0e5c02007-01-26 15:51:26 -05001961 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04001962 free_extent_buffer(path->nodes[0]);
1963 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 path->slots[0] -= mid;
1965 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001966 } else
Chris Mason5f39d392007-10-15 16:14:19 -04001967 free_extent_buffer(right);
1968
Chris Masoneb60cea2007-02-02 09:18:22 -05001969 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001970
Chris Masoncc0c5532007-10-25 15:42:57 -04001971 if (double_split) {
1972 BUG_ON(num_doubles != 0);
1973 num_doubles++;
1974 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04001975 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001976 return ret;
1977}
1978
Chris Masonb18c6682007-04-17 13:26:50 -04001979int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1980 struct btrfs_root *root,
1981 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04001982 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04001983{
1984 int ret = 0;
1985 int slot;
1986 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04001987 struct extent_buffer *leaf;
1988 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04001989 u32 nritems;
1990 unsigned int data_end;
1991 unsigned int old_data_start;
1992 unsigned int old_size;
1993 unsigned int size_diff;
1994 int i;
1995
1996 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001997 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04001998 slot = path->slots[0];
1999
2000 old_size = btrfs_item_size_nr(leaf, slot);
2001 if (old_size == new_size)
2002 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002005 data_end = leaf_data_end(root, leaf);
2006
Chris Mason5f39d392007-10-15 16:14:19 -04002007 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002008
Chris Masonb18c6682007-04-17 13:26:50 -04002009 size_diff = old_size - new_size;
2010
2011 BUG_ON(slot < 0);
2012 BUG_ON(slot >= nritems);
2013
2014 /*
2015 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2016 */
2017 /* first correct the data pointers */
2018 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002019 u32 ioff;
2020 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002021
2022 if (!leaf->map_token) {
2023 map_extent_buffer(leaf, (unsigned long)item,
2024 sizeof(struct btrfs_item),
2025 &leaf->map_token, &leaf->kaddr,
2026 &leaf->map_start, &leaf->map_len,
2027 KM_USER1);
2028 }
2029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 ioff = btrfs_item_offset(leaf, item);
2031 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002032 }
Chris Masondb945352007-10-15 16:15:53 -04002033
2034 if (leaf->map_token) {
2035 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2036 leaf->map_token = NULL;
2037 }
2038
Chris Masonb18c6682007-04-17 13:26:50 -04002039 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002040 if (from_end) {
2041 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2042 data_end + size_diff, btrfs_leaf_data(leaf) +
2043 data_end, old_data_start + new_size - data_end);
2044 } else {
2045 struct btrfs_disk_key disk_key;
2046 u64 offset;
2047
2048 btrfs_item_key(leaf, &disk_key, slot);
2049
2050 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2051 unsigned long ptr;
2052 struct btrfs_file_extent_item *fi;
2053
2054 fi = btrfs_item_ptr(leaf, slot,
2055 struct btrfs_file_extent_item);
2056 fi = (struct btrfs_file_extent_item *)(
2057 (unsigned long)fi - size_diff);
2058
2059 if (btrfs_file_extent_type(leaf, fi) ==
2060 BTRFS_FILE_EXTENT_INLINE) {
2061 ptr = btrfs_item_ptr_offset(leaf, slot);
2062 memmove_extent_buffer(leaf, ptr,
2063 (unsigned long)fi,
2064 offsetof(struct btrfs_file_extent_item,
2065 disk_bytenr));
2066 }
2067 }
2068
2069 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2070 data_end + size_diff, btrfs_leaf_data(leaf) +
2071 data_end, old_data_start - data_end);
2072
2073 offset = btrfs_disk_key_offset(&disk_key);
2074 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2075 btrfs_set_item_key(leaf, &disk_key, slot);
2076 if (slot == 0)
2077 fixup_low_keys(trans, root, path, &disk_key, 1);
2078 }
Chris Mason5f39d392007-10-15 16:14:19 -04002079
2080 item = btrfs_item_nr(leaf, slot);
2081 btrfs_set_item_size(leaf, item, new_size);
2082 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002083
2084 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002085 if (btrfs_leaf_free_space(root, leaf) < 0) {
2086 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002087 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002088 }
Chris Masonb18c6682007-04-17 13:26:50 -04002089 return ret;
2090}
2091
Chris Mason5f39d392007-10-15 16:14:19 -04002092int btrfs_extend_item(struct btrfs_trans_handle *trans,
2093 struct btrfs_root *root, struct btrfs_path *path,
2094 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002095{
2096 int ret = 0;
2097 int slot;
2098 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002099 struct extent_buffer *leaf;
2100 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002101 u32 nritems;
2102 unsigned int data_end;
2103 unsigned int old_data;
2104 unsigned int old_size;
2105 int i;
2106
2107 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002108 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002109
Chris Mason5f39d392007-10-15 16:14:19 -04002110 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002111 data_end = leaf_data_end(root, leaf);
2112
Chris Mason5f39d392007-10-15 16:14:19 -04002113 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2114 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002115 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002116 }
Chris Mason6567e832007-04-16 09:22:45 -04002117 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002118 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002119
2120 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002121 if (slot >= nritems) {
2122 btrfs_print_leaf(root, leaf);
2123 printk("slot %d too large, nritems %d\n", slot, nritems);
2124 BUG_ON(1);
2125 }
Chris Mason6567e832007-04-16 09:22:45 -04002126
2127 /*
2128 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2129 */
2130 /* first correct the data pointers */
2131 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002132 u32 ioff;
2133 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002134
2135 if (!leaf->map_token) {
2136 map_extent_buffer(leaf, (unsigned long)item,
2137 sizeof(struct btrfs_item),
2138 &leaf->map_token, &leaf->kaddr,
2139 &leaf->map_start, &leaf->map_len,
2140 KM_USER1);
2141 }
Chris Mason5f39d392007-10-15 16:14:19 -04002142 ioff = btrfs_item_offset(leaf, item);
2143 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002144 }
Chris Mason5f39d392007-10-15 16:14:19 -04002145
Chris Masondb945352007-10-15 16:15:53 -04002146 if (leaf->map_token) {
2147 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2148 leaf->map_token = NULL;
2149 }
2150
Chris Mason6567e832007-04-16 09:22:45 -04002151 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002152 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002153 data_end - data_size, btrfs_leaf_data(leaf) +
2154 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002155
Chris Mason6567e832007-04-16 09:22:45 -04002156 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002157 old_size = btrfs_item_size_nr(leaf, slot);
2158 item = btrfs_item_nr(leaf, slot);
2159 btrfs_set_item_size(leaf, item, old_size + data_size);
2160 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002161
2162 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002163 if (btrfs_leaf_free_space(root, leaf) < 0) {
2164 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002165 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002166 }
Chris Mason6567e832007-04-16 09:22:45 -04002167 return ret;
2168}
2169
Chris Mason74123bd2007-02-02 11:05:29 -05002170/*
2171 * Given a key and some data, insert an item into the tree.
2172 * This does all the path init required, making room in the tree if needed.
2173 */
Chris Mason5f39d392007-10-15 16:14:19 -04002174int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2175 struct btrfs_root *root,
2176 struct btrfs_path *path,
2177 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002178{
Chris Mason5f39d392007-10-15 16:14:19 -04002179 struct extent_buffer *leaf;
2180 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002181 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002183 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002184 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002185 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002186 struct btrfs_disk_key disk_key;
2187
2188 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002189
Chris Mason74123bd2007-02-02 11:05:29 -05002190 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002191 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002192 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002193
Chris Masone089f052007-03-16 16:20:31 -04002194 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002195 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002196 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002197 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002198 if (ret < 0)
2199 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002200
Chris Mason62e27492007-03-15 12:56:47 -04002201 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002202 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002203
Chris Mason5f39d392007-10-15 16:14:19 -04002204 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002205 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002206
Chris Mason123abc82007-03-14 14:14:43 -04002207 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002208 sizeof(struct btrfs_item) + data_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002209 btrfs_print_leaf(root, leaf);
2210 printk("not enough freespace need %u have %d\n",
2211 data_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002212 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002213 }
Chris Mason5f39d392007-10-15 16:14:19 -04002214
Chris Mason62e27492007-03-15 12:56:47 -04002215 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002216 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002217
Chris Masonbe0e5c02007-01-26 15:51:26 -05002218 if (slot != nritems) {
2219 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002220 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002221
Chris Mason5f39d392007-10-15 16:14:19 -04002222 if (old_data < data_end) {
2223 btrfs_print_leaf(root, leaf);
2224 printk("slot %d old_data %d data_end %d\n",
2225 slot, old_data, data_end);
2226 BUG_ON(1);
2227 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002228 /*
2229 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2230 */
2231 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002232 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002233 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002234 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002235
Chris Mason5f39d392007-10-15 16:14:19 -04002236 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002237 if (!leaf->map_token) {
2238 map_extent_buffer(leaf, (unsigned long)item,
2239 sizeof(struct btrfs_item),
2240 &leaf->map_token, &leaf->kaddr,
2241 &leaf->map_start, &leaf->map_len,
2242 KM_USER1);
2243 }
2244
Chris Mason5f39d392007-10-15 16:14:19 -04002245 ioff = btrfs_item_offset(leaf, item);
2246 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002247 }
Chris Masondb945352007-10-15 16:15:53 -04002248 if (leaf->map_token) {
2249 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2250 leaf->map_token = NULL;
2251 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002252
2253 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002254 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2255 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002256 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002257
2258 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002259 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002260 data_end - data_size, btrfs_leaf_data(leaf) +
2261 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002262 data_end = old_data;
2263 }
Chris Mason5f39d392007-10-15 16:14:19 -04002264
Chris Mason62e27492007-03-15 12:56:47 -04002265 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002266 btrfs_set_item_key(leaf, &disk_key, slot);
2267 item = btrfs_item_nr(leaf, slot);
2268 btrfs_set_item_offset(leaf, item, data_end - data_size);
2269 btrfs_set_item_size(leaf, item, data_size);
2270 btrfs_set_header_nritems(leaf, nritems + 1);
2271 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002272
2273 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002274 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002275 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002276
Chris Mason5f39d392007-10-15 16:14:19 -04002277 if (btrfs_leaf_free_space(root, leaf) < 0) {
2278 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002279 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002280 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002281out:
Chris Mason62e27492007-03-15 12:56:47 -04002282 return ret;
2283}
2284
2285/*
2286 * Given a key and some data, insert an item into the tree.
2287 * This does all the path init required, making room in the tree if needed.
2288 */
Chris Masone089f052007-03-16 16:20:31 -04002289int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2290 *root, struct btrfs_key *cpu_key, void *data, u32
2291 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002292{
2293 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002294 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002295 struct extent_buffer *leaf;
2296 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002297
Chris Mason2c90e5d2007-04-02 10:50:19 -04002298 path = btrfs_alloc_path();
2299 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002300 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002301 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002302 leaf = path->nodes[0];
2303 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2304 write_extent_buffer(leaf, data, ptr, data_size);
2305 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002306 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002307 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002308 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002309}
2310
Chris Mason74123bd2007-02-02 11:05:29 -05002311/*
Chris Mason5de08d72007-02-24 06:24:44 -05002312 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002313 *
2314 * If the delete empties a node, the node is removed from the tree,
2315 * continuing all the way the root if required. The root is converted into
2316 * a leaf if all the nodes are emptied.
2317 */
Chris Masone089f052007-03-16 16:20:31 -04002318static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2319 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002320{
Chris Mason5f39d392007-10-15 16:14:19 -04002321 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002322 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002323 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002324 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002325
Chris Mason5f39d392007-10-15 16:14:19 -04002326 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002327 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002328 memmove_extent_buffer(parent,
2329 btrfs_node_key_ptr_offset(slot),
2330 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002331 sizeof(struct btrfs_key_ptr) *
2332 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002333 }
Chris Mason7518a232007-03-12 12:01:18 -04002334 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002335 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002336 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002337 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002338 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002339 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002340 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002341 struct btrfs_disk_key disk_key;
2342
2343 btrfs_node_key(parent, &disk_key, 0);
2344 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002345 if (wret)
2346 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002347 }
Chris Masond6025572007-03-30 14:27:56 -04002348 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002349 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002350}
2351
Chris Mason74123bd2007-02-02 11:05:29 -05002352/*
2353 * delete the item at the leaf level in path. If that empties
2354 * the leaf, remove it from the tree
2355 */
Chris Masone089f052007-03-16 16:20:31 -04002356int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2357 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002358{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002359 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002360 struct extent_buffer *leaf;
2361 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002362 int doff;
2363 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002364 int ret = 0;
2365 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002366 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002367
Chris Mason5f39d392007-10-15 16:14:19 -04002368 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002369 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002370 doff = btrfs_item_offset_nr(leaf, slot);
2371 dsize = btrfs_item_size_nr(leaf, slot);
2372 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002373
Chris Mason7518a232007-03-12 12:01:18 -04002374 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002375 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002376 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002377
2378 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002379 data_end + dsize,
2380 btrfs_leaf_data(leaf) + data_end,
2381 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002382
Chris Mason0783fcf2007-03-12 20:12:07 -04002383 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002384 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002385
Chris Mason5f39d392007-10-15 16:14:19 -04002386 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002387 if (!leaf->map_token) {
2388 map_extent_buffer(leaf, (unsigned long)item,
2389 sizeof(struct btrfs_item),
2390 &leaf->map_token, &leaf->kaddr,
2391 &leaf->map_start, &leaf->map_len,
2392 KM_USER1);
2393 }
Chris Mason5f39d392007-10-15 16:14:19 -04002394 ioff = btrfs_item_offset(leaf, item);
2395 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002396 }
Chris Masondb945352007-10-15 16:15:53 -04002397
2398 if (leaf->map_token) {
2399 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2400 leaf->map_token = NULL;
2401 }
2402
Chris Mason5f39d392007-10-15 16:14:19 -04002403 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2404 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002405 sizeof(struct btrfs_item) *
2406 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002407 }
Chris Mason5f39d392007-10-15 16:14:19 -04002408 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002409 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002410
Chris Mason74123bd2007-02-02 11:05:29 -05002411 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002412 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002413 if (leaf == root->node) {
2414 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002415 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002416 clean_tree_block(trans, root, leaf);
2417 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002418 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002419 if (wret)
2420 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002421 wret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002422 leaf->start, leaf->len, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002423 if (wret)
2424 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002425 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002426 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002427 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002428 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002429 struct btrfs_disk_key disk_key;
2430
2431 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002432 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002433 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002434 if (wret)
2435 ret = wret;
2436 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002437
Chris Mason74123bd2007-02-02 11:05:29 -05002438 /* delete the leaf if it is mostly empty */
Chris Mason7936ca32007-10-19 09:22:41 -04002439 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002440 /* push_leaf_left fixes the path.
2441 * make sure the path still points to our leaf
2442 * for possible call to del_ptr below
2443 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002444 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002445 extent_buffer_get(leaf);
2446
Chris Mason34a38212007-11-07 13:31:03 -05002447 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002448 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002449 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002450
2451 if (path->nodes[0] == leaf &&
2452 btrfs_header_nritems(leaf)) {
Chris Mason34a38212007-11-07 13:31:03 -05002453 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002454 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002455 ret = wret;
2456 }
Chris Mason5f39d392007-10-15 16:14:19 -04002457
2458 if (btrfs_header_nritems(leaf) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002459 u64 bytenr = leaf->start;
2460 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002461
2462 clean_tree_block(trans, root, leaf);
2463 wait_on_tree_block_writeback(root, leaf);
2464
Chris Masone089f052007-03-16 16:20:31 -04002465 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002466 if (wret)
2467 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002468
2469 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002470 wret = btrfs_free_extent(trans, root, bytenr,
2471 blocksize, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002472 if (wret)
2473 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002474 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002475 btrfs_mark_buffer_dirty(leaf);
2476 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002477 }
Chris Masond5719762007-03-23 10:01:08 -04002478 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002479 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 }
2481 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002482 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002483}
2484
Chris Mason97571fd2007-02-24 13:39:08 -05002485/*
2486 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002487 * returns 0 if it found something or 1 if there are no greater leaves.
2488 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002489 */
Chris Mason234b63a2007-03-13 10:46:10 -04002490int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002491{
2492 int slot;
2493 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002494 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002495 struct extent_buffer *c;
2496 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002497
Chris Mason234b63a2007-03-13 10:46:10 -04002498 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002499 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002500 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002501
Chris Masond97e63b2007-02-20 16:40:44 -05002502 slot = path->slots[level] + 1;
2503 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002504 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002505 level++;
2506 continue;
2507 }
Chris Mason5f39d392007-10-15 16:14:19 -04002508
Chris Masondb945352007-10-15 16:15:53 -04002509 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002510 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002511 free_extent_buffer(next);
2512
Chris Mason6702ed42007-08-07 16:15:09 -04002513 if (path->reada)
2514 reada_for_search(root, path, level, slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002515
Chris Masondb945352007-10-15 16:15:53 -04002516 next = read_tree_block(root, bytenr,
2517 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002518 break;
2519 }
2520 path->slots[level] = slot;
2521 while(1) {
2522 level--;
2523 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002524 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002525 path->nodes[level] = next;
2526 path->slots[level] = 0;
2527 if (!level)
2528 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002529 if (path->reada)
Yan32020612007-08-27 15:17:54 -04002530 reada_for_search(root, path, level, 0);
Chris Masondb945352007-10-15 16:15:53 -04002531 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2532 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002533 }
2534 return 0;
2535}