blob: 606a19b5916d3e2a8e9bfc2455fa84a7b1eb18f6 [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 Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.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 Mason9a8dd152007-02-23 08:38:36 -050023
Chris Masone089f052007-03-16 16:20:31 -040024static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
25 *root, struct btrfs_path *path, int level);
26static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040027 *root, struct btrfs_key *ins_key,
28 struct btrfs_path *path, int data_size);
Chris Masone089f052007-03-16 16:20:31 -040029static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040030 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040031 *src);
32static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040033 btrfs_root *root, struct buffer_head *dst_buf,
34 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040035static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
36 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050037
Chris Masondf24a2b2007-04-04 09:36:31 -040038inline void btrfs_init_path(struct btrfs_path *p)
39{
40 memset(p, 0, sizeof(*p));
41}
42
Chris Mason2c90e5d2007-04-02 10:50:19 -040043struct btrfs_path *btrfs_alloc_path(void)
44{
Chris Masondf24a2b2007-04-04 09:36:31 -040045 struct btrfs_path *path;
46 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
47 if (path)
48 btrfs_init_path(path);
49 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040050}
51
52void btrfs_free_path(struct btrfs_path *p)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040055 kmem_cache_free(btrfs_path_cachep, p);
56}
57
Chris Mason234b63a2007-03-13 10:46:10 -040058void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050059{
60 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040061 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050062 if (!p->nodes[i])
63 break;
Chris Mason234b63a2007-03-13 10:46:10 -040064 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050065 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050066 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050067}
68
Chris Masone089f052007-03-16 16:20:31 -040069static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040070 *root, struct buffer_head *buf, struct buffer_head
71 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040072 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050073{
Chris Masone20d96d2007-03-22 12:13:20 -040074 struct buffer_head *cow;
75 struct btrfs_node *cow_node;
Chris Mason54aa1f42007-06-22 14:16:25 -040076 int ret;
Chris Mason02217ed2007-03-02 16:08:05 -050077
Chris Mason7f5c1512007-03-23 15:56:19 -040078 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
79 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -050080 *cow_ret = buf;
81 return 0;
82 }
Chris Mason31f3c992007-04-30 15:25:45 -040083 cow = btrfs_alloc_free_block(trans, root, buf->b_blocknr);
Chris Mason54aa1f42007-06-22 14:16:25 -040084 if (IS_ERR(cow))
85 return PTR_ERR(cow);
Chris Masone20d96d2007-03-22 12:13:20 -040086 cow_node = btrfs_buffer_node(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040087 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
88 WARN_ON(1);
Chris Masone20d96d2007-03-22 12:13:20 -040089 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
Chris Mason7eccb902007-04-11 15:53:25 -040090 btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
Chris Mason7f5c1512007-03-23 15:56:19 -040091 btrfs_set_header_generation(&cow_node->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -040092 btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -040093 ret = btrfs_inc_ref(trans, root, buf);
94 if (ret)
95 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -050096 if (buf == root->node) {
97 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040098 get_bh(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040099 if (buf != root->commit_root) {
Chris Mason7eccb902007-04-11 15:53:25 -0400100 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400101 }
Chris Mason234b63a2007-03-13 10:46:10 -0400102 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500103 } else {
Chris Masone20d96d2007-03-22 12:13:20 -0400104 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
Chris Mason7eccb902007-04-11 15:53:25 -0400105 bh_blocknr(cow));
Chris Masond6025572007-03-30 14:27:56 -0400106 btrfs_mark_buffer_dirty(parent);
Chris Mason7eccb902007-04-11 15:53:25 -0400107 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -0500108 }
Chris Mason234b63a2007-03-13 10:46:10 -0400109 btrfs_block_release(root, buf);
Chris Masondf24a2b2007-04-04 09:36:31 -0400110 mark_buffer_dirty(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400111 *cow_ret = cow;
Chris Mason02217ed2007-03-02 16:08:05 -0500112 return 0;
113}
114
Chris Mason74123bd2007-02-02 11:05:29 -0500115/*
116 * The leaf data grows from end-to-front in the node.
117 * this returns the address of the start of the last item,
118 * which is the stop of the leaf data stack
119 */
Chris Mason123abc82007-03-14 14:14:43 -0400120static inline unsigned int leaf_data_end(struct btrfs_root *root,
121 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500122{
Chris Mason7518a232007-03-12 12:01:18 -0400123 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500124 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400125 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400126 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500127}
128
Chris Mason74123bd2007-02-02 11:05:29 -0500129/*
Chris Mason74123bd2007-02-02 11:05:29 -0500130 * compare two keys in a memcmp fashion
131 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400132static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500133{
Chris Masone2fa7222007-03-12 16:22:34 -0400134 struct btrfs_key k1;
135
136 btrfs_disk_key_to_cpu(&k1, disk);
137
138 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500139 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400140 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500141 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400142 if (k1.flags > k2->flags)
143 return 1;
144 if (k1.flags < k2->flags)
145 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400146 if (k1.offset > k2->offset)
147 return 1;
148 if (k1.offset < k2->offset)
149 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500150 return 0;
151}
Chris Mason74123bd2007-02-02 11:05:29 -0500152
Chris Mason123abc82007-03-14 14:14:43 -0400153static int check_node(struct btrfs_root *root, struct btrfs_path *path,
154 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155{
Chris Mason234b63a2007-03-13 10:46:10 -0400156 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400157 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500158 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400159 int slot;
160 struct btrfs_key cpukey;
Chris Mason7518a232007-03-12 12:01:18 -0400161 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500162
163 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400164 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500165 parent_slot = path->slots[level + 1];
Chris Mason8d7be552007-05-10 11:24:42 -0400166 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400167 BUG_ON(nritems == 0);
168 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400169 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400170 parent_key = &parent->ptrs[parent_slot].key;
171 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400172 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400173 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400174 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175 }
Chris Mason123abc82007-03-14 14:14:43 -0400176 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400177 if (slot != 0) {
178 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
179 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
180 }
181 if (slot < nritems - 1) {
182 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
183 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500184 }
185 return 0;
186}
187
Chris Mason123abc82007-03-14 14:14:43 -0400188static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
189 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500190{
Chris Masone20d96d2007-03-22 12:13:20 -0400191 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400192 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500193 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400194 int slot = path->slots[0];
195 struct btrfs_key cpukey;
196
Chris Mason7518a232007-03-12 12:01:18 -0400197 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500198
199 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400200 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500201 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400202 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400203
204 if (nritems == 0)
205 return 0;
206
207 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400208 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400209 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500210 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400211 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400212 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400213 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500214 }
Chris Mason8d7be552007-05-10 11:24:42 -0400215 if (slot != 0) {
216 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
217 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
218 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
219 btrfs_item_end(leaf->items + slot));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500220 }
Chris Mason8d7be552007-05-10 11:24:42 -0400221 if (slot < nritems - 1) {
222 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
223 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
224 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
225 btrfs_item_end(leaf->items + slot + 1));
226 }
227 BUG_ON(btrfs_item_offset(leaf->items) +
228 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500229 return 0;
230}
231
Chris Mason123abc82007-03-14 14:14:43 -0400232static int check_block(struct btrfs_root *root, struct btrfs_path *path,
233 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500234{
Chris Mason3eb03142007-04-05 14:28:50 -0400235 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
236 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
237 sizeof(node->header.fsid)))
238 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500239 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400240 return check_leaf(root, path, level);
241 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500242}
243
Chris Mason74123bd2007-02-02 11:05:29 -0500244/*
245 * search for key in the array p. items p are item_size apart
246 * and there are 'max' items in p
247 * the slot in the array is returned via slot, and it points to
248 * the place where you would insert key if it is not found in
249 * the array.
250 *
251 * slot may point to max if the key is bigger than all of the keys
252 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400253static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500254 int max, int *slot)
255{
256 int low = 0;
257 int high = max;
258 int mid;
259 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400260 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500261
262 while(low < high) {
263 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400264 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500265 ret = comp_keys(tmp, key);
266
267 if (ret < 0)
268 low = mid + 1;
269 else if (ret > 0)
270 high = mid;
271 else {
272 *slot = mid;
273 return 0;
274 }
275 }
276 *slot = low;
277 return 1;
278}
279
Chris Mason97571fd2007-02-24 13:39:08 -0500280/*
281 * simple bin_search frontend that does the right thing for
282 * leaves vs nodes
283 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400284static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500285{
Chris Mason7518a232007-03-12 12:01:18 -0400286 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400287 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400288 return generic_bin_search((void *)l->items,
289 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400290 key, btrfs_header_nritems(&c->header),
291 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500292 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400293 return generic_bin_search((void *)c->ptrs,
294 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400295 key, btrfs_header_nritems(&c->header),
296 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500297 }
298 return -1;
299}
300
Chris Masone20d96d2007-03-22 12:13:20 -0400301static struct buffer_head *read_node_slot(struct btrfs_root *root,
302 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500303 int slot)
304{
Chris Masone20d96d2007-03-22 12:13:20 -0400305 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500306 if (slot < 0)
307 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400308 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500309 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400310 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500311}
312
Chris Masone089f052007-03-16 16:20:31 -0400313static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
314 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500315{
Chris Masone20d96d2007-03-22 12:13:20 -0400316 struct buffer_head *right_buf;
317 struct buffer_head *mid_buf;
318 struct buffer_head *left_buf;
319 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400320 struct btrfs_node *right = NULL;
321 struct btrfs_node *mid;
322 struct btrfs_node *left = NULL;
323 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500324 int ret = 0;
325 int wret;
326 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500327 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400328 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500329 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500330
331 if (level == 0)
332 return 0;
333
334 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400335 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400336 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500337
Chris Mason234b63a2007-03-13 10:46:10 -0400338 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500339 parent_buf = path->nodes[level + 1];
340 pslot = path->slots[level + 1];
341
Chris Mason40689472007-03-17 14:29:23 -0400342 /*
343 * deal with the case where there is only one pointer in the root
344 * by promoting the node below to a root
345 */
Chris Masonbb803952007-03-01 12:04:21 -0500346 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400347 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400348 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500349
Chris Mason7518a232007-03-12 12:01:18 -0400350 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500351 return 0;
352
353 /* promote the child to a root */
354 child = read_node_slot(root, mid_buf, 0);
355 BUG_ON(!child);
356 root->node = child;
357 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400358 clean_tree_block(trans, root, mid_buf);
359 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500360 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400361 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500362 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400363 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400364 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500365 }
Chris Masone20d96d2007-03-22 12:13:20 -0400366 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500367
Chris Mason123abc82007-03-14 14:14:43 -0400368 if (btrfs_header_nritems(&mid->header) >
369 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500370 return 0;
371
Chris Mason54aa1f42007-06-22 14:16:25 -0400372 if (btrfs_header_nritems(&mid->header) < 2)
373 err_on_enospc = 1;
374
Chris Masonbb803952007-03-01 12:04:21 -0500375 left_buf = read_node_slot(root, parent_buf, pslot - 1);
376 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500377
378 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500379 if (left_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400380 wret = btrfs_cow_block(trans, root, left_buf,
381 parent_buf, pslot - 1, &left_buf);
382 if (wret) {
383 ret = wret;
384 goto enospc;
385 }
Chris Masone20d96d2007-03-22 12:13:20 -0400386 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400387 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400388 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500389 if (wret < 0)
390 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400391 if (btrfs_header_nritems(&mid->header) < 2)
392 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500393 }
Chris Mason79f95c82007-03-01 15:16:26 -0500394
395 /*
396 * then try to empty the right most buffer into the middle
397 */
Chris Masonbb803952007-03-01 12:04:21 -0500398 if (right_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400399 wret = btrfs_cow_block(trans, root, right_buf,
400 parent_buf, pslot + 1, &right_buf);
401 if (wret) {
402 ret = wret;
403 goto enospc;
404 }
405
Chris Masone20d96d2007-03-22 12:13:20 -0400406 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400407 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400408 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500409 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400410 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400411 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400412 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400413 wait_on_buffer(right_buf);
414 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500415 right_buf = NULL;
416 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400417 wret = del_ptr(trans, root, path, level + 1, pslot +
418 1);
Chris Masonbb803952007-03-01 12:04:21 -0500419 if (wret)
420 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400421 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500422 if (wret)
423 ret = wret;
424 } else {
Chris Masond6025572007-03-30 14:27:56 -0400425 btrfs_memcpy(root, parent,
426 &parent->ptrs[pslot + 1].key,
427 &right->ptrs[0].key,
428 sizeof(struct btrfs_disk_key));
429 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500430 }
431 }
Chris Mason7518a232007-03-12 12:01:18 -0400432 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500433 /*
434 * we're not allowed to leave a node with one item in the
435 * tree during a delete. A deletion from lower in the tree
436 * could try to delete the only pointer in this node.
437 * So, pull some keys from the left.
438 * There has to be a left pointer at this point because
439 * otherwise we would have pulled some pointers from the
440 * right
441 */
442 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400443 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400444 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500445 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400446 goto enospc;
447 }
Chris Mason79f95c82007-03-01 15:16:26 -0500448 BUG_ON(wret == 1);
449 }
Chris Mason7518a232007-03-12 12:01:18 -0400450 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500451 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400452 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400453 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400454 wait_on_buffer(mid_buf);
455 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500456 mid_buf = NULL;
457 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400458 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500459 if (wret)
460 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400461 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500462 if (wret)
463 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500464 } else {
465 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400466 btrfs_memcpy(root, parent,
467 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
468 sizeof(struct btrfs_disk_key));
469 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500470 }
Chris Masonbb803952007-03-01 12:04:21 -0500471
Chris Mason79f95c82007-03-01 15:16:26 -0500472 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500473 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400474 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400475 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500476 path->nodes[level] = left_buf;
477 path->slots[level + 1] -= 1;
478 path->slots[level] = orig_slot;
479 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400480 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500481 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400482 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500483 path->slots[level] = orig_slot;
484 }
485 }
Chris Mason79f95c82007-03-01 15:16:26 -0500486 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400487 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400488 if (orig_ptr !=
489 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
490 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500491 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400492enospc:
Chris Masonbb803952007-03-01 12:04:21 -0500493 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400494 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500495 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400496 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500497 return ret;
498}
499
Chris Masone66f7092007-04-20 13:16:02 -0400500/* returns zero if the push worked, non-zero otherwise */
501static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
502 struct btrfs_root *root,
503 struct btrfs_path *path, int level)
504{
505 struct buffer_head *right_buf;
506 struct buffer_head *mid_buf;
507 struct buffer_head *left_buf;
508 struct buffer_head *parent_buf = NULL;
509 struct btrfs_node *right = NULL;
510 struct btrfs_node *mid;
511 struct btrfs_node *left = NULL;
512 struct btrfs_node *parent = NULL;
513 int ret = 0;
514 int wret;
515 int pslot;
516 int orig_slot = path->slots[level];
517 u64 orig_ptr;
518
519 if (level == 0)
520 return 1;
521
522 mid_buf = path->nodes[level];
523 mid = btrfs_buffer_node(mid_buf);
524 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
525
526 if (level < BTRFS_MAX_LEVEL - 1)
527 parent_buf = path->nodes[level + 1];
528 pslot = path->slots[level + 1];
529
530 if (!parent_buf)
531 return 1;
532 parent = btrfs_buffer_node(parent_buf);
533
534 left_buf = read_node_slot(root, parent_buf, pslot - 1);
535
536 /* first, try to make some room in the middle buffer */
537 if (left_buf) {
538 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400539 left = btrfs_buffer_node(left_buf);
540 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400541 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
542 wret = 1;
543 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400544 ret = btrfs_cow_block(trans, root, left_buf, parent_buf,
545 pslot - 1, &left_buf);
546 if (ret)
547 wret = 1;
548 else {
549 left = btrfs_buffer_node(left_buf);
550 wret = push_node_left(trans, root,
551 left_buf, mid_buf);
552 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400553 }
Chris Masone66f7092007-04-20 13:16:02 -0400554 if (wret < 0)
555 ret = wret;
556 if (wret == 0) {
557 orig_slot += left_nr;
558 btrfs_memcpy(root, parent,
559 &parent->ptrs[pslot].key,
560 &mid->ptrs[0].key,
561 sizeof(struct btrfs_disk_key));
562 btrfs_mark_buffer_dirty(parent_buf);
563 if (btrfs_header_nritems(&left->header) > orig_slot) {
564 path->nodes[level] = left_buf;
565 path->slots[level + 1] -= 1;
566 path->slots[level] = orig_slot;
567 btrfs_block_release(root, mid_buf);
568 } else {
569 orig_slot -=
570 btrfs_header_nritems(&left->header);
571 path->slots[level] = orig_slot;
572 btrfs_block_release(root, left_buf);
573 }
574 check_node(root, path, level);
575 return 0;
576 }
577 btrfs_block_release(root, left_buf);
578 }
579 right_buf = read_node_slot(root, parent_buf, pslot + 1);
580
581 /*
582 * then try to empty the right most buffer into the middle
583 */
584 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400585 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400586 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400587 right_nr = btrfs_header_nritems(&right->header);
588 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
589 wret = 1;
590 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400591 ret = btrfs_cow_block(trans, root, right_buf,
592 parent_buf, pslot + 1,
593 &right_buf);
594 if (ret)
595 wret = 1;
596 else {
597 right = btrfs_buffer_node(right_buf);
598 wret = balance_node_right(trans, root,
599 right_buf, mid_buf);
600 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400601 }
Chris Masone66f7092007-04-20 13:16:02 -0400602 if (wret < 0)
603 ret = wret;
604 if (wret == 0) {
605 btrfs_memcpy(root, parent,
606 &parent->ptrs[pslot + 1].key,
607 &right->ptrs[0].key,
608 sizeof(struct btrfs_disk_key));
609 btrfs_mark_buffer_dirty(parent_buf);
610 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
611 path->nodes[level] = right_buf;
612 path->slots[level + 1] += 1;
613 path->slots[level] = orig_slot -
614 btrfs_header_nritems(&mid->header);
615 btrfs_block_release(root, mid_buf);
616 } else {
617 btrfs_block_release(root, right_buf);
618 }
619 check_node(root, path, level);
620 return 0;
621 }
622 btrfs_block_release(root, right_buf);
623 }
624 check_node(root, path, level);
625 return 1;
626}
627
Chris Mason74123bd2007-02-02 11:05:29 -0500628/*
629 * look for key in the tree. path is filled in with nodes along the way
630 * if key is found, we return zero and you can find the item in the leaf
631 * level of the path (level 0)
632 *
633 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500634 * be inserted, and 1 is returned. If there are other errors during the
635 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500636 *
637 * if ins_len > 0, nodes and leaves will be split as we walk down the
638 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
639 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500640 */
Chris Masone089f052007-03-16 16:20:31 -0400641int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
642 *root, struct btrfs_key *key, struct btrfs_path *p, int
643 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500644{
Chris Masone20d96d2007-03-22 12:13:20 -0400645 struct buffer_head *b;
646 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400647 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500648 int slot;
649 int ret;
650 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500651
Chris Mason22b0ebd2007-03-30 08:47:31 -0400652 WARN_ON(p->nodes[0] != NULL);
653 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500654again:
655 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400656 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500657 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400658 c = btrfs_buffer_node(b);
659 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500660 if (cow) {
661 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400662 wret = btrfs_cow_block(trans, root, b,
663 p->nodes[level + 1],
664 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400665 &cow_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400666 if (wret) {
667 btrfs_block_release(root, cow_buf);
668 return wret;
669 }
Chris Mason02217ed2007-03-02 16:08:05 -0500670 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400671 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500672 }
673 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400674 if (level != btrfs_header_level(&c->header))
675 WARN_ON(1);
676 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500677 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400678 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500679 if (ret)
680 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500681 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400682 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500683 if (ret && slot > 0)
684 slot -= 1;
685 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400686 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
687 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400688 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500689 BUG_ON(sret > 0);
690 if (sret)
691 return sret;
692 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400693 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500694 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500695 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400696 int sret = balance_level(trans, root, p,
697 level);
Chris Masonbb803952007-03-01 12:04:21 -0500698 if (sret)
699 return sret;
700 b = p->nodes[level];
701 if (!b)
702 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400703 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500704 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400705 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500706 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400707 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500708 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400709 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500710 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400711 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400712 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400713 int sret = split_leaf(trans, root, key,
714 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500715 BUG_ON(sret > 0);
716 if (sret)
717 return sret;
718 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500719 return ret;
720 }
721 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500722 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723}
724
Chris Mason74123bd2007-02-02 11:05:29 -0500725/*
726 * adjust the pointers going up the tree, starting at level
727 * making sure the right key of each node is points to 'key'.
728 * This is used after shifting pointers to the left, so it stops
729 * fixing up pointers when a given leaf/node is not in slot 0 of the
730 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731 *
732 * If this fails to write a tree block, it returns -1, but continues
733 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500734 */
Chris Masone089f052007-03-16 16:20:31 -0400735static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
736 *root, struct btrfs_path *path, struct btrfs_disk_key
737 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500738{
739 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500740 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400741 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
742 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500744 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500745 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400746 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400747 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
748 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500749 if (tslot != 0)
750 break;
751 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500752 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500753}
754
Chris Mason74123bd2007-02-02 11:05:29 -0500755/*
756 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500757 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500758 *
759 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
760 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500761 */
Chris Masone089f052007-03-16 16:20:31 -0400762static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400763 *root, struct buffer_head *dst_buf, struct
764 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500765{
Chris Masone20d96d2007-03-22 12:13:20 -0400766 struct btrfs_node *src = btrfs_buffer_node(src_buf);
767 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500768 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500769 int src_nritems;
770 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500771 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500772
Chris Mason7518a232007-03-12 12:01:18 -0400773 src_nritems = btrfs_header_nritems(&src->header);
774 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400775 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -0400776
Chris Masoneb60cea2007-02-02 09:18:22 -0500777 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500778 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500779 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500780
781 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500782 push_items = src_nritems;
783
Chris Masond6025572007-03-30 14:27:56 -0400784 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
785 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500786 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400787 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400788 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400789 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500790 }
Chris Mason7518a232007-03-12 12:01:18 -0400791 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
792 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -0400793 btrfs_mark_buffer_dirty(src_buf);
794 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500795 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500796}
797
Chris Mason97571fd2007-02-24 13:39:08 -0500798/*
Chris Mason79f95c82007-03-01 15:16:26 -0500799 * try to push data from one node into the next node right in the
800 * tree.
801 *
802 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
803 * error, and > 0 if there was no room in the right hand block.
804 *
805 * this will only push up to 1/2 the contents of the left node over
806 */
Chris Masone089f052007-03-16 16:20:31 -0400807static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400808 btrfs_root *root, struct buffer_head *dst_buf,
809 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500810{
Chris Masone20d96d2007-03-22 12:13:20 -0400811 struct btrfs_node *src = btrfs_buffer_node(src_buf);
812 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500813 int push_items = 0;
814 int max_push;
815 int src_nritems;
816 int dst_nritems;
817 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500818
Chris Mason7518a232007-03-12 12:01:18 -0400819 src_nritems = btrfs_header_nritems(&src->header);
820 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400821 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500822 if (push_items <= 0) {
823 return 1;
824 }
825
826 max_push = src_nritems / 2 + 1;
827 /* don't try to empty the node */
828 if (max_push > src_nritems)
829 return 1;
830 if (max_push < push_items)
831 push_items = max_push;
832
Chris Masond6025572007-03-30 14:27:56 -0400833 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
834 dst_nritems * sizeof(struct btrfs_key_ptr));
835
836 btrfs_memcpy(root, dst, dst->ptrs,
837 src->ptrs + src_nritems - push_items,
838 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500839
Chris Mason7518a232007-03-12 12:01:18 -0400840 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
841 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500842
Chris Masond6025572007-03-30 14:27:56 -0400843 btrfs_mark_buffer_dirty(src_buf);
844 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500845 return ret;
846}
847
848/*
Chris Mason97571fd2007-02-24 13:39:08 -0500849 * helper function to insert a new root level in the tree.
850 * A new node is allocated, and a single item is inserted to
851 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500852 *
853 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500854 */
Chris Masone089f052007-03-16 16:20:31 -0400855static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
856 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500857{
Chris Masone20d96d2007-03-22 12:13:20 -0400858 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400859 struct btrfs_node *lower;
860 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400861 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500862
863 BUG_ON(path->nodes[level]);
864 BUG_ON(path->nodes[level-1] != root->node);
865
Chris Mason31f3c992007-04-30 15:25:45 -0400866 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr);
Chris Mason54aa1f42007-06-22 14:16:25 -0400867 if (IS_ERR(t))
868 return PTR_ERR(t);
Chris Masone20d96d2007-03-22 12:13:20 -0400869 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400870 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400871 btrfs_set_header_nritems(&c->header, 1);
872 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -0400873 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -0400874 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400875 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -0400876 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -0400877 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
878 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400879 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400880 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500881 else
Chris Mason123abc82007-03-14 14:14:43 -0400882 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -0400883 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
884 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -0400885 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -0400886
Chris Masond6025572007-03-30 14:27:56 -0400887 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -0400888
Chris Mason5c680ed2007-02-22 11:39:13 -0500889 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400890 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500891 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400892 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500893 path->nodes[level] = t;
894 path->slots[level] = 0;
895 return 0;
896}
897
Chris Mason74123bd2007-02-02 11:05:29 -0500898/*
899 * worker function to insert a single pointer in a node.
900 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500901 *
Chris Mason74123bd2007-02-02 11:05:29 -0500902 * slot and level indicate where you want the key to go, and
903 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500904 *
905 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500906 */
Chris Masone089f052007-03-16 16:20:31 -0400907static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
908 *root, struct btrfs_path *path, struct btrfs_disk_key
909 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500910{
Chris Mason234b63a2007-03-13 10:46:10 -0400911 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500912 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500913
914 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400915 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400916 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500917 if (slot > nritems)
918 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400919 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500920 BUG();
921 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400922 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
923 lower->ptrs + slot,
924 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500925 }
Chris Masond6025572007-03-30 14:27:56 -0400926 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
927 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400928 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400929 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -0400930 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason098f59c2007-05-11 11:33:21 -0400931 check_node(root, path, level);
Chris Mason74123bd2007-02-02 11:05:29 -0500932 return 0;
933}
934
Chris Mason97571fd2007-02-24 13:39:08 -0500935/*
936 * split the node at the specified level in path in two.
937 * The path is corrected to point to the appropriate node after the split
938 *
939 * Before splitting this tries to make some room in the node by pushing
940 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500941 *
942 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500943 */
Chris Masone089f052007-03-16 16:20:31 -0400944static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
945 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500946{
Chris Masone20d96d2007-03-22 12:13:20 -0400947 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400948 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400949 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400950 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500951 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500952 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500953 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400954 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500955
Chris Mason5c680ed2007-02-22 11:39:13 -0500956 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400957 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500958 if (t == root->node) {
959 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400960 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500961 if (ret)
962 return ret;
Chris Masone66f7092007-04-20 13:16:02 -0400963 } else {
964 ret = push_nodes_for_insert(trans, root, path, level);
965 t = path->nodes[level];
966 c = btrfs_buffer_node(t);
967 if (!ret &&
968 btrfs_header_nritems(&c->header) <
969 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
970 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400971 if (ret < 0)
972 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500973 }
Chris Masone66f7092007-04-20 13:16:02 -0400974
Chris Mason7518a232007-03-12 12:01:18 -0400975 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason31f3c992007-04-30 15:25:45 -0400976 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr);
Chris Mason54aa1f42007-06-22 14:16:25 -0400977 if (IS_ERR(split_buffer))
978 return PTR_ERR(split_buffer);
979
Chris Masone20d96d2007-03-22 12:13:20 -0400980 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400981 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -0400982 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -0400983 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -0400984 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400985 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -0400986 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
987 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400988 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -0400989 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
990 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400991 btrfs_set_header_nritems(&split->header, c_nritems - mid);
992 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500993 ret = 0;
994
Chris Masond6025572007-03-30 14:27:56 -0400995 btrfs_mark_buffer_dirty(t);
996 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400997 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -0400998 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400999 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001000 if (wret)
1001 ret = wret;
1002
Chris Mason5de08d72007-02-24 06:24:44 -05001003 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001004 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -04001005 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001006 path->nodes[level] = split_buffer;
1007 path->slots[level + 1] += 1;
1008 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001009 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001010 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001011 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001012}
1013
Chris Mason74123bd2007-02-02 11:05:29 -05001014/*
1015 * how many bytes are required to store the items in a leaf. start
1016 * and nr indicate which items in the leaf to check. This totals up the
1017 * space used both by the item structs and the item data
1018 */
Chris Mason234b63a2007-03-13 10:46:10 -04001019static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001020{
1021 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -04001022 int nritems = btrfs_header_nritems(&l->header);
1023 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001024
1025 if (!nr)
1026 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001027 data_len = btrfs_item_end(l->items + start);
1028 data_len = data_len - btrfs_item_offset(l->items + end);
1029 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001030 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001031 return data_len;
1032}
1033
Chris Mason74123bd2007-02-02 11:05:29 -05001034/*
Chris Masond4dbff92007-04-04 14:08:15 -04001035 * The space between the end of the leaf items and
1036 * the start of the leaf data. IOW, how much room
1037 * the leaf has left for both items and data
1038 */
1039int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
1040{
1041 int nritems = btrfs_header_nritems(&leaf->header);
1042 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1043}
1044
1045/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001046 * push some data in the path leaf to the right, trying to free up at
1047 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001048 *
1049 * returns 1 if the push failed because the other node didn't have enough
1050 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001051 */
Chris Masone089f052007-03-16 16:20:31 -04001052static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1053 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001054{
Chris Masone20d96d2007-03-22 12:13:20 -04001055 struct buffer_head *left_buf = path->nodes[0];
1056 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001057 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001058 struct buffer_head *right_buf;
1059 struct buffer_head *upper;
1060 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -05001061 int slot;
1062 int i;
1063 int free_space;
1064 int push_space = 0;
1065 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001066 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001067 u32 left_nritems;
1068 u32 right_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001069 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001070
1071 slot = path->slots[1];
1072 if (!path->nodes[1]) {
1073 return 1;
1074 }
1075 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001076 upper_node = btrfs_buffer_node(upper);
1077 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001078 return 1;
1079 }
Chris Masone20d96d2007-03-22 12:13:20 -04001080 right_buf = read_tree_block(root,
1081 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1082 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001083 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001084 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001085 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001086 return 1;
1087 }
Chris Mason02217ed2007-03-02 16:08:05 -05001088 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001089 ret = btrfs_cow_block(trans, root, right_buf, upper,
1090 slot + 1, &right_buf);
1091 if (ret) {
1092 btrfs_block_release(root, right_buf);
1093 return 1;
1094 }
Chris Masone20d96d2007-03-22 12:13:20 -04001095 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001096 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001097 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001098 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001099 return 1;
1100 }
1101
Chris Mason7518a232007-03-12 12:01:18 -04001102 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001103 if (left_nritems == 0) {
1104 btrfs_block_release(root, right_buf);
1105 return 1;
1106 }
1107 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001108 item = left->items + i;
1109 if (path->slots[0] == i)
1110 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001111 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1112 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001113 break;
1114 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001115 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001116 }
1117 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001118 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001119 return 1;
1120 }
Chris Masona429e512007-04-18 16:15:28 -04001121 if (push_items == left_nritems)
1122 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001123 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001124 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001125 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001126 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001127 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001128 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1129 leaf_data_end(root, right) - push_space,
1130 btrfs_leaf_data(right) +
1131 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1132 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001133 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001134 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1135 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1136 btrfs_leaf_data(left) + leaf_data_end(root, left),
1137 push_space);
1138 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001139 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001140 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001141 btrfs_memcpy(root, right, right->items, left->items +
1142 left_nritems - push_items,
1143 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001144
1145 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001146 right_nritems += push_items;
1147 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001148 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001149 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001150 btrfs_set_item_offset(right->items + i, push_space -
1151 btrfs_item_size(right->items + i));
1152 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001153 }
Chris Mason7518a232007-03-12 12:01:18 -04001154 left_nritems -= push_items;
1155 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001156
Chris Masond6025572007-03-30 14:27:56 -04001157 btrfs_mark_buffer_dirty(left_buf);
1158 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001159
Chris Masond6025572007-03-30 14:27:56 -04001160 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001161 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001162 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001163
Chris Mason00ec4c52007-02-24 12:47:20 -05001164 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001165 if (path->slots[0] >= left_nritems) {
1166 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001167 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001168 path->nodes[0] = right_buf;
1169 path->slots[1] += 1;
1170 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001171 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001172 }
Chris Mason098f59c2007-05-11 11:33:21 -04001173 if (path->nodes[1])
1174 check_node(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -05001175 return 0;
1176}
1177/*
Chris Mason74123bd2007-02-02 11:05:29 -05001178 * push some data in the path leaf to the left, trying to free up at
1179 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1180 */
Chris Masone089f052007-03-16 16:20:31 -04001181static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1182 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001183{
Chris Masone20d96d2007-03-22 12:13:20 -04001184 struct buffer_head *right_buf = path->nodes[0];
1185 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1186 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001187 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001188 int slot;
1189 int i;
1190 int free_space;
1191 int push_space = 0;
1192 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001193 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001194 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001195 int ret = 0;
1196 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001197
1198 slot = path->slots[1];
1199 if (slot == 0) {
1200 return 1;
1201 }
1202 if (!path->nodes[1]) {
1203 return 1;
1204 }
Chris Masone20d96d2007-03-22 12:13:20 -04001205 t = read_tree_block(root,
1206 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1207 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001208 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001209 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001210 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001211 return 1;
1212 }
Chris Mason02217ed2007-03-02 16:08:05 -05001213
1214 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001215 ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1216 if (ret) {
1217 /* we hit -ENOSPC, but it isn't fatal here */
1218 return 1;
1219 }
Chris Masone20d96d2007-03-22 12:13:20 -04001220 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001221 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001222 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001223 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001224 return 1;
1225 }
1226
Chris Masona429e512007-04-18 16:15:28 -04001227 if (btrfs_header_nritems(&right->header) == 0) {
1228 btrfs_block_release(root, t);
1229 return 1;
1230 }
1231
1232 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001233 item = right->items + i;
1234 if (path->slots[0] == i)
1235 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001236 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1237 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001238 break;
1239 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001240 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001241 }
1242 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001243 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001244 return 1;
1245 }
Chris Masona429e512007-04-18 16:15:28 -04001246 if (push_items == btrfs_header_nritems(&right->header))
1247 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001248 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001249 btrfs_memcpy(root, left, left->items +
1250 btrfs_header_nritems(&left->header),
1251 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001252 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001253 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001254 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1255 leaf_data_end(root, left) - push_space,
1256 btrfs_leaf_data(right) +
1257 btrfs_item_offset(right->items + push_items - 1),
1258 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001259 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001260 BUG_ON(old_left_nritems < 0);
1261
Chris Mason0783fcf2007-03-12 20:12:07 -04001262 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001263 u32 ioff = btrfs_item_offset(left->items + i);
1264 btrfs_set_item_offset(left->items + i, ioff -
1265 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001266 btrfs_item_offset(left->items +
1267 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001268 }
Chris Mason7518a232007-03-12 12:01:18 -04001269 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001270
1271 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001272 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001273 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001274 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1275 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1276 btrfs_leaf_data(right) +
1277 leaf_data_end(root, right), push_space);
1278 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001279 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001280 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001281 btrfs_set_header_nritems(&right->header,
1282 btrfs_header_nritems(&right->header) -
1283 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001284 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001285
Chris Mason7518a232007-03-12 12:01:18 -04001286 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001287 btrfs_set_item_offset(right->items + i, push_space -
1288 btrfs_item_size(right->items + i));
1289 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001290 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001291
Chris Masond6025572007-03-30 14:27:56 -04001292 btrfs_mark_buffer_dirty(t);
1293 btrfs_mark_buffer_dirty(right_buf);
Chris Mason098f59c2007-05-11 11:33:21 -04001294
Chris Masone089f052007-03-16 16:20:31 -04001295 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001296 if (wret)
1297 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001298
1299 /* then fixup the leaf pointer in the path */
1300 if (path->slots[0] < push_items) {
1301 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001302 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001303 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001304 path->slots[1] -= 1;
1305 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001306 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001307 path->slots[0] -= push_items;
1308 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001309 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001310 if (path->nodes[1])
1311 check_node(root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001312 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001313}
1314
Chris Mason74123bd2007-02-02 11:05:29 -05001315/*
1316 * split the path's leaf in two, making sure there is at least data_size
1317 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001318 *
1319 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001320 */
Chris Masone089f052007-03-16 16:20:31 -04001321static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001322 *root, struct btrfs_key *ins_key,
1323 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001324{
Chris Masone20d96d2007-03-22 12:13:20 -04001325 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001326 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001327 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001328 int mid;
1329 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001330 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001331 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001332 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001333 int data_copy_size;
1334 int rt_data_off;
1335 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001336 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001337 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001338 int double_split = 0;
1339 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001340
Chris Mason40689472007-03-17 14:29:23 -04001341 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001342 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001343 if (wret < 0)
1344 return wret;
1345 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001346 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001347 if (wret < 0)
1348 return wret;
1349 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001350 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001351 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001352
1353 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001354 if (btrfs_leaf_free_space(root, l) >=
1355 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001356 return 0;
1357
Chris Mason5c680ed2007-02-22 11:39:13 -05001358 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001359 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001360 if (ret)
1361 return ret;
1362 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001363 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001364 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001365 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001366
Chris Mason31f3c992007-04-30 15:25:45 -04001367 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
Chris Mason54aa1f42007-06-22 14:16:25 -04001368 if (IS_ERR(right_buffer))
1369 return PTR_ERR(right_buffer);
1370
Chris Masone20d96d2007-03-22 12:13:20 -04001371 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001372 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001373 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001374 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001375 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001376 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001377 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1378 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001379 if (mid <= slot) {
1380 if (nritems == 1 ||
1381 leaf_space_used(l, mid, nritems - mid) + space_needed >
1382 BTRFS_LEAF_DATA_SIZE(root)) {
1383 if (slot >= nritems) {
1384 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1385 btrfs_set_header_nritems(&right->header, 0);
1386 wret = insert_ptr(trans, root, path,
1387 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001388 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001389 path->slots[1] + 1, 1);
1390 if (wret)
1391 ret = wret;
1392 btrfs_block_release(root, path->nodes[0]);
1393 path->nodes[0] = right_buffer;
1394 path->slots[0] = 0;
1395 path->slots[1] += 1;
1396 return ret;
1397 }
1398 mid = slot;
1399 double_split = 1;
1400 }
1401 } else {
1402 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1403 BTRFS_LEAF_DATA_SIZE(root)) {
1404 if (slot == 0) {
1405 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1406 btrfs_set_header_nritems(&right->header, 0);
1407 wret = insert_ptr(trans, root, path,
1408 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001409 bh_blocknr(right_buffer),
Chris Mason098f59c2007-05-11 11:33:21 -04001410 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001411 if (wret)
1412 ret = wret;
1413 btrfs_block_release(root, path->nodes[0]);
1414 path->nodes[0] = right_buffer;
1415 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001416 if (path->slots[1] == 0) {
1417 wret = fixup_low_keys(trans, root,
1418 path, &disk_key, 1);
1419 if (wret)
1420 ret = wret;
1421 }
Chris Masond4dbff92007-04-04 14:08:15 -04001422 return ret;
1423 }
1424 mid = slot;
1425 double_split = 1;
1426 }
1427 }
1428 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001429 data_copy_size = btrfs_item_end(l->items + mid) -
1430 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001431 btrfs_memcpy(root, right, right->items, l->items + mid,
1432 (nritems - mid) * sizeof(struct btrfs_item));
1433 btrfs_memcpy(root, right,
1434 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1435 data_copy_size, btrfs_leaf_data(l) +
1436 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001437 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1438 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001439
Chris Mason0783fcf2007-03-12 20:12:07 -04001440 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001441 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001442 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1443 }
Chris Mason74123bd2007-02-02 11:05:29 -05001444
Chris Mason7518a232007-03-12 12:01:18 -04001445 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001446 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001447 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001448 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001449 if (wret)
1450 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001451 btrfs_mark_buffer_dirty(right_buffer);
1452 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001453 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001454 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001455 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001456 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001457 path->slots[0] -= mid;
1458 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001459 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001460 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001461 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001462 check_node(root, path, 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001463
1464 if (!double_split)
1465 return ret;
Chris Mason31f3c992007-04-30 15:25:45 -04001466 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
Chris Mason54aa1f42007-06-22 14:16:25 -04001467 if (IS_ERR(right_buffer))
1468 return PTR_ERR(right_buffer);
1469
Chris Masond4dbff92007-04-04 14:08:15 -04001470 right = btrfs_buffer_leaf(right_buffer);
1471 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001472 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001473 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001474 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001475 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001476 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1477 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001478 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1479 btrfs_set_header_nritems(&right->header, 0);
1480 wret = insert_ptr(trans, root, path,
1481 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001482 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001483 path->slots[1], 1);
1484 if (wret)
1485 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001486 if (path->slots[1] == 0) {
1487 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1488 if (wret)
1489 ret = wret;
1490 }
Chris Masond4dbff92007-04-04 14:08:15 -04001491 btrfs_block_release(root, path->nodes[0]);
1492 path->nodes[0] = right_buffer;
1493 path->slots[0] = 0;
1494 check_node(root, path, 1);
1495 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001496 return ret;
1497}
1498
Chris Masonb18c6682007-04-17 13:26:50 -04001499int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1500 struct btrfs_root *root,
1501 struct btrfs_path *path,
1502 u32 new_size)
1503{
1504 int ret = 0;
1505 int slot;
1506 int slot_orig;
1507 struct btrfs_leaf *leaf;
1508 struct buffer_head *leaf_buf;
1509 u32 nritems;
1510 unsigned int data_end;
1511 unsigned int old_data_start;
1512 unsigned int old_size;
1513 unsigned int size_diff;
1514 int i;
1515
1516 slot_orig = path->slots[0];
1517 leaf_buf = path->nodes[0];
1518 leaf = btrfs_buffer_leaf(leaf_buf);
1519
1520 nritems = btrfs_header_nritems(&leaf->header);
1521 data_end = leaf_data_end(root, leaf);
1522
1523 slot = path->slots[0];
1524 old_data_start = btrfs_item_offset(leaf->items + slot);
1525 old_size = btrfs_item_size(leaf->items + slot);
1526 BUG_ON(old_size <= new_size);
1527 size_diff = old_size - new_size;
1528
1529 BUG_ON(slot < 0);
1530 BUG_ON(slot >= nritems);
1531
1532 /*
1533 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1534 */
1535 /* first correct the data pointers */
1536 for (i = slot; i < nritems; i++) {
1537 u32 ioff = btrfs_item_offset(leaf->items + i);
1538 btrfs_set_item_offset(leaf->items + i,
1539 ioff + size_diff);
1540 }
1541 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001542 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1543 data_end + size_diff, btrfs_leaf_data(leaf) +
1544 data_end, old_data_start + new_size - data_end);
1545 btrfs_set_item_size(leaf->items + slot, new_size);
1546 btrfs_mark_buffer_dirty(leaf_buf);
1547
1548 ret = 0;
1549 if (btrfs_leaf_free_space(root, leaf) < 0)
1550 BUG();
1551 check_leaf(root, path, 0);
1552 return ret;
1553}
1554
Chris Mason6567e832007-04-16 09:22:45 -04001555int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1556 *root, struct btrfs_path *path, u32 data_size)
1557{
1558 int ret = 0;
1559 int slot;
1560 int slot_orig;
1561 struct btrfs_leaf *leaf;
1562 struct buffer_head *leaf_buf;
1563 u32 nritems;
1564 unsigned int data_end;
1565 unsigned int old_data;
1566 unsigned int old_size;
1567 int i;
1568
1569 slot_orig = path->slots[0];
1570 leaf_buf = path->nodes[0];
1571 leaf = btrfs_buffer_leaf(leaf_buf);
1572
1573 nritems = btrfs_header_nritems(&leaf->header);
1574 data_end = leaf_data_end(root, leaf);
1575
1576 if (btrfs_leaf_free_space(root, leaf) < data_size)
1577 BUG();
1578 slot = path->slots[0];
1579 old_data = btrfs_item_end(leaf->items + slot);
1580
1581 BUG_ON(slot < 0);
1582 BUG_ON(slot >= nritems);
1583
1584 /*
1585 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1586 */
1587 /* first correct the data pointers */
1588 for (i = slot; i < nritems; i++) {
1589 u32 ioff = btrfs_item_offset(leaf->items + i);
1590 btrfs_set_item_offset(leaf->items + i,
1591 ioff - data_size);
1592 }
1593 /* shift the data */
1594 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1595 data_end - data_size, btrfs_leaf_data(leaf) +
1596 data_end, old_data - data_end);
1597 data_end = old_data;
1598 old_size = btrfs_item_size(leaf->items + slot);
1599 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1600 btrfs_mark_buffer_dirty(leaf_buf);
1601
1602 ret = 0;
1603 if (btrfs_leaf_free_space(root, leaf) < 0)
1604 BUG();
1605 check_leaf(root, path, 0);
1606 return ret;
1607}
1608
Chris Mason74123bd2007-02-02 11:05:29 -05001609/*
1610 * Given a key and some data, insert an item into the tree.
1611 * This does all the path init required, making room in the tree if needed.
1612 */
Chris Masone089f052007-03-16 16:20:31 -04001613int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1614 *root, struct btrfs_path *path, struct btrfs_key
1615 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001616{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001617 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001618 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001619 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001620 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001621 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001622 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001623 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001624 struct btrfs_disk_key disk_key;
1625
1626 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001627
Chris Mason74123bd2007-02-02 11:05:29 -05001628 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001629 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001630 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001631 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001632 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001633 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001634 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001635 if (ret < 0)
1636 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001637
Chris Mason62e27492007-03-15 12:56:47 -04001638 slot_orig = path->slots[0];
1639 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001640 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001641
Chris Mason7518a232007-03-12 12:01:18 -04001642 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001643 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001644
Chris Mason123abc82007-03-14 14:14:43 -04001645 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001646 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001647 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001648 }
Chris Mason62e27492007-03-15 12:56:47 -04001649 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001650 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001651 if (slot != nritems) {
1652 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001653 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001654
1655 /*
1656 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1657 */
1658 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001659 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001660 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001661 btrfs_set_item_offset(leaf->items + i,
1662 ioff - data_size);
1663 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664
1665 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001666 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1667 leaf->items + slot,
1668 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669
1670 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001671 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1672 data_end - data_size, btrfs_leaf_data(leaf) +
1673 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001674 data_end = old_data;
1675 }
Chris Mason62e27492007-03-15 12:56:47 -04001676 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001677 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1678 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001679 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1680 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001681 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001682 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001683
1684 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001685 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001686 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001687
Chris Mason123abc82007-03-14 14:14:43 -04001688 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001689 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001690 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001691out:
Chris Mason62e27492007-03-15 12:56:47 -04001692 return ret;
1693}
1694
1695/*
1696 * Given a key and some data, insert an item into the tree.
1697 * This does all the path init required, making room in the tree if needed.
1698 */
Chris Masone089f052007-03-16 16:20:31 -04001699int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1700 *root, struct btrfs_key *cpu_key, void *data, u32
1701 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001702{
1703 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001704 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001705 u8 *ptr;
1706
Chris Mason2c90e5d2007-04-02 10:50:19 -04001707 path = btrfs_alloc_path();
1708 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001709 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001710 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001711 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1712 path->slots[0], u8);
1713 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001714 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001715 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001716 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001717 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001718 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001719}
1720
Chris Mason74123bd2007-02-02 11:05:29 -05001721/*
Chris Mason5de08d72007-02-24 06:24:44 -05001722 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001723 *
1724 * If the delete empties a node, the node is removed from the tree,
1725 * continuing all the way the root if required. The root is converted into
1726 * a leaf if all the nodes are emptied.
1727 */
Chris Masone089f052007-03-16 16:20:31 -04001728static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1729 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001730{
Chris Mason234b63a2007-03-13 10:46:10 -04001731 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001732 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001733 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001734 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001735 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736
Chris Masone20d96d2007-03-22 12:13:20 -04001737 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001738 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001739 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001740 btrfs_memmove(root, node, node->ptrs + slot,
1741 node->ptrs + slot + 1,
1742 sizeof(struct btrfs_key_ptr) *
1743 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001744 }
Chris Mason7518a232007-03-12 12:01:18 -04001745 nritems--;
1746 btrfs_set_header_nritems(&node->header, nritems);
1747 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001748 struct btrfs_header *header = btrfs_buffer_header(root->node);
1749 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001750 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001751 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001752 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001753 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001754 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001755 if (wret)
1756 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757 }
Chris Masond6025572007-03-30 14:27:56 -04001758 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001759 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001760}
1761
Chris Mason74123bd2007-02-02 11:05:29 -05001762/*
1763 * delete the item at the leaf level in path. If that empties
1764 * the leaf, remove it from the tree
1765 */
Chris Masone089f052007-03-16 16:20:31 -04001766int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1767 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001768{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001770 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001771 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001772 int doff;
1773 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001774 int ret = 0;
1775 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001776 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001777
Chris Masoneb60cea2007-02-02 09:18:22 -05001778 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001779 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001780 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001781 doff = btrfs_item_offset(leaf->items + slot);
1782 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001783 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001784
Chris Mason7518a232007-03-12 12:01:18 -04001785 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001786 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001787 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04001788 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1789 data_end + dsize,
1790 btrfs_leaf_data(leaf) + data_end,
1791 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001792 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001793 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001794 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1795 }
Chris Masond6025572007-03-30 14:27:56 -04001796 btrfs_memmove(root, leaf, leaf->items + slot,
1797 leaf->items + slot + 1,
1798 sizeof(struct btrfs_item) *
1799 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001800 }
Chris Mason7518a232007-03-12 12:01:18 -04001801 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1802 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001803 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001804 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001805 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001806 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001807 } else {
Chris Masone089f052007-03-16 16:20:31 -04001808 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001809 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001810 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001811 if (wret)
1812 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001813 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001814 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001815 if (wret)
1816 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001817 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001819 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001820 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001821 wret = fixup_low_keys(trans, root, path,
1822 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001823 if (wret)
1824 ret = wret;
1825 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001826
Chris Mason74123bd2007-02-02 11:05:29 -05001827 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001828 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001829 /* push_leaf_left fixes the path.
1830 * make sure the path still points to our leaf
1831 * for possible call to del_ptr below
1832 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001833 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001834 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001835 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001836 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001837 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001838 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001839 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001840 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001841 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001842 ret = wret;
1843 }
Chris Mason7518a232007-03-12 12:01:18 -04001844 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04001845 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001846 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001847 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001848 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001849 if (wret)
1850 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001851 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001852 wret = btrfs_free_extent(trans, root, blocknr,
1853 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001854 if (wret)
1855 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001856 } else {
Chris Masond6025572007-03-30 14:27:56 -04001857 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001858 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 }
Chris Masond5719762007-03-23 10:01:08 -04001860 } else {
Chris Masond6025572007-03-30 14:27:56 -04001861 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862 }
1863 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001864 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001865}
1866
Chris Mason97571fd2007-02-24 13:39:08 -05001867/*
1868 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001869 * returns 0 if it found something or 1 if there are no greater leaves.
1870 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001871 */
Chris Mason234b63a2007-03-13 10:46:10 -04001872int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001873{
1874 int slot;
1875 int level = 1;
1876 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001877 struct buffer_head *c;
1878 struct btrfs_node *c_node;
1879 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001880
Chris Mason234b63a2007-03-13 10:46:10 -04001881 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001882 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001883 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001884 slot = path->slots[level] + 1;
1885 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001886 c_node = btrfs_buffer_node(c);
1887 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001888 level++;
1889 continue;
1890 }
Chris Masone20d96d2007-03-22 12:13:20 -04001891 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001892 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001893 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001894 next = read_tree_block(root, blocknr);
1895 break;
1896 }
1897 path->slots[level] = slot;
1898 while(1) {
1899 level--;
1900 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001901 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001902 path->nodes[level] = next;
1903 path->slots[level] = 0;
1904 if (!level)
1905 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001906 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001907 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001908 }
1909 return 0;
1910}