blob: 39b551564fcf4e9333fa48af47e241f17bc5fbdc [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
Chris Masond4dbff92007-04-04 14:08:15 -04009 *root, struct btrfs_key *ins_key,
10 struct btrfs_path *path, int data_size);
Chris Masone089f052007-03-16 16:20:31 -040011static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040012 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040013 *src);
14static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040015 btrfs_root *root, struct buffer_head *dst_buf,
16 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040017static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
18 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050019
Chris Masondf24a2b2007-04-04 09:36:31 -040020inline void btrfs_init_path(struct btrfs_path *p)
21{
22 memset(p, 0, sizeof(*p));
23}
24
Chris Mason2c90e5d2007-04-02 10:50:19 -040025struct btrfs_path *btrfs_alloc_path(void)
26{
Chris Masondf24a2b2007-04-04 09:36:31 -040027 struct btrfs_path *path;
28 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
29 if (path)
30 btrfs_init_path(path);
31 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040032}
33
34void btrfs_free_path(struct btrfs_path *p)
35{
Chris Masondf24a2b2007-04-04 09:36:31 -040036 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040037 kmem_cache_free(btrfs_path_cachep, p);
38}
39
Chris Mason234b63a2007-03-13 10:46:10 -040040void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050041{
42 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040043 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050044 if (!p->nodes[i])
45 break;
Chris Mason234b63a2007-03-13 10:46:10 -040046 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050047 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050048 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050049}
50
Chris Masone089f052007-03-16 16:20:31 -040051static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040052 *root, struct buffer_head *buf, struct buffer_head
53 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040054 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050055{
Chris Masone20d96d2007-03-22 12:13:20 -040056 struct buffer_head *cow;
57 struct btrfs_node *cow_node;
Chris Mason02217ed2007-03-02 16:08:05 -050058
Chris Mason7f5c1512007-03-23 15:56:19 -040059 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
60 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -050061 *cow_ret = buf;
62 return 0;
63 }
Chris Masone089f052007-03-16 16:20:31 -040064 cow = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -040065 cow_node = btrfs_buffer_node(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040066 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
67 WARN_ON(1);
Chris Masone20d96d2007-03-22 12:13:20 -040068 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
69 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -040070 btrfs_set_header_generation(&cow_node->header, trans->transid);
Chris Masone089f052007-03-16 16:20:31 -040071 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050072 if (buf == root->node) {
73 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040074 get_bh(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040075 if (buf != root->commit_root) {
Chris Masone20d96d2007-03-22 12:13:20 -040076 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason2c90e5d2007-04-02 10:50:19 -040077 }
Chris Mason234b63a2007-03-13 10:46:10 -040078 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050079 } else {
Chris Masone20d96d2007-03-22 12:13:20 -040080 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
81 cow->b_blocknr);
Chris Masond6025572007-03-30 14:27:56 -040082 btrfs_mark_buffer_dirty(parent);
Chris Masone20d96d2007-03-22 12:13:20 -040083 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050084 }
Chris Mason234b63a2007-03-13 10:46:10 -040085 btrfs_block_release(root, buf);
Chris Masondf24a2b2007-04-04 09:36:31 -040086 mark_buffer_dirty(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040087 *cow_ret = cow;
Chris Mason02217ed2007-03-02 16:08:05 -050088 return 0;
89}
90
Chris Mason74123bd2007-02-02 11:05:29 -050091/*
92 * The leaf data grows from end-to-front in the node.
93 * this returns the address of the start of the last item,
94 * which is the stop of the leaf data stack
95 */
Chris Mason123abc82007-03-14 14:14:43 -040096static inline unsigned int leaf_data_end(struct btrfs_root *root,
97 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050098{
Chris Mason7518a232007-03-12 12:01:18 -040099 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500100 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400101 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400102 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500103}
104
Chris Mason74123bd2007-02-02 11:05:29 -0500105/*
Chris Mason74123bd2007-02-02 11:05:29 -0500106 * compare two keys in a memcmp fashion
107 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400108static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500109{
Chris Masone2fa7222007-03-12 16:22:34 -0400110 struct btrfs_key k1;
111
112 btrfs_disk_key_to_cpu(&k1, disk);
113
114 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500115 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400116 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500117 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400118 if (k1.offset > k2->offset)
119 return 1;
120 if (k1.offset < k2->offset)
121 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400122 if (k1.flags > k2->flags)
123 return 1;
124 if (k1.flags < k2->flags)
125 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500126 return 0;
127}
Chris Mason74123bd2007-02-02 11:05:29 -0500128
Chris Mason123abc82007-03-14 14:14:43 -0400129static int check_node(struct btrfs_root *root, struct btrfs_path *path,
130 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500131{
132 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400133 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400134 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500135 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400136 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500137
138 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400139 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500140 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400141 BUG_ON(nritems == 0);
142 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400143 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400144 parent_key = &parent->ptrs[parent_slot].key;
145 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400146 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400147 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400148 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500149 }
Chris Mason123abc82007-03-14 14:14:43 -0400150 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400151 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400152 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400153 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
154 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155 }
156 return 0;
157}
158
Chris Mason123abc82007-03-14 14:14:43 -0400159static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
160 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500161{
162 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400163 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400164 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500165 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400166 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500167
168 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400169 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500170 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400171 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400172
173 if (nritems == 0)
174 return 0;
175
176 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400177 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400178 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500179 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400180 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400181 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400182 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500183 }
Chris Mason7518a232007-03-12 12:01:18 -0400184 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400185 struct btrfs_key cpukey;
186 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500187 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400188 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400189 BUG_ON(btrfs_item_offset(leaf->items + i) !=
190 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500191 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400192 BUG_ON(btrfs_item_offset(leaf->items + i) +
193 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400194 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500195 }
196 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500197 return 0;
198}
199
Chris Mason123abc82007-03-14 14:14:43 -0400200static int check_block(struct btrfs_root *root, struct btrfs_path *path,
201 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500202{
Chris Mason3eb03142007-04-05 14:28:50 -0400203 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
204 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
205 sizeof(node->header.fsid)))
206 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500207 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400208 return check_leaf(root, path, level);
209 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500210}
211
Chris Mason74123bd2007-02-02 11:05:29 -0500212/*
213 * search for key in the array p. items p are item_size apart
214 * and there are 'max' items in p
215 * the slot in the array is returned via slot, and it points to
216 * the place where you would insert key if it is not found in
217 * the array.
218 *
219 * slot may point to max if the key is bigger than all of the keys
220 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400221static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500222 int max, int *slot)
223{
224 int low = 0;
225 int high = max;
226 int mid;
227 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400228 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500229
230 while(low < high) {
231 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400232 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500233 ret = comp_keys(tmp, key);
234
235 if (ret < 0)
236 low = mid + 1;
237 else if (ret > 0)
238 high = mid;
239 else {
240 *slot = mid;
241 return 0;
242 }
243 }
244 *slot = low;
245 return 1;
246}
247
Chris Mason97571fd2007-02-24 13:39:08 -0500248/*
249 * simple bin_search frontend that does the right thing for
250 * leaves vs nodes
251 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400252static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500253{
Chris Mason7518a232007-03-12 12:01:18 -0400254 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400255 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400256 return generic_bin_search((void *)l->items,
257 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400258 key, btrfs_header_nritems(&c->header),
259 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500260 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400261 return generic_bin_search((void *)c->ptrs,
262 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400263 key, btrfs_header_nritems(&c->header),
264 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500265 }
266 return -1;
267}
268
Chris Masone20d96d2007-03-22 12:13:20 -0400269static struct buffer_head *read_node_slot(struct btrfs_root *root,
270 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500271 int slot)
272{
Chris Masone20d96d2007-03-22 12:13:20 -0400273 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500274 if (slot < 0)
275 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400276 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500277 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400278 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500279}
280
Chris Masone089f052007-03-16 16:20:31 -0400281static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
282 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500283{
Chris Masone20d96d2007-03-22 12:13:20 -0400284 struct buffer_head *right_buf;
285 struct buffer_head *mid_buf;
286 struct buffer_head *left_buf;
287 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400288 struct btrfs_node *right = NULL;
289 struct btrfs_node *mid;
290 struct btrfs_node *left = NULL;
291 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500292 int ret = 0;
293 int wret;
294 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500295 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500296 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500297
298 if (level == 0)
299 return 0;
300
301 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400302 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400303 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500304
Chris Mason234b63a2007-03-13 10:46:10 -0400305 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500306 parent_buf = path->nodes[level + 1];
307 pslot = path->slots[level + 1];
308
Chris Mason40689472007-03-17 14:29:23 -0400309 /*
310 * deal with the case where there is only one pointer in the root
311 * by promoting the node below to a root
312 */
Chris Masonbb803952007-03-01 12:04:21 -0500313 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400314 struct buffer_head *child;
315 u64 blocknr = mid_buf->b_blocknr;
Chris Masonbb803952007-03-01 12:04:21 -0500316
Chris Mason7518a232007-03-12 12:01:18 -0400317 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500318 return 0;
319
320 /* promote the child to a root */
321 child = read_node_slot(root, mid_buf, 0);
322 BUG_ON(!child);
323 root->node = child;
324 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400325 clean_tree_block(trans, root, mid_buf);
326 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500327 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400328 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500329 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400330 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400331 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500332 }
Chris Masone20d96d2007-03-22 12:13:20 -0400333 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500334
Chris Mason123abc82007-03-14 14:14:43 -0400335 if (btrfs_header_nritems(&mid->header) >
336 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500337 return 0;
338
Chris Masonbb803952007-03-01 12:04:21 -0500339 left_buf = read_node_slot(root, parent_buf, pslot - 1);
340 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500341
342 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500343 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400344 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
345 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400346 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400347 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400348 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500349 if (wret < 0)
350 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500351 }
Chris Mason79f95c82007-03-01 15:16:26 -0500352
353 /*
354 * then try to empty the right most buffer into the middle
355 */
Chris Masonbb803952007-03-01 12:04:21 -0500356 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400357 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
358 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400359 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400360 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500361 if (wret < 0)
362 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400363 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -0400364 u64 blocknr = right_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -0400365 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400366 wait_on_buffer(right_buf);
367 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500368 right_buf = NULL;
369 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400370 wret = del_ptr(trans, root, path, level + 1, pslot +
371 1);
Chris Masonbb803952007-03-01 12:04:21 -0500372 if (wret)
373 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400374 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500375 if (wret)
376 ret = wret;
377 } else {
Chris Masond6025572007-03-30 14:27:56 -0400378 btrfs_memcpy(root, parent,
379 &parent->ptrs[pslot + 1].key,
380 &right->ptrs[0].key,
381 sizeof(struct btrfs_disk_key));
382 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500383 }
384 }
Chris Mason7518a232007-03-12 12:01:18 -0400385 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500386 /*
387 * we're not allowed to leave a node with one item in the
388 * tree during a delete. A deletion from lower in the tree
389 * could try to delete the only pointer in this node.
390 * So, pull some keys from the left.
391 * There has to be a left pointer at this point because
392 * otherwise we would have pulled some pointers from the
393 * right
394 */
395 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400396 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500397 if (wret < 0)
398 ret = wret;
399 BUG_ON(wret == 1);
400 }
Chris Mason7518a232007-03-12 12:01:18 -0400401 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500402 /* we've managed to empty the middle node, drop it */
Chris Masone20d96d2007-03-22 12:13:20 -0400403 u64 blocknr = mid_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -0400404 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400405 wait_on_buffer(mid_buf);
406 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500407 mid_buf = NULL;
408 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400409 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500410 if (wret)
411 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400412 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500413 if (wret)
414 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500415 } else {
416 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400417 btrfs_memcpy(root, parent,
418 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
419 sizeof(struct btrfs_disk_key));
420 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500421 }
Chris Masonbb803952007-03-01 12:04:21 -0500422
Chris Mason79f95c82007-03-01 15:16:26 -0500423 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500424 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400425 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400426 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500427 path->nodes[level] = left_buf;
428 path->slots[level + 1] -= 1;
429 path->slots[level] = orig_slot;
430 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400431 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500432 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400433 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500434 path->slots[level] = orig_slot;
435 }
436 }
Chris Mason79f95c82007-03-01 15:16:26 -0500437 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400438 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400439 if (orig_ptr !=
440 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
441 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500442 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500443
444 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400445 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500446 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400447 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500448 return ret;
449}
450
Chris Mason74123bd2007-02-02 11:05:29 -0500451/*
452 * look for key in the tree. path is filled in with nodes along the way
453 * if key is found, we return zero and you can find the item in the leaf
454 * level of the path (level 0)
455 *
456 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500457 * be inserted, and 1 is returned. If there are other errors during the
458 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500459 *
460 * if ins_len > 0, nodes and leaves will be split as we walk down the
461 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
462 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500463 */
Chris Masone089f052007-03-16 16:20:31 -0400464int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
465 *root, struct btrfs_key *key, struct btrfs_path *p, int
466 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500467{
Chris Masone20d96d2007-03-22 12:13:20 -0400468 struct buffer_head *b;
469 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400470 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500471 int slot;
472 int ret;
473 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500474
Chris Mason22b0ebd2007-03-30 08:47:31 -0400475 WARN_ON(p->nodes[0] != NULL);
476 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500477again:
478 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400479 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500480 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400481 c = btrfs_buffer_node(b);
482 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500483 if (cow) {
484 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400485 wret = btrfs_cow_block(trans, root, b,
486 p->nodes[level + 1],
487 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400488 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500489 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400490 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500491 }
492 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400493 if (level != btrfs_header_level(&c->header))
494 WARN_ON(1);
495 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500496 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400497 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500498 if (ret)
499 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500500 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400501 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500502 if (ret && slot > 0)
503 slot -= 1;
504 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400505 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
506 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400507 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500508 BUG_ON(sret > 0);
509 if (sret)
510 return sret;
511 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400512 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500513 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500514 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400515 int sret = balance_level(trans, root, p,
516 level);
Chris Masonbb803952007-03-01 12:04:21 -0500517 if (sret)
518 return sret;
519 b = p->nodes[level];
520 if (!b)
521 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400522 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500523 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400524 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500525 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400526 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500527 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400528 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500529 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400530 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400531 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400532 int sret = split_leaf(trans, root, key,
533 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500534 BUG_ON(sret > 0);
535 if (sret)
536 return sret;
537 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500538 return ret;
539 }
540 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500541 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500542}
543
Chris Mason74123bd2007-02-02 11:05:29 -0500544/*
545 * adjust the pointers going up the tree, starting at level
546 * making sure the right key of each node is points to 'key'.
547 * This is used after shifting pointers to the left, so it stops
548 * fixing up pointers when a given leaf/node is not in slot 0 of the
549 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500550 *
551 * If this fails to write a tree block, it returns -1, but continues
552 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500553 */
Chris Masone089f052007-03-16 16:20:31 -0400554static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
555 *root, struct btrfs_path *path, struct btrfs_disk_key
556 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500557{
558 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500559 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400560 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
561 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500562 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500563 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500564 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400565 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400566 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
567 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500568 if (tslot != 0)
569 break;
570 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500571 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500572}
573
Chris Mason74123bd2007-02-02 11:05:29 -0500574/*
575 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500576 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500577 *
578 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
579 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500580 */
Chris Masone089f052007-03-16 16:20:31 -0400581static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400582 *root, struct buffer_head *dst_buf, struct
583 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500584{
Chris Masone20d96d2007-03-22 12:13:20 -0400585 struct btrfs_node *src = btrfs_buffer_node(src_buf);
586 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500587 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500588 int src_nritems;
589 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500590 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500591
Chris Mason7518a232007-03-12 12:01:18 -0400592 src_nritems = btrfs_header_nritems(&src->header);
593 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400594 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500595 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500596 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500597 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500598
599 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500600 push_items = src_nritems;
601
Chris Masond6025572007-03-30 14:27:56 -0400602 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
603 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500604 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400605 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400606 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400607 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500608 }
Chris Mason7518a232007-03-12 12:01:18 -0400609 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
610 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -0400611 btrfs_mark_buffer_dirty(src_buf);
612 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500613 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500614}
615
Chris Mason97571fd2007-02-24 13:39:08 -0500616/*
Chris Mason79f95c82007-03-01 15:16:26 -0500617 * try to push data from one node into the next node right in the
618 * tree.
619 *
620 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
621 * error, and > 0 if there was no room in the right hand block.
622 *
623 * this will only push up to 1/2 the contents of the left node over
624 */
Chris Masone089f052007-03-16 16:20:31 -0400625static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400626 btrfs_root *root, struct buffer_head *dst_buf,
627 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500628{
Chris Masone20d96d2007-03-22 12:13:20 -0400629 struct btrfs_node *src = btrfs_buffer_node(src_buf);
630 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500631 int push_items = 0;
632 int max_push;
633 int src_nritems;
634 int dst_nritems;
635 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500636
Chris Mason7518a232007-03-12 12:01:18 -0400637 src_nritems = btrfs_header_nritems(&src->header);
638 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400639 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500640 if (push_items <= 0) {
641 return 1;
642 }
643
644 max_push = src_nritems / 2 + 1;
645 /* don't try to empty the node */
646 if (max_push > src_nritems)
647 return 1;
648 if (max_push < push_items)
649 push_items = max_push;
650
Chris Masond6025572007-03-30 14:27:56 -0400651 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
652 dst_nritems * sizeof(struct btrfs_key_ptr));
653
654 btrfs_memcpy(root, dst, dst->ptrs,
655 src->ptrs + src_nritems - push_items,
656 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500657
Chris Mason7518a232007-03-12 12:01:18 -0400658 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
659 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500660
Chris Masond6025572007-03-30 14:27:56 -0400661 btrfs_mark_buffer_dirty(src_buf);
662 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500663 return ret;
664}
665
666/*
Chris Mason97571fd2007-02-24 13:39:08 -0500667 * helper function to insert a new root level in the tree.
668 * A new node is allocated, and a single item is inserted to
669 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500670 *
671 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500672 */
Chris Masone089f052007-03-16 16:20:31 -0400673static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
674 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500675{
Chris Masone20d96d2007-03-22 12:13:20 -0400676 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400677 struct btrfs_node *lower;
678 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400679 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500680
681 BUG_ON(path->nodes[level]);
682 BUG_ON(path->nodes[level-1] != root->node);
683
Chris Masone089f052007-03-16 16:20:31 -0400684 t = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400685 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400686 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400687 btrfs_set_header_nritems(&c->header, 1);
688 btrfs_set_header_level(&c->header, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400689 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -0400690 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -0400691 btrfs_set_header_parentid(&c->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400692 btrfs_header_parentid(btrfs_buffer_header(root->node)));
693 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -0400694 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
695 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400696 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400697 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500698 else
Chris Mason123abc82007-03-14 14:14:43 -0400699 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -0400700 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
701 sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400702 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
Chris Masond5719762007-03-23 10:01:08 -0400703
Chris Masond6025572007-03-30 14:27:56 -0400704 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -0400705
Chris Mason5c680ed2007-02-22 11:39:13 -0500706 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400707 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500708 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400709 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500710 path->nodes[level] = t;
711 path->slots[level] = 0;
712 return 0;
713}
714
Chris Mason74123bd2007-02-02 11:05:29 -0500715/*
716 * worker function to insert a single pointer in a node.
717 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500718 *
Chris Mason74123bd2007-02-02 11:05:29 -0500719 * slot and level indicate where you want the key to go, and
720 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500721 *
722 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500723 */
Chris Masone089f052007-03-16 16:20:31 -0400724static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
725 *root, struct btrfs_path *path, struct btrfs_disk_key
726 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500727{
Chris Mason234b63a2007-03-13 10:46:10 -0400728 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500729 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500730
731 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400732 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400733 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500734 if (slot > nritems)
735 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400736 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500737 BUG();
738 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400739 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
740 lower->ptrs + slot,
741 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500742 }
Chris Masond6025572007-03-30 14:27:56 -0400743 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
744 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400745 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400746 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -0400747 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500748 return 0;
749}
750
Chris Mason97571fd2007-02-24 13:39:08 -0500751/*
752 * split the node at the specified level in path in two.
753 * The path is corrected to point to the appropriate node after the split
754 *
755 * Before splitting this tries to make some room in the node by pushing
756 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500757 *
758 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500759 */
Chris Masone089f052007-03-16 16:20:31 -0400760static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
761 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500762{
Chris Masone20d96d2007-03-22 12:13:20 -0400763 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400764 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400765 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400766 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500767 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500768 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500769 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400770 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500771
Chris Mason5c680ed2007-02-22 11:39:13 -0500772 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400773 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500774 if (t == root->node) {
775 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400776 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500777 if (ret)
778 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779 }
Chris Mason7518a232007-03-12 12:01:18 -0400780 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400781 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400782 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400783 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -0400784 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Masone20d96d2007-03-22 12:13:20 -0400785 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -0400786 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -0400787 btrfs_set_header_parentid(&split->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400788 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason3eb03142007-04-05 14:28:50 -0400789 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
790 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400791 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -0400792 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
793 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400794 btrfs_set_header_nritems(&split->header, c_nritems - mid);
795 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500796 ret = 0;
797
Chris Masond6025572007-03-30 14:27:56 -0400798 btrfs_mark_buffer_dirty(t);
799 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400800 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -0400801 split_buffer->b_blocknr, path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400802 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500803 if (wret)
804 ret = wret;
805
Chris Mason5de08d72007-02-24 06:24:44 -0500806 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500807 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400808 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500809 path->nodes[level] = split_buffer;
810 path->slots[level + 1] += 1;
811 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400812 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500813 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500814 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500815}
816
Chris Mason74123bd2007-02-02 11:05:29 -0500817/*
818 * how many bytes are required to store the items in a leaf. start
819 * and nr indicate which items in the leaf to check. This totals up the
820 * space used both by the item structs and the item data
821 */
Chris Mason234b63a2007-03-13 10:46:10 -0400822static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823{
824 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -0400825 int nritems = btrfs_header_nritems(&l->header);
826 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500827
828 if (!nr)
829 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400830 data_len = btrfs_item_end(l->items + start);
831 data_len = data_len - btrfs_item_offset(l->items + end);
832 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -0400833 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500834 return data_len;
835}
836
Chris Mason74123bd2007-02-02 11:05:29 -0500837/*
Chris Masond4dbff92007-04-04 14:08:15 -0400838 * The space between the end of the leaf items and
839 * the start of the leaf data. IOW, how much room
840 * the leaf has left for both items and data
841 */
842int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
843{
844 int nritems = btrfs_header_nritems(&leaf->header);
845 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
846}
847
848/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500849 * push some data in the path leaf to the right, trying to free up at
850 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500851 *
852 * returns 1 if the push failed because the other node didn't have enough
853 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500854 */
Chris Masone089f052007-03-16 16:20:31 -0400855static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
856 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500857{
Chris Masone20d96d2007-03-22 12:13:20 -0400858 struct buffer_head *left_buf = path->nodes[0];
859 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400860 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400861 struct buffer_head *right_buf;
862 struct buffer_head *upper;
863 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500864 int slot;
865 int i;
866 int free_space;
867 int push_space = 0;
868 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400869 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400870 u32 left_nritems;
871 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500872
873 slot = path->slots[1];
874 if (!path->nodes[1]) {
875 return 1;
876 }
877 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -0400878 upper_node = btrfs_buffer_node(upper);
879 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500880 return 1;
881 }
Chris Masone20d96d2007-03-22 12:13:20 -0400882 right_buf = read_tree_block(root,
883 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
884 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400885 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400886 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400887 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500888 return 1;
889 }
Chris Mason02217ed2007-03-02 16:08:05 -0500890 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400891 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400892 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400893 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400894 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400895 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500896 return 1;
897 }
898
Chris Mason7518a232007-03-12 12:01:18 -0400899 left_nritems = btrfs_header_nritems(&left->header);
900 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500901 item = left->items + i;
902 if (path->slots[0] == i)
903 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400904 if (btrfs_item_size(item) + sizeof(*item) + push_space >
905 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500906 break;
907 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400908 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500909 }
910 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400911 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500912 return 1;
913 }
Chris Mason7518a232007-03-12 12:01:18 -0400914 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500915 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400916 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400917 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500918 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -0400919 btrfs_memmove(root, right, btrfs_leaf_data(right) +
920 leaf_data_end(root, right) - push_space,
921 btrfs_leaf_data(right) +
922 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
923 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500924 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -0400925 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
926 BTRFS_LEAF_DATA_SIZE(root) - push_space,
927 btrfs_leaf_data(left) + leaf_data_end(root, left),
928 push_space);
929 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400930 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500931 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -0400932 btrfs_memcpy(root, right, right->items, left->items +
933 left_nritems - push_items,
934 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500935
936 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400937 right_nritems += push_items;
938 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400939 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400940 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400941 btrfs_set_item_offset(right->items + i, push_space -
942 btrfs_item_size(right->items + i));
943 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500944 }
Chris Mason7518a232007-03-12 12:01:18 -0400945 left_nritems -= push_items;
946 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500947
Chris Masond6025572007-03-30 14:27:56 -0400948 btrfs_mark_buffer_dirty(left_buf);
949 btrfs_mark_buffer_dirty(right_buf);
950 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400951 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -0400952 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -0500953
Chris Mason00ec4c52007-02-24 12:47:20 -0500954 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400955 if (path->slots[0] >= left_nritems) {
956 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400957 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500958 path->nodes[0] = right_buf;
959 path->slots[1] += 1;
960 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400961 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500962 }
963 return 0;
964}
965/*
Chris Mason74123bd2007-02-02 11:05:29 -0500966 * push some data in the path leaf to the left, trying to free up at
967 * least data_size bytes. returns zero if the push worked, nonzero otherwise
968 */
Chris Masone089f052007-03-16 16:20:31 -0400969static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
970 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500971{
Chris Masone20d96d2007-03-22 12:13:20 -0400972 struct buffer_head *right_buf = path->nodes[0];
973 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
974 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400975 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500976 int slot;
977 int i;
978 int free_space;
979 int push_space = 0;
980 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400981 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400982 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500983 int ret = 0;
984 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500985
986 slot = path->slots[1];
987 if (slot == 0) {
988 return 1;
989 }
990 if (!path->nodes[1]) {
991 return 1;
992 }
Chris Masone20d96d2007-03-22 12:13:20 -0400993 t = read_tree_block(root,
994 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
995 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400996 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400997 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400998 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500999 return 1;
1000 }
Chris Mason02217ed2007-03-02 16:08:05 -05001001
1002 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -04001003 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -04001004 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001005 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001006 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001007 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001008 return 1;
1009 }
1010
Chris Mason7518a232007-03-12 12:01:18 -04001011 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001012 item = right->items + i;
1013 if (path->slots[0] == i)
1014 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001015 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1016 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001017 break;
1018 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001019 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001020 }
1021 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001022 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001023 return 1;
1024 }
1025 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001026 btrfs_memcpy(root, left, left->items +
1027 btrfs_header_nritems(&left->header),
1028 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001029 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001030 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001031 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1032 leaf_data_end(root, left) - push_space,
1033 btrfs_leaf_data(right) +
1034 btrfs_item_offset(right->items + push_items - 1),
1035 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001036 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001037 BUG_ON(old_left_nritems < 0);
1038
Chris Mason0783fcf2007-03-12 20:12:07 -04001039 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001040 u32 ioff = btrfs_item_offset(left->items + i);
1041 btrfs_set_item_offset(left->items + i, ioff -
1042 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001043 btrfs_item_offset(left->items +
1044 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001045 }
Chris Mason7518a232007-03-12 12:01:18 -04001046 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001047
1048 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001049 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001050 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001051 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1052 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1053 btrfs_leaf_data(right) +
1054 leaf_data_end(root, right), push_space);
1055 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001056 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001057 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001058 btrfs_set_header_nritems(&right->header,
1059 btrfs_header_nritems(&right->header) -
1060 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001061 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001062
Chris Mason7518a232007-03-12 12:01:18 -04001063 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001064 btrfs_set_item_offset(right->items + i, push_space -
1065 btrfs_item_size(right->items + i));
1066 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001067 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001068
Chris Masond6025572007-03-30 14:27:56 -04001069 btrfs_mark_buffer_dirty(t);
1070 btrfs_mark_buffer_dirty(right_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001071
Chris Masone089f052007-03-16 16:20:31 -04001072 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001073 if (wret)
1074 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001075
1076 /* then fixup the leaf pointer in the path */
1077 if (path->slots[0] < push_items) {
1078 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001079 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001080 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001081 path->slots[1] -= 1;
1082 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001083 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001084 path->slots[0] -= push_items;
1085 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001086 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001087 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088}
1089
Chris Mason74123bd2007-02-02 11:05:29 -05001090/*
1091 * split the path's leaf in two, making sure there is at least data_size
1092 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001093 *
1094 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001095 */
Chris Masone089f052007-03-16 16:20:31 -04001096static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001097 *root, struct btrfs_key *ins_key,
1098 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099{
Chris Masone20d96d2007-03-22 12:13:20 -04001100 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001101 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001102 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001103 int mid;
1104 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001105 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001106 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001107 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001108 int data_copy_size;
1109 int rt_data_off;
1110 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001111 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001112 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001113 int double_split = 0;
1114 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001115
Chris Mason40689472007-03-17 14:29:23 -04001116 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001117 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001118 if (wret < 0)
1119 return wret;
1120 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001121 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001122 if (wret < 0)
1123 return wret;
1124 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001125 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001126 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001127
1128 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001129 if (btrfs_leaf_free_space(root, l) >=
1130 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001131 return 0;
1132
Chris Mason5c680ed2007-02-22 11:39:13 -05001133 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001134 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001135 if (ret)
1136 return ret;
1137 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001138 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001139 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001140 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001141 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001142 BUG_ON(!right_buffer);
Chris Masone20d96d2007-03-22 12:13:20 -04001143 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001144 memset(&right->header, 0, sizeof(right->header));
Chris Masone20d96d2007-03-22 12:13:20 -04001145 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
Chris Mason7f5c1512007-03-23 15:56:19 -04001146 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason7518a232007-03-12 12:01:18 -04001147 btrfs_set_header_level(&right->header, 0);
1148 btrfs_set_header_parentid(&right->header,
Chris Masone20d96d2007-03-22 12:13:20 -04001149 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason3eb03142007-04-05 14:28:50 -04001150 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1151 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001152 if (mid <= slot) {
1153 if (nritems == 1 ||
1154 leaf_space_used(l, mid, nritems - mid) + space_needed >
1155 BTRFS_LEAF_DATA_SIZE(root)) {
1156 if (slot >= nritems) {
1157 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1158 btrfs_set_header_nritems(&right->header, 0);
1159 wret = insert_ptr(trans, root, path,
1160 &disk_key,
1161 right_buffer->b_blocknr,
1162 path->slots[1] + 1, 1);
1163 if (wret)
1164 ret = wret;
1165 btrfs_block_release(root, path->nodes[0]);
1166 path->nodes[0] = right_buffer;
1167 path->slots[0] = 0;
1168 path->slots[1] += 1;
1169 return ret;
1170 }
1171 mid = slot;
1172 double_split = 1;
1173 }
1174 } else {
1175 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1176 BTRFS_LEAF_DATA_SIZE(root)) {
1177 if (slot == 0) {
1178 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1179 btrfs_set_header_nritems(&right->header, 0);
1180 wret = insert_ptr(trans, root, path,
1181 &disk_key,
1182 right_buffer->b_blocknr,
1183 path->slots[1] - 1, 1);
1184 if (wret)
1185 ret = wret;
1186 btrfs_block_release(root, path->nodes[0]);
1187 path->nodes[0] = right_buffer;
1188 path->slots[0] = 0;
1189 path->slots[1] -= 1;
1190 return ret;
1191 }
1192 mid = slot;
1193 double_split = 1;
1194 }
1195 }
1196 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001197 data_copy_size = btrfs_item_end(l->items + mid) -
1198 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001199 btrfs_memcpy(root, right, right->items, l->items + mid,
1200 (nritems - mid) * sizeof(struct btrfs_item));
1201 btrfs_memcpy(root, right,
1202 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1203 data_copy_size, btrfs_leaf_data(l) +
1204 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001205 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1206 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001207
Chris Mason0783fcf2007-03-12 20:12:07 -04001208 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001209 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001210 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1211 }
Chris Mason74123bd2007-02-02 11:05:29 -05001212
Chris Mason7518a232007-03-12 12:01:18 -04001213 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001214 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001215 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -04001216 right_buffer->b_blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001217 if (wret)
1218 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001219 btrfs_mark_buffer_dirty(right_buffer);
1220 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001221 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001222 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001223 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001224 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001225 path->slots[0] -= mid;
1226 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001227 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001228 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001229 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001230
1231 if (!double_split)
1232 return ret;
1233 right_buffer = btrfs_alloc_free_block(trans, root);
1234 BUG_ON(!right_buffer);
1235 right = btrfs_buffer_leaf(right_buffer);
1236 memset(&right->header, 0, sizeof(right->header));
1237 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
1238 btrfs_set_header_generation(&right->header, trans->transid);
1239 btrfs_set_header_level(&right->header, 0);
1240 btrfs_set_header_parentid(&right->header,
1241 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason3eb03142007-04-05 14:28:50 -04001242 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1243 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001244 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1245 btrfs_set_header_nritems(&right->header, 0);
1246 wret = insert_ptr(trans, root, path,
1247 &disk_key,
1248 right_buffer->b_blocknr,
1249 path->slots[1], 1);
1250 if (wret)
1251 ret = wret;
1252 btrfs_block_release(root, path->nodes[0]);
1253 path->nodes[0] = right_buffer;
1254 path->slots[0] = 0;
1255 check_node(root, path, 1);
1256 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001257 return ret;
1258}
1259
Chris Mason74123bd2007-02-02 11:05:29 -05001260/*
1261 * Given a key and some data, insert an item into the tree.
1262 * This does all the path init required, making room in the tree if needed.
1263 */
Chris Masone089f052007-03-16 16:20:31 -04001264int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1265 *root, struct btrfs_path *path, struct btrfs_key
1266 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001267{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001268 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001269 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001270 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001271 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001272 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001273 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001274 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001275 struct btrfs_disk_key disk_key;
1276
1277 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001278
Chris Mason74123bd2007-02-02 11:05:29 -05001279 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001280 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001281 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001282 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001283 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001284 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001285 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001286 if (ret < 0)
1287 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288
Chris Mason62e27492007-03-15 12:56:47 -04001289 slot_orig = path->slots[0];
1290 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001291 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001292
Chris Mason7518a232007-03-12 12:01:18 -04001293 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001294 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001295
Chris Mason123abc82007-03-14 14:14:43 -04001296 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001297 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001298 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001299 }
Chris Mason62e27492007-03-15 12:56:47 -04001300 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001301 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001302 if (slot != nritems) {
1303 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001304 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001305
1306 /*
1307 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1308 */
1309 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001310 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001311 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001312 btrfs_set_item_offset(leaf->items + i,
1313 ioff - data_size);
1314 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001315
1316 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001317 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1318 leaf->items + slot,
1319 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001320
1321 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001322 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1323 data_end - data_size, btrfs_leaf_data(leaf) +
1324 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001325 data_end = old_data;
1326 }
Chris Mason62e27492007-03-15 12:56:47 -04001327 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001328 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1329 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001330 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1331 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001332 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001333 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001334
1335 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001336 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001337 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001338
Chris Mason123abc82007-03-14 14:14:43 -04001339 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001340 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001341 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001342out:
Chris Mason62e27492007-03-15 12:56:47 -04001343 return ret;
1344}
1345
1346/*
1347 * Given a key and some data, insert an item into the tree.
1348 * This does all the path init required, making room in the tree if needed.
1349 */
Chris Masone089f052007-03-16 16:20:31 -04001350int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1351 *root, struct btrfs_key *cpu_key, void *data, u32
1352 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001353{
1354 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001355 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001356 u8 *ptr;
1357
Chris Mason2c90e5d2007-04-02 10:50:19 -04001358 path = btrfs_alloc_path();
1359 BUG_ON(!path);
1360 btrfs_init_path(path);
1361 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001362 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001363 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1364 path->slots[0], u8);
1365 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001366 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001367 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001368 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001369 btrfs_release_path(root, path);
1370 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001371 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001372}
1373
Chris Mason74123bd2007-02-02 11:05:29 -05001374/*
Chris Mason5de08d72007-02-24 06:24:44 -05001375 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001376 *
1377 * If the delete empties a node, the node is removed from the tree,
1378 * continuing all the way the root if required. The root is converted into
1379 * a leaf if all the nodes are emptied.
1380 */
Chris Masone089f052007-03-16 16:20:31 -04001381static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1382 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001383{
Chris Mason234b63a2007-03-13 10:46:10 -04001384 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001385 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001386 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001387 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001388 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001389
Chris Masone20d96d2007-03-22 12:13:20 -04001390 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001391 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001392 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001393 btrfs_memmove(root, node, node->ptrs + slot,
1394 node->ptrs + slot + 1,
1395 sizeof(struct btrfs_key_ptr) *
1396 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001397 }
Chris Mason7518a232007-03-12 12:01:18 -04001398 nritems--;
1399 btrfs_set_header_nritems(&node->header, nritems);
1400 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001401 struct btrfs_header *header = btrfs_buffer_header(root->node);
1402 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001403 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001404 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001405 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001406 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001407 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001408 if (wret)
1409 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001410 }
Chris Masond6025572007-03-30 14:27:56 -04001411 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001412 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001413}
1414
Chris Mason74123bd2007-02-02 11:05:29 -05001415/*
1416 * delete the item at the leaf level in path. If that empties
1417 * the leaf, remove it from the tree
1418 */
Chris Masone089f052007-03-16 16:20:31 -04001419int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1420 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001421{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001422 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001423 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001424 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001425 int doff;
1426 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001427 int ret = 0;
1428 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001429 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001430
Chris Masoneb60cea2007-02-02 09:18:22 -05001431 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001432 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001433 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001434 doff = btrfs_item_offset(leaf->items + slot);
1435 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001436 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001437
Chris Mason7518a232007-03-12 12:01:18 -04001438 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001439 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001440 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04001441 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1442 data_end + dsize,
1443 btrfs_leaf_data(leaf) + data_end,
1444 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001445 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001446 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001447 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1448 }
Chris Masond6025572007-03-30 14:27:56 -04001449 btrfs_memmove(root, leaf, leaf->items + slot,
1450 leaf->items + slot + 1,
1451 sizeof(struct btrfs_item) *
1452 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001453 }
Chris Mason7518a232007-03-12 12:01:18 -04001454 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1455 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001456 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001457 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001458 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001459 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001460 } else {
Chris Masone089f052007-03-16 16:20:31 -04001461 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001462 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001463 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001464 if (wret)
1465 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001466 wret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -04001467 leaf_buf->b_blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001468 if (wret)
1469 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001470 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001471 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001472 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001473 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001474 wret = fixup_low_keys(trans, root, path,
1475 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001476 if (wret)
1477 ret = wret;
1478 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001479
Chris Mason74123bd2007-02-02 11:05:29 -05001480 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001481 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001482 /* push_leaf_left fixes the path.
1483 * make sure the path still points to our leaf
1484 * for possible call to del_ptr below
1485 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001486 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001487 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001488 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001489 if (wret < 0)
1490 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001491 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001492 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001493 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001494 if (wret < 0)
1495 ret = wret;
1496 }
Chris Mason7518a232007-03-12 12:01:18 -04001497 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -04001498 u64 blocknr = leaf_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001499 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001500 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001501 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001502 if (wret)
1503 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001504 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001505 wret = btrfs_free_extent(trans, root, blocknr,
1506 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001507 if (wret)
1508 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001509 } else {
Chris Masond6025572007-03-30 14:27:56 -04001510 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001511 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001512 }
Chris Masond5719762007-03-23 10:01:08 -04001513 } else {
Chris Masond6025572007-03-30 14:27:56 -04001514 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001515 }
1516 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001517 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001518}
1519
Chris Mason97571fd2007-02-24 13:39:08 -05001520/*
1521 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001522 * returns 0 if it found something or 1 if there are no greater leaves.
1523 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001524 */
Chris Mason234b63a2007-03-13 10:46:10 -04001525int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001526{
1527 int slot;
1528 int level = 1;
1529 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001530 struct buffer_head *c;
1531 struct btrfs_node *c_node;
1532 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001533
Chris Mason234b63a2007-03-13 10:46:10 -04001534 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001535 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001536 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001537 slot = path->slots[level] + 1;
1538 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001539 c_node = btrfs_buffer_node(c);
1540 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001541 level++;
1542 continue;
1543 }
Chris Masone20d96d2007-03-22 12:13:20 -04001544 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001545 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001546 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001547 next = read_tree_block(root, blocknr);
1548 break;
1549 }
1550 path->slots[level] = slot;
1551 while(1) {
1552 level--;
1553 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001554 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001555 path->nodes[level] = next;
1556 path->slots[level] = 0;
1557 if (!level)
1558 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001559 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001560 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001561 }
1562 return 0;
1563}