Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2c90e5d6 authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse
Browse files

Btrfs: still corruption hunting

parent d6025579
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#define BIT_ARRAY_BYTES 256
#define BIT_RADIX_BITS_PER_ARRAY ((BIT_ARRAY_BYTES - sizeof(unsigned long)) * 8)

extern struct kmem_cache *btrfs_bit_radix_cachep;
int set_radix_bit(struct radix_tree_root *radix, unsigned long bit)
{
	unsigned long *bits;
@@ -16,7 +17,7 @@ int set_radix_bit(struct radix_tree_root *radix, unsigned long bit)

	bits = radix_tree_lookup(radix, slot);
	if (!bits) {
		bits = kmalloc(BIT_ARRAY_BYTES, GFP_NOFS);
		bits = kmem_cache_alloc(btrfs_bit_radix_cachep, GFP_NOFS);
		if (!bits)
			return -ENOMEM;
		memset(bits + 1, 0, BIT_ARRAY_BYTES - sizeof(unsigned long));
@@ -68,12 +69,10 @@ int clear_radix_bit(struct radix_tree_root *radix, unsigned long bit)
			break;
		}
	}

	if (empty) {
		bits = radix_tree_delete(radix, slot);
		synchronize_rcu();
		BUG_ON(!bits);
		kfree(bits);
		kmem_cache_free(btrfs_bit_radix_cachep, bits);
	}
#endif
	return 0;

fs/btrfs/btrfs_inode.h

0 → 100644
+14 −0
Original line number Diff line number Diff line
#ifndef __BTRFS_I__
#define __BTRFS_I__

struct btrfs_inode {
	u32 magic;
	struct inode vfs_inode;
	u32 magic2;
};
static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
{
	return container_of(inode, struct btrfs_inode, vfs_inode);
}

#endif
+30 −12
Original line number Diff line number Diff line
@@ -16,6 +16,16 @@ static int balance_node_right(struct btrfs_trans_handle *trans, struct
static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
		   struct btrfs_path *path, int level, int slot);

struct btrfs_path *btrfs_alloc_path(void)
{
	return kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
}

void btrfs_free_path(struct btrfs_path *p)
{
	kmem_cache_free(btrfs_path_cachep, p);
}

inline void btrfs_init_path(struct btrfs_path *p)
{
	memset(p, 0, sizeof(*p));
@@ -47,17 +57,18 @@ static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
	}
	cow = btrfs_alloc_free_block(trans, root);
	cow_node = btrfs_buffer_node(cow);
	if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
		WARN_ON(1);
	memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
	btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
	btrfs_set_header_generation(&cow_node->header, trans->transid);
	*cow_ret = cow;
	btrfs_mark_buffer_dirty(cow);
	btrfs_inc_ref(trans, root, buf);
	if (buf == root->node) {
		root->node = cow;
		get_bh(cow);
		if (buf != root->commit_root)
		if (buf != root->commit_root) {
			btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
		}
		btrfs_block_release(root, buf);
	} else {
		btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
@@ -66,6 +77,7 @@ static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
		btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
	}
	btrfs_block_release(root, buf);
	*cow_ret = cow;
	return 0;
}

@@ -477,9 +489,12 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
					       p->slots[level + 1],
					       &cow_buf);
			b = cow_buf;
			c = btrfs_buffer_node(b);
		}
		BUG_ON(!cow && ins_len);
		c = btrfs_buffer_node(b);
		if (level != btrfs_header_level(&c->header))
			WARN_ON(1);
		level = btrfs_header_level(&c->header);
		p->nodes[level] = b;
		ret = check_block(root, p, level);
		if (ret)
@@ -1257,19 +1272,22 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
		      data_size)
{
	int ret = 0;
	struct btrfs_path path;
	struct btrfs_path *path;
	u8 *ptr;

	btrfs_init_path(&path);
	ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
	if (!ret) {
		ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
				     path.slots[0], u8);
		btrfs_memcpy(root, path.nodes[0]->b_data,
		ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
				     path->slots[0], u8);
		btrfs_memcpy(root, path->nodes[0]->b_data,
			     ptr, data, data_size);
		btrfs_mark_buffer_dirty(path.nodes[0]);
		btrfs_mark_buffer_dirty(path->nodes[0]);
	}
	btrfs_release_path(root, &path);
	btrfs_release_path(root, path);
	btrfs_free_path(path);
	return ret;
}

+3 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@

struct btrfs_trans_handle;
struct btrfs_transaction;
extern struct kmem_cache *btrfs_path_cachep;

#define BTRFS_MAGIC "_BtRfS_M"

@@ -888,6 +889,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_key *key, struct btrfs_path *p, int
		      ins_len, int cow);
