Commit 5f39d397 authored by Chris Mason's avatar Chris Mason

Btrfs: Create extent_buffer interface for large blocksizes

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 50b78c24
...@@ -23,4 +23,6 @@ modules_install: ...@@ -23,4 +23,6 @@ modules_install:
clean: clean:
$(MAKE) -C $(KERNELDIR) M=`pwd` clean $(MAKE) -C $(KERNELDIR) M=`pwd` clean
tester:
$(MAKE) -C $(KERNELDIR) M=`pwd` tree-defrag.o transaction.o sysfs.o super.o root-tree.o inode-map.o inode-item.o inode.o file-item.o file.o extent_map.o disk-io.o ctree.o dir-item.o extent-tree.o
endif endif
...@@ -16,21 +16,24 @@ ...@@ -16,21 +16,24 @@
* Boston, MA 021110-1307, USA. * Boston, MA 021110-1307, USA.
*/ */
#include <linux/highmem.h>
#include "ctree.h" #include "ctree.h"
#include "disk-io.h" #include "disk-io.h"
#include "transaction.h" #include "transaction.h"
#include "print-tree.h"
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level); *root, struct btrfs_path *path, int level);
static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *ins_key, *root, struct btrfs_key *ins_key,
struct btrfs_path *path, int data_size); struct btrfs_path *path, int data_size);
static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root static int push_node_left(struct btrfs_trans_handle *trans,
*root, struct buffer_head *dst, struct buffer_head struct btrfs_root *root, struct extent_buffer *dst,
*src); struct extent_buffer *src);
static int balance_node_right(struct btrfs_trans_handle *trans, struct static int balance_node_right(struct btrfs_trans_handle *trans,
btrfs_root *root, struct buffer_head *dst_buf, struct btrfs_root *root,
struct buffer_head *src_buf); struct extent_buffer *dst_buf,
struct extent_buffer *src_buf);
static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_path *path, int level, int slot); struct btrfs_path *path, int level, int slot);
...@@ -62,40 +65,38 @@ void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) ...@@ -62,40 +65,38 @@ void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
for (i = 0; i < BTRFS_MAX_LEVEL; i++) { for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
if (!p->nodes[i]) if (!p->nodes[i])
break; break;
btrfs_block_release(root, p->nodes[i]); free_extent_buffer(p->nodes[i]);
} }
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
} }
static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
*root, struct buffer_head *buf, struct buffer_head struct btrfs_root *root,
*parent, int parent_slot, struct buffer_head struct extent_buffer *buf,
**cow_ret, u64 search_start, u64 empty_size) struct extent_buffer *parent, int parent_slot,
struct extent_buffer **cow_ret,
u64 search_start, u64 empty_size)
{ {
struct buffer_head *cow; struct extent_buffer *cow;
struct btrfs_node *cow_node;
int ret = 0; int ret = 0;
int different_trans = 0; int different_trans = 0;
WARN_ON(root->ref_cows && trans->transid != root->last_trans); WARN_ON(root->ref_cows && trans->transid != root->last_trans);
WARN_ON(!buffer_uptodate(buf));
cow = btrfs_alloc_free_block(trans, root, search_start, empty_size); cow = btrfs_alloc_free_block(trans, root, search_start, empty_size);
if (IS_ERR(cow)) if (IS_ERR(cow))
return PTR_ERR(cow); return PTR_ERR(cow);
cow_node = btrfs_buffer_node(cow); if (buf->len != root->sectorsize || cow->len != root->sectorsize)
if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
WARN_ON(1); WARN_ON(1);
memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize); copy_extent_buffer(cow, buf, 0, 0, cow->len);
btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow)); btrfs_set_header_blocknr(cow, extent_buffer_blocknr(cow));
btrfs_set_header_generation(&cow_node->header, trans->transid); btrfs_set_header_generation(cow, trans->transid);
btrfs_set_header_owner(&cow_node->header, root->root_key.objectid); btrfs_set_header_owner(cow, root->root_key.objectid);
WARN_ON(btrfs_header_generation(btrfs_buffer_header(buf)) > WARN_ON(btrfs_header_generation(buf) > trans->transid);
trans->transid); if (btrfs_header_generation(buf) != trans->transid) {
if (btrfs_header_generation(btrfs_buffer_header(buf)) !=
trans->transid) {
different_trans = 1; different_trans = 1;
ret = btrfs_inc_ref(trans, root, buf); ret = btrfs_inc_ref(trans, root, buf);
if (ret) if (ret)
...@@ -106,29 +107,29 @@ static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -106,29 +107,29 @@ static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
if (buf == root->node) { if (buf == root->node) {
root->node = cow; root->node = cow;
get_bh(cow); extent_buffer_get(cow);
if (buf != root->commit_root) { if (buf != root->commit_root) {
btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); btrfs_free_extent(trans, root,
extent_buffer_blocknr(buf), 1, 1);
} }
btrfs_block_release(root, buf); free_extent_buffer(buf);
} else { } else {
btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot, btrfs_set_node_blockptr(parent, parent_slot,
bh_blocknr(cow)); extent_buffer_blocknr(cow));
btrfs_mark_buffer_dirty(parent); btrfs_mark_buffer_dirty(parent);
WARN_ON(btrfs_header_generation(btrfs_buffer_header(parent)) != WARN_ON(btrfs_header_generation(parent) != trans->transid);
trans->transid); btrfs_free_extent(trans, root, extent_buffer_blocknr(buf),1,1);
btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
} }
btrfs_block_release(root, buf); free_extent_buffer(buf);
btrfs_mark_buffer_dirty(cow); btrfs_mark_buffer_dirty(cow);
*cow_ret = cow; *cow_ret = cow;
return 0; return 0;
} }
int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_cow_block(struct btrfs_trans_handle *trans,
*root, struct buffer_head *buf, struct buffer_head struct btrfs_root *root, struct extent_buffer *buf,
*parent, int parent_slot, struct buffer_head struct extent_buffer *parent, int parent_slot,
**cow_ret) struct extent_buffer **cow_ret)
{ {
u64 search_start; u64 search_start;
if (trans->transaction != root->fs_info->running_transaction) { if (trans->transaction != root->fs_info->running_transaction) {
...@@ -141,13 +142,12 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -141,13 +142,12 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
root->fs_info->generation); root->fs_info->generation);
WARN_ON(1); WARN_ON(1);
} }
if (btrfs_header_generation(btrfs_buffer_header(buf)) == if (btrfs_header_generation(buf) == trans->transid) {
trans->transid) {
*cow_ret = buf; *cow_ret = buf;
return 0; return 0;
} }
search_start = bh_blocknr(buf) & ~((u64)65535); search_start = extent_buffer_blocknr(buf) & ~((u64)65535);
return __btrfs_cow_block(trans, root, buf, parent, return __btrfs_cow_block(trans, root, buf, parent,
parent_slot, cow_ret, search_start, 0); parent_slot, cow_ret, search_start, 0);
} }
...@@ -161,9 +161,11 @@ static int close_blocks(u64 blocknr, u64 other) ...@@ -161,9 +161,11 @@ static int close_blocks(u64 blocknr, u64 other)
return 0; return 0;
} }
static int should_defrag_leaf(struct buffer_head *bh) #if 0
static int should_defrag_leaf(struct extent_buffer *eb)
{ {
struct btrfs_leaf *leaf = btrfs_buffer_leaf(bh); return 0;
struct btrfs_leaf *leaf = btrfs_buffer_leaf(eb);
struct btrfs_disk_key *key; struct btrfs_disk_key *key;
u32 nritems; u32 nritems;
...@@ -188,14 +190,17 @@ static int should_defrag_leaf(struct buffer_head *bh) ...@@ -188,14 +190,17 @@ static int should_defrag_leaf(struct buffer_head *bh)
} }
return 0; return 0;
} }
#endif
int btrfs_realloc_node(struct btrfs_trans_handle *trans, int btrfs_realloc_node(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct buffer_head *parent, struct btrfs_root *root, struct extent_buffer *parent,
int cache_only, u64 *last_ret) int cache_only, u64 *last_ret)
{ {
return 0;
#if 0
struct btrfs_node *parent_node; struct btrfs_node *parent_node;
struct buffer_head *cur_bh; struct extent_buffer *cur_eb;
struct buffer_head *tmp_bh; struct extent_buffer *tmp_eb;
u64 blocknr; u64 blocknr;
u64 search_start = *last_ret; u64 search_start = *last_ret;
u64 last_block = 0; u64 last_block = 0;
...@@ -281,6 +286,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, ...@@ -281,6 +286,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
brelse(tmp_bh); brelse(tmp_bh);
} }
return err; return err;
#endif
} }
/* /*
...@@ -289,12 +295,12 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, ...@@ -289,12 +295,12 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
* which is the stop of the leaf data stack * which is the stop of the leaf data stack
*/ */
static inline unsigned int leaf_data_end(struct btrfs_root *root, static inline unsigned int leaf_data_end(struct btrfs_root *root,
struct btrfs_leaf *leaf) struct extent_buffer *leaf)
{ {
u32 nr = btrfs_header_nritems(&leaf->header); u32 nr = btrfs_header_nritems(leaf);
if (nr == 0) if (nr == 0)
return BTRFS_LEAF_DATA_SIZE(root); return BTRFS_LEAF_DATA_SIZE(root);
return btrfs_item_offset(leaf->items + nr - 1); return btrfs_item_offset_nr(leaf, nr - 1);
} }
/* /*
...@@ -310,9 +316,9 @@ static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) ...@@ -310,9 +316,9 @@ static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
return 1; return 1;
if (k1.objectid < k2->objectid) if (k1.objectid < k2->objectid)
return -1; return -1;
if (k1.flags > k2->flags) if (k1.type > k2->type)
return 1; return 1;
if (k1.flags < k2->flags) if (k1.type < k2->type)
return -1; return -1;
if (k1.offset > k2->offset) if (k1.offset > k2->offset)
return 1; return 1;
...@@ -324,37 +330,39 @@ static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) ...@@ -324,37 +330,39 @@ static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
static int check_node(struct btrfs_root *root, struct btrfs_path *path, static int check_node(struct btrfs_root *root, struct btrfs_path *path,
int level) int level)
{ {
struct btrfs_node *parent = NULL; struct extent_buffer *parent = NULL;
struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); struct extent_buffer *node = path->nodes[level];
struct btrfs_disk_key parent_key;
struct btrfs_disk_key node_key;
int parent_slot; int parent_slot;
int slot; int slot;
struct btrfs_key cpukey; struct btrfs_key cpukey;
u32 nritems = btrfs_header_nritems(&node->header); u32 nritems = btrfs_header_nritems(node);
if (path->nodes[level + 1]) if (path->nodes[level + 1])
parent = btrfs_buffer_node(path->nodes[level + 1]); parent = path->nodes[level + 1];
slot = path->slots[level]; slot = path->slots[level];
BUG_ON(!buffer_uptodate(path->nodes[level]));
BUG_ON(nritems == 0); BUG_ON(nritems == 0);
if (parent) { if (parent) {
struct btrfs_disk_key *parent_key;
parent_slot = path->slots[level + 1]; parent_slot = path->slots[level + 1];
parent_key = &parent->ptrs[parent_slot].key; btrfs_node_key(parent, &parent_key, parent_slot);
BUG_ON(memcmp(parent_key, &node->ptrs[0].key, btrfs_node_key(node, &node_key, 0);
BUG_ON(memcmp(&parent_key, &node_key,
sizeof(struct btrfs_disk_key))); sizeof(struct btrfs_disk_key)));
BUG_ON(btrfs_node_blockptr(parent, parent_slot) != BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
btrfs_header_blocknr(&node->header)); btrfs_header_blocknr(node));
} }
BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
if (slot != 0) { if (slot != 0) {
btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key); btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0); btrfs_node_key(node, &node_key, slot);
BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
} }
if (slot < nritems - 1) { if (slot < nritems - 1) {
btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key); btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0); btrfs_node_key(node, &node_key, slot);
BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
} }
return 0; return 0;
} }
...@@ -362,72 +370,134 @@ static int check_node(struct btrfs_root *root, struct btrfs_path *path, ...@@ -362,72 +370,134 @@ static int check_node(struct btrfs_root *root, struct btrfs_path *path,
static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
int level) int level)
{ {
struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]); struct extent_buffer *leaf = path->nodes[level];
struct btrfs_node *parent = NULL; struct extent_buffer *parent = NULL;
int parent_slot; int parent_slot;
int slot = path->slots[0];
struct btrfs_key cpukey; struct btrfs_key cpukey;
struct btrfs_disk_key parent_key;
struct btrfs_disk_key leaf_key;
int slot = path->slots[0];
u32 nritems = btrfs_header_nritems(&leaf->header); u32 nritems = btrfs_header_nritems(leaf);
if (path->nodes[level + 1]) if (path->nodes[level + 1])
parent = btrfs_buffer_node(path->nodes[level + 1]); parent = path->nodes[level + 1];
BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
if (nritems == 0) if (nritems == 0)
return 0; return 0;
if (parent) { if (parent) {
struct btrfs_disk_key *parent_key;
parent_slot = path->slots[level + 1]; parent_slot = path->slots[level + 1];
parent_key = &parent->ptrs[parent_slot].key; btrfs_node_key(parent, &parent_key, parent_slot);
btrfs_item_key(leaf, &leaf_key, 0);
BUG_ON(memcmp(parent_key, &leaf->items[0].key, BUG_ON(memcmp(&parent_key, &leaf_key,
sizeof(struct btrfs_disk_key))); sizeof(struct btrfs_disk_key)));
BUG_ON(btrfs_node_blockptr(parent, parent_slot) != BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
btrfs_header_blocknr(&leaf->header)); btrfs_header_blocknr(leaf));
}
#if 0
for (i = 0; nritems > 1 && i < nritems - 2; i++) {
btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
btrfs_item_key(leaf, &leaf_key, i);
if (comp_keys(&leaf_key, &cpukey) >= 0) {
btrfs_print_leaf(root, leaf);
printk("slot %d offset bad key\n", i);
BUG_ON(1);
}
if (btrfs_item_offset_nr(leaf, i) !=
btrfs_item_end_nr(leaf, i + 1)) {
btrfs_print_leaf(root, leaf);
printk("slot %d offset bad\n", i);
BUG_ON(1);
}
if (i == 0) {
if (btrfs_item_offset_nr(leaf, i) +
btrfs_item_size_nr(leaf, i) !=
BTRFS_LEAF_DATA_SIZE(root)) {
btrfs_print_leaf(root, leaf);
printk("slot %d first offset bad\n", i);
BUG_ON(1);
} }
if (slot != 0) {
btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
btrfs_item_end(leaf->items + slot));
} }
if (slot < nritems - 1) {
btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
BUG_ON(btrfs_item_offset(leaf->items + slot) !=
btrfs_item_end(leaf->items + slot + 1));
} }
BUG_ON(btrfs_item_offset(leaf->items) + if (nritems > 0) {
btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root)); if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
btrfs_print_leaf(root, leaf);
printk("slot %d bad size \n", nritems - 1);
BUG_ON(1);
}
}
#endif
if (slot != 0 && slot < nritems - 1) {
btrfs_item_key(leaf, &leaf_key, slot);
btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
if (comp_keys(&leaf_key, &cpukey) <= 0) {
btrfs_print_leaf(root, leaf);
printk("slot %d offset bad key\n", slot);
BUG_ON(1);
}
if (btrfs_item_offset_nr(leaf, slot - 1) !=
btrfs_item_end_nr(leaf, slot)) {
btrfs_print_leaf(root, leaf);
printk("slot %d offset bad\n", slot);
BUG_ON(1);
}
}
if (slot < nritems - 1) {
btrfs_item_key(leaf, &leaf_key, slot);
btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
if (btrfs_item_offset_nr(leaf, slot) !=
btrfs_item_end_nr(leaf, slot + 1)) {
btrfs_print_leaf(root, leaf);
printk("slot %d offset bad\n", slot);
BUG_ON(1);
}
}
BUG_ON(btrfs_item_offset_nr(leaf, 0) +
btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
return 0; return 0;
} }
static int check_block(struct btrfs_root *root, struct btrfs_path *path, static int check_block(struct btrfs_root *root, struct btrfs_path *path,
int level) int level)
{ {
struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); struct extent_buffer *buf = path->nodes[level];
if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid, char fsid[BTRFS_FSID_SIZE];
sizeof(node->header.fsid)))
BUG(); read_extent_buffer(buf, fsid, (unsigned long)btrfs_header_fsid(buf),
BTRFS_FSID_SIZE);
if (memcmp(fsid, root->fs_info->fsid, BTRFS_FSID_SIZE)) {
int i = 0;
printk("warning bad block %Lu\n", buf->start);
if (!btrfs_buffer_uptodate(buf)) {
WARN_ON(1);
}
for (i = 0; i < BTRFS_FSID_SIZE; i++) {
printk("%x:%x ", root->fs_info->fsid[i], fsid[i]);
}
printk("\n");
// BUG();
}
if (level == 0) if (level == 0)
return check_leaf(root, path, level); return check_leaf(root, path, level);
return check_node(root, path, level); return check_node(root, path, level);
} }
/* /*
* search for key in the array p. items p are item_size apart * search for key in the extent_buffer. The items start at offset p,
* and there are 'max' items in p * and they are item_size apart. There are 'max' items in p.
*
* the slot in the array is returned via slot, and it points to * the slot in the array is returned via slot, and it points to
* the place where you would insert key if it is not found in * the place where you would insert key if it is not found in
* the array. * the array.
* *
* slot may point to max if the key is bigger than all of the keys * slot may point to max if the key is bigger than all of the keys
*/ */
static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
int item_size, struct btrfs_key *key,
int max, int *slot) int max, int *slot)
{ {
int low = 0; int low = 0;
...@@ -435,10 +505,37 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, ...@@ -435,10 +505,37 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
int mid; int mid;
int ret; int ret;
struct btrfs_disk_key *tmp; struct btrfs_disk_key *tmp;
struct btrfs_disk_key unaligned;
unsigned long offset;
char *map_token = NULL;
char *kaddr = NULL;
unsigned long map_start = 0;
unsigned long map_len = 0;
while(low < high) { while(low < high) {
mid = (low + high) / 2; mid = (low + high) / 2;
tmp = (struct btrfs_disk_key *)(p + mid * item_size); offset = p + mid * item_size;
if (!map_token || offset < map_start ||
(offset + sizeof(struct btrfs_disk_key)) >
map_start + map_len) {
if (map_token)
unmap_extent_buffer(eb, map_token, KM_USER0);
map_extent_buffer(eb, offset, &map_token, &kaddr,
&map_start, &map_len, KM_USER0);
}
if (offset + sizeof(struct btrfs_disk_key) >
map_start + map_len) {
unmap_extent_buffer(eb, map_token, KM_USER0);
read_extent_buffer(eb, &unaligned,
offset, sizeof(unaligned));
map_token = NULL;
tmp = &unaligned;
} else {
tmp = (struct btrfs_disk_key *)(kaddr + offset -
map_start);
}
ret = comp_keys(tmp, key); ret = comp_keys(tmp, key);
if (ret < 0) if (ret < 0)
...@@ -447,10 +544,13 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, ...@@ -447,10 +544,13 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
high = mid; high = mid;
else { else {
*slot = mid; *slot = mid;
unmap_extent_buffer(eb, map_token, KM_USER0);
return 0; return 0;
} }
} }
*slot = low; *slot = low;
if (map_token)
unmap_extent_buffer(eb, map_token, KM_USER0);
return 1; return 1;
} }
...@@ -458,46 +558,42 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, ...@@ -458,46 +558,42 @@ static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
* simple bin_search frontend that does the right thing for * simple bin_search frontend that does the right thing for
* leaves vs nodes * leaves vs nodes
*/ */
static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot) static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
int level, int *slot)
{ {
if (btrfs_is_leaf(c)) { if (level == 0) {
struct btrfs_leaf *l = (struct btrfs_leaf *)c; return generic_bin_search(eb,
return generic_bin_search((void *)l->items, offsetof(struct btrfs_leaf, items),
sizeof(struct btrfs_item), sizeof(struct btrfs_item),
key, btrfs_header_nritems(&c->header), key, btrfs_header_nritems(eb),
slot); slot);
} else { } else {
return generic_bin_search((void *)c->ptrs, return generic_bin_search(eb,
offsetof(struct btrfs_node, ptrs),
sizeof(struct btrfs_key_ptr), sizeof(struct btrfs_key_ptr),
key, btrfs_header_nritems(&c->header), key, btrfs_header_nritems(eb),
slot); slot);
} }
return -1; return -1;
} }
static struct buffer_head *read_node_slot(struct btrfs_root *root, static struct extent_buffer *read_node_slot(struct btrfs_root *root,
struct buffer_head *parent_buf, struct extent_buffer *parent, int slot)
int slot)
{ {
struct btrfs_node *node = btrfs_buffer_node(parent_buf);
if (slot < 0) if (slot < 0)
return NULL; return NULL;
if (slot >= btrfs_header_nritems(&node->header)) if (slot >= btrfs_header_nritems(parent))
return NULL; return NULL;
return read_tree_block(root, btrfs_node_blockptr(node, slot)); return read_tree_block(root, btrfs_node_blockptr(parent, slot));
} }
static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level) *root, struct btrfs_path *path, int level)
{ {
struct buffer_head *right_buf; struct extent_buffer *right = NULL;
struct buffer_head *mid_buf; struct extent_buffer *mid;
struct buffer_head *left_buf; struct extent_buffer *left = NULL;
struct buffer_head *parent_buf = NULL; struct extent_buffer *parent = NULL;
struct btrfs_node *right = NULL;
struct btrfs_node *mid;
struct btrfs_node *left = NULL;
struct btrfs_node *parent = NULL;
int ret = 0; int ret = 0;
int wret; int wret;
int pslot; int pslot;
...@@ -508,60 +604,57 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -508,60 +604,57 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
if (level == 0) if (level == 0)
return 0; return 0;
mid_buf = path->nodes[level]; mid = path->nodes[level];
mid = btrfs_buffer_node(mid_buf);
orig_ptr = btrfs_node_blockptr(mid, orig_slot); orig_ptr = btrfs_node_blockptr(mid, orig_slot);
if (level < BTRFS_MAX_LEVEL - 1) if (level < BTRFS_MAX_LEVEL - 1)
parent_buf = path->nodes[level + 1]; parent = path->nodes[level + 1];
pslot = path->slots[level + 1]; pslot = path->slots[level + 1];
/* /*
* deal with the case where there is only one pointer in the root * deal with the case where there is only one pointer in the root
* by promoting the node below to a root * by promoting the node below to a root
*/ */
if (!parent_buf) { if (!parent) {
struct buffer_head *child; struct extent_buffer *child;
u64 blocknr = bh_blocknr(mid_buf); u64 blocknr = extent_buffer_blocknr(mid);
if (btrfs_header_nritems(&mid->header) != 1) if (btrfs_header_nritems(mid) != 1)
return 0; return 0;
/* promote the child to a root */ /* promote the child to a root */
child = read_node_slot(root, mid_buf, 0); child = read_node_slot(root, mid, 0);
BUG_ON(!child); BUG_ON(!child);
root->node = child; root->node = child;
path->nodes[level] = NULL; path->nodes[level] = NULL;
clean_tree_block(trans, root, mid_buf); clean_tree_block(trans, root, mid);
wait_on_buffer(mid_buf); wait_on_tree_block_writeback(root, mid);
/* once for the path */ /* once for the path */
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
/* once for the root ptr */ /* once for the root ptr */
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
return btrfs_free_extent(trans, root, blocknr, 1, 1); return btrfs_free_extent(trans, root, blocknr, 1, 1);
} }
parent = btrfs_buffer_node(parent_buf); if (btrfs_header_nritems(mid) >
if (btrfs_header_nritems(&mid->header) >
BTRFS_NODEPTRS_PER_BLOCK(root) / 4) BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
return 0; return 0;
if (btrfs_header_nritems(&mid->header) < 2) if (btrfs_header_nritems(mid) < 2)
err_on_enospc = 1; err_on_enospc = 1;
left_buf = read_node_slot(root, parent_buf, pslot - 1); left = read_node_slot(root, parent, pslot - 1);
if (left_buf) { if (left) {
wret = btrfs_cow_block(trans, root, left_buf, wret = btrfs_cow_block(trans, root, left,
parent_buf, pslot - 1, &left_buf); parent, pslot - 1, &left);
if (wret) { if (wret) {
ret = wret; ret = wret;
goto enospc; goto enospc;
} }
} }
right_buf = read_node_slot(root, parent_buf, pslot + 1); right = read_node_slot(root, parent, pslot + 1);
if (right_buf) { if (right) {
wret = btrfs_cow_block(trans, root, right_buf, wret = btrfs_cow_block(trans, root, right,
parent_buf, pslot + 1, &right_buf); parent, pslot + 1, &right);
if (wret) { if (wret) {
ret = wret; ret = wret;
goto enospc; goto enospc;
...@@ -569,30 +662,27 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -569,30 +662,27 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
} }
/* first, try to make some room in the middle buffer */ /* first, try to make some room in the middle buffer */
if (left_buf) { if (left) {
left = btrfs_buffer_node(left_buf); orig_slot += btrfs_header_nritems(left);
orig_slot += btrfs_header_nritems(&left->header); wret = push_node_left(trans, root, left, mid);
wret = push_node_left(trans, root, left_buf, mid_buf);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
if (btrfs_header_nritems(&mid->header) < 2) if (btrfs_header_nritems(mid) < 2)
err_on_enospc = 1; err_on_enospc = 1;
} }
/* /*
* then try to empty the right most buffer into the middle * then try to empty the right most buffer into the middle
*/ */
if (right_buf) { if (right) {
right = btrfs_buffer_node(right_buf); wret = push_node_left(trans, root, mid, right);
wret = push_node_left(trans, root, mid_buf, right_buf);
if (wret < 0 && wret != -ENOSPC) if (wret < 0 && wret != -ENOSPC)
ret = wret; ret = wret;
if (btrfs_header_nritems(&right->header) == 0) { if (btrfs_header_nritems(right) == 0) {
u64 blocknr = bh_blocknr(right_buf); u64 blocknr = extent_buffer_blocknr(right);
clean_tree_block(trans, root, right_buf); clean_tree_block(trans, root, right);
wait_on_buffer(right_buf); wait_on_tree_block_writeback(root, right);
btrfs_block_release(root, right_buf); free_extent_buffer(right);
right_buf = NULL;
right = NULL; right = NULL;
wret = del_ptr(trans, root, path, level + 1, pslot + wret = del_ptr(trans, root, path, level + 1, pslot +
1); 1);
...@@ -602,14 +692,13 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -602,14 +692,13 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
if (wret) if (wret)
ret = wret; ret = wret;
} else { } else {
btrfs_memcpy(root, parent, struct btrfs_disk_key right_key;
&parent->ptrs[pslot + 1].key, btrfs_node_key(right, &right_key, 0);
&right->ptrs[0].key, btrfs_set_node_key(parent, &right_key, pslot + 1);
sizeof(struct btrfs_disk_key)); btrfs_mark_buffer_dirty(parent);
btrfs_mark_buffer_dirty(parent_buf);
} }
} }
if (btrfs_header_nritems(&mid->header) == 1) { if (btrfs_header_nritems(mid) == 1) {
/* /*
* we're not allowed to leave a node with one item in the * we're not allowed to leave a node with one item in the
* tree during a delete. A deletion from lower in the tree * tree during a delete. A deletion from lower in the tree
...@@ -619,21 +708,20 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -619,21 +708,20 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
* otherwise we would have pulled some pointers from the * otherwise we would have pulled some pointers from the
* right * right
*/ */
BUG_ON(!left_buf); BUG_ON(!left);
wret = balance_node_right(trans, root, mid_buf, left_buf); wret = balance_node_right(trans, root, mid, left);
if (wret < 0) { if (wret < 0) {
ret = wret; ret = wret;
goto enospc; goto enospc;
} }
BUG_ON(wret == 1); BUG_ON(wret == 1);
} }
if (btrfs_header_nritems(&mid->header) == 0) { if (btrfs_header_nritems(mid) == 0) {
/* we've managed to empty the middle node, drop it */ /* we've managed to empty the middle node, drop it */
u64 blocknr = bh_blocknr(mid_buf); u64 blocknr = extent_buffer_blocknr(mid);
clean_tree_block(trans, root, mid_buf); clean_tree_block(trans, root, mid);
wait_on_buffer(mid_buf); wait_on_tree_block_writeback(root, mid);
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
mid_buf = NULL;
mid = NULL; mid = NULL;
wret = del_ptr(trans, root, path, level + 1, pslot); wret = del_ptr(trans, root, path, level + 1, pslot);
if (wret) if (wret)
...@@ -643,37 +731,36 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -643,37 +731,36 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
ret = wret; ret = wret;
} else { } else {
/* update the parent key to reflect our changes */ /* update the parent key to reflect our changes */
btrfs_memcpy(root, parent, struct btrfs_disk_key mid_key;
&parent->ptrs[pslot].key, &mid->ptrs[0].key, btrfs_node_key(mid, &mid_key, 0);
sizeof(struct btrfs_disk_key)); btrfs_set_node_key(parent, &mid_key, pslot);
btrfs_mark_buffer_dirty(parent_buf); btrfs_mark_buffer_dirty(parent);
} }
/* update the path */ /* update the path */
if (left_buf) { if (left) {
if (btrfs_header_nritems(&left->header) > orig_slot) { if (btrfs_header_nritems(left) > orig_slot) {
get_bh(left_buf); extent_buffer_get(left);
path->nodes[level] = left_buf; path->nodes[level] = left;
path->slots[level + 1] -= 1; path->slots[level + 1] -= 1;
path->slots[level] = orig_slot; path->slots[level] = orig_slot;
if (mid_buf) if (mid)
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
} else { } else {
orig_slot -= btrfs_header_nritems(&left->header); orig_slot -= btrfs_header_nritems(left);
path->slots[level] = orig_slot; path->slots[level] = orig_slot;
} }
} }
/* double check we haven't messed things up */ /* double check we haven't messed things up */
check_block(root, path, level); check_block(root, path, level);
if (orig_ptr != if (orig_ptr !=
btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]), btrfs_node_blockptr(path->nodes[level], path->slots[level]))
path->slots[level]))
BUG(); BUG();
enospc: enospc:
if (right_buf) if (right)
btrfs_block_release(root, right_buf); free_extent_buffer(right);
if (left_buf) if (left)
btrfs_block_release(root, left_buf); free_extent_buffer(left);
return ret; return ret;
} }
...@@ -682,14 +769,10 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans, ...@@ -682,14 +769,10 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
struct btrfs_path *path, int level) struct btrfs_path *path, int level)
{ {
struct buffer_head *right_buf; struct extent_buffer *right = NULL;
struct buffer_head *mid_buf; struct extent_buffer *mid;
struct buffer_head *left_buf; struct extent_buffer *left = NULL;
struct buffer_head *parent_buf = NULL; struct extent_buffer *parent = NULL;
struct btrfs_node *right = NULL;
struct btrfs_node *mid;
struct btrfs_node *left = NULL;
struct btrfs_node *parent = NULL;
int ret = 0; int ret = 0;
int wret; int wret;
int pslot; int pslot;
...@@ -699,107 +782,101 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans, ...@@ -699,107 +782,101 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
if (level == 0) if (level == 0)
return 1; return 1;
mid_buf = path->nodes[level]; mid = path->nodes[level];
mid = btrfs_buffer_node(mid_buf);
orig_ptr = btrfs_node_blockptr(mid, orig_slot); orig_ptr = btrfs_node_blockptr(mid, orig_slot);
if (level < BTRFS_MAX_LEVEL - 1) if (level < BTRFS_MAX_LEVEL - 1)
parent_buf = path->nodes[level + 1]; parent = path->nodes[level + 1];
pslot = path->slots[level + 1]; pslot = path->slots[level + 1];
if (!parent_buf) if (!parent)
return 1; return 1;
parent = btrfs_buffer_node(parent_buf);
left_buf = read_node_slot(root, parent_buf, pslot - 1); left = read_node_slot(root, parent, pslot - 1);
/* first, try to make some room in the middle buffer */ /* first, try to make some room in the middle buffer */
if (left_buf) { if (left) {
u32 left_nr; u32 left_nr;
left = btrfs_buffer_node(left_buf); left_nr = btrfs_header_nritems(left);
left_nr = btrfs_header_nritems(&left->header);
if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
wret = 1; wret = 1;
} else { } else {
ret = btrfs_cow_block(trans, root, left_buf, parent_buf, ret = btrfs_cow_block(trans, root, left, parent,
pslot - 1, &left_buf); pslot - 1, &left);
if (ret) if (ret)
wret = 1; wret = 1;
else { else {
left = btrfs_buffer_node(left_buf);
wret = push_node_left(trans, root, wret = push_node_left(trans, root,
left_buf, mid_buf); left, mid);
} }
} }
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
if (wret == 0) { if (wret == 0) {
struct btrfs_disk_key disk_key;
orig_slot += left_nr; orig_slot += left_nr;
btrfs_memcpy(root, parent, btrfs_node_key(mid, &disk_key, 0);
&parent->ptrs[pslot].key, btrfs_set_node_key(parent, &disk_key, pslot);
&mid->ptrs[0].key, btrfs_mark_buffer_dirty(parent);
sizeof(struct btrfs_disk_key)); if (btrfs_header_nritems(left) > orig_slot) {
btrfs_mark_buffer_dirty(parent_buf); path->nodes[level] = left;
if (btrfs_header_nritems(&left->header) > orig_slot) {
path->nodes[level] = left_buf;
path->slots[level + 1] -= 1; path->slots[level + 1] -= 1;
path->slots[level] = orig_slot; path->slots[level] = orig_slot;
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
} else { } else {
orig_slot -= orig_slot -=
btrfs_header_nritems(&left->header); btrfs_header_nritems(left);
path->slots[level] = orig_slot; path->slots[level] = orig_slot;
btrfs_block_release(root, left_buf); free_extent_buffer(left);
} }
check_node(root, path, level); check_node(root, path, level);
return 0; return 0;
} }
btrfs_block_release(root, left_buf); free_extent_buffer(left);
} }
right_buf = read_node_slot(root, parent_buf, pslot + 1); right= read_node_slot(root, parent, pslot + 1);
/* /*
* then try to empty the right most buffer into the middle * then try to empty the right most buffer into the middle
*/ */
if (right_buf) { if (right) {
u32 right_nr; u32 right_nr;
right = btrfs_buffer_node(right_buf); right_nr = btrfs_header_nritems(right);
right_nr = btrfs_header_nritems(&right->header);
if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
wret = 1; wret = 1;
} else { } else {
ret = btrfs_cow_block(trans, root, right_buf, ret = btrfs_cow_block(trans, root, right,
parent_buf, pslot + 1, parent, pslot + 1,
&right_buf); &right);
if (ret) if (ret)
wret = 1; wret = 1;
else { else {
right = btrfs_buffer_node(right_buf);
wret = balance_node_right(trans, root, wret = balance_node_right(trans, root,
right_buf, mid_buf); right, mid);
} }
} }
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
if (wret == 0) { if (wret == 0) {
btrfs_memcpy(root, parent, struct btrfs_disk_key disk_key;
&parent->ptrs[pslot + 1].key,
&right->ptrs[0].key, btrfs_node_key(right, &disk_key, 0);
sizeof(struct btrfs_disk_key)); btrfs_set_node_key(parent, &disk_key, pslot + 1);
btrfs_mark_buffer_dirty(parent_buf); btrfs_mark_buffer_dirty(parent);
if (btrfs_header_nritems(&mid->header) <= orig_slot) {
path->nodes[level] = right_buf; if (btrfs_header_nritems(mid) <= orig_slot) {
path->nodes[level] = right;
path->slots[level + 1] += 1; path->slots[level + 1] += 1;
path->slots[level] = orig_slot - path->slots[level] = orig_slot -
btrfs_header_nritems(&mid->header); btrfs_header_nritems(mid);
btrfs_block_release(root, mid_buf); free_extent_buffer(mid);
} else { } else {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
} }
check_node(root, path, level); check_node(root, path, level);
return 0; return 0;
} }
btrfs_block_release(root, right_buf); free_extent_buffer(right);
} }
check_node(root, path, level); check_node(root, path, level);
return 1; return 1;
...@@ -811,10 +888,9 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans, ...@@ -811,10 +888,9 @@ static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
int level, int slot) int level, int slot)
{ {
struct btrfs_node *node; struct extent_buffer *node;
int i; int i;
u32 nritems; u32 nritems;
u64 item_objectid;
u64 blocknr; u64 blocknr;
u64 search; u64 search;
u64 cluster_start; u64 cluster_start;
...@@ -823,7 +899,7 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, ...@@ -823,7 +899,7 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
int direction = path->reada; int direction = path->reada;
struct radix_tree_root found; struct radix_tree_root found;
unsigned long gang[8]; unsigned long gang[8];
struct buffer_head *bh; struct extent_buffer *eb;
if (level == 0) if (level == 0)
return; return;
...@@ -831,18 +907,17 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, ...@@ -831,18 +907,17 @@ static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
if (!path->nodes[level]) if (!path->nodes[level])
return; return;
node = btrfs_buffer_node(path->nodes[level]); node = path->nodes[level];
search = btrfs_node_blockptr(node, slot); search = btrfs_node_blockptr(node, slot);
bh = btrfs_find_tree_block(root, search); eb = btrfs_find_tree_block(root, search);
if (bh) { if (eb) {
brelse(bh); free_extent_buffer(eb);
return; return;
} }
init_bit_radix(&found); init_bit_radix(&found);
nritems = btrfs_header_nritems(&node->header); nritems = btrfs_header_nritems(node);
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
blocknr = btrfs_node_blockptr(node, i); blocknr = btrfs_node_blockptr(node, i);
set_radix_bit(&found, blocknr); set_radix_bit(&found, blocknr);
} }
...@@ -886,8 +961,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -886,8 +961,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_path *p, int *root, struct btrfs_key *key, struct btrfs_path *p, int
ins_len, int cow) ins_len, int cow)
{ {
struct buffer_head *b; struct extent_buffer *b;
struct btrfs_node *c;
u64 blocknr; u64 blocknr;
int slot; int slot;
int ret; int ret;
...@@ -901,10 +975,9 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -901,10 +975,9 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
again: again:
b = root->node; b = root->node;
get_bh(b); extent_buffer_get(b);
while (b) { while (b) {
c = btrfs_buffer_node(b); level = btrfs_header_level(b);
level = btrfs_header_level(&c->header);
if (cow) { if (cow) {
int wret; int wret;
wret = btrfs_cow_block(trans, root, b, wret = btrfs_cow_block(trans, root, b,
...@@ -912,32 +985,30 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -912,32 +985,30 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
p->slots[level + 1], p->slots[level + 1],
&b); &b);
if (wret) { if (wret) {
btrfs_block_release(root, b); free_extent_buffer(b);
return wret; return wret;
} }
c = btrfs_buffer_node(b);
} }
BUG_ON(!cow && ins_len); BUG_ON(!cow && ins_len);
if (level != btrfs_header_level(&c->header)) if (level != btrfs_header_level(b))
WARN_ON(1); WARN_ON(1);
level = btrfs_header_level(&c->header); level = btrfs_header_level(b);
p->nodes[level] = b; p->nodes[level] = b;
ret = check_block(root, p, level); ret = check_block(root, p, level);
if (ret) if (ret)
return -1; return -1;
ret = bin_search(c, key, &slot); ret = bin_search(b, key, level, &slot);
if (!btrfs_is_leaf(c)) { if (level != 0) {
if (ret && slot > 0) if (ret && slot > 0)
slot -= 1; slot -= 1;
p->slots[level] = slot; p->slots[level] = slot;
if (ins_len > 0 && btrfs_header_nritems(&c->header) >= if (ins_len > 0 && btrfs_header_nritems(b) >=
BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
int sret = split_node(trans, root, p, level); int sret = split_node(trans, root, p, level);
BUG_ON(sret > 0); BUG_ON(sret > 0);
if (sret) if (sret)
return sret; return sret;
b = p->nodes[level]; b = p->nodes[level];
c = btrfs_buffer_node(b);
slot = p->slots[level]; slot = p->slots[level];
} else if (ins_len < 0) { } else if (ins_len < 0) {
int sret = balance_level(trans, root, p, int sret = balance_level(trans, root, p,
...@@ -947,22 +1018,19 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -947,22 +1018,19 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
b = p->nodes[level]; b = p->nodes[level];
if (!b) if (!b)
goto again; goto again;
c = btrfs_buffer_node(b);
slot = p->slots[level]; slot = p->slots[level];
BUG_ON(btrfs_header_nritems(&c->header) == 1); BUG_ON(btrfs_header_nritems(b) == 1);
} }
/* this is only true while dropping a snapshot */ /* this is only true while dropping a snapshot */
if (level == lowest_level) if (level == lowest_level)
break; break;
blocknr = btrfs_node_blockptr(c, slot); blocknr = btrfs_node_blockptr(b, slot);
if (should_reada) if (should_reada)
reada_for_search(root, p, level, slot); reada_for_search(root, p, level, slot);
b = read_tree_block(root, btrfs_node_blockptr(c, slot)); b = read_tree_block(root, btrfs_node_blockptr(b, slot));
} else { } else {
struct btrfs_leaf *l = (struct btrfs_leaf *)c;
p->slots[level] = slot; p->slots[level] = slot;
if (ins_len > 0 && btrfs_leaf_free_space(root, l) < if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
sizeof(struct btrfs_item) + ins_len) { sizeof(struct btrfs_item) + ins_len) {
int sret = split_leaf(trans, root, key, int sret = split_leaf(trans, root, key,
p, ins_len); p, ins_len);
...@@ -986,19 +1054,20 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -986,19 +1054,20 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
* If this fails to write a tree block, it returns -1, but continues * If this fails to write a tree block, it returns -1, but continues
* fixing up the blocks in ram so the tree is consistent. * fixing up the blocks in ram so the tree is consistent.
*/ */
static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root static int fixup_low_keys(struct btrfs_trans_handle *trans,
*root, struct btrfs_path *path, struct btrfs_disk_key struct btrfs_root *root, struct btrfs_path *path,
*key, int level) struct btrfs_disk_key *key, int level)
{ {
int i; int i;
int ret = 0; int ret = 0;
struct extent_buffer *t;
for (i = level; i < BTRFS_MAX_LEVEL; i++) { for (i = level; i < BTRFS_MAX_LEVEL; i++) {
struct btrfs_node *t;
int tslot = path->slots[i]; int tslot = path->slots[i];
if (!path->nodes[i]) if (!path->nodes[i])
break; break;
t = btrfs_buffer_node(path->nodes[i]); t = path->nodes[i];
btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key)); btrfs_set_node_key(t, key, tslot);
btrfs_mark_buffer_dirty(path->nodes[i]); btrfs_mark_buffer_dirty(path->nodes[i]);
if (tslot != 0) if (tslot != 0)
break; break;
...@@ -1014,18 +1083,16 @@ static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1014,18 +1083,16 @@ static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
* error, and > 0 if there was no room in the left hand block. * error, and > 0 if there was no room in the left hand block.
*/ */
static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct buffer_head *dst_buf, struct *root, struct extent_buffer *dst,
buffer_head *src_buf) struct extent_buffer *src)
{ {
struct btrfs_node *src = btrfs_buffer_node(src_buf);
struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
int push_items = 0; int push_items = 0;
int src_nritems; int src_nritems;
int dst_nritems; int dst_nritems;
int ret = 0; int ret = 0;
src_nritems = btrfs_header_nritems(&src->header); src_nritems = btrfs_header_nritems(src);
dst_nritems = btrfs_header_nritems(&dst->header); dst_nritems = btrfs_header_nritems(dst);
push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
if (push_items <= 0) { if (push_items <= 0) {
...@@ -1035,17 +1102,21 @@ static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1035,17 +1102,21 @@ static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
if (src_nritems < push_items) if (src_nritems < push_items)
push_items = src_nritems; push_items = src_nritems;
btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs, copy_extent_buffer(dst, src,
btrfs_node_key_ptr_offset(dst_nritems),
btrfs_node_key_ptr_offset(0),
push_items * sizeof(struct btrfs_key_ptr)); push_items * sizeof(struct btrfs_key_ptr));
if (push_items < src_nritems) { if (push_items < src_nritems) {
btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items, memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
btrfs_node_key_ptr_offset(push_items),
(src_nritems - push_items) * (src_nritems - push_items) *
sizeof(struct btrfs_key_ptr)); sizeof(struct btrfs_key_ptr));
} }
btrfs_set_header_nritems(&src->header, src_nritems - push_items); btrfs_set_header_nritems(src, src_nritems - push_items);
btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); btrfs_set_header_nritems(dst, dst_nritems + push_items);
btrfs_mark_buffer_dirty(src_buf); btrfs_mark_buffer_dirty(src);
btrfs_mark_buffer_dirty(dst_buf); btrfs_mark_buffer_dirty(dst);
return ret; return ret;
} }
...@@ -1058,24 +1129,22 @@ static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1058,24 +1129,22 @@ static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
* *
* this will only push up to 1/2 the contents of the left node over * this will only push up to 1/2 the contents of the left node over
*/ */
static int balance_node_right(struct btrfs_trans_handle *trans, struct static int balance_node_right(struct btrfs_trans_handle *trans,
btrfs_root *root, struct buffer_head *dst_buf, struct btrfs_root *root,
struct buffer_head *src_buf) struct extent_buffer *dst,
struct extent_buffer *src)
{ {
struct btrfs_node *src = btrfs_buffer_node(src_buf);
struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
int push_items = 0; int push_items = 0;
int max_push; int max_push;
int src_nritems; int src_nritems;
int dst_nritems; int dst_nritems;
int ret = 0; int ret = 0;
src_nritems = btrfs_header_nritems(&src->header); src_nritems = btrfs_header_nritems(src);
dst_nritems = btrfs_header_nritems(&dst->header); dst_nritems = btrfs_header_nritems(dst);
push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
if (push_items <= 0) { if (push_items <= 0)
return 1; return 1;
}
max_push = src_nritems / 2 + 1; max_push = src_nritems / 2 + 1;
/* don't try to empty the node */ /* don't try to empty the node */
...@@ -1085,18 +1154,21 @@ static int balance_node_right(struct btrfs_trans_handle *trans, struct ...@@ -1085,18 +1154,21 @@ static int balance_node_right(struct btrfs_trans_handle *trans, struct
if (max_push < push_items) if (max_push < push_items)
push_items = max_push; push_items = max_push;
btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs, memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
dst_nritems * sizeof(struct btrfs_key_ptr)); btrfs_node_key_ptr_offset(0),
(dst_nritems) *
sizeof(struct btrfs_key_ptr));
btrfs_memcpy(root, dst, dst->ptrs, copy_extent_buffer(dst, src,
src->ptrs + src_nritems - push_items, btrfs_node_key_ptr_offset(0),
btrfs_node_key_ptr_offset(src_nritems - push_items),
push_items * sizeof(struct btrfs_key_ptr)); push_items * sizeof(struct btrfs_key_ptr));
btrfs_set_header_nritems(&src->header, src_nritems - push_items); btrfs_set_header_nritems(src, src_nritems - push_items);
btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); btrfs_set_header_nritems(dst, dst_nritems + push_items);
btrfs_mark_buffer_dirty(src_buf); btrfs_mark_buffer_dirty(src);
btrfs_mark_buffer_dirty(dst_buf); btrfs_mark_buffer_dirty(dst);
return ret; return ret;
} }
...@@ -1107,45 +1179,46 @@ static int balance_node_right(struct btrfs_trans_handle *trans, struct ...@@ -1107,45 +1179,46 @@ static int balance_node_right(struct btrfs_trans_handle *trans, struct
* *
* returns zero on success or < 0 on failure. * returns zero on success or < 0 on failure.
*/ */
static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root static int insert_new_root(struct btrfs_trans_handle *trans,
*root, struct btrfs_path *path, int level) struct btrfs_root *root,
struct btrfs_path *path, int level)
{ {
struct buffer_head *t; struct extent_buffer *lower;
struct btrfs_node *lower; struct extent_buffer *c;
struct btrfs_node *c; struct btrfs_disk_key lower_key;
struct btrfs_disk_key *lower_key;
BUG_ON(path->nodes[level]); BUG_ON(path->nodes[level]);
BUG_ON(path->nodes[level-1] != root->node); BUG_ON(path->nodes[level-1] != root->node);
t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr, 0); c = btrfs_alloc_free_block(trans, root,
if (IS_ERR(t)) extent_buffer_blocknr(root->node), 0);
return PTR_ERR(t); if (IS_ERR(c))
c = btrfs_buffer_node(t); return PTR_ERR(c);
memset(c, 0, root->blocksize); memset_extent_buffer(c, 0, 0, root->nodesize);
btrfs_set_header_nritems(&c->header, 1); btrfs_set_header_nritems(c, 1);
btrfs_set_header_level(&c->header, level); btrfs_set_header_level(c, level);
btrfs_set_header_blocknr(&c->header, bh_blocknr(t)); btrfs_set_header_blocknr(c, extent_buffer_blocknr(c));
btrfs_set_header_generation(&c->header, trans->transid); btrfs_set_header_generation(c, trans->transid);
btrfs_set_header_owner(&c->header, root->root_key.objectid); btrfs_set_header_owner(c, root->root_key.objectid);
lower = btrfs_buffer_node(path->nodes[level-1]); lower = path->nodes[level-1];
memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
sizeof(c->header.fsid)); write_extent_buffer(c, root->fs_info->fsid,
if (btrfs_is_leaf(lower)) (unsigned long)btrfs_header_fsid(c),
lower_key = &((struct btrfs_leaf *)lower)->items[0].key; BTRFS_FSID_SIZE);
if (level == 1)
btrfs_item_key(lower, &lower_key, 0);
else else
lower_key = &lower->ptrs[0].key; btrfs_node_key(lower, &lower_key, 0);
btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key, btrfs_set_node_key(c, &lower_key, 0);
sizeof(struct btrfs_disk_key)); btrfs_set_node_blockptr(c, 0, extent_buffer_blocknr(lower));
btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
btrfs_mark_buffer_dirty(t); btrfs_mark_buffer_dirty(c);
/* the super has an extra ref to root->node */ /* the super has an extra ref to root->node */
btrfs_block_release(root, root->node); free_extent_buffer(root->node);
root->node = t; root->node = c;
get_bh(t); extent_buffer_get(c);
path->nodes[level] = t; path->nodes[level] = c;
path->slots[level] = 0; path->slots[level] = 0;
return 0; return 0;
} }
...@@ -1163,26 +1236,26 @@ static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1163,26 +1236,26 @@ static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, struct btrfs_disk_key *root, struct btrfs_path *path, struct btrfs_disk_key
*key, u64 blocknr, int slot, int level) *key, u64 blocknr, int slot, int level)
{ {
struct btrfs_node *lower; struct extent_buffer *lower;
int nritems; int nritems;
BUG_ON(!path->nodes[level]); BUG_ON(!path->nodes[level]);
lower = btrfs_buffer_node(path->nodes[level]); lower = path->nodes[level];
nritems = btrfs_header_nritems(&lower->header); nritems = btrfs_header_nritems(lower);
if (slot > nritems) if (slot > nritems)
BUG(); BUG();
if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
BUG(); BUG();
if (slot != nritems) { if (slot != nritems) {
btrfs_memmove(root, lower, lower->ptrs + slot + 1, memmove_extent_buffer(lower,
lower->ptrs + slot, btrfs_node_key_ptr_offset(slot + 1),
btrfs_node_key_ptr_offset(slot),
(nritems - slot) * sizeof(struct btrfs_key_ptr)); (nritems - slot) * sizeof(struct btrfs_key_ptr));
} }
btrfs_memcpy(root, lower, &lower->ptrs[slot].key, btrfs_set_node_key(lower, key, slot);
key, sizeof(struct btrfs_disk_key));
btrfs_set_node_blockptr(lower, slot, blocknr); btrfs_set_node_blockptr(lower, slot, blocknr);
btrfs_set_header_nritems(&lower->header, nritems + 1); btrfs_set_header_nritems(lower, nritems + 1);
btrfs_mark_buffer_dirty(path->nodes[level]); btrfs_mark_buffer_dirty(lower);
check_node(root, path, level); check_node(root, path, level);
return 0; return 0;
} }
...@@ -1199,69 +1272,73 @@ static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1199,69 +1272,73 @@ static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level) *root, struct btrfs_path *path, int level)
{ {
struct buffer_head *t; struct extent_buffer *c;
struct btrfs_node *c; struct extent_buffer *split;
struct buffer_head *split_buffer; struct btrfs_disk_key disk_key;
struct btrfs_node *split;
int mid; int mid;
int ret; int ret;
int wret; int wret;
u32 c_nritems; u32 c_nritems;
t = path->nodes[level]; c = path->nodes[level];
c = btrfs_buffer_node(t); if (c == root->node) {
if (t == root->node) {
/* trying to split the root, lets make a new one */ /* trying to split the root, lets make a new one */
ret = insert_new_root(trans, root, path, level + 1); ret = insert_new_root(trans, root, path, level + 1);
if (ret) if (ret)
return ret; return ret;
} else { } else {
ret = push_nodes_for_insert(trans, root, path, level); ret = push_nodes_for_insert(trans, root, path, level);
t = path->nodes[level]; c = path->nodes[level];
c = btrfs_buffer_node(t); if (!ret && btrfs_header_nritems(c) <
if (!ret &&
btrfs_header_nritems(&c->header) <
BTRFS_NODEPTRS_PER_BLOCK(root) - 1) BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
return 0; return 0;
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
c_nritems = btrfs_header_nritems(&c->header); c_nritems = btrfs_header_nritems(c);
split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr, 0); split = btrfs_alloc_free_block(trans, root,
if (IS_ERR(split_buffer)) extent_buffer_blocknr(c), 0);
return PTR_ERR(split_buffer); if (IS_ERR(split))
return PTR_ERR(split);
btrfs_set_header_flags(split, btrfs_header_flags(c));
btrfs_set_header_level(split, btrfs_header_level(c));
btrfs_set_header_blocknr(split, extent_buffer_blocknr(split));
btrfs_set_header_generation(split, trans->transid);
btrfs_set_header_owner(split, root->root_key.objectid);
write_extent_buffer(split, root->fs_info->fsid,
(unsigned long)btrfs_header_fsid(split),
BTRFS_FSID_SIZE);
split = btrfs_buffer_node(split_buffer);
btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
btrfs_set_header_generation(&split->header, trans->transid);
btrfs_set_header_owner(&split->header, root->root_key.objectid);
memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
sizeof(split->header.fsid));
mid = (c_nritems + 1) / 2; mid = (c_nritems + 1) / 2;
btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
copy_extent_buffer(split, c,
btrfs_node_key_ptr_offset(0),
btrfs_node_key_ptr_offset(mid),
(c_nritems - mid) * sizeof(struct btrfs_key_ptr)); (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
btrfs_set_header_nritems(&split->header, c_nritems - mid); btrfs_set_header_nritems(split, c_nritems - mid);
btrfs_set_header_nritems(&c->header, mid); btrfs_set_header_nritems(c, mid);
ret = 0; ret = 0;
btrfs_mark_buffer_dirty(t); btrfs_mark_buffer_dirty(c);
btrfs_mark_buffer_dirty(split_buffer); btrfs_mark_buffer_dirty(split);
wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
bh_blocknr(split_buffer), path->slots[level + 1] + 1, btrfs_node_key(split, &disk_key, 0);
wret = insert_ptr(trans, root, path, &disk_key,
extent_buffer_blocknr(split),
path->slots[level + 1] + 1,
level + 1); level + 1);
if (wret) if (wret)
ret = wret; ret = wret;
if (path->slots[level] >= mid) { if (path->slots[level] >= mid) {
path->slots[level] -= mid; path->slots[level] -= mid;
btrfs_block_release(root, t); free_extent_buffer(c);
path->nodes[level] = split_buffer; path->nodes[level] = split;
path->slots[level + 1] += 1; path->slots[level + 1] += 1;
} else { } else {
btrfs_block_release(root, split_buffer); free_extent_buffer(split);
} }
return ret; return ret;
} }
...@@ -1271,16 +1348,16 @@ static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1271,16 +1348,16 @@ static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
* and nr indicate which items in the leaf to check. This totals up the * and nr indicate which items in the leaf to check. This totals up the
* space used both by the item structs and the item data * space used both by the item structs and the item data
*/ */
static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) static int leaf_space_used(struct extent_buffer *l, int start, int nr)
{ {
int data_len; int data_len;
int nritems = btrfs_header_nritems(&l->header); int nritems = btrfs_header_nritems(l);
int end = min(nritems, start + nr) - 1; int end = min(nritems, start + nr) - 1;
if (!nr) if (!nr)
return 0; return 0;
data_len = btrfs_item_end(l->items + start); data_len = btrfs_item_end_nr(l, start);
data_len = data_len - btrfs_item_offset(l->items + end); data_len = data_len - btrfs_item_offset_nr(l, end);
data_len += sizeof(struct btrfs_item) * nr; data_len += sizeof(struct btrfs_item) * nr;
WARN_ON(data_len < 0); WARN_ON(data_len < 0);
return data_len; return data_len;
...@@ -1291,10 +1368,17 @@ static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) ...@@ -1291,10 +1368,17 @@ static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
* the start of the leaf data. IOW, how much room * the start of the leaf data. IOW, how much room
* the leaf has left for both items and data * the leaf has left for both items and data
*/ */
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
{ {
int nritems = btrfs_header_nritems(&leaf->header); int nritems = btrfs_header_nritems(leaf);
return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); int ret;
ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
if (ret < 0) {
printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
ret, BTRFS_LEAF_DATA_SIZE(root),
leaf_space_used(leaf, 0, nritems), nritems);
}
return ret;
} }
/* /*
...@@ -1307,12 +1391,10 @@ int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) ...@@ -1307,12 +1391,10 @@ int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int data_size) *root, struct btrfs_path *path, int data_size)
{ {
struct buffer_head *left_buf = path->nodes[0]; struct extent_buffer *left = path->nodes[0];
struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf); struct extent_buffer *right;
struct btrfs_leaf *right; struct extent_buffer *upper;
struct buffer_head *right_buf; struct btrfs_disk_key disk_key;
struct buffer_head *upper;
struct btrfs_node *upper_node;
int slot; int slot;
int i; int i;
int free_space; int free_space;
...@@ -1321,6 +1403,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1321,6 +1403,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
struct btrfs_item *item; struct btrfs_item *item;
u32 left_nritems; u32 left_nritems;
u32 right_nritems; u32 right_nritems;
u32 data_end;
int ret; int ret;
slot = path->slots[1]; slot = path->slots[1];
...@@ -1328,102 +1411,109 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1328,102 +1411,109 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
return 1; return 1;
} }
upper = path->nodes[1]; upper = path->nodes[1];
upper_node = btrfs_buffer_node(upper); if (slot >= btrfs_header_nritems(upper) - 1)
if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
return 1; return 1;
}
right_buf = read_tree_block(root, right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1));
btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
right = btrfs_buffer_leaf(right_buf);
free_space = btrfs_leaf_free_space(root, right); free_space = btrfs_leaf_free_space(root, right);
if (free_space < data_size + sizeof(struct btrfs_item)) { if (free_space < data_size + sizeof(struct btrfs_item)) {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
return 1; return 1;
} }
/* cow and double check */ /* cow and double check */
ret = btrfs_cow_block(trans, root, right_buf, upper, ret = btrfs_cow_block(trans, root, right, upper,
slot + 1, &right_buf); slot + 1, &right);
if (ret) { if (ret) {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
return 1; return 1;
} }
right = btrfs_buffer_leaf(right_buf);
free_space = btrfs_leaf_free_space(root, right); free_space = btrfs_leaf_free_space(root, right);
if (free_space < data_size + sizeof(struct btrfs_item)) { if (free_space < data_size + sizeof(struct btrfs_item)) {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
return 1; return 1;
} }
left_nritems = btrfs_header_nritems(&left->header); left_nritems = btrfs_header_nritems(left);
if (left_nritems == 0) { if (left_nritems == 0) {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
return 1; return 1;
} }
for (i = left_nritems - 1; i >= 1; i--) { for (i = left_nritems - 1; i >= 1; i--) {
item = left->items + i; item = btrfs_item_nr(left, i);
if (path->slots[0] == i) if (path->slots[0] == i)
push_space += data_size + sizeof(*item); push_space += data_size + sizeof(*item);
if (btrfs_item_size(item) + sizeof(*item) + push_space > if (btrfs_item_size(left, item) + sizeof(*item) + push_space >
free_space) free_space)
break; break;
push_items++; push_items++;
push_space += btrfs_item_size(item) + sizeof(*item); push_space += btrfs_item_size(left, item) + sizeof(*item);
} }
if (push_items == 0) { if (push_items == 0) {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
return 1; return 1;
} }
if (push_items == left_nritems) if (push_items == left_nritems)
WARN_ON(1); WARN_ON(1);
right_nritems = btrfs_header_nritems(&right->header);
/* push left to right */ /* push left to right */
push_space = btrfs_item_end(left->items + left_nritems - push_items); right_nritems = btrfs_header_nritems(right);
push_space = btrfs_item_end_nr(left, left_nritems - push_items);
push_space -= leaf_data_end(root, left); push_space -= leaf_data_end(root, left);
/* make room in the right data area */ /* make room in the right data area */
btrfs_memmove(root, right, btrfs_leaf_data(right) + data_end = leaf_data_end(root, right);
leaf_data_end(root, right) - push_space, memmove_extent_buffer(right,
btrfs_leaf_data(right) + btrfs_leaf_data(right) + data_end - push_space,
leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) - btrfs_leaf_data(right) + data_end,
leaf_data_end(root, right)); BTRFS_LEAF_DATA_SIZE(root) - data_end);
/* copy from the left data area */ /* copy from the left data area */
btrfs_memcpy(root, right, btrfs_leaf_data(right) + copy_extent_buffer(right, left, btrfs_leaf_data(right) +
BTRFS_LEAF_DATA_SIZE(root) - push_space, BTRFS_LEAF_DATA_SIZE(root) - push_space,
btrfs_leaf_data(left) + leaf_data_end(root, left), btrfs_leaf_data(left) + leaf_data_end(root, left),
push_space); push_space);
btrfs_memmove(root, right, right->items + push_items, right->items,
memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
btrfs_item_nr_offset(0),
right_nritems * sizeof(struct btrfs_item)); right_nritems * sizeof(struct btrfs_item));
/* copy the items from left to right */ /* copy the items from left to right */
btrfs_memcpy(root, right, right->items, left->items + copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
left_nritems - push_items, btrfs_item_nr_offset(left_nritems - push_items),
push_items * sizeof(struct btrfs_item)); push_items * sizeof(struct btrfs_item));
/* update the item pointers */ /* update the item pointers */
right_nritems += push_items; right_nritems += push_items;
btrfs_set_header_nritems(&right->header, right_nritems); btrfs_set_header_nritems(right, right_nritems);
push_space = BTRFS_LEAF_DATA_SIZE(root); push_space = BTRFS_LEAF_DATA_SIZE(root);
for (i = 0; i < right_nritems; i++) { for (i = 0; i < right_nritems; i++) {
btrfs_set_item_offset(right->items + i, push_space - item = btrfs_item_nr(right, i);
btrfs_item_size(right->items + i)); btrfs_set_item_offset(right, item, push_space -
push_space = btrfs_item_offset(right->items + i); btrfs_item_size(right, item));
push_space = btrfs_item_offset(right, item);
} }
left_nritems -= push_items; left_nritems -= push_items;
btrfs_set_header_nritems(&left->header, left_nritems); btrfs_set_header_nritems(left, left_nritems);
btrfs_mark_buffer_dirty(left_buf); btrfs_mark_buffer_dirty(left);
btrfs_mark_buffer_dirty(right_buf); btrfs_mark_buffer_dirty(right);
btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key, btrfs_item_key(right, &disk_key, 0);
&right->items[0].key, sizeof(struct btrfs_disk_key)); btrfs_set_node_key(upper, &disk_key, slot + 1);
btrfs_mark_buffer_dirty(upper); btrfs_mark_buffer_dirty(upper);
/* then fixup the leaf pointer in the path */ /* then fixup the leaf pointer in the path */
if (path->slots[0] >= left_nritems) { if (path->slots[0] >= left_nritems) {
path->slots[0] -= left_nritems; path->slots[0] -= left_nritems;
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = right_buf; path->nodes[0] = right;
path->slots[1] += 1; path->slots[1] += 1;
} else { } else {
btrfs_block_release(root, right_buf); free_extent_buffer(right);
} }
if (path->nodes[1]) if (path->nodes[1])
check_node(root, path, 1); check_node(root, path, 1);
...@@ -1436,10 +1526,9 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1436,10 +1526,9 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int data_size) *root, struct btrfs_path *path, int data_size)
{ {
struct buffer_head *right_buf = path->nodes[0]; struct btrfs_disk_key disk_key;
struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf); struct extent_buffer *right = path->nodes[0];
struct buffer_head *t; struct extent_buffer *left;
struct btrfs_leaf *left;
int slot; int slot;
int i; int i;
int free_space; int free_space;
...@@ -1447,119 +1536,128 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1447,119 +1536,128 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
int push_items = 0; int push_items = 0;
struct btrfs_item *item; struct btrfs_item *item;
u32 old_left_nritems; u32 old_left_nritems;
u32 right_nritems;
int ret = 0; int ret = 0;
int wret; int wret;
slot = path->slots[1]; slot = path->slots[1];
if (slot == 0) { if (slot == 0)
return 1; return 1;
} if (!path->nodes[1])
if (!path->nodes[1]) {
return 1; return 1;
}
t = read_tree_block(root, left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1)); slot - 1));
left = btrfs_buffer_leaf(t);
free_space = btrfs_leaf_free_space(root, left); free_space = btrfs_leaf_free_space(root, left);
if (free_space < data_size + sizeof(struct btrfs_item)) { if (free_space < data_size + sizeof(struct btrfs_item)) {
btrfs_block_release(root, t); free_extent_buffer(left);
return 1; return 1;
} }
/* cow and double check */ /* cow and double check */
ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); ret = btrfs_cow_block(trans, root, left,
path->nodes[1], slot - 1, &left);
if (ret) { if (ret) {
/* we hit -ENOSPC, but it isn't fatal here */ /* we hit -ENOSPC, but it isn't fatal here */
btrfs_block_release(root, t); free_extent_buffer(left);
return 1; return 1;
} }
left = btrfs_buffer_leaf(t);
free_space = btrfs_leaf_free_space(root, left); free_space = btrfs_leaf_free_space(root, left);
if (free_space < data_size + sizeof(struct btrfs_item)) { if (free_space < data_size + sizeof(struct btrfs_item)) {
btrfs_block_release(root, t); free_extent_buffer(left);
return 1; return 1;
} }
if (btrfs_header_nritems(&right->header) == 0) { right_nritems = btrfs_header_nritems(right);
btrfs_block_release(root, t); if (right_nritems == 0) {
free_extent_buffer(left);
return 1; return 1;
} }
for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) { for (i = 0; i < right_nritems - 1; i++) {
item = right->items + i; item = btrfs_item_nr(right, i);
if (path->slots[0] == i) if (path->slots[0] == i)
push_space += data_size + sizeof(*item); push_space += data_size + sizeof(*item);
if (btrfs_item_size(item) + sizeof(*item) + push_space > if (btrfs_item_size(right, item) + sizeof(*item) + push_space >
free_space) free_space)
break; break;
push_items++; push_items++;
push_space += btrfs_item_size(item) + sizeof(*item); push_space += btrfs_item_size(right, item) + sizeof(*item);
} }
if (push_items == 0) { if (push_items == 0) {
btrfs_block_release(root, t); free_extent_buffer(left);
return 1; return 1;
} }
if (push_items == btrfs_header_nritems(&right->header)) if (push_items == btrfs_header_nritems(right))
WARN_ON(1); WARN_ON(1);
/* push data from right to left */ /* push data from right to left */
btrfs_memcpy(root, left, left->items + copy_extent_buffer(left, right,
btrfs_header_nritems(&left->header), btrfs_item_nr_offset(btrfs_header_nritems(left)),
right->items, push_items * sizeof(struct btrfs_item)); btrfs_item_nr_offset(0),
push_items * sizeof(struct btrfs_item));
push_space = BTRFS_LEAF_DATA_SIZE(root) - push_space = BTRFS_LEAF_DATA_SIZE(root) -
btrfs_item_offset(right->items + push_items -1); btrfs_item_offset_nr(right, push_items -1);
btrfs_memcpy(root, left, btrfs_leaf_data(left) +
copy_extent_buffer(left, right, btrfs_leaf_data(left) +
leaf_data_end(root, left) - push_space, leaf_data_end(root, left) - push_space,
btrfs_leaf_data(right) + btrfs_leaf_data(right) +
btrfs_item_offset(right->items + push_items - 1), btrfs_item_offset_nr(right, push_items - 1),
push_space); push_space);
old_left_nritems = btrfs_header_nritems(&left->header); old_left_nritems = btrfs_header_nritems(left);
BUG_ON(old_left_nritems < 0); BUG_ON(old_left_nritems < 0);
for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
u32 ioff = btrfs_item_offset(left->items + i); u32 ioff;
btrfs_set_item_offset(left->items + i, ioff - item = btrfs_item_nr(left, i);
(BTRFS_LEAF_DATA_SIZE(root) - ioff = btrfs_item_offset(left, item);
btrfs_item_offset(left->items + btrfs_set_item_offset(left, item,
old_left_nritems - 1))); ioff - (BTRFS_LEAF_DATA_SIZE(root) -
btrfs_item_offset_nr(left, old_left_nritems - 1)));
} }
btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); btrfs_set_header_nritems(left, old_left_nritems + push_items);
/* fixup right node */ /* fixup right node */
push_space = btrfs_item_offset(right->items + push_items - 1) - push_space = btrfs_item_offset_nr(right, push_items - 1) -
leaf_data_end(root, right); leaf_data_end(root, right);
btrfs_memmove(root, right, btrfs_leaf_data(right) + memmove_extent_buffer(right, btrfs_leaf_data(right) +
BTRFS_LEAF_DATA_SIZE(root) - push_space, BTRFS_LEAF_DATA_SIZE(root) - push_space,
btrfs_leaf_data(right) + btrfs_leaf_data(right) +
leaf_data_end(root, right), push_space); leaf_data_end(root, right), push_space);
btrfs_memmove(root, right, right->items, right->items + push_items,
(btrfs_header_nritems(&right->header) - push_items) * memmove_extent_buffer(right, btrfs_item_nr_offset(0),
btrfs_item_nr_offset(push_items),
(btrfs_header_nritems(right) - push_items) *
sizeof(struct btrfs_item)); sizeof(struct btrfs_item));
btrfs_set_header_nritems(&right->header,
btrfs_header_nritems(&right->header) - right_nritems = btrfs_header_nritems(right) - push_items;
push_items); btrfs_set_header_nritems(right, right_nritems);
push_space = BTRFS_LEAF_DATA_SIZE(root); push_space = BTRFS_LEAF_DATA_SIZE(root);
for (i = 0; i < btrfs_header_nritems(&right->header); i++) { for (i = 0; i < right_nritems; i++) {
btrfs_set_item_offset(right->items + i, push_space - item = btrfs_item_nr(right, i);
btrfs_item_size(right->items + i)); btrfs_set_item_offset(right, item, push_space -
push_space = btrfs_item_offset(right->items + i); btrfs_item_size(right, item));
push_space = btrfs_item_offset(right, item);
} }
btrfs_mark_buffer_dirty(t); btrfs_mark_buffer_dirty(left);
btrfs_mark_buffer_dirty(right_buf); btrfs_mark_buffer_dirty(right);
wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); btrfs_item_key(right, &disk_key, 0);
wret = fixup_low_keys(trans, root, path, &disk_key, 1);
if (wret) if (wret)
ret = wret; ret = wret;
/* then fixup the leaf pointer in the path */ /* then fixup the leaf pointer in the path */
if (path->slots[0] < push_items) { if (path->slots[0] < push_items) {
path->slots[0] += old_left_nritems; path->slots[0] += old_left_nritems;
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = t; path->nodes[0] = left;
path->slots[1] -= 1; path->slots[1] -= 1;
} else { } else {
btrfs_block_release(root, t); free_extent_buffer(left);
path->slots[0] -= push_items; path->slots[0] -= push_items;
} }
BUG_ON(path->slots[0] < 0); BUG_ON(path->slots[0] < 0);
...@@ -1578,13 +1676,11 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1578,13 +1676,11 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *ins_key, *root, struct btrfs_key *ins_key,
struct btrfs_path *path, int data_size) struct btrfs_path *path, int data_size)
{ {
struct buffer_head *l_buf; struct extent_buffer *l;
struct btrfs_leaf *l;
u32 nritems; u32 nritems;
int mid; int mid;
int slot; int slot;
struct btrfs_leaf *right; struct extent_buffer *right;
struct buffer_head *right_buffer;
int space_needed = data_size + sizeof(struct btrfs_item); int space_needed = data_size + sizeof(struct btrfs_item);
int data_copy_size; int data_copy_size;
int rt_data_off; int rt_data_off;
...@@ -1603,8 +1699,7 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1603,8 +1699,7 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
if (wret < 0) if (wret < 0)
return wret; return wret;
} }
l_buf = path->nodes[0]; l = path->nodes[0];
l = btrfs_buffer_leaf(l_buf);
/* did the pushes work? */ /* did the pushes work? */
if (btrfs_leaf_free_space(root, l) >= if (btrfs_leaf_free_space(root, l) >=
...@@ -1617,36 +1712,38 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1617,36 +1712,38 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
return ret; return ret;
} }
slot = path->slots[0]; slot = path->slots[0];
nritems = btrfs_header_nritems(&l->header); nritems = btrfs_header_nritems(l);
mid = (nritems + 1)/ 2; mid = (nritems + 1)/ 2;
right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0); right = btrfs_alloc_free_block(trans, root,
if (IS_ERR(right_buffer)) extent_buffer_blocknr(l), 0);
return PTR_ERR(right_buffer); if (IS_ERR(right))
return PTR_ERR(right);
right = btrfs_buffer_leaf(right_buffer);
memset(&right->header, 0, sizeof(right->header)); memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); btrfs_set_header_blocknr(right, extent_buffer_blocknr(right));
btrfs_set_header_generation(&right->header, trans->transid); btrfs_set_header_generation(right, trans->transid);
btrfs_set_header_owner(&right->header, root->root_key.objectid); btrfs_set_header_owner(right, root->root_key.objectid);
btrfs_set_header_level(&right->header, 0); btrfs_set_header_level(right, 0);
memcpy(right->header.fsid, root->fs_info->disk_super->fsid, write_extent_buffer(right, root->fs_info->fsid,
sizeof(right->header.fsid)); (unsigned long)btrfs_header_fsid(right),
BTRFS_FSID_SIZE);
if (mid <= slot) { if (mid <= slot) {
if (nritems == 1 || if (nritems == 1 ||
leaf_space_used(l, mid, nritems - mid) + space_needed > leaf_space_used(l, mid, nritems - mid) + space_needed >
BTRFS_LEAF_DATA_SIZE(root)) { BTRFS_LEAF_DATA_SIZE(root)) {
if (slot >= nritems) { if (slot >= nritems) {
btrfs_cpu_key_to_disk(&disk_key, ins_key); btrfs_cpu_key_to_disk(&disk_key, ins_key);
btrfs_set_header_nritems(&right->header, 0); btrfs_set_header_nritems(right, 0);
wret = insert_ptr(trans, root, path, wret = insert_ptr(trans, root, path,
&disk_key, &disk_key,
bh_blocknr(right_buffer), extent_buffer_blocknr(right),
path->slots[1] + 1, 1); path->slots[1] + 1, 1);
if (wret) if (wret)
ret = wret; ret = wret;
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = right_buffer; path->nodes[0] = right;
path->slots[0] = 0; path->slots[0] = 0;
path->slots[1] += 1; path->slots[1] += 1;
return ret; return ret;
...@@ -1659,15 +1756,15 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1659,15 +1756,15 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
BTRFS_LEAF_DATA_SIZE(root)) { BTRFS_LEAF_DATA_SIZE(root)) {
if (slot == 0) { if (slot == 0) {
btrfs_cpu_key_to_disk(&disk_key, ins_key); btrfs_cpu_key_to_disk(&disk_key, ins_key);
btrfs_set_header_nritems(&right->header, 0); btrfs_set_header_nritems(right, 0);
wret = insert_ptr(trans, root, path, wret = insert_ptr(trans, root, path,
&disk_key, &disk_key,
bh_blocknr(right_buffer), extent_buffer_blocknr(right),
path->slots[1], 1); path->slots[1], 1);
if (wret) if (wret)
ret = wret; ret = wret;
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = right_buffer; path->nodes[0] = right;
path->slots[0] = 0; path->slots[0] = 0;
if (path->slots[1] == 0) { if (path->slots[1] == 0) {
wret = fixup_low_keys(trans, root, wret = fixup_low_keys(trans, root,
...@@ -1681,61 +1778,74 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1681,61 +1778,74 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
double_split = 1; double_split = 1;
} }
} }
btrfs_set_header_nritems(&right->header, nritems - mid); nritems = nritems - mid;
data_copy_size = btrfs_item_end(l->items + mid) - btrfs_set_header_nritems(right, nritems);
leaf_data_end(root, l); data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
btrfs_memcpy(root, right, right->items, l->items + mid,
(nritems - mid) * sizeof(struct btrfs_item)); copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
btrfs_memcpy(root, right, btrfs_item_nr_offset(mid),
nritems * sizeof(struct btrfs_item));
copy_extent_buffer(right, l,
btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
data_copy_size, btrfs_leaf_data(l) + data_copy_size, btrfs_leaf_data(l) +
leaf_data_end(root, l), data_copy_size); leaf_data_end(root, l), data_copy_size);
rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
btrfs_item_end(l->items + mid); btrfs_item_end_nr(l, mid);
for (i = 0; i < btrfs_header_nritems(&right->header); i++) { for (i = 0; i < nritems; i++) {
u32 ioff = btrfs_item_offset(right->items + i); struct btrfs_item *item = btrfs_item_nr(right, i);
btrfs_set_item_offset(right->items + i, ioff + rt_data_off); u32 ioff = btrfs_item_offset(right, item);
btrfs_set_item_offset(right, item, ioff + rt_data_off);
} }
btrfs_set_header_nritems(&l->header, mid); btrfs_set_header_nritems(l, mid);
ret = 0; ret = 0;
wret = insert_ptr(trans, root, path, &right->items[0].key, btrfs_item_key(right, &disk_key, 0);
bh_blocknr(right_buffer), path->slots[1] + 1, 1); wret = insert_ptr(trans, root, path, &disk_key,
extent_buffer_blocknr(right), path->slots[1] + 1, 1);
if (wret) if (wret)
ret = wret; ret = wret;
btrfs_mark_buffer_dirty(right_buffer);
btrfs_mark_buffer_dirty(l_buf); btrfs_mark_buffer_dirty(right);
btrfs_mark_buffer_dirty(l);
BUG_ON(path->slots[0] != slot); BUG_ON(path->slots[0] != slot);
if (mid <= slot) { if (mid <= slot) {
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = right_buffer; path->nodes[0] = right;
path->slots[0] -= mid; path->slots[0] -= mid;
path->slots[1] += 1; path->slots[1] += 1;
} else } else
btrfs_block_release(root, right_buffer); free_extent_buffer(right);
BUG_ON(path->slots[0] < 0); BUG_ON(path->slots[0] < 0);
check_node(root, path, 1); check_node(root, path, 1);
check_leaf(root, path, 0);
if (!double_split) if (!double_split)
return ret; return ret;
right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
if (IS_ERR(right_buffer)) right = btrfs_alloc_free_block(trans, root,
return PTR_ERR(right_buffer); extent_buffer_blocknr(l), 0);
if (IS_ERR(right))
right = btrfs_buffer_leaf(right_buffer); return PTR_ERR(right);
memset(&right->header, 0, sizeof(right->header));
btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
btrfs_set_header_generation(&right->header, trans->transid); btrfs_set_header_blocknr(right, extent_buffer_blocknr(right));
btrfs_set_header_owner(&right->header, root->root_key.objectid); btrfs_set_header_generation(right, trans->transid);
btrfs_set_header_level(&right->header, 0); btrfs_set_header_owner(right, root->root_key.objectid);
memcpy(right->header.fsid, root->fs_info->disk_super->fsid, btrfs_set_header_level(right, 0);
sizeof(right->header.fsid)); write_extent_buffer(right, root->fs_info->fsid,
(unsigned long)btrfs_header_fsid(right),
BTRFS_FSID_SIZE);
btrfs_cpu_key_to_disk(&disk_key, ins_key); btrfs_cpu_key_to_disk(&disk_key, ins_key);
btrfs_set_header_nritems(&right->header, 0); btrfs_set_header_nritems(right, 0);
wret = insert_ptr(trans, root, path, wret = insert_ptr(trans, root, path,
&disk_key, &disk_key,
bh_blocknr(right_buffer), extent_buffer_blocknr(right),
path->slots[1], 1); path->slots[1], 1);
if (wret) if (wret)
ret = wret; ret = wret;
...@@ -1744,8 +1854,8 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1744,8 +1854,8 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
if (wret) if (wret)
ret = wret; ret = wret;
} }
btrfs_block_release(root, path->nodes[0]); free_extent_buffer(path->nodes[0]);
path->nodes[0] = right_buffer; path->nodes[0] = right;
path->slots[0] = 0; path->slots[0] = 0;
check_node(root, path, 1); check_node(root, path, 1);
check_leaf(root, path, 0); check_leaf(root, path, 0);
...@@ -1760,8 +1870,8 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans, ...@@ -1760,8 +1870,8 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
int ret = 0; int ret = 0;
int slot; int slot;
int slot_orig; int slot_orig;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct buffer_head *leaf_buf; struct btrfs_item *item;
u32 nritems; u32 nritems;
unsigned int data_end; unsigned int data_end;
unsigned int old_data_start; unsigned int old_data_start;
...@@ -1770,15 +1880,14 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans, ...@@ -1770,15 +1880,14 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
int i; int i;
slot_orig = path->slots[0]; slot_orig = path->slots[0];
leaf_buf = path->nodes[0]; leaf = path->nodes[0];
leaf = btrfs_buffer_leaf(leaf_buf);
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
data_end = leaf_data_end(root, leaf); data_end = leaf_data_end(root, leaf);
slot = path->slots[0]; slot = path->slots[0];
old_data_start = btrfs_item_offset(leaf->items + slot); old_data_start = btrfs_item_offset_nr(leaf, slot);
old_size = btrfs_item_size(leaf->items + slot); old_size = btrfs_item_size_nr(leaf, slot);
BUG_ON(old_size <= new_size); BUG_ON(old_size <= new_size);
size_diff = old_size - new_size; size_diff = old_size - new_size;
...@@ -1790,32 +1899,38 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans, ...@@ -1790,32 +1899,38 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
*/ */
/* first correct the data pointers */ /* first correct the data pointers */
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
u32 ioff = btrfs_item_offset(leaf->items + i); u32 ioff;
btrfs_set_item_offset(leaf->items + i, item = btrfs_item_nr(leaf, i);
ioff + size_diff); ioff = btrfs_item_offset(leaf, item);
btrfs_set_item_offset(leaf, item, ioff + size_diff);
} }
/* shift the data */ /* shift the data */
btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
data_end + size_diff, btrfs_leaf_data(leaf) + data_end + size_diff, btrfs_leaf_data(leaf) +
data_end, old_data_start + new_size - data_end); data_end, old_data_start + new_size - data_end);
btrfs_set_item_size(leaf->items + slot, new_size);
btrfs_mark_buffer_dirty(leaf_buf); item = btrfs_item_nr(leaf, slot);
btrfs_set_item_size(leaf, item, new_size);
btrfs_mark_buffer_dirty(leaf);
ret = 0; ret = 0;
if (btrfs_leaf_free_space(root, leaf) < 0) if (btrfs_leaf_free_space(root, leaf) < 0) {
btrfs_print_leaf(root, leaf);
BUG(); BUG();
}
check_leaf(root, path, 0); check_leaf(root, path, 0);
return ret; return ret;
} }
int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_extend_item(struct btrfs_trans_handle *trans,
*root, struct btrfs_path *path, u32 data_size) struct btrfs_root *root, struct btrfs_path *path,
u32 data_size)
{ {
int ret = 0; int ret = 0;
int slot; int slot;
int slot_orig; int slot_orig;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct buffer_head *leaf_buf; struct btrfs_item *item;
u32 nritems; u32 nritems;
unsigned int data_end; unsigned int data_end;
unsigned int old_data; unsigned int old_data;
...@@ -1823,16 +1938,17 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1823,16 +1938,17 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
int i; int i;
slot_orig = path->slots[0]; slot_orig = path->slots[0];
leaf_buf = path->nodes[0]; leaf = path->nodes[0];
leaf = btrfs_buffer_leaf(leaf_buf);
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
data_end = leaf_data_end(root, leaf); data_end = leaf_data_end(root, leaf);
if (btrfs_leaf_free_space(root, leaf) < data_size) if (btrfs_leaf_free_space(root, leaf) < data_size) {
btrfs_print_leaf(root, leaf);
BUG(); BUG();
}
slot = path->slots[0]; slot = path->slots[0];
old_data = btrfs_item_end(leaf->items + slot); old_data = btrfs_item_end_nr(leaf, slot);
BUG_ON(slot < 0); BUG_ON(slot < 0);
BUG_ON(slot >= nritems); BUG_ON(slot >= nritems);
...@@ -1842,22 +1958,28 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1842,22 +1958,28 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
*/ */
/* first correct the data pointers */ /* first correct the data pointers */
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
u32 ioff = btrfs_item_offset(leaf->items + i); u32 ioff;
btrfs_set_item_offset(leaf->items + i, item = btrfs_item_nr(leaf, i);
ioff - data_size); ioff = btrfs_item_offset(leaf, item);
btrfs_set_item_offset(leaf, item, ioff - data_size);
} }
/* shift the data */ /* shift the data */
btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
data_end - data_size, btrfs_leaf_data(leaf) + data_end - data_size, btrfs_leaf_data(leaf) +
data_end, old_data - data_end); data_end, old_data - data_end);
data_end = old_data; data_end = old_data;
old_size = btrfs_item_size(leaf->items + slot); old_size = btrfs_item_size_nr(leaf, slot);
btrfs_set_item_size(leaf->items + slot, old_size + data_size); item = btrfs_item_nr(leaf, slot);
btrfs_mark_buffer_dirty(leaf_buf); btrfs_set_item_size(leaf, item, old_size + data_size);
btrfs_mark_buffer_dirty(leaf);
ret = 0; ret = 0;
if (btrfs_leaf_free_space(root, leaf) < 0) if (btrfs_leaf_free_space(root, leaf) < 0) {
btrfs_print_leaf(root, leaf);
BUG(); BUG();
}
check_leaf(root, path, 0); check_leaf(root, path, 0);
return ret; return ret;
} }
...@@ -1866,15 +1988,16 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1866,15 +1988,16 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
* Given a key and some data, insert an item into the tree. * Given a key and some data, insert an item into the tree.
* This does all the path init required, making room in the tree if needed. * This does all the path init required, making room in the tree if needed.
*/ */
int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
*root, struct btrfs_path *path, struct btrfs_key struct btrfs_root *root,
*cpu_key, u32 data_size) struct btrfs_path *path,
struct btrfs_key *cpu_key, u32 data_size)
{ {
struct extent_buffer *leaf;
struct btrfs_item *item;
int ret = 0; int ret = 0;
int slot; int slot;
int slot_orig; int slot_orig;
struct btrfs_leaf *leaf;
struct buffer_head *leaf_buf;
u32 nritems; u32 nritems;
unsigned int data_end; unsigned int data_end;
struct btrfs_disk_key disk_key; struct btrfs_disk_key disk_key;
...@@ -1884,6 +2007,7 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1884,6 +2007,7 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
/* create a root if there isn't one */ /* create a root if there isn't one */
if (!root->node) if (!root->node)
BUG(); BUG();
ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
if (ret == 0) { if (ret == 0) {
return -EEXIST; return -EEXIST;
...@@ -1892,57 +2016,68 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1892,57 +2016,68 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
goto out; goto out;
slot_orig = path->slots[0]; slot_orig = path->slots[0];
leaf_buf = path->nodes[0]; leaf = path->nodes[0];
leaf = btrfs_buffer_leaf(leaf_buf);
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
data_end = leaf_data_end(root, leaf); data_end = leaf_data_end(root, leaf);
if (btrfs_leaf_free_space(root, leaf) < if (btrfs_leaf_free_space(root, leaf) <
sizeof(struct btrfs_item) + data_size) { sizeof(struct btrfs_item) + data_size) {
BUG(); BUG();
} }
slot = path->slots[0]; slot = path->slots[0];
BUG_ON(slot < 0); BUG_ON(slot < 0);
if (slot != nritems) { if (slot != nritems) {
int i; int i;
unsigned int old_data = btrfs_item_end(leaf->items + slot); unsigned int old_data = btrfs_item_end_nr(leaf, slot);
if (old_data < data_end) {
btrfs_print_leaf(root, leaf);
printk("slot %d old_data %d data_end %d\n",
slot, old_data, data_end);
BUG_ON(1);
}
/* /*
* item0..itemN ... dataN.offset..dataN.size .. data0.size * item0..itemN ... dataN.offset..dataN.size .. data0.size
*/ */
/* first correct the data pointers */ /* first correct the data pointers */
for (i = slot; i < nritems; i++) { for (i = slot; i < nritems; i++) {
u32 ioff = btrfs_item_offset(leaf->items + i); u32 ioff;
btrfs_set_item_offset(leaf->items + i, item = btrfs_item_nr(leaf, i);
ioff - data_size); ioff = btrfs_item_offset(leaf, item);
btrfs_set_item_offset(leaf, item, ioff - data_size);
} }
/* shift the items */ /* shift the items */
btrfs_memmove(root, leaf, leaf->items + slot + 1, memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
leaf->items + slot, btrfs_item_nr_offset(slot),
(nritems - slot) * sizeof(struct btrfs_item)); (nritems - slot) * sizeof(struct btrfs_item));
/* shift the data */ /* shift the data */
btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
data_end - data_size, btrfs_leaf_data(leaf) + data_end - data_size, btrfs_leaf_data(leaf) +
data_end, old_data - data_end); data_end, old_data - data_end);
data_end = old_data; data_end = old_data;
} }
/* setup the item for the new data */ /* setup the item for the new data */
btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key, btrfs_set_item_key(leaf, &disk_key, slot);
sizeof(struct btrfs_disk_key)); item = btrfs_item_nr(leaf, slot);
btrfs_set_item_offset(leaf->items + slot, data_end - data_size); btrfs_set_item_offset(leaf, item, data_end - data_size);
btrfs_set_item_size(leaf->items + slot, data_size); btrfs_set_item_size(leaf, item, data_size);
btrfs_set_header_nritems(&leaf->header, nritems + 1); btrfs_set_header_nritems(leaf, nritems + 1);
btrfs_mark_buffer_dirty(leaf_buf); btrfs_mark_buffer_dirty(leaf);
ret = 0; ret = 0;
if (slot == 0) if (slot == 0)
ret = fixup_low_keys(trans, root, path, &disk_key, 1); ret = fixup_low_keys(trans, root, path, &disk_key, 1);
if (btrfs_leaf_free_space(root, leaf) < 0) if (btrfs_leaf_free_space(root, leaf) < 0) {
btrfs_print_leaf(root, leaf);
BUG(); BUG();
}
check_leaf(root, path, 0); check_leaf(root, path, 0);
out: out:
return ret; return ret;
...@@ -1958,17 +2093,17 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1958,17 +2093,17 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
{ {
int ret = 0; int ret = 0;
struct btrfs_path *path; struct btrfs_path *path;
u8 *ptr; struct extent_buffer *leaf;
unsigned long ptr;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
if (!ret) { if (!ret) {
ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], u8); ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
btrfs_memcpy(root, path->nodes[0]->b_data, write_extent_buffer(leaf, data, ptr, data_size);
ptr, data, data_size); btrfs_mark_buffer_dirty(leaf);
btrfs_mark_buffer_dirty(path->nodes[0]);
} }
btrfs_free_path(path); btrfs_free_path(path);
return ret; return ret;
...@@ -1984,30 +2119,30 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1984,30 +2119,30 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_path *path, int level, int slot) struct btrfs_path *path, int level, int slot)
{ {
struct btrfs_node *node; struct extent_buffer *parent = path->nodes[level];
struct buffer_head *parent = path->nodes[level];
u32 nritems; u32 nritems;
int ret = 0; int ret = 0;
int wret; int wret;
node = btrfs_buffer_node(parent); nritems = btrfs_header_nritems(parent);
nritems = btrfs_header_nritems(&node->header);
if (slot != nritems -1) { if (slot != nritems -1) {
btrfs_memmove(root, node, node->ptrs + slot, memmove_extent_buffer(parent,
node->ptrs + slot + 1, btrfs_node_key_ptr_offset(slot),
btrfs_node_key_ptr_offset(slot + 1),
sizeof(struct btrfs_key_ptr) * sizeof(struct btrfs_key_ptr) *
(nritems - slot - 1)); (nritems - slot - 1));
} }
nritems--; nritems--;
btrfs_set_header_nritems(&node->header, nritems); btrfs_set_header_nritems(parent, nritems);
if (nritems == 0 && parent == root->node) { if (nritems == 0 && parent == root->node) {
struct btrfs_header *header = btrfs_buffer_header(root->node); BUG_ON(btrfs_header_level(root->node) != 1);
BUG_ON(btrfs_header_level(header) != 1);
/* just turn the root into a leaf and break */ /* just turn the root into a leaf and break */
btrfs_set_header_level(header, 0); btrfs_set_header_level(root->node, 0);
} else if (slot == 0) { } else if (slot == 0) {
wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, struct btrfs_disk_key disk_key;
level + 1);
btrfs_node_key(parent, &disk_key, 0);
wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
if (wret) if (wret)
ret = wret; ret = wret;
} }
...@@ -2023,59 +2158,67 @@ int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -2023,59 +2158,67 @@ int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_path *path) struct btrfs_path *path)
{ {
int slot; int slot;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct buffer_head *leaf_buf; struct btrfs_item *item;
int doff; int doff;
int dsize; int dsize;
int ret = 0; int ret = 0;
int wret; int wret;
u32 nritems; u32 nritems;
leaf_buf = path->nodes[0]; leaf = path->nodes[0];
leaf = btrfs_buffer_leaf(leaf_buf);
slot = path->slots[0]; slot = path->slots[0];
doff = btrfs_item_offset(leaf->items + slot); doff = btrfs_item_offset_nr(leaf, slot);
dsize = btrfs_item_size(leaf->items + slot); dsize = btrfs_item_size_nr(leaf, slot);
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
if (slot != nritems - 1) { if (slot != nritems - 1) {
int i; int i;
int data_end = leaf_data_end(root, leaf); int data_end = leaf_data_end(root, leaf);
btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
data_end + dsize, data_end + dsize,
btrfs_leaf_data(leaf) + data_end, btrfs_leaf_data(leaf) + data_end,
doff - data_end); doff - data_end);
for (i = slot + 1; i < nritems; i++) { for (i = slot + 1; i < nritems; i++) {
u32 ioff = btrfs_item_offset(leaf->items + i); u32 ioff;
btrfs_set_item_offset(leaf->items + i, ioff + dsize); item = btrfs_item_nr(leaf, i);
ioff = btrfs_item_offset(leaf, item);
btrfs_set_item_offset(leaf, item, ioff + dsize);
} }
btrfs_memmove(root, leaf, leaf->items + slot, memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
leaf->items + slot + 1, btrfs_item_nr_offset(slot + 1),
sizeof(struct btrfs_item) * sizeof(struct btrfs_item) *
(nritems - slot - 1)); (nritems - slot - 1));
} }
btrfs_set_header_nritems(&leaf->header, nritems - 1); btrfs_set_header_nritems(leaf, nritems - 1);
nritems--; nritems--;
/* delete the leaf if we've emptied it */ /* delete the leaf if we've emptied it */
if (nritems == 0) { if (nritems == 0) {
if (leaf_buf == root->node) { if (leaf == root->node) {
btrfs_set_header_level(&leaf->header, 0); btrfs_set_header_level(leaf, 0);
} else { } else {
clean_tree_block(trans, root, leaf_buf); clean_tree_block(trans, root, leaf);
wait_on_buffer(leaf_buf); wait_on_tree_block_writeback(root, leaf);
wret = del_ptr(trans, root, path, 1, path->slots[1]); wret = del_ptr(trans, root, path, 1, path->slots[1]);
if (wret) if (wret)
ret = wret; ret = wret;
wret = btrfs_free_extent(trans, root, wret = btrfs_free_extent(trans, root,
bh_blocknr(leaf_buf), 1, 1); extent_buffer_blocknr(leaf),
1, 1);
if (wret) if (wret)
ret = wret; ret = wret;
} }
} else { } else {
int used = leaf_space_used(leaf, 0, nritems); int used = leaf_space_used(leaf, 0, nritems);
if (slot == 0) { if (slot == 0) {
struct btrfs_disk_key disk_key;
btrfs_item_key(leaf, &disk_key, 0);
wret = fixup_low_keys(trans, root, path, wret = fixup_low_keys(trans, root, path,
&leaf->items[0].key, 1); &disk_key, 1);
if (wret) if (wret)
ret = wret; ret = wret;
} }
...@@ -2087,34 +2230,40 @@ int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -2087,34 +2230,40 @@ int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
* for possible call to del_ptr below * for possible call to del_ptr below
*/ */
slot = path->slots[1]; slot = path->slots[1];
get_bh(leaf_buf); extent_buffer_get(leaf);
wret = push_leaf_left(trans, root, path, 1); wret = push_leaf_left(trans, root, path, 1);
if (wret < 0 && wret != -ENOSPC) if (wret < 0 && wret != -ENOSPC)
ret = wret; ret = wret;
if (path->nodes[0] == leaf_buf &&
btrfs_header_nritems(&leaf->header)) { if (path->nodes[0] == leaf &&
btrfs_header_nritems(leaf)) {
wret = push_leaf_right(trans, root, path, 1); wret = push_leaf_right(trans, root, path, 1);
if (wret < 0 && wret != -ENOSPC) if (wret < 0 && wret != -ENOSPC)
ret = wret; ret = wret;
} }
if (btrfs_header_nritems(&leaf->header) == 0) {
u64 blocknr = bh_blocknr(leaf_buf); if (btrfs_header_nritems(leaf) == 0) {
clean_tree_block(trans, root, leaf_buf); u64 blocknr = extent_buffer_blocknr(leaf);
wait_on_buffer(leaf_buf);
clean_tree_block(trans, root, leaf);
wait_on_tree_block_writeback(root, leaf);
wret = del_ptr(trans, root, path, 1, slot); wret = del_ptr(trans, root, path, 1, slot);
if (wret) if (wret)
ret = wret; ret = wret;
btrfs_block_release(root, leaf_buf);
free_extent_buffer(leaf);
wret = btrfs_free_extent(trans, root, blocknr, wret = btrfs_free_extent(trans, root, blocknr,
1, 1); 1, 1);
if (wret) if (wret)
ret = wret; ret = wret;
} else { } else {
btrfs_mark_buffer_dirty(leaf_buf); btrfs_mark_buffer_dirty(leaf);
btrfs_block_release(root, leaf_buf); free_extent_buffer(leaf);
} }
} else { } else {
btrfs_mark_buffer_dirty(leaf_buf); btrfs_mark_buffer_dirty(leaf);
} }
} }
return ret; return ret;
...@@ -2130,25 +2279,27 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) ...@@ -2130,25 +2279,27 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
int slot; int slot;
int level = 1; int level = 1;
u64 blocknr; u64 blocknr;
struct buffer_head *c; struct extent_buffer *c;
struct btrfs_node *c_node; struct extent_buffer *next = NULL;
struct buffer_head *next = NULL;
while(level < BTRFS_MAX_LEVEL) { while(level < BTRFS_MAX_LEVEL) {
if (!path->nodes[level]) if (!path->nodes[level])
return 1; return 1;
slot = path->slots[level] + 1; slot = path->slots[level] + 1;
c = path->nodes[level]; c = path->nodes[level];
c_node = btrfs_buffer_node(c); if (slot >= btrfs_header_nritems(c)) {
if (slot >= btrfs_header_nritems(&c_node->header)) {
level++; level++;
continue; continue;
} }
blocknr = btrfs_node_blockptr(c_node, slot);
blocknr = btrfs_node_blockptr(c, slot);
if (next) if (next)
btrfs_block_release(root, next); free_extent_buffer(next);
if (path->reada) if (path->reada)
reada_for_search(root, path, level, slot); reada_for_search(root, path, level, slot);
next = read_tree_block(root, blocknr); next = read_tree_block(root, blocknr);
break; break;
} }
...@@ -2156,15 +2307,14 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) ...@@ -2156,15 +2307,14 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
while(1) { while(1) {
level--; level--;
c = path->nodes[level]; c = path->nodes[level];
btrfs_block_release(root, c); free_extent_buffer(c);
path->nodes[level] = next; path->nodes[level] = next;
path->slots[level] = 0; path->slots[level] = 0;
if (!level) if (!level)
break; break;
if (path->reada) if (path->reada)
reada_for_search(root, path, level, 0); reada_for_search(root, path, level, 0);
next = read_tree_block(root, next = read_tree_block(root, btrfs_node_blockptr(next, 0));
btrfs_node_blockptr(btrfs_buffer_node(next), 0));
} }
return 0; return 0;
} }
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
#define __BTRFS__ #define __BTRFS__
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/completion.h> #include <linux/completion.h>
#include "bit-radix.h" #include "bit-radix.h"
#include "extent_map.h"
struct btrfs_trans_handle; struct btrfs_trans_handle;
struct btrfs_transaction; struct btrfs_transaction;
...@@ -32,7 +32,7 @@ extern struct kmem_cache *btrfs_transaction_cachep; ...@@ -32,7 +32,7 @@ extern struct kmem_cache *btrfs_transaction_cachep;
extern struct kmem_cache *btrfs_bit_radix_cachep; extern struct kmem_cache *btrfs_bit_radix_cachep;
extern struct kmem_cache *btrfs_path_cachep; extern struct kmem_cache *btrfs_path_cachep;
#define BTRFS_MAGIC "_BtRfS_M" #define BTRFS_MAGIC "_B2RfS_M"
#define BTRFS_ROOT_TREE_OBJECTID 1ULL #define BTRFS_ROOT_TREE_OBJECTID 1ULL
#define BTRFS_EXTENT_TREE_OBJECTID 2ULL #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
...@@ -78,41 +78,41 @@ extern struct kmem_cache *btrfs_path_cachep; ...@@ -78,41 +78,41 @@ extern struct kmem_cache *btrfs_path_cachep;
*/ */
struct btrfs_disk_key { struct btrfs_disk_key {
__le64 objectid; __le64 objectid;
__le32 flags; u8 type;
__le64 offset; __le64 offset;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
struct btrfs_key { struct btrfs_key {
u64 objectid; u64 objectid;
u32 flags; u8 type;
u64 offset; u64 offset;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
#define BTRFS_FSID_SIZE 16
/* /*
* every tree block (leaf or node) starts with this header. * every tree block (leaf or node) starts with this header.
*/ */
struct btrfs_header { struct btrfs_header {
u8 csum[BTRFS_CSUM_SIZE]; u8 csum[BTRFS_CSUM_SIZE];
u8 fsid[16]; /* FS specific uuid */ u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
__le64 blocknr; /* which block this node is supposed to live in */ __le64 blocknr; /* which block this node is supposed to live in */
__le64 generation; __le64 generation;
__le64 owner; __le64 owner;
__le16 nritems; __le32 nritems;
__le16 flags; __le16 flags;
u8 level; u8 level;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
#define BTRFS_MAX_LEVEL 8 #define BTRFS_MAX_LEVEL 8
#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \ #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
sizeof(struct btrfs_header)) / \ sizeof(struct btrfs_header)) / \
(sizeof(struct btrfs_disk_key) + sizeof(u64))) (sizeof(struct btrfs_disk_key) + sizeof(u64)))
#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header)) #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize)) #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
#define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \ #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
sizeof(struct btrfs_item) - \ sizeof(struct btrfs_item) - \
sizeof(struct btrfs_file_extent_item)) sizeof(struct btrfs_file_extent_item))
struct buffer_head;
/* /*
* the super block basically lists the main trees of the FS * the super block basically lists the main trees of the FS
* it currently lacks any block count etc etc * it currently lacks any block count etc etc
...@@ -128,7 +128,9 @@ struct btrfs_super_block { ...@@ -128,7 +128,9 @@ struct btrfs_super_block {
__le64 total_blocks; __le64 total_blocks;
__le64 blocks_used; __le64 blocks_used;
__le64 root_dir_objectid; __le64 root_dir_objectid;
__le32 blocksize; __le32 sectorsize;
__le32 nodesize;
__le32 leafsize;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
/* /*
...@@ -138,7 +140,7 @@ struct btrfs_super_block { ...@@ -138,7 +140,7 @@ struct btrfs_super_block {
struct btrfs_item { struct btrfs_item {
struct btrfs_disk_key key; struct btrfs_disk_key key;
__le32 offset; __le32 offset;
__le16 size; __le32 size;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
/* /*
...@@ -176,7 +178,7 @@ struct btrfs_node { ...@@ -176,7 +178,7 @@ struct btrfs_node {
* used while walking the tree. * used while walking the tree.
*/ */
struct btrfs_path { struct btrfs_path {
struct buffer_head *nodes[BTRFS_MAX_LEVEL]; struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
int slots[BTRFS_MAX_LEVEL]; int slots[BTRFS_MAX_LEVEL];
int reada; int reada;
int lowest_level; int lowest_level;
...@@ -292,6 +294,7 @@ struct btrfs_block_group_cache { ...@@ -292,6 +294,7 @@ struct btrfs_block_group_cache {
}; };
struct btrfs_fs_info { struct btrfs_fs_info {
u8 fsid[BTRFS_FSID_SIZE];
struct btrfs_root *extent_root; struct btrfs_root *extent_root;
struct btrfs_root *tree_root; struct btrfs_root *tree_root;
struct radix_tree_root fs_roots_radix; struct radix_tree_root fs_roots_radix;
...@@ -304,9 +307,8 @@ struct btrfs_fs_info { ...@@ -304,9 +307,8 @@ struct btrfs_fs_info {
u64 generation; u64 generation;
u64 last_trans_committed; u64 last_trans_committed;
struct btrfs_transaction *running_transaction; struct btrfs_transaction *running_transaction;
struct btrfs_super_block *disk_super;
struct btrfs_super_block super_copy; struct btrfs_super_block super_copy;
struct buffer_head *sb_buffer; struct extent_buffer *sb_buffer;
struct super_block *sb; struct super_block *sb;
struct inode *btree_inode; struct inode *btree_inode;
struct mutex trans_mutex; struct mutex trans_mutex;
...@@ -325,8 +327,8 @@ struct btrfs_fs_info { ...@@ -325,8 +327,8 @@ struct btrfs_fs_info {
* and for the extent tree extent_root root. * and for the extent tree extent_root root.
*/ */
struct btrfs_root { struct btrfs_root {
struct buffer_head *node; struct extent_buffer *node;
struct buffer_head *commit_root; struct extent_buffer *commit_root;
struct btrfs_root_item root_item; struct btrfs_root_item root_item;
struct btrfs_key root_key; struct btrfs_key root_key;
struct btrfs_fs_info *fs_info; struct btrfs_fs_info *fs_info;
...@@ -336,7 +338,16 @@ struct btrfs_root { ...@@ -336,7 +338,16 @@ struct btrfs_root {
struct rw_semaphore snap_sem; struct rw_semaphore snap_sem;
u64 objectid; u64 objectid;
u64 last_trans; u64 last_trans;
u32 blocksize;
/* data allocations are done in sectorsize units */
u32 sectorsize;
/* node allocations are done in nodesize units */
u32 nodesize;
/* leaf allocations are done in leafsize units */
u32 leafsize;
u32 type; u32 type;
u64 highest_inode; u64 highest_inode;
u64 last_inode_alloc; u64 last_inode_alloc;
...@@ -347,12 +358,6 @@ struct btrfs_root { ...@@ -347,12 +358,6 @@ struct btrfs_root {
char *name; char *name;
}; };
/* the lower bits in the key flags defines the item type */
#define BTRFS_KEY_TYPE_MAX 256
#define BTRFS_KEY_TYPE_SHIFT 24
#define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
BTRFS_KEY_TYPE_SHIFT)
/* /*
* inode items have the data typically returned from stat and store other * inode items have the data typically returned from stat and store other
* info about object characteristics. There is one for every file and dir in * info about object characteristics. There is one for every file and dir in
...@@ -402,246 +407,253 @@ struct btrfs_root { ...@@ -402,246 +407,253 @@ struct btrfs_root {
*/ */
#define BTRFS_STRING_ITEM_KEY 253 #define BTRFS_STRING_ITEM_KEY 253
/* some macros to generate set/get funcs for the struct fields. This
* assumes there is a lefoo_to_cpu for every type, so lets make a simple
* one for u8:
*/
#define le8_to_cpu(v) (v)
#define cpu_to_le8(v) (v)
#define __le8 u8
#define read_eb_member(eb, ptr, type, member, result) ( \
read_extent_buffer(eb, (char *)(result), \
((unsigned long)(ptr)) + \
offsetof(type, member), \
sizeof(((type *)0)->member)))
#define write_eb_member(eb, ptr, type, member, result) ( \
write_extent_buffer(eb, (char *)(result), \
((unsigned long)(ptr)) + \
offsetof(type, member), \
sizeof(((type *)0)->member)))
#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
static inline u##bits btrfs_##name(struct extent_buffer *eb, \
type *s) \
{ \
__le##bits res; \
read_eb_member(eb, s, type, member, &res); \
return le##bits##_to_cpu(res); \
} \
static inline void btrfs_set_##name(struct extent_buffer *eb, \
type *s, u##bits val) \
{ \
val = cpu_to_le##bits(val); \
write_eb_member(eb, s, type, member, &val); \
}
#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
static inline u##bits btrfs_##name(struct extent_buffer *eb) \
{ \
__le##bits res; \
read_eb_member(eb, NULL, type, member, &res); \
return le##bits##_to_cpu(res); \
} \
static inline void btrfs_set_##name(struct extent_buffer *eb, \
u##bits val) \
{ \
val = cpu_to_le##bits(val); \
write_eb_member(eb, NULL, type, member, &val); \
}
static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi) #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
{ static inline u##bits btrfs_##name(type *s) \
return le64_to_cpu(bi->used); { \
} return le##bits##_to_cpu(s->member); \
} \
static inline void btrfs_set_block_group_used(struct static inline void btrfs_set_##name(type *s, u##bits val) \
btrfs_block_group_item *bi, { \
u64 val) s->member = cpu_to_le##bits(val); \
{
bi->used = cpu_to_le64(val);
}
static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
{
return le64_to_cpu(i->generation);
}
static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
u64 val)
{
i->generation = cpu_to_le64(val);
}
static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
{
return le64_to_cpu(i->size);
}
static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
{
i->size = cpu_to_le64(val);
}
static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
{
return le64_to_cpu(i->nblocks);
}
static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
{
i->nblocks = cpu_to_le64(val);
}
static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
{
return le64_to_cpu(i->block_group);
}
static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
u64 val)
{
i->block_group = cpu_to_le64(val);
}
static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
{
return le32_to_cpu(i->nlink);
}
static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
{
i->nlink = cpu_to_le32(val);
}
static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
{
return le32_to_cpu(i->uid);
}
static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
{
i->uid = cpu_to_le32(val);
}
static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
{
return le32_to_cpu(i->gid);
}
static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
{
i->gid = cpu_to_le32(val);
}
static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
{
return le32_to_cpu(i->mode);
}
static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
{
i->mode = cpu_to_le32(val);
} }
static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i) /* struct btrfs_block_group_item */
{ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
return le32_to_cpu(i->rdev); used, 64);
} BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
used, 64);
static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val) /* struct btrfs_inode_item */
{ BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
i->rdev = cpu_to_le32(val); BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
} BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32);
BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
compat_flags, 16);
static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i) static inline struct btrfs_inode_timespec *
btrfs_inode_atime(struct btrfs_inode_item *inode_item)
{ {
return le16_to_cpu(i->flags); unsigned long ptr = (unsigned long)inode_item;
ptr += offsetof(struct btrfs_inode_item, atime);
return (struct btrfs_inode_timespec *)ptr;
} }
static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val) static inline struct btrfs_inode_timespec *
btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
{ {
i->flags = cpu_to_le16(val); unsigned long ptr = (unsigned long)inode_item;
ptr += offsetof(struct btrfs_inode_item, mtime);
return (struct btrfs_inode_timespec *)ptr;
} }
static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i) static inline struct btrfs_inode_timespec *
btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
{ {
return le16_to_cpu(i->compat_flags); unsigned long ptr = (unsigned long)inode_item;
ptr += offsetof(struct btrfs_inode_item, ctime);
return (struct btrfs_inode_timespec *)ptr;
} }
static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i, static inline struct btrfs_inode_timespec *
u16 val) btrfs_inode_otime(struct btrfs_inode_item *inode_item)
{ {
i->compat_flags = cpu_to_le16(val); unsigned long ptr = (unsigned long)inode_item;
ptr += offsetof(struct btrfs_inode_item, otime);
return (struct btrfs_inode_timespec *)ptr;
} }
static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts) BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_inode_timespec, sec, 64);
{ BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_inode_timespec, nsec, 32);
return le64_to_cpu(ts->sec);
}
static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts, /* struct btrfs_extent_item */
u64 val) BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
{ BTRFS_SETGET_FUNCS(extent_owner, struct btrfs_extent_item, owner, 32);
ts->sec = cpu_to_le64(val);
}
static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts) BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
{ refs, 32);
return le32_to_cpu(ts->nsec); BTRFS_SETGET_STACK_FUNCS(stack_extent_owner, struct btrfs_extent_item,
} owner, 32);
static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts, /* struct btrfs_node */
u32 val) BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
{
ts->nsec = cpu_to_le32(val);
}
static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei) static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
{ {
return le32_to_cpu(ei->refs); unsigned long ptr;
ptr = offsetof(struct btrfs_node, ptrs) +
sizeof(struct btrfs_key_ptr) * nr;
return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
} }
static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val) static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
int nr, u64 val)
{ {
ei->refs = cpu_to_le32(val); unsigned long ptr;
ptr = offsetof(struct btrfs_node, ptrs) +
sizeof(struct btrfs_key_ptr) * nr;
btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
} }
static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei) static unsigned long btrfs_node_key_ptr_offset(int nr)
{ {
return le64_to_cpu(ei->owner); return offsetof(struct btrfs_node, ptrs) +
sizeof(struct btrfs_key_ptr) * nr;
} }
static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val) static void btrfs_node_key(struct extent_buffer *eb,
struct btrfs_disk_key *disk_key, int nr)
{ {
ei->owner = cpu_to_le64(val); unsigned long ptr;
ptr = btrfs_node_key_ptr_offset(nr);
read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
struct btrfs_key_ptr, key, disk_key);
} }
static inline void btrfs_set_node_key(struct extent_buffer *eb,
static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr) struct btrfs_disk_key *disk_key, int nr)
{ {
return le64_to_cpu(n->ptrs[nr].blockptr); unsigned long ptr;
ptr = btrfs_node_key_ptr_offset(nr);
write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
struct btrfs_key_ptr, key, disk_key);
} }
/* struct btrfs_item */
BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr, static inline unsigned long btrfs_item_nr_offset(int nr)
u64 val)
{ {
n->ptrs[nr].blockptr = cpu_to_le64(val); return offsetof(struct btrfs_leaf, items) +
sizeof(struct btrfs_item) * nr;
} }
static inline u32 btrfs_item_offset(struct btrfs_item *item) static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
int nr)
{ {
return le32_to_cpu(item->offset); return (struct btrfs_item *)btrfs_item_nr_offset(nr);
} }
static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val) static inline u32 btrfs_item_end(struct extent_buffer *eb,
struct btrfs_item *item)
{ {
item->offset = cpu_to_le32(val); return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
} }
static inline u32 btrfs_item_end(struct btrfs_item *item) static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
{ {
return le32_to_cpu(item->offset) + le16_to_cpu(item->size); return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
} }
static inline u16 btrfs_item_size(struct btrfs_item *item) static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
{ {
return le16_to_cpu(item->size); return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
} }
static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val) static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
{ {
item->size = cpu_to_le16(val); return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
} }
static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d) static inline void btrfs_item_key(struct extent_buffer *eb,
struct btrfs_disk_key *disk_key, int nr)
{ {
return le16_to_cpu(d->flags); struct btrfs_item *item = btrfs_item_nr(eb, nr);
read_eb_member(eb, item, struct btrfs_item, key, disk_key);
} }
static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val) static inline void btrfs_set_item_key(struct extent_buffer *eb,
struct btrfs_disk_key *disk_key, int nr)
{ {
d->flags = cpu_to_le16(val); struct btrfs_item *item = btrfs_item_nr(eb, nr);
write_eb_member(eb, item, struct btrfs_item, key, disk_key);
} }
static inline u8 btrfs_dir_type(struct btrfs_dir_item *d) /* struct btrfs_dir_item */
{ BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, flags, 16);
return d->type; BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
} BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val) static inline void btrfs_dir_item_key(struct extent_buffer *eb,
struct btrfs_dir_item *item,
struct btrfs_disk_key *key)
{ {
d->type = val; read_eb_member(eb, item, struct btrfs_dir_item, location, key);
} }
static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d) static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
struct btrfs_dir_item *item,
struct btrfs_disk_key *key)
{ {
return le16_to_cpu(d->name_len); write_eb_member(eb, item, struct btrfs_dir_item, location, key);
} }
static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val) /* struct btrfs_disk_key */
{ BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
d->name_len = cpu_to_le16(val); objectid, 64);
} BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
struct btrfs_disk_key *disk) struct btrfs_disk_key *disk)
{ {
cpu->offset = le64_to_cpu(disk->offset); cpu->offset = le64_to_cpu(disk->offset);
cpu->flags = le32_to_cpu(disk->flags); cpu->type = disk->type;
cpu->objectid = le64_to_cpu(disk->objectid); cpu->objectid = le64_to_cpu(disk->objectid);
} }
...@@ -649,400 +661,167 @@ static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, ...@@ -649,400 +661,167 @@ static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
struct btrfs_key *cpu) struct btrfs_key *cpu)
{ {
disk->offset = cpu_to_le64(cpu->offset); disk->offset = cpu_to_le64(cpu->offset);
disk->flags = cpu_to_le32(cpu->flags); disk->type = cpu->type;
disk->objectid = cpu_to_le64(cpu->objectid); disk->objectid = cpu_to_le64(cpu->objectid);
} }
static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk) static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
{ struct btrfs_key *key, int nr)
return le64_to_cpu(disk->objectid);
}
static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
u64 val)
{
disk->objectid = cpu_to_le64(val);
}
static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
{
return le64_to_cpu(disk->offset);
}
static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
u64 val)
{
disk->offset = cpu_to_le64(val);
}
static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
{
return le32_to_cpu(disk->flags);
}
static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
u32 val)
{
disk->flags = cpu_to_le32(val);
}
static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
{
return le32_to_cpu(key->flags) >> BTRFS_KEY_TYPE_SHIFT;
}
static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key,
u32 val)
{
u32 flags = btrfs_disk_key_flags(key);
BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
val = val << BTRFS_KEY_TYPE_SHIFT;
flags = (flags & ~BTRFS_KEY_TYPE_MASK) | val;
btrfs_set_disk_key_flags(key, flags);
}
static inline u32 btrfs_key_type(struct btrfs_key *key)
{
return key->flags >> BTRFS_KEY_TYPE_SHIFT;
}
static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
{
BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
val = val << BTRFS_KEY_TYPE_SHIFT;
key->flags = (key->flags & ~(BTRFS_KEY_TYPE_MASK)) | val;
}
static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
{
return le64_to_cpu(h->blocknr);
}
static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
{
h->blocknr = cpu_to_le64(blocknr);
}
static inline u64 btrfs_header_generation(struct btrfs_header *h)
{ {
return le64_to_cpu(h->generation); struct btrfs_disk_key disk_key;
btrfs_node_key(eb, &disk_key, nr);
btrfs_disk_key_to_cpu(key, &disk_key);
} }
static inline void btrfs_set_header_generation(struct btrfs_header *h, static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
u64 val) struct btrfs_key *key, int nr)
{ {
h->generation = cpu_to_le64(val); struct btrfs_disk_key disk_key;
btrfs_item_key(eb, &disk_key, nr);
btrfs_disk_key_to_cpu(key, &disk_key);
} }
static inline u64 btrfs_header_owner(struct btrfs_header *h) static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
struct btrfs_dir_item *item,
struct btrfs_key *key)
{ {
return le64_to_cpu(h->owner); struct btrfs_disk_key disk_key;
btrfs_dir_item_key(eb, item, &disk_key);
btrfs_disk_key_to_cpu(key, &disk_key);
} }
static inline void btrfs_set_header_owner(struct btrfs_header *h,
u64 val)
{
h->owner = cpu_to_le64(val);
}
static inline u16 btrfs_header_nritems(struct btrfs_header *h)
{
return le16_to_cpu(h->nritems);
}
static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
{
h->nritems = cpu_to_le16(val);
}
static inline u16 btrfs_header_flags(struct btrfs_header *h)
{
return le16_to_cpu(h->flags);
}
static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
{
h->flags = cpu_to_le16(val);
}
static inline int btrfs_header_level(struct btrfs_header *h)
{
return h->level;
}
static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
{
BUG_ON(level > BTRFS_MAX_LEVEL);
h->level = level;
}
static inline int btrfs_is_leaf(struct btrfs_node *n)
{
return (btrfs_header_level(&n->header) == 0);
}
static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
{
return le64_to_cpu(item->blocknr);
}
static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
{
item->blocknr = cpu_to_le64(val);
}
static inline u64 btrfs_root_dirid(struct btrfs_root_item *item) static inline u8 btrfs_key_type(struct btrfs_key *key)
{ {
return le64_to_cpu(item->root_dirid); return key->type;
} }
static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val) static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
{ {
item->root_dirid = cpu_to_le64(val); key->type = val;
} }
static inline u32 btrfs_root_refs(struct btrfs_root_item *item) /* struct btrfs_header */
{ BTRFS_SETGET_HEADER_FUNCS(header_blocknr, struct btrfs_header, blocknr, 64);
return le32_to_cpu(item->refs); BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
} generation, 64);
BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 16);
BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val) static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
{ {
item->refs = cpu_to_le32(val); unsigned long ptr = offsetof(struct btrfs_header, fsid);
return (u8 *)ptr;
} }
static inline u32 btrfs_root_flags(struct btrfs_root_item *item) static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
{ {
return le32_to_cpu(item->flags); unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
return (u8 *)ptr;
} }
static inline void btrfs_set_root_flags(struct btrfs_root_item *item, u32 val) static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
{ {
item->flags = cpu_to_le32(val); unsigned long ptr = offsetof(struct btrfs_header, csum);
return (u8 *)ptr;
} }
static inline void btrfs_set_root_blocks_used(struct btrfs_root_item *item, static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
u64 val)
{ {
item->blocks_used = cpu_to_le64(val); return NULL;
} }
static inline u64 btrfs_root_blocks_used(struct btrfs_root_item *item) static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
{ {
return le64_to_cpu(item->blocks_used); return NULL;
} }
static inline void btrfs_set_root_block_limit(struct btrfs_root_item *item, static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
u64 val)
{ {
item->block_limit = cpu_to_le64(val); return NULL;
} }
static inline u64 btrfs_root_block_limit(struct btrfs_root_item *item) static inline int btrfs_is_leaf(struct extent_buffer *eb)
{ {
return le64_to_cpu(item->block_limit); return (btrfs_header_level(eb) == 0);
} }
static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s) /* struct btrfs_root_item */
{ BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
return le64_to_cpu(s->blocknr); BTRFS_SETGET_FUNCS(disk_root_blocknr, struct btrfs_root_item, blocknr, 64);
}
static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val) BTRFS_SETGET_STACK_FUNCS(root_blocknr, struct btrfs_root_item, blocknr, 64);
{ BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
s->blocknr = cpu_to_le64(val); BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
} BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, blocks_used, 64);
BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, block_limit, 64);
static inline u64 btrfs_super_generation(struct btrfs_super_block *s) /* struct btrfs_super_block */
{ BTRFS_SETGET_STACK_FUNCS(super_blocknr, struct btrfs_super_block, blocknr, 64);
return le64_to_cpu(s->generation); BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
} generation, 64);
BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
BTRFS_SETGET_STACK_FUNCS(super_total_blocks, struct btrfs_super_block,
total_blocks, 64);
BTRFS_SETGET_STACK_FUNCS(super_blocks_used, struct btrfs_super_block,
blocks_used, 64);
BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
sectorsize, 32);
BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
nodesize, 32);
BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
leafsize, 32);
BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
root_dir_objectid, 64);
static inline void btrfs_set_super_generation(struct btrfs_super_block *s, static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
u64 val)
{ {
s->generation = cpu_to_le64(val); return offsetof(struct btrfs_leaf, items);
} }
static inline u64 btrfs_super_root(struct btrfs_super_block *s) /* struct btrfs_file_extent_item */
{ BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
return le64_to_cpu(s->root);
}
static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val) static inline unsigned long btrfs_file_extent_inline_start(struct
{
s->root = cpu_to_le64(val);
}
static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
{
return le64_to_cpu(s->total_blocks);
}
static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
u64 val)
{
s->total_blocks = cpu_to_le64(val);
}
static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
{
return le64_to_cpu(s->blocks_used);
}
static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
u64 val)
{
s->blocks_used = cpu_to_le64(val);
}
static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
{
return le32_to_cpu(s->blocksize);
}
static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
u32 val)
{
s->blocksize = cpu_to_le32(val);
}
static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
{
return le64_to_cpu(s->root_dir_objectid);
}
static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
val)
{
s->root_dir_objectid = cpu_to_le64(val);
}
static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
{
return (u8 *)l->items;
}
static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
{
return e->type;
}
static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
u8 val)
{
e->type = val;
}
static inline char *btrfs_file_extent_inline_start(struct
btrfs_file_extent_item *e) btrfs_file_extent_item *e)
{ {
return (char *)(&e->disk_blocknr); unsigned long offset = (unsigned long)e;
offset += offsetof(struct btrfs_file_extent_item, disk_blocknr);
return offset;
} }
static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize) static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
{ {
return (unsigned long)(&((struct return offsetof(struct btrfs_file_extent_item, disk_blocknr) + datasize;
btrfs_file_extent_item *)NULL)->disk_blocknr) + datasize;
}
static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
{
struct btrfs_file_extent_item *fe = NULL;
return btrfs_item_size(e) - (unsigned long)(&fe->disk_blocknr);
}
static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
*e)
{
return le64_to_cpu(e->disk_blocknr);
} }
static inline void btrfs_set_file_extent_disk_blocknr(struct static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
btrfs_file_extent_item struct btrfs_item *e)
*e, u64 val)
{ {
e->disk_blocknr = cpu_to_le64(val); unsigned long offset;
offset = offsetof(struct btrfs_file_extent_item, disk_blocknr);
return btrfs_item_size(eb, e) - offset;
} }
static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e) BTRFS_SETGET_FUNCS(file_extent_disk_blocknr, struct btrfs_file_extent_item,
{ disk_blocknr, 64);
return le64_to_cpu(e->generation); BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
} generation, 64);
BTRFS_SETGET_FUNCS(file_extent_disk_num_blocks, struct btrfs_file_extent_item,
static inline void btrfs_set_file_extent_generation(struct disk_num_blocks, 64);
btrfs_file_extent_item *e, BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
u64 val) offset, 64);
{ BTRFS_SETGET_FUNCS(file_extent_num_blocks, struct btrfs_file_extent_item,
e->generation = cpu_to_le64(val); num_blocks, 64);
}
static inline u64 btrfs_file_extent_disk_num_blocks(struct
btrfs_file_extent_item *e)
{
return le64_to_cpu(e->disk_num_blocks);
}
static inline void btrfs_set_file_extent_disk_num_blocks(struct
btrfs_file_extent_item
*e, u64 val)
{
e->disk_num_blocks = cpu_to_le64(val);
}
static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
{
return le64_to_cpu(e->offset);
}
static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
*e, u64 val)
{
e->offset = cpu_to_le64(val);
}
static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
*e)
{
return le64_to_cpu(e->num_blocks);
}
static inline void btrfs_set_file_extent_num_blocks(struct
btrfs_file_extent_item *e,
u64 val)
{
e->num_blocks = cpu_to_le64(val);
}
static inline struct btrfs_root *btrfs_sb(struct super_block *sb) static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
{ {
return sb->s_fs_info; return sb->s_fs_info;
} }
static inline void btrfs_check_bounds(void *vptr, size_t len,
void *vcontainer, size_t container_len)
{
char *ptr = vptr;
char *container = vcontainer;
WARN_ON(ptr < container);
WARN_ON(ptr + len > container + container_len);
}
static inline void btrfs_memcpy(struct btrfs_root *root,
void *dst_block,
void *dst, const void *src, size_t nr)
{
btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
memcpy(dst, src, nr);
}
static inline void btrfs_memmove(struct btrfs_root *root,
void *dst_block,
void *dst, void *src, size_t nr)
{
btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
memmove(dst, src, nr);
}
static inline int btrfs_set_root_name(struct btrfs_root *root, static inline int btrfs_set_root_name(struct btrfs_root *root,
const char *name, int len) const char *name, int len)
{ {
...@@ -1063,7 +842,11 @@ static inline int btrfs_set_root_name(struct btrfs_root *root, ...@@ -1063,7 +842,11 @@ static inline int btrfs_set_root_name(struct btrfs_root *root,
/* helper function to cast into the data area of the leaf. */ /* helper function to cast into the data area of the leaf. */
#define btrfs_item_ptr(leaf, slot, type) \ #define btrfs_item_ptr(leaf, slot, type) \
((type *)(btrfs_leaf_data(leaf) + \ ((type *)(btrfs_leaf_data(leaf) + \
btrfs_item_offset((leaf)->items + (slot)))) btrfs_item_offset_nr(leaf, slot)))
#define btrfs_item_ptr_offset(leaf, slot) \
((unsigned long)(btrfs_leaf_data(leaf) + \
btrfs_item_offset_nr(leaf, slot)))
/* mount option defines and helpers */ /* mount option defines and helpers */
#define BTRFS_MOUNT_SUBVOL 0x000001 #define BTRFS_MOUNT_SUBVOL 0x000001
...@@ -1084,7 +867,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, ...@@ -1084,7 +867,7 @@ struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
int data, int owner); int data, int owner);
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root); struct btrfs_root *root);
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 hint, struct btrfs_root *root, u64 hint,
u64 empty_size); u64 empty_size);
int btrfs_alloc_extent(struct btrfs_trans_handle *trans, int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
...@@ -1092,7 +875,7 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans, ...@@ -1092,7 +875,7 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
u64 num_blocks, u64 empty_size, u64 search_start, u64 num_blocks, u64 empty_size, u64 search_start,
u64 search_end, struct btrfs_key *ins, int data); u64 search_end, struct btrfs_key *ins, int data);
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf); struct extent_buffer *buf);
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 blocknr, u64 num_blocks, int pin); *root, u64 blocknr, u64 num_blocks, int pin);
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
...@@ -1106,10 +889,10 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, ...@@ -1106,10 +889,10 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
int btrfs_free_block_groups(struct btrfs_fs_info *info); int btrfs_free_block_groups(struct btrfs_fs_info *info);
int btrfs_read_block_groups(struct btrfs_root *root); int btrfs_read_block_groups(struct btrfs_root *root);
/* ctree.c */ /* ctree.c */
int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_cow_block(struct btrfs_trans_handle *trans,
*root, struct buffer_head *buf, struct buffer_head struct btrfs_root *root, struct extent_buffer *buf,
*parent, int parent_slot, struct buffer_head struct extent_buffer *parent, int parent_slot,
**cow_ret); struct extent_buffer **cow_ret);
int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, u32 data_size); *root, struct btrfs_path *path, u32 data_size);
int btrfs_truncate_item(struct btrfs_trans_handle *trans, int btrfs_truncate_item(struct btrfs_trans_handle *trans,
...@@ -1120,7 +903,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1120,7 +903,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_path *p, int *root, struct btrfs_key *key, struct btrfs_path *p, int
ins_len, int cow); ins_len, int cow);
int btrfs_realloc_node(struct btrfs_trans_handle *trans, int btrfs_realloc_node(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct buffer_head *parent, struct btrfs_root *root, struct extent_buffer *parent,
int cache_only, u64 *last_ret); int cache_only, u64 *last_ret);
void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
struct btrfs_path *btrfs_alloc_path(void); struct btrfs_path *btrfs_alloc_path(void);
...@@ -1134,7 +917,7 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1134,7 +917,7 @@ int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, struct btrfs_key *root, struct btrfs_path *path, struct btrfs_key
*cpu_key, u32 data_size); *cpu_key, u32 data_size);
int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf); int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
*root); *root);
/* root-item.c */ /* root-item.c */
...@@ -1179,9 +962,9 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, ...@@ -1179,9 +962,9 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid); int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
/* inode-item.c */ /* inode-item.c */
int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
*root, u64 objectid, struct btrfs_inode_item struct btrfs_root *root,
*inode_item); struct btrfs_path *path, u64 objectid);
int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, *root, struct btrfs_path *path,
struct btrfs_key *location, int mod); struct btrfs_key *location, int mod);
...@@ -1224,8 +1007,6 @@ struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, ...@@ -1224,8 +1007,6 @@ struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
struct btrfs_root *root); struct btrfs_root *root);
int btrfs_commit_write(struct file *file, struct page *page, int btrfs_commit_write(struct file *file, struct page *page,
unsigned from, unsigned to); unsigned from, unsigned to);
int btrfs_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *result, int create);
struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
size_t page_offset, u64 start, u64 end, size_t page_offset, u64 start, u64 end,
int create); int create);
......
...@@ -33,7 +33,7 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle ...@@ -33,7 +33,7 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
int ret; int ret;
char *ptr; char *ptr;
struct btrfs_item *item; struct btrfs_item *item;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
if (ret == -EEXIST) { if (ret == -EEXIST) {
...@@ -49,11 +49,11 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle ...@@ -49,11 +49,11 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
if (ret < 0) if (ret < 0)
return ERR_PTR(ret); return ERR_PTR(ret);
WARN_ON(ret > 0); WARN_ON(ret > 0);
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
item = leaf->items + path->slots[0]; item = btrfs_item_nr(leaf, path->slots[0]);
ptr = btrfs_item_ptr(leaf, path->slots[0], char); ptr = btrfs_item_ptr(leaf, path->slots[0], char);
BUG_ON(data_size > btrfs_item_size(item)); BUG_ON(data_size > btrfs_item_size(leaf, item));
ptr += btrfs_item_size(item) - data_size; ptr += btrfs_item_size(leaf, item) - data_size;
return (struct btrfs_dir_item *)ptr; return (struct btrfs_dir_item *)ptr;
} }
...@@ -65,12 +65,13 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -65,12 +65,13 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
int ret2 = 0; int ret2 = 0;
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_dir_item *dir_item; struct btrfs_dir_item *dir_item;
char *name_ptr; struct extent_buffer *leaf;
unsigned long name_ptr;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_disk_key disk_key;
u32 data_size; u32 data_size;
key.objectid = dir; key.objectid = dir;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
ret = btrfs_name_hash(name, name_len, &key.offset); ret = btrfs_name_hash(name, name_len, &key.offset);
BUG_ON(ret); BUG_ON(ret);
...@@ -85,14 +86,16 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -85,14 +86,16 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
goto out; goto out;
} }
btrfs_cpu_key_to_disk(&dir_item->location, location); leaf = path->nodes[0];
btrfs_set_dir_type(dir_item, type); btrfs_cpu_key_to_disk(&disk_key, location);
btrfs_set_dir_flags(dir_item, 0); btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
btrfs_set_dir_name_len(dir_item, name_len); btrfs_set_dir_type(leaf, dir_item, type);
name_ptr = (char *)(dir_item + 1); btrfs_set_dir_flags(leaf, dir_item, 0);
btrfs_set_dir_name_len(leaf, dir_item, name_len);
name_ptr = (unsigned long)(dir_item + 1);
btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); write_extent_buffer(leaf, name, name_ptr, name_len);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
second_insert: second_insert:
/* FIXME, use some real flag for selecting the extra index */ /* FIXME, use some real flag for selecting the extra index */
...@@ -110,13 +113,15 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -110,13 +113,15 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
ret2 = PTR_ERR(dir_item); ret2 = PTR_ERR(dir_item);
goto out; goto out;
} }
btrfs_cpu_key_to_disk(&dir_item->location, location); leaf = path->nodes[0];
btrfs_set_dir_type(dir_item, type); btrfs_cpu_key_to_disk(&disk_key, location);
btrfs_set_dir_flags(dir_item, 0); btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
btrfs_set_dir_name_len(dir_item, name_len); btrfs_set_dir_type(leaf, dir_item, type);
name_ptr = (char *)(dir_item + 1); btrfs_set_dir_flags(leaf, dir_item, 0);
btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); btrfs_set_dir_name_len(leaf, dir_item, name_len);
btrfs_mark_buffer_dirty(path->nodes[0]); name_ptr = (unsigned long)(dir_item + 1);
write_extent_buffer(leaf, name, name_ptr, name_len);
btrfs_mark_buffer_dirty(leaf);
out: out:
btrfs_free_path(path); btrfs_free_path(path);
if (ret) if (ret)
...@@ -136,14 +141,15 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, ...@@ -136,14 +141,15 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
struct btrfs_key key; struct btrfs_key key;
int ins_len = mod < 0 ? -1 : 0; int ins_len = mod < 0 ? -1 : 0;
int cow = mod != 0; int cow = mod != 0;
struct btrfs_disk_key *found_key; struct btrfs_key found_key;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
key.objectid = dir; key.objectid = dir;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
ret = btrfs_name_hash(name, name_len, &key.offset); ret = btrfs_name_hash(name, name_len, &key.offset);
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
if (ret < 0) if (ret < 0)
return ERR_PTR(ret); return ERR_PTR(ret);
...@@ -152,12 +158,13 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, ...@@ -152,12 +158,13 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
return NULL; return NULL;
path->slots[0]--; path->slots[0]--;
} }
leaf = btrfs_buffer_leaf(path->nodes[0]);
found_key = &leaf->items[path->slots[0]].key;
if (btrfs_disk_key_objectid(found_key) != dir || leaf = path->nodes[0];
btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
btrfs_disk_key_offset(found_key) != key.offset)
if (found_key.objectid != dir ||
btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
found_key.offset != key.offset)
return NULL; return NULL;
return btrfs_match_dir_item_name(root, path, name, name_len); return btrfs_match_dir_item_name(root, path, name, name_len);
...@@ -176,7 +183,6 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, ...@@ -176,7 +183,6 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
int cow = mod != 0; int cow = mod != 0;
key.objectid = dir; key.objectid = dir;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
key.offset = objectid; key.offset = objectid;
...@@ -193,21 +199,22 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, ...@@ -193,21 +199,22 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
const char *name, int name_len) const char *name, int name_len)
{ {
struct btrfs_dir_item *dir_item; struct btrfs_dir_item *dir_item;
char *name_ptr; unsigned long name_ptr;
u32 total_len; u32 total_len;
u32 cur = 0; u32 cur = 0;
u32 this_len; u32 this_len;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
total_len = btrfs_item_size(leaf->items + path->slots[0]); total_len = btrfs_item_size_nr(leaf, path->slots[0]);
while(cur < total_len) { while(cur < total_len) {
this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item); this_len = sizeof(*dir_item) +
name_ptr = (char *)(dir_item + 1); btrfs_dir_name_len(leaf, dir_item);
name_ptr = (unsigned long)(dir_item + 1);
if (btrfs_dir_name_len(dir_item) == name_len && if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
memcmp(name_ptr, name, name_len) == 0) memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
return dir_item; return dir_item;
cur += this_len; cur += this_len;
...@@ -223,20 +230,23 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, ...@@ -223,20 +230,23 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
struct btrfs_dir_item *di) struct btrfs_dir_item *di)
{ {
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
u32 sub_item_len; u32 sub_item_len;
u32 item_len; u32 item_len;
int ret = 0; int ret = 0;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
sub_item_len = sizeof(*di) + btrfs_dir_name_len(di); sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di);
item_len = btrfs_item_size(leaf->items + path->slots[0]); item_len = btrfs_item_size_nr(leaf, path->slots[0]);
if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) { if (sub_item_len == item_len) {
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
} else { } else {
char *ptr = (char *)di; /* MARKER */
char *start = btrfs_item_ptr(leaf, path->slots[0], char); unsigned long ptr = (unsigned long)di;
btrfs_memmove(root, leaf, ptr, ptr + sub_item_len, unsigned long start;
start = btrfs_item_ptr_offset(leaf, path->slots[0]);
memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
item_len - (ptr + sub_item_len - start)); item_len - (ptr + sub_item_len - start));
ret = btrfs_truncate_item(trans, root, path, ret = btrfs_truncate_item(trans, root, path,
item_len - sub_item_len); item_len - sub_item_len);
......
...@@ -23,138 +23,132 @@ ...@@ -23,138 +23,132 @@
#include <linux/swap.h> #include <linux/swap.h>
#include <linux/radix-tree.h> #include <linux/radix-tree.h>
#include <linux/writeback.h> #include <linux/writeback.h>
#include <linux/buffer_head.h> // for block_sync_page
#include "ctree.h" #include "ctree.h"
#include "disk-io.h" #include "disk-io.h"
#include "transaction.h" #include "transaction.h"
#include "btrfs_inode.h" #include "btrfs_inode.h"
u64 bh_blocknr(struct buffer_head *bh) #if 0
static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
{ {
return bh->b_blocknr; if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
} printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
(unsigned long long)extent_buffer_blocknr(buf),
static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) (unsigned long long)btrfs_header_blocknr(buf));
{
struct btrfs_node *node = btrfs_buffer_node(buf);
if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
(unsigned long long)bh_blocknr(buf),
(unsigned long long)btrfs_header_blocknr(&node->header));
return 1; return 1;
} }
return 0; return 0;
} }
#endif
struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
u64 blocknr)
{ {
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; struct inode *btree_inode = root->fs_info->btree_inode;
int blockbits = root->fs_info->sb->s_blocksize_bits; return find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); blocknr * root->sectorsize,
struct page *page; root->sectorsize, GFP_NOFS);
struct buffer_head *bh; }
struct buffer_head *head;
struct buffer_head *ret = NULL;
page = find_lock_page(mapping, index); struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
if (!page) u64 blocknr)
return NULL; {
struct inode *btree_inode = root->fs_info->btree_inode;
return alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
blocknr * root->sectorsize,
root->sectorsize, GFP_NOFS);
}
if (!page_has_buffers(page)) struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
goto out_unlock; size_t page_offset, u64 start, u64 end,
int create)
{
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
struct extent_map *em;
int ret;
head = page_buffers(page); again:
bh = head; em = lookup_extent_mapping(em_tree, start, end);
do { if (em) {
if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { goto out;
ret = bh;
get_bh(bh);
goto out_unlock;
} }
bh = bh->b_this_page; em = alloc_extent_map(GFP_NOFS);
} while (bh != head); if (!em) {
out_unlock: em = ERR_PTR(-ENOMEM);
unlock_page(page); goto out;
page_cache_release(page); }
return ret; em->start = 0;
em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
em->block_start = 0;
em->block_end = em->end;
em->bdev = inode->i_sb->s_bdev;
ret = add_extent_mapping(em_tree, em);
if (ret == -EEXIST) {
free_extent_map(em);
em = NULL;
goto again;
} else if (ret) {
em = ERR_PTR(ret);
}
out:
return em;
} }
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh, static int btree_writepage(struct page *page, struct writeback_control *wbc)
u64 logical)
{ {
if (logical == 0) { struct extent_map_tree *tree;
bh->b_bdev = NULL; tree = &BTRFS_I(page->mapping->host)->extent_tree;
bh->b_blocknr = 0; return extent_write_full_page(tree, page, btree_get_extent, wbc);
set_buffer_mapped(bh);
} else {
map_bh(bh, root->fs_info->sb, logical);
}
return 0;
} }
int btree_readpage(struct file *file, struct page *page)
struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
u64 blocknr)
{ {
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; struct extent_map_tree *tree;
int blockbits = root->fs_info->sb->s_blocksize_bits; tree = &BTRFS_I(page->mapping->host)->extent_tree;
unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); return extent_read_full_page(tree, page, btree_get_extent);
struct page *page; }
struct buffer_head *bh;
struct buffer_head *head;
struct buffer_head *ret = NULL;
int err;
u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
page = find_or_create_page(mapping, index, GFP_NOFS); static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
if (!page) {
return NULL; struct extent_map_tree *tree;
int ret;
if (!page_has_buffers(page)) BUG_ON(page->private != 1);
create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); tree = &BTRFS_I(page->mapping->host)->extent_tree;
head = page_buffers(page); ret = try_release_extent_mapping(tree, page);
bh = head; if (ret == 1) {
do { ClearPagePrivate(page);
if (!buffer_mapped(bh)) { set_page_private(page, 0);
err = btrfs_map_bh_to_logical(root, bh, first_block);
BUG_ON(err);
}
if (bh_blocknr(bh) == blocknr) {
ret = bh;
get_bh(bh);
goto out_unlock;
}
bh = bh->b_this_page;
first_block++;
} while (bh != head);
out_unlock:
unlock_page(page);
if (ret)
touch_buffer(ret);
page_cache_release(page); page_cache_release(page);
}
return ret; return ret;
} }
static int btree_get_block(struct inode *inode, sector_t iblock, static void btree_invalidatepage(struct page *page, unsigned long offset)
struct buffer_head *bh, int create)
{ {
int err; struct extent_map_tree *tree;
struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; tree = &BTRFS_I(page->mapping->host)->extent_tree;
err = btrfs_map_bh_to_logical(root, bh, iblock); extent_invalidatepage(tree, page, offset);
return err; btree_releasepage(page, GFP_NOFS);
} }
int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
char *result) char *result)
{ {
return 0;
#if 0
u32 crc; u32 crc;
crc = crc32c(0, data, len); crc = crc32c(0, data, len);
memcpy(result, &crc, BTRFS_CRC32_SIZE); memcpy(result, &crc, BTRFS_CRC32_SIZE);
return 0; return 0;
#endif
} }
static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, #if 0
static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
int verify) int verify)
{ {
return 0;
char result[BTRFS_CRC32_SIZE]; char result[BTRFS_CRC32_SIZE];
int ret; int ret;
struct btrfs_node *node; struct btrfs_node *node;
...@@ -176,7 +170,9 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, ...@@ -176,7 +170,9 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
} }
return 0; return 0;
} }
#endif
#if 0
static int btree_writepage(struct page *page, struct writeback_control *wbc) static int btree_writepage(struct page *page, struct writeback_control *wbc)
{ {
struct buffer_head *bh; struct buffer_head *bh;
...@@ -195,87 +191,65 @@ static int btree_writepage(struct page *page, struct writeback_control *wbc) ...@@ -195,87 +191,65 @@ static int btree_writepage(struct page *page, struct writeback_control *wbc)
} while (bh != head); } while (bh != head);
return block_write_full_page(page, btree_get_block, wbc); return block_write_full_page(page, btree_get_block, wbc);
} }
#endif
static int btree_readpage(struct file * file, struct page * page)
{
return block_read_full_page(page, btree_get_block);
}
static struct address_space_operations btree_aops = { static struct address_space_operations btree_aops = {
.readpage = btree_readpage, .readpage = btree_readpage,
.writepage = btree_writepage, .writepage = btree_writepage,
.releasepage = btree_releasepage,
.invalidatepage = btree_invalidatepage,
.sync_page = block_sync_page, .sync_page = block_sync_page,
}; };
int readahead_tree_block(struct btrfs_root *root, u64 blocknr) int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
{ {
struct buffer_head *bh = NULL; struct extent_buffer *buf = NULL;
struct inode *btree_inode = root->fs_info->btree_inode;
int ret = 0; int ret = 0;
bh = btrfs_find_create_tree_block(root, blocknr); buf = btrfs_find_create_tree_block(root, blocknr);
if (!bh) if (!buf)
return 0; return 0;
if (buffer_uptodate(bh)) { read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
ret = 1; buf, 0);
goto done; free_extent_buffer(buf);
}
if (test_set_buffer_locked(bh)) {
ret = 1;
goto done;
}
if (!buffer_uptodate(bh)) {
get_bh(bh);
bh->b_end_io = end_buffer_read_sync;
submit_bh(READ, bh);
} else {
unlock_buffer(bh);
ret = 1;
}
done:
brelse(bh);
return ret; return ret;
} }
struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
{ {
struct buffer_head *bh = NULL; struct extent_buffer *buf = NULL;
struct inode *btree_inode = root->fs_info->btree_inode;
bh = btrfs_find_create_tree_block(root, blocknr);
if (!bh) buf = btrfs_find_create_tree_block(root, blocknr);
return bh; if (!buf)
if (buffer_uptodate(bh))
goto uptodate;
lock_buffer(bh);
if (!buffer_uptodate(bh)) {
get_bh(bh);
bh->b_end_io = end_buffer_read_sync;
submit_bh(READ, bh);
wait_on_buffer(bh);
if (!buffer_uptodate(bh))
goto fail;
} else {
unlock_buffer(bh);
}
uptodate:
if (!buffer_checked(bh)) {
csum_tree_block(root, bh, 1);
set_buffer_checked(bh);
}
if (check_tree_block(root, bh))
goto fail;
return bh;
fail:
brelse(bh);
return NULL; return NULL;
read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
buf, 1);
return buf;
} }
int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf) struct extent_buffer *buf)
{ {
WARN_ON(atomic_read(&buf->b_count) == 0); struct inode *btree_inode = root->fs_info->btree_inode;
lock_buffer(buf); clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
clear_buffer_dirty(buf); return 0;
unlock_buffer(buf); }
int wait_on_tree_block_writeback(struct btrfs_root *root,
struct extent_buffer *buf)
{
struct inode *btree_inode = root->fs_info->btree_inode;
wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
buf);
return 0;
}
int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
{
struct inode *btree_inode = root->fs_info->btree_inode;
set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
return 0; return 0;
} }
...@@ -287,7 +261,9 @@ static int __setup_root(int blocksize, ...@@ -287,7 +261,9 @@ static int __setup_root(int blocksize,
root->node = NULL; root->node = NULL;
root->inode = NULL; root->inode = NULL;
root->commit_root = NULL; root->commit_root = NULL;
root->blocksize = blocksize; root->sectorsize = blocksize;
root->nodesize = blocksize;
root->leafsize = blocksize;
root->ref_cows = 0; root->ref_cows = 0;
root->fs_info = fs_info; root->fs_info = fs_info;
root->objectid = objectid; root->objectid = objectid;
...@@ -332,7 +308,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, ...@@ -332,7 +308,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
struct btrfs_root *root; struct btrfs_root *root;
struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *tree_root = fs_info->tree_root;
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_leaf *l; struct extent_buffer *l;
u64 highest_inode; u64 highest_inode;
int ret = 0; int ret = 0;
...@@ -361,11 +337,10 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, ...@@ -361,11 +337,10 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
ret = -ENOENT; ret = -ENOENT;
goto out; goto out;
} }
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
memcpy(&root->root_item, read_extent_buffer(l, &root->root_item,
btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), btrfs_item_ptr_offset(l, path->slots[0]),
sizeof(root->root_item)); sizeof(root->root_item));
memcpy(&root->root_key, location, sizeof(*location));
ret = 0; ret = 0;
out: out:
btrfs_release_path(root, path); btrfs_release_path(root, path);
...@@ -406,21 +381,21 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, ...@@ -406,21 +381,21 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
(unsigned long)root->root_key.objectid, (unsigned long)root->root_key.objectid,
root); root);
if (ret) { if (ret) {
brelse(root->node); free_extent_buffer(root->node);
kfree(root); kfree(root);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
ret = btrfs_set_root_name(root, name, namelen); ret = btrfs_set_root_name(root, name, namelen);
if (ret) { if (ret) {
brelse(root->node); free_extent_buffer(root->node);
kfree(root); kfree(root);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
ret = btrfs_sysfs_add_root(root); ret = btrfs_sysfs_add_root(root);
if (ret) { if (ret) {
brelse(root->node); free_extent_buffer(root->node);
kfree(root->name); kfree(root->name);
kfree(root); kfree(root);
return ERR_PTR(ret); return ERR_PTR(ret);
...@@ -471,6 +446,9 @@ struct btrfs_root *open_ctree(struct super_block *sb) ...@@ -471,6 +446,9 @@ struct btrfs_root *open_ctree(struct super_block *sb)
fs_info->btree_inode->i_nlink = 1; fs_info->btree_inode->i_nlink = 1;
fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
fs_info->btree_inode->i_mapping->a_ops = &btree_aops; fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
fs_info->btree_inode->i_mapping,
GFP_NOFS);
fs_info->do_barriers = 1; fs_info->do_barriers = 1;
fs_info->closing = 0; fs_info->closing = 0;
...@@ -493,10 +471,14 @@ struct btrfs_root *open_ctree(struct super_block *sb) ...@@ -493,10 +471,14 @@ struct btrfs_root *open_ctree(struct super_block *sb)
if (!fs_info->sb_buffer) if (!fs_info->sb_buffer)
goto fail_iput; goto fail_iput;
disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
fs_info->disk_super = disk_super;
memcpy(&fs_info->super_copy, disk_super, sizeof(fs_info->super_copy));
read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
sizeof(fs_info->super_copy));
read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
(unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
BTRFS_FSID_SIZE);
disk_super = &fs_info->super_copy;
if (!btrfs_super_root(disk_super)) if (!btrfs_super_root(disk_super))
goto fail_sb_buffer; goto fail_sb_buffer;
...@@ -530,9 +512,9 @@ struct btrfs_root *open_ctree(struct super_block *sb) ...@@ -530,9 +512,9 @@ struct btrfs_root *open_ctree(struct super_block *sb)
return tree_root; return tree_root;
fail_tree_root: fail_tree_root:
btrfs_block_release(tree_root, tree_root->node); free_extent_buffer(tree_root->node);
fail_sb_buffer: fail_sb_buffer:
btrfs_block_release(tree_root, fs_info->sb_buffer); free_extent_buffer(fs_info->sb_buffer);
fail_iput: fail_iput:
iput(fs_info->btree_inode); iput(fs_info->btree_inode);
fail: fail:
...@@ -546,31 +528,13 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -546,31 +528,13 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
*root) *root)
{ {
int ret; int ret;
struct buffer_head *bh = root->fs_info->sb_buffer; struct extent_buffer *super = root->fs_info->sb_buffer;
struct inode *btree_inode = root->fs_info->btree_inode;
lock_buffer(bh); set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
WARN_ON(atomic_read(&bh->b_count) < 1); ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
clear_buffer_dirty(bh); super->start, super->len);
csum_tree_block(root, bh, 0); return ret;
bh->b_end_io = end_buffer_write_sync;
get_bh(bh);
if (root->fs_info->do_barriers)
ret = submit_bh(WRITE_BARRIER, bh);
else
ret = submit_bh(WRITE, bh);
if (ret == -EOPNOTSUPP) {
get_bh(bh);
lock_buffer(bh);
set_buffer_uptodate(bh);
root->fs_info->do_barriers = 0;
ret = submit_bh(WRITE, bh);
}
wait_on_buffer(bh);
if (!buffer_uptodate(bh)) {
WARN_ON(1);
return -EIO;
}
return 0;
} }
int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
...@@ -581,9 +545,9 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) ...@@ -581,9 +545,9 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
if (root->inode) if (root->inode)
iput(root->inode); iput(root->inode);
if (root->node) if (root->node)
brelse(root->node); free_extent_buffer(root->node);
if (root->commit_root) if (root->commit_root)
brelse(root->commit_root); free_extent_buffer(root->commit_root);
if (root->name) if (root->name)
kfree(root->name); kfree(root->name);
kfree(root); kfree(root);
...@@ -629,12 +593,10 @@ int close_ctree(struct btrfs_root *root) ...@@ -629,12 +593,10 @@ int close_ctree(struct btrfs_root *root)
mutex_unlock(&fs_info->fs_mutex); mutex_unlock(&fs_info->fs_mutex);
if (fs_info->extent_root->node) if (fs_info->extent_root->node)
btrfs_block_release(fs_info->extent_root, free_extent_buffer(fs_info->extent_root->node);
fs_info->extent_root->node);
if (fs_info->tree_root->node) if (fs_info->tree_root->node)
btrfs_block_release(fs_info->tree_root, free_extent_buffer(fs_info->tree_root->node);
fs_info->tree_root->node); free_extent_buffer(fs_info->sb_buffer);
btrfs_block_release(root, fs_info->sb_buffer);
truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
iput(fs_info->btree_inode); iput(fs_info->btree_inode);
...@@ -645,25 +607,32 @@ int close_ctree(struct btrfs_root *root) ...@@ -645,25 +607,32 @@ int close_ctree(struct btrfs_root *root)
return 0; return 0;
} }
void btrfs_mark_buffer_dirty(struct buffer_head *bh) int btrfs_buffer_uptodate(struct extent_buffer *buf)
{
struct inode *btree_inode = buf->pages[0]->mapping->host;
return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
}
int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
{ {
struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; struct inode *btree_inode = buf->pages[0]->mapping->host;
u64 transid = btrfs_header_generation(btrfs_buffer_header(bh)); return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
buf);
}
WARN_ON(!atomic_read(&bh->b_count)); void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
{
struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
u64 transid = btrfs_header_generation(buf);
struct inode *btree_inode = root->fs_info->btree_inode;
if (transid != root->fs_info->generation) { if (transid != root->fs_info->generation) {
printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
(unsigned long long)bh->b_blocknr, (unsigned long long)extent_buffer_blocknr(buf),
transid, root->fs_info->generation); transid, root->fs_info->generation);
WARN_ON(1); WARN_ON(1);
} }
mark_buffer_dirty(bh); set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
}
void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
{
brelse(buf);
} }
void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
......
...@@ -19,68 +19,35 @@ ...@@ -19,68 +19,35 @@
#ifndef __DISKIO__ #ifndef __DISKIO__
#define __DISKIO__ #define __DISKIO__
#include <linux/buffer_head.h>
#define BTRFS_SUPER_INFO_OFFSET (16 * 1024) #define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
enum btrfs_bh_state_bits { struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr);
BH_Checked = BH_PrivateStart,
BH_Defrag,
BH_DefragDone,
};
BUFFER_FNS(Checked, checked);
BUFFER_FNS(Defrag, defrag);
BUFFER_FNS(DefragDone, defrag_done);
static inline struct btrfs_node *btrfs_buffer_node(struct buffer_head *bh)
{
return (struct btrfs_node *)bh->b_data;
}
static inline struct btrfs_leaf *btrfs_buffer_leaf(struct buffer_head *bh)
{
return (struct btrfs_leaf *)bh->b_data;
}
static inline struct btrfs_header *btrfs_buffer_header(struct buffer_head *bh)
{
return &((struct btrfs_node *)bh->b_data)->header;
}
struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr);
int readahead_tree_block(struct btrfs_root *root, u64 blocknr); int readahead_tree_block(struct btrfs_root *root, u64 blocknr);
struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
u64 blocknr); u64 blocknr);
int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf);
int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf);
int clean_tree_block(struct btrfs_trans_handle *trans, int clean_tree_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct buffer_head *buf); struct btrfs_root *root, struct extent_buffer *buf);
int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
struct btrfs_root *open_ctree(struct super_block *sb); struct btrfs_root *open_ctree(struct super_block *sb);
int close_ctree(struct btrfs_root *root); int close_ctree(struct btrfs_root *root);
void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf);
int write_ctree_super(struct btrfs_trans_handle *trans, int write_ctree_super(struct btrfs_trans_handle *trans,
struct btrfs_root *root); struct btrfs_root *root);
struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr); struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, u64 blocknr);
char *result);
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
struct btrfs_key *location, struct btrfs_key *location,
const char *name, int namelen); const char *name, int namelen);
struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
struct btrfs_key *location); struct btrfs_key *location);
u64 bh_blocknr(struct buffer_head *bh);
int btrfs_insert_dev_radix(struct btrfs_root *root, int btrfs_insert_dev_radix(struct btrfs_root *root,
struct block_device *bdev, struct block_device *bdev,
u64 device_id, u64 device_id,
u64 block_start, u64 block_start,
u64 num_blocks); u64 num_blocks);
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
u64 logical);
void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr); void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr);
int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root); int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root);
void btrfs_mark_buffer_dirty(struct buffer_head *bh); void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
int btrfs_buffer_uptodate(struct extent_buffer *buf);
int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
int wait_on_tree_block_writeback(struct btrfs_root *root,
struct extent_buffer *buf);
#endif #endif
...@@ -33,7 +33,7 @@ static int cache_block_group(struct btrfs_root *root, ...@@ -33,7 +33,7 @@ static int cache_block_group(struct btrfs_root *root,
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct radix_tree_root *extent_radix; struct radix_tree_root *extent_radix;
int slot; int slot;
u64 i; u64 i;
...@@ -56,7 +56,6 @@ static int cache_block_group(struct btrfs_root *root, ...@@ -56,7 +56,6 @@ static int cache_block_group(struct btrfs_root *root,
path->reada = 2; path->reada = 2;
first_free = block_group->key.objectid; first_free = block_group->key.objectid;
key.objectid = block_group->key.objectid; key.objectid = block_group->key.objectid;
key.flags = 0;
key.offset = 0; key.offset = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
...@@ -69,9 +68,9 @@ static int cache_block_group(struct btrfs_root *root, ...@@ -69,9 +68,9 @@ static int cache_block_group(struct btrfs_root *root,
path->slots[0]--; path->slots[0]--;
while(1) { while(1) {
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
if (slot >= btrfs_header_nritems(&leaf->header)) { if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, path); ret = btrfs_next_leaf(root, path);
if (ret < 0) if (ret < 0)
goto err; goto err;
...@@ -82,7 +81,7 @@ static int cache_block_group(struct btrfs_root *root, ...@@ -82,7 +81,7 @@ static int cache_block_group(struct btrfs_root *root,
} }
} }
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid < block_group->key.objectid) { if (key.objectid < block_group->key.objectid) {
if (key.objectid + key.offset > first_free) if (key.objectid + key.offset > first_free)
first_free = key.objectid + key.offset; first_free = key.objectid + key.offset;
...@@ -116,8 +115,7 @@ static int cache_block_group(struct btrfs_root *root, ...@@ -116,8 +115,7 @@ static int cache_block_group(struct btrfs_root *root,
hole_size = block_group->key.objectid + hole_size = block_group->key.objectid +
block_group->key.offset - last; block_group->key.offset - last;
for (i = 0; i < hole_size; i++) { for (i = 0; i < hole_size; i++) {
set_radix_bit(extent_radix, set_radix_bit(extent_radix, last + i);
last + i);
} }
} }
block_group->cached = 1; block_group->cached = 1;
...@@ -366,7 +364,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, ...@@ -366,7 +364,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_leaf *l; struct extent_buffer *l;
struct btrfs_extent_item *item; struct btrfs_extent_item *item;
u32 refs; u32 refs;
...@@ -375,7 +373,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, ...@@ -375,7 +373,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
return -ENOMEM; return -ENOMEM;
key.objectid = blocknr; key.objectid = blocknr;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
key.offset = num_blocks; key.offset = num_blocks;
ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
...@@ -386,10 +383,10 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, ...@@ -386,10 +383,10 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
BUG(); BUG();
} }
BUG_ON(ret != 0); BUG_ON(ret != 0);
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
refs = btrfs_extent_refs(item); refs = btrfs_extent_refs(l, item);
btrfs_set_extent_refs(item, refs + 1); btrfs_set_extent_refs(l, item, refs + 1);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_release_path(root->fs_info->extent_root, path); btrfs_release_path(root->fs_info->extent_root, path);
...@@ -414,23 +411,25 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans, ...@@ -414,23 +411,25 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans,
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_leaf *l; struct extent_buffer *l;
struct btrfs_extent_item *item; struct btrfs_extent_item *item;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
key.objectid = blocknr; key.objectid = blocknr;
key.offset = num_blocks; key.offset = num_blocks;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
0, 0); 0, 0);
if (ret < 0) if (ret < 0)
goto out; goto out;
if (ret != 0) if (ret != 0) {
btrfs_print_leaf(root, path->nodes[0]);
printk("failed to find block number %Lu\n", blocknr);
BUG(); BUG();
l = btrfs_buffer_leaf(path->nodes[0]); }
l = path->nodes[0];
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
*refs = btrfs_extent_refs(item); *refs = btrfs_extent_refs(l, item);
out: out:
btrfs_free_path(path); btrfs_free_path(path);
return 0; return 0;
...@@ -439,16 +438,16 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans, ...@@ -439,16 +438,16 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans,
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root) struct btrfs_root *root)
{ {
return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1); return btrfs_inc_extent_ref(trans, root,
extent_buffer_blocknr(root->node), 1);
} }
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf) struct extent_buffer *buf)
{ {
u64 blocknr; u64 blocknr;
struct btrfs_node *buf_node; u32 nritems;
struct btrfs_leaf *buf_leaf; struct btrfs_key key;
struct btrfs_disk_key *key;
struct btrfs_file_extent_item *fi; struct btrfs_file_extent_item *fi;
int i; int i;
int leaf; int leaf;
...@@ -458,31 +457,31 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -458,31 +457,31 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
if (!root->ref_cows) if (!root->ref_cows)
return 0; return 0;
buf_node = btrfs_buffer_node(buf);
leaf = btrfs_is_leaf(buf_node); leaf = btrfs_is_leaf(buf);
buf_leaf = btrfs_buffer_leaf(buf); nritems = btrfs_header_nritems(buf);
for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) { for (i = 0; i < nritems; i++) {
if (leaf) { if (leaf) {
u64 disk_blocknr; u64 disk_blocknr;
key = &buf_leaf->items[i].key; btrfs_item_key_to_cpu(buf, &key, i);
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
continue; continue;
fi = btrfs_item_ptr(buf_leaf, i, fi = btrfs_item_ptr(buf, i,
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
if (btrfs_file_extent_type(fi) == if (btrfs_file_extent_type(buf, fi) ==
BTRFS_FILE_EXTENT_INLINE) BTRFS_FILE_EXTENT_INLINE)
continue; continue;
disk_blocknr = btrfs_file_extent_disk_blocknr(fi); disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
if (disk_blocknr == 0) if (disk_blocknr == 0)
continue; continue;
ret = btrfs_inc_extent_ref(trans, root, disk_blocknr, ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
btrfs_file_extent_disk_num_blocks(fi)); btrfs_file_extent_disk_num_blocks(buf, fi));
if (ret) { if (ret) {
faili = i; faili = i;
goto fail; goto fail;
} }
} else { } else {
blocknr = btrfs_node_blockptr(buf_node, i); blocknr = btrfs_node_blockptr(buf, i);
ret = btrfs_inc_extent_ref(trans, root, blocknr, 1); ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
if (ret) { if (ret) {
faili = i; faili = i;
...@@ -496,22 +495,23 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -496,22 +495,23 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
for (i =0; i < faili; i++) { for (i =0; i < faili; i++) {
if (leaf) { if (leaf) {
u64 disk_blocknr; u64 disk_blocknr;
key = &buf_leaf->items[i].key; btrfs_item_key_to_cpu(buf, &key, i);
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
continue; continue;
fi = btrfs_item_ptr(buf_leaf, i, fi = btrfs_item_ptr(buf, i,
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
if (btrfs_file_extent_type(fi) == if (btrfs_file_extent_type(buf, fi) ==
BTRFS_FILE_EXTENT_INLINE) BTRFS_FILE_EXTENT_INLINE)
continue; continue;
disk_blocknr = btrfs_file_extent_disk_blocknr(fi); disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
if (disk_blocknr == 0) if (disk_blocknr == 0)
continue; continue;
err = btrfs_free_extent(trans, root, disk_blocknr, err = btrfs_free_extent(trans, root, disk_blocknr,
btrfs_file_extent_disk_num_blocks(fi), 0); btrfs_file_extent_disk_num_blocks(buf,
fi), 0);
BUG_ON(err); BUG_ON(err);
} else { } else {
blocknr = btrfs_node_blockptr(buf_node, i); blocknr = btrfs_node_blockptr(buf, i);
err = btrfs_free_extent(trans, root, blocknr, 1, 0); err = btrfs_free_extent(trans, root, blocknr, 1, 0);
BUG_ON(err); BUG_ON(err);
} }
...@@ -527,16 +527,18 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans, ...@@ -527,16 +527,18 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
int ret; int ret;
int pending_ret; int pending_ret;
struct btrfs_root *extent_root = root->fs_info->extent_root; struct btrfs_root *extent_root = root->fs_info->extent_root;
struct btrfs_block_group_item *bi; unsigned long bi;
struct extent_buffer *leaf;
ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
BUG_ON(ret); BUG_ON(ret);
bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
struct btrfs_block_group_item); leaf = path->nodes[0];
memcpy(bi, &cache->item, sizeof(*bi)); bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
btrfs_mark_buffer_dirty(path->nodes[0]); write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
btrfs_mark_buffer_dirty(leaf);
btrfs_release_path(extent_root, path); btrfs_release_path(extent_root, path);
fail: fail:
finish_current_insert(trans, extent_root); finish_current_insert(trans, extent_root);
...@@ -768,11 +770,11 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct ...@@ -768,11 +770,11 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
unsigned long gang[8]; unsigned long gang[8];
struct btrfs_fs_info *info = extent_root->fs_info; struct btrfs_fs_info *info = extent_root->fs_info;
btrfs_set_extent_refs(&extent_item, 1); btrfs_set_stack_extent_refs(&extent_item, 1);
ins.offset = 1; ins.offset = 1;
ins.flags = 0;
btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid); btrfs_set_stack_extent_owner(&extent_item,
extent_root->root_key.objectid);
while(1) { while(1) {
ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0, ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
...@@ -795,23 +797,20 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct ...@@ -795,23 +797,20 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending) static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
{ {
int err; int err;
struct btrfs_header *header; struct extent_buffer *buf;
struct buffer_head *bh;
if (!pending) { if (!pending) {
bh = btrfs_find_tree_block(root, blocknr); buf = btrfs_find_tree_block(root, blocknr);
if (bh) { if (buf) {
if (buffer_uptodate(bh)) { if (btrfs_buffer_uptodate(buf)) {
u64 transid = u64 transid =
root->fs_info->running_transaction->transid; root->fs_info->running_transaction->transid;
header = btrfs_buffer_header(bh); if (btrfs_header_generation(buf) == transid) {
if (btrfs_header_generation(header) == free_extent_buffer(buf);
transid) {
btrfs_block_release(root, bh);
return 0; return 0;
} }
} }
btrfs_block_release(root, bh); free_extent_buffer(buf);
} }
err = set_radix_bit(&root->fs_info->pinned_radix, blocknr); err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
if (!err) { if (!err) {
...@@ -839,12 +838,12 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -839,12 +838,12 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
struct btrfs_key key; struct btrfs_key key;
struct btrfs_fs_info *info = root->fs_info; struct btrfs_fs_info *info = root->fs_info;
struct btrfs_root *extent_root = info->extent_root; struct btrfs_root *extent_root = info->extent_root;
struct extent_buffer *leaf;
int ret; int ret;
struct btrfs_extent_item *ei; struct btrfs_extent_item *ei;
u32 refs; u32 refs;
key.objectid = blocknr; key.objectid = blocknr;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
key.offset = num_blocks; key.offset = num_blocks;
...@@ -856,12 +855,16 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -856,12 +855,16 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
if (ret < 0) if (ret < 0)
return ret; return ret;
BUG_ON(ret); BUG_ON(ret);
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
leaf = path->nodes[0];
ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item); struct btrfs_extent_item);
BUG_ON(ei->refs == 0); refs = btrfs_extent_refs(leaf, ei);
refs = btrfs_extent_refs(ei) - 1; BUG_ON(refs == 0);
btrfs_set_extent_refs(ei, refs); refs -= 1;
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_set_extent_refs(leaf, ei, refs);
btrfs_mark_buffer_dirty(leaf);
if (refs == 0) { if (refs == 0) {
u64 super_blocks_used, root_blocks_used; u64 super_blocks_used, root_blocks_used;
...@@ -876,8 +879,8 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -876,8 +879,8 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
super_blocks_used - num_blocks); super_blocks_used - num_blocks);
/* block accounting for root item */ /* block accounting for root item */
root_blocks_used = btrfs_root_blocks_used(&root->root_item); root_blocks_used = btrfs_root_used(&root->root_item);
btrfs_set_root_blocks_used(&root->root_item, btrfs_set_root_used(&root->root_item,
root_blocks_used - num_blocks); root_blocks_used - num_blocks);
ret = btrfs_del_item(trans, extent_root, path); ret = btrfs_del_item(trans, extent_root, path);
...@@ -984,7 +987,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -984,7 +987,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
u64 test_block; u64 test_block;
u64 orig_search_start = search_start; u64 orig_search_start = search_start;
int start_found; int start_found;
struct btrfs_leaf *l; struct extent_buffer *l;
struct btrfs_root * root = orig_root->fs_info->extent_root; struct btrfs_root * root = orig_root->fs_info->extent_root;
struct btrfs_fs_info *info = root->fs_info; struct btrfs_fs_info *info = root->fs_info;
int total_needed = num_blocks; int total_needed = num_blocks;
...@@ -994,10 +997,10 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -994,10 +997,10 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
int wrapped = 0; int wrapped = 0;
WARN_ON(num_blocks < 1); WARN_ON(num_blocks < 1);
ins->flags = 0;
btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
level = btrfs_header_level(btrfs_buffer_header(root->node)); level = btrfs_header_level(root->node);
if (search_end == (u64)-1) if (search_end == (u64)-1)
search_end = btrfs_super_total_blocks(&info->super_copy); search_end = btrfs_super_total_blocks(&info->super_copy);
if (hint_block) { if (hint_block) {
...@@ -1034,8 +1037,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1034,8 +1037,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
path->slots[0]--; path->slots[0]--;
} }
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key); btrfs_item_key_to_cpu(l, &key, path->slots[0]);
/* /*
* a rare case, go back one key if we hit a block group item * a rare case, go back one key if we hit a block group item
* instead of an extent item * instead of an extent item
...@@ -1055,9 +1059,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1055,9 +1059,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
} }
while (1) { while (1) {
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
if (slot >= btrfs_header_nritems(&l->header)) { if (slot >= btrfs_header_nritems(l)) {
ret = btrfs_next_leaf(root, path); ret = btrfs_next_leaf(root, path);
if (ret == 0) if (ret == 0)
continue; continue;
...@@ -1075,7 +1079,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1075,7 +1079,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
goto check_pending; goto check_pending;
} }
btrfs_disk_key_to_cpu(&key, &l->items[slot].key); btrfs_item_key_to_cpu(l, &key, slot);
if (key.objectid >= search_start && key.objectid > last_block && if (key.objectid >= search_start && key.objectid > last_block &&
start_found) { start_found) {
if (last_block < search_start) if (last_block < search_start)
...@@ -1183,8 +1187,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans, ...@@ -1183,8 +1187,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *extent_root = info->extent_root; struct btrfs_root *extent_root = info->extent_root;
struct btrfs_extent_item extent_item; struct btrfs_extent_item extent_item;
btrfs_set_extent_refs(&extent_item, 1); btrfs_set_stack_extent_refs(&extent_item, 1);
btrfs_set_extent_owner(&extent_item, owner); btrfs_set_stack_extent_owner(&extent_item, owner);
WARN_ON(num_blocks < 1); WARN_ON(num_blocks < 1);
ret = find_free_extent(trans, root, num_blocks, empty_size, ret = find_free_extent(trans, root, num_blocks, empty_size,
...@@ -1201,8 +1205,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans, ...@@ -1201,8 +1205,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
num_blocks); num_blocks);
/* block accounting for root item */ /* block accounting for root item */
root_blocks_used = btrfs_root_blocks_used(&root->root_item); root_blocks_used = btrfs_root_used(&root->root_item);
btrfs_set_root_blocks_used(&root->root_item, root_blocks_used + btrfs_set_root_used(&root->root_item, root_blocks_used +
num_blocks); num_blocks);
if (root == extent_root) { if (root == extent_root) {
...@@ -1241,13 +1245,13 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans, ...@@ -1241,13 +1245,13 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
* helper function to allocate a block for a given tree * helper function to allocate a block for a given tree
* returns the tree buffer or NULL. * returns the tree buffer or NULL.
*/ */
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 hint, struct btrfs_root *root, u64 hint,
u64 empty_size) u64 empty_size)
{ {
struct btrfs_key ins; struct btrfs_key ins;
int ret; int ret;
struct buffer_head *buf; struct extent_buffer *buf;
ret = btrfs_alloc_extent(trans, root, root->root_key.objectid, ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1, empty_size, hint, (u64)-1, &ins, 0); 1, empty_size, hint, (u64)-1, &ins, 0);
...@@ -1260,53 +1264,57 @@ struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, ...@@ -1260,53 +1264,57 @@ struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
btrfs_free_extent(trans, root, ins.objectid, 1, 0); btrfs_free_extent(trans, root, ins.objectid, 1, 0);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
WARN_ON(buffer_dirty(buf)); btrfs_set_buffer_uptodate(buf);
set_buffer_uptodate(buf); set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
buf->start + buf->len - 1, GFP_NOFS);
/*
set_buffer_checked(buf); set_buffer_checked(buf);
set_buffer_defrag(buf); set_buffer_defrag(buf);
set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index); */
/* FIXME!!!!!!!!!!!!!!!!
set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
*/
trans->blocks_used++; trans->blocks_used++;
return buf; return buf;
} }
static int drop_leaf_ref(struct btrfs_trans_handle *trans, static int drop_leaf_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct buffer_head *cur) struct btrfs_root *root, struct extent_buffer *leaf)
{ {
struct btrfs_disk_key *key; struct btrfs_key key;
struct btrfs_leaf *leaf;
struct btrfs_file_extent_item *fi; struct btrfs_file_extent_item *fi;
int i; int i;
int nritems; int nritems;
int ret; int ret;
BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur))); BUG_ON(!btrfs_is_leaf(leaf));
leaf = btrfs_buffer_leaf(cur); nritems = btrfs_header_nritems(leaf);
nritems = btrfs_header_nritems(&leaf->header);
for (i = 0; i < nritems; i++) { for (i = 0; i < nritems; i++) {
u64 disk_blocknr; u64 disk_blocknr;
key = &leaf->items[i].key;
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) btrfs_item_key_to_cpu(leaf, &key, i);
if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
continue; continue;
fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE) if (btrfs_file_extent_type(leaf, fi) ==
BTRFS_FILE_EXTENT_INLINE)
continue; continue;
/* /*
* FIXME make sure to insert a trans record that * FIXME make sure to insert a trans record that
* repeats the snapshot del on crash * repeats the snapshot del on crash
*/ */
disk_blocknr = btrfs_file_extent_disk_blocknr(fi); disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
if (disk_blocknr == 0) if (disk_blocknr == 0)
continue; continue;
ret = btrfs_free_extent(trans, root, disk_blocknr, ret = btrfs_free_extent(trans, root, disk_blocknr,
btrfs_file_extent_disk_num_blocks(fi), btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
0);
BUG_ON(ret); BUG_ON(ret);
} }
return 0; return 0;
} }
static void reada_walk_down(struct btrfs_root *root, static void reada_walk_down(struct btrfs_root *root,
struct btrfs_node *node) struct extent_buffer *node)
{ {
int i; int i;
u32 nritems; u32 nritems;
...@@ -1314,7 +1322,7 @@ static void reada_walk_down(struct btrfs_root *root, ...@@ -1314,7 +1322,7 @@ static void reada_walk_down(struct btrfs_root *root,
int ret; int ret;
u32 refs; u32 refs;
nritems = btrfs_header_nritems(&node->header); nritems = btrfs_header_nritems(node);
for (i = 0; i < nritems; i++) { for (i = 0; i < nritems; i++) {
blocknr = btrfs_node_blockptr(node, i); blocknr = btrfs_node_blockptr(node, i);
ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs); ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
...@@ -1337,15 +1345,16 @@ static void reada_walk_down(struct btrfs_root *root, ...@@ -1337,15 +1345,16 @@ static void reada_walk_down(struct btrfs_root *root,
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int *level) *root, struct btrfs_path *path, int *level)
{ {
struct buffer_head *next; struct extent_buffer *next;
struct buffer_head *cur; struct extent_buffer *cur;
u64 blocknr; u64 blocknr;
int ret; int ret;
u32 refs; u32 refs;
WARN_ON(*level < 0); WARN_ON(*level < 0);
WARN_ON(*level >= BTRFS_MAX_LEVEL); WARN_ON(*level >= BTRFS_MAX_LEVEL);
ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]), ret = lookup_extent_ref(trans, root,
extent_buffer_blocknr(path->nodes[*level]),
1, &refs); 1, &refs);
BUG_ON(ret); BUG_ON(ret);
if (refs > 1) if (refs > 1)
...@@ -1360,21 +1369,20 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1360,21 +1369,20 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
cur = path->nodes[*level]; cur = path->nodes[*level];
if (*level > 0 && path->slots[*level] == 0) if (*level > 0 && path->slots[*level] == 0)
reada_walk_down(root, btrfs_buffer_node(cur)); reada_walk_down(root, cur);
if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) if (btrfs_header_level(cur) != *level)
WARN_ON(1); WARN_ON(1);
if (path->slots[*level] >= if (path->slots[*level] >=
btrfs_header_nritems(btrfs_buffer_header(cur))) btrfs_header_nritems(cur))
break; break;
if (*level == 0) { if (*level == 0) {
ret = drop_leaf_ref(trans, root, cur); ret = drop_leaf_ref(trans, root, cur);
BUG_ON(ret); BUG_ON(ret);
break; break;
} }
blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
path->slots[*level]);
ret = lookup_extent_ref(trans, root, blocknr, 1, &refs); ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
BUG_ON(ret); BUG_ON(ret);
if (refs != 1) { if (refs != 1) {
...@@ -1384,8 +1392,8 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1384,8 +1392,8 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
continue; continue;
} }
next = btrfs_find_tree_block(root, blocknr); next = btrfs_find_tree_block(root, blocknr);
if (!next || !buffer_uptodate(next)) { if (!next || !btrfs_buffer_uptodate(next)) {
brelse(next); free_extent_buffer(next);
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
next = read_tree_block(root, blocknr); next = read_tree_block(root, blocknr);
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
...@@ -1395,7 +1403,7 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1395,7 +1403,7 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
BUG_ON(ret); BUG_ON(ret);
if (refs != 1) { if (refs != 1) {
path->slots[*level]++; path->slots[*level]++;
brelse(next); free_extent_buffer(next);
ret = btrfs_free_extent(trans, root, ret = btrfs_free_extent(trans, root,
blocknr, 1, 1); blocknr, 1, 1);
BUG_ON(ret); BUG_ON(ret);
...@@ -1404,17 +1412,17 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1404,17 +1412,17 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
} }
WARN_ON(*level <= 0); WARN_ON(*level <= 0);
if (path->nodes[*level-1]) if (path->nodes[*level-1])
btrfs_block_release(root, path->nodes[*level-1]); free_extent_buffer(path->nodes[*level-1]);
path->nodes[*level-1] = next; path->nodes[*level-1] = next;
*level = btrfs_header_level(btrfs_buffer_header(next)); *level = btrfs_header_level(next);
path->slots[*level] = 0; path->slots[*level] = 0;
} }
out: out:
WARN_ON(*level < 0); WARN_ON(*level < 0);
WARN_ON(*level >= BTRFS_MAX_LEVEL); WARN_ON(*level >= BTRFS_MAX_LEVEL);
ret = btrfs_free_extent(trans, root, ret = btrfs_free_extent(trans, root,
bh_blocknr(path->nodes[*level]), 1, 1); extent_buffer_blocknr(path->nodes[*level]), 1, 1);
btrfs_block_release(root, path->nodes[*level]); free_extent_buffer(path->nodes[*level]);
path->nodes[*level] = NULL; path->nodes[*level] = NULL;
*level += 1; *level += 1;
BUG_ON(ret); BUG_ON(ret);
...@@ -1436,24 +1444,24 @@ static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1436,24 +1444,24 @@ static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
slot = path->slots[i]; slot = path->slots[i];
if (slot < btrfs_header_nritems( if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
btrfs_buffer_header(path->nodes[i])) - 1) { struct extent_buffer *node;
struct btrfs_node *node; struct btrfs_disk_key disk_key;
node = btrfs_buffer_node(path->nodes[i]); node = path->nodes[i];
path->slots[i]++; path->slots[i]++;
*level = i; *level = i;
WARN_ON(*level == 0); WARN_ON(*level == 0);
btrfs_node_key(node, &disk_key, path->slots[i]);
memcpy(&root_item->drop_progress, memcpy(&root_item->drop_progress,
&node->ptrs[path->slots[i]].key, &disk_key, sizeof(disk_key));
sizeof(root_item->drop_progress));
root_item->drop_level = i; root_item->drop_level = i;
return 0; return 0;
} else { } else {
ret = btrfs_free_extent(trans, root, ret = btrfs_free_extent(trans, root,
bh_blocknr(path->nodes[*level]), extent_buffer_blocknr(path->nodes[*level]),
1, 1); 1, 1);
BUG_ON(ret); BUG_ON(ret);
btrfs_block_release(root, path->nodes[*level]); free_extent_buffer(path->nodes[*level]);
path->nodes[*level] = NULL; path->nodes[*level] = NULL;
*level = i + 1; *level = i + 1;
} }
...@@ -1480,15 +1488,15 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1480,15 +1488,15 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
level = btrfs_header_level(btrfs_buffer_header(root->node)); level = btrfs_header_level(root->node);
orig_level = level; orig_level = level;
if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
path->nodes[level] = root->node; path->nodes[level] = root->node;
path->slots[level] = 0; path->slots[level] = 0;
} else { } else {
struct btrfs_key key; struct btrfs_key key;
struct btrfs_disk_key *found_key; struct btrfs_disk_key found_key;
struct btrfs_node *node; struct extent_buffer *node;
btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
level = root_item->drop_level; level = root_item->drop_level;
...@@ -1498,10 +1506,10 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1498,10 +1506,10 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
ret = wret; ret = wret;
goto out; goto out;
} }
node = btrfs_buffer_node(path->nodes[level]); node = path->nodes[level];
found_key = &node->ptrs[path->slots[level]].key; btrfs_node_key(node, &found_key, path->slots[level]);
WARN_ON(memcmp(found_key, &root_item->drop_progress, WARN_ON(memcmp(&found_key, &root_item->drop_progress,
sizeof(*found_key))); sizeof(found_key)));
} }
while(1) { while(1) {
wret = walk_down_tree(trans, root, path, &level); wret = walk_down_tree(trans, root, path, &level);
...@@ -1516,12 +1524,12 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -1516,12 +1524,12 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
ret = -EAGAIN; ret = -EAGAIN;
get_bh(root->node); extent_buffer_get(root->node);
break; break;
} }
for (i = 0; i <= orig_level; i++) { for (i = 0; i <= orig_level; i++) {
if (path->nodes[i]) { if (path->nodes[i]) {
btrfs_block_release(root, path->nodes[i]); free_extent_buffer(path->nodes[i]);
path->nodes[i] = 0; path->nodes[i] = 0;
} }
} }
...@@ -1581,13 +1589,12 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -1581,13 +1589,12 @@ int btrfs_read_block_groups(struct btrfs_root *root)
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
int err = 0; int err = 0;
struct btrfs_block_group_item *bi;
struct btrfs_block_group_cache *cache; struct btrfs_block_group_cache *cache;
struct btrfs_fs_info *info = root->fs_info; struct btrfs_fs_info *info = root->fs_info;
struct radix_tree_root *radix; struct radix_tree_root *radix;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_key found_key; struct btrfs_key found_key;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
u64 group_size_blocks; u64 group_size_blocks;
u64 used; u64 used;
...@@ -1596,7 +1603,6 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -1596,7 +1603,6 @@ int btrfs_read_block_groups(struct btrfs_root *root)
root = info->extent_root; root = info->extent_root;
key.objectid = 0; key.objectid = 0;
key.offset = group_size_blocks; key.offset = group_size_blocks;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -1610,18 +1616,18 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -1610,18 +1616,18 @@ int btrfs_read_block_groups(struct btrfs_root *root)
err = ret; err = ret;
break; break;
} }
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
btrfs_disk_key_to_cpu(&found_key, btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
&leaf->items[path->slots[0]].key);
cache = kmalloc(sizeof(*cache), GFP_NOFS); cache = kmalloc(sizeof(*cache), GFP_NOFS);
if (!cache) { if (!cache) {
err = -1; err = -1;
break; break;
} }
bi = btrfs_item_ptr(leaf, path->slots[0], read_extent_buffer(leaf, &cache->item,
struct btrfs_block_group_item); btrfs_item_ptr_offset(leaf, path->slots[0]),
if (bi->flags & BTRFS_BLOCK_GROUP_DATA) { sizeof(cache->item));
if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
radix = &info->block_group_data_radix; radix = &info->block_group_data_radix;
cache->data = 1; cache->data = 1;
} else { } else {
...@@ -1629,7 +1635,6 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -1629,7 +1635,6 @@ int btrfs_read_block_groups(struct btrfs_root *root)
cache->data = 0; cache->data = 0;
} }
memcpy(&cache->item, bi, sizeof(*bi));
memcpy(&cache->key, &found_key, sizeof(found_key)); memcpy(&cache->key, &found_key, sizeof(found_key));
cache->last_alloc = cache->key.objectid; cache->last_alloc = cache->key.objectid;
cache->first_free = cache->key.objectid; cache->first_free = cache->key.objectid;
...@@ -1640,11 +1645,12 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -1640,11 +1645,12 @@ int btrfs_read_block_groups(struct btrfs_root *root)
key.objectid = found_key.objectid + found_key.offset; key.objectid = found_key.objectid + found_key.offset;
btrfs_release_path(root, path); btrfs_release_path(root, path);
ret = radix_tree_insert(radix, found_key.objectid + ret = radix_tree_insert(radix, found_key.objectid +
found_key.offset - 1, found_key.offset - 1,
(void *)cache); (void *)cache);
BUG_ON(ret); BUG_ON(ret);
used = btrfs_block_group_used(bi); used = btrfs_block_group_used(&cache->item);
if (used < div_factor(key.offset, 8)) { if (used < div_factor(key.offset, 8)) {
radix_tree_tag_set(radix, found_key.objectid + radix_tree_tag_set(radix, found_key.objectid +
found_key.offset - 1, found_key.offset - 1,
......
...@@ -26,16 +26,6 @@ struct tree_entry { ...@@ -26,16 +26,6 @@ struct tree_entry {
struct rb_node rb_node; struct rb_node rb_node;
}; };
/* bits for the extent state */
#define EXTENT_DIRTY 1
#define EXTENT_WRITEBACK (1 << 1)
#define EXTENT_UPTODATE (1 << 2)
#define EXTENT_LOCKED (1 << 3)
#define EXTENT_NEW (1 << 4)
#define EXTENT_DELALLOC (1 << 5)
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
void __init extent_map_init(void) void __init extent_map_init(void)
{ {
extent_map_cache = btrfs_cache_create("extent_map", extent_map_cache = btrfs_cache_create("extent_map",
...@@ -223,7 +213,8 @@ int add_extent_mapping(struct extent_map_tree *tree, ...@@ -223,7 +213,8 @@ int add_extent_mapping(struct extent_map_tree *tree,
if (rb) if (rb)
prev = rb_entry(rb, struct extent_map, rb_node); prev = rb_entry(rb, struct extent_map, rb_node);
if (prev && prev->end + 1 == em->start && if (prev && prev->end + 1 == em->start &&
((em->block_start == 0 && prev->block_start == 0) || ((em->block_start == EXTENT_MAP_HOLE &&
prev->block_start == EXTENT_MAP_HOLE) ||
(em->block_start == prev->block_end + 1))) { (em->block_start == prev->block_end + 1))) {
em->start = prev->start; em->start = prev->start;
em->block_start = prev->block_start; em->block_start = prev->block_start;
...@@ -926,6 +917,40 @@ int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end) ...@@ -926,6 +917,40 @@ int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end)
} }
EXPORT_SYMBOL(set_range_writeback); EXPORT_SYMBOL(set_range_writeback);
int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
u64 *start_ret, u64 *end_ret, int bits)
{
struct rb_node *node;
struct extent_state *state;
int ret = 1;
write_lock_irq(&tree->lock);
/*
* this search will find all the extents that end after
* our range starts.
*/
node = tree_search(&tree->state, start);
if (!node || IS_ERR(node)) {
goto out;
}
while(1) {
state = rb_entry(node, struct extent_state, rb_node);
if (state->state & bits) {
*start_ret = state->start;
*end_ret = state->end;
ret = 0;
}
node = rb_next(node);
if (!node)
break;
}
out:
write_unlock_irq(&tree->lock);
return ret;
}
EXPORT_SYMBOL(find_first_extent_bit);
u64 find_lock_delalloc_range(struct extent_map_tree *tree, u64 find_lock_delalloc_range(struct extent_map_tree *tree,
u64 start, u64 lock_start, u64 *end, u64 max_bytes) u64 start, u64 lock_start, u64 *end, u64 max_bytes)
{ {
...@@ -1450,7 +1475,7 @@ int extent_read_full_page(struct extent_map_tree *tree, struct page *page, ...@@ -1450,7 +1475,7 @@ int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
em = NULL; em = NULL;
/* we've found a hole, just zero and go on */ /* we've found a hole, just zero and go on */
if (block_start == 0) { if (block_start == EXTENT_MAP_HOLE) {
zero_user_page(page, page_offset, iosize, KM_USER0); zero_user_page(page, page_offset, iosize, KM_USER0);
set_extent_uptodate(tree, cur, cur + iosize - 1, set_extent_uptodate(tree, cur, cur + iosize - 1,
GFP_NOFS); GFP_NOFS);
...@@ -1593,7 +1618,8 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page, ...@@ -1593,7 +1618,8 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
free_extent_map(em); free_extent_map(em);
em = NULL; em = NULL;
if (block_start == 0 || block_start == EXTENT_MAP_INLINE) { if (block_start == EXTENT_MAP_HOLE ||
block_start == EXTENT_MAP_INLINE) {
clear_extent_dirty(tree, cur, clear_extent_dirty(tree, cur,
cur + iosize - 1, GFP_NOFS); cur + iosize - 1, GFP_NOFS);
cur = cur + iosize; cur = cur + iosize;
...@@ -1630,7 +1656,6 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page, ...@@ -1630,7 +1656,6 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
nr++; nr++;
} }
done: done:
WARN_ON(test_range_bit(tree, start, page_end, EXTENT_DIRTY, 0));
unlock_extent(tree, start, page_end, GFP_NOFS); unlock_extent(tree, start, page_end, GFP_NOFS);
unlock_page(page); unlock_page(page);
return 0; return 0;
...@@ -1827,8 +1852,623 @@ sector_t extent_bmap(struct address_space *mapping, sector_t iblock, ...@@ -1827,8 +1852,623 @@ sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
// XXX(hch): block 0 is valid in some cases, e.g. XFS RT device // XXX(hch): block 0 is valid in some cases, e.g. XFS RT device
if (em->block_start == EXTENT_MAP_INLINE || if (em->block_start == EXTENT_MAP_INLINE ||
em->block_start == 0) em->block_start == EXTENT_MAP_HOLE)
return 0; return 0;
return (em->block_start + start - em->start) >> inode->i_blkbits; return (em->block_start + start - em->start) >> inode->i_blkbits;
} }
struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
u64 start, unsigned long len,
gfp_t mask)
{
unsigned long num_pages = ((start + len - 1) >> PAGE_CACHE_SHIFT) -
(start >> PAGE_CACHE_SHIFT) + 1;
unsigned long i;
unsigned long index = start >> PAGE_CACHE_SHIFT;
struct extent_buffer *eb;
struct page *p;
struct address_space *mapping = tree->mapping;
int uptodate = 0;
eb = kzalloc(EXTENT_BUFFER_SIZE(num_pages), mask);
if (!eb || IS_ERR(eb))
return NULL;
eb->start = start;
eb->len = len;
atomic_set(&eb->refs, 1);
for (i = 0; i < num_pages; i++, index++) {
p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
if (!p)
goto fail;
eb->pages[i] = p;
if (!PageUptodate(p))
uptodate = 0;
unlock_page(p);
}
if (uptodate)
eb->flags |= EXTENT_UPTODATE;
return eb;
fail:
free_extent_buffer(eb);
return NULL;
}
EXPORT_SYMBOL(alloc_extent_buffer);
struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
u64 start, unsigned long len,
gfp_t mask)
{
unsigned long num_pages = ((start + len - 1) >> PAGE_CACHE_SHIFT) -
(start >> PAGE_CACHE_SHIFT) + 1;
unsigned long i;
unsigned long index = start >> PAGE_CACHE_SHIFT;
struct extent_buffer *eb;
struct page *p;
struct address_space *mapping = tree->mapping;
eb = kzalloc(EXTENT_BUFFER_SIZE(num_pages), mask);
if (!eb || IS_ERR(eb))
return NULL;
eb->start = start;
eb->len = len;
atomic_set(&eb->refs, 1);
for (i = 0; i < num_pages; i++, index++) {
p = find_get_page(mapping, index);
if (!p)
goto fail;
eb->pages[i] = p;
}
return eb;
fail:
free_extent_buffer(eb);
return NULL;
}
EXPORT_SYMBOL(find_extent_buffer);
void free_extent_buffer(struct extent_buffer *eb)
{
unsigned long i;
unsigned long num_pages;
if (!eb)
return;
if (!atomic_dec_and_test(&eb->refs))
return;
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
(eb->start >> PAGE_CACHE_SHIFT) + 1;
for (i = 0; i < num_pages; i++) {
if (eb->pages[i])
page_cache_release(eb->pages[i]);
}
kfree(eb);
}
EXPORT_SYMBOL(free_extent_buffer);
int clear_extent_buffer_dirty(struct extent_map_tree *tree,
struct extent_buffer *eb)
{
int set;
unsigned long i;
unsigned long num_pages;
struct page *page;
u64 start = eb->start;
u64 end = start + eb->len - 1;
set = clear_extent_dirty(tree, start, end, GFP_NOFS);
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
(eb->start >> PAGE_CACHE_SHIFT) + 1;
for (i = 0; i < num_pages; i++) {
page = eb->pages[i];
lock_page(page);
/*
* if we're on the last page or the first page and the
* block isn't aligned on a page boundary, do extra checks
* to make sure we don't clean page that is partially dirty
*/
if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
((i == num_pages - 1) &&
((eb->start + eb->len - 1) & (PAGE_CACHE_SIZE - 1)))) {
start = page->index << PAGE_CACHE_SHIFT;
end = start + PAGE_CACHE_SIZE - 1;
if (test_range_bit(tree, start, end,
EXTENT_DIRTY, 0)) {
unlock_page(page);
continue;
}
}
clear_page_dirty_for_io(page);
unlock_page(page);
}
return 0;
}
EXPORT_SYMBOL(clear_extent_buffer_dirty);
int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
struct extent_buffer *eb)
{
return wait_on_extent_writeback(tree, eb->start,
eb->start + eb->len - 1);
}
EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
int set_extent_buffer_dirty(struct extent_map_tree *tree,
struct extent_buffer *eb)
{
return set_range_dirty(tree, eb->start, eb->start + eb->len - 1);
}
EXPORT_SYMBOL(set_extent_buffer_dirty);
int set_extent_buffer_uptodate(struct extent_map_tree *tree,
struct extent_buffer *eb)
{
unsigned long i;
struct page *page;
unsigned long num_pages;
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
(eb->start >> PAGE_CACHE_SHIFT) + 1;
set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
GFP_NOFS);
for (i = 0; i < num_pages; i++) {
page = eb->pages[i];
if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
((i == num_pages - 1) &&
((eb->start + eb->len - 1) & (PAGE_CACHE_SIZE - 1)))) {
check_page_uptodate(tree, page);
continue;
}
SetPageUptodate(page);
}
return 0;
}
EXPORT_SYMBOL(set_extent_buffer_uptodate);
int extent_buffer_uptodate(struct extent_map_tree *tree,
struct extent_buffer *eb)
{
if (eb->flags & EXTENT_UPTODATE)
return 1;
return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
EXTENT_UPTODATE, 1);
}
EXPORT_SYMBOL(extent_buffer_uptodate);
int read_extent_buffer_pages(struct extent_map_tree *tree,
struct extent_buffer *eb, int wait)
{
unsigned long i;
struct page *page;
int err;
int ret = 0;
unsigned long num_pages;
if (eb->flags & EXTENT_UPTODATE)
return 0;
if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
EXTENT_UPTODATE, 1)) {
return 0;
}
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
(eb->start >> PAGE_CACHE_SHIFT) + 1;
for (i = 0; i < num_pages; i++) {
page = eb->pages[i];
if (PageUptodate(page)) {
continue;
}
if (!wait) {
if (TestSetPageLocked(page)) {
continue;
}
} else {
lock_page(page);
}
if (!PageUptodate(page)) {
err = page->mapping->a_ops->readpage(NULL, page);
if (err) {
ret = err;
}
} else {
unlock_page(page);
}
}
if (ret || !wait) {
return ret;
}
for (i = 0; i < num_pages; i++) {
page = eb->pages[i];
wait_on_page_locked(page);
if (!PageUptodate(page)) {
ret = -EIO;
}
}
eb->flags |= EXTENT_UPTODATE;
return ret;
}
EXPORT_SYMBOL(read_extent_buffer_pages);
void read_extent_buffer(struct extent_buffer *eb, void *dstv,
unsigned long start,
unsigned long len)
{
size_t cur;
size_t offset;
struct page *page;
char *kaddr;
char *dst = (char *)dstv;
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len);
page = eb->pages[i];
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
if (i == 0)
offset += start_offset;
while(len > 0) {
WARN_ON(!PageUptodate(page));
cur = min(len, (PAGE_CACHE_SIZE - offset));
// kaddr = kmap_atomic(page, KM_USER0);
kaddr = page_address(page);
memcpy(dst, kaddr + offset, cur);
// kunmap_atomic(kaddr, KM_USER0);
dst += cur;
len -= cur;
offset = 0;
i++;
page = eb->pages[i];
}
}
EXPORT_SYMBOL(read_extent_buffer);
int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
char **token, char **map,
unsigned long *map_start,
unsigned long *map_len, int km)
{
size_t offset;
char *kaddr;
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
WARN_ON(start > eb->len);
if (i == 0) {
offset = start_offset;
*map_start = 0;
} else {
offset = 0;
*map_start = (i << PAGE_CACHE_SHIFT) - offset;
}
// kaddr = kmap_atomic(eb->pages[i], km);
kaddr = page_address(eb->pages[i]);
*token = kaddr;
*map = kaddr + offset;
*map_len = PAGE_CACHE_SIZE - offset;
return 0;
}
EXPORT_SYMBOL(map_extent_buffer);
void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
{
// kunmap_atomic(token, km);
}
EXPORT_SYMBOL(unmap_extent_buffer);
int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
unsigned long start,
unsigned long len)
{
size_t cur;
size_t offset;
struct page *page;
char *kaddr;
char *ptr = (char *)ptrv;
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
int ret = 0;
WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len);
page = eb->pages[i];
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
if (i == 0)
offset += start_offset;
while(len > 0) {
WARN_ON(!PageUptodate(page));
cur = min(len, (PAGE_CACHE_SIZE - offset));
// kaddr = kmap_atomic(page, KM_USER0);
kaddr = page_address(page);
ret = memcmp(ptr, kaddr + offset, cur);
// kunmap_atomic(kaddr, KM_USER0);
if (ret)
break;
ptr += cur;
len -= cur;
offset = 0;
i++;
page = eb->pages[i];
}
return ret;
}
EXPORT_SYMBOL(memcmp_extent_buffer);
void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
unsigned long start, unsigned long len)
{
size_t cur;
size_t offset;
struct page *page;
char *kaddr;
char *src = (char *)srcv;
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len);
page = eb->pages[i];
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
if (i == 0)
offset += start_offset;
while(len > 0) {
WARN_ON(!PageUptodate(page));
cur = min(len, PAGE_CACHE_SIZE - offset);
// kaddr = kmap_atomic(page, KM_USER0);
kaddr = page_address(page);
memcpy(kaddr + offset, src, cur);
// kunmap_atomic(kaddr, KM_USER0);
src += cur;
len -= cur;
offset = 0;
i++;
page = eb->pages[i];
}
}
EXPORT_SYMBOL(write_extent_buffer);
void memset_extent_buffer(struct extent_buffer *eb, char c,
unsigned long start, unsigned long len)
{
size_t cur;
size_t offset;
struct page *page;
char *kaddr;
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len);
page = eb->pages[i];
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
if (i == 0)
offset += start_offset;
while(len > 0) {
WARN_ON(!PageUptodate(page));
cur = min(len, PAGE_CACHE_SIZE - offset);
// kaddr = kmap_atomic(page, KM_USER0);
kaddr = page_address(page);
memset(kaddr + offset, c, cur);
// kunmap_atomic(kaddr, KM_USER0);
len -= cur;
offset = 0;
i++;
page = eb->pages[i];
}
}
EXPORT_SYMBOL(memset_extent_buffer);
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
unsigned long dst_offset, unsigned long src_offset,
unsigned long len)
{
u64 dst_len = dst->len;
size_t cur;
size_t offset;
struct page *page;
char *kaddr;
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
WARN_ON(src->len != dst_len);
offset = dst_offset & ((unsigned long)PAGE_CACHE_SIZE - 1);
if (i == 0)
offset += start_offset;
while(len > 0) {
page = dst->pages[i];
WARN_ON(!PageUptodate(page));
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
// kaddr = kmap_atomic(page, KM_USER1);
kaddr = page_address(page);
read_extent_buffer(src, kaddr + offset, src_offset, cur);
// kunmap_atomic(kaddr, KM_USER1);
src_offset += cur;
len -= cur;
offset = 0;
i++;
}
}
EXPORT_SYMBOL(copy_extent_buffer);
static void move_pages(struct page *dst_page, struct page *src_page,
unsigned long dst_off, unsigned long src_off,
unsigned long len)
{
// char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
char *dst_kaddr = page_address(dst_page);
if (dst_page == src_page) {
memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
} else {
// char *src_kaddr = kmap_atomic(src_page, KM_USER1);
char *src_kaddr = page_address(src_page);
char *p = dst_kaddr + dst_off + len;
char *s = src_kaddr + src_off + len;
while (len--)
*--p = *--s;
// kunmap_atomic(src_kaddr, KM_USER1);
}
// kunmap_atomic(dst_kaddr, KM_USER0);
}
static void copy_pages(struct page *dst_page, struct page *src_page,
unsigned long dst_off, unsigned long src_off,
unsigned long len)
{
//kmap_atomic(dst_page, KM_USER0);
char *dst_kaddr = page_address(dst_page);
char *src_kaddr;
if (dst_page != src_page)
src_kaddr = page_address(src_page); // kmap_atomic(src_page, KM_USER1);
else
src_kaddr = dst_kaddr;
memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
/*
kunmap_atomic(dst_kaddr, KM_USER0);
if (dst_page != src_page)
kunmap_atomic(src_kaddr, KM_USER1);
*/
}
void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
unsigned long src_offset, unsigned long len)
{
size_t cur;
size_t dst_off_in_page;
size_t src_off_in_page;
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long dst_i;
unsigned long src_i;
if (src_offset + len > dst->len) {
printk("memmove bogus src_offset %lu move len %lu len %lu\n",
src_offset, len, dst->len);
BUG_ON(1);
}
if (dst_offset + len > dst->len) {
printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
dst_offset, len, dst->len);
BUG_ON(1);
}
while(len > 0) {
dst_off_in_page = dst_offset &
((unsigned long)PAGE_CACHE_SIZE - 1);
src_off_in_page = src_offset &
((unsigned long)PAGE_CACHE_SIZE - 1);
dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
if (src_i == 0)
src_off_in_page += start_offset;
if (dst_i == 0)
dst_off_in_page += start_offset;
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
src_off_in_page));
cur = min(cur, (unsigned long)(PAGE_CACHE_SIZE -
dst_off_in_page));
copy_pages(dst->pages[dst_i], dst->pages[src_i],
dst_off_in_page, src_off_in_page, cur);
src_offset += cur;
dst_offset += cur;
len -= cur;
}
}
EXPORT_SYMBOL(memcpy_extent_buffer);
void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
unsigned long src_offset, unsigned long len)
{
size_t cur;
size_t dst_off_in_page;
size_t src_off_in_page;
unsigned long dst_end = dst_offset + len - 1;
unsigned long src_end = src_offset + len - 1;
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
unsigned long dst_i;
unsigned long src_i;
if (src_offset + len > dst->len) {
printk("memmove bogus src_offset %lu move len %lu len %lu\n",
src_offset, len, dst->len);
BUG_ON(1);
}
if (dst_offset + len > dst->len) {
printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
dst_offset, len, dst->len);
BUG_ON(1);
}
if (dst_offset < src_offset) {
memcpy_extent_buffer(dst, dst_offset, src_offset, len);
return;
}
while(len > 0) {
dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
dst_off_in_page = dst_end &
((unsigned long)PAGE_CACHE_SIZE - 1);
src_off_in_page = src_end &
((unsigned long)PAGE_CACHE_SIZE - 1);
if (src_i == 0)
src_off_in_page += start_offset;
if (dst_i == 0)
dst_off_in_page += start_offset;
cur = min(len, src_off_in_page + 1);
cur = min(cur, dst_off_in_page + 1);
// printk("move pages orig dst %lu src %lu len %lu, this %lu %lu %lu\n", dst_offset, src_offset, len, dst_off_in_page - cur + 1, src_off_in_page - cur + 1, cur);
move_pages(dst->pages[dst_i], dst->pages[src_i],
dst_off_in_page - cur + 1,
src_off_in_page - cur + 1, cur);
dst_end -= cur - 1;
src_end -= cur - 1;
len -= cur;
}
}
EXPORT_SYMBOL(memmove_extent_buffer);
...@@ -3,9 +3,20 @@ ...@@ -3,9 +3,20 @@
#include <linux/rbtree.h> #include <linux/rbtree.h>
#define EXTENT_MAP_HOLE (u64)-3
#define EXTENT_MAP_INLINE (u64)-2 #define EXTENT_MAP_INLINE (u64)-2
#define EXTENT_MAP_DELALLOC (u64)-1 #define EXTENT_MAP_DELALLOC (u64)-1
/* bits for the extent state */
#define EXTENT_DIRTY 1
#define EXTENT_WRITEBACK (1 << 1)
#define EXTENT_UPTODATE (1 << 2)
#define EXTENT_LOCKED (1 << 3)
#define EXTENT_NEW (1 << 4)
#define EXTENT_DELALLOC (1 << 5)
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
struct extent_map_ops { struct extent_map_ops {
int (*fill_delalloc)(struct inode *inode, u64 start, u64 end); int (*fill_delalloc)(struct inode *inode, u64 start, u64 end);
int (*writepage_io_hook)(struct page *page, u64 start, u64 end); int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
...@@ -51,10 +62,13 @@ struct extent_state { ...@@ -51,10 +62,13 @@ struct extent_state {
struct list_head list; struct list_head list;
}; };
#define EXTENT_BUFFER_SIZE(nr) (sizeof(struct extent_buffer) + \
(nr - 1) * sizeof(struct page *))
struct extent_buffer { struct extent_buffer {
u64 start; u64 start;
u64 end; /* inclusive */ unsigned long len;
char *addr; atomic_t refs;
int flags;
struct page *pages[]; struct page *pages[];
}; };
...@@ -87,8 +101,12 @@ int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end, ...@@ -87,8 +101,12 @@ int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask); gfp_t mask);
int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end, int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask); gfp_t mask);
int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask);
int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end, int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
gfp_t mask); gfp_t mask);
int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
u64 *start_ret, u64 *end_ret, int bits);
int extent_invalidatepage(struct extent_map_tree *tree, int extent_invalidatepage(struct extent_map_tree *tree,
struct page *page, unsigned long offset); struct page *page, unsigned long offset);
int extent_write_full_page(struct extent_map_tree *tree, struct page *page, int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
...@@ -106,4 +124,57 @@ int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end); ...@@ -106,4 +124,57 @@ int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end);
int set_state_private(struct extent_map_tree *tree, u64 start, u64 private); int set_state_private(struct extent_map_tree *tree, u64 start, u64 private);
int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private); int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private);
void set_page_extent_mapped(struct page *page); void set_page_extent_mapped(struct page *page);
struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
u64 start, unsigned long len,
gfp_t mask);
struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
u64 start, unsigned long len,
gfp_t mask);
void free_extent_buffer(struct extent_buffer *eb);
int read_extent_buffer_pages(struct extent_map_tree *tree,
struct extent_buffer *eb, int wait);
static inline void extent_buffer_get(struct extent_buffer *eb)
{
atomic_inc(&eb->refs);
}
static inline u64 extent_buffer_blocknr(struct extent_buffer *eb)
{
return eb->start / 4096;
}
int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
unsigned long start,
unsigned long len);
void read_extent_buffer(struct extent_buffer *eb, void *dst,
unsigned long start,
unsigned long len);
void write_extent_buffer(struct extent_buffer *eb, const void *src,
unsigned long start, unsigned long len);
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
unsigned long dst_offset, unsigned long src_offset,
unsigned long len);
void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
unsigned long src_offset, unsigned long len);
void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
unsigned long src_offset, unsigned long len);
void memset_extent_buffer(struct extent_buffer *eb, char c,
unsigned long start, unsigned long len);
int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
struct extent_buffer *eb);
int clear_extent_buffer_dirty(struct extent_map_tree *tree,
struct extent_buffer *eb);
int set_extent_buffer_dirty(struct extent_map_tree *tree,
struct extent_buffer *eb);
int set_extent_buffer_uptodate(struct extent_map_tree *tree,
struct extent_buffer *eb);
int extent_buffer_uptodate(struct extent_map_tree *tree,
struct extent_buffer *eb);
int map_extent_buffer(struct extent_buffer *eb, unsigned long offset,
char **token, char **map,
unsigned long *map_start,
unsigned long *map_len, int km);
void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km);
#endif #endif
...@@ -34,12 +34,12 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, ...@@ -34,12 +34,12 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_file_extent_item *item; struct btrfs_file_extent_item *item;
struct btrfs_key file_key; struct btrfs_key file_key;
struct btrfs_path *path; struct btrfs_path *path;
struct extent_buffer *leaf;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
file_key.objectid = objectid; file_key.objectid = objectid;
file_key.offset = pos; file_key.offset = pos;
file_key.flags = 0;
btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
ret = btrfs_insert_empty_item(trans, root, path, &file_key, ret = btrfs_insert_empty_item(trans, root, path, &file_key,
...@@ -47,15 +47,16 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, ...@@ -47,15 +47,16 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
if (ret < 0) if (ret < 0)
goto out; goto out;
BUG_ON(ret); BUG_ON(ret);
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], leaf = path->nodes[0];
item = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
btrfs_set_file_extent_disk_blocknr(item, offset); btrfs_set_file_extent_disk_blocknr(leaf, item, offset);
btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks); btrfs_set_file_extent_disk_num_blocks(leaf, item, disk_num_blocks);
btrfs_set_file_extent_offset(item, 0); btrfs_set_file_extent_offset(leaf, item, 0);
btrfs_set_file_extent_num_blocks(item, num_blocks); btrfs_set_file_extent_num_blocks(leaf, item, num_blocks);
btrfs_set_file_extent_generation(item, trans->transid); btrfs_set_file_extent_generation(leaf, item, trans->transid);
btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG); btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
out: out:
btrfs_free_path(path); btrfs_free_path(path);
return ret; return ret;
...@@ -71,32 +72,30 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, ...@@ -71,32 +72,30 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
struct btrfs_key file_key; struct btrfs_key file_key;
struct btrfs_key found_key; struct btrfs_key found_key;
struct btrfs_csum_item *item; struct btrfs_csum_item *item;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
u64 csum_offset = 0; u64 csum_offset = 0;
int csums_in_item; int csums_in_item;
file_key.objectid = objectid; file_key.objectid = objectid;
file_key.offset = offset; file_key.offset = offset;
file_key.flags = 0;
btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
if (ret > 0) { if (ret > 0) {
ret = 1; ret = 1;
if (path->slots[0] == 0) if (path->slots[0] == 0)
goto fail; goto fail;
path->slots[0]--; path->slots[0]--;
btrfs_disk_key_to_cpu(&found_key, btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
&leaf->items[path->slots[0]].key);
if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
found_key.objectid != objectid) { found_key.objectid != objectid) {
goto fail; goto fail;
} }
csum_offset = (offset - found_key.offset) >> csum_offset = (offset - found_key.offset) >>
root->fs_info->sb->s_blocksize_bits; root->fs_info->sb->s_blocksize_bits;
csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
csums_in_item /= BTRFS_CRC32_SIZE; csums_in_item /= BTRFS_CRC32_SIZE;
if (csum_offset >= csums_in_item) { if (csum_offset >= csums_in_item) {
...@@ -127,7 +126,6 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, ...@@ -127,7 +126,6 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
file_key.objectid = objectid; file_key.objectid = objectid;
file_key.offset = offset; file_key.offset = offset;
file_key.flags = 0;
btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
return ret; return ret;
...@@ -138,12 +136,14 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -138,12 +136,14 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
u64 objectid, u64 offset, u64 objectid, u64 offset,
char *data, size_t len) char *data, size_t len)
{ {
return 0;
#if 0
int ret; int ret;
struct btrfs_key file_key; struct btrfs_key file_key;
struct btrfs_key found_key; struct btrfs_key found_key;
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_csum_item *item; struct btrfs_csum_item *item;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
u64 csum_offset; u64 csum_offset;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -161,8 +161,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -161,8 +161,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
if (ret == -EFBIG) { if (ret == -EFBIG) {
u32 item_size; u32 item_size;
/* we found one, but it isn't big enough yet */ /* we found one, but it isn't big enough yet */
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
item_size = btrfs_item_size(leaf->items + path->slots[0]); item_size = btrfs_item_size_nr(leaf, path->slots[0]);
if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
/* already at max size, make a new one */ /* already at max size, make a new one */
goto insert; goto insert;
...@@ -188,8 +188,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -188,8 +188,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
goto insert; goto insert;
} }
path->slots[0]--; path->slots[0]--;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
csum_offset = (offset - found_key.offset) >> csum_offset = (offset - found_key.offset) >>
root->fs_info->sb->s_blocksize_bits; root->fs_info->sb->s_blocksize_bits;
if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
...@@ -197,10 +197,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -197,10 +197,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
csum_offset >= MAX_CSUM_ITEMS(root)) { csum_offset >= MAX_CSUM_ITEMS(root)) {
goto insert; goto insert;
} }
if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
BTRFS_CRC32_SIZE) { BTRFS_CRC32_SIZE) {
u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
diff = diff - btrfs_item_size(leaf->items + path->slots[0]); diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
if (diff != BTRFS_CRC32_SIZE) if (diff != BTRFS_CRC32_SIZE)
goto insert; goto insert;
ret = btrfs_extend_item(trans, root, path, diff); ret = btrfs_extend_item(trans, root, path, diff);
...@@ -220,21 +220,20 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -220,21 +220,20 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
goto fail; goto fail;
} }
csum: csum:
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], leaf = path->nodes[0];
struct btrfs_csum_item); item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
ret = 0; ret = 0;
item = (struct btrfs_csum_item *)((unsigned char *)item + item = (struct btrfs_csum_item *)((unsigned char *)item +
csum_offset * BTRFS_CRC32_SIZE); csum_offset * BTRFS_CRC32_SIZE);
found: found:
btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE, /* FIXME!!!!!!!!!!!! */
path->nodes[0]->b_data,
root->fs_info->sb->s_blocksize);
ret = btrfs_csum_data(root, data, len, &item->csum); ret = btrfs_csum_data(root, data, len, &item->csum);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(path->nodes[0]);
fail: fail:
btrfs_release_path(root, path); btrfs_release_path(root, path);
btrfs_free_path(path); btrfs_free_path(path);
return ret; return ret;
#endif
} }
int btrfs_csum_truncate(struct btrfs_trans_handle *trans, int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
...@@ -242,21 +241,21 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans, ...@@ -242,21 +241,21 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
u64 isize) u64 isize)
{ {
struct btrfs_key key; struct btrfs_key key;
struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]); struct extent_buffer *leaf = path->nodes[0];
int slot = path->slots[0]; int slot = path->slots[0];
int ret; int ret;
u32 new_item_size; u32 new_item_size;
u64 new_item_span; u64 new_item_span;
u64 blocks; u64 blocks;
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); btrfs_item_key_to_cpu(leaf, &key, slot);
if (isize <= key.offset) if (isize <= key.offset)
return 0; return 0;
new_item_span = isize - key.offset; new_item_span = isize - key.offset;
blocks = (new_item_span + root->blocksize - 1) >> blocks = (new_item_span + root->sectorsize - 1) >>
root->fs_info->sb->s_blocksize_bits; root->fs_info->sb->s_blocksize_bits;
new_item_size = blocks * BTRFS_CRC32_SIZE; new_item_size = blocks * BTRFS_CRC32_SIZE;
if (new_item_size >= btrfs_item_size(leaf->items + slot)) if (new_item_size >= btrfs_item_size_nr(leaf, slot))
return 0; return 0;
ret = btrfs_truncate_item(trans, root, path, new_item_size); ret = btrfs_truncate_item(trans, root, path, new_item_size);
BUG_ON(ret); BUG_ON(ret);
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
* Boston, MA 021110-1307, USA. * Boston, MA 021110-1307, USA.
*/ */
#include <linux/buffer_head.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/highmem.h> #include <linux/highmem.h>
...@@ -88,7 +87,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -88,7 +87,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
{ {
struct btrfs_key key; struct btrfs_key key;
struct btrfs_path *path; struct btrfs_path *path;
char *ptr, *kaddr; struct extent_buffer *leaf;
char *kaddr;
unsigned long ptr;
struct btrfs_file_extent_item *ei; struct btrfs_file_extent_item *ei;
u32 datasize; u32 datasize;
int err = 0; int err = 0;
...@@ -102,7 +103,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -102,7 +103,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.offset = offset; key.offset = offset;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
BUG_ON(size >= PAGE_CACHE_SIZE); BUG_ON(size >= PAGE_CACHE_SIZE);
datasize = btrfs_file_extent_calc_inline_size(size); datasize = btrfs_file_extent_calc_inline_size(size);
...@@ -113,18 +113,17 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -113,18 +113,17 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
err = ret; err = ret;
goto fail; goto fail;
} }
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], struct btrfs_file_extent_item); ei = btrfs_item_ptr(leaf, path->slots[0],
btrfs_set_file_extent_generation(ei, trans->transid); struct btrfs_file_extent_item);
btrfs_set_file_extent_type(ei, btrfs_set_file_extent_generation(leaf, ei, trans->transid);
BTRFS_FILE_EXTENT_INLINE); btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
ptr = btrfs_file_extent_inline_start(ei); ptr = btrfs_file_extent_inline_start(ei);
kaddr = kmap_atomic(page, KM_USER0); kaddr = kmap_atomic(page, KM_USER0);
btrfs_memcpy(root, path->nodes[0]->b_data, write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
ptr, kaddr + page_offset, size);
kunmap_atomic(kaddr, KM_USER0); kunmap_atomic(kaddr, KM_USER0);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
fail: fail:
btrfs_free_path(path); btrfs_free_path(path);
return err; return err;
...@@ -156,8 +155,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -156,8 +155,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
em->bdev = inode->i_sb->s_bdev; em->bdev = inode->i_sb->s_bdev;
start_pos = pos & ~((u64)root->blocksize - 1); start_pos = pos & ~((u64)root->sectorsize - 1);
num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >> num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
inode->i_blkbits; inode->i_blkbits;
down_read(&BTRFS_I(inode)->root->snap_sem); down_read(&BTRFS_I(inode)->root->snap_sem);
...@@ -184,7 +183,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -184,7 +183,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
if (inode->i_size < start_pos) { if (inode->i_size < start_pos) {
u64 last_pos_in_file; u64 last_pos_in_file;
u64 hole_size; u64 hole_size;
u64 mask = root->blocksize - 1; u64 mask = root->sectorsize - 1;
last_pos_in_file = (isize + mask) & ~mask; last_pos_in_file = (isize + mask) & ~mask;
hole_size = (start_pos - last_pos_in_file + mask) & ~mask; hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
...@@ -227,8 +226,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -227,8 +226,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
/* step one, delete the existing extents in this range */ /* step one, delete the existing extents in this range */
/* FIXME blocksize != pagesize */ /* FIXME blocksize != pagesize */
err = btrfs_drop_extents(trans, root, inode, start_pos, err = btrfs_drop_extents(trans, root, inode, start_pos,
(pos + write_bytes + root->blocksize -1) & (pos + write_bytes + root->sectorsize -1) &
~((u64)root->blocksize - 1), &hint_block); ~((u64)root->sectorsize - 1), &hint_block);
if (err) if (err)
goto failed; goto failed;
...@@ -288,7 +287,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -288,7 +287,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
{ {
int ret; int ret;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
int slot; int slot;
struct btrfs_file_extent_item *extent; struct btrfs_file_extent_item *extent;
u64 extent_end = 0; u64 extent_end = 0;
...@@ -327,10 +326,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -327,10 +326,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
found_extent = 0; found_extent = 0;
found_inline = 0; found_inline = 0;
extent = NULL; extent = NULL;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
ret = 0; ret = 0;
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.offset >= end || key.objectid != inode->i_ino) { if (key.offset >= end || key.objectid != inode->i_ino) {
goto out; goto out;
} }
...@@ -344,17 +343,18 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -344,17 +343,18 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
extent = btrfs_item_ptr(leaf, slot, extent = btrfs_item_ptr(leaf, slot,
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
found_type = btrfs_file_extent_type(extent); found_type = btrfs_file_extent_type(leaf, extent);
if (found_type == BTRFS_FILE_EXTENT_REG) { if (found_type == BTRFS_FILE_EXTENT_REG) {
extent_end = key.offset + extent_end = key.offset +
(btrfs_file_extent_num_blocks(extent) << (btrfs_file_extent_num_blocks(leaf, extent) <<
inode->i_blkbits); inode->i_blkbits);
found_extent = 1; found_extent = 1;
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) { } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
struct btrfs_item *item;
item = btrfs_item_nr(leaf, slot);
found_inline = 1; found_inline = 1;
extent_end = key.offset + extent_end = key.offset +
btrfs_file_extent_inline_len(leaf->items + btrfs_file_extent_inline_len(leaf, item);
slot);
} }
} else { } else {
extent_end = search_start; extent_end = search_start;
...@@ -365,8 +365,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -365,8 +365,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
search_start >= extent_end) { search_start >= extent_end) {
int nextret; int nextret;
u32 nritems; u32 nritems;
nritems = btrfs_header_nritems( nritems = btrfs_header_nritems(leaf);
btrfs_buffer_header(path->nodes[0]));
if (slot >= nritems - 1) { if (slot >= nritems - 1) {
nextret = btrfs_next_leaf(root, path); nextret = btrfs_next_leaf(root, path);
if (nextret) if (nextret)
...@@ -380,7 +379,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -380,7 +379,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
/* FIXME, there's only one inline extent allowed right now */ /* FIXME, there's only one inline extent allowed right now */
if (found_inline) { if (found_inline) {
u64 mask = root->blocksize - 1; u64 mask = root->sectorsize - 1;
search_start = (extent_end + mask) & ~mask; search_start = (extent_end + mask) & ~mask;
} else } else
search_start = extent_end; search_start = extent_end;
...@@ -388,10 +387,13 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -388,10 +387,13 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
if (end < extent_end && end >= key.offset) { if (end < extent_end && end >= key.offset) {
if (found_extent) { if (found_extent) {
u64 disk_blocknr = u64 disk_blocknr =
btrfs_file_extent_disk_blocknr(extent); btrfs_file_extent_disk_blocknr(leaf,extent);
u64 disk_num_blocks = u64 disk_num_blocks =
btrfs_file_extent_disk_num_blocks(extent); btrfs_file_extent_disk_num_blocks(leaf,
memcpy(&old, extent, sizeof(old)); extent);
read_extent_buffer(leaf, &old,
(unsigned long)extent,
sizeof(old));
if (disk_blocknr != 0) { if (disk_blocknr != 0) {
ret = btrfs_inc_extent_ref(trans, root, ret = btrfs_inc_extent_ref(trans, root,
disk_blocknr, disk_num_blocks); disk_blocknr, disk_num_blocks);
...@@ -406,20 +408,24 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -406,20 +408,24 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
u64 new_num; u64 new_num;
u64 old_num; u64 old_num;
keep = 1; keep = 1;
WARN_ON(start & (root->blocksize - 1)); WARN_ON(start & (root->sectorsize - 1));
if (found_extent) { if (found_extent) {
new_num = (start - key.offset) >> new_num = (start - key.offset) >>
inode->i_blkbits; inode->i_blkbits;
old_num = btrfs_file_extent_num_blocks(extent); old_num = btrfs_file_extent_num_blocks(leaf,
extent);
*hint_block = *hint_block =
btrfs_file_extent_disk_blocknr(extent); btrfs_file_extent_disk_blocknr(leaf,
if (btrfs_file_extent_disk_blocknr(extent)) { extent);
if (btrfs_file_extent_disk_blocknr(leaf,
extent)) {
inode->i_blocks -= inode->i_blocks -=
(old_num - new_num) << 3; (old_num - new_num) << 3;
} }
btrfs_set_file_extent_num_blocks(extent, btrfs_set_file_extent_num_blocks(leaf,
extent,
new_num); new_num);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
} else { } else {
WARN_ON(1); WARN_ON(1);
} }
...@@ -431,13 +437,17 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -431,13 +437,17 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
u64 extent_num_blocks = 0; u64 extent_num_blocks = 0;
if (found_extent) { if (found_extent) {
disk_blocknr = disk_blocknr =
btrfs_file_extent_disk_blocknr(extent); btrfs_file_extent_disk_blocknr(leaf,
extent);
disk_num_blocks = disk_num_blocks =
btrfs_file_extent_disk_num_blocks(extent); btrfs_file_extent_disk_num_blocks(leaf,
extent);
extent_num_blocks = extent_num_blocks =
btrfs_file_extent_num_blocks(extent); btrfs_file_extent_num_blocks(leaf,
extent);
*hint_block = *hint_block =
btrfs_file_extent_disk_blocknr(extent); btrfs_file_extent_disk_blocknr(leaf,
extent);
} }
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
/* TODO update progress marker and return */ /* TODO update progress marker and return */
...@@ -464,42 +474,37 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -464,42 +474,37 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
struct btrfs_key ins; struct btrfs_key ins;
ins.objectid = inode->i_ino; ins.objectid = inode->i_ino;
ins.offset = end; ins.offset = end;
ins.flags = 0;
btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
btrfs_release_path(root, path); btrfs_release_path(root, path);
ret = btrfs_insert_empty_item(trans, root, path, &ins, ret = btrfs_insert_empty_item(trans, root, path, &ins,
sizeof(*extent)); sizeof(*extent));
leaf = path->nodes[0];
if (ret) { if (ret) {
btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0])); btrfs_print_leaf(root, leaf);
printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.flags, ins.offset, start, end, key.offset, extent_end, keep); printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
} }
BUG_ON(ret); BUG_ON(ret);
extent = btrfs_item_ptr( extent = btrfs_item_ptr(leaf, path->slots[0],
btrfs_buffer_leaf(path->nodes[0]),
path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
btrfs_set_file_extent_disk_blocknr(extent, write_extent_buffer(leaf, &old,
btrfs_file_extent_disk_blocknr(&old)); (unsigned long)extent, sizeof(old));
btrfs_set_file_extent_disk_num_blocks(extent,
btrfs_file_extent_disk_num_blocks(&old));
btrfs_set_file_extent_offset(extent, btrfs_set_file_extent_offset(leaf, extent,
btrfs_file_extent_offset(&old) + le64_to_cpu(old.offset) +
((end - key.offset) >> inode->i_blkbits)); ((end - key.offset) >> inode->i_blkbits));
WARN_ON(btrfs_file_extent_num_blocks(&old) < WARN_ON(le64_to_cpu(old.num_blocks) <
(extent_end - end) >> inode->i_blkbits); (extent_end - end) >> inode->i_blkbits);
btrfs_set_file_extent_num_blocks(extent, btrfs_set_file_extent_num_blocks(leaf, extent,
(extent_end - end) >> inode->i_blkbits); (extent_end - end) >> inode->i_blkbits);
btrfs_set_file_extent_type(extent, btrfs_set_file_extent_type(leaf, extent,
BTRFS_FILE_EXTENT_REG); BTRFS_FILE_EXTENT_REG);
btrfs_set_file_extent_generation(extent,
btrfs_file_extent_generation(&old));
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(path->nodes[0]);
if (btrfs_file_extent_disk_blocknr(&old) != 0) { if (le64_to_cpu(old.disk_blocknr) != 0) {
inode->i_blocks += inode->i_blocks +=
btrfs_file_extent_num_blocks(extent) << 3; btrfs_file_extent_num_blocks(leaf,
extent) << 3;
} }
ret = 0; ret = 0;
goto out; goto out;
...@@ -529,8 +534,8 @@ static int prepare_pages(struct btrfs_root *root, ...@@ -529,8 +534,8 @@ static int prepare_pages(struct btrfs_root *root,
u64 num_blocks; u64 num_blocks;
u64 start_pos; u64 start_pos;
start_pos = pos & ~((u64)root->blocksize - 1); start_pos = pos & ~((u64)root->sectorsize - 1);
num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >> num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
inode->i_blkbits; inode->i_blkbits;
memset(pages, 0, num_pages * sizeof(struct page *)); memset(pages, 0, num_pages * sizeof(struct page *));
......
...@@ -20,24 +20,18 @@ ...@@ -20,24 +20,18 @@
#include "disk-io.h" #include "disk-io.h"
#include "transaction.h" #include "transaction.h"
int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
*root, u64 objectid, struct btrfs_inode_item struct btrfs_root *root,
*inode_item) struct btrfs_path *path, u64 objectid)
{ {
struct btrfs_path *path;
struct btrfs_key key; struct btrfs_key key;
int ret; int ret;
key.objectid = objectid; key.objectid = objectid;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
key.offset = 0; key.offset = 0;
path = btrfs_alloc_path(); ret = btrfs_insert_empty_item(trans, root, path, &key,
BUG_ON(!path); sizeof(struct btrfs_inode_item));
ret = btrfs_insert_item(trans, root, &key, inode_item,
sizeof(*inode_item));
btrfs_release_path(root, path);
btrfs_free_path(path);
if (ret == 0 && objectid > root->highest_inode) if (ret == 0 && objectid > root->highest_inode)
root->highest_inode = objectid; root->highest_inode = objectid;
return ret; return ret;
...@@ -51,15 +45,15 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -51,15 +45,15 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
int cow = mod != 0; int cow = mod != 0;
int ret; int ret;
int slot; int slot;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct btrfs_key found_key; struct btrfs_key found_key;
ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY && if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
location->offset == (u64)-1 && path->slots[0] != 0) { location->offset == (u64)-1 && path->slots[0] != 0) {
slot = path->slots[0] - 1; slot = path->slots[0] - 1;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key); btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (found_key.objectid == location->objectid && if (found_key.objectid == location->objectid &&
btrfs_key_type(&found_key) == btrfs_key_type(location)) { btrfs_key_type(&found_key) == btrfs_key_type(location)) {
path->slots[0]--; path->slots[0]--;
......
...@@ -24,8 +24,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) ...@@ -24,8 +24,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
{ {
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
struct btrfs_leaf *l; struct extent_buffer *l;
struct btrfs_key search_key; struct btrfs_key search_key;
struct btrfs_key found_key;
int slot; int slot;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -39,8 +40,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) ...@@ -39,8 +40,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
BUG_ON(ret == 0); BUG_ON(ret == 0);
if (path->slots[0] > 0) { if (path->slots[0] > 0) {
slot = path->slots[0] - 1; slot = path->slots[0] - 1;
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
*objectid = btrfs_disk_key_objectid(&l->items[slot].key); btrfs_item_key_to_cpu(l, &found_key, slot);
*objectid = found_key.objectid;
} else { } else {
*objectid = BTRFS_FIRST_FREE_OBJECTID; *objectid = BTRFS_FIRST_FREE_OBJECTID;
} }
...@@ -64,13 +66,12 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, ...@@ -64,13 +66,12 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
int slot = 0; int slot = 0;
u64 last_ino = 0; u64 last_ino = 0;
int start_found; int start_found;
struct btrfs_leaf *l; struct extent_buffer *l;
struct btrfs_key search_key; struct btrfs_key search_key;
u64 search_start = dirid; u64 search_start = dirid;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
search_key.flags = 0;
search_start = root->last_inode_alloc; search_start = root->last_inode_alloc;
search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID); search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
search_key.objectid = search_start; search_key.objectid = search_start;
...@@ -86,9 +87,9 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, ...@@ -86,9 +87,9 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
path->slots[0]--; path->slots[0]--;
while (1) { while (1) {
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
if (slot >= btrfs_header_nritems(&l->header)) { if (slot >= btrfs_header_nritems(l)) {
ret = btrfs_next_leaf(root, path); ret = btrfs_next_leaf(root, path);
if (ret == 0) if (ret == 0)
continue; continue;
...@@ -103,7 +104,7 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, ...@@ -103,7 +104,7 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
last_ino : search_start; last_ino : search_start;
goto found; goto found;
} }
btrfs_disk_key_to_cpu(&key, &l->items[slot].key); btrfs_item_key_to_cpu(l, &key, slot);
if (key.objectid >= search_start) { if (key.objectid >= search_start) {
if (start_found) { if (start_found) {
if (last_ino < search_start) if (last_ino < search_start)
......
...@@ -159,10 +159,8 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end) ...@@ -159,10 +159,8 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end) int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
{ {
char csum[BTRFS_CRC32_SIZE];
size_t offset = start - (page->index << PAGE_CACHE_SHIFT); size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
struct inode *inode = page->mapping->host; struct inode *inode = page->mapping->host;
struct btrfs_root *root = BTRFS_I(inode)->root;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
char *kaddr; char *kaddr;
u64 private; u64 private;
...@@ -173,11 +171,15 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end) ...@@ -173,11 +171,15 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
if (ret) { if (ret) {
goto zeroit; goto zeroit;
} }
/*
struct btrfs_root *root = BTRFS_I(inode)->root;
char csum[BTRFS_CRC32_SIZE];
ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum); ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
BUG_ON(ret); BUG_ON(ret);
if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) { if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
goto zeroit; goto zeroit;
} }
*/
kunmap_atomic(kaddr, KM_IRQ0); kunmap_atomic(kaddr, KM_IRQ0);
return 0; return 0;
...@@ -192,7 +194,9 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end) ...@@ -192,7 +194,9 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
void btrfs_read_locked_inode(struct inode *inode) void btrfs_read_locked_inode(struct inode *inode)
{ {
struct btrfs_path *path; struct btrfs_path *path;
struct extent_buffer *leaf;
struct btrfs_inode_item *inode_item; struct btrfs_inode_item *inode_item;
struct btrfs_inode_timespec *tspec;
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_key location; struct btrfs_key location;
u64 alloc_group_block; u64 alloc_group_block;
...@@ -205,29 +209,37 @@ void btrfs_read_locked_inode(struct inode *inode) ...@@ -205,29 +209,37 @@ void btrfs_read_locked_inode(struct inode *inode)
memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
ret = btrfs_lookup_inode(NULL, root, path, &location, 0); ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
if (ret) { if (ret)
goto make_bad; goto make_bad;
}
inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], inode_item = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_inode_item); struct btrfs_inode_item);
inode->i_mode = btrfs_inode_mode(inode_item); inode->i_mode = btrfs_inode_mode(leaf, inode_item);
inode->i_nlink = btrfs_inode_nlink(inode_item); inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
inode->i_uid = btrfs_inode_uid(inode_item); inode->i_uid = btrfs_inode_uid(leaf, inode_item);
inode->i_gid = btrfs_inode_gid(inode_item); inode->i_gid = btrfs_inode_gid(leaf, inode_item);
inode->i_size = btrfs_inode_size(inode_item); inode->i_size = btrfs_inode_size(leaf, inode_item);
inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); tspec = btrfs_inode_atime(inode_item);
inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); tspec = btrfs_inode_mtime(inode_item);
inode->i_blocks = btrfs_inode_nblocks(inode_item); inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
inode->i_generation = btrfs_inode_generation(inode_item); inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
tspec = btrfs_inode_ctime(inode_item);
inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
inode->i_generation = btrfs_inode_generation(leaf, inode_item);
inode->i_rdev = 0; inode->i_rdev = 0;
rdev = btrfs_inode_rdev(inode_item); rdev = btrfs_inode_rdev(leaf, inode_item);
alloc_group_block = btrfs_inode_block_group(inode_item);
alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
alloc_group_block); alloc_group_block);
...@@ -267,24 +279,35 @@ void btrfs_read_locked_inode(struct inode *inode) ...@@ -267,24 +279,35 @@ void btrfs_read_locked_inode(struct inode *inode)
make_bad_inode(inode); make_bad_inode(inode);
} }
static void fill_inode_item(struct btrfs_inode_item *item, static void fill_inode_item(struct extent_buffer *leaf,
struct btrfs_inode_item *item,
struct inode *inode) struct inode *inode)
{ {
btrfs_set_inode_uid(item, inode->i_uid); btrfs_set_inode_uid(leaf, item, inode->i_uid);
btrfs_set_inode_gid(item, inode->i_gid); btrfs_set_inode_gid(leaf, item, inode->i_gid);
btrfs_set_inode_size(item, inode->i_size); btrfs_set_inode_size(leaf, item, inode->i_size);
btrfs_set_inode_mode(item, inode->i_mode); btrfs_set_inode_mode(leaf, item, inode->i_mode);
btrfs_set_inode_nlink(item, inode->i_nlink); btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); inode->i_atime.tv_sec);
btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); inode->i_atime.tv_nsec);
btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
btrfs_set_inode_nblocks(item, inode->i_blocks); btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
btrfs_set_inode_generation(item, inode->i_generation); inode->i_mtime.tv_sec);
btrfs_set_inode_rdev(item, inode->i_rdev); btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
btrfs_set_inode_block_group(item, inode->i_mtime.tv_nsec);
btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
inode->i_ctime.tv_sec);
btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
inode->i_ctime.tv_nsec);
btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
btrfs_set_inode_generation(leaf, item, inode->i_generation);
btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
btrfs_set_inode_block_group(leaf, item,
BTRFS_I(inode)->block_group->key.objectid); BTRFS_I(inode)->block_group->key.objectid);
} }
...@@ -294,6 +317,7 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans, ...@@ -294,6 +317,7 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
{ {
struct btrfs_inode_item *inode_item; struct btrfs_inode_item *inode_item;
struct btrfs_path *path; struct btrfs_path *path;
struct extent_buffer *leaf;
int ret; int ret;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -306,12 +330,12 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans, ...@@ -306,12 +330,12 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
goto failed; goto failed;
} }
inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], inode_item = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_inode_item); struct btrfs_inode_item);
fill_inode_item(inode_item, inode); fill_inode_item(leaf, inode_item, inode);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
btrfs_set_inode_last_trans(trans, inode); btrfs_set_inode_last_trans(trans, inode);
ret = 0; ret = 0;
failed: failed:
...@@ -330,8 +354,9 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, ...@@ -330,8 +354,9 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
const char *name = dentry->d_name.name; const char *name = dentry->d_name.name;
int name_len = dentry->d_name.len; int name_len = dentry->d_name.len;
int ret = 0; int ret = 0;
u64 objectid; struct extent_buffer *leaf;
struct btrfs_dir_item *di; struct btrfs_dir_item *di;
struct btrfs_key key;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) { if (!path) {
...@@ -349,14 +374,15 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, ...@@ -349,14 +374,15 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
ret = -ENOENT; ret = -ENOENT;
goto err; goto err;
} }
objectid = btrfs_disk_key_objectid(&di->location); leaf = path->nodes[0];
btrfs_dir_item_key_to_cpu(leaf, di, &key);
ret = btrfs_delete_one_dir_name(trans, root, path, di); ret = btrfs_delete_one_dir_name(trans, root, path, di);
if (ret) if (ret)
goto err; goto err;
btrfs_release_path(root, path); btrfs_release_path(root, path);
di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
objectid, name, name_len, -1); key.objectid, name, name_len, -1);
if (IS_ERR(di)) { if (IS_ERR(di)) {
ret = PTR_ERR(di); ret = PTR_ERR(di);
goto err; goto err;
...@@ -391,12 +417,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) ...@@ -391,12 +417,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
root = BTRFS_I(dir)->root; root = BTRFS_I(dir)->root;
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, dir); btrfs_set_trans_block_group(trans, dir);
ret = btrfs_unlink_trans(trans, root, dir, dentry); ret = btrfs_unlink_trans(trans, root, dir, dentry);
nr = trans->blocks_used; nr = trans->blocks_used;
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
btrfs_btree_balance_dirty(root, nr); btrfs_btree_balance_dirty(root, nr);
return ret; return ret;
} }
...@@ -411,7 +440,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -411,7 +440,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct btrfs_key found_key; struct btrfs_key found_key;
int found_type; int found_type;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
char *goodnames = ".."; char *goodnames = "..";
unsigned long nr; unsigned long nr;
...@@ -419,10 +448,11 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -419,10 +448,11 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
BUG_ON(!path); BUG_ON(!path);
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, dir); btrfs_set_trans_block_group(trans, dir);
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.offset = (u64)-1; key.offset = (u64)-1;
key.flags = (u32)-1; key.type = (u8)-1;
while(1) { while(1) {
ret = btrfs_search_slot(trans, root, &key, path, -1, 1); ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
if (ret < 0) { if (ret < 0) {
...@@ -435,9 +465,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -435,9 +465,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
goto out; goto out;
} }
path->slots[0]--; path->slots[0]--;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
btrfs_disk_key_to_cpu(&found_key, btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
&leaf->items[path->slots[0]].key);
found_type = btrfs_key_type(&found_key); found_type = btrfs_key_type(&found_key);
if (found_key.objectid != inode->i_ino) { if (found_key.objectid != inode->i_ino) {
err = -ENOENT; err = -ENOENT;
...@@ -513,9 +542,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, ...@@ -513,9 +542,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
int ret; int ret;
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_disk_key *found_key; struct btrfs_key found_key;
u32 found_type; u32 found_type;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct btrfs_file_extent_item *fi; struct btrfs_file_extent_item *fi;
u64 extent_start = 0; u64 extent_start = 0;
u64 extent_num_blocks = 0; u64 extent_num_blocks = 0;
...@@ -527,10 +556,12 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, ...@@ -527,10 +556,12 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
path = btrfs_alloc_path(); path = btrfs_alloc_path();
path->reada = -1; path->reada = -1;
BUG_ON(!path); BUG_ON(!path);
/* FIXME, add redo link to tree so we don't leak on crash */ /* FIXME, add redo link to tree so we don't leak on crash */
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.offset = (u64)-1; key.offset = (u64)-1;
key.flags = (u32)-1; key.type = (u8)-1;
while(1) { while(1) {
btrfs_init_path(path); btrfs_init_path(path);
fi = NULL; fi = NULL;
...@@ -542,26 +573,27 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, ...@@ -542,26 +573,27 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
BUG_ON(path->slots[0] == 0); BUG_ON(path->slots[0] == 0);
path->slots[0]--; path->slots[0]--;
} }
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
found_key = &leaf->items[path->slots[0]].key; btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
found_type = btrfs_disk_key_type(found_key); found_type = btrfs_key_type(&found_key);
if (btrfs_disk_key_objectid(found_key) != inode->i_ino) if (found_key.objectid != inode->i_ino)
break; break;
if (found_type != BTRFS_CSUM_ITEM_KEY && if (found_type != BTRFS_CSUM_ITEM_KEY &&
found_type != BTRFS_DIR_ITEM_KEY && found_type != BTRFS_DIR_ITEM_KEY &&
found_type != BTRFS_DIR_INDEX_KEY && found_type != BTRFS_DIR_INDEX_KEY &&
found_type != BTRFS_EXTENT_DATA_KEY) found_type != BTRFS_EXTENT_DATA_KEY)
break; break;
item_end = btrfs_disk_key_offset(found_key); item_end = found_key.offset;
if (found_type == BTRFS_EXTENT_DATA_KEY) { if (found_type == BTRFS_EXTENT_DATA_KEY) {
fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), fi = btrfs_item_ptr(leaf, path->slots[0],
path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
if (btrfs_file_extent_type(fi) != if (btrfs_file_extent_type(leaf, fi) !=
BTRFS_FILE_EXTENT_INLINE) { BTRFS_FILE_EXTENT_INLINE) {
item_end += btrfs_file_extent_num_blocks(fi) << item_end +=
btrfs_file_extent_num_blocks(leaf, fi) <<
inode->i_blkbits; inode->i_blkbits;
} }
} }
...@@ -583,7 +615,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, ...@@ -583,7 +615,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
btrfs_set_key_type(&key, found_type); btrfs_set_key_type(&key, found_type);
continue; continue;
} }
if (btrfs_disk_key_offset(found_key) >= inode->i_size) if (found_key.offset >= inode->i_size)
del_item = 1; del_item = 1;
else else
del_item = 0; del_item = 0;
...@@ -591,30 +623,31 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, ...@@ -591,30 +623,31 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
/* FIXME, shrink the extent if the ref count is only 1 */ /* FIXME, shrink the extent if the ref count is only 1 */
if (found_type == BTRFS_EXTENT_DATA_KEY && if (found_type == BTRFS_EXTENT_DATA_KEY &&
btrfs_file_extent_type(fi) != btrfs_file_extent_type(leaf, fi) !=
BTRFS_FILE_EXTENT_INLINE) { BTRFS_FILE_EXTENT_INLINE) {
u64 num_dec; u64 num_dec;
extent_start = btrfs_file_extent_disk_blocknr(fi); extent_start = btrfs_file_extent_disk_blocknr(leaf, fi);
if (!del_item) { if (!del_item) {
u64 orig_num_blocks = u64 orig_num_blocks =
btrfs_file_extent_num_blocks(fi); btrfs_file_extent_num_blocks(leaf, fi);
extent_num_blocks = inode->i_size - extent_num_blocks = inode->i_size -
btrfs_disk_key_offset(found_key) + found_key.offset + root->sectorsize - 1;
root->blocksize - 1;
extent_num_blocks >>= inode->i_blkbits; extent_num_blocks >>= inode->i_blkbits;
btrfs_set_file_extent_num_blocks(fi, btrfs_set_file_extent_num_blocks(leaf, fi,
extent_num_blocks); extent_num_blocks);
num_dec = (orig_num_blocks - num_dec = (orig_num_blocks -
extent_num_blocks) << 3; extent_num_blocks) << 3;
if (extent_start != 0) { if (extent_start != 0) {
inode->i_blocks -= num_dec; inode->i_blocks -= num_dec;
} }
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(leaf);
} else { } else {
extent_num_blocks = extent_num_blocks =
btrfs_file_extent_disk_num_blocks(fi); btrfs_file_extent_disk_num_blocks(leaf,
fi);
/* FIXME blocksize != 4096 */ /* FIXME blocksize != 4096 */
num_dec = btrfs_file_extent_num_blocks(fi) << 3; num_dec = btrfs_file_extent_num_blocks(leaf,
fi) << 3;
if (extent_start != 0) { if (extent_start != 0) {
found_extent = 1; found_extent = 1;
inode->i_blocks -= num_dec; inode->i_blocks -= num_dec;
...@@ -725,7 +758,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -725,7 +758,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
u64 mask = root->blocksize - 1; u64 mask = root->sectorsize - 1;
u64 pos = (inode->i_size + mask) & ~mask; u64 pos = (inode->i_size + mask) & ~mask;
u64 block_end = attr->ia_size | mask; u64 block_end = attr->ia_size | mask;
u64 hole_size; u64 hole_size;
...@@ -771,9 +804,11 @@ void btrfs_delete_inode(struct inode *inode) ...@@ -771,9 +804,11 @@ void btrfs_delete_inode(struct inode *inode)
if (is_bad_inode(inode)) { if (is_bad_inode(inode)) {
goto no_delete; goto no_delete;
} }
inode->i_size = 0; inode->i_size = 0;
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, inode); btrfs_set_trans_block_group(trans, inode);
ret = btrfs_truncate_in_trans(trans, root, inode); ret = btrfs_truncate_in_trans(trans, root, inode);
if (ret) if (ret)
...@@ -782,6 +817,7 @@ void btrfs_delete_inode(struct inode *inode) ...@@ -782,6 +817,7 @@ void btrfs_delete_inode(struct inode *inode)
if (ret) if (ret)
goto no_delete_lock; goto no_delete_lock;
nr = trans->blocks_used; nr = trans->blocks_used;
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
btrfs_btree_balance_dirty(root, nr); btrfs_btree_balance_dirty(root, nr);
...@@ -819,7 +855,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, ...@@ -819,7 +855,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
ret = 0; ret = 0;
goto out; goto out;
} }
btrfs_disk_key_to_cpu(location, &di->location); btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
out: out:
btrfs_release_path(root, path); btrfs_release_path(root, path);
btrfs_free_path(path); btrfs_free_path(path);
...@@ -856,7 +892,6 @@ static int fixup_tree_root_location(struct btrfs_root *root, ...@@ -856,7 +892,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
ri = &(*sub_root)->root_item; ri = &(*sub_root)->root_item;
location->objectid = btrfs_root_dirid(ri); location->objectid = btrfs_root_dirid(ri);
location->flags = 0;
btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
location->offset = 0; location->offset = 0;
...@@ -908,11 +943,14 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, ...@@ -908,11 +943,14 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
if (dentry->d_name.len > BTRFS_NAME_LEN) if (dentry->d_name.len > BTRFS_NAME_LEN)
return ERR_PTR(-ENAMETOOLONG); return ERR_PTR(-ENAMETOOLONG);
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
ret = btrfs_inode_by_name(dir, dentry, &location); ret = btrfs_inode_by_name(dir, dentry, &location);
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
if (ret < 0) if (ret < 0)
return ERR_PTR(ret); return ERR_PTR(ret);
inode = NULL; inode = NULL;
if (location.objectid) { if (location.objectid) {
ret = fixup_tree_root_location(root, &location, &sub_root, ret = fixup_tree_root_location(root, &location, &sub_root,
...@@ -952,10 +990,11 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -952,10 +990,11 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
struct btrfs_item *item; struct btrfs_item *item;
struct btrfs_dir_item *di; struct btrfs_dir_item *di;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_key found_key;
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
u32 nritems; u32 nritems;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
int slot; int slot;
int advance; int advance;
unsigned char d_type; unsigned char d_type;
...@@ -964,15 +1003,19 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -964,15 +1003,19 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
u32 di_total; u32 di_total;
u32 di_len; u32 di_len;
int key_type = BTRFS_DIR_INDEX_KEY; int key_type = BTRFS_DIR_INDEX_KEY;
char tmp_name[32];
char *name_ptr;
int name_len;
/* FIXME, use a real flag for deciding about the key type */ /* FIXME, use a real flag for deciding about the key type */
if (root->fs_info->tree_root == root) if (root->fs_info->tree_root == root)
key_type = BTRFS_DIR_ITEM_KEY; key_type = BTRFS_DIR_ITEM_KEY;
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.flags = 0;
btrfs_set_key_type(&key, key_type); btrfs_set_key_type(&key, key_type);
key.offset = filp->f_pos; key.offset = filp->f_pos;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
path->reada = 2; path->reada = 2;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
...@@ -980,16 +1023,16 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -980,16 +1023,16 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
goto err; goto err;
advance = 0; advance = 0;
while(1) { while(1) {
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
slot = path->slots[0]; slot = path->slots[0];
if (advance || slot >= nritems) { if (advance || slot >= nritems) {
if (slot >= nritems -1) { if (slot >= nritems -1) {
ret = btrfs_next_leaf(root, path); ret = btrfs_next_leaf(root, path);
if (ret) if (ret)
break; break;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
slot = path->slots[0]; slot = path->slots[0];
} else { } else {
slot++; slot++;
...@@ -997,28 +1040,48 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -997,28 +1040,48 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
} }
} }
advance = 1; advance = 1;
item = leaf->items + slot; item = btrfs_item_nr(leaf, slot);
if (btrfs_disk_key_objectid(&item->key) != key.objectid) btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (found_key.objectid != key.objectid)
break; break;
if (btrfs_disk_key_type(&item->key) != key_type) if (btrfs_key_type(&found_key) != key_type)
break; break;
if (btrfs_disk_key_offset(&item->key) < filp->f_pos) if (found_key.offset < filp->f_pos)
continue; continue;
filp->f_pos = btrfs_disk_key_offset(&item->key);
filp->f_pos = found_key.offset;
advance = 1; advance = 1;
di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
di_cur = 0; di_cur = 0;
di_total = btrfs_item_size(leaf->items + slot); di_total = btrfs_item_size(leaf, item);
while(di_cur < di_total) { while(di_cur < di_total) {
d_type = btrfs_filetype_table[btrfs_dir_type(di)]; struct btrfs_key location;
over = filldir(dirent, (const char *)(di + 1),
btrfs_dir_name_len(di), name_len = btrfs_dir_name_len(leaf, di);
btrfs_disk_key_offset(&item->key), if (name_len < 32) {
btrfs_disk_key_objectid(&di->location), name_ptr = tmp_name;
} else {
name_ptr = kmalloc(name_len, GFP_NOFS);
BUG_ON(!name_ptr);
}
read_extent_buffer(leaf, name_ptr,
(unsigned long)(di + 1), name_len);
d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
btrfs_dir_item_key_to_cpu(leaf, di, &location);
over = filldir(dirent, name_ptr, name_len,
found_key.offset,
location.objectid,
d_type); d_type);
if (name_ptr != tmp_name)
kfree(name_ptr);
if (over) if (over)
goto nopos; goto nopos;
di_len = btrfs_dir_name_len(di) + sizeof(*di); di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
di_cur += di_len; di_cur += di_len;
di = (struct btrfs_dir_item *)((char *)di + di_len); di = (struct btrfs_dir_item *)((char *)di + di_len);
} }
...@@ -1075,11 +1138,15 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, ...@@ -1075,11 +1138,15 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
int mode) int mode)
{ {
struct inode *inode; struct inode *inode;
struct btrfs_inode_item inode_item; struct btrfs_inode_item *inode_item;
struct btrfs_key *location; struct btrfs_key *location;
struct btrfs_path *path;
int ret; int ret;
int owner; int owner;
path = btrfs_alloc_path();
BUG_ON(!path);
inode = new_inode(root->fs_info->sb); inode = new_inode(root->fs_info->sb);
if (!inode) if (!inode)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
...@@ -1095,24 +1162,32 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, ...@@ -1095,24 +1162,32 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
group = btrfs_find_block_group(root, group, 0, 0, owner); group = btrfs_find_block_group(root, group, 0, 0, owner);
BTRFS_I(inode)->block_group = group; BTRFS_I(inode)->block_group = group;
ret = btrfs_insert_empty_inode(trans, root, path, objectid);
if (ret)
goto fail;
inode->i_uid = current->fsuid; inode->i_uid = current->fsuid;
inode->i_gid = current->fsgid; inode->i_gid = current->fsgid;
inode->i_mode = mode; inode->i_mode = mode;
inode->i_ino = objectid; inode->i_ino = objectid;
inode->i_blocks = 0; inode->i_blocks = 0;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
fill_inode_item(&inode_item, inode); inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
fill_inode_item(path->nodes[0], inode_item, inode);
btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_free_path(path);
location = &BTRFS_I(inode)->location; location = &BTRFS_I(inode)->location;
location->objectid = objectid; location->objectid = objectid;
location->flags = 0;
location->offset = 0; location->offset = 0;
btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
if (ret)
return ERR_PTR(ret);
insert_inode_hash(inode); insert_inode_hash(inode);
return inode; return inode;
fail:
btrfs_free_path(path);
return ERR_PTR(ret);
} }
static inline u8 btrfs_inode_type(struct inode *inode) static inline u8 btrfs_inode_type(struct inode *inode)
...@@ -1127,8 +1202,8 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans, ...@@ -1127,8 +1202,8 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans,
struct btrfs_key key; struct btrfs_key key;
struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
struct inode *parent_inode; struct inode *parent_inode;
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
key.offset = 0; key.offset = 0;
...@@ -1285,14 +1360,18 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, ...@@ -1285,14 +1360,18 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
inc_nlink(inode); inc_nlink(inode);
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, dir); btrfs_set_trans_block_group(trans, dir);
atomic_inc(&inode->i_count); atomic_inc(&inode->i_count);
err = btrfs_add_nondir(trans, dentry, inode); err = btrfs_add_nondir(trans, dentry, inode);
if (err) if (err)
drop_inode = 1; drop_inode = 1;
dir->i_sb->s_dirt = 1; dir->i_sb->s_dirt = 1;
btrfs_update_inode_block_group(trans, dir); btrfs_update_inode_block_group(trans, dir);
err = btrfs_update_inode(trans, root, inode); err = btrfs_update_inode(trans, root, inode);
if (err) if (err)
drop_inode = 1; drop_inode = 1;
...@@ -1321,13 +1400,13 @@ static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, ...@@ -1321,13 +1400,13 @@ static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
key.objectid = objectid; key.objectid = objectid;
key.offset = 0; key.offset = 0;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
&key, BTRFS_FT_DIR); &key, BTRFS_FT_DIR);
if (ret) if (ret)
goto error; goto error;
key.objectid = dirid; key.objectid = dirid;
ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
&key, BTRFS_FT_DIR); &key, BTRFS_FT_DIR);
...@@ -1350,6 +1429,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) ...@@ -1350,6 +1429,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, dir); btrfs_set_trans_block_group(trans, dir);
if (IS_ERR(trans)) { if (IS_ERR(trans)) {
err = PTR_ERR(trans); err = PTR_ERR(trans);
goto out_unlock; goto out_unlock;
...@@ -1367,6 +1447,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) ...@@ -1367,6 +1447,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
err = PTR_ERR(inode); err = PTR_ERR(inode);
goto out_fail; goto out_fail;
} }
drop_on_err = 1; drop_on_err = 1;
inode->i_op = &btrfs_dir_inode_operations; inode->i_op = &btrfs_dir_inode_operations;
inode->i_fop = &btrfs_dir_file_operations; inode->i_fop = &btrfs_dir_file_operations;
...@@ -1380,9 +1461,11 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) ...@@ -1380,9 +1461,11 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
err = btrfs_update_inode(trans, root, inode); err = btrfs_update_inode(trans, root, inode);
if (err) if (err)
goto out_fail; goto out_fail;
err = btrfs_add_link(trans, dentry, inode); err = btrfs_add_link(trans, dentry, inode);
if (err) if (err)
goto out_fail; goto out_fail;
d_instantiate(dentry, inode); d_instantiate(dentry, inode);
drop_on_err = 0; drop_on_err = 0;
dir->i_sb->s_dirt = 1; dir->i_sb->s_dirt = 1;
...@@ -1392,6 +1475,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) ...@@ -1392,6 +1475,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
out_fail: out_fail:
nr = trans->blocks_used; nr = trans->blocks_used;
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
out_unlock: out_unlock:
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
if (drop_on_err) if (drop_on_err)
...@@ -1415,8 +1499,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1415,8 +1499,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_file_extent_item *item; struct btrfs_file_extent_item *item;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
struct btrfs_disk_key *found_key; struct btrfs_key found_key;
struct extent_map *em = NULL; struct extent_map *em = NULL;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
struct btrfs_trans_handle *trans = NULL; struct btrfs_trans_handle *trans = NULL;
...@@ -1436,8 +1520,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1436,8 +1520,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
err = -ENOMEM; err = -ENOMEM;
goto out; goto out;
} }
em->start = 0; em->start = EXTENT_MAP_HOLE;
em->end = 0; em->end = EXTENT_MAP_HOLE;
} }
em->bdev = inode->i_sb->s_bdev; em->bdev = inode->i_sb->s_bdev;
ret = btrfs_lookup_file_extent(NULL, root, path, ret = btrfs_lookup_file_extent(NULL, root, path,
...@@ -1453,25 +1537,27 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1453,25 +1537,27 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
path->slots[0]--; path->slots[0]--;
} }
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], leaf = path->nodes[0];
item = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
leaf = btrfs_buffer_leaf(path->nodes[0]);
blocknr = btrfs_file_extent_disk_blocknr(item); blocknr = btrfs_file_extent_disk_blocknr(leaf, item);
blocknr += btrfs_file_extent_offset(item); blocknr += btrfs_file_extent_offset(leaf, item);
/* are we inside the extent that was found? */ /* are we inside the extent that was found? */
found_key = &leaf->items[path->slots[0]].key; btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
found_type = btrfs_disk_key_type(found_key); found_type = btrfs_key_type(&found_key);
if (btrfs_disk_key_objectid(found_key) != objectid || if (found_key.objectid != objectid ||
found_type != BTRFS_EXTENT_DATA_KEY) { found_type != BTRFS_EXTENT_DATA_KEY) {
goto not_found; goto not_found;
} }
found_type = btrfs_file_extent_type(item); found_type = btrfs_file_extent_type(leaf, item);
extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); extent_start = found_key.offset;
if (found_type == BTRFS_FILE_EXTENT_REG) { if (found_type == BTRFS_FILE_EXTENT_REG) {
extent_end = extent_start + extent_end = extent_start +
(btrfs_file_extent_num_blocks(item) << inode->i_blkbits); (btrfs_file_extent_num_blocks(leaf, item) <<
inode->i_blkbits);
err = 0; err = 0;
if (start < extent_start || start >= extent_end) { if (start < extent_start || start >= extent_end) {
em->start = start; em->start = start;
...@@ -1484,28 +1570,29 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1484,28 +1570,29 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
} }
goto not_found_em; goto not_found_em;
} }
if (btrfs_file_extent_disk_blocknr(item) == 0) { if (btrfs_file_extent_disk_blocknr(leaf, item) == 0) {
em->start = extent_start; em->start = extent_start;
em->end = extent_end - 1; em->end = extent_end - 1;
em->block_start = 0; em->block_start = EXTENT_MAP_HOLE;
em->block_end = 0; em->block_end = EXTENT_MAP_HOLE;
goto insert; goto insert;
} }
em->block_start = blocknr << inode->i_blkbits; em->block_start = blocknr << inode->i_blkbits;
em->block_end = em->block_start + em->block_end = em->block_start +
(btrfs_file_extent_num_blocks(item) << (btrfs_file_extent_num_blocks(leaf, item) <<
inode->i_blkbits) - 1; inode->i_blkbits) - 1;
em->start = extent_start; em->start = extent_start;
em->end = extent_end - 1; em->end = extent_end - 1;
goto insert; goto insert;
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) { } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
char *ptr; unsigned long ptr;
char *map; char *map;
u32 size; u32 size;
size = btrfs_file_extent_inline_len(leaf->items + size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
path->slots[0]); path->slots[0]));
extent_end = extent_start | ((u64)root->blocksize - 1);
extent_end = extent_start | ((u64)root->sectorsize - 1);
if (start < extent_start || start >= extent_end) { if (start < extent_start || start >= extent_end) {
em->start = start; em->start = start;
if (start < extent_start) { if (start < extent_start) {
...@@ -1517,18 +1604,21 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1517,18 +1604,21 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
} }
goto not_found_em; goto not_found_em;
} }
em->block_start = EXTENT_MAP_INLINE; em->block_start = EXTENT_MAP_INLINE;
em->block_end = EXTENT_MAP_INLINE; em->block_end = EXTENT_MAP_INLINE;
em->start = extent_start; em->start = extent_start;
em->end = extent_end; em->end = extent_end;
if (!page) { if (!page) {
goto insert; goto insert;
} }
ptr = btrfs_file_extent_inline_start(item); ptr = btrfs_file_extent_inline_start(item);
map = kmap(page); map = kmap(page);
memcpy(map + page_offset, ptr, size); read_extent_buffer(leaf, map + page_offset, ptr, size);
memset(map + page_offset + size, 0, memset(map + page_offset + size, 0,
root->blocksize - (page_offset + size)); root->sectorsize - (page_offset + size));
flush_dcache_page(page); flush_dcache_page(page);
kunmap(page); kunmap(page);
set_extent_uptodate(em_tree, extent_start, set_extent_uptodate(em_tree, extent_start,
...@@ -1542,8 +1632,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -1542,8 +1632,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
em->start = start; em->start = start;
em->end = end; em->end = end;
not_found_em: not_found_em:
em->block_start = 0; em->block_start = EXTENT_MAP_HOLE;
em->block_end = 0; em->block_end = EXTENT_MAP_HOLE;
insert: insert:
btrfs_release_path(root, path); btrfs_release_path(root, path);
if (em->start > start || em->end < start) { if (em->start > start || em->end < start) {
...@@ -1712,6 +1802,7 @@ static void btrfs_truncate(struct inode *inode) ...@@ -1712,6 +1802,7 @@ static void btrfs_truncate(struct inode *inode)
ret = btrfs_truncate_in_trans(trans, root, inode); ret = btrfs_truncate_in_trans(trans, root, inode);
btrfs_update_inode(trans, root, inode); btrfs_update_inode(trans, root, inode);
nr = trans->blocks_used; nr = trans->blocks_used;
ret = btrfs_end_transaction(trans, root); ret = btrfs_end_transaction(trans, root);
BUG_ON(ret); BUG_ON(ret);
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
...@@ -1731,8 +1822,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen) ...@@ -1731,8 +1822,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
struct btrfs_key key; struct btrfs_key key;
struct btrfs_root_item root_item; struct btrfs_root_item root_item;
struct btrfs_inode_item *inode_item; struct btrfs_inode_item *inode_item;
struct buffer_head *subvol; struct extent_buffer *leaf;
struct btrfs_leaf *leaf;
struct btrfs_root *new_root; struct btrfs_root *new_root;
struct inode *inode; struct inode *inode;
struct inode *dir; struct inode *dir;
...@@ -1746,34 +1836,37 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen) ...@@ -1746,34 +1836,37 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
BUG_ON(!trans); BUG_ON(!trans);
subvol = btrfs_alloc_free_block(trans, root, 0, 0); leaf = btrfs_alloc_free_block(trans, root, 0, 0);
if (IS_ERR(subvol)) if (IS_ERR(leaf))
return PTR_ERR(subvol); return PTR_ERR(leaf);
leaf = btrfs_buffer_leaf(subvol);
btrfs_set_header_nritems(&leaf->header, 0); btrfs_set_header_nritems(leaf, 0);
btrfs_set_header_level(&leaf->header, 0); btrfs_set_header_level(leaf, 0);
btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); btrfs_set_header_blocknr(leaf, extent_buffer_blocknr(leaf));
btrfs_set_header_generation(&leaf->header, trans->transid); btrfs_set_header_generation(leaf, trans->transid);
btrfs_set_header_owner(&leaf->header, root->root_key.objectid); btrfs_set_header_owner(leaf, root->root_key.objectid);
memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, write_extent_buffer(leaf, root->fs_info->fsid,
sizeof(leaf->header.fsid)); (unsigned long)btrfs_header_fsid(leaf),
btrfs_mark_buffer_dirty(subvol); BTRFS_FSID_SIZE);
btrfs_mark_buffer_dirty(leaf);
inode_item = &root_item.inode; inode_item = &root_item.inode;
memset(inode_item, 0, sizeof(*inode_item)); memset(inode_item, 0, sizeof(*inode_item));
btrfs_set_inode_generation(inode_item, 1); inode_item->generation = cpu_to_le64(1);
btrfs_set_inode_size(inode_item, 3); inode_item->size = cpu_to_le64(3);
btrfs_set_inode_nlink(inode_item, 1); inode_item->nlink = cpu_to_le32(1);
btrfs_set_inode_nblocks(inode_item, 1); inode_item->nblocks = cpu_to_le64(1);
btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); btrfs_set_root_blocknr(&root_item, extent_buffer_blocknr(leaf));
btrfs_set_root_refs(&root_item, 1); btrfs_set_root_refs(&root_item, 1);
btrfs_set_root_blocks_used(&root_item, 0); btrfs_set_root_used(&root_item, 0);
memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
root_item.drop_level = 0; root_item.drop_level = 0;
brelse(subvol);
subvol = NULL; free_extent_buffer(leaf);
leaf = NULL;
ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
0, &objectid); 0, &objectid);
...@@ -1784,7 +1877,6 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen) ...@@ -1784,7 +1877,6 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
key.objectid = objectid; key.objectid = objectid;
key.offset = 1; key.offset = 1;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
&root_item); &root_item);
...@@ -1845,7 +1937,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen) ...@@ -1845,7 +1937,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_root_item new_root_item; struct btrfs_root_item new_root_item;
struct buffer_head *tmp; struct extent_buffer *tmp;
int ret; int ret;
int err; int err;
u64 objectid; u64 objectid;
...@@ -1876,10 +1968,11 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen) ...@@ -1876,10 +1968,11 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
key.objectid = objectid; key.objectid = objectid;
key.offset = 1; key.offset = 1;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); btrfs_set_root_blocknr(&new_root_item,
extent_buffer_blocknr(root->node));
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
&new_root_item); &new_root_item);
...@@ -1904,8 +1997,10 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen) ...@@ -1904,8 +1997,10 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
fail: fail:
nr = trans->blocks_used; nr = trans->blocks_used;
err = btrfs_commit_transaction(trans, root); err = btrfs_commit_transaction(trans, root);
if (err && !ret) if (err && !ret)
ret = err; ret = err;
mutex_unlock(&root->fs_info->fs_mutex); mutex_unlock(&root->fs_info->fs_mutex);
up_write(&root->snap_sem); up_write(&root->snap_sem);
btrfs_btree_balance_dirty(root, nr); btrfs_btree_balance_dirty(root, nr);
...@@ -2164,8 +2259,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, ...@@ -2164,8 +2259,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
return -ENOTEMPTY; return -ENOTEMPTY;
} }
mutex_lock(&root->fs_info->fs_mutex); mutex_lock(&root->fs_info->fs_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, new_dir); btrfs_set_trans_block_group(trans, new_dir);
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) { if (!path) {
...@@ -2177,9 +2274,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, ...@@ -2177,9 +2274,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
old_dir->i_ctime = old_dir->i_mtime = ctime; old_dir->i_ctime = old_dir->i_mtime = ctime;
new_dir->i_ctime = new_dir->i_mtime = ctime; new_dir->i_ctime = new_dir->i_mtime = ctime;
old_inode->i_ctime = ctime; old_inode->i_ctime = ctime;
if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
struct btrfs_key *location = &BTRFS_I(new_dir)->location; struct btrfs_key *location = &BTRFS_I(new_dir)->location;
u64 old_parent_oid; struct btrfs_key old_parent_key;
di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino, di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
"..", 2, -1); "..", 2, -1);
if (IS_ERR(di)) { if (IS_ERR(di)) {
...@@ -2190,7 +2288,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, ...@@ -2190,7 +2288,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
ret = -ENOENT; ret = -ENOENT;
goto out_fail; goto out_fail;
} }
old_parent_oid = btrfs_disk_key_objectid(&di->location); btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
if (ret) { if (ret) {
goto out_fail; goto out_fail;
...@@ -2199,7 +2297,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, ...@@ -2199,7 +2297,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
di = btrfs_lookup_dir_index_item(trans, root, path, di = btrfs_lookup_dir_index_item(trans, root, path,
old_inode->i_ino, old_inode->i_ino,
old_parent_oid, old_parent_key.objectid,
"..", 2, -1); "..", 2, -1);
if (IS_ERR(di)) { if (IS_ERR(di)) {
ret = PTR_ERR(di); ret = PTR_ERR(di);
...@@ -2257,8 +2355,9 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -2257,8 +2355,9 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
u64 objectid; u64 objectid;
int name_len; int name_len;
int datasize; int datasize;
char *ptr; unsigned long ptr;
struct btrfs_file_extent_item *ei; struct btrfs_file_extent_item *ei;
struct extent_buffer *leaf;
unsigned long nr; unsigned long nr;
name_len = strlen(symname) + 1; name_len = strlen(symname) + 1;
...@@ -2302,7 +2401,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -2302,7 +2401,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
BUG_ON(!path); BUG_ON(!path);
key.objectid = inode->i_ino; key.objectid = inode->i_ino;
key.offset = 0; key.offset = 0;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
datasize = btrfs_file_extent_calc_inline_size(name_len); datasize = btrfs_file_extent_calc_inline_size(name_len);
err = btrfs_insert_empty_item(trans, root, path, &key, err = btrfs_insert_empty_item(trans, root, path, &key,
...@@ -2311,16 +2409,17 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -2311,16 +2409,17 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
drop_inode = 1; drop_inode = 1;
goto out_unlock; goto out_unlock;
} }
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], struct btrfs_file_extent_item); ei = btrfs_item_ptr(leaf, path->slots[0],
btrfs_set_file_extent_generation(ei, trans->transid); struct btrfs_file_extent_item);
btrfs_set_file_extent_type(ei, btrfs_set_file_extent_generation(leaf, ei, trans->transid);
btrfs_set_file_extent_type(leaf, ei,
BTRFS_FILE_EXTENT_INLINE); BTRFS_FILE_EXTENT_INLINE);
ptr = btrfs_file_extent_inline_start(ei); ptr = btrfs_file_extent_inline_start(ei);
btrfs_memcpy(root, path->nodes[0]->b_data, write_extent_buffer(leaf, symname, ptr, name_len);
ptr, symname, name_len); btrfs_mark_buffer_dirty(leaf);
btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_free_path(path); btrfs_free_path(path);
inode->i_op = &btrfs_symlink_inode_operations; inode->i_op = &btrfs_symlink_inode_operations;
inode->i_mapping->a_ops = &btrfs_symlink_aops; inode->i_mapping->a_ops = &btrfs_symlink_aops;
inode->i_size = name_len - 1; inode->i_size = name_len - 1;
......
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
#include "disk-io.h" #include "disk-io.h"
#include "print-tree.h" #include "print-tree.h"
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l) void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
{ {
int i; int i;
u32 nr = btrfs_header_nritems(&l->header); u32 nr = btrfs_header_nritems(l);
struct btrfs_item *item; struct btrfs_item *item;
struct btrfs_extent_item *ei; struct btrfs_extent_item *ei;
struct btrfs_root_item *ri; struct btrfs_root_item *ri;
...@@ -31,119 +31,113 @@ void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l) ...@@ -31,119 +31,113 @@ void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
struct btrfs_inode_item *ii; struct btrfs_inode_item *ii;
struct btrfs_block_group_item *bi; struct btrfs_block_group_item *bi;
struct btrfs_file_extent_item *fi; struct btrfs_file_extent_item *fi;
struct btrfs_key key;
struct btrfs_key found_key;
u32 type; u32 type;
printk("leaf %llu total ptrs %d free space %d\n", printk("leaf %llu total ptrs %d free space %d\n",
(unsigned long long)btrfs_header_blocknr(&l->header), nr, (unsigned long long)btrfs_header_blocknr(l), nr,
btrfs_leaf_free_space(root, l)); btrfs_leaf_free_space(root, l));
for (i = 0 ; i < nr ; i++) { for (i = 0 ; i < nr ; i++) {
item = l->items + i; item = btrfs_item_nr(l, i);
type = btrfs_disk_key_type(&item->key); btrfs_item_key_to_cpu(l, &key, i);
type = btrfs_key_type(&key);
printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n", printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
i, i,
(unsigned long long)btrfs_disk_key_objectid(&item->key), (unsigned long long)key.objectid, type,
btrfs_disk_key_flags(&item->key), (unsigned long long)key.offset,
(unsigned long long)btrfs_disk_key_offset(&item->key), btrfs_item_offset(l, item), btrfs_item_size(l, item));
btrfs_item_offset(item),
btrfs_item_size(item));
switch (type) { switch (type) {
case BTRFS_INODE_ITEM_KEY: case BTRFS_INODE_ITEM_KEY:
ii = btrfs_item_ptr(l, i, struct btrfs_inode_item); ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
printk("\t\tinode generation %llu size %llu mode %o\n", printk("\t\tinode generation %llu size %llu mode %o\n",
(unsigned long long)btrfs_inode_generation(ii), (unsigned long long)btrfs_inode_generation(l, ii),
(unsigned long long)btrfs_inode_size(ii), (unsigned long long)btrfs_inode_size(l, ii),
btrfs_inode_mode(ii)); btrfs_inode_mode(l, ii));
break; break;
case BTRFS_DIR_ITEM_KEY: case BTRFS_DIR_ITEM_KEY:
di = btrfs_item_ptr(l, i, struct btrfs_dir_item); di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
btrfs_dir_item_key_to_cpu(l, di, &found_key);
printk("\t\tdir oid %llu flags %u type %u\n", printk("\t\tdir oid %llu flags %u type %u\n",
(unsigned long long)btrfs_disk_key_objectid( (unsigned long long)found_key.objectid,
&di->location), btrfs_dir_flags(l, di),
btrfs_dir_flags(di), btrfs_dir_type(l, di));
btrfs_dir_type(di));
printk("\t\tname %.*s\n",
btrfs_dir_name_len(di),(char *)(di + 1));
break; break;
case BTRFS_ROOT_ITEM_KEY: case BTRFS_ROOT_ITEM_KEY:
ri = btrfs_item_ptr(l, i, struct btrfs_root_item); ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
printk("\t\troot data blocknr %llu refs %u\n", printk("\t\troot data blocknr %llu refs %u\n",
(unsigned long long)btrfs_root_blocknr(ri), (unsigned long long)btrfs_disk_root_blocknr(l, ri),
btrfs_root_refs(ri)); btrfs_disk_root_refs(l, ri));
break; break;
case BTRFS_EXTENT_ITEM_KEY: case BTRFS_EXTENT_ITEM_KEY:
ei = btrfs_item_ptr(l, i, struct btrfs_extent_item); ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
printk("\t\textent data refs %u\n", printk("\t\textent data refs %u\n",
btrfs_extent_refs(ei)); btrfs_extent_refs(l, ei));
break; break;
case BTRFS_EXTENT_DATA_KEY: case BTRFS_EXTENT_DATA_KEY:
fi = btrfs_item_ptr(l, i, fi = btrfs_item_ptr(l, i,
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
if (btrfs_file_extent_type(fi) == if (btrfs_file_extent_type(l, fi) ==
BTRFS_FILE_EXTENT_INLINE) { BTRFS_FILE_EXTENT_INLINE) {
printk("\t\tinline extent data size %u\n", printk("\t\tinline extent data size %u\n",
btrfs_file_extent_inline_len(l->items + i)); btrfs_file_extent_inline_len(l, item));
break; break;
} }
printk("\t\textent data disk block %llu nr %llu\n", printk("\t\textent data disk block %llu nr %llu\n",
(unsigned long long)btrfs_file_extent_disk_blocknr(fi), (unsigned long long)btrfs_file_extent_disk_blocknr(l, fi),
(unsigned long long)btrfs_file_extent_disk_num_blocks(fi)); (unsigned long long)btrfs_file_extent_disk_num_blocks(l, fi));
printk("\t\textent data offset %llu nr %llu\n", printk("\t\textent data offset %llu nr %llu\n",
(unsigned long long)btrfs_file_extent_offset(fi), (unsigned long long)btrfs_file_extent_offset(l, fi),
(unsigned long long)btrfs_file_extent_num_blocks(fi)); (unsigned long long)btrfs_file_extent_num_blocks(l, fi));
break; break;
case BTRFS_BLOCK_GROUP_ITEM_KEY: case BTRFS_BLOCK_GROUP_ITEM_KEY:
bi = btrfs_item_ptr(l, i, bi = btrfs_item_ptr(l, i,
struct btrfs_block_group_item); struct btrfs_block_group_item);
printk("\t\tblock group used %llu\n", printk("\t\tblock group used %llu\n",
(unsigned long long)btrfs_block_group_used(bi)); (unsigned long long)btrfs_disk_block_group_used(l, bi));
break;
case BTRFS_STRING_ITEM_KEY:
printk("\t\titem data %.*s\n", btrfs_item_size(item),
btrfs_leaf_data(l) + btrfs_item_offset(item));
break; break;
}; };
} }
} }
void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t) void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
{ {
int i; int i;
u32 nr; u32 nr;
struct btrfs_node *c; struct btrfs_key key;
if (!t) if (!c)
return; return;
c = btrfs_buffer_node(t); nr = btrfs_header_nritems(c);
nr = btrfs_header_nritems(&c->header);
if (btrfs_is_leaf(c)) { if (btrfs_is_leaf(c)) {
btrfs_print_leaf(root, (struct btrfs_leaf *)c); btrfs_print_leaf(root, c);
return; return;
} }
printk("node %llu level %d total ptrs %d free spc %u\n", printk("node %llu level %d total ptrs %d free spc %u\n",
(unsigned long long)btrfs_header_blocknr(&c->header), (unsigned long long)btrfs_header_blocknr(c),
btrfs_header_level(&c->header), nr, btrfs_header_level(c), nr,
(u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr); (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
btrfs_node_key_to_cpu(c, &key, i);
printk("\tkey %d (%llu %u %llu) block %llu\n", printk("\tkey %d (%llu %u %llu) block %llu\n",
i, i,
(unsigned long long)c->ptrs[i].key.objectid, (unsigned long long)key.objectid,
c->ptrs[i].key.flags, key.type,
(unsigned long long)c->ptrs[i].key.offset, (unsigned long long)key.offset,
(unsigned long long)btrfs_node_blockptr(c, i)); (unsigned long long)btrfs_node_blockptr(c, i));
} }
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
struct buffer_head *next_buf = read_tree_block(root, struct extent_buffer *next = read_tree_block(root,
btrfs_node_blockptr(c, i)); btrfs_node_blockptr(c, i));
struct btrfs_node *next = btrfs_buffer_node(next_buf);
if (btrfs_is_leaf(next) && if (btrfs_is_leaf(next) &&
btrfs_header_level(&c->header) != 1) btrfs_header_level(c) != 1)
BUG(); BUG();
if (btrfs_header_level(&next->header) != if (btrfs_header_level(next) !=
btrfs_header_level(&c->header) - 1) btrfs_header_level(c) - 1)
BUG(); BUG();
btrfs_print_tree(root, next_buf); btrfs_print_tree(root, next);
btrfs_block_release(root, next_buf); free_extent_buffer(next);
} }
} }
...@@ -18,6 +18,6 @@ ...@@ -18,6 +18,6 @@
#ifndef __PRINT_TREE_ #ifndef __PRINT_TREE_
#define __PRINT_TREE_ #define __PRINT_TREE_
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l); void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l);
void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t); void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *t);
#endif #endif
...@@ -26,12 +26,13 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, ...@@ -26,12 +26,13 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
{ {
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_key search_key; struct btrfs_key search_key;
struct btrfs_leaf *l; struct btrfs_key found_key;
struct extent_buffer *l;
int ret; int ret;
int slot; int slot;
search_key.objectid = objectid; search_key.objectid = objectid;
search_key.flags = (u32)-1; search_key.type = (u8)-1;
search_key.offset = (u64)-1; search_key.offset = (u64)-1;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -39,17 +40,19 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, ...@@ -39,17 +40,19 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
if (ret < 0) if (ret < 0)
goto out; goto out;
BUG_ON(ret == 0); BUG_ON(ret == 0);
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
BUG_ON(path->slots[0] == 0); BUG_ON(path->slots[0] == 0);
slot = path->slots[0] - 1; slot = path->slots[0] - 1;
if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { btrfs_item_key_to_cpu(l, &found_key, slot);
if (found_key.objectid != objectid) {
ret = 1; ret = 1;
goto out; goto out;
} }
memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
sizeof(*item)); sizeof(*item));
btrfs_disk_key_to_cpu(key, &l->items[slot].key); memcpy(key, &found_key, sizeof(found_key));
ret = 0; ret = 0;
out: out:
btrfs_release_path(root, path); btrfs_release_path(root, path);
...@@ -62,10 +65,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -62,10 +65,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
*item) *item)
{ {
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_leaf *l; struct extent_buffer *l;
int ret; int ret;
int slot; int slot;
struct btrfs_root_item *update_item; unsigned long ptr;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
...@@ -73,10 +76,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -73,10 +76,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
if (ret < 0) if (ret < 0)
goto out; goto out;
BUG_ON(ret != 0); BUG_ON(ret != 0);
l = btrfs_buffer_leaf(path->nodes[0]); l = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item); ptr = btrfs_item_ptr_offset(l, slot);
btrfs_memcpy(root, l, update_item, item, sizeof(*item)); write_extent_buffer(l, item, ptr, sizeof(*item));
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(path->nodes[0]);
out: out:
btrfs_release_path(root, path); btrfs_release_path(root, path);
...@@ -103,11 +106,10 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, ...@@ -103,11 +106,10 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
struct btrfs_path *path; struct btrfs_path *path;
int ret; int ret;
u32 nritems; u32 nritems;
struct btrfs_leaf *leaf; struct extent_buffer *leaf;
int slot; int slot;
key.objectid = objectid; key.objectid = objectid;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
key.offset = 0; key.offset = 0;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
...@@ -117,19 +119,19 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, ...@@ -117,19 +119,19 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
if (ret < 0) if (ret < 0)
goto err; goto err;
while(1) { while(1) {
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
slot = path->slots[0]; slot = path->slots[0];
if (slot >= nritems) { if (slot >= nritems) {
ret = btrfs_next_leaf(root, path); ret = btrfs_next_leaf(root, path);
if (ret) if (ret)
break; break;
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = path->nodes[0];
nritems = btrfs_header_nritems(&leaf->header); nritems = btrfs_header_nritems(leaf);
slot = path->slots[0]; slot = path->slots[0];
} }
item = leaf->items + slot; item = btrfs_item_nr(leaf, slot);
btrfs_disk_key_to_cpu(&key, &item->key); btrfs_item_key_to_cpu(leaf, &key, slot);
if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
goto next; goto next;
...@@ -140,7 +142,7 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, ...@@ -140,7 +142,7 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
break; break;
ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
if (btrfs_root_refs(ri) != 0) if (btrfs_disk_root_refs(leaf, ri) != 0)
goto next; goto next;
dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key); dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
...@@ -170,6 +172,7 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -170,6 +172,7 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
int ret; int ret;
u32 refs; u32 refs;
struct btrfs_root_item *ri; struct btrfs_root_item *ri;
struct extent_buffer *leaf;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
...@@ -177,10 +180,10 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -177,10 +180,10 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
if (ret < 0) if (ret < 0)
goto out; goto out;
BUG_ON(ret != 0); BUG_ON(ret != 0);
ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), leaf = path->nodes[0];
path->slots[0], struct btrfs_root_item); ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
refs = btrfs_root_refs(ri); refs = btrfs_disk_root_refs(leaf, ri);
BUG_ON(refs != 0); BUG_ON(refs != 0);
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
out: out:
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#include "ioctl.h" #include "ioctl.h"
#include "print-tree.h" #include "print-tree.h"
#define BTRFS_SUPER_MAGIC 0x9123682E #define BTRFS_SUPER_MAGIC 0x9123683E
static struct super_operations btrfs_super_ops; static struct super_operations btrfs_super_ops;
...@@ -115,13 +115,12 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent) ...@@ -115,13 +115,12 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
return -EIO; return -EIO;
} }
sb->s_fs_info = tree_root; sb->s_fs_info = tree_root;
disk_super = tree_root->fs_info->disk_super; disk_super = &tree_root->fs_info->super_copy;
inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
tree_root); tree_root);
bi = BTRFS_I(inode); bi = BTRFS_I(inode);
bi->location.objectid = inode->i_ino; bi->location.objectid = inode->i_ino;
bi->location.offset = 0; bi->location.offset = 0;
bi->location.flags = 0;
bi->root = tree_root; bi->root = tree_root;
btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
...@@ -281,6 +280,7 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type, ...@@ -281,6 +280,7 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type,
error_bdev: error_bdev:
close_bdev_excl(bdev); close_bdev_excl(bdev);
error: error:
printk("get_sb failed\n");
return error; return error;
} }
/* end copy & paste */ /* end copy & paste */
...@@ -295,6 +295,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type, ...@@ -295,6 +295,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type,
ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
btrfs_fill_super, mnt, btrfs_fill_super, mnt,
subvol_name ? subvol_name : "default"); subvol_name ? subvol_name : "default");
printk("btrfs_get_sb returns %d\n", ret);
return ret; return ret;
} }
......
...@@ -31,31 +31,31 @@ ...@@ -31,31 +31,31 @@
static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf) static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%llu\n", return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)btrfs_root_blocks_used(&root->root_item)); (unsigned long long)btrfs_root_used(&root->root_item));
} }
static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf) static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%llu\n", return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)btrfs_root_block_limit(&root->root_item)); (unsigned long long)btrfs_root_limit(&root->root_item));
} }
static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf) static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%llu\n", return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)btrfs_super_blocks_used(fs->disk_super)); (unsigned long long)btrfs_super_blocks_used(&fs->super_copy));
} }
static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf) static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%llu\n", return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)btrfs_super_total_blocks(fs->disk_super)); (unsigned long long)btrfs_super_total_blocks(&fs->super_copy));
} }
static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf) static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%llu\n", return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)btrfs_super_blocksize(fs->disk_super)); (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
} }
/* this is for root attrs (subvols/snapshots) */ /* this is for root attrs (subvols/snapshots) */
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/writeback.h> #include <linux/writeback.h>
#include <linux/pagemap.h>
#include "ctree.h" #include "ctree.h"
#include "disk-io.h" #include "disk-io.h"
#include "transaction.h" #include "transaction.h"
...@@ -66,7 +67,9 @@ static int join_transaction(struct btrfs_root *root) ...@@ -66,7 +67,9 @@ static int join_transaction(struct btrfs_root *root)
cur_trans->commit_done = 0; cur_trans->commit_done = 0;
cur_trans->start_time = get_seconds(); cur_trans->start_time = get_seconds();
list_add_tail(&cur_trans->list, &root->fs_info->trans_list); list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
init_bit_radix(&cur_trans->dirty_pages); extent_map_tree_init(&cur_trans->dirty_pages,
root->fs_info->btree_inode->i_mapping,
GFP_NOFS);
} else { } else {
cur_trans->num_writers++; cur_trans->num_writers++;
cur_trans->num_joined++; cur_trans->num_joined++;
...@@ -88,7 +91,7 @@ static int record_root_in_trans(struct btrfs_root *root) ...@@ -88,7 +91,7 @@ static int record_root_in_trans(struct btrfs_root *root)
(unsigned long)root->root_key.objectid, (unsigned long)root->root_key.objectid,
BTRFS_ROOT_DEFRAG_TAG); BTRFS_ROOT_DEFRAG_TAG);
root->commit_root = root->node; root->commit_root = root->node;
get_bh(root->node); extent_buffer_get(root->node);
} else { } else {
WARN_ON(1); WARN_ON(1);
} }
...@@ -144,29 +147,30 @@ int btrfs_end_transaction(struct btrfs_trans_handle *trans, ...@@ -144,29 +147,30 @@ int btrfs_end_transaction(struct btrfs_trans_handle *trans,
int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
struct btrfs_root *root) struct btrfs_root *root)
{ {
unsigned long gang[16];
int ret; int ret;
int i;
int err; int err;
int werr = 0; int werr = 0;
struct extent_map_tree *dirty_pages;
struct page *page; struct page *page;
struct radix_tree_root *dirty_pages;
struct inode *btree_inode = root->fs_info->btree_inode; struct inode *btree_inode = root->fs_info->btree_inode;
u64 start;
u64 end;
unsigned long index;
if (!trans || !trans->transaction) { if (!trans || !trans->transaction) {
return filemap_write_and_wait(btree_inode->i_mapping); return filemap_write_and_wait(btree_inode->i_mapping);
} }
dirty_pages = &trans->transaction->dirty_pages; dirty_pages = &trans->transaction->dirty_pages;
while(1) { while(1) {
ret = find_first_radix_bit(dirty_pages, gang, ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
0, ARRAY_SIZE(gang)); EXTENT_DIRTY);
if (!ret) if (ret)
break; break;
for (i = 0; i < ret; i++) { clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
/* FIXME EIO */ while(start <= end) {
clear_radix_bit(dirty_pages, gang[i]); index = start >> PAGE_CACHE_SHIFT;
page = find_lock_page(btree_inode->i_mapping, start = (index + 1) << PAGE_CACHE_SHIFT;
gang[i]); page = find_lock_page(btree_inode->i_mapping, index);
if (!page) if (!page)
continue; continue;
if (PageWriteback(page)) { if (PageWriteback(page)) {
...@@ -202,10 +206,11 @@ int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, ...@@ -202,10 +206,11 @@ int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
btrfs_write_dirty_block_groups(trans, extent_root); btrfs_write_dirty_block_groups(trans, extent_root);
while(1) { while(1) {
old_extent_block = btrfs_root_blocknr(&extent_root->root_item); old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
if (old_extent_block == bh_blocknr(extent_root->node)) if (old_extent_block ==
extent_buffer_blocknr(extent_root->node))
break; break;
btrfs_set_root_blocknr(&extent_root->root_item, btrfs_set_root_blocknr(&extent_root->root_item,
bh_blocknr(extent_root->node)); extent_buffer_blocknr(extent_root->node));
ret = btrfs_update_root(trans, tree_root, ret = btrfs_update_root(trans, tree_root,
&extent_root->root_key, &extent_root->root_key,
&extent_root->root_item); &extent_root->root_item);
...@@ -279,9 +284,9 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans, ...@@ -279,9 +284,9 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans,
(unsigned long)root->root_key.objectid, (unsigned long)root->root_key.objectid,
BTRFS_ROOT_TRANS_TAG); BTRFS_ROOT_TRANS_TAG);
if (root->commit_root == root->node) { if (root->commit_root == root->node) {
WARN_ON(bh_blocknr(root->node) != WARN_ON(extent_buffer_blocknr(root->node) !=
btrfs_root_blocknr(&root->root_item)); btrfs_root_blocknr(&root->root_item));
brelse(root->commit_root); free_extent_buffer(root->commit_root);
root->commit_root = NULL; root->commit_root = NULL;
/* make sure to update the root on disk /* make sure to update the root on disk
...@@ -310,7 +315,7 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans, ...@@ -310,7 +315,7 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans,
root->root_key.offset = root->fs_info->generation; root->root_key.offset = root->fs_info->generation;
btrfs_set_root_blocknr(&root->root_item, btrfs_set_root_blocknr(&root->root_item,
bh_blocknr(root->node)); extent_buffer_blocknr(root->node));
err = btrfs_insert_root(trans, root->fs_info->tree_root, err = btrfs_insert_root(trans, root->fs_info->tree_root,
&root->root_key, &root->root_key,
&root->root_item); &root->root_item);
...@@ -389,10 +394,10 @@ int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) ...@@ -389,10 +394,10 @@ int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
for (i = 0; i < ret; i++) { for (i = 0; i < ret; i++) {
root = gang[i]; root = gang[i];
last = root->root_key.objectid + 1; last = root->root_key.objectid + 1;
btrfs_defrag_root(root, 1); // btrfs_defrag_root(root, 1);
} }
} }
btrfs_defrag_root(info->extent_root, 1); // btrfs_defrag_root(info->extent_root, 1);
return err; return err;
} }
...@@ -414,7 +419,7 @@ static int drop_dirty_roots(struct btrfs_root *tree_root, ...@@ -414,7 +419,7 @@ static int drop_dirty_roots(struct btrfs_root *tree_root,
dirty = list_entry(list->next, struct dirty_root, list); dirty = list_entry(list->next, struct dirty_root, list);
list_del_init(&dirty->list); list_del_init(&dirty->list);
num_blocks = btrfs_root_blocks_used(&dirty->root->root_item); num_blocks = btrfs_root_used(&dirty->root->root_item);
root = dirty->latest_root; root = dirty->latest_root;
while(1) { while(1) {
...@@ -441,11 +446,11 @@ static int drop_dirty_roots(struct btrfs_root *tree_root, ...@@ -441,11 +446,11 @@ static int drop_dirty_roots(struct btrfs_root *tree_root,
} }
BUG_ON(ret); BUG_ON(ret);
num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item); num_blocks -= btrfs_root_used(&dirty->root->root_item);
blocks_used = btrfs_root_blocks_used(&root->root_item); blocks_used = btrfs_root_used(&root->root_item);
if (num_blocks) { if (num_blocks) {
record_root_in_trans(root); record_root_in_trans(root);
btrfs_set_root_blocks_used(&root->root_item, btrfs_set_root_used(&root->root_item,
blocks_used - num_blocks); blocks_used - num_blocks);
} }
ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
...@@ -553,8 +558,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, ...@@ -553,8 +558,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
btrfs_set_super_generation(&root->fs_info->super_copy, btrfs_set_super_generation(&root->fs_info->super_copy,
cur_trans->transid); cur_trans->transid);
btrfs_set_super_root(&root->fs_info->super_copy, btrfs_set_super_root(&root->fs_info->super_copy,
bh_blocknr(root->fs_info->tree_root->node)); extent_buffer_blocknr(root->fs_info->tree_root->node));
memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
write_extent_buffer(root->fs_info->sb_buffer,
&root->fs_info->super_copy, 0,
sizeof(root->fs_info->super_copy)); sizeof(root->fs_info->super_copy));
btrfs_copy_pinned(root, &pinned_copy); btrfs_copy_pinned(root, &pinned_copy);
......
...@@ -28,7 +28,7 @@ struct btrfs_transaction { ...@@ -28,7 +28,7 @@ struct btrfs_transaction {
int use_count; int use_count;
int commit_done; int commit_done;
struct list_head list; struct list_head list;
struct radix_tree_root dirty_pages; struct extent_map_tree dirty_pages;
unsigned long start_time; unsigned long start_time;
wait_queue_head_t writer_wait; wait_queue_head_t writer_wait;
wait_queue_head_t commit_wait; wait_queue_head_t commit_wait;
...@@ -83,5 +83,6 @@ int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest, ...@@ -83,5 +83,6 @@ int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest,
int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info); int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info);
int btrfs_defrag_root(struct btrfs_root *root, int cacheonly); int btrfs_defrag_root(struct btrfs_root *root, int cacheonly);
int btrfs_clean_old_snapshots(struct btrfs_root *root); int btrfs_clean_old_snapshots(struct btrfs_root *root);
int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
#endif #endif
...@@ -23,14 +23,14 @@ ...@@ -23,14 +23,14 @@
#include "transaction.h" #include "transaction.h"
static void reada_defrag(struct btrfs_root *root, static void reada_defrag(struct btrfs_root *root,
struct btrfs_node *node) struct extent_buffer *node)
{ {
int i; int i;
u32 nritems; u32 nritems;
u64 blocknr; u64 blocknr;
int ret; int ret;
nritems = btrfs_header_nritems(&node->header); nritems = btrfs_header_nritems(node);
for (i = 0; i < nritems; i++) { for (i = 0; i < nritems; i++) {
blocknr = btrfs_node_blockptr(node, i); blocknr = btrfs_node_blockptr(node, i);
ret = readahead_tree_block(root, blocknr); ret = readahead_tree_block(root, blocknr);
...@@ -44,8 +44,8 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, ...@@ -44,8 +44,8 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
struct btrfs_path *path, int *level, struct btrfs_path *path, int *level,
int cache_only, u64 *last_ret) int cache_only, u64 *last_ret)
{ {
struct buffer_head *next; struct extent_buffer *next;
struct buffer_head *cur; struct extent_buffer *cur;
u64 blocknr; u64 blocknr;
int ret = 0; int ret = 0;
int is_extent = 0; int is_extent = 0;
...@@ -62,13 +62,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, ...@@ -62,13 +62,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
cur = path->nodes[*level]; cur = path->nodes[*level];
if (!cache_only && *level > 1 && path->slots[*level] == 0) if (!cache_only && *level > 1 && path->slots[*level] == 0)
reada_defrag(root, btrfs_buffer_node(cur)); reada_defrag(root, cur);
if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) if (btrfs_header_level(cur) != *level)
WARN_ON(1); WARN_ON(1);
if (path->slots[*level] >= if (path->slots[*level] >=
btrfs_header_nritems(btrfs_buffer_header(cur))) btrfs_header_nritems(cur))
break; break;
if (*level == 1) { if (*level == 1) {
...@@ -80,14 +80,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, ...@@ -80,14 +80,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
break; break;
} }
blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
path->slots[*level]);
if (cache_only) { if (cache_only) {
next = btrfs_find_tree_block(root, blocknr); next = btrfs_find_tree_block(root, blocknr);
if (!next || !buffer_uptodate(next) || /* FIXME, test for defrag */
buffer_locked(next) || !buffer_defrag(next)) { if (!next || !btrfs_buffer_uptodate(next)) {
brelse(next); free_extent_buffer(next);
path->slots[*level]++; path->slots[*level]++;
continue; continue;
} }
...@@ -106,16 +105,18 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans, ...@@ -106,16 +105,18 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
WARN_ON(*level <= 0); WARN_ON(*level <= 0);
if (path->nodes[*level-1]) if (path->nodes[*level-1])
btrfs_block_release(root, path->nodes[*level-1]); free_extent_buffer(path->nodes[*level-1]);
path->nodes[*level-1] = next; path->nodes[*level-1] = next;
*level = btrfs_header_level(btrfs_buffer_header(next)); *level = btrfs_header_level(next);
path->slots[*level] = 0; path->slots[*level] = 0;
} }
WARN_ON(*level < 0); WARN_ON(*level < 0);
WARN_ON(*level >= BTRFS_MAX_LEVEL); WARN_ON(*level >= BTRFS_MAX_LEVEL);
#if 0
clear_buffer_defrag(path->nodes[*level]); clear_buffer_defrag(path->nodes[*level]);
clear_buffer_defrag_done(path->nodes[*level]); clear_buffer_defrag_done(path->nodes[*level]);
btrfs_block_release(root, path->nodes[*level]); #endif
free_extent_buffer(path->nodes[*level]);
path->nodes[*level] = NULL; path->nodes[*level] = NULL;
*level += 1; *level += 1;
WARN_ON(ret); WARN_ON(ret);
...@@ -129,24 +130,25 @@ static int defrag_walk_up(struct btrfs_trans_handle *trans, ...@@ -129,24 +130,25 @@ static int defrag_walk_up(struct btrfs_trans_handle *trans,
{ {
int i; int i;
int slot; int slot;
struct btrfs_node *node; struct extent_buffer *node;
for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
slot = path->slots[i]; slot = path->slots[i];
if (slot < btrfs_header_nritems( if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
btrfs_buffer_header(path->nodes[i])) - 1) {
path->slots[i]++; path->slots[i]++;
*level = i; *level = i;
node = btrfs_buffer_node(path->nodes[i]); node = path->nodes[i];
WARN_ON(i == 0); WARN_ON(i == 0);
btrfs_disk_key_to_cpu(&root->defrag_progress, btrfs_node_key_to_cpu(node, &root->defrag_progress,
&node->ptrs[path->slots[i]].key); path->slots[i]);
root->defrag_level = i; root->defrag_level = i;
return 0; return 0;
} else { } else {
/*
clear_buffer_defrag(path->nodes[*level]); clear_buffer_defrag(path->nodes[*level]);
clear_buffer_defrag_done(path->nodes[*level]); clear_buffer_defrag_done(path->nodes[*level]);
btrfs_block_release(root, path->nodes[*level]); */
free_extent_buffer(path->nodes[*level]);
path->nodes[*level] = NULL; path->nodes[*level] = NULL;
*level = i + 1; *level = i + 1;
} }
...@@ -158,7 +160,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, ...@@ -158,7 +160,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
struct btrfs_root *root, int cache_only) struct btrfs_root *root, int cache_only)
{ {
struct btrfs_path *path = NULL; struct btrfs_path *path = NULL;
struct buffer_head *tmp; struct extent_buffer *tmp;
int ret = 0; int ret = 0;
int wret; int wret;
int level; int level;
...@@ -172,17 +174,18 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, ...@@ -172,17 +174,18 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
if (root->ref_cows == 0 && !is_extent) if (root->ref_cows == 0 && !is_extent)
goto out; goto out;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
level = btrfs_header_level(btrfs_buffer_header(root->node)); level = btrfs_header_level(root->node);
orig_level = level; orig_level = level;
if (level == 0) { if (level == 0) {
goto out; goto out;
} }
if (root->defrag_progress.objectid == 0) { if (root->defrag_progress.objectid == 0) {
get_bh(root->node); extent_buffer_get(root->node);
ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_realloc_node(trans, root, root->node, cache_only, ret = btrfs_realloc_node(trans, root, root->node, cache_only,
...@@ -200,12 +203,15 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, ...@@ -200,12 +203,15 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
if (is_extent) if (is_extent)
btrfs_extent_post_op(trans, root); btrfs_extent_post_op(trans, root);
if (wret < 0) { if (wret < 0) {
ret = wret; ret = wret;
goto out; goto out;
} }
while(level > 0 && !path->nodes[level]) while(level > 0 && !path->nodes[level])
level--; level--;
if (!path->nodes[level]) { if (!path->nodes[level]) {
ret = 0; ret = 0;
goto out; goto out;
...@@ -230,7 +236,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, ...@@ -230,7 +236,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
} }
for (i = 0; i <= orig_level; i++) { for (i = 0; i <= orig_level; i++) {
if (path->nodes[i]) { if (path->nodes[i]) {
btrfs_block_release(root, path->nodes[i]); free_extent_buffer(path->nodes[i]);
path->nodes[i] = 0; path->nodes[i] = 0;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment