Skip to content

Commit

Permalink
Btrfs: crash recovery fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Chris Mason authored and David Woodhouse committed Jun 28, 2007
1 parent f2654de commit ccd467d
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 48 deletions.
13 changes: 12 additions & 1 deletion fs/btrfs/ctree.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
struct btrfs_node *cow_node;
int ret;

WARN_ON(!buffer_uptodate(buf));
if (trans->transaction != root->fs_info->running_transaction) {
printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
root->fs_info->running_transaction->transid);
WARN_ON(1);
}
if (trans->transid != root->fs_info->generation) {
printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
root->fs_info->generation);
WARN_ON(1);
}
if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
trans->transid) {
*cow_ret = buf;
Expand Down Expand Up @@ -107,7 +118,7 @@ static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
}
btrfs_block_release(root, buf);
mark_buffer_dirty(cow);
btrfs_mark_buffer_dirty(cow);
*cow_ret = cow;
return 0;
}
Expand Down
12 changes: 4 additions & 8 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1013,18 +1013,13 @@ static inline void btrfs_memmove(struct btrfs_root *root,
memmove(dst, src, nr);
}

static inline void btrfs_mark_buffer_dirty(struct buffer_head *bh)
{
WARN_ON(!atomic_read(&bh->b_count));
mark_buffer_dirty(bh);
}

/* helper function to cast into the data area of the leaf. */
#define btrfs_item_ptr(leaf, slot, type) \
((type *)(btrfs_leaf_data(leaf) + \
btrfs_item_offset((leaf)->items + (slot))))

/* extent-tree.c */
int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy);
struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
btrfs_fs_info *info,
u64 blocknr);
Expand All @@ -1044,8 +1039,9 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf);
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 blocknr, u64 num_blocks, int pin);
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
btrfs_root *root);
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct radix_tree_root *unpin_radix);
int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
u64 blocknr, u64 num_blocks);
Expand Down
22 changes: 14 additions & 8 deletions fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,6 @@ struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
return NULL;
}

int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf)
{
WARN_ON(atomic_read(&buf->b_count) == 0);
mark_buffer_dirty(buf);
return 0;
}

int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf)
{
Expand Down Expand Up @@ -621,6 +613,20 @@ int close_ctree(struct btrfs_root *root)
return 0;
}

void btrfs_mark_buffer_dirty(struct buffer_head *bh)
{
struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
u64 transid = btrfs_header_generation(btrfs_buffer_header(bh));
WARN_ON(!atomic_read(&bh->b_count));
if (transid != root->fs_info->generation) {
printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
(unsigned long long)bh->b_blocknr,
transid, root->fs_info->generation);
WARN_ON(1);
}
mark_buffer_dirty(bh);
}

void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
{
brelse(buf);
Expand Down
1 change: 1 addition & 0 deletions fs/btrfs/disk-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
int btrfs_releasepage(struct page *page, gfp_t flags);
void btrfs_btree_balance_dirty(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);
#endif
37 changes: 33 additions & 4 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
}
return 0;
fail:
WARN_ON(1);
for (i =0; i < faili; i++) {
if (leaf) {
u64 disk_blocknr;
Expand Down Expand Up @@ -572,7 +573,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
struct btrfs_block_group_item);
memcpy(bi, &cache->item, sizeof(*bi));
mark_buffer_dirty(path->nodes[0]);
btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_release_path(extent_root, path);
fail:
finish_current_insert(trans, extent_root);
Expand Down Expand Up @@ -739,8 +740,30 @@ static int try_remove_page(struct address_space *mapping, unsigned long index)
return ret;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
btrfs_root *root)
int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
{
unsigned long gang[8];
u64 last = 0;
struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
int ret;
int i;

while(1) {
ret = find_first_radix_bit(pinned_radix, gang, last,
ARRAY_SIZE(gang));
if (!ret)
break;
for (i = 0 ; i < ret; i++) {
set_radix_bit(copy, gang[i]);
last = gang[i] + 1;
}
}
return 0;
}