void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
struct btrfs_path *btrfs_alloc_path(void);
void btrfs_free_path(struct btrfs_path *p);
void btrfs_init_path(struct btrfs_path *p);
int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
		   struct btrfs_path *path);
+41 −43
Original line number Diff line number Diff line
@@ -8,18 +8,6 @@
#include "disk-io.h"
#include "transaction.h"

#define PATTERN 0xDEADBEEFUL
static inline void check_pattern(struct buffer_head *buf)
{
	if (buf->b_private != (void *)PATTERN)
		WARN_ON(1);
}

static inline void set_pattern(struct buffer_head *buf)
{
	buf->b_private = (void *)PATTERN;
}

static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
{
	struct btrfs_node *node = btrfs_buffer_node(buf);
@@ -35,6 +23,8 @@ static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)

struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
{
	return sb_find_get_block(root->fs_info->sb, blocknr);
#if 0
	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
	int blockbits = root->fs_info->sb->s_blocksize_bits;
	unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
@@ -43,6 +33,7 @@ struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
	struct buffer_head *head;
	struct buffer_head *ret = NULL;


	page = find_lock_page(mapping, index);
	if (!page)
		return NULL;
@@ -64,15 +55,17 @@ struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
	unlock_page(page);
	if (ret) {
		touch_buffer(ret);
		check_pattern(ret);
	}
	page_cache_release(page);
	return ret;
#endif
}

struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
						 u64 blocknr)
{
	return sb_getblk(root->fs_info->sb, blocknr);
#if 0
	struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
	int blockbits = root->fs_info->sb->s_blocksize_bits;
	unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
@@ -95,7 +88,6 @@ struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
			bh->b_bdev = root->fs_info->sb->s_bdev;
			bh->b_blocknr = first_block;
			set_buffer_mapped(bh);
			set_pattern(bh);
		}
		if (bh->b_blocknr == blocknr) {
			ret = bh;
@@ -111,6 +103,7 @@ struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
		touch_buffer(ret);
	page_cache_release(page);
	return ret;
#endif
}

static sector_t max_block(struct block_device *bdev)
@@ -225,6 +218,8 @@ static struct address_space_operations btree_aops = {

struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
{
	return sb_bread(root->fs_info->sb, blocknr);
#if 0
	struct buffer_head *bh = NULL;

	bh = btrfs_find_create_tree_block(root, blocknr);
@@ -239,7 +234,6 @@ struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
		if (!buffer_uptodate(bh))
			goto fail;
		csum_tree_block(root, bh, 1);
		set_pattern(bh);
	} else {
		unlock_buffer(bh);
	}
@@ -250,6 +244,7 @@ struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
	brelse(bh);
	return NULL;

#endif
}

int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -268,14 +263,14 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
	return 0;
}

