blob: 48c611948d11969c42642649c4da20fcb79b74f4 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05002#include "ctree.h"
3#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -04004#include "transaction.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05005
Chris Masone089f052007-03-16 16:20:31 -04006static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
7 *root, struct btrfs_path *path, int level);
8static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
9 *root, struct btrfs_path *path, int data_size);
10static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040011 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040012 *src);
13static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040014 btrfs_root *root, struct buffer_head *dst_buf,
15 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040016static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
17 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050018
Chris Mason234b63a2007-03-13 10:46:10 -040019inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050020{
21 memset(p, 0, sizeof(*p));
22}
23
Chris Mason234b63a2007-03-13 10:46:10 -040024void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050025{
26 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040027 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050028 if (!p->nodes[i])
29 break;
Chris Mason234b63a2007-03-13 10:46:10 -040030 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050031 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050032 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050033}
34
Chris Masone089f052007-03-16 16:20:31 -040035static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040036 *root, struct buffer_head *buf, struct buffer_head
37 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040038 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050039{
Chris Masone20d96d2007-03-22 12:13:20 -040040 struct buffer_head *cow;
41 struct btrfs_node *cow_node;
Chris Mason02217ed2007-03-02 16:08:05 -050042
Chris Mason7f5c1512007-03-23 15:56:19 -040043 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
44 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -050045 *cow_ret = buf;
46 return 0;
47 }
Chris Masone089f052007-03-16 16:20:31 -040048 cow = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -040049 cow_node = btrfs_buffer_node(cow);
50 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
51 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -040052 btrfs_set_header_generation(&cow_node->header, trans->transid);
Chris Mason02217ed2007-03-02 16:08:05 -050053 *cow_ret = cow;
Chris Masond5719762007-03-23 10:01:08 -040054 mark_buffer_dirty(cow);
Chris Masone089f052007-03-16 16:20:31 -040055 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050056 if (buf == root->node) {
57 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040058 get_bh(cow);
Chris Masona28ec192007-03-06 20:08:01 -050059 if (buf != root->commit_root)
Chris Masone20d96d2007-03-22 12:13:20 -040060 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -040061 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050062 } else {
Chris Masone20d96d2007-03-22 12:13:20 -040063 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
64 cow->b_blocknr);
Chris Masond5719762007-03-23 10:01:08 -040065 mark_buffer_dirty(parent);
Chris Masone20d96d2007-03-22 12:13:20 -040066 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050067 }
Chris Mason234b63a2007-03-13 10:46:10 -040068 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050069 return 0;
70}
71
Chris Mason74123bd2007-02-02 11:05:29 -050072/*
73 * The leaf data grows from end-to-front in the node.
74 * this returns the address of the start of the last item,
75 * which is the stop of the leaf data stack
76 */
Chris Mason123abc82007-03-14 14:14:43 -040077static inline unsigned int leaf_data_end(struct btrfs_root *root,
78 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050079{
Chris Mason7518a232007-03-12 12:01:18 -040080 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050081 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -040082 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -040083 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050084}
85
Chris Mason74123bd2007-02-02 11:05:29 -050086/*
87 * The space between the end of the leaf items and
88 * the start of the leaf data. IOW, how much room
89 * the leaf has left for both items and data
90 */
Chris Mason123abc82007-03-14 14:14:43 -040091int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050092{
Chris Mason123abc82007-03-14 14:14:43 -040093 int data_end = leaf_data_end(root, leaf);
Chris Mason7518a232007-03-12 12:01:18 -040094 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050095 char *items_end = (char *)(leaf->items + nritems + 1);
Chris Mason123abc82007-03-14 14:14:43 -040096 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050097}
98
Chris Mason74123bd2007-02-02 11:05:29 -050099/*
100 * compare two keys in a memcmp fashion
101 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400102static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500103{
Chris Masone2fa7222007-03-12 16:22:34 -0400104 struct btrfs_key k1;
105
106 btrfs_disk_key_to_cpu(&k1, disk);
107
108 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500109 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400110 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500111 return -1;
Chris Mason62e27492007-03-15 12:56:47 -0400112 if (k1.flags > k2->flags)
113 return 1;
114 if (k1.flags < k2->flags)
115 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400116 if (k1.offset > k2->offset)
117 return 1;
118 if (k1.offset < k2->offset)
119 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500120 return 0;
121}
Chris Mason74123bd2007-02-02 11:05:29 -0500122
Chris Mason123abc82007-03-14 14:14:43 -0400123static int check_node(struct btrfs_root *root, struct btrfs_path *path,
124 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500125{
126 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400127 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400128 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500129 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400130 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500131
132 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400133 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500134 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400135 BUG_ON(nritems == 0);
136 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400137 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400138 parent_key = &parent->ptrs[parent_slot].key;
139 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400140 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400141 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400142 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500143 }
Chris Mason123abc82007-03-14 14:14:43 -0400144 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400145 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400146 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400147 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
148 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500149 }
150 return 0;
151}
152
Chris Mason123abc82007-03-14 14:14:43 -0400153static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
154 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155{
156 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400157 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400158 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500159 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400160 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500161
162 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400163 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500164 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400165 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400166
167 if (nritems == 0)
168 return 0;
169
170 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400171 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400172 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500173 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400174 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400175 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400176 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500177 }
Chris Mason7518a232007-03-12 12:01:18 -0400178 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400179 struct btrfs_key cpukey;
180 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500181 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400182 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400183 BUG_ON(btrfs_item_offset(leaf->items + i) !=
184 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500185 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400186 BUG_ON(btrfs_item_offset(leaf->items + i) +
187 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400188 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500189 }
190 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500191 return 0;
192}
193
Chris Mason123abc82007-03-14 14:14:43 -0400194static int check_block(struct btrfs_root *root, struct btrfs_path *path,
195 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500196{
197 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400198 return check_leaf(root, path, level);
199 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500200}
201
Chris Mason74123bd2007-02-02 11:05:29 -0500202/*
203 * search for key in the array p. items p are item_size apart
204 * and there are 'max' items in p
205 * the slot in the array is returned via slot, and it points to
206 * the place where you would insert key if it is not found in
207 * the array.
208 *
209 * slot may point to max if the key is bigger than all of the keys
210 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400211static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500212 int max, int *slot)
213{
214 int low = 0;
215 int high = max;
216 int mid;
217 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400218 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500219
220 while(low < high) {
221 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400222 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500223 ret = comp_keys(tmp, key);
224
225 if (ret < 0)
226 low = mid + 1;
227 else if (ret > 0)
228 high = mid;
229 else {
230 *slot = mid;
231 return 0;
232 }
233 }
234 *slot = low;
235 return 1;
236}
237
Chris Mason97571fd2007-02-24 13:39:08 -0500238/*
239 * simple bin_search frontend that does the right thing for
240 * leaves vs nodes
241 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400242static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500243{
Chris Mason7518a232007-03-12 12:01:18 -0400244 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400245 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400246 return generic_bin_search((void *)l->items,
247 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400248 key, btrfs_header_nritems(&c->header),
249 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500250 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400251 return generic_bin_search((void *)c->ptrs,
252 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400253 key, btrfs_header_nritems(&c->header),
254 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500255 }
256 return -1;
257}
258
Chris Masone20d96d2007-03-22 12:13:20 -0400259static struct buffer_head *read_node_slot(struct btrfs_root *root,
260 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500261 int slot)
262{
Chris Masone20d96d2007-03-22 12:13:20 -0400263 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500264 if (slot < 0)
265 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400266 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500267 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400268 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500269}
270
Chris Masone089f052007-03-16 16:20:31 -0400271static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
272 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500273{
Chris Masone20d96d2007-03-22 12:13:20 -0400274 struct buffer_head *right_buf;
275 struct buffer_head *mid_buf;
276 struct buffer_head *left_buf;
277 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400278 struct btrfs_node *right = NULL;
279 struct btrfs_node *mid;
280 struct btrfs_node *left = NULL;
281 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500282 int ret = 0;
283 int wret;
284 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500285 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500286 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500287
288 if (level == 0)
289 return 0;
290
291 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400292 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400293 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500294
Chris Mason234b63a2007-03-13 10:46:10 -0400295 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500296 parent_buf = path->nodes[level + 1];
297 pslot = path->slots[level + 1];
298
Chris Mason40689472007-03-17 14:29:23 -0400299 /*
300 * deal with the case where there is only one pointer in the root
301 * by promoting the node below to a root
302 */
Chris Masonbb803952007-03-01 12:04:21 -0500303 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400304 struct buffer_head *child;
305 u64 blocknr = mid_buf->b_blocknr;
Chris Masonbb803952007-03-01 12:04:21 -0500306
Chris Mason7518a232007-03-12 12:01:18 -0400307 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500308 return 0;
309
310 /* promote the child to a root */
311 child = read_node_slot(root, mid_buf, 0);
312 BUG_ON(!child);
313 root->node = child;
314 path->nodes[level] = NULL;
315 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400316 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500317 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400318 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400319 clean_tree_block(trans, root, mid_buf);
320 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500321 }
Chris Masone20d96d2007-03-22 12:13:20 -0400322 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500323
Chris Mason123abc82007-03-14 14:14:43 -0400324 if (btrfs_header_nritems(&mid->header) >
325 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500326 return 0;
327
Chris Masonbb803952007-03-01 12:04:21 -0500328 left_buf = read_node_slot(root, parent_buf, pslot - 1);
329 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500330
331 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500332 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400333 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
334 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400335 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400336 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400337 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500338 if (wret < 0)
339 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500340 }
Chris Mason79f95c82007-03-01 15:16:26 -0500341
342 /*
343 * then try to empty the right most buffer into the middle
344 */
Chris Masonbb803952007-03-01 12:04:21 -0500345 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400346 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
347 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400348 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400349 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500350 if (wret < 0)
351 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400352 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -0400353 u64 blocknr = right_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400354 btrfs_block_release(root, right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400355 clean_tree_block(trans, root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500356 right_buf = NULL;
357 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400358 wret = del_ptr(trans, root, path, level + 1, pslot +
359 1);
Chris Masonbb803952007-03-01 12:04:21 -0500360 if (wret)
361 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400362 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500363 if (wret)
364 ret = wret;
365 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400366 memcpy(&parent->ptrs[pslot + 1].key,
367 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400368 sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400369 mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500370 }
371 }
Chris Mason7518a232007-03-12 12:01:18 -0400372 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500373 /*
374 * we're not allowed to leave a node with one item in the
375 * tree during a delete. A deletion from lower in the tree
376 * could try to delete the only pointer in this node.
377 * So, pull some keys from the left.
378 * There has to be a left pointer at this point because
379 * otherwise we would have pulled some pointers from the
380 * right
381 */
382 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400383 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500384 if (wret < 0)
385 ret = wret;
386 BUG_ON(wret == 1);
387 }
Chris Mason7518a232007-03-12 12:01:18 -0400388 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500389 /* we've managed to empty the middle node, drop it */
Chris Masone20d96d2007-03-22 12:13:20 -0400390 u64 blocknr = mid_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400391 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400392 clean_tree_block(trans, root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500393 mid_buf = NULL;
394 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400395 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500396 if (wret)
397 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400398 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500399 if (wret)
400 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500401 } else {
402 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400403 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400404 sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400405 mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500406 }
Chris Masonbb803952007-03-01 12:04:21 -0500407
Chris Mason79f95c82007-03-01 15:16:26 -0500408 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500409 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400410 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400411 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500412 path->nodes[level] = left_buf;
413 path->slots[level + 1] -= 1;
414 path->slots[level] = orig_slot;
415 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400416 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500417 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400418 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500419 path->slots[level] = orig_slot;
420 }
421 }
Chris Mason79f95c82007-03-01 15:16:26 -0500422 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400423 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400424 if (orig_ptr !=
425 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
426 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500427 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500428
429 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400430 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500431 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400432 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500433 return ret;
434}
435
Chris Mason74123bd2007-02-02 11:05:29 -0500436/*
437 * look for key in the tree. path is filled in with nodes along the way
438 * if key is found, we return zero and you can find the item in the leaf
439 * level of the path (level 0)
440 *
441 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500442 * be inserted, and 1 is returned. If there are other errors during the
443 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500444 *
445 * if ins_len > 0, nodes and leaves will be split as we walk down the
446 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
447 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500448 */
Chris Masone089f052007-03-16 16:20:31 -0400449int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
450 *root, struct btrfs_key *key, struct btrfs_path *p, int
451 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500452{
Chris Masone20d96d2007-03-22 12:13:20 -0400453 struct buffer_head *b;
454 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400455 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500456 int slot;
457 int ret;
458 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500459
Chris Masonbb803952007-03-01 12:04:21 -0500460again:
461 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400462 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500463 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400464 c = btrfs_buffer_node(b);
465 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500466 if (cow) {
467 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400468 wret = btrfs_cow_block(trans, root, b,
469 p->nodes[level + 1],
470 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400471 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500472 b = cow_buf;
473 }
474 BUG_ON(!cow && ins_len);
Chris Masone20d96d2007-03-22 12:13:20 -0400475 c = btrfs_buffer_node(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500476 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400477 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500478 if (ret)
479 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500480 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400481 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500482 if (ret && slot > 0)
483 slot -= 1;
484 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400485 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400486 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Masone089f052007-03-16 16:20:31 -0400487 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500488 BUG_ON(sret > 0);
489 if (sret)
490 return sret;
491 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400492 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500493 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500494 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400495 int sret = balance_level(trans, root, p,
496 level);
Chris Masonbb803952007-03-01 12:04:21 -0500497 if (sret)
498 return sret;
499 b = p->nodes[level];
500 if (!b)
501 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400502 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500503 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400504 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500505 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400506 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500507 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400508 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500509 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400510 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400511 sizeof(struct btrfs_item) + ins_len) {
Chris Masone089f052007-03-16 16:20:31 -0400512 int sret = split_leaf(trans, root, p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500513 BUG_ON(sret > 0);
514 if (sret)
515 return sret;
516 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500517 return ret;
518 }
519 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500520 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500521}
522
Chris Mason74123bd2007-02-02 11:05:29 -0500523/*
524 * adjust the pointers going up the tree, starting at level
525 * making sure the right key of each node is points to 'key'.
526 * This is used after shifting pointers to the left, so it stops
527 * fixing up pointers when a given leaf/node is not in slot 0 of the
528 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500529 *
530 * If this fails to write a tree block, it returns -1, but continues
531 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500532 */
Chris Masone089f052007-03-16 16:20:31 -0400533static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
534 *root, struct btrfs_path *path, struct btrfs_disk_key
535 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500536{
537 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500538 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400539 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
540 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500541 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500542 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500543 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400544 t = btrfs_buffer_node(path->nodes[i]);
Chris Mason123abc82007-03-14 14:14:43 -0400545 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Masond5719762007-03-23 10:01:08 -0400546 mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500547 if (tslot != 0)
548 break;
549 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500550 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500551}
552
Chris Mason74123bd2007-02-02 11:05:29 -0500553/*
554 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500555 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500556 *
557 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
558 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500559 */
Chris Masone089f052007-03-16 16:20:31 -0400560static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400561 *root, struct buffer_head *dst_buf, struct
562 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563{
Chris Masone20d96d2007-03-22 12:13:20 -0400564 struct btrfs_node *src = btrfs_buffer_node(src_buf);
565 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500566 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500567 int src_nritems;
568 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500569 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500570
Chris Mason7518a232007-03-12 12:01:18 -0400571 src_nritems = btrfs_header_nritems(&src->header);
572 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400573 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500574 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500575 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500576 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500577
578 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500579 push_items = src_nritems;
580
Chris Mason123abc82007-03-14 14:14:43 -0400581 memcpy(dst->ptrs + dst_nritems, src->ptrs,
582 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500583 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400584 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400585 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400586 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500587 }
Chris Mason7518a232007-03-12 12:01:18 -0400588 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
589 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond5719762007-03-23 10:01:08 -0400590 mark_buffer_dirty(src_buf);
591 mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500592 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500593}
594
Chris Mason97571fd2007-02-24 13:39:08 -0500595/*
Chris Mason79f95c82007-03-01 15:16:26 -0500596 * try to push data from one node into the next node right in the
597 * tree.
598 *
599 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
600 * error, and > 0 if there was no room in the right hand block.
601 *
602 * this will only push up to 1/2 the contents of the left node over
603 */
Chris Masone089f052007-03-16 16:20:31 -0400604static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400605 btrfs_root *root, struct buffer_head *dst_buf,
606 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500607{
Chris Masone20d96d2007-03-22 12:13:20 -0400608 struct btrfs_node *src = btrfs_buffer_node(src_buf);
609 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500610 int push_items = 0;
611 int max_push;
612 int src_nritems;
613 int dst_nritems;
614 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500615
Chris Mason7518a232007-03-12 12:01:18 -0400616 src_nritems = btrfs_header_nritems(&src->header);
617 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400618 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500619 if (push_items <= 0) {
620 return 1;
621 }
622
623 max_push = src_nritems / 2 + 1;
624 /* don't try to empty the node */
625 if (max_push > src_nritems)
626 return 1;
627 if (max_push < push_items)
628 push_items = max_push;
629
Chris Mason123abc82007-03-14 14:14:43 -0400630 memmove(dst->ptrs + push_items, dst->ptrs,
631 dst_nritems * sizeof(struct btrfs_key_ptr));
632 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
633 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500634
Chris Mason7518a232007-03-12 12:01:18 -0400635 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
636 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500637
Chris Masond5719762007-03-23 10:01:08 -0400638 mark_buffer_dirty(src_buf);
639 mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500640 return ret;
641}
642
643/*
Chris Mason97571fd2007-02-24 13:39:08 -0500644 * helper function to insert a new root level in the tree.
645 * A new node is allocated, and a single item is inserted to
646 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500647 *
648 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500649 */
Chris Masone089f052007-03-16 16:20:31 -0400650static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
651 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500652{
Chris Masone20d96d2007-03-22 12:13:20 -0400653 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400654 struct btrfs_node *lower;
655 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400656 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500657
658 BUG_ON(path->nodes[level]);
659 BUG_ON(path->nodes[level-1] != root->node);
660
Chris Masone089f052007-03-16 16:20:31 -0400661 t = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400662 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400663 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400664 btrfs_set_header_nritems(&c->header, 1);
665 btrfs_set_header_level(&c->header, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400666 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -0400667 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -0400668 btrfs_set_header_parentid(&c->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400669 btrfs_header_parentid(btrfs_buffer_header(root->node)));
670 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason7518a232007-03-12 12:01:18 -0400671 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400672 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500673 else
Chris Mason123abc82007-03-14 14:14:43 -0400674 lower_key = &lower->ptrs[0].key;
675 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400676 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
Chris Masond5719762007-03-23 10:01:08 -0400677
678 mark_buffer_dirty(t);
679
Chris Mason5c680ed2007-02-22 11:39:13 -0500680 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400681 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500682 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400683 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500684 path->nodes[level] = t;
685 path->slots[level] = 0;
686 return 0;
687}
688
Chris Mason74123bd2007-02-02 11:05:29 -0500689/*
690 * worker function to insert a single pointer in a node.
691 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500692 *
Chris Mason74123bd2007-02-02 11:05:29 -0500693 * slot and level indicate where you want the key to go, and
694 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500695 *
696 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500697 */
Chris Masone089f052007-03-16 16:20:31 -0400698static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
699 *root, struct btrfs_path *path, struct btrfs_disk_key
700 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500701{
Chris Mason234b63a2007-03-13 10:46:10 -0400702 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500703 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500704
705 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400706 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400707 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500708 if (slot > nritems)
709 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400710 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500711 BUG();
712 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400713 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
714 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500715 }
Chris Mason123abc82007-03-14 14:14:43 -0400716 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400717 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400718 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond5719762007-03-23 10:01:08 -0400719 mark_buffer_dirty(path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500720 return 0;
721}
722
Chris Mason97571fd2007-02-24 13:39:08 -0500723/*
724 * split the node at the specified level in path in two.
725 * The path is corrected to point to the appropriate node after the split
726 *
727 * Before splitting this tries to make some room in the node by pushing
728 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500729 *
730 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500731 */
Chris Masone089f052007-03-16 16:20:31 -0400732static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
733 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500734{
Chris Masone20d96d2007-03-22 12:13:20 -0400735 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400736 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400737 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400738 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500739 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500740 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400742 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743
Chris Mason5c680ed2007-02-22 11:39:13 -0500744 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400745 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500746 if (t == root->node) {
747 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400748 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500749 if (ret)
750 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500751 }
Chris Mason7518a232007-03-12 12:01:18 -0400752 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400753 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400754 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400755 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -0400756 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Masone20d96d2007-03-22 12:13:20 -0400757 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -0400758 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -0400759 btrfs_set_header_parentid(&split->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400760 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason7518a232007-03-12 12:01:18 -0400761 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400762 memcpy(split->ptrs, c->ptrs + mid,
763 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400764 btrfs_set_header_nritems(&split->header, c_nritems - mid);
765 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500766 ret = 0;
767
Chris Masond5719762007-03-23 10:01:08 -0400768 mark_buffer_dirty(t);
769 mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400770 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -0400771 split_buffer->b_blocknr, path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400772 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500773 if (wret)
774 ret = wret;
775
Chris Mason5de08d72007-02-24 06:24:44 -0500776 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500777 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400778 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500779 path->nodes[level] = split_buffer;
780 path->slots[level + 1] += 1;
781 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400782 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500783 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500784 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500785}
786
Chris Mason74123bd2007-02-02 11:05:29 -0500787/*
788 * how many bytes are required to store the items in a leaf. start
789 * and nr indicate which items in the leaf to check. This totals up the
790 * space used both by the item structs and the item data
791 */
Chris Mason234b63a2007-03-13 10:46:10 -0400792static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500793{
794 int data_len;
795 int end = start + nr - 1;
796
797 if (!nr)
798 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400799 data_len = btrfs_item_end(l->items + start);
800 data_len = data_len - btrfs_item_offset(l->items + end);
801 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500802 return data_len;
803}
804
Chris Mason74123bd2007-02-02 11:05:29 -0500805/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500806 * push some data in the path leaf to the right, trying to free up at
807 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500808 *
809 * returns 1 if the push failed because the other node didn't have enough
810 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500811 */
Chris Masone089f052007-03-16 16:20:31 -0400812static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
813 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500814{
Chris Masone20d96d2007-03-22 12:13:20 -0400815 struct buffer_head *left_buf = path->nodes[0];
816 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400817 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400818 struct buffer_head *right_buf;
819 struct buffer_head *upper;
820 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500821 int slot;
822 int i;
823 int free_space;
824 int push_space = 0;
825 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400826 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400827 u32 left_nritems;
828 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500829
830 slot = path->slots[1];
831 if (!path->nodes[1]) {
832 return 1;
833 }
834 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -0400835 upper_node = btrfs_buffer_node(upper);
836 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500837 return 1;
838 }
Chris Masone20d96d2007-03-22 12:13:20 -0400839 right_buf = read_tree_block(root,
840 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
841 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400842 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400843 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400844 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500845 return 1;
846 }
Chris Mason02217ed2007-03-02 16:08:05 -0500847 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400848 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400849 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400850 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400851 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400852 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500853 return 1;
854 }
855
Chris Mason7518a232007-03-12 12:01:18 -0400856 left_nritems = btrfs_header_nritems(&left->header);
857 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500858 item = left->items + i;
859 if (path->slots[0] == i)
860 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400861 if (btrfs_item_size(item) + sizeof(*item) + push_space >
862 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500863 break;
864 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400865 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500866 }
867 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400868 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500869 return 1;
870 }
Chris Mason7518a232007-03-12 12:01:18 -0400871 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500872 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400873 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400874 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500875 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400876 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
877 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
878 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500879 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400880 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
881 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500882 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400883 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500884 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400885 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400886 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500887
888 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400889 right_nritems += push_items;
890 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400891 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400892 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400893 btrfs_set_item_offset(right->items + i, push_space -
894 btrfs_item_size(right->items + i));
895 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500896 }
Chris Mason7518a232007-03-12 12:01:18 -0400897 left_nritems -= push_items;
898 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500899
Chris Masond5719762007-03-23 10:01:08 -0400900 mark_buffer_dirty(left_buf);
901 mark_buffer_dirty(right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400902 memcpy(&upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400903 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400904 mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -0500905
Chris Mason00ec4c52007-02-24 12:47:20 -0500906 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400907 if (path->slots[0] >= left_nritems) {
908 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400909 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500910 path->nodes[0] = right_buf;
911 path->slots[1] += 1;
912 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400913 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500914 }
915 return 0;
916}
917/*
Chris Mason74123bd2007-02-02 11:05:29 -0500918 * push some data in the path leaf to the left, trying to free up at
919 * least data_size bytes. returns zero if the push worked, nonzero otherwise
920 */
Chris Masone089f052007-03-16 16:20:31 -0400921static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
922 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500923{
Chris Masone20d96d2007-03-22 12:13:20 -0400924 struct buffer_head *right_buf = path->nodes[0];
925 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
926 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400927 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500928 int slot;
929 int i;
930 int free_space;
931 int push_space = 0;
932 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400933 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400934 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500935 int ret = 0;
936 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500937
938 slot = path->slots[1];
939 if (slot == 0) {
940 return 1;
941 }
942 if (!path->nodes[1]) {
943 return 1;
944 }
Chris Masone20d96d2007-03-22 12:13:20 -0400945 t = read_tree_block(root,
946 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
947 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400948 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400949 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400950 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500951 return 1;
952 }
Chris Mason02217ed2007-03-02 16:08:05 -0500953
954 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400955 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -0400956 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400957 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400958 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400959 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500960 return 1;
961 }
962
Chris Mason7518a232007-03-12 12:01:18 -0400963 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500964 item = right->items + i;
965 if (path->slots[0] == i)
966 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400967 if (btrfs_item_size(item) + sizeof(*item) + push_space >
968 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 break;
970 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400971 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500972 }
973 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400974 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500975 return 1;
976 }
977 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400978 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400979 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400980 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400981 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400982 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
983 btrfs_leaf_data(right) +
984 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500985 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400986 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500987 BUG_ON(old_left_nritems < 0);
988
Chris Mason0783fcf2007-03-12 20:12:07 -0400989 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400990 u32 ioff = btrfs_item_offset(left->items + i);
991 btrfs_set_item_offset(left->items + i, ioff -
992 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400993 btrfs_item_offset(left->items +
994 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500995 }
Chris Mason7518a232007-03-12 12:01:18 -0400996 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500997
998 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400999 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001000 leaf_data_end(root, right);
1001 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1002 push_space, btrfs_leaf_data(right) +
1003 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001005 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001006 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001007 btrfs_set_header_nritems(&right->header,
1008 btrfs_header_nritems(&right->header) -
1009 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001010 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001011
Chris Mason7518a232007-03-12 12:01:18 -04001012 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001013 btrfs_set_item_offset(right->items + i, push_space -
1014 btrfs_item_size(right->items + i));
1015 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001016 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001017
Chris Masond5719762007-03-23 10:01:08 -04001018 mark_buffer_dirty(t);
1019 mark_buffer_dirty(right_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001020
Chris Masone089f052007-03-16 16:20:31 -04001021 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001022 if (wret)
1023 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001024
1025 /* then fixup the leaf pointer in the path */
1026 if (path->slots[0] < push_items) {
1027 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001028 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001029 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001030 path->slots[1] -= 1;
1031 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001032 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001033 path->slots[0] -= push_items;
1034 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001035 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001036 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001037}
1038
Chris Mason74123bd2007-02-02 11:05:29 -05001039/*
1040 * split the path's leaf in two, making sure there is at least data_size
1041 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001042 *
1043 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001044 */
Chris Masone089f052007-03-16 16:20:31 -04001045static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1046 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001047{
Chris Masone20d96d2007-03-22 12:13:20 -04001048 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001049 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001050 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001051 int mid;
1052 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001053 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001054 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001055 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001056 int data_copy_size;
1057 int rt_data_off;
1058 int i;
1059 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001060 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001061
Chris Mason40689472007-03-17 14:29:23 -04001062 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001063 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001064 if (wret < 0)
1065 return wret;
1066 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001067 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001068 if (wret < 0)
1069 return wret;
1070 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001071 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001072 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001073
1074 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001075 if (btrfs_leaf_free_space(root, l) >=
1076 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001077 return 0;
1078
Chris Mason5c680ed2007-02-22 11:39:13 -05001079 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001080 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001081 if (ret)
1082 return ret;
1083 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001084 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001085 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001086 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001087 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001088 BUG_ON(!right_buffer);
1089 BUG_ON(mid == nritems);
Chris Masone20d96d2007-03-22 12:13:20 -04001090 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001091 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001092 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001093 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001094 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001095 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001096 BUG();
1097 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001098 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099 if (leaf_space_used(l, 0, mid + 1) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001100 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001101 BUG();
1102 }
Chris Mason7518a232007-03-12 12:01:18 -04001103 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Masone20d96d2007-03-22 12:13:20 -04001104 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -04001105 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -04001106 btrfs_set_header_level(&right->header, 0);
1107 btrfs_set_header_parentid(&right->header,
Chris Masone20d96d2007-03-22 12:13:20 -04001108 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason123abc82007-03-14 14:14:43 -04001109 data_copy_size = btrfs_item_end(l->items + mid) -
1110 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001111 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001112 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001113 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1114 data_copy_size, btrfs_leaf_data(l) +
1115 leaf_data_end(root, l), data_copy_size);
1116 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1117 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001118
Chris Mason0783fcf2007-03-12 20:12:07 -04001119 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001120 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001121 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1122 }
Chris Mason74123bd2007-02-02 11:05:29 -05001123
Chris Mason7518a232007-03-12 12:01:18 -04001124 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001125 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001126 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -04001127 right_buffer->b_blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001128 if (wret)
1129 ret = wret;
Chris Masond5719762007-03-23 10:01:08 -04001130 mark_buffer_dirty(right_buffer);
1131 mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001132 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001133 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001134 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001135 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001136 path->slots[0] -= mid;
1137 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001138 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001139 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001140 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001141 return ret;
1142}
1143
Chris Mason74123bd2007-02-02 11:05:29 -05001144/*
1145 * Given a key and some data, insert an item into the tree.
1146 * This does all the path init required, making room in the tree if needed.
1147 */
Chris Masone089f052007-03-16 16:20:31 -04001148int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1149 *root, struct btrfs_path *path, struct btrfs_key
1150 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001151{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001152 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001153 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001154 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001155 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001156 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001157 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001158 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001159 struct btrfs_disk_key disk_key;
1160
1161 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001162
Chris Mason74123bd2007-02-02 11:05:29 -05001163 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001164 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001165 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001166 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001167 if (ret == 0) {
Chris Mason62e27492007-03-15 12:56:47 -04001168 btrfs_release_path(root, path);
Chris Masonf0930a32007-03-02 09:47:58 -05001169 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001170 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001171 if (ret < 0)
1172 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001173
Chris Mason62e27492007-03-15 12:56:47 -04001174 slot_orig = path->slots[0];
1175 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001176 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001177
Chris Mason7518a232007-03-12 12:01:18 -04001178 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001179 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001180
Chris Mason123abc82007-03-14 14:14:43 -04001181 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001182 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001183 BUG();
1184
Chris Mason62e27492007-03-15 12:56:47 -04001185 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001186 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001187 if (slot != nritems) {
1188 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001189 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001190
1191 /*
1192 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1193 */
1194 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001195 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001196 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001197 btrfs_set_item_offset(leaf->items + i,
1198 ioff - data_size);
1199 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001200
1201 /* shift the items */
1202 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001203 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001204
1205 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001206 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1207 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001208 data_end, old_data - data_end);
1209 data_end = old_data;
1210 }
Chris Mason62e27492007-03-15 12:56:47 -04001211 /* setup the item for the new data */
Chris Masone2fa7222007-03-12 16:22:34 -04001212 memcpy(&leaf->items[slot].key, &disk_key,
1213 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001214 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1215 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001216 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond5719762007-03-23 10:01:08 -04001217 mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001218
1219 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001220 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001221 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001222
Chris Mason123abc82007-03-14 14:14:43 -04001223 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001224 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001225 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001226out:
Chris Mason62e27492007-03-15 12:56:47 -04001227 return ret;
1228}
1229
1230/*
1231 * Given a key and some data, insert an item into the tree.
1232 * This does all the path init required, making room in the tree if needed.
1233 */
Chris Masone089f052007-03-16 16:20:31 -04001234int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1235 *root, struct btrfs_key *cpu_key, void *data, u32
1236 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001237{
1238 int ret = 0;
1239 struct btrfs_path path;
1240 u8 *ptr;
1241
1242 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -04001243 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001244 if (!ret) {
Chris Masone20d96d2007-03-22 12:13:20 -04001245 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
1246 path.slots[0], u8);
Chris Mason62e27492007-03-15 12:56:47 -04001247 memcpy(ptr, data, data_size);
Chris Masond5719762007-03-23 10:01:08 -04001248 mark_buffer_dirty(path.nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001249 }
Chris Mason234b63a2007-03-13 10:46:10 -04001250 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001251 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001252}
1253
Chris Mason74123bd2007-02-02 11:05:29 -05001254/*
Chris Mason5de08d72007-02-24 06:24:44 -05001255 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001256 *
1257 * If the delete empties a node, the node is removed from the tree,
1258 * continuing all the way the root if required. The root is converted into
1259 * a leaf if all the nodes are emptied.
1260 */
Chris Masone089f052007-03-16 16:20:31 -04001261static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1262 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001263{
Chris Mason234b63a2007-03-13 10:46:10 -04001264 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001265 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001266 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001267 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001268 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001269
Chris Masone20d96d2007-03-22 12:13:20 -04001270 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001271 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001272 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001273 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1274 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001275 }
Chris Mason7518a232007-03-12 12:01:18 -04001276 nritems--;
1277 btrfs_set_header_nritems(&node->header, nritems);
1278 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001279 struct btrfs_header *header = btrfs_buffer_header(root->node);
1280 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001281 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001282 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001283 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001284 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001285 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001286 if (wret)
1287 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288 }
Chris Masond5719762007-03-23 10:01:08 -04001289 mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001290 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001291}
1292
Chris Mason74123bd2007-02-02 11:05:29 -05001293/*
1294 * delete the item at the leaf level in path. If that empties
1295 * the leaf, remove it from the tree
1296 */
Chris Masone089f052007-03-16 16:20:31 -04001297int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1298 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001299{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001300 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001301 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001302 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001303 int doff;
1304 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001305 int ret = 0;
1306 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001307 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001308
Chris Masoneb60cea2007-02-02 09:18:22 -05001309 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001310 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001311 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001312 doff = btrfs_item_offset(leaf->items + slot);
1313 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001314 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001315
Chris Mason7518a232007-03-12 12:01:18 -04001316 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001317 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001318 int data_end = leaf_data_end(root, leaf);
1319 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1320 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001321 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001322 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001323 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001324 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1325 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001326 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001327 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001328 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001329 }
Chris Mason7518a232007-03-12 12:01:18 -04001330 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1331 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001332 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001333 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001334 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001335 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001336 } else {
Chris Masone089f052007-03-16 16:20:31 -04001337 clean_tree_block(trans, root, leaf_buf);
1338 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001339 if (wret)
1340 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001341 wret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -04001342 leaf_buf->b_blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001343 if (wret)
1344 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001345 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001346 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001347 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001348 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001349 wret = fixup_low_keys(trans, root, path,
1350 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001351 if (wret)
1352 ret = wret;
1353 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001354
Chris Mason74123bd2007-02-02 11:05:29 -05001355 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001356 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001357 /* push_leaf_left fixes the path.
1358 * make sure the path still points to our leaf
1359 * for possible call to del_ptr below
1360 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001361 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001362 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001363 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001364 if (wret < 0)
1365 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001366 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001367 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001368 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001369 if (wret < 0)
1370 ret = wret;
1371 }
Chris Mason7518a232007-03-12 12:01:18 -04001372 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -04001373 u64 blocknr = leaf_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001374 clean_tree_block(trans, root, leaf_buf);
1375 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001376 if (wret)
1377 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001378 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001379 wret = btrfs_free_extent(trans, root, blocknr,
1380 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001381 if (wret)
1382 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001383 } else {
Chris Masond5719762007-03-23 10:01:08 -04001384 mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001385 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001386 }
Chris Masond5719762007-03-23 10:01:08 -04001387 } else {
1388 mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001389 }
1390 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001391 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001392}
1393
Chris Mason97571fd2007-02-24 13:39:08 -05001394/*
1395 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001396 * returns 0 if it found something or 1 if there are no greater leaves.
1397 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001398 */
Chris Mason234b63a2007-03-13 10:46:10 -04001399int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001400{
1401 int slot;
1402 int level = 1;
1403 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001404 struct buffer_head *c;
1405 struct btrfs_node *c_node;
1406 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001407
Chris Mason234b63a2007-03-13 10:46:10 -04001408 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001409 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001410 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001411 slot = path->slots[level] + 1;
1412 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001413 c_node = btrfs_buffer_node(c);
1414 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001415 level++;
1416 continue;
1417 }
Chris Masone20d96d2007-03-22 12:13:20 -04001418 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001419 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001420 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001421 next = read_tree_block(root, blocknr);
1422 break;
1423 }
1424 path->slots[level] = slot;
1425 while(1) {
1426 level--;
1427 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001428 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001429 path->nodes[level] = next;
1430 path->slots[level] = 0;
1431 if (!level)
1432 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001433 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001434 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001435 }
1436 return 0;
1437}