int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct radix_tree_root *unpin_radix)
{
unsigned long gang[8];
struct inode *btree_inode = root->fs_info->btree_inode;
Expand All @@ -752,14 +775,15 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;

while(1) {
ret = find_first_radix_bit(pinned_radix, gang, 0,
ret = find_first_radix_bit(unpin_radix, gang, 0,
ARRAY_SIZE(gang));
if (!ret)
break;
if (!first)
first = gang[0];
for (i = 0; i < ret; i++) {
clear_radix_bit(pinned_radix, gang[i]);
clear_radix_bit(unpin_radix, gang[i]);
block_group = btrfs_lookup_block_group(root->fs_info,
gang[i]);
if (block_group) {
Expand Down Expand Up @@ -1309,6 +1333,7 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
if (data) {
ret = find_free_extent(trans, root, 0, 0,
search_end, 0, &prealloc_key, 0, 0, 0);
BUG_ON(ret);
if (ret)
return ret;
exclude_nr = info->extent_tree_prealloc_nr;
Expand All @@ -1319,6 +1344,7 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
ret = find_free_extent(trans, root, num_blocks, search_start,
search_end, hint_block, ins,
exclude_start, exclude_nr, data);
BUG_ON(ret);
if (ret)
return ret;

Expand All @@ -1334,10 +1360,12 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
if (!data) {
exclude_start = ins->objectid;
exclude_nr = ins->offset;
hint_block = exclude_start + exclude_nr;
ret = find_free_extent(trans, root, 0, search_start,
search_end, hint_block,
&prealloc_key, exclude_start,
exclude_nr, 0);
BUG_ON(ret);
if (ret)
return ret;
}
Expand All @@ -1348,6 +1376,7 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
sizeof(extent_item));

BUG_ON(ret);
finish_current_insert(trans, extent_root);
pending_ret = del_pending_extents(trans, extent_root);
if (ret) {
Expand Down
15 changes: 11 additions & 4 deletions fs/btrfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static int insert_inline_extent(struct btrfs_root *root, struct inode *inode,
ptr, kaddr + bh_offset(bh),
size);
kunmap_atomic(kaddr, KM_USER0);
mark_buffer_dirty(path->nodes[0]);
btrfs_mark_buffer_dirty(path->nodes[0]);
fail:
btrfs_free_path(path);
ret = btrfs_end_transaction(trans, root);
Expand Down Expand Up @@ -211,11 +211,13 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
int found_type;
int found_extent;
int found_inline;
int recow;

path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
while(1) {
recow = 0;
btrfs_release_path(root, path);
ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
search_start, -1);
Expand Down Expand Up @@ -244,6 +246,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
goto out;
}
if (recow) {
search_start = key.offset;
continue;
}
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
extent = btrfs_item_ptr(leaf, slot,
struct btrfs_file_extent_item);
Expand Down Expand Up @@ -274,6 +280,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
nextret = btrfs_next_leaf(root, path);
if (nextret)
goto out;
recow = 1;
} else {
path->slots[0]++;
}
Expand Down Expand Up @@ -321,7 +328,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
}
btrfs_set_file_extent_num_blocks(extent,
new_num);
mark_buffer_dirty(path->nodes[0]);
btrfs_mark_buffer_dirty(path->nodes[0]);
} else {
WARN_ON(1);
}
Expand Down Expand Up @@ -452,6 +459,8 @@ static int prepare_pages(struct btrfs_root *root,
err = -ENOMEM;
goto failed_release;
}
cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
wait_on_page_writeback(pages[i]);
}

mutex_lock(&root->fs_info->fs_mutex);
Expand Down Expand Up @@ -522,8 +531,6 @@ static int prepare_pages(struct btrfs_root *root,
mutex_unlock(&root->fs_info->fs_mutex);

for (i = 0; i < num_pages; i++) {
cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
wait_on_page_writeback(pages[i]);
offset = pos & (PAGE_CACHE_SIZE -1);
this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
if (!page_has_buffers(pages[i])) {
Expand Down
6 changes: 3 additions & 3 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
extent_num_blocks);
inode->i_blocks -= (orig_num_blocks -
extent_num_blocks) << 3;
mark_buffer_dirty(path->nodes[0]);
btrfs_mark_buffer_dirty(path->nodes[0]);
} else {
extent_start =
btrfs_file_extent_disk_blocknr(fi);
Expand Down Expand Up @@ -2020,7 +2020,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
sizeof(leaf->header.fsid));
mark_buffer_dirty(subvol);
btrfs_mark_buffer_dirty(subvol);

inode_item = &root_item.inode;
memset(inode_item, 0, sizeof(*inode_item));
Expand Down Expand Up @@ -2497,7 +2497,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
ptr = btrfs_file_extent_inline_start(ei);
btrfs_memcpy(root, path->nodes[0]->b_data,
ptr, symname, name_len);
mark_buffer_dirty(path->nodes[0]);
btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_free_path(path);
inode->i_op = &btrfs_symlink_inode_operations;
inode->i_mapping->a_ops = &btrfs_symlink_aops;
Expand Down
Loading

0 comments on commit ccd467d

Please sign in to comment.