blob: eef9c92f86d5408397c98e4e373e57b86d7f0a08 [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 Mason6702ed42007-08-07 16:15:09 -0400117 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400118 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Masondb945352007-10-15 16:15:53 -0400119 btrfs_free_extent(trans, root, buf->start, buf->len, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400120 }
Chris Mason5f39d392007-10-15 16:14:19 -0400121 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400122 btrfs_mark_buffer_dirty(cow);
123 *cow_ret = cow;
124 return 0;
125}
126
Chris Mason5f39d392007-10-15 16:14:19 -0400127int btrfs_cow_block(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root, struct extent_buffer *buf,
129 struct extent_buffer *parent, int parent_slot,
130 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500131{
Chris Mason6702ed42007-08-07 16:15:09 -0400132 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400133 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400134 if (trans->transaction != root->fs_info->running_transaction) {
135 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
136 root->fs_info->running_transaction->transid);
137 WARN_ON(1);
138 }
139 if (trans->transid != root->fs_info->generation) {
140 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
141 root->fs_info->generation);
142 WARN_ON(1);
143 }
Chris Mason5f39d392007-10-15 16:14:19 -0400144 if (btrfs_header_generation(buf) == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500145 *cow_ret = buf;
146 return 0;
147 }
Chris Mason6702ed42007-08-07 16:15:09 -0400148
Chris Masondb945352007-10-15 16:15:53 -0400149 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400150 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400151 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400152 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400153}
154
Chris Mason6b800532007-10-15 16:17:34 -0400155static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400156{
Chris Mason6b800532007-10-15 16:17:34 -0400157 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400158 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400159 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400160 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500161 return 0;
162}
163
Chris Mason6702ed42007-08-07 16:15:09 -0400164int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400165 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400166 int start_slot, int cache_only, u64 *last_ret,
167 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400168{
Chris Mason6b800532007-10-15 16:17:34 -0400169 struct extent_buffer *cur;
170 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400171 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400172 u64 search_start = *last_ret;
173 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400174 u64 other;
175 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400176 int end_slot;
177 int i;
178 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400179 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400180 int uptodate;
181 u32 blocksize;
Chris Mason6702ed42007-08-07 16:15:09 -0400182
Chris Mason5708b952007-10-25 15:43:18 -0400183 parent_level = btrfs_header_level(parent);
184 if (cache_only && parent_level != 1)
185 return 0;
186
Chris Mason6702ed42007-08-07 16:15:09 -0400187 if (trans->transaction != root->fs_info->running_transaction) {
188 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
189 root->fs_info->running_transaction->transid);
190 WARN_ON(1);
191 }
192 if (trans->transid != root->fs_info->generation) {
193 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
194 root->fs_info->generation);
195 WARN_ON(1);
196 }
Chris Mason86479a02007-09-10 19:58:16 -0400197
Chris Mason6b800532007-10-15 16:17:34 -0400198 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400199 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400200 end_slot = parent_nritems;
201
202 if (parent_nritems == 1)
203 return 0;
204
205 for (i = start_slot; i < end_slot; i++) {
206 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400207
Chris Mason5708b952007-10-25 15:43:18 -0400208 if (!parent->map_token) {
209 map_extent_buffer(parent,
210 btrfs_node_key_ptr_offset(i),
211 sizeof(struct btrfs_key_ptr),
212 &parent->map_token, &parent->kaddr,
213 &parent->map_start, &parent->map_len,
214 KM_USER1);
215 }
Chris Mason6b800532007-10-15 16:17:34 -0400216 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400217 if (last_block == 0)
218 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400219
Chris Mason6702ed42007-08-07 16:15:09 -0400220 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400221 other = btrfs_node_blockptr(parent, i - 1);
222 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400223 }
Chris Mason5708b952007-10-25 15:43:18 -0400224 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400225 other = btrfs_node_blockptr(parent, i + 1);
226 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400227 }
Chris Masone9d0b132007-08-10 14:06:19 -0400228 if (close) {
229 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400230 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400231 }
Chris Mason5708b952007-10-25 15:43:18 -0400232 if (parent->map_token) {
233 unmap_extent_buffer(parent, parent->map_token,
234 KM_USER1);
235 parent->map_token = NULL;
236 }
Chris Mason6702ed42007-08-07 16:15:09 -0400237
Chris Mason6b800532007-10-15 16:17:34 -0400238 cur = btrfs_find_tree_block(root, blocknr, blocksize);
239 if (cur)
240 uptodate = btrfs_buffer_uptodate(cur);
241 else
242 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400243 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400244 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400245 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400246 continue;
247 }
Chris Mason6b800532007-10-15 16:17:34 -0400248 if (!cur) {
249 cur = read_tree_block(root, blocknr,
250 blocksize);
251 } else if (!uptodate) {
252 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400253 }
Chris Mason6702ed42007-08-07 16:15:09 -0400254 }
Chris Masone9d0b132007-08-10 14:06:19 -0400255 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400256 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400257
Chris Mason6b800532007-10-15 16:17:34 -0400258 err = __btrfs_cow_block(trans, root, cur, parent, i,
259 &tmp, search_start,
260 min(16 * blocksize,
261 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400262 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400263 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400264 break;
Yan252c38f2007-08-29 09:11:44 -0400265 }
Chris Mason6b800532007-10-15 16:17:34 -0400266 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400267 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400268 *last_ret = search_start;
269 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400270 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400271 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400272 }
Chris Mason5708b952007-10-25 15:43:18 -0400273 if (parent->map_token) {
274 unmap_extent_buffer(parent, parent->map_token,
275 KM_USER1);
276 parent->map_token = NULL;
277 }
Chris Mason6702ed42007-08-07 16:15:09 -0400278 return err;
279}
280
Chris Mason74123bd2007-02-02 11:05:29 -0500281/*
282 * The leaf data grows from end-to-front in the node.
283 * this returns the address of the start of the last item,
284 * which is the stop of the leaf data stack
285 */
Chris Mason123abc82007-03-14 14:14:43 -0400286static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400287 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500288{
Chris Mason5f39d392007-10-15 16:14:19 -0400289 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500290 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400291 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400292 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500293}
294
Chris Mason74123bd2007-02-02 11:05:29 -0500295/*
Chris Mason74123bd2007-02-02 11:05:29 -0500296 * compare two keys in a memcmp fashion
297 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400298static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500299{
Chris Masone2fa7222007-03-12 16:22:34 -0400300 struct btrfs_key k1;
301
302 btrfs_disk_key_to_cpu(&k1, disk);
303
304 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500305 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400306 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500307 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -0400308 if (k1.type > k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400309 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400310 if (k1.type < k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400311 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400312 if (k1.offset > k2->offset)
313 return 1;
314 if (k1.offset < k2->offset)
315 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500316 return 0;
317}
Chris Mason74123bd2007-02-02 11:05:29 -0500318
Chris Mason123abc82007-03-14 14:14:43 -0400319static int check_node(struct btrfs_root *root, struct btrfs_path *path,
320 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500321{
Chris Mason5f39d392007-10-15 16:14:19 -0400322 struct extent_buffer *parent = NULL;
323 struct extent_buffer *node = path->nodes[level];
324 struct btrfs_disk_key parent_key;
325 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500326 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400327 int slot;
328 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400329 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500330
331 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400332 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400333
Chris Mason8d7be552007-05-10 11:24:42 -0400334 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400335 BUG_ON(nritems == 0);
336 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400337 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400338 btrfs_node_key(parent, &parent_key, parent_slot);
339 btrfs_node_key(node, &node_key, 0);
340 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400341 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400342 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400343 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500344 }
Chris Mason123abc82007-03-14 14:14:43 -0400345 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400346 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400347 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
348 btrfs_node_key(node, &node_key, slot);
349 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400350 }
351 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400352 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
353 btrfs_node_key(node, &node_key, slot);
354 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500355 }
356 return 0;
357}
358
Chris Mason123abc82007-03-14 14:14:43 -0400359static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
360 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500361{
Chris Mason5f39d392007-10-15 16:14:19 -0400362 struct extent_buffer *leaf = path->nodes[level];
363 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500364 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400365 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400366 struct btrfs_disk_key parent_key;
367 struct btrfs_disk_key leaf_key;
368 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400369
Chris Mason5f39d392007-10-15 16:14:19 -0400370 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500371
372 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400373 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400374
375 if (nritems == 0)
376 return 0;
377
378 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400379 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400380 btrfs_node_key(parent, &parent_key, parent_slot);
381 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400382
Chris Mason5f39d392007-10-15 16:14:19 -0400383 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400384 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400385 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400386 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500387 }
Chris Mason5f39d392007-10-15 16:14:19 -0400388#if 0
389 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
390 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
391 btrfs_item_key(leaf, &leaf_key, i);
392 if (comp_keys(&leaf_key, &cpukey) >= 0) {
393 btrfs_print_leaf(root, leaf);
394 printk("slot %d offset bad key\n", i);
395 BUG_ON(1);
396 }
397 if (btrfs_item_offset_nr(leaf, i) !=
398 btrfs_item_end_nr(leaf, i + 1)) {
399 btrfs_print_leaf(root, leaf);
400 printk("slot %d offset bad\n", i);
401 BUG_ON(1);
402 }
403 if (i == 0) {
404 if (btrfs_item_offset_nr(leaf, i) +
405 btrfs_item_size_nr(leaf, i) !=
406 BTRFS_LEAF_DATA_SIZE(root)) {
407 btrfs_print_leaf(root, leaf);
408 printk("slot %d first offset bad\n", i);
409 BUG_ON(1);
410 }
411 }
412 }
413 if (nritems > 0) {
414 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
415 btrfs_print_leaf(root, leaf);
416 printk("slot %d bad size \n", nritems - 1);
417 BUG_ON(1);
418 }
419 }
420#endif
421 if (slot != 0 && slot < nritems - 1) {
422 btrfs_item_key(leaf, &leaf_key, slot);
423 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
424 if (comp_keys(&leaf_key, &cpukey) <= 0) {
425 btrfs_print_leaf(root, leaf);
426 printk("slot %d offset bad key\n", slot);
427 BUG_ON(1);
428 }
429 if (btrfs_item_offset_nr(leaf, slot - 1) !=
430 btrfs_item_end_nr(leaf, slot)) {
431 btrfs_print_leaf(root, leaf);
432 printk("slot %d offset bad\n", slot);
433 BUG_ON(1);
434 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500435 }
Chris Mason8d7be552007-05-10 11:24:42 -0400436 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400437 btrfs_item_key(leaf, &leaf_key, slot);
438 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
439 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
440 if (btrfs_item_offset_nr(leaf, slot) !=
441 btrfs_item_end_nr(leaf, slot + 1)) {
442 btrfs_print_leaf(root, leaf);
443 printk("slot %d offset bad\n", slot);
444 BUG_ON(1);
445 }
Chris Mason8d7be552007-05-10 11:24:42 -0400446 }
Chris Mason5f39d392007-10-15 16:14:19 -0400447 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
448 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500449 return 0;
450}
451
Chris Mason123abc82007-03-14 14:14:43 -0400452static int check_block(struct btrfs_root *root, struct btrfs_path *path,
453 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500454{
Chris Mason810191f2007-10-15 16:18:55 -0400455 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400456#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400457 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400458
Chris Mason479965d2007-10-15 16:14:27 -0400459 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
460 (unsigned long)btrfs_header_fsid(buf),
461 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400462 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400463 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400464 }
Chris Masondb945352007-10-15 16:15:53 -0400465#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500466 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400467 return check_leaf(root, path, level);
468 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500469}
470
Chris Mason74123bd2007-02-02 11:05:29 -0500471/*
Chris Mason5f39d392007-10-15 16:14:19 -0400472 * search for key in the extent_buffer. The items start at offset p,
473 * and they are item_size apart. There are 'max' items in p.
474 *
Chris Mason74123bd2007-02-02 11:05:29 -0500475 * the slot in the array is returned via slot, and it points to
476 * the place where you would insert key if it is not found in
477 * the array.
478 *
479 * slot may point to max if the key is bigger than all of the keys
480 */
Chris Mason5f39d392007-10-15 16:14:19 -0400481static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
482 int item_size, struct btrfs_key *key,
483 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500484{
485 int low = 0;
486 int high = max;
487 int mid;
488 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400489 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400490 struct btrfs_disk_key unaligned;
491 unsigned long offset;
492 char *map_token = NULL;
493 char *kaddr = NULL;
494 unsigned long map_start = 0;
495 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400496 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500497
498 while(low < high) {
499 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400500 offset = p + mid * item_size;
501
502 if (!map_token || offset < map_start ||
503 (offset + sizeof(struct btrfs_disk_key)) >
504 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400505 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400506 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400507 map_token = NULL;
508 }
509 err = map_extent_buffer(eb, offset,
510 sizeof(struct btrfs_disk_key),
511 &map_token, &kaddr,
512 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400513
Chris Mason479965d2007-10-15 16:14:27 -0400514 if (!err) {
515 tmp = (struct btrfs_disk_key *)(kaddr + offset -
516 map_start);
517 } else {
518 read_extent_buffer(eb, &unaligned,
519 offset, sizeof(unaligned));
520 tmp = &unaligned;
521 }
522
Chris Mason5f39d392007-10-15 16:14:19 -0400523 } else {
524 tmp = (struct btrfs_disk_key *)(kaddr + offset -
525 map_start);
526 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500527 ret = comp_keys(tmp, key);
528
529 if (ret < 0)
530 low = mid + 1;
531 else if (ret > 0)
532 high = mid;
533 else {
534 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400535 if (map_token)
536 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500537 return 0;
538 }
539 }
540 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400541 if (map_token)
542 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500543 return 1;
544}
545
Chris Mason97571fd2007-02-24 13:39:08 -0500546/*
547 * simple bin_search frontend that does the right thing for
548 * leaves vs nodes
549 */
Chris Mason5f39d392007-10-15 16:14:19 -0400550static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
551 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500552{
Chris Mason5f39d392007-10-15 16:14:19 -0400553 if (level == 0) {
554 return generic_bin_search(eb,
555 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400556 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400557 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400558 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500559 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400560 return generic_bin_search(eb,
561 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400562 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400563 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400564 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500565 }
566 return -1;
567}
568
Chris Mason5f39d392007-10-15 16:14:19 -0400569static struct extent_buffer *read_node_slot(struct btrfs_root *root,
570 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500571{
Chris Masonbb803952007-03-01 12:04:21 -0500572 if (slot < 0)
573 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400574 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500575 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400576 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
577 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500578}
579
Chris Masone089f052007-03-16 16:20:31 -0400580static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
581 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500582{
Chris Mason5f39d392007-10-15 16:14:19 -0400583 struct extent_buffer *right = NULL;
584 struct extent_buffer *mid;
585 struct extent_buffer *left = NULL;
586 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500587 int ret = 0;
588 int wret;
589 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500590 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400591 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500592 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500593
594 if (level == 0)
595 return 0;
596
Chris Mason5f39d392007-10-15 16:14:19 -0400597 mid = path->nodes[level];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400598 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500599
Chris Mason234b63a2007-03-13 10:46:10 -0400600 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400601 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500602 pslot = path->slots[level + 1];
603
Chris Mason40689472007-03-17 14:29:23 -0400604 /*
605 * deal with the case where there is only one pointer in the root
606 * by promoting the node below to a root
607 */
Chris Mason5f39d392007-10-15 16:14:19 -0400608 if (!parent) {
609 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500610
Chris Mason5f39d392007-10-15 16:14:19 -0400611 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500612 return 0;
613
614 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400615 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500616 BUG_ON(!child);
617 root->node = child;
618 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400619 clean_tree_block(trans, root, mid);
620 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500621 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400622 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400623 ret = btrfs_free_extent(trans, root, mid->start, mid->len, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500624 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400625 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400626 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500627 }
Chris Mason5f39d392007-10-15 16:14:19 -0400628 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400629 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500630 return 0;
631
Chris Mason5f39d392007-10-15 16:14:19 -0400632 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400633 err_on_enospc = 1;
634
Chris Mason5f39d392007-10-15 16:14:19 -0400635 left = read_node_slot(root, parent, pslot - 1);
636 if (left) {
637 wret = btrfs_cow_block(trans, root, left,
638 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400639 if (wret) {
640 ret = wret;
641 goto enospc;
642 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400643 }
Chris Mason5f39d392007-10-15 16:14:19 -0400644 right = read_node_slot(root, parent, pslot + 1);
645 if (right) {
646 wret = btrfs_cow_block(trans, root, right,
647 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400648 if (wret) {
649 ret = wret;
650 goto enospc;
651 }
652 }
653
654 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400655 if (left) {
656 orig_slot += btrfs_header_nritems(left);
657 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500658 if (wret < 0)
659 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400660 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400661 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500662 }
Chris Mason79f95c82007-03-01 15:16:26 -0500663
664 /*
665 * then try to empty the right most buffer into the middle
666 */
Chris Mason5f39d392007-10-15 16:14:19 -0400667 if (right) {
668 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400669 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500670 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400671 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400672 u64 bytenr = right->start;
673 u32 blocksize = right->len;
674
Chris Mason5f39d392007-10-15 16:14:19 -0400675 clean_tree_block(trans, root, right);
676 wait_on_tree_block_writeback(root, right);
677 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500678 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400679 wret = del_ptr(trans, root, path, level + 1, pslot +
680 1);
Chris Masonbb803952007-03-01 12:04:21 -0500681 if (wret)
682 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400683 wret = btrfs_free_extent(trans, root, bytenr,
684 blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500685 if (wret)
686 ret = wret;
687 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400688 struct btrfs_disk_key right_key;
689 btrfs_node_key(right, &right_key, 0);
690 btrfs_set_node_key(parent, &right_key, pslot + 1);
691 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500692 }
693 }
Chris Mason5f39d392007-10-15 16:14:19 -0400694 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500695 /*
696 * we're not allowed to leave a node with one item in the
697 * tree during a delete. A deletion from lower in the tree
698 * could try to delete the only pointer in this node.
699 * So, pull some keys from the left.
700 * There has to be a left pointer at this point because
701 * otherwise we would have pulled some pointers from the
702 * right
703 */
Chris Mason5f39d392007-10-15 16:14:19 -0400704 BUG_ON(!left);
705 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400706 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500707 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400708 goto enospc;
709 }
Chris Mason79f95c82007-03-01 15:16:26 -0500710 BUG_ON(wret == 1);
711 }
Chris Mason5f39d392007-10-15 16:14:19 -0400712 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500713 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -0400714 u64 bytenr = mid->start;
715 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400716 clean_tree_block(trans, root, mid);
717 wait_on_tree_block_writeback(root, mid);
718 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500719 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400720 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500721 if (wret)
722 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400723 wret = btrfs_free_extent(trans, root, bytenr, blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500724 if (wret)
725 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500726 } else {
727 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400728 struct btrfs_disk_key mid_key;
729 btrfs_node_key(mid, &mid_key, 0);
730 btrfs_set_node_key(parent, &mid_key, pslot);
731 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500732 }
Chris Masonbb803952007-03-01 12:04:21 -0500733
Chris Mason79f95c82007-03-01 15:16:26 -0500734 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400735 if (left) {
736 if (btrfs_header_nritems(left) > orig_slot) {
737 extent_buffer_get(left);
738 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500739 path->slots[level + 1] -= 1;
740 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400741 if (mid)
742 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500743 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400744 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500745 path->slots[level] = orig_slot;
746 }
747 }
Chris Mason79f95c82007-03-01 15:16:26 -0500748 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400749 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400750 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400751 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500752 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400753enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400754 if (right)
755 free_extent_buffer(right);
756 if (left)
757 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500758 return ret;
759}
760
Chris Masone66f7092007-04-20 13:16:02 -0400761/* returns zero if the push worked, non-zero otherwise */
762static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
763 struct btrfs_root *root,
764 struct btrfs_path *path, int level)
765{
Chris Mason5f39d392007-10-15 16:14:19 -0400766 struct extent_buffer *right = NULL;
767 struct extent_buffer *mid;
768 struct extent_buffer *left = NULL;
769 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400770 int ret = 0;
771 int wret;
772 int pslot;
773 int orig_slot = path->slots[level];
774 u64 orig_ptr;
775
776 if (level == 0)
777 return 1;
778
Chris Mason5f39d392007-10-15 16:14:19 -0400779 mid = path->nodes[level];
Chris Masone66f7092007-04-20 13:16:02 -0400780 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
781
782 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400783 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400784 pslot = path->slots[level + 1];
785
Chris Mason5f39d392007-10-15 16:14:19 -0400786 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400787 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400788
Chris Mason5f39d392007-10-15 16:14:19 -0400789 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400790
791 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400792 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400793 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400794 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400795 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
796 wret = 1;
797 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400798 ret = btrfs_cow_block(trans, root, left, parent,
799 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400800 if (ret)
801 wret = 1;
802 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400803 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400804 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400805 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400806 }
Chris Masone66f7092007-04-20 13:16:02 -0400807 if (wret < 0)
808 ret = wret;
809 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400810 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400811 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 btrfs_node_key(mid, &disk_key, 0);
813 btrfs_set_node_key(parent, &disk_key, pslot);
814 btrfs_mark_buffer_dirty(parent);
815 if (btrfs_header_nritems(left) > orig_slot) {
816 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400817 path->slots[level + 1] -= 1;
818 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400819 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400820 } else {
821 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400822 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400823 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400824 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400825 }
Chris Masone66f7092007-04-20 13:16:02 -0400826 return 0;
827 }
Chris Mason5f39d392007-10-15 16:14:19 -0400828 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400829 }
Chris Mason5f39d392007-10-15 16:14:19 -0400830 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400831
832 /*
833 * then try to empty the right most buffer into the middle
834 */
Chris Mason5f39d392007-10-15 16:14:19 -0400835 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400836 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400837 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400838 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
839 wret = 1;
840 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400841 ret = btrfs_cow_block(trans, root, right,
842 parent, pslot + 1,
843 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400844 if (ret)
845 wret = 1;
846 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400847 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400848 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400849 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400850 }
Chris Masone66f7092007-04-20 13:16:02 -0400851 if (wret < 0)
852 ret = wret;
853 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400854 struct btrfs_disk_key disk_key;
855
856 btrfs_node_key(right, &disk_key, 0);
857 btrfs_set_node_key(parent, &disk_key, pslot + 1);
858 btrfs_mark_buffer_dirty(parent);
859
860 if (btrfs_header_nritems(mid) <= orig_slot) {
861 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400862 path->slots[level + 1] += 1;
863 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400864 btrfs_header_nritems(mid);
865 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400866 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400867 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400868 }
Chris Masone66f7092007-04-20 13:16:02 -0400869 return 0;
870 }
Chris Mason5f39d392007-10-15 16:14:19 -0400871 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400872 }
Chris Masone66f7092007-04-20 13:16:02 -0400873 return 1;
874}
875
Chris Mason74123bd2007-02-02 11:05:29 -0500876/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400877 * readahead one full node of leaves
878 */
879static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400880 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400881{
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *node;
Chris Mason3c69fae2007-08-07 15:52:22 -0400883 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400884 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -0400885 u64 lowest_read;
886 u64 highest_read;
887 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -0400888 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -0400889 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -0400890 u32 nr;
891 u32 blocksize;
892 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -0400893
Chris Masona6b6e752007-10-15 16:22:39 -0400894 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -0400895 return;
896
Chris Mason6702ed42007-08-07 16:15:09 -0400897 if (!path->nodes[level])
898 return;
899
Chris Mason5f39d392007-10-15 16:14:19 -0400900 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -0400901 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -0400902 blocksize = btrfs_level_size(root, level - 1);
903 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400904 if (eb) {
905 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -0400906 return;
907 }
908
Chris Mason6b800532007-10-15 16:17:34 -0400909 highest_read = search;
910 lowest_read = search;
911
Chris Mason5f39d392007-10-15 16:14:19 -0400912 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -0400913 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -0400914 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -0400915 if (direction < 0) {
916 if (nr == 0)
917 break;
918 nr--;
919 } else if (direction > 0) {
920 nr++;
921 if (nr >= nritems)
922 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400923 }
Chris Mason6b800532007-10-15 16:17:34 -0400924 search = btrfs_node_blockptr(node, nr);
925 if ((search >= lowest_read && search <= highest_read) ||
926 (search < lowest_read && lowest_read - search <= 32768) ||
927 (search > highest_read && search - highest_read <= 32768)) {
928 readahead_tree_block(root, search, blocksize);
929 nread += blocksize;
930 }
931 nscan++;
932 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
933 break;
934 if(nread > (1024 * 1024) || nscan > 128)
935 break;
936
937 if (search < lowest_read)
938 lowest_read = search;
939 if (search > highest_read)
940 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -0400941 }
942}
943/*
Chris Mason74123bd2007-02-02 11:05:29 -0500944 * look for key in the tree. path is filled in with nodes along the way
945 * if key is found, we return zero and you can find the item in the leaf
946 * level of the path (level 0)
947 *
948 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500949 * be inserted, and 1 is returned. If there are other errors during the
950 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500951 *
952 * if ins_len > 0, nodes and leaves will be split as we walk down the
953 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
954 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500955 */
Chris Masone089f052007-03-16 16:20:31 -0400956int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
957 *root, struct btrfs_key *key, struct btrfs_path *p, int
958 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500959{
Chris Mason5f39d392007-10-15 16:14:19 -0400960 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -0400961 u64 bytenr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500962 int slot;
963 int ret;
964 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400965 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400966 u8 lowest_level = 0;
967
Chris Mason6702ed42007-08-07 16:15:09 -0400968 lowest_level = p->lowest_level;
969 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400970 WARN_ON(p->nodes[0] != NULL);
971 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500972again:
973 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -0400974 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500975 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -0400976 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500977 if (cow) {
978 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400979 wret = btrfs_cow_block(trans, root, b,
980 p->nodes[level + 1],
981 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -0400982 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400984 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 return wret;
986 }
Chris Mason02217ed2007-03-02 16:08:05 -0500987 }
988 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400990 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -0400991 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500992 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400993 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500994 if (ret)
995 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -0400996 ret = bin_search(b, key, level, &slot);
997 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500998 if (ret && slot > 0)
999 slot -= 1;
1000 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001001 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001002 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001003 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001004 BUG_ON(sret > 0);
1005 if (sret)
1006 return sret;
1007 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001008 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001009 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001010 int sret = balance_level(trans, root, p,
1011 level);
Chris Masonbb803952007-03-01 12:04:21 -05001012 if (sret)
1013 return sret;
1014 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001015 if (!b) {
1016 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001017 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001018 }
Chris Masonbb803952007-03-01 12:04:21 -05001019 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001020 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001021 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001022 /* this is only true while dropping a snapshot */
1023 if (level == lowest_level)
1024 break;
Chris Masondb945352007-10-15 16:15:53 -04001025 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001026 if (should_reada)
1027 reada_for_search(root, p, level, slot);
Chris Masondb945352007-10-15 16:15:53 -04001028 b = read_tree_block(root, bytenr,
1029 btrfs_level_size(root, level - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001030 } else {
1031 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001033 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001034 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001035 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001036 BUG_ON(sret > 0);
1037 if (sret)
1038 return sret;
1039 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001040 return ret;
1041 }
1042 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001043 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001044}
1045
Chris Mason74123bd2007-02-02 11:05:29 -05001046/*
1047 * adjust the pointers going up the tree, starting at level
1048 * making sure the right key of each node is points to 'key'.
1049 * This is used after shifting pointers to the left, so it stops
1050 * fixing up pointers when a given leaf/node is not in slot 0 of the
1051 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001052 *
1053 * If this fails to write a tree block, it returns -1, but continues
1054 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001055 */
Chris Mason5f39d392007-10-15 16:14:19 -04001056static int fixup_low_keys(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root, struct btrfs_path *path,
1058 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001059{
1060 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001061 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001062 struct extent_buffer *t;
1063
Chris Mason234b63a2007-03-13 10:46:10 -04001064 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001065 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001066 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001067 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001068 t = path->nodes[i];
1069 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001070 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001071 if (tslot != 0)
1072 break;
1073 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001074 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001075}
1076
Chris Mason74123bd2007-02-02 11:05:29 -05001077/*
1078 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001079 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001080 *
1081 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1082 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001083 */
Chris Masone089f052007-03-16 16:20:31 -04001084static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason5f39d392007-10-15 16:14:19 -04001085 *root, struct extent_buffer *dst,
1086 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001087{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001089 int src_nritems;
1090 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001091 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001092
Chris Mason5f39d392007-10-15 16:14:19 -04001093 src_nritems = btrfs_header_nritems(src);
1094 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001095 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001096
Chris Masoneb60cea2007-02-02 09:18:22 -05001097 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001098 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001099 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001100
1101 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001102 push_items = src_nritems;
1103
Chris Mason5f39d392007-10-15 16:14:19 -04001104 copy_extent_buffer(dst, src,
1105 btrfs_node_key_ptr_offset(dst_nritems),
1106 btrfs_node_key_ptr_offset(0),
1107 push_items * sizeof(struct btrfs_key_ptr));
1108
Chris Masonbb803952007-03-01 12:04:21 -05001109 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001110 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1111 btrfs_node_key_ptr_offset(push_items),
1112 (src_nritems - push_items) *
1113 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001114 }
Chris Mason5f39d392007-10-15 16:14:19 -04001115 btrfs_set_header_nritems(src, src_nritems - push_items);
1116 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1117 btrfs_mark_buffer_dirty(src);
1118 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001119 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001120}
1121
Chris Mason97571fd2007-02-24 13:39:08 -05001122/*
Chris Mason79f95c82007-03-01 15:16:26 -05001123 * try to push data from one node into the next node right in the
1124 * tree.
1125 *
1126 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1127 * error, and > 0 if there was no room in the right hand block.
1128 *
1129 * this will only push up to 1/2 the contents of the left node over
1130 */
Chris Mason5f39d392007-10-15 16:14:19 -04001131static int balance_node_right(struct btrfs_trans_handle *trans,
1132 struct btrfs_root *root,
1133 struct extent_buffer *dst,
1134 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001135{
Chris Mason79f95c82007-03-01 15:16:26 -05001136 int push_items = 0;
1137 int max_push;
1138 int src_nritems;
1139 int dst_nritems;
1140 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001141
Chris Mason5f39d392007-10-15 16:14:19 -04001142 src_nritems = btrfs_header_nritems(src);
1143 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001144 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001145 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001146 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001147
1148 max_push = src_nritems / 2 + 1;
1149 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001150 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001151 return 1;
Yan252c38f2007-08-29 09:11:44 -04001152
Chris Mason79f95c82007-03-01 15:16:26 -05001153 if (max_push < push_items)
1154 push_items = max_push;
1155
Chris Mason5f39d392007-10-15 16:14:19 -04001156 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1157 btrfs_node_key_ptr_offset(0),
1158 (dst_nritems) *
1159 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001160
Chris Mason5f39d392007-10-15 16:14:19 -04001161 copy_extent_buffer(dst, src,
1162 btrfs_node_key_ptr_offset(0),
1163 btrfs_node_key_ptr_offset(src_nritems - push_items),
1164 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001165
Chris Mason5f39d392007-10-15 16:14:19 -04001166 btrfs_set_header_nritems(src, src_nritems - push_items);
1167 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001168
Chris Mason5f39d392007-10-15 16:14:19 -04001169 btrfs_mark_buffer_dirty(src);
1170 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001171 return ret;
1172}
1173
1174/*
Chris Mason97571fd2007-02-24 13:39:08 -05001175 * helper function to insert a new root level in the tree.
1176 * A new node is allocated, and a single item is inserted to
1177 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001178 *
1179 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001180 */
Chris Mason5f39d392007-10-15 16:14:19 -04001181static int insert_new_root(struct btrfs_trans_handle *trans,
1182 struct btrfs_root *root,
1183 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001184{
Chris Mason5f39d392007-10-15 16:14:19 -04001185 struct extent_buffer *lower;
1186 struct extent_buffer *c;
1187 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001188
1189 BUG_ON(path->nodes[level]);
1190 BUG_ON(path->nodes[level-1] != root->node);
1191
Chris Masondb945352007-10-15 16:15:53 -04001192 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1193 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001194 if (IS_ERR(c))
1195 return PTR_ERR(c);
1196 memset_extent_buffer(c, 0, 0, root->nodesize);
1197 btrfs_set_header_nritems(c, 1);
1198 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001199 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001200 btrfs_set_header_generation(c, trans->transid);
1201 btrfs_set_header_owner(c, root->root_key.objectid);
1202 lower = path->nodes[level-1];
Chris Masond5719762007-03-23 10:01:08 -04001203
Chris Mason5f39d392007-10-15 16:14:19 -04001204 write_extent_buffer(c, root->fs_info->fsid,
1205 (unsigned long)btrfs_header_fsid(c),
1206 BTRFS_FSID_SIZE);
1207 if (level == 1)
1208 btrfs_item_key(lower, &lower_key, 0);
1209 else
1210 btrfs_node_key(lower, &lower_key, 0);
1211 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001212 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001213
1214 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001215
Chris Mason5c680ed2007-02-22 11:39:13 -05001216 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001217 free_extent_buffer(root->node);
1218 root->node = c;
1219 extent_buffer_get(c);
1220 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001221 path->slots[level] = 0;
1222 return 0;
1223}
1224
Chris Mason74123bd2007-02-02 11:05:29 -05001225/*
1226 * worker function to insert a single pointer in a node.
1227 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001228 *
Chris Mason74123bd2007-02-02 11:05:29 -05001229 * slot and level indicate where you want the key to go, and
1230 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001231 *
1232 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001233 */
Chris Masone089f052007-03-16 16:20:31 -04001234static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1235 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001236 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001237{
Chris Mason5f39d392007-10-15 16:14:19 -04001238 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001239 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001240
1241 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001242 lower = path->nodes[level];
1243 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001244 if (slot > nritems)
1245 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001246 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001247 BUG();
1248 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001249 memmove_extent_buffer(lower,
1250 btrfs_node_key_ptr_offset(slot + 1),
1251 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001252 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001253 }
Chris Mason5f39d392007-10-15 16:14:19 -04001254 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001255 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason5f39d392007-10-15 16:14:19 -04001256 btrfs_set_header_nritems(lower, nritems + 1);
1257 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001258 return 0;
1259}
1260
Chris Mason97571fd2007-02-24 13:39:08 -05001261/*
1262 * split the node at the specified level in path in two.
1263 * The path is corrected to point to the appropriate node after the split
1264 *
1265 * Before splitting this tries to make some room in the node by pushing
1266 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001267 *
1268 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001269 */
Chris Masone089f052007-03-16 16:20:31 -04001270static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1271 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001272{
Chris Mason5f39d392007-10-15 16:14:19 -04001273 struct extent_buffer *c;
1274 struct extent_buffer *split;
1275 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001276 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001277 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001278 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001279 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001280
Chris Mason5f39d392007-10-15 16:14:19 -04001281 c = path->nodes[level];
1282 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001283 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001284 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001285 if (ret)
1286 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001287 } else {
1288 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001289 c = path->nodes[level];
1290 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001291 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1292 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001293 if (ret < 0)
1294 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001295 }
Chris Masone66f7092007-04-20 13:16:02 -04001296
Chris Mason5f39d392007-10-15 16:14:19 -04001297 c_nritems = btrfs_header_nritems(c);
Chris Masondb945352007-10-15 16:15:53 -04001298 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1299 c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001300 if (IS_ERR(split))
1301 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001302
Chris Mason5f39d392007-10-15 16:14:19 -04001303 btrfs_set_header_flags(split, btrfs_header_flags(c));
1304 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001305 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001306 btrfs_set_header_generation(split, trans->transid);
1307 btrfs_set_header_owner(split, root->root_key.objectid);
1308 write_extent_buffer(split, root->fs_info->fsid,
1309 (unsigned long)btrfs_header_fsid(split),
1310 BTRFS_FSID_SIZE);
1311
Chris Mason7518a232007-03-12 12:01:18 -04001312 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001313
1314 copy_extent_buffer(split, c,
1315 btrfs_node_key_ptr_offset(0),
1316 btrfs_node_key_ptr_offset(mid),
1317 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1318 btrfs_set_header_nritems(split, c_nritems - mid);
1319 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001320 ret = 0;
1321
Chris Mason5f39d392007-10-15 16:14:19 -04001322 btrfs_mark_buffer_dirty(c);
1323 btrfs_mark_buffer_dirty(split);
1324
1325 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001326 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001327 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001328 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001329 if (wret)
1330 ret = wret;
1331
Chris Mason5de08d72007-02-24 06:24:44 -05001332 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001333 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001334 free_extent_buffer(c);
1335 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001336 path->slots[level + 1] += 1;
1337 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001338 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001339 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001340 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001341}
1342
Chris Mason74123bd2007-02-02 11:05:29 -05001343/*
1344 * how many bytes are required to store the items in a leaf. start
1345 * and nr indicate which items in the leaf to check. This totals up the
1346 * space used both by the item structs and the item data
1347 */
Chris Mason5f39d392007-10-15 16:14:19 -04001348static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001349{
1350 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001351 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001352 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001353
1354 if (!nr)
1355 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001356 data_len = btrfs_item_end_nr(l, start);
1357 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001358 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001359 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001360 return data_len;
1361}
1362
Chris Mason74123bd2007-02-02 11:05:29 -05001363/*
Chris Masond4dbff92007-04-04 14:08:15 -04001364 * The space between the end of the leaf items and
1365 * the start of the leaf data. IOW, how much room
1366 * the leaf has left for both items and data
1367 */
Chris Mason5f39d392007-10-15 16:14:19 -04001368int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001369{
Chris Mason5f39d392007-10-15 16:14:19 -04001370 int nritems = btrfs_header_nritems(leaf);
1371 int ret;
1372 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1373 if (ret < 0) {
1374 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001375 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001376 leaf_space_used(leaf, 0, nritems), nritems);
1377 }
1378 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001379}
1380
1381/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001382 * push some data in the path leaf to the right, trying to free up at
1383 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001384 *
1385 * returns 1 if the push failed because the other node didn't have enough
1386 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001387 */
Chris Masone089f052007-03-16 16:20:31 -04001388static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1389 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001390{
Chris Mason5f39d392007-10-15 16:14:19 -04001391 struct extent_buffer *left = path->nodes[0];
1392 struct extent_buffer *right;
1393 struct extent_buffer *upper;
1394 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001395 int slot;
1396 int i;
1397 int free_space;
1398 int push_space = 0;
1399 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001400 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001401 u32 left_nritems;
1402 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001403 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001404 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001405 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001406
1407 slot = path->slots[1];
1408 if (!path->nodes[1]) {
1409 return 1;
1410 }
1411 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001412 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001413 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001414
Chris Masondb945352007-10-15 16:15:53 -04001415 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1416 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001417 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001418 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001419 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001420 return 1;
1421 }
1422
Chris Mason5f39d392007-10-15 16:14:19 -04001423 /* cow and double check */
1424 ret = btrfs_cow_block(trans, root, right, upper,
1425 slot + 1, &right);
1426 if (ret) {
1427 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001428 return 1;
1429 }
Chris Mason5f39d392007-10-15 16:14:19 -04001430 free_space = btrfs_leaf_free_space(root, right);
1431 if (free_space < data_size + sizeof(struct btrfs_item)) {
1432 free_extent_buffer(right);
1433 return 1;
1434 }
1435
1436 left_nritems = btrfs_header_nritems(left);
1437 if (left_nritems == 0) {
1438 free_extent_buffer(right);
1439 return 1;
1440 }
1441
Chris Masona429e512007-04-18 16:15:28 -04001442 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason5f39d392007-10-15 16:14:19 -04001443 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001444
Chris Mason00ec4c52007-02-24 12:47:20 -05001445 if (path->slots[0] == i)
1446 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001447
1448 if (!left->map_token) {
1449 map_extent_buffer(left, (unsigned long)item,
1450 sizeof(struct btrfs_item),
1451 &left->map_token, &left->kaddr,
1452 &left->map_start, &left->map_len,
1453 KM_USER1);
1454 }
1455
1456 this_item_size = btrfs_item_size(left, item);
1457 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001458 break;
1459 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001460 push_space += this_item_size + sizeof(*item);
1461 }
1462 if (left->map_token) {
1463 unmap_extent_buffer(left, left->map_token, KM_USER1);
1464 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001465 }
Chris Mason5f39d392007-10-15 16:14:19 -04001466
Chris Mason00ec4c52007-02-24 12:47:20 -05001467 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001468 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001469 return 1;
1470 }
Chris Mason5f39d392007-10-15 16:14:19 -04001471
Chris Masona429e512007-04-18 16:15:28 -04001472 if (push_items == left_nritems)
1473 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001474
Chris Mason00ec4c52007-02-24 12:47:20 -05001475 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001476 right_nritems = btrfs_header_nritems(right);
1477 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001478 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001479
Chris Mason00ec4c52007-02-24 12:47:20 -05001480 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001481 data_end = leaf_data_end(root, right);
1482 memmove_extent_buffer(right,
1483 btrfs_leaf_data(right) + data_end - push_space,
1484 btrfs_leaf_data(right) + data_end,
1485 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1486
Chris Mason00ec4c52007-02-24 12:47:20 -05001487 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001488 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001489 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1490 btrfs_leaf_data(left) + leaf_data_end(root, left),
1491 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001492
1493 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1494 btrfs_item_nr_offset(0),
1495 right_nritems * sizeof(struct btrfs_item));
1496
Chris Mason00ec4c52007-02-24 12:47:20 -05001497 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001498 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1499 btrfs_item_nr_offset(left_nritems - push_items),
1500 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001501
1502 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001503 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001504 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001505 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masondb945352007-10-15 16:15:53 -04001506
Chris Mason7518a232007-03-12 12:01:18 -04001507 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001508 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001509 if (!right->map_token) {
1510 map_extent_buffer(right, (unsigned long)item,
1511 sizeof(struct btrfs_item),
1512 &right->map_token, &right->kaddr,
1513 &right->map_start, &right->map_len,
1514 KM_USER1);
1515 }
1516 push_space -= btrfs_item_size(right, item);
1517 btrfs_set_item_offset(right, item, push_space);
1518 }
1519
1520 if (right->map_token) {
1521 unmap_extent_buffer(right, right->map_token, KM_USER1);
1522 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001523 }
Chris Mason7518a232007-03-12 12:01:18 -04001524 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001525 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001526
Chris Mason5f39d392007-10-15 16:14:19 -04001527 btrfs_mark_buffer_dirty(left);
1528 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001529
Chris Mason5f39d392007-10-15 16:14:19 -04001530 btrfs_item_key(right, &disk_key, 0);
1531 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001532 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001533
Chris Mason00ec4c52007-02-24 12:47:20 -05001534 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001535 if (path->slots[0] >= left_nritems) {
1536 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001537 free_extent_buffer(path->nodes[0]);
1538 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001539 path->slots[1] += 1;
1540 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001541 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001542 }
1543 return 0;
1544}
1545/*
Chris Mason74123bd2007-02-02 11:05:29 -05001546 * push some data in the path leaf to the left, trying to free up at
1547 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1548 */
Chris Masone089f052007-03-16 16:20:31 -04001549static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1550 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001551{
Chris Mason5f39d392007-10-15 16:14:19 -04001552 struct btrfs_disk_key disk_key;
1553 struct extent_buffer *right = path->nodes[0];
1554 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001555 int slot;
1556 int i;
1557 int free_space;
1558 int push_space = 0;
1559 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001560 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001561 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001562 u32 right_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001563 int ret = 0;
1564 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001565 u32 this_item_size;
1566 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001567
1568 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001569 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001570 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001571 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001572 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001573
Chris Mason3685f792007-10-19 09:23:27 -04001574 right_nritems = btrfs_header_nritems(right);
1575 if (right_nritems == 0) {
1576 return 1;
1577 }
1578
Chris Mason5f39d392007-10-15 16:14:19 -04001579 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001580 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001581 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001582 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001583 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001584 return 1;
1585 }
Chris Mason02217ed2007-03-02 16:08:05 -05001586
1587 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001588 ret = btrfs_cow_block(trans, root, left,
1589 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001590 if (ret) {
1591 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001592 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001593 return 1;
1594 }
Chris Mason3685f792007-10-19 09:23:27 -04001595
Chris Mason123abc82007-03-14 14:14:43 -04001596 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001597 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001598 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001599 return 1;
1600 }
1601
Chris Mason5f39d392007-10-15 16:14:19 -04001602 for (i = 0; i < right_nritems - 1; i++) {
1603 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001604 if (!right->map_token) {
1605 map_extent_buffer(right, (unsigned long)item,
1606 sizeof(struct btrfs_item),
1607 &right->map_token, &right->kaddr,
1608 &right->map_start, &right->map_len,
1609 KM_USER1);
1610 }
1611
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612 if (path->slots[0] == i)
1613 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001614
1615 this_item_size = btrfs_item_size(right, item);
1616 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001617 break;
Chris Masondb945352007-10-15 16:15:53 -04001618
Chris Masonbe0e5c02007-01-26 15:51:26 -05001619 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001620 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001621 }
Chris Masondb945352007-10-15 16:15:53 -04001622
1623 if (right->map_token) {
1624 unmap_extent_buffer(right, right->map_token, KM_USER1);
1625 right->map_token = NULL;
1626 }
1627
Chris Masonbe0e5c02007-01-26 15:51:26 -05001628 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001629 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001630 return 1;
1631 }
Chris Mason5f39d392007-10-15 16:14:19 -04001632 if (push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001633 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001634
Chris Masonbe0e5c02007-01-26 15:51:26 -05001635 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001636 copy_extent_buffer(left, right,
1637 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1638 btrfs_item_nr_offset(0),
1639 push_items * sizeof(struct btrfs_item));
1640
Chris Mason123abc82007-03-14 14:14:43 -04001641 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001642 btrfs_item_offset_nr(right, push_items -1);
1643
1644 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001645 leaf_data_end(root, left) - push_space,
1646 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001647 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001648 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001649 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001650 BUG_ON(old_left_nritems < 0);
1651
Chris Masondb945352007-10-15 16:15:53 -04001652 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001653 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001654 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001655
Chris Mason5f39d392007-10-15 16:14:19 -04001656 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001657 if (!left->map_token) {
1658 map_extent_buffer(left, (unsigned long)item,
1659 sizeof(struct btrfs_item),
1660 &left->map_token, &left->kaddr,
1661 &left->map_start, &left->map_len,
1662 KM_USER1);
1663 }
1664
Chris Mason5f39d392007-10-15 16:14:19 -04001665 ioff = btrfs_item_offset(left, item);
1666 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001667 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001668 }
Chris Mason5f39d392007-10-15 16:14:19 -04001669 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001670 if (left->map_token) {
1671 unmap_extent_buffer(left, left->map_token, KM_USER1);
1672 left->map_token = NULL;
1673 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001674
1675 /* fixup right node */
Chris Mason5f39d392007-10-15 16:14:19 -04001676 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1677 leaf_data_end(root, right);
1678 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1679 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1680 btrfs_leaf_data(right) +
1681 leaf_data_end(root, right), push_space);
1682
1683 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1684 btrfs_item_nr_offset(push_items),
1685 (btrfs_header_nritems(right) - push_items) *
1686 sizeof(struct btrfs_item));
1687
1688 right_nritems = btrfs_header_nritems(right) - push_items;
1689 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001690 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001691
Chris Mason5f39d392007-10-15 16:14:19 -04001692 for (i = 0; i < right_nritems; i++) {
1693 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001694
1695 if (!right->map_token) {
1696 map_extent_buffer(right, (unsigned long)item,
1697 sizeof(struct btrfs_item),
1698 &right->map_token, &right->kaddr,
1699 &right->map_start, &right->map_len,
1700 KM_USER1);
1701 }
1702
1703 push_space = push_space - btrfs_item_size(right, item);
1704 btrfs_set_item_offset(right, item, push_space);
1705 }
1706 if (right->map_token) {
1707 unmap_extent_buffer(right, right->map_token, KM_USER1);
1708 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001709 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001710
Chris Mason5f39d392007-10-15 16:14:19 -04001711 btrfs_mark_buffer_dirty(left);
1712 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001713
Chris Mason5f39d392007-10-15 16:14:19 -04001714 btrfs_item_key(right, &disk_key, 0);
1715 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001716 if (wret)
1717 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001718
1719 /* then fixup the leaf pointer in the path */
1720 if (path->slots[0] < push_items) {
1721 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001722 free_extent_buffer(path->nodes[0]);
1723 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001724 path->slots[1] -= 1;
1725 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001726 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001727 path->slots[0] -= push_items;
1728 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001729 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001731}
1732
Chris Mason74123bd2007-02-02 11:05:29 -05001733/*
1734 * split the path's leaf in two, making sure there is at least data_size
1735 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001736 *
1737 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001738 */
Chris Masone089f052007-03-16 16:20:31 -04001739static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001740 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001741 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001742{
Chris Mason5f39d392007-10-15 16:14:19 -04001743 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001744 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001745 int mid;
1746 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001747 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001748 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749 int data_copy_size;
1750 int rt_data_off;
1751 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001752 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001753 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001754 int double_split;
1755 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001756 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757
Chris Masoncc0c5532007-10-25 15:42:57 -04001758 if (extend)
1759 space_needed = data_size;
1760
Chris Mason40689472007-03-17 14:29:23 -04001761 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001762 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Masone089f052007-03-16 16:20:31 -04001763 wret = push_leaf_right(trans, root, path, data_size);
Chris Mason3685f792007-10-19 09:23:27 -04001764 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001765 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001766 }
1767 if (wret) {
1768 wret = push_leaf_left(trans, root, path, data_size);
1769 if (wret < 0)
1770 return wret;
1771 }
1772 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001773
Chris Mason3685f792007-10-19 09:23:27 -04001774 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04001775 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04001776 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001777 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001778
Chris Mason5c680ed2007-02-22 11:39:13 -05001779 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001780 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001781 if (ret)
1782 return ret;
1783 }
Chris Masoncc0c5532007-10-25 15:42:57 -04001784again:
1785 double_split = 0;
1786 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001787 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001788 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001789 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001790
Chris Masondb945352007-10-15 16:15:53 -04001791 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1792 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001793 if (IS_ERR(right))
1794 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04001797 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001798 btrfs_set_header_generation(right, trans->transid);
1799 btrfs_set_header_owner(right, root->root_key.objectid);
1800 btrfs_set_header_level(right, 0);
1801 write_extent_buffer(right, root->fs_info->fsid,
1802 (unsigned long)btrfs_header_fsid(right),
1803 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04001804 if (mid <= slot) {
1805 if (nritems == 1 ||
1806 leaf_space_used(l, mid, nritems - mid) + space_needed >
1807 BTRFS_LEAF_DATA_SIZE(root)) {
1808 if (slot >= nritems) {
1809 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001810 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001811 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04001812 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04001813 path->slots[1] + 1, 1);
1814 if (wret)
1815 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001816 free_extent_buffer(path->nodes[0]);
1817 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001818 path->slots[0] = 0;
1819 path->slots[1] += 1;
1820 return ret;
1821 }
1822 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04001823 if (mid != nritems &&
1824 leaf_space_used(l, mid, nritems - mid) +
1825 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1826 double_split = 1;
1827 }
Chris Masond4dbff92007-04-04 14:08:15 -04001828 }
1829 } else {
1830 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1831 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04001832 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04001833 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001834 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001835 wret = insert_ptr(trans, root, path,
1836 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04001837 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04001838 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001839 if (wret)
1840 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001841 free_extent_buffer(path->nodes[0]);
1842 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001843 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001844 if (path->slots[1] == 0) {
1845 wret = fixup_low_keys(trans, root,
1846 path, &disk_key, 1);
1847 if (wret)
1848 ret = wret;
1849 }
Chris Masond4dbff92007-04-04 14:08:15 -04001850 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001851 } else if (extend && slot == 0) {
1852 mid = 1;
1853 } else {
1854 mid = slot;
1855 if (mid != nritems &&
1856 leaf_space_used(l, mid, nritems - mid) +
1857 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1858 double_split = 1;
1859 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04001860 }
Chris Masond4dbff92007-04-04 14:08:15 -04001861 }
1862 }
Chris Mason5f39d392007-10-15 16:14:19 -04001863 nritems = nritems - mid;
1864 btrfs_set_header_nritems(right, nritems);
1865 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1866
1867 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1868 btrfs_item_nr_offset(mid),
1869 nritems * sizeof(struct btrfs_item));
1870
1871 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04001872 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1873 data_copy_size, btrfs_leaf_data(l) +
1874 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05001875
Chris Mason5f39d392007-10-15 16:14:19 -04001876 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1877 btrfs_item_end_nr(l, mid);
1878
1879 for (i = 0; i < nritems; i++) {
1880 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001881 u32 ioff;
1882
1883 if (!right->map_token) {
1884 map_extent_buffer(right, (unsigned long)item,
1885 sizeof(struct btrfs_item),
1886 &right->map_token, &right->kaddr,
1887 &right->map_start, &right->map_len,
1888 KM_USER1);
1889 }
1890
1891 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04001892 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04001893 }
Chris Mason74123bd2007-02-02 11:05:29 -05001894
Chris Masondb945352007-10-15 16:15:53 -04001895 if (right->map_token) {
1896 unmap_extent_buffer(right, right->map_token, KM_USER1);
1897 right->map_token = NULL;
1898 }
1899
Chris Mason5f39d392007-10-15 16:14:19 -04001900 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001901 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001903 wret = insert_ptr(trans, root, path, &disk_key, right->start,
1904 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001905 if (wret)
1906 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001907
1908 btrfs_mark_buffer_dirty(right);
1909 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001910 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04001911
Chris Masonbe0e5c02007-01-26 15:51:26 -05001912 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04001913 free_extent_buffer(path->nodes[0]);
1914 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001915 path->slots[0] -= mid;
1916 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001917 } else
Chris Mason5f39d392007-10-15 16:14:19 -04001918 free_extent_buffer(right);
1919
Chris Masoneb60cea2007-02-02 09:18:22 -05001920 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001921
Chris Masoncc0c5532007-10-25 15:42:57 -04001922 if (double_split) {
1923 BUG_ON(num_doubles != 0);
1924 num_doubles++;
1925 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04001926 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001927 return ret;
1928}
1929
Chris Masonb18c6682007-04-17 13:26:50 -04001930int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1931 struct btrfs_root *root,
1932 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04001933 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04001934{
1935 int ret = 0;
1936 int slot;
1937 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04001938 struct extent_buffer *leaf;
1939 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04001940 u32 nritems;
1941 unsigned int data_end;
1942 unsigned int old_data_start;
1943 unsigned int old_size;
1944 unsigned int size_diff;
1945 int i;
1946
1947 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001948 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04001949 slot = path->slots[0];
1950
1951 old_size = btrfs_item_size_nr(leaf, slot);
1952 if (old_size == new_size)
1953 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001954
Chris Mason5f39d392007-10-15 16:14:19 -04001955 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04001956 data_end = leaf_data_end(root, leaf);
1957
Chris Mason5f39d392007-10-15 16:14:19 -04001958 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04001959
Chris Masonb18c6682007-04-17 13:26:50 -04001960 size_diff = old_size - new_size;
1961
1962 BUG_ON(slot < 0);
1963 BUG_ON(slot >= nritems);
1964
1965 /*
1966 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1967 */
1968 /* first correct the data pointers */
1969 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001970 u32 ioff;
1971 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04001972
1973 if (!leaf->map_token) {
1974 map_extent_buffer(leaf, (unsigned long)item,
1975 sizeof(struct btrfs_item),
1976 &leaf->map_token, &leaf->kaddr,
1977 &leaf->map_start, &leaf->map_len,
1978 KM_USER1);
1979 }
1980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 ioff = btrfs_item_offset(leaf, item);
1982 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04001983 }
Chris Masondb945352007-10-15 16:15:53 -04001984
1985 if (leaf->map_token) {
1986 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
1987 leaf->map_token = NULL;
1988 }
1989
Chris Masonb18c6682007-04-17 13:26:50 -04001990 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04001991 if (from_end) {
1992 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
1993 data_end + size_diff, btrfs_leaf_data(leaf) +
1994 data_end, old_data_start + new_size - data_end);
1995 } else {
1996 struct btrfs_disk_key disk_key;
1997 u64 offset;
1998
1999 btrfs_item_key(leaf, &disk_key, slot);
2000
2001 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2002 unsigned long ptr;
2003 struct btrfs_file_extent_item *fi;
2004
2005 fi = btrfs_item_ptr(leaf, slot,
2006 struct btrfs_file_extent_item);
2007 fi = (struct btrfs_file_extent_item *)(
2008 (unsigned long)fi - size_diff);
2009
2010 if (btrfs_file_extent_type(leaf, fi) ==
2011 BTRFS_FILE_EXTENT_INLINE) {
2012 ptr = btrfs_item_ptr_offset(leaf, slot);
2013 memmove_extent_buffer(leaf, ptr,
2014 (unsigned long)fi,
2015 offsetof(struct btrfs_file_extent_item,
2016 disk_bytenr));
2017 }
2018 }
2019
2020 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2021 data_end + size_diff, btrfs_leaf_data(leaf) +
2022 data_end, old_data_start - data_end);
2023
2024 offset = btrfs_disk_key_offset(&disk_key);
2025 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2026 btrfs_set_item_key(leaf, &disk_key, slot);
2027 if (slot == 0)
2028 fixup_low_keys(trans, root, path, &disk_key, 1);
2029 }
Chris Mason5f39d392007-10-15 16:14:19 -04002030
2031 item = btrfs_item_nr(leaf, slot);
2032 btrfs_set_item_size(leaf, item, new_size);
2033 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002034
2035 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002036 if (btrfs_leaf_free_space(root, leaf) < 0) {
2037 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002038 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002039 }
Chris Masonb18c6682007-04-17 13:26:50 -04002040 return ret;
2041}
2042
Chris Mason5f39d392007-10-15 16:14:19 -04002043int btrfs_extend_item(struct btrfs_trans_handle *trans,
2044 struct btrfs_root *root, struct btrfs_path *path,
2045 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002046{
2047 int ret = 0;
2048 int slot;
2049 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002050 struct extent_buffer *leaf;
2051 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002052 u32 nritems;
2053 unsigned int data_end;
2054 unsigned int old_data;
2055 unsigned int old_size;
2056 int i;
2057
2058 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002059 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002060
Chris Mason5f39d392007-10-15 16:14:19 -04002061 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002062 data_end = leaf_data_end(root, leaf);
2063
Chris Mason5f39d392007-10-15 16:14:19 -04002064 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2065 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002066 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002067 }
Chris Mason6567e832007-04-16 09:22:45 -04002068 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002069 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002070
2071 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002072 if (slot >= nritems) {
2073 btrfs_print_leaf(root, leaf);
2074 printk("slot %d too large, nritems %d\n", slot, nritems);
2075 BUG_ON(1);
2076 }
Chris Mason6567e832007-04-16 09:22:45 -04002077
2078 /*
2079 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2080 */
2081 /* first correct the data pointers */
2082 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002083 u32 ioff;
2084 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002085
2086 if (!leaf->map_token) {
2087 map_extent_buffer(leaf, (unsigned long)item,
2088 sizeof(struct btrfs_item),
2089 &leaf->map_token, &leaf->kaddr,
2090 &leaf->map_start, &leaf->map_len,
2091 KM_USER1);
2092 }
Chris Mason5f39d392007-10-15 16:14:19 -04002093 ioff = btrfs_item_offset(leaf, item);
2094 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002095 }
Chris Mason5f39d392007-10-15 16:14:19 -04002096
Chris Masondb945352007-10-15 16:15:53 -04002097 if (leaf->map_token) {
2098 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2099 leaf->map_token = NULL;
2100 }
2101
Chris Mason6567e832007-04-16 09:22:45 -04002102 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002103 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002104 data_end - data_size, btrfs_leaf_data(leaf) +
2105 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002106
Chris Mason6567e832007-04-16 09:22:45 -04002107 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002108 old_size = btrfs_item_size_nr(leaf, slot);
2109 item = btrfs_item_nr(leaf, slot);
2110 btrfs_set_item_size(leaf, item, old_size + data_size);
2111 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002112
2113 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002114 if (btrfs_leaf_free_space(root, leaf) < 0) {
2115 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002116 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002117 }
Chris Mason6567e832007-04-16 09:22:45 -04002118 return ret;
2119}
2120
Chris Mason74123bd2007-02-02 11:05:29 -05002121/*
2122 * Given a key and some data, insert an item into the tree.
2123 * This does all the path init required, making room in the tree if needed.
2124 */
Chris Mason5f39d392007-10-15 16:14:19 -04002125int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2126 struct btrfs_root *root,
2127 struct btrfs_path *path,
2128 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002129{
Chris Mason5f39d392007-10-15 16:14:19 -04002130 struct extent_buffer *leaf;
2131 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002132 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002133 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002134 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002135 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002136 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002137 struct btrfs_disk_key disk_key;
2138
2139 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002140
Chris Mason74123bd2007-02-02 11:05:29 -05002141 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002142 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002143 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002144
Chris Masone089f052007-03-16 16:20:31 -04002145 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002146 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002147 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002148 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002149 if (ret < 0)
2150 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002151
Chris Mason62e27492007-03-15 12:56:47 -04002152 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002153 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002154
Chris Mason5f39d392007-10-15 16:14:19 -04002155 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002156 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002157
Chris Mason123abc82007-03-14 14:14:43 -04002158 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002159 sizeof(struct btrfs_item) + data_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002160 btrfs_print_leaf(root, leaf);
2161 printk("not enough freespace need %u have %d\n",
2162 data_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002163 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002164 }
Chris Mason5f39d392007-10-15 16:14:19 -04002165
Chris Mason62e27492007-03-15 12:56:47 -04002166 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002167 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002168
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 if (slot != nritems) {
2170 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002171 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002172
Chris Mason5f39d392007-10-15 16:14:19 -04002173 if (old_data < data_end) {
2174 btrfs_print_leaf(root, leaf);
2175 printk("slot %d old_data %d data_end %d\n",
2176 slot, old_data, data_end);
2177 BUG_ON(1);
2178 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002179 /*
2180 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2181 */
2182 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002183 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002184 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002185 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002186
Chris Mason5f39d392007-10-15 16:14:19 -04002187 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002188 if (!leaf->map_token) {
2189 map_extent_buffer(leaf, (unsigned long)item,
2190 sizeof(struct btrfs_item),
2191 &leaf->map_token, &leaf->kaddr,
2192 &leaf->map_start, &leaf->map_len,
2193 KM_USER1);
2194 }
2195
Chris Mason5f39d392007-10-15 16:14:19 -04002196 ioff = btrfs_item_offset(leaf, item);
2197 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002198 }
Chris Masondb945352007-10-15 16:15:53 -04002199 if (leaf->map_token) {
2200 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2201 leaf->map_token = NULL;
2202 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002203
2204 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002205 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2206 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002207 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208
2209 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002210 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002211 data_end - data_size, btrfs_leaf_data(leaf) +
2212 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002213 data_end = old_data;
2214 }
Chris Mason5f39d392007-10-15 16:14:19 -04002215
Chris Mason62e27492007-03-15 12:56:47 -04002216 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002217 btrfs_set_item_key(leaf, &disk_key, slot);
2218 item = btrfs_item_nr(leaf, slot);
2219 btrfs_set_item_offset(leaf, item, data_end - data_size);
2220 btrfs_set_item_size(leaf, item, data_size);
2221 btrfs_set_header_nritems(leaf, nritems + 1);
2222 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002223
2224 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002225 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002226 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002227
Chris Mason5f39d392007-10-15 16:14:19 -04002228 if (btrfs_leaf_free_space(root, leaf) < 0) {
2229 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002230 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002231 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002232out:
Chris Mason62e27492007-03-15 12:56:47 -04002233 return ret;
2234}
2235
2236/*
2237 * Given a key and some data, insert an item into the tree.
2238 * This does all the path init required, making room in the tree if needed.
2239 */
Chris Masone089f052007-03-16 16:20:31 -04002240int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2241 *root, struct btrfs_key *cpu_key, void *data, u32
2242 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002243{
2244 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002245 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002246 struct extent_buffer *leaf;
2247 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002248
Chris Mason2c90e5d2007-04-02 10:50:19 -04002249 path = btrfs_alloc_path();
2250 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002251 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002252 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002253 leaf = path->nodes[0];
2254 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2255 write_extent_buffer(leaf, data, ptr, data_size);
2256 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002257 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002258 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002259 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002260}
2261
Chris Mason74123bd2007-02-02 11:05:29 -05002262/*
Chris Mason5de08d72007-02-24 06:24:44 -05002263 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002264 *
2265 * If the delete empties a node, the node is removed from the tree,
2266 * continuing all the way the root if required. The root is converted into
2267 * a leaf if all the nodes are emptied.
2268 */
Chris Masone089f052007-03-16 16:20:31 -04002269static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2270 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002271{
Chris Mason5f39d392007-10-15 16:14:19 -04002272 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002273 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002274 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002275 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002276
Chris Mason5f39d392007-10-15 16:14:19 -04002277 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002278 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002279 memmove_extent_buffer(parent,
2280 btrfs_node_key_ptr_offset(slot),
2281 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002282 sizeof(struct btrfs_key_ptr) *
2283 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002284 }
Chris Mason7518a232007-03-12 12:01:18 -04002285 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002286 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002287 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002288 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002289 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002290 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002291 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002292 struct btrfs_disk_key disk_key;
2293
2294 btrfs_node_key(parent, &disk_key, 0);
2295 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002296 if (wret)
2297 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298 }
Chris Masond6025572007-03-30 14:27:56 -04002299 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002300 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002301}
2302
Chris Mason74123bd2007-02-02 11:05:29 -05002303/*
2304 * delete the item at the leaf level in path. If that empties
2305 * the leaf, remove it from the tree
2306 */
Chris Masone089f052007-03-16 16:20:31 -04002307int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2308 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002309{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002310 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002311 struct extent_buffer *leaf;
2312 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002313 int doff;
2314 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002315 int ret = 0;
2316 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002317 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002318
Chris Mason5f39d392007-10-15 16:14:19 -04002319 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002320 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002321 doff = btrfs_item_offset_nr(leaf, slot);
2322 dsize = btrfs_item_size_nr(leaf, slot);
2323 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002324
Chris Mason7518a232007-03-12 12:01:18 -04002325 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002326 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002327 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002328
2329 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002330 data_end + dsize,
2331 btrfs_leaf_data(leaf) + data_end,
2332 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002333
Chris Mason0783fcf2007-03-12 20:12:07 -04002334 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002335 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002336
Chris Mason5f39d392007-10-15 16:14:19 -04002337 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002338 if (!leaf->map_token) {
2339 map_extent_buffer(leaf, (unsigned long)item,
2340 sizeof(struct btrfs_item),
2341 &leaf->map_token, &leaf->kaddr,
2342 &leaf->map_start, &leaf->map_len,
2343 KM_USER1);
2344 }
Chris Mason5f39d392007-10-15 16:14:19 -04002345 ioff = btrfs_item_offset(leaf, item);
2346 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002347 }
Chris Masondb945352007-10-15 16:15:53 -04002348
2349 if (leaf->map_token) {
2350 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2351 leaf->map_token = NULL;
2352 }
2353
Chris Mason5f39d392007-10-15 16:14:19 -04002354 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2355 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002356 sizeof(struct btrfs_item) *
2357 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002358 }
Chris Mason5f39d392007-10-15 16:14:19 -04002359 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002360 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002361
Chris Mason74123bd2007-02-02 11:05:29 -05002362 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002363 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002364 if (leaf == root->node) {
2365 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002366 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002367 clean_tree_block(trans, root, leaf);
2368 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002369 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002370 if (wret)
2371 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002372 wret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002373 leaf->start, leaf->len, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002374 if (wret)
2375 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002376 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002377 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002378 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002379 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002380 struct btrfs_disk_key disk_key;
2381
2382 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002383 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002384 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002385 if (wret)
2386 ret = wret;
2387 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002388
Chris Mason74123bd2007-02-02 11:05:29 -05002389 /* delete the leaf if it is mostly empty */
Chris Mason7936ca32007-10-19 09:22:41 -04002390 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002391 /* push_leaf_left fixes the path.
2392 * make sure the path still points to our leaf
2393 * for possible call to del_ptr below
2394 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002395 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002396 extent_buffer_get(leaf);
2397
Chris Mason3685f792007-10-19 09:23:27 -04002398 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002399 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002400 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002401
2402 if (path->nodes[0] == leaf &&
2403 btrfs_header_nritems(leaf)) {
Chris Mason3685f792007-10-19 09:23:27 -04002404 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002405 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002406 ret = wret;
2407 }
Chris Mason5f39d392007-10-15 16:14:19 -04002408
2409 if (btrfs_header_nritems(leaf) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002410 u64 bytenr = leaf->start;
2411 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002412
2413 clean_tree_block(trans, root, leaf);
2414 wait_on_tree_block_writeback(root, leaf);
2415
Chris Masone089f052007-03-16 16:20:31 -04002416 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002417 if (wret)
2418 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002419
2420 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002421 wret = btrfs_free_extent(trans, root, bytenr,
2422 blocksize, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002423 if (wret)
2424 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002425 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002426 btrfs_mark_buffer_dirty(leaf);
2427 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428 }
Chris Masond5719762007-03-23 10:01:08 -04002429 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002430 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002431 }
2432 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002433 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002434}
2435
Chris Mason97571fd2007-02-24 13:39:08 -05002436/*
2437 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002438 * returns 0 if it found something or 1 if there are no greater leaves.
2439 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002440 */
Chris Mason234b63a2007-03-13 10:46:10 -04002441int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002442{
2443 int slot;
2444 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002445 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002446 struct extent_buffer *c;
2447 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002448
Chris Mason234b63a2007-03-13 10:46:10 -04002449 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002450 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002451 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002452
Chris Masond97e63b2007-02-20 16:40:44 -05002453 slot = path->slots[level] + 1;
2454 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002455 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002456 level++;
2457 continue;
2458 }
Chris Mason5f39d392007-10-15 16:14:19 -04002459
Chris Masondb945352007-10-15 16:15:53 -04002460 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002461 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002462 free_extent_buffer(next);
2463
Chris Mason6702ed42007-08-07 16:15:09 -04002464 if (path->reada)
2465 reada_for_search(root, path, level, slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002466
Chris Masondb945352007-10-15 16:15:53 -04002467 next = read_tree_block(root, bytenr,
2468 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002469 break;
2470 }
2471 path->slots[level] = slot;
2472 while(1) {
2473 level--;
2474 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002475 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002476 path->nodes[level] = next;
2477 path->slots[level] = 0;
2478 if (!level)
2479 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002480 if (path->reada)
Yan32020612007-08-27 15:17:54 -04002481 reada_for_search(root, path, level, 0);
Chris Masondb945352007-10-15 16:15:53 -04002482 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2483 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002484 }
2485 return 0;
2486}