blob: 052434a328ec91cd2a060f85279772e4236c9ec0 [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 Mason9a8dd152007-02-23 08:38:36 -05004
Chris Masone089f052007-03-16 16:20:31 -04005static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
6 *root, struct btrfs_path *path, int level);
7static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
8 *root, struct btrfs_path *path, int data_size);
9static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040010 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040011 *src);
12static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040013 btrfs_root *root, struct buffer_head *dst_buf,
14 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040015static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
16 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050017
Chris Mason234b63a2007-03-13 10:46:10 -040018inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050019{
20 memset(p, 0, sizeof(*p));
21}
22
Chris Mason234b63a2007-03-13 10:46:10 -040023void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050024{
25 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040026 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050027 if (!p->nodes[i])
28 break;
Chris Mason234b63a2007-03-13 10:46:10 -040029 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050030 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050031 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050032}
33
Chris Masone089f052007-03-16 16:20:31 -040034static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040035 *root, struct buffer_head *buf, struct buffer_head
36 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040037 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050038{
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct buffer_head *cow;
40 struct btrfs_node *cow_node;
Chris Mason02217ed2007-03-02 16:08:05 -050041
Chris Masond5719762007-03-23 10:01:08 -040042 if (buffer_dirty(buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -050043 *cow_ret = buf;
44 return 0;
45 }
Chris Masone089f052007-03-16 16:20:31 -040046 cow = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -040047 cow_node = btrfs_buffer_node(cow);
48 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
49 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050050 *cow_ret = cow;
Chris Masond5719762007-03-23 10:01:08 -040051 mark_buffer_dirty(cow);
Chris Masone089f052007-03-16 16:20:31 -040052 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050053 if (buf == root->node) {
54 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040055 get_bh(cow);
Chris Masona28ec192007-03-06 20:08:01 -050056 if (buf != root->commit_root)
Chris Masone20d96d2007-03-22 12:13:20 -040057 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -040058 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050059 } else {
Chris Masone20d96d2007-03-22 12:13:20 -040060 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
61 cow->b_blocknr);
Chris Masond5719762007-03-23 10:01:08 -040062 mark_buffer_dirty(parent);
Chris Masone20d96d2007-03-22 12:13:20 -040063 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050064 }
Chris Mason234b63a2007-03-13 10:46:10 -040065 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050066 return 0;
67}
68
Chris Mason74123bd2007-02-02 11:05:29 -050069/*
70 * The leaf data grows from end-to-front in the node.
71 * this returns the address of the start of the last item,
72 * which is the stop of the leaf data stack
73 */
Chris Mason123abc82007-03-14 14:14:43 -040074static inline unsigned int leaf_data_end(struct btrfs_root *root,
75 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 Mason123abc82007-03-14 14:14:43 -040079 return BTRFS_LEAF_DATA_SIZE(root);
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 Mason123abc82007-03-14 14:14:43 -040088int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050089{
Chris Mason123abc82007-03-14 14:14:43 -040090 int data_end = leaf_data_end(root, 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);
Chris Mason123abc82007-03-14 14:14:43 -040093 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050094}
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 Mason62e27492007-03-15 12:56:47 -0400109 if (k1.flags > k2->flags)
110 return 1;
111 if (k1.flags < k2->flags)
112 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400113 if (k1.offset > k2->offset)
114 return 1;
115 if (k1.offset < k2->offset)
116 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500117 return 0;
118}
Chris Mason74123bd2007-02-02 11:05:29 -0500119
Chris Mason123abc82007-03-14 14:14:43 -0400120static int check_node(struct btrfs_root *root, struct btrfs_path *path,
121 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500122{
123 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400124 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400125 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500126 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400127 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500128
129 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400130 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500131 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400132 BUG_ON(nritems == 0);
133 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400134 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400135 parent_key = &parent->ptrs[parent_slot].key;
136 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400137 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400138 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400139 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500140 }
Chris Mason123abc82007-03-14 14:14:43 -0400141 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400142 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400143 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400144 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
145 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500146 }
147 return 0;
148}
149
Chris Mason123abc82007-03-14 14:14:43 -0400150static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
151 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500152{
153 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400154 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400155 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500156 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400157 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500158
159 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400160 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500161 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400162 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400163
164 if (nritems == 0)
165 return 0;
166
167 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400168 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400169 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500170 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400171 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400172 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400173 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500174 }
Chris Mason7518a232007-03-12 12:01:18 -0400175 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400176 struct btrfs_key cpukey;
177 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500178 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400179 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400180 BUG_ON(btrfs_item_offset(leaf->items + i) !=
181 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500182 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400183 BUG_ON(btrfs_item_offset(leaf->items + i) +
184 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400185 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500186 }
187 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500188 return 0;
189}
190
Chris Mason123abc82007-03-14 14:14:43 -0400191static int check_block(struct btrfs_root *root, struct btrfs_path *path,
192 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500193{
194 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400195 return check_leaf(root, path, level);
196 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500197}
198
Chris Mason74123bd2007-02-02 11:05:29 -0500199/*
200 * search for key in the array p. items p are item_size apart
201 * and there are 'max' items in p
202 * the slot in the array is returned via slot, and it points to
203 * the place where you would insert key if it is not found in
204 * the array.
205 *
206 * slot may point to max if the key is bigger than all of the keys
207 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400208static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500209 int max, int *slot)
210{
211 int low = 0;
212 int high = max;
213 int mid;
214 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400215 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500216
217 while(low < high) {
218 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400219 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500220 ret = comp_keys(tmp, key);
221
222 if (ret < 0)
223 low = mid + 1;
224 else if (ret > 0)
225 high = mid;
226 else {
227 *slot = mid;
228 return 0;
229 }
230 }
231 *slot = low;
232 return 1;
233}
234
Chris Mason97571fd2007-02-24 13:39:08 -0500235/*
236 * simple bin_search frontend that does the right thing for
237 * leaves vs nodes
238 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400239static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500240{
Chris Mason7518a232007-03-12 12:01:18 -0400241 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400242 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400243 return generic_bin_search((void *)l->items,
244 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400245 key, btrfs_header_nritems(&c->header),
246 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500247 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400248 return generic_bin_search((void *)c->ptrs,
249 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400250 key, btrfs_header_nritems(&c->header),
251 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500252 }
253 return -1;
254}
255
Chris Masone20d96d2007-03-22 12:13:20 -0400256static struct buffer_head *read_node_slot(struct btrfs_root *root,
257 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500258 int slot)
259{
Chris Masone20d96d2007-03-22 12:13:20 -0400260 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500261 if (slot < 0)
262 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400263 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500264 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400265 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500266}
267
Chris Masone089f052007-03-16 16:20:31 -0400268static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
269 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500270{
Chris Masone20d96d2007-03-22 12:13:20 -0400271 struct buffer_head *right_buf;
272 struct buffer_head *mid_buf;
273 struct buffer_head *left_buf;
274 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400275 struct btrfs_node *right = NULL;
276 struct btrfs_node *mid;
277 struct btrfs_node *left = NULL;
278 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500279 int ret = 0;
280 int wret;
281 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500282 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500283 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500284
285 if (level == 0)
286 return 0;
287
288 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400289 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400290 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500291
Chris Mason234b63a2007-03-13 10:46:10 -0400292 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500293 parent_buf = path->nodes[level + 1];
294 pslot = path->slots[level + 1];
295
Chris Mason40689472007-03-17 14:29:23 -0400296 /*
297 * deal with the case where there is only one pointer in the root
298 * by promoting the node below to a root
299 */
Chris Masonbb803952007-03-01 12:04:21 -0500300 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400301 struct buffer_head *child;
302 u64 blocknr = mid_buf->b_blocknr;
Chris Masonbb803952007-03-01 12:04:21 -0500303
Chris Mason7518a232007-03-12 12:01:18 -0400304 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500305 return 0;
306
307 /* promote the child to a root */
308 child = read_node_slot(root, mid_buf, 0);
309 BUG_ON(!child);
310 root->node = child;
311 path->nodes[level] = NULL;
312 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400313 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500314 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400315 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400316 clean_tree_block(trans, root, mid_buf);
317 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500318 }
Chris Masone20d96d2007-03-22 12:13:20 -0400319 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500320
Chris Mason123abc82007-03-14 14:14:43 -0400321 if (btrfs_header_nritems(&mid->header) >
322 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500323 return 0;
324
Chris Masonbb803952007-03-01 12:04:21 -0500325 left_buf = read_node_slot(root, parent_buf, pslot - 1);
326 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500327
328 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500329 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400330 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
331 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400332 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400333 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400334 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500335 if (wret < 0)
336 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500337 }
Chris Mason79f95c82007-03-01 15:16:26 -0500338
339 /*
340 * then try to empty the right most buffer into the middle
341 */
Chris Masonbb803952007-03-01 12:04:21 -0500342 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400343 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
344 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400345 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400346 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500347 if (wret < 0)
348 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400349 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -0400350 u64 blocknr = right_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400351 btrfs_block_release(root, right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400352 clean_tree_block(trans, root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500353 right_buf = NULL;
354 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400355 wret = del_ptr(trans, root, path, level + 1, pslot +
356 1);
Chris Masonbb803952007-03-01 12:04:21 -0500357 if (wret)
358 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400359 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500360 if (wret)
361 ret = wret;
362 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400363 memcpy(&parent->ptrs[pslot + 1].key,
364 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400365 sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400366 mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500367 }
368 }
Chris Mason7518a232007-03-12 12:01:18 -0400369 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500370 /*
371 * we're not allowed to leave a node with one item in the
372 * tree during a delete. A deletion from lower in the tree
373 * could try to delete the only pointer in this node.
374 * So, pull some keys from the left.
375 * There has to be a left pointer at this point because
376 * otherwise we would have pulled some pointers from the
377 * right
378 */
379 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400380 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500381 if (wret < 0)
382 ret = wret;
383 BUG_ON(wret == 1);
384 }
Chris Mason7518a232007-03-12 12:01:18 -0400385 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500386 /* we've managed to empty the middle node, drop it */
Chris Masone20d96d2007-03-22 12:13:20 -0400387 u64 blocknr = mid_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400388 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400389 clean_tree_block(trans, root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500390 mid_buf = NULL;
391 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400392 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500393 if (wret)
394 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400395 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500396 if (wret)
397 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500398 } else {
399 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400400 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400401 sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400402 mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500403 }
Chris Masonbb803952007-03-01 12:04:21 -0500404
Chris Mason79f95c82007-03-01 15:16:26 -0500405 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500406 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400407 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400408 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500409 path->nodes[level] = left_buf;
410 path->slots[level + 1] -= 1;
411 path->slots[level] = orig_slot;
412 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400413 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500414 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400415 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500416 path->slots[level] = orig_slot;
417 }
418 }
Chris Mason79f95c82007-03-01 15:16:26 -0500419 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400420 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400421 if (orig_ptr !=
422 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
423 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500424 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500425
426 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400427 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500428 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400429 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500430 return ret;
431}
432
Chris Mason74123bd2007-02-02 11:05:29 -0500433/*
434 * look for key in the tree. path is filled in with nodes along the way
435 * if key is found, we return zero and you can find the item in the leaf
436 * level of the path (level 0)
437 *
438 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500439 * be inserted, and 1 is returned. If there are other errors during the
440 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500441 *
442 * if ins_len > 0, nodes and leaves will be split as we walk down the
443 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
444 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500445 */
Chris Masone089f052007-03-16 16:20:31 -0400446int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
447 *root, struct btrfs_key *key, struct btrfs_path *p, int
448 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500449{
Chris Masone20d96d2007-03-22 12:13:20 -0400450 struct buffer_head *b;
451 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400452 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500453 int slot;
454 int ret;
455 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500456
Chris Masonbb803952007-03-01 12:04:21 -0500457again:
458 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400459 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500460 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400461 c = btrfs_buffer_node(b);
462 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500463 if (cow) {
464 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400465 wret = btrfs_cow_block(trans, root, b,
466 p->nodes[level + 1],
467 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400468 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500469 b = cow_buf;
470 }
471 BUG_ON(!cow && ins_len);
Chris Masone20d96d2007-03-22 12:13:20 -0400472 c = btrfs_buffer_node(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500473 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400474 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500475 if (ret)
476 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500477 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400478 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500479 if (ret && slot > 0)
480 slot -= 1;
481 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400482 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400483 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Masone089f052007-03-16 16:20:31 -0400484 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500485 BUG_ON(sret > 0);
486 if (sret)
487 return sret;
488 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400489 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500490 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500491 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400492 int sret = balance_level(trans, root, p,
493 level);
Chris Masonbb803952007-03-01 12:04:21 -0500494 if (sret)
495 return sret;
496 b = p->nodes[level];
497 if (!b)
498 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400499 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500500 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400501 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500502 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400503 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500504 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400505 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500506 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400507 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400508 sizeof(struct btrfs_item) + ins_len) {
Chris Masone089f052007-03-16 16:20:31 -0400509 int sret = split_leaf(trans, root, p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500510 BUG_ON(sret > 0);
511 if (sret)
512 return sret;
513 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500514 return ret;
515 }
516 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500517 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500518}
519
Chris Mason74123bd2007-02-02 11:05:29 -0500520/*
521 * adjust the pointers going up the tree, starting at level
522 * making sure the right key of each node is points to 'key'.
523 * This is used after shifting pointers to the left, so it stops
524 * fixing up pointers when a given leaf/node is not in slot 0 of the
525 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500526 *
527 * If this fails to write a tree block, it returns -1, but continues
528 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500529 */
Chris Masone089f052007-03-16 16:20:31 -0400530static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_path *path, struct btrfs_disk_key
532 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500533{
534 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500535 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400536 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
537 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500538 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500539 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500540 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400541 t = btrfs_buffer_node(path->nodes[i]);
Chris Mason123abc82007-03-14 14:14:43 -0400542 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Masond5719762007-03-23 10:01:08 -0400543 mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500544 if (tslot != 0)
545 break;
546 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500547 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548}
549
Chris Mason74123bd2007-02-02 11:05:29 -0500550/*
551 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500552 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553 *
554 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
555 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500556 */
Chris Masone089f052007-03-16 16:20:31 -0400557static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400558 *root, struct buffer_head *dst_buf, struct
559 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500560{
Chris Masone20d96d2007-03-22 12:13:20 -0400561 struct btrfs_node *src = btrfs_buffer_node(src_buf);
562 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500564 int src_nritems;
565 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500566 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500567
Chris Mason7518a232007-03-12 12:01:18 -0400568 src_nritems = btrfs_header_nritems(&src->header);
569 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400570 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500571 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500572 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500573 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500574
575 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500576 push_items = src_nritems;
577
Chris Mason123abc82007-03-14 14:14:43 -0400578 memcpy(dst->ptrs + dst_nritems, src->ptrs,
579 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500580 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400581 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400582 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400583 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500584 }
Chris Mason7518a232007-03-12 12:01:18 -0400585 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
586 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond5719762007-03-23 10:01:08 -0400587 mark_buffer_dirty(src_buf);
588 mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500589 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500590}
591
Chris Mason97571fd2007-02-24 13:39:08 -0500592/*
Chris Mason79f95c82007-03-01 15:16:26 -0500593 * try to push data from one node into the next node right in the
594 * tree.
595 *
596 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
597 * error, and > 0 if there was no room in the right hand block.
598 *
599 * this will only push up to 1/2 the contents of the left node over
600 */
Chris Masone089f052007-03-16 16:20:31 -0400601static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400602 btrfs_root *root, struct buffer_head *dst_buf,
603 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500604{
Chris Masone20d96d2007-03-22 12:13:20 -0400605 struct btrfs_node *src = btrfs_buffer_node(src_buf);
606 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500607 int push_items = 0;
608 int max_push;
609 int src_nritems;
610 int dst_nritems;
611 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500612
Chris Mason7518a232007-03-12 12:01:18 -0400613 src_nritems = btrfs_header_nritems(&src->header);
614 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400615 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500616 if (push_items <= 0) {
617 return 1;
618 }
619
620 max_push = src_nritems / 2 + 1;
621 /* don't try to empty the node */
622 if (max_push > src_nritems)
623 return 1;
624 if (max_push < push_items)
625 push_items = max_push;
626
Chris Mason123abc82007-03-14 14:14:43 -0400627 memmove(dst->ptrs + push_items, dst->ptrs,
628 dst_nritems * sizeof(struct btrfs_key_ptr));
629 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
630 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500631
Chris Mason7518a232007-03-12 12:01:18 -0400632 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
633 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500634
Chris Masond5719762007-03-23 10:01:08 -0400635 mark_buffer_dirty(src_buf);
636 mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500637 return ret;
638}
639
640/*
Chris Mason97571fd2007-02-24 13:39:08 -0500641 * helper function to insert a new root level in the tree.
642 * A new node is allocated, and a single item is inserted to
643 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500644 *
645 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500646 */
Chris Masone089f052007-03-16 16:20:31 -0400647static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
648 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500649{
Chris Masone20d96d2007-03-22 12:13:20 -0400650 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400651 struct btrfs_node *lower;
652 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400653 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500654
655 BUG_ON(path->nodes[level]);
656 BUG_ON(path->nodes[level-1] != root->node);
657
Chris Masone089f052007-03-16 16:20:31 -0400658 t = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400659 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400660 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400661 btrfs_set_header_nritems(&c->header, 1);
662 btrfs_set_header_level(&c->header, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400663 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400664 btrfs_set_header_parentid(&c->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400665 btrfs_header_parentid(btrfs_buffer_header(root->node)));
666 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason7518a232007-03-12 12:01:18 -0400667 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400668 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500669 else
Chris Mason123abc82007-03-14 14:14:43 -0400670 lower_key = &lower->ptrs[0].key;
671 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400672 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
Chris Masond5719762007-03-23 10:01:08 -0400673
674 mark_buffer_dirty(t);
675
Chris Mason5c680ed2007-02-22 11:39:13 -0500676 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400677 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500678 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400679 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500680 path->nodes[level] = t;
681 path->slots[level] = 0;
682 return 0;
683}
684
Chris Mason74123bd2007-02-02 11:05:29 -0500685/*
686 * worker function to insert a single pointer in a node.
687 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500688 *
Chris Mason74123bd2007-02-02 11:05:29 -0500689 * slot and level indicate where you want the key to go, and
690 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500691 *
692 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500693 */
Chris Masone089f052007-03-16 16:20:31 -0400694static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
695 *root, struct btrfs_path *path, struct btrfs_disk_key
696 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500697{
Chris Mason234b63a2007-03-13 10:46:10 -0400698 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500699 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500700
701 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400702 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400703 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500704 if (slot > nritems)
705 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400706 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500707 BUG();
708 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400709 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
710 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500711 }
Chris Mason123abc82007-03-14 14:14:43 -0400712 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400713 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400714 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond5719762007-03-23 10:01:08 -0400715 mark_buffer_dirty(path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500716 return 0;
717}
718
Chris Mason97571fd2007-02-24 13:39:08 -0500719/*
720 * split the node at the specified level in path in two.
721 * The path is corrected to point to the appropriate node after the split
722 *
723 * Before splitting this tries to make some room in the node by pushing
724 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500725 *
726 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500727 */
Chris Masone089f052007-03-16 16:20:31 -0400728static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
729 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500730{
Chris Masone20d96d2007-03-22 12:13:20 -0400731 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400732 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400733 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400734 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500736 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500737 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400738 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500739
Chris Mason5c680ed2007-02-22 11:39:13 -0500740 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400741 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500742 if (t == root->node) {
743 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400744 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500745 if (ret)
746 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500747 }
Chris Mason7518a232007-03-12 12:01:18 -0400748 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400749 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400750 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400751 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Masone20d96d2007-03-22 12:13:20 -0400752 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400753 btrfs_set_header_parentid(&split->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400754 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason7518a232007-03-12 12:01:18 -0400755 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400756 memcpy(split->ptrs, c->ptrs + mid,
757 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400758 btrfs_set_header_nritems(&split->header, c_nritems - mid);
759 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500760 ret = 0;
761
Chris Masond5719762007-03-23 10:01:08 -0400762 mark_buffer_dirty(t);
763 mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400764 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -0400765 split_buffer->b_blocknr, path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400766 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500767 if (wret)
768 ret = wret;
769
Chris Mason5de08d72007-02-24 06:24:44 -0500770 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500771 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400772 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500773 path->nodes[level] = split_buffer;
774 path->slots[level + 1] += 1;
775 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400776 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500778 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779}
780
Chris Mason74123bd2007-02-02 11:05:29 -0500781/*
782 * how many bytes are required to store the items in a leaf. start
783 * and nr indicate which items in the leaf to check. This totals up the
784 * space used both by the item structs and the item data
785 */
Chris Mason234b63a2007-03-13 10:46:10 -0400786static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500787{
788 int data_len;
789 int end = start + nr - 1;
790
791 if (!nr)
792 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400793 data_len = btrfs_item_end(l->items + start);
794 data_len = data_len - btrfs_item_offset(l->items + end);
795 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500796 return data_len;
797}
798
Chris Mason74123bd2007-02-02 11:05:29 -0500799/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500800 * push some data in the path leaf to the right, trying to free up at
801 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500802 *
803 * returns 1 if the push failed because the other node didn't have enough
804 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500805 */
Chris Masone089f052007-03-16 16:20:31 -0400806static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
807 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500808{
Chris Masone20d96d2007-03-22 12:13:20 -0400809 struct buffer_head *left_buf = path->nodes[0];
810 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400811 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400812 struct buffer_head *right_buf;
813 struct buffer_head *upper;
814 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500815 int slot;
816 int i;
817 int free_space;
818 int push_space = 0;
819 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400820 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400821 u32 left_nritems;
822 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500823
824 slot = path->slots[1];
825 if (!path->nodes[1]) {
826 return 1;
827 }
828 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -0400829 upper_node = btrfs_buffer_node(upper);
830 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500831 return 1;
832 }
Chris Masone20d96d2007-03-22 12:13:20 -0400833 right_buf = read_tree_block(root,
834 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
835 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400836 free_space = btrfs_leaf_free_space(root, 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 Mason00ec4c52007-02-24 12:47:20 -0500839 return 1;
840 }
Chris Mason02217ed2007-03-02 16:08:05 -0500841 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400842 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400843 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400844 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400845 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400846 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500847 return 1;
848 }
849
Chris Mason7518a232007-03-12 12:01:18 -0400850 left_nritems = btrfs_header_nritems(&left->header);
851 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500852 item = left->items + i;
853 if (path->slots[0] == i)
854 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400855 if (btrfs_item_size(item) + sizeof(*item) + push_space >
856 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500857 break;
858 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400859 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500860 }
861 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400862 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500863 return 1;
864 }
Chris Mason7518a232007-03-12 12:01:18 -0400865 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500866 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400867 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400868 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500869 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400870 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
871 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
872 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500873 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400874 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
875 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500876 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400877 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500878 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400879 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400880 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500881
882 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400883 right_nritems += push_items;
884 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400885 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400886 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400887 btrfs_set_item_offset(right->items + i, push_space -
888 btrfs_item_size(right->items + i));
889 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500890 }
Chris Mason7518a232007-03-12 12:01:18 -0400891 left_nritems -= push_items;
892 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500893
Chris Masond5719762007-03-23 10:01:08 -0400894 mark_buffer_dirty(left_buf);
895 mark_buffer_dirty(right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400896 memcpy(&upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400897 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond5719762007-03-23 10:01:08 -0400898 mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -0500899
Chris Mason00ec4c52007-02-24 12:47:20 -0500900 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400901 if (path->slots[0] >= left_nritems) {
902 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400903 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500904 path->nodes[0] = right_buf;
905 path->slots[1] += 1;
906 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400907 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500908 }
909 return 0;
910}
911/*
Chris Mason74123bd2007-02-02 11:05:29 -0500912 * push some data in the path leaf to the left, trying to free up at
913 * least data_size bytes. returns zero if the push worked, nonzero otherwise
914 */
Chris Masone089f052007-03-16 16:20:31 -0400915static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
916 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500917{
Chris Masone20d96d2007-03-22 12:13:20 -0400918 struct buffer_head *right_buf = path->nodes[0];
919 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
920 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400921 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500922 int slot;
923 int i;
924 int free_space;
925 int push_space = 0;
926 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400927 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400928 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500929 int ret = 0;
930 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931
932 slot = path->slots[1];
933 if (slot == 0) {
934 return 1;
935 }
936 if (!path->nodes[1]) {
937 return 1;
938 }
Chris Masone20d96d2007-03-22 12:13:20 -0400939 t = read_tree_block(root,
940 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
941 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400942 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400943 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400944 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500945 return 1;
946 }
Chris Mason02217ed2007-03-02 16:08:05 -0500947
948 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400949 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -0400950 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400951 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400952 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400953 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500954 return 1;
955 }
956
Chris Mason7518a232007-03-12 12:01:18 -0400957 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500958 item = right->items + i;
959 if (path->slots[0] == i)
960 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400961 if (btrfs_item_size(item) + sizeof(*item) + push_space >
962 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500963 break;
964 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400965 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500966 }
967 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400968 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 return 1;
970 }
971 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400972 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400973 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400974 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400975 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400976 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
977 btrfs_leaf_data(right) +
978 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500979 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400980 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500981 BUG_ON(old_left_nritems < 0);
982
Chris Mason0783fcf2007-03-12 20:12:07 -0400983 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400984 u32 ioff = btrfs_item_offset(left->items + i);
985 btrfs_set_item_offset(left->items + i, ioff -
986 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400987 btrfs_item_offset(left->items +
988 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500989 }
Chris Mason7518a232007-03-12 12:01:18 -0400990 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500991
992 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400993 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -0400994 leaf_data_end(root, right);
995 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
996 push_space, btrfs_leaf_data(right) +
997 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500998 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400999 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001000 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001001 btrfs_set_header_nritems(&right->header,
1002 btrfs_header_nritems(&right->header) -
1003 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001004 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001005
Chris Mason7518a232007-03-12 12:01:18 -04001006 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001007 btrfs_set_item_offset(right->items + i, push_space -
1008 btrfs_item_size(right->items + i));
1009 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001010 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001011
Chris Masond5719762007-03-23 10:01:08 -04001012 mark_buffer_dirty(t);
1013 mark_buffer_dirty(right_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001014
Chris Masone089f052007-03-16 16:20:31 -04001015 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001016 if (wret)
1017 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001018
1019 /* then fixup the leaf pointer in the path */
1020 if (path->slots[0] < push_items) {
1021 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001022 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001023 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001024 path->slots[1] -= 1;
1025 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001026 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001027 path->slots[0] -= push_items;
1028 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001029 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001030 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001031}
1032
Chris Mason74123bd2007-02-02 11:05:29 -05001033/*
1034 * split the path's leaf in two, making sure there is at least data_size
1035 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001036 *
1037 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001038 */
Chris Masone089f052007-03-16 16:20:31 -04001039static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1040 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001041{
Chris Masone20d96d2007-03-22 12:13:20 -04001042 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001043 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001044 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001045 int mid;
1046 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001047 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001048 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001049 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001050 int data_copy_size;
1051 int rt_data_off;
1052 int i;
1053 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001054 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001055
Chris Mason40689472007-03-17 14:29:23 -04001056 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001057 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001058 if (wret < 0)
1059 return wret;
1060 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001061 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001062 if (wret < 0)
1063 return wret;
1064 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001065 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001066 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001067
1068 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001069 if (btrfs_leaf_free_space(root, l) >=
1070 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001071 return 0;
1072
Chris Mason5c680ed2007-02-22 11:39:13 -05001073 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001074 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001075 if (ret)
1076 return ret;
1077 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001078 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001079 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001080 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001081 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001082 BUG_ON(!right_buffer);
1083 BUG_ON(mid == nritems);
Chris Masone20d96d2007-03-22 12:13:20 -04001084 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001085 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001086 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001087 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001089 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001090 BUG();
1091 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001092 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001093 if (leaf_space_used(l, 0, mid + 1) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001094 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001095 BUG();
1096 }
Chris Mason7518a232007-03-12 12:01:18 -04001097 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Masone20d96d2007-03-22 12:13:20 -04001098 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -04001099 btrfs_set_header_level(&right->header, 0);
1100 btrfs_set_header_parentid(&right->header,
Chris Masone20d96d2007-03-22 12:13:20 -04001101 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason123abc82007-03-14 14:14:43 -04001102 data_copy_size = btrfs_item_end(l->items + mid) -
1103 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001104 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001105 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001106 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1107 data_copy_size, btrfs_leaf_data(l) +
1108 leaf_data_end(root, l), data_copy_size);
1109 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1110 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001111
Chris Mason0783fcf2007-03-12 20:12:07 -04001112 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001113 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001114 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1115 }
Chris Mason74123bd2007-02-02 11:05:29 -05001116
Chris Mason7518a232007-03-12 12:01:18 -04001117 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001118 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001119 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -04001120 right_buffer->b_blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001121 if (wret)
1122 ret = wret;
Chris Masond5719762007-03-23 10:01:08 -04001123 mark_buffer_dirty(right_buffer);
1124 mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001125 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001126 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001127 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001128 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001129 path->slots[0] -= mid;
1130 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001131 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001132 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001133 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001134 return ret;
1135}
1136
Chris Mason74123bd2007-02-02 11:05:29 -05001137/*
1138 * Given a key and some data, insert an item into the tree.
1139 * This does all the path init required, making room in the tree if needed.
1140 */
Chris Masone089f052007-03-16 16:20:31 -04001141int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1142 *root, struct btrfs_path *path, struct btrfs_key
1143 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001144{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001145 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001146 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001147 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001148 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001149 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001150 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001151 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001152 struct btrfs_disk_key disk_key;
1153
1154 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001155
Chris Mason74123bd2007-02-02 11:05:29 -05001156 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001157 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001158 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001159 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001160 if (ret == 0) {
Chris Mason62e27492007-03-15 12:56:47 -04001161 btrfs_release_path(root, path);
Chris Masonf0930a32007-03-02 09:47:58 -05001162 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001163 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001164 if (ret < 0)
1165 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001166
Chris Mason62e27492007-03-15 12:56:47 -04001167 slot_orig = path->slots[0];
1168 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001169 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001170
Chris Mason7518a232007-03-12 12:01:18 -04001171 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001172 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001173
Chris Mason123abc82007-03-14 14:14:43 -04001174 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001175 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001176 BUG();
1177
Chris Mason62e27492007-03-15 12:56:47 -04001178 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001179 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001180 if (slot != nritems) {
1181 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001182 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001183
1184 /*
1185 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1186 */
1187 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001188 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001189 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001190 btrfs_set_item_offset(leaf->items + i,
1191 ioff - data_size);
1192 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193
1194 /* shift the items */
1195 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001196 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001197
1198 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001199 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1200 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201 data_end, old_data - data_end);
1202 data_end = old_data;
1203 }
Chris Mason62e27492007-03-15 12:56:47 -04001204 /* setup the item for the new data */
Chris Masone2fa7222007-03-12 16:22:34 -04001205 memcpy(&leaf->items[slot].key, &disk_key,
1206 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001207 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1208 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001209 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond5719762007-03-23 10:01:08 -04001210 mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001211
1212 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001213 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001214 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001215
Chris Mason123abc82007-03-14 14:14:43 -04001216 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001217 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001218 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001219out:
Chris Mason62e27492007-03-15 12:56:47 -04001220 return ret;
1221}
1222
1223/*
1224 * Given a key and some data, insert an item into the tree.
1225 * This does all the path init required, making room in the tree if needed.
1226 */
Chris Masone089f052007-03-16 16:20:31 -04001227int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1228 *root, struct btrfs_key *cpu_key, void *data, u32
1229 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001230{
1231 int ret = 0;
1232 struct btrfs_path path;
1233 u8 *ptr;
1234
1235 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -04001236 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001237 if (!ret) {
Chris Masone20d96d2007-03-22 12:13:20 -04001238 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
1239 path.slots[0], u8);
Chris Mason62e27492007-03-15 12:56:47 -04001240 memcpy(ptr, data, data_size);
Chris Masond5719762007-03-23 10:01:08 -04001241 mark_buffer_dirty(path.nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001242 }
Chris Mason234b63a2007-03-13 10:46:10 -04001243 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001244 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001245}
1246
Chris Mason74123bd2007-02-02 11:05:29 -05001247/*
Chris Mason5de08d72007-02-24 06:24:44 -05001248 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001249 *
1250 * If the delete empties a node, the node is removed from the tree,
1251 * continuing all the way the root if required. The root is converted into
1252 * a leaf if all the nodes are emptied.
1253 */
Chris Masone089f052007-03-16 16:20:31 -04001254static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1255 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256{
Chris Mason234b63a2007-03-13 10:46:10 -04001257 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001258 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001259 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001260 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001261 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001262
Chris Masone20d96d2007-03-22 12:13:20 -04001263 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001264 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001265 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001266 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1267 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001268 }
Chris Mason7518a232007-03-12 12:01:18 -04001269 nritems--;
1270 btrfs_set_header_nritems(&node->header, nritems);
1271 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001272 struct btrfs_header *header = btrfs_buffer_header(root->node);
1273 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001274 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001275 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001276 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001277 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001278 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001279 if (wret)
1280 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001281 }
Chris Masond5719762007-03-23 10:01:08 -04001282 mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001283 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001284}
1285
Chris Mason74123bd2007-02-02 11:05:29 -05001286/*
1287 * delete the item at the leaf level in path. If that empties
1288 * the leaf, remove it from the tree
1289 */
Chris Masone089f052007-03-16 16:20:31 -04001290int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1291 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001292{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001293 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001294 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001295 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001296 int doff;
1297 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001298 int ret = 0;
1299 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001300 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001301
Chris Masoneb60cea2007-02-02 09:18:22 -05001302 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001303 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001304 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001305 doff = btrfs_item_offset(leaf->items + slot);
1306 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001307 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001308
Chris Mason7518a232007-03-12 12:01:18 -04001309 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001310 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001311 int data_end = leaf_data_end(root, leaf);
1312 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1313 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001314 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001315 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001316 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001317 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1318 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001319 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001320 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001321 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001322 }
Chris Mason7518a232007-03-12 12:01:18 -04001323 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1324 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001325 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001326 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001327 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001328 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001329 } else {
Chris Masone089f052007-03-16 16:20:31 -04001330 clean_tree_block(trans, root, leaf_buf);
1331 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001332 if (wret)
1333 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001334 wret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -04001335 leaf_buf->b_blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001336 if (wret)
1337 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001338 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001339 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001340 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001341 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001342 wret = fixup_low_keys(trans, root, path,
1343 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001344 if (wret)
1345 ret = wret;
1346 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001347
Chris Mason74123bd2007-02-02 11:05:29 -05001348 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001349 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001350 /* push_leaf_left fixes the path.
1351 * make sure the path still points to our leaf
1352 * for possible call to del_ptr below
1353 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001354 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001355 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001356 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001357 if (wret < 0)
1358 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001359 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001360 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001361 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001362 if (wret < 0)
1363 ret = wret;
1364 }
Chris Mason7518a232007-03-12 12:01:18 -04001365 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -04001366 u64 blocknr = leaf_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001367 clean_tree_block(trans, root, leaf_buf);
1368 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001369 if (wret)
1370 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001371 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001372 wret = btrfs_free_extent(trans, root, blocknr,
1373 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001374 if (wret)
1375 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001376 } else {
Chris Masond5719762007-03-23 10:01:08 -04001377 mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001378 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001379 }
Chris Masond5719762007-03-23 10:01:08 -04001380 } else {
1381 mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001382 }
1383 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001384 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001385}
1386
Chris Mason97571fd2007-02-24 13:39:08 -05001387/*
1388 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001389 * returns 0 if it found something or 1 if there are no greater leaves.
1390 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001391 */
Chris Mason234b63a2007-03-13 10:46:10 -04001392int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001393{
1394 int slot;
1395 int level = 1;
1396 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001397 struct buffer_head *c;
1398 struct btrfs_node *c_node;
1399 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001400
Chris Mason234b63a2007-03-13 10:46:10 -04001401 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001402 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001403 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001404 slot = path->slots[level] + 1;
1405 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001406 c_node = btrfs_buffer_node(c);
1407 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001408 level++;
1409 continue;
1410 }
Chris Masone20d96d2007-03-22 12:13:20 -04001411 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001412 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001413 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001414 next = read_tree_block(root, blocknr);
1415 break;
1416 }
1417 path->slots[level] = slot;
1418 while(1) {
1419 level--;
1420 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001421 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001422 path->nodes[level] = next;
1423 path->slots[level] = 0;
1424 if (!level)
1425 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001426 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001427 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001428 }
1429 return 0;
1430}