blob: bbb632291709c9b1d3335156e66a232a33a68d3c [file] [log] [blame]
Chris Masonbe0e5c02007-01-26 15:51:26 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
Chris Mason5de08d72007-02-24 06:24:44 -05007#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05008
Chris Mason234b63a2007-03-13 10:46:10 -04009static int split_node(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -050010 int level);
Chris Mason234b63a2007-03-13 10:46:10 -040011static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -050012 int data_size);
Chris Mason234b63a2007-03-13 10:46:10 -040013static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst,
14 struct btrfs_buffer *src);
15static int balance_node_right(struct btrfs_root *root,
16 struct btrfs_buffer *dst_buf,
17 struct btrfs_buffer *src_buf);
18static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
Chris Masonbb803952007-03-01 12:04:21 -050019 int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050020
Chris Mason234b63a2007-03-13 10:46:10 -040021inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050022{
23 memset(p, 0, sizeof(*p));
24}
25
Chris Mason234b63a2007-03-13 10:46:10 -040026void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050027{
28 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040029 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050030 if (!p->nodes[i])
31 break;
Chris Mason234b63a2007-03-13 10:46:10 -040032 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050033 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050034 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050035}
36
Chris Mason9aca1d52007-03-13 11:09:37 -040037static int btrfs_cow_block(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -040038 struct btrfs_buffer *buf,
39 struct btrfs_buffer *parent,
Chris Mason02217ed2007-03-02 16:08:05 -050040 int parent_slot,
Chris Mason234b63a2007-03-13 10:46:10 -040041 struct btrfs_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050042{
Chris Mason234b63a2007-03-13 10:46:10 -040043 struct btrfs_buffer *cow;
Chris Mason02217ed2007-03-02 16:08:05 -050044
45 if (!list_empty(&buf->dirty)) {
46 *cow_ret = buf;
47 return 0;
48 }
Chris Mason234b63a2007-03-13 10:46:10 -040049 cow = btrfs_alloc_free_block(root);
Chris Mason02217ed2007-03-02 16:08:05 -050050 memcpy(&cow->node, &buf->node, sizeof(buf->node));
Chris Mason7518a232007-03-12 12:01:18 -040051 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050052 *cow_ret = cow;
Chris Masona28ec192007-03-06 20:08:01 -050053 btrfs_inc_ref(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050054 if (buf == root->node) {
55 root->node = cow;
56 cow->count++;
Chris Masona28ec192007-03-06 20:08:01 -050057 if (buf != root->commit_root)
Chris Mason234b63a2007-03-13 10:46:10 -040058 btrfs_free_extent(root, buf->blocknr, 1);
59 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050060 } else {
Chris Mason1d4f8a02007-03-13 09:28:32 -040061 btrfs_set_node_blockptr(&parent->node, parent_slot,
62 cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050063 BUG_ON(list_empty(&parent->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040064 btrfs_free_extent(root, buf->blocknr, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050065 }
Chris Mason234b63a2007-03-13 10:46:10 -040066 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050067 return 0;
68}
69
Chris Mason74123bd2007-02-02 11:05:29 -050070/*
71 * The leaf data grows from end-to-front in the node.
72 * this returns the address of the start of the last item,
73 * which is the stop of the leaf data stack
74 */
Chris Mason234b63a2007-03-13 10:46:10 -040075static inline unsigned int leaf_data_end(struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050076{
Chris Mason7518a232007-03-12 12:01:18 -040077 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050078 if (nr == 0)
Chris Masond97e63b2007-02-20 16:40:44 -050079 return sizeof(leaf->data);
Chris Mason0783fcf2007-03-12 20:12:07 -040080 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050081}
82
Chris Mason74123bd2007-02-02 11:05:29 -050083/*
84 * The space between the end of the leaf items and
85 * the start of the leaf data. IOW, how much room
86 * the leaf has left for both items and data
87 */
Chris Mason234b63a2007-03-13 10:46:10 -040088int btrfs_leaf_free_space(struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050089{
90 int data_end = leaf_data_end(leaf);
Chris Mason7518a232007-03-12 12:01:18 -040091 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050092 char *items_end = (char *)(leaf->items + nritems + 1);
93 return (char *)(leaf->data + data_end) - (char *)items_end;
94}
95
Chris Mason74123bd2007-02-02 11:05:29 -050096/*
97 * compare two keys in a memcmp fashion
98 */
Chris Mason9aca1d52007-03-13 11:09:37 -040099static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500100{
Chris Masone2fa7222007-03-12 16:22:34 -0400101 struct btrfs_key k1;
102
103 btrfs_disk_key_to_cpu(&k1, disk);
104
105 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500106 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400107 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500108 return -1;
Chris Masone2fa7222007-03-12 16:22:34 -0400109 if (k1.flags > k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500110 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400111 if (k1.flags < k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500112 return -1;
Chris Masone2fa7222007-03-12 16:22:34 -0400113 if (k1.offset > k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500114 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400115 if (k1.offset < k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500116 return -1;
117 return 0;
118}
Chris Mason74123bd2007-02-02 11:05:29 -0500119
Chris Mason9aca1d52007-03-13 11:09:37 -0400120static int check_node(struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500121{
122 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400123 struct btrfs_node *parent = NULL;
124 struct btrfs_node *node = &path->nodes[level]->node;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500125 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400126 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500127
128 if (path->nodes[level + 1])
129 parent = &path->nodes[level + 1]->node;
130 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400131 BUG_ON(nritems == 0);
132 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400133 struct btrfs_disk_key *parent_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500134 parent_key = &parent->keys[parent_slot];
Chris Masone2fa7222007-03-12 16:22:34 -0400135 BUG_ON(memcmp(parent_key, node->keys,
136 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400137 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400138 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500139 }
Chris Mason7518a232007-03-12 12:01:18 -0400140 BUG_ON(nritems > NODEPTRS_PER_BLOCK);
141 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400142 struct btrfs_key cpukey;
143 btrfs_disk_key_to_cpu(&cpukey, &node->keys[i + 1]);
144 BUG_ON(comp_keys(&node->keys[i], &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500145 }
146 return 0;
147}
148
Chris Mason9aca1d52007-03-13 11:09:37 -0400149static int check_leaf(struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500150{
151 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400152 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
153 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500154 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400155 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500156
157 if (path->nodes[level + 1])
158 parent = &path->nodes[level + 1]->node;
159 parent_slot = path->slots[level + 1];
Chris Mason234b63a2007-03-13 10:46:10 -0400160 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400161
162 if (nritems == 0)
163 return 0;
164
165 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400166 struct btrfs_disk_key *parent_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500167 parent_key = &parent->keys[parent_slot];
168 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400169 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400170 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400171 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500172 }
Chris Mason7518a232007-03-12 12:01:18 -0400173 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400174 struct btrfs_key cpukey;
175 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500176 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400177 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400178 BUG_ON(btrfs_item_offset(leaf->items + i) !=
179 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500180 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400181 BUG_ON(btrfs_item_offset(leaf->items + i) +
182 btrfs_item_size(leaf->items + i) !=
183 LEAF_DATA_SIZE);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500184 }
185 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500186 return 0;
187}
188
Chris Mason9aca1d52007-03-13 11:09:37 -0400189static int check_block(struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500190{
191 if (level == 0)
192 return check_leaf(path, level);
193 return check_node(path, level);
194}
195
Chris Mason74123bd2007-02-02 11:05:29 -0500196/*
197 * search for key in the array p. items p are item_size apart
198 * and there are 'max' items in p
199 * the slot in the array is returned via slot, and it points to
200 * the place where you would insert key if it is not found in
201 * the array.
202 *
203 * slot may point to max if the key is bigger than all of the keys
204 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400205static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500206 int max, int *slot)
207{
208 int low = 0;
209 int high = max;
210 int mid;
211 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400212 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500213
214 while(low < high) {
215 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400216 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500217 ret = comp_keys(tmp, key);
218
219 if (ret < 0)
220 low = mid + 1;
221 else if (ret > 0)
222 high = mid;
223 else {
224 *slot = mid;
225 return 0;
226 }
227 }
228 *slot = low;
229 return 1;
230}
231
Chris Mason97571fd2007-02-24 13:39:08 -0500232/*
233 * simple bin_search frontend that does the right thing for
234 * leaves vs nodes
235 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400236static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500237{
Chris Mason7518a232007-03-12 12:01:18 -0400238 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400239 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400240 return generic_bin_search((void *)l->items,
241 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400242 key, btrfs_header_nritems(&c->header),
243 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500244 } else {
Chris Masone2fa7222007-03-12 16:22:34 -0400245 return generic_bin_search((void *)c->keys,
246 sizeof(struct btrfs_disk_key),
Chris Mason7518a232007-03-12 12:01:18 -0400247 key, btrfs_header_nritems(&c->header),
248 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500249 }
250 return -1;
251}
252
Chris Mason9aca1d52007-03-13 11:09:37 -0400253static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -0400254 struct btrfs_buffer *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500255 int slot)
256{
Chris Mason234b63a2007-03-13 10:46:10 -0400257 struct btrfs_node *node = &parent_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500258 if (slot < 0)
259 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400260 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500261 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400262 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500263}
264
Chris Mason234b63a2007-03-13 10:46:10 -0400265static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonbb803952007-03-01 12:04:21 -0500266 int level)
267{
Chris Mason234b63a2007-03-13 10:46:10 -0400268 struct btrfs_buffer *right_buf;
269 struct btrfs_buffer *mid_buf;
270 struct btrfs_buffer *left_buf;
271 struct btrfs_buffer *parent_buf = NULL;
272 struct btrfs_node *right = NULL;
273 struct btrfs_node *mid;
274 struct btrfs_node *left = NULL;
275 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500276 int ret = 0;
277 int wret;
278 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500279 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500280 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500281
282 if (level == 0)
283 return 0;
284
285 mid_buf = path->nodes[level];
286 mid = &mid_buf->node;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400287 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500288
Chris Mason234b63a2007-03-13 10:46:10 -0400289 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500290 parent_buf = path->nodes[level + 1];
291 pslot = path->slots[level + 1];
292
293 if (!parent_buf) {
Chris Mason234b63a2007-03-13 10:46:10 -0400294 struct btrfs_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500295 u64 blocknr = mid_buf->blocknr;
296
Chris Mason7518a232007-03-12 12:01:18 -0400297 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500298 return 0;
299
300 /* promote the child to a root */
301 child = read_node_slot(root, mid_buf, 0);
302 BUG_ON(!child);
303 root->node = child;
304 path->nodes[level] = NULL;
305 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400306 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500307 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400308 btrfs_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500309 clean_tree_block(root, mid_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400310 return btrfs_free_extent(root, blocknr, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500311 }
312 parent = &parent_buf->node;
313
Chris Mason7518a232007-03-12 12:01:18 -0400314 if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500315 return 0;
316
Chris Masonbb803952007-03-01 12:04:21 -0500317 left_buf = read_node_slot(root, parent_buf, pslot - 1);
318 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500319
320 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500321 if (left_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500322 btrfs_cow_block(root, left_buf, parent_buf,
323 pslot - 1, &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500324 left = &left_buf->node;
Chris Mason7518a232007-03-12 12:01:18 -0400325 orig_slot += btrfs_header_nritems(&left->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500326 wret = push_node_left(root, left_buf, mid_buf);
327 if (wret < 0)
328 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500329 }
Chris Mason79f95c82007-03-01 15:16:26 -0500330
331 /*
332 * then try to empty the right most buffer into the middle
333 */
Chris Masonbb803952007-03-01 12:04:21 -0500334 if (right_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500335 btrfs_cow_block(root, right_buf, parent_buf,
336 pslot + 1, &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500337 right = &right_buf->node;
338 wret = push_node_left(root, mid_buf, right_buf);
339 if (wret < 0)
340 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400341 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masonbb803952007-03-01 12:04:21 -0500342 u64 blocknr = right_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400343 btrfs_block_release(root, right_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500344 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500345 right_buf = NULL;
346 right = NULL;
347 wret = del_ptr(root, path, level + 1, pslot + 1);
348 if (wret)
349 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400350 wret = btrfs_free_extent(root, blocknr, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500351 if (wret)
352 ret = wret;
353 } else {
354 memcpy(parent->keys + pslot + 1, right->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400355 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500356 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500357 }
358 }
Chris Mason7518a232007-03-12 12:01:18 -0400359 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500360 /*
361 * we're not allowed to leave a node with one item in the
362 * tree during a delete. A deletion from lower in the tree
363 * could try to delete the only pointer in this node.
364 * So, pull some keys from the left.
365 * There has to be a left pointer at this point because
366 * otherwise we would have pulled some pointers from the
367 * right
368 */
369 BUG_ON(!left_buf);
370 wret = balance_node_right(root, mid_buf, left_buf);
371 if (wret < 0)
372 ret = wret;
373 BUG_ON(wret == 1);
374 }
Chris Mason7518a232007-03-12 12:01:18 -0400375 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500376 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500377 u64 blocknr = mid_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400378 btrfs_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500379 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500380 mid_buf = NULL;
381 mid = NULL;
382 wret = del_ptr(root, path, level + 1, pslot);
383 if (wret)
384 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400385 wret = btrfs_free_extent(root, blocknr, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500386 if (wret)
387 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500388 } else {
389 /* update the parent key to reflect our changes */
Chris Masone2fa7222007-03-12 16:22:34 -0400390 memcpy(parent->keys + pslot, mid->keys,
391 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500392 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500393 }
Chris Masonbb803952007-03-01 12:04:21 -0500394
Chris Mason79f95c82007-03-01 15:16:26 -0500395 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500396 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400397 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500398 left_buf->count++; // released below
399 path->nodes[level] = left_buf;
400 path->slots[level + 1] -= 1;
401 path->slots[level] = orig_slot;
402 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400403 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500404 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400405 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500406 path->slots[level] = orig_slot;
407 }
408 }
Chris Mason79f95c82007-03-01 15:16:26 -0500409 /* double check we haven't messed things up */
410 check_block(path, level);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400411 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
412 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500413 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500414
415 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400416 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500417 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400418 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500419 return ret;
420}
421
Chris Mason74123bd2007-02-02 11:05:29 -0500422/*
423 * look for key in the tree. path is filled in with nodes along the way
424 * if key is found, we return zero and you can find the item in the leaf
425 * level of the path (level 0)
426 *
427 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500428 * be inserted, and 1 is returned. If there are other errors during the
429 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500430 *
431 * if ins_len > 0, nodes and leaves will be split as we walk down the
432 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
433 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500434 */
Chris Mason234b63a2007-03-13 10:46:10 -0400435int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
436 struct btrfs_path *p, int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500437{
Chris Mason234b63a2007-03-13 10:46:10 -0400438 struct btrfs_buffer *b;
439 struct btrfs_buffer *cow_buf;
440 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500441 int slot;
442 int ret;
443 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500444
Chris Masonbb803952007-03-01 12:04:21 -0500445again:
446 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500447 b->count++;
448 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400449 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500450 if (cow) {
451 int wret;
452 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
453 p->slots[level + 1], &cow_buf);
454 b = cow_buf;
455 }
456 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500457 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500458 p->nodes[level] = b;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500459 ret = check_block(p, level);
460 if (ret)
461 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500462 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400463 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500464 if (ret && slot > 0)
465 slot -= 1;
466 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400467 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
468 NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500469 int sret = split_node(root, p, level);
470 BUG_ON(sret > 0);
471 if (sret)
472 return sret;
473 b = p->nodes[level];
474 c = &b->node;
475 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500476 } else if (ins_len < 0) {
477 int sret = balance_level(root, p, level);
478 if (sret)
479 return sret;
480 b = p->nodes[level];
481 if (!b)
482 goto again;
483 c = &b->node;
484 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400485 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500486 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400487 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500488 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400489 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500490 p->slots[level] = slot;
Chris Mason234b63a2007-03-13 10:46:10 -0400491 if (ins_len > 0 && btrfs_leaf_free_space(l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400492 sizeof(struct btrfs_item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500493 int sret = split_leaf(root, p, ins_len);
494 BUG_ON(sret > 0);
495 if (sret)
496 return sret;
497 }
Chris Masonbb803952007-03-01 12:04:21 -0500498 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500499 return ret;
500 }
501 }
Chris Masonbb803952007-03-01 12:04:21 -0500502 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500503 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500504}
505
Chris Mason74123bd2007-02-02 11:05:29 -0500506/*
507 * adjust the pointers going up the tree, starting at level
508 * making sure the right key of each node is points to 'key'.
509 * This is used after shifting pointers to the left, so it stops
510 * fixing up pointers when a given leaf/node is not in slot 0 of the
511 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500512 *
513 * If this fails to write a tree block, it returns -1, but continues
514 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500515 */
Chris Mason234b63a2007-03-13 10:46:10 -0400516static int fixup_low_keys(struct btrfs_root *root,
517 struct btrfs_path *path, struct btrfs_disk_key *key,
Chris Masoneb60cea2007-02-02 09:18:22 -0500518 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500519{
520 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500521 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400522 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
523 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500524 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500525 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500526 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500527 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500528 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500529 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500530 if (tslot != 0)
531 break;
532 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500533 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500534}
535
Chris Mason74123bd2007-02-02 11:05:29 -0500536/*
537 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500538 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500539 *
540 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
541 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500542 */
Chris Mason234b63a2007-03-13 10:46:10 -0400543static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
544 struct btrfs_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500545{
Chris Mason234b63a2007-03-13 10:46:10 -0400546 struct btrfs_node *src = &src_buf->node;
547 struct btrfs_node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500549 int src_nritems;
550 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500551 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500552
Chris Mason7518a232007-03-12 12:01:18 -0400553 src_nritems = btrfs_header_nritems(&src->header);
554 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Masonbb803952007-03-01 12:04:21 -0500555 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500556 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500557 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500558 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500559
560 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500561 push_items = src_nritems;
562
Chris Masonbb803952007-03-01 12:04:21 -0500563 memcpy(dst->keys + dst_nritems, src->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400564 push_items * sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500565 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500566 push_items * sizeof(u64));
Chris Masonbb803952007-03-01 12:04:21 -0500567 if (push_items < src_nritems) {
568 memmove(src->keys, src->keys + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400569 (src_nritems - push_items) *
570 sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500571 memmove(src->blockptrs, src->blockptrs + push_items,
572 (src_nritems - push_items) * sizeof(u64));
573 }
Chris Mason7518a232007-03-12 12:01:18 -0400574 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
575 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500576 BUG_ON(list_empty(&src_buf->dirty));
577 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500578 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500579}
580
Chris Mason97571fd2007-02-24 13:39:08 -0500581/*
Chris Mason79f95c82007-03-01 15:16:26 -0500582 * try to push data from one node into the next node right in the
583 * tree.
584 *
585 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
586 * error, and > 0 if there was no room in the right hand block.
587 *
588 * this will only push up to 1/2 the contents of the left node over
589 */
Chris Mason234b63a2007-03-13 10:46:10 -0400590static int balance_node_right(struct btrfs_root *root,
591 struct btrfs_buffer *dst_buf,
592 struct btrfs_buffer *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500593{
Chris Mason234b63a2007-03-13 10:46:10 -0400594 struct btrfs_node *src = &src_buf->node;
595 struct btrfs_node *dst = &dst_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500596 int push_items = 0;
597 int max_push;
598 int src_nritems;
599 int dst_nritems;
600 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500601
Chris Mason7518a232007-03-12 12:01:18 -0400602 src_nritems = btrfs_header_nritems(&src->header);
603 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500604 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
605 if (push_items <= 0) {
606 return 1;
607 }
608
609 max_push = src_nritems / 2 + 1;
610 /* don't try to empty the node */
611 if (max_push > src_nritems)
612 return 1;
613 if (max_push < push_items)
614 push_items = max_push;
615
616 memmove(dst->keys + push_items, dst->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400617 dst_nritems * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500618 memmove(dst->blockptrs + push_items, dst->blockptrs,
619 dst_nritems * sizeof(u64));
620 memcpy(dst->keys, src->keys + src_nritems - push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400621 push_items * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500622 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
623 push_items * sizeof(u64));
624
Chris Mason7518a232007-03-12 12:01:18 -0400625 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
626 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500627
Chris Mason02217ed2007-03-02 16:08:05 -0500628 BUG_ON(list_empty(&src_buf->dirty));
629 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500630 return ret;
631}
632
633/*
Chris Mason97571fd2007-02-24 13:39:08 -0500634 * helper function to insert a new root level in the tree.
635 * A new node is allocated, and a single item is inserted to
636 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500637 *
638 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500639 */
Chris Mason234b63a2007-03-13 10:46:10 -0400640static int insert_new_root(struct btrfs_root *root,
641 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500642{
Chris Mason234b63a2007-03-13 10:46:10 -0400643 struct btrfs_buffer *t;
644 struct btrfs_node *lower;
645 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400646 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500647
648 BUG_ON(path->nodes[level]);
649 BUG_ON(path->nodes[level-1] != root->node);
650
Chris Mason234b63a2007-03-13 10:46:10 -0400651 t = btrfs_alloc_free_block(root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500652 c = &t->node;
653 memset(c, 0, sizeof(c));
Chris Mason7518a232007-03-12 12:01:18 -0400654 btrfs_set_header_nritems(&c->header, 1);
655 btrfs_set_header_level(&c->header, level);
656 btrfs_set_header_blocknr(&c->header, t->blocknr);
657 btrfs_set_header_parentid(&c->header,
658 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500659 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400660 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400661 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500662 else
663 lower_key = lower->keys;
Chris Masone2fa7222007-03-12 16:22:34 -0400664 memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400665 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
Chris Mason5c680ed2007-02-22 11:39:13 -0500666 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400667 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500668 root->node = t;
669 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500670 path->nodes[level] = t;
671 path->slots[level] = 0;
672 return 0;
673}
674
Chris Mason74123bd2007-02-02 11:05:29 -0500675/*
676 * worker function to insert a single pointer in a node.
677 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500678 *
Chris Mason74123bd2007-02-02 11:05:29 -0500679 * slot and level indicate where you want the key to go, and
680 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500681 *
682 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500683 */
Chris Mason234b63a2007-03-13 10:46:10 -0400684static int insert_ptr(struct btrfs_root *root,
685 struct btrfs_path *path, struct btrfs_disk_key *key,
Chris Mason74123bd2007-02-02 11:05:29 -0500686 u64 blocknr, int slot, int level)
687{
Chris Mason234b63a2007-03-13 10:46:10 -0400688 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500689 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500690
691 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500692 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400693 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500694 if (slot > nritems)
695 BUG();
696 if (nritems == NODEPTRS_PER_BLOCK)
697 BUG();
698 if (slot != nritems) {
699 memmove(lower->keys + slot + 1, lower->keys + slot,
Chris Masone2fa7222007-03-12 16:22:34 -0400700 (nritems - slot) * sizeof(struct btrfs_disk_key));
Chris Mason74123bd2007-02-02 11:05:29 -0500701 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
702 (nritems - slot) * sizeof(u64));
703 }
Chris Masone2fa7222007-03-12 16:22:34 -0400704 memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400705 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400706 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason74123bd2007-02-02 11:05:29 -0500707 if (lower->keys[1].objectid == 0)
708 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -0500709 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500710 return 0;
711}
712
Chris Mason97571fd2007-02-24 13:39:08 -0500713/*
714 * split the node at the specified level in path in two.
715 * The path is corrected to point to the appropriate node after the split
716 *
717 * Before splitting this tries to make some room in the node by pushing
718 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500719 *
720 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500721 */
Chris Mason234b63a2007-03-13 10:46:10 -0400722static int split_node(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500723 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724{
Chris Mason234b63a2007-03-13 10:46:10 -0400725 struct btrfs_buffer *t;
726 struct btrfs_node *c;
727 struct btrfs_buffer *split_buffer;
728 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500729 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500730 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400732 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500733
Chris Mason5c680ed2007-02-22 11:39:13 -0500734 t = path->nodes[level];
735 c = &t->node;
736 if (t == root->node) {
737 /* trying to split the root, lets make a new one */
738 ret = insert_new_root(root, path, level + 1);
739 if (ret)
740 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500741 }
Chris Mason7518a232007-03-12 12:01:18 -0400742 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason234b63a2007-03-13 10:46:10 -0400743 split_buffer = btrfs_alloc_free_block(root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500744 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400745 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
746 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
747 btrfs_set_header_parentid(&split->header,
748 btrfs_header_parentid(&root->node->node.header));
749 mid = (c_nritems + 1) / 2;
Chris Mason5c680ed2007-02-22 11:39:13 -0500750 memcpy(split->keys, c->keys + mid,
Chris Masone2fa7222007-03-12 16:22:34 -0400751 (c_nritems - mid) * sizeof(struct btrfs_disk_key));
Chris Mason5c680ed2007-02-22 11:39:13 -0500752 memcpy(split->blockptrs, c->blockptrs + mid,
Chris Mason7518a232007-03-12 12:01:18 -0400753 (c_nritems - mid) * sizeof(u64));
754 btrfs_set_header_nritems(&split->header, c_nritems - mid);
755 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500756 ret = 0;
757
Chris Mason02217ed2007-03-02 16:08:05 -0500758 BUG_ON(list_empty(&t->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500759 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
760 path->slots[level + 1] + 1, level + 1);
761 if (wret)
762 ret = wret;
763
Chris Mason5de08d72007-02-24 06:24:44 -0500764 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500765 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400766 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500767 path->nodes[level] = split_buffer;
768 path->slots[level + 1] += 1;
769 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400770 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500771 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500772 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500773}
774
Chris Mason74123bd2007-02-02 11:05:29 -0500775/*
776 * how many bytes are required to store the items in a leaf. start
777 * and nr indicate which items in the leaf to check. This totals up the
778 * space used both by the item structs and the item data
779 */
Chris Mason234b63a2007-03-13 10:46:10 -0400780static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500781{
782 int data_len;
783 int end = start + nr - 1;
784
785 if (!nr)
786 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400787 data_len = btrfs_item_end(l->items + start);
788 data_len = data_len - btrfs_item_offset(l->items + end);
789 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500790 return data_len;
791}
792
Chris Mason74123bd2007-02-02 11:05:29 -0500793/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500794 * push some data in the path leaf to the right, trying to free up at
795 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500796 *
797 * returns 1 if the push failed because the other node didn't have enough
798 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500799 */
Chris Mason234b63a2007-03-13 10:46:10 -0400800static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500801 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500802{
Chris Mason234b63a2007-03-13 10:46:10 -0400803 struct btrfs_buffer *left_buf = path->nodes[0];
804 struct btrfs_leaf *left = &left_buf->leaf;
805 struct btrfs_leaf *right;
806 struct btrfs_buffer *right_buf;
807 struct btrfs_buffer *upper;
Chris Mason00ec4c52007-02-24 12:47:20 -0500808 int slot;
809 int i;
810 int free_space;
811 int push_space = 0;
812 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400813 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400814 u32 left_nritems;
815 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500816
817 slot = path->slots[1];
818 if (!path->nodes[1]) {
819 return 1;
820 }
821 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400822 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500823 return 1;
824 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400825 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
826 slot + 1));
Chris Mason00ec4c52007-02-24 12:47:20 -0500827 right = &right_buf->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -0400828 free_space = btrfs_leaf_free_space(right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400829 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400830 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500831 return 1;
832 }
Chris Mason02217ed2007-03-02 16:08:05 -0500833 /* cow and double check */
834 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
835 right = &right_buf->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -0400836 free_space = btrfs_leaf_free_space(right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400837 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400838 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500839 return 1;
840 }
841
Chris Mason7518a232007-03-12 12:01:18 -0400842 left_nritems = btrfs_header_nritems(&left->header);
843 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500844 item = left->items + i;
845 if (path->slots[0] == i)
846 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400847 if (btrfs_item_size(item) + sizeof(*item) + push_space >
848 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500849 break;
850 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400851 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500852 }
853 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400854 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500855 return 1;
856 }
Chris Mason7518a232007-03-12 12:01:18 -0400857 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500858 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400859 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason00ec4c52007-02-24 12:47:20 -0500860 push_space -= leaf_data_end(left);
861 /* make room in the right data area */
862 memmove(right->data + leaf_data_end(right) - push_space,
863 right->data + leaf_data_end(right),
864 LEAF_DATA_SIZE - leaf_data_end(right));
865 /* copy from the left data area */
866 memcpy(right->data + LEAF_DATA_SIZE - push_space,
867 left->data + leaf_data_end(left),
868 push_space);
869 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400870 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500871 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400872 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400873 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500874
875 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400876 right_nritems += push_items;
877 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500878 push_space = LEAF_DATA_SIZE;
Chris Mason7518a232007-03-12 12:01:18 -0400879 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400880 btrfs_set_item_offset(right->items + i, push_space -
881 btrfs_item_size(right->items + i));
882 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500883 }
Chris Mason7518a232007-03-12 12:01:18 -0400884 left_nritems -= push_items;
885 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500886
Chris Mason02217ed2007-03-02 16:08:05 -0500887 BUG_ON(list_empty(&left_buf->dirty));
888 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason00ec4c52007-02-24 12:47:20 -0500889 memcpy(upper->node.keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -0400890 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500891 BUG_ON(list_empty(&upper->dirty));
892
Chris Mason00ec4c52007-02-24 12:47:20 -0500893 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400894 if (path->slots[0] >= left_nritems) {
895 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400896 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500897 path->nodes[0] = right_buf;
898 path->slots[1] += 1;
899 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400900 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500901 }
902 return 0;
903}
904/*
Chris Mason74123bd2007-02-02 11:05:29 -0500905 * push some data in the path leaf to the left, trying to free up at
906 * least data_size bytes. returns zero if the push worked, nonzero otherwise
907 */
Chris Mason234b63a2007-03-13 10:46:10 -0400908static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500909 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910{
Chris Mason234b63a2007-03-13 10:46:10 -0400911 struct btrfs_buffer *right_buf = path->nodes[0];
912 struct btrfs_leaf *right = &right_buf->leaf;
913 struct btrfs_buffer *t;
914 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500915 int slot;
916 int i;
917 int free_space;
918 int push_space = 0;
919 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400920 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400921 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500922 int ret = 0;
923 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500924
925 slot = path->slots[1];
926 if (slot == 0) {
927 return 1;
928 }
929 if (!path->nodes[1]) {
930 return 1;
931 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400932 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
933 slot - 1));
Chris Masoneb60cea2007-02-02 09:18:22 -0500934 left = &t->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -0400935 free_space = btrfs_leaf_free_space(left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400936 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400937 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500938 return 1;
939 }
Chris Mason02217ed2007-03-02 16:08:05 -0500940
941 /* cow and double check */
942 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
943 left = &t->leaf;
Chris Mason234b63a2007-03-13 10:46:10 -0400944 free_space = btrfs_leaf_free_space(left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400945 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400946 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500947 return 1;
948 }
949
Chris Mason7518a232007-03-12 12:01:18 -0400950 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500951 item = right->items + i;
952 if (path->slots[0] == i)
953 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400954 if (btrfs_item_size(item) + sizeof(*item) + push_space >
955 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500956 break;
957 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400958 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500959 }
960 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400961 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500962 return 1;
963 }
964 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400965 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400966 right->items, push_items * sizeof(struct btrfs_item));
967 push_space = LEAF_DATA_SIZE -
968 btrfs_item_offset(right->items + push_items -1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 memcpy(left->data + leaf_data_end(left) - push_space,
Chris Mason0783fcf2007-03-12 20:12:07 -0400970 right->data + btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500971 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400972 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500973 BUG_ON(old_left_nritems < 0);
974
Chris Mason0783fcf2007-03-12 20:12:07 -0400975 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
976 u16 ioff = btrfs_item_offset(left->items + i);
977 btrfs_set_item_offset(left->items + i, ioff - (LEAF_DATA_SIZE -
978 btrfs_item_offset(left->items +
979 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500980 }
Chris Mason7518a232007-03-12 12:01:18 -0400981 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500982
983 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400984 push_space = btrfs_item_offset(right->items + push_items - 1) -
985 leaf_data_end(right);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500986 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
987 leaf_data_end(right), push_space);
988 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400989 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400990 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400991 btrfs_set_header_nritems(&right->header,
992 btrfs_header_nritems(&right->header) -
993 push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500994 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500995
Chris Mason7518a232007-03-12 12:01:18 -0400996 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400997 btrfs_set_item_offset(right->items + i, push_space -
998 btrfs_item_size(right->items + i));
999 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001000 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001001
Chris Mason02217ed2007-03-02 16:08:05 -05001002 BUG_ON(list_empty(&t->dirty));
1003 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001004
Chris Masonaa5d6be2007-02-28 16:35:06 -05001005 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
1006 if (wret)
1007 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001008
1009 /* then fixup the leaf pointer in the path */
1010 if (path->slots[0] < push_items) {
1011 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001012 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001013 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001014 path->slots[1] -= 1;
1015 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001016 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001017 path->slots[0] -= push_items;
1018 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001019 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001020 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001021}
1022
Chris Mason74123bd2007-02-02 11:05:29 -05001023/*
1024 * split the path's leaf in two, making sure there is at least data_size
1025 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001026 *
1027 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001028 */
Chris Mason234b63a2007-03-13 10:46:10 -04001029static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -05001030 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001031{
Chris Mason234b63a2007-03-13 10:46:10 -04001032 struct btrfs_buffer *l_buf;
1033 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001034 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001035 int mid;
1036 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001037 struct btrfs_leaf *right;
1038 struct btrfs_buffer *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001039 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001040 int data_copy_size;
1041 int rt_data_off;
1042 int i;
1043 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001044 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001045
Chris Masonaa5d6be2007-02-28 16:35:06 -05001046 l_buf = path->nodes[0];
1047 l = &l_buf->leaf;
1048
1049 /* did the pushes work? */
Chris Mason234b63a2007-03-13 10:46:10 -04001050 if (btrfs_leaf_free_space(l) >= sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001051 return 0;
1052
Chris Mason5c680ed2007-02-22 11:39:13 -05001053 if (!path->nodes[1]) {
1054 ret = insert_new_root(root, path, 1);
1055 if (ret)
1056 return ret;
1057 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001058 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001059 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001060 mid = (nritems + 1)/ 2;
Chris Mason234b63a2007-03-13 10:46:10 -04001061 right_buffer = btrfs_alloc_free_block(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001062 BUG_ON(!right_buffer);
1063 BUG_ON(mid == nritems);
1064 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001065 memset(right, 0, sizeof(*right));
1066 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001067 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001068 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1069 LEAF_DATA_SIZE)
1070 BUG();
1071 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001072 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001073 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1074 LEAF_DATA_SIZE)
1075 BUG();
1076 }
Chris Mason7518a232007-03-12 12:01:18 -04001077 btrfs_set_header_nritems(&right->header, nritems - mid);
1078 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1079 btrfs_set_header_level(&right->header, 0);
1080 btrfs_set_header_parentid(&right->header,
1081 btrfs_header_parentid(&root->node->node.header));
Chris Mason0783fcf2007-03-12 20:12:07 -04001082 data_copy_size = btrfs_item_end(l->items + mid) - leaf_data_end(l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001083 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001084 (nritems - mid) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001085 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1086 l->data + leaf_data_end(l), data_copy_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04001087 rt_data_off = LEAF_DATA_SIZE - btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001088
Chris Mason0783fcf2007-03-12 20:12:07 -04001089 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1090 u16 ioff = btrfs_item_offset(right->items + i);
1091 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1092 }
Chris Mason74123bd2007-02-02 11:05:29 -05001093
Chris Mason7518a232007-03-12 12:01:18 -04001094 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001095 ret = 0;
1096 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001097 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001098 if (wret)
1099 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001100 BUG_ON(list_empty(&right_buffer->dirty));
1101 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001102 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001103 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001104 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001105 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001106 path->slots[0] -= mid;
1107 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001108 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001109 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001110 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001111 return ret;
1112}
1113
Chris Mason74123bd2007-02-02 11:05:29 -05001114/*
1115 * Given a key and some data, insert an item into the tree.
1116 * This does all the path init required, making room in the tree if needed.
1117 */
Chris Mason234b63a2007-03-13 10:46:10 -04001118int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001119 void *data, int data_size)
1120{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001121 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001122 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001123 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001124 struct btrfs_leaf *leaf;
1125 struct btrfs_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001126 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001127 unsigned int data_end;
Chris Mason234b63a2007-03-13 10:46:10 -04001128 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -04001129 struct btrfs_disk_key disk_key;
1130
1131 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001132
Chris Mason74123bd2007-02-02 11:05:29 -05001133 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001134 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001135 BUG();
Chris Mason234b63a2007-03-13 10:46:10 -04001136 btrfs_init_path(&path);
1137 ret = btrfs_search_slot(root, cpu_key, &path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001138 if (ret == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001139 btrfs_release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001140 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001141 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001142 if (ret < 0)
1143 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001144
Chris Masoneb60cea2007-02-02 09:18:22 -05001145 slot_orig = path.slots[0];
1146 leaf_buf = path.nodes[0];
1147 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001148
Chris Mason7518a232007-03-12 12:01:18 -04001149 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001150 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001151
Chris Mason234b63a2007-03-13 10:46:10 -04001152 if (btrfs_leaf_free_space(leaf) <
1153 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001154 BUG();
1155
1156 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001157 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001158 if (slot != nritems) {
1159 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001160 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001161
1162 /*
1163 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1164 */
1165 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001166 for (i = slot; i < nritems; i++) {
1167 u16 ioff = btrfs_item_offset(leaf->items + i);
1168 btrfs_set_item_offset(leaf->items + i,
1169 ioff - data_size);
1170 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001171
1172 /* shift the items */
1173 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001174 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001175
1176 /* shift the data */
1177 memmove(leaf->data + data_end - data_size, leaf->data +
1178 data_end, old_data - data_end);
1179 data_end = old_data;
1180 }
Chris Mason74123bd2007-02-02 11:05:29 -05001181 /* copy the new data in */
Chris Masone2fa7222007-03-12 16:22:34 -04001182 memcpy(&leaf->items[slot].key, &disk_key,
1183 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001184 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1185 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001186 memcpy(leaf->data + data_end - data_size, data, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001187 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001188
1189 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001190 if (slot == 0)
Chris Masone2fa7222007-03-12 16:22:34 -04001191 ret = fixup_low_keys(root, &path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001192
Chris Mason02217ed2007-03-02 16:08:05 -05001193 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -04001194 if (btrfs_leaf_free_space(leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001195 BUG();
Chris Masonbb803952007-03-01 12:04:21 -05001196 check_leaf(&path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001197out:
Chris Mason234b63a2007-03-13 10:46:10 -04001198 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001199 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001200}
1201
Chris Mason74123bd2007-02-02 11:05:29 -05001202/*
Chris Mason5de08d72007-02-24 06:24:44 -05001203 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001204 *
1205 * If the delete empties a node, the node is removed from the tree,
1206 * continuing all the way the root if required. The root is converted into
1207 * a leaf if all the nodes are emptied.
1208 */
Chris Mason234b63a2007-03-13 10:46:10 -04001209static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
Chris Masonbb803952007-03-01 12:04:21 -05001210 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001211{
Chris Mason234b63a2007-03-13 10:46:10 -04001212 struct btrfs_node *node;
1213 struct btrfs_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001214 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001215 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001216 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001217
Chris Masonbb803952007-03-01 12:04:21 -05001218 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001219 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001220 if (slot != nritems -1) {
1221 memmove(node->keys + slot, node->keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -04001222 sizeof(struct btrfs_disk_key) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001223 memmove(node->blockptrs + slot,
1224 node->blockptrs + slot + 1,
1225 sizeof(u64) * (nritems - slot - 1));
1226 }
Chris Mason7518a232007-03-12 12:01:18 -04001227 nritems--;
1228 btrfs_set_header_nritems(&node->header, nritems);
1229 if (nritems == 0 && parent == root->node) {
1230 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001231 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001232 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001233 } else if (slot == 0) {
1234 wret = fixup_low_keys(root, path, node->keys, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001235 if (wret)
1236 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001237 }
Chris Mason02217ed2007-03-02 16:08:05 -05001238 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001239 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001240}
1241
Chris Mason74123bd2007-02-02 11:05:29 -05001242/*
1243 * delete the item at the leaf level in path. If that empties
1244 * the leaf, remove it from the tree
1245 */
Chris Mason234b63a2007-03-13 10:46:10 -04001246int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001248 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001249 struct btrfs_leaf *leaf;
1250 struct btrfs_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001251 int doff;
1252 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001253 int ret = 0;
1254 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001255 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256
Chris Masoneb60cea2007-02-02 09:18:22 -05001257 leaf_buf = path->nodes[0];
1258 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001259 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001260 doff = btrfs_item_offset(leaf->items + slot);
1261 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001262 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001263
Chris Mason7518a232007-03-12 12:01:18 -04001264 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001265 int i;
1266 int data_end = leaf_data_end(leaf);
1267 memmove(leaf->data + data_end + dsize,
1268 leaf->data + data_end,
1269 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001270 for (i = slot + 1; i < nritems; i++) {
1271 u16 ioff = btrfs_item_offset(leaf->items + i);
1272 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1273 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001274 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001275 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001276 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001277 }
Chris Mason7518a232007-03-12 12:01:18 -04001278 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1279 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001280 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001281 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001282 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001283 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001284 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001285 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001286 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001287 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001288 if (wret)
1289 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001290 wret = btrfs_free_extent(root, leaf_buf->blocknr, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001291 if (wret)
1292 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001293 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001294 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001295 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001296 if (slot == 0) {
1297 wret = fixup_low_keys(root, path,
1298 &leaf->items[0].key, 1);
1299 if (wret)
1300 ret = wret;
1301 }
Chris Mason02217ed2007-03-02 16:08:05 -05001302 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001303
Chris Mason74123bd2007-02-02 11:05:29 -05001304 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -05001305 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001306 /* push_leaf_left fixes the path.
1307 * make sure the path still points to our leaf
1308 * for possible call to del_ptr below
1309 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001310 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001311 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001312 wret = push_leaf_left(root, path, 1);
1313 if (wret < 0)
1314 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001315 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001316 btrfs_header_nritems(&leaf->header)) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001317 wret = push_leaf_right(root, path, 1);
1318 if (wret < 0)
1319 ret = wret;
1320 }
Chris Mason7518a232007-03-12 12:01:18 -04001321 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001322 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001323 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001324 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001325 if (wret)
1326 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001327 btrfs_block_release(root, leaf_buf);
1328 wret = btrfs_free_extent(root, blocknr, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001329 if (wret)
1330 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001331 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001332 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001333 }
1334 }
1335 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001336 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001337}
1338
Chris Mason97571fd2007-02-24 13:39:08 -05001339/*
1340 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001341 * returns 0 if it found something or 1 if there are no greater leaves.
1342 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001343 */
Chris Mason234b63a2007-03-13 10:46:10 -04001344int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001345{
1346 int slot;
1347 int level = 1;
1348 u64 blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -04001349 struct btrfs_buffer *c;
1350 struct btrfs_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001351
Chris Mason234b63a2007-03-13 10:46:10 -04001352 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001353 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001354 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001355 slot = path->slots[level] + 1;
1356 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001357 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001358 level++;
1359 continue;
1360 }
Chris Mason1d4f8a02007-03-13 09:28:32 -04001361 blocknr = btrfs_node_blockptr(&c->node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001362 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001363 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001364 next = read_tree_block(root, blocknr);
1365 break;
1366 }
1367 path->slots[level] = slot;
1368 while(1) {
1369 level--;
1370 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001371 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001372 path->nodes[level] = next;
1373 path->slots[level] = 0;
1374 if (!level)
1375 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001376 next = read_tree_block(root,
1377 btrfs_node_blockptr(&next->node, 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001378 }
1379 return 0;
1380}
1381
Chris Mason02217ed2007-03-02 16:08:05 -05001382