static int __setup_root(struct btrfs_super_block *super,
static int __setup_root(int blocksize,
			struct btrfs_root *root,
			struct btrfs_fs_info *fs_info,
			u64 objectid)
{
	root->node = NULL;
	root->commit_root = NULL;
	root->blocksize = btrfs_super_blocksize(super);
	root->blocksize = blocksize;
	root->ref_cows = 0;
	root->fs_info = fs_info;
	memset(&root->root_key, 0, sizeof(root->root_key));
@@ -283,7 +278,7 @@ static int __setup_root(struct btrfs_super_block *super,
	return 0;
}

static int find_and_setup_root(struct btrfs_super_block *super,
static int find_and_setup_root(int blocksize,
			       struct btrfs_root *tree_root,
			       struct btrfs_fs_info *fs_info,
			       u64 objectid,
@@ -291,7 +286,7 @@ static int find_and_setup_root(struct btrfs_super_block *super,
{
	int ret;

	__setup_root(super, root, fs_info, objectid);
	__setup_root(blocksize, root, fs_info, objectid);
	ret = btrfs_find_last_root(tree_root, objectid,
				   &root->root_item, &root->root_key);
	BUG_ON(ret);
@@ -302,9 +297,7 @@ static int find_and_setup_root(struct btrfs_super_block *super,
	return 0;
}

struct btrfs_root *open_ctree(struct super_block *sb,
			      struct buffer_head *sb_buffer,
			      struct btrfs_super_block *disk_super)
struct btrfs_root *open_ctree(struct super_block *sb)
{
	struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
					  GFP_NOFS);
@@ -317,13 +310,11 @@ struct btrfs_root *open_ctree(struct super_block *sb,
	struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
						GFP_NOFS);
	int ret;
	struct btrfs_super_block *disk_super;

	if (!btrfs_super_root(disk_super)) {
		return NULL;
	}
	init_bit_radix(&fs_info->pinned_radix);
	init_bit_radix(&fs_info->pending_del_radix);
	sb_set_blocksize(sb, sb_buffer->b_size);
	sb_set_blocksize(sb, 4096);
	fs_info->running_transaction = NULL;
	fs_info->fs_root = root;
	fs_info->tree_root = tree_root;
@@ -331,55 +322,59 @@ struct btrfs_root *open_ctree(struct super_block *sb,
	fs_info->inode_root = inode_root;
	fs_info->last_inode_alloc = 0;
	fs_info->last_inode_alloc_dirid = 0;
	fs_info->disk_super = disk_super;
	fs_info->sb = sb;
	fs_info->btree_inode = NULL;
#if 0
	fs_info->btree_inode = new_inode(sb);
	fs_info->btree_inode->i_ino = 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_mapping->a_ops = &btree_aops;
	insert_inode_hash(fs_info->btree_inode);

	mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
#endif
	fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
	spin_lock_init(&fs_info->hash_lock);

	if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
		printk("failed to allocate sha256 hash\n");
		return NULL;
	}

	mutex_init(&fs_info->trans_mutex);
	mutex_init(&fs_info->fs_mutex);
	memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
	memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));

	__setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);

	fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
	__setup_root(sb->s_blocksize, tree_root,
		     fs_info, BTRFS_ROOT_TREE_OBJECTID);
	fs_info->sb_buffer = read_tree_block(tree_root,
					     BTRFS_SUPER_INFO_OFFSET /
					     sb->s_blocksize);

	if (!fs_info->sb_buffer) {
printk("failed2\n");
		return NULL;
	}
	brelse(sb_buffer);
	sb_buffer = NULL;
	disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
	if (!btrfs_super_root(disk_super)) {
		return NULL;
	}
	fs_info->disk_super = disk_super;

	tree_root->node = read_tree_block(tree_root,
					  btrfs_super_root(disk_super));
	BUG_ON(!tree_root->node);

	ret = find_and_setup_root(disk_super, tree_root, fs_info,
	mutex_lock(&fs_info->fs_mutex);
	ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
				  BTRFS_EXTENT_TREE_OBJECTID, extent_root);
	BUG_ON(ret);

	ret = find_and_setup_root(disk_super, tree_root, fs_info,
	ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
				  BTRFS_INODE_MAP_OBJECTID, inode_root);
	BUG_ON(ret);

	ret = find_and_setup_root(disk_super, tree_root, fs_info,
	ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
				  BTRFS_FS_TREE_OBJECTID, root);
	mutex_unlock(&fs_info->fs_mutex);
	BUG_ON(ret);
	root->commit_root = root->node;
	get_bh(root->node);
@@ -392,9 +387,11 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root)
{
	struct buffer_head *bh = root->fs_info->sb_buffer;

	btrfs_set_super_root(root->fs_info->disk_super,
			     root->fs_info->tree_root->node->b_blocknr);
	lock_buffer(bh);
	WARN_ON(atomic_read(&bh->b_count) < 1);
	clear_buffer_dirty(bh);
	csum_tree_block(root, bh, 0);
	bh->b_end_io = end_buffer_write_sync;
@@ -413,6 +410,7 @@ int close_ctree(struct btrfs_root *root)
	int ret;
	struct btrfs_trans_handle *trans;

	mutex_lock(&root->fs_info->fs_mutex);
	trans = btrfs_start_transaction(root, 1);
	btrfs_commit_transaction(trans, root);
	/* run commit again to  drop the original snapshot */
@@ -421,6 +419,7 @@ int close_ctree(struct btrfs_root *root)
	ret = btrfs_write_and_wait_transaction(NULL, root);
	BUG_ON(ret);
	write_ctree_super(NULL, root);
	mutex_unlock(&root->fs_info->fs_mutex);

	if (root->node)
		btrfs_block_release(root, root->node);
@@ -436,8 +435,8 @@ int close_ctree(struct btrfs_root *root)
	btrfs_block_release(root, root->commit_root);
	btrfs_block_release(root, root->fs_info->sb_buffer);
	crypto_free_hash(root->fs_info->hash_tfm);
	truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
	iput(root->fs_info->btree_inode);
	// truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
	// iput(root->fs_info->btree_inode);
	kfree(root->fs_info->extent_root);
	kfree(root->fs_info->inode_root);
	kfree(root->fs_info->tree_root);
@@ -448,7 +447,6 @@ int close_ctree(struct btrfs_root *root)

void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
{
	check_pattern(buf);
	brelse(buf);
	// brelse(buf);
}
Loading