Skip to content

Commit

Permalink
Btrfs: variable block size support
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 Mar 14, 2007
1 parent 4beb1b8 commit 123abc8
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 300 deletions.
225 changes: 111 additions & 114 deletions fs/btrfs/ctree.c

Large diffs are not rendered by default.

62 changes: 35 additions & 27 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "kerncompat.h"

#define BTRFS_MAGIC "_BtRfS_M"
#define BTRFS_BLOCKSIZE 1024

#define BTRFS_ROOT_TREE_OBJECTID 1
#define BTRFS_EXTENT_TREE_OBJECTID 2
Expand Down Expand Up @@ -52,8 +51,11 @@ struct btrfs_header {
} __attribute__ ((__packed__));

#define BTRFS_MAX_LEVEL 8
#define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
(sizeof(struct btrfs_disk_key) + sizeof(u64)))
#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
sizeof(struct btrfs_header)) / \
(sizeof(struct btrfs_disk_key) + sizeof(u64)))
#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))

struct btrfs_buffer;

Expand Down Expand Up @@ -86,6 +88,7 @@ struct btrfs_root {
int ref_cows;
struct btrfs_root_item root_item;
struct btrfs_key root_key;
u32 blocksize;
};

/*
Expand All @@ -97,7 +100,7 @@ struct btrfs_super_block {
__le64 blocknr; /* this block number */
__le32 csum;
__le64 magic;
__le16 blocksize;
__le32 blocksize;
__le64 generation;
__le64 root;
__le64 total_blocks;
Expand All @@ -111,7 +114,7 @@ struct btrfs_super_block {
*/
struct btrfs_item {
struct btrfs_disk_key key;
__le16 offset;
__le32 offset;
__le16 size;
} __attribute__ ((__packed__));

Expand All @@ -122,24 +125,23 @@ struct btrfs_item {
* The data is separate from the items to get the keys closer together
* during searches.
*/
#define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
struct btrfs_leaf {
struct btrfs_header header;
union {
struct btrfs_item items[LEAF_DATA_SIZE/
sizeof(struct btrfs_item)];
u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
};
struct btrfs_item items[];
} __attribute__ ((__packed__));

/*
* all non-leaf blocks are nodes, they hold only keys and pointers to
* other blocks
*/
struct btrfs_key_ptr {
struct btrfs_disk_key key;
__le64 blockptr;
} __attribute__ ((__packed__));

struct btrfs_node {
struct btrfs_header header;
struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
__le64 blockptrs[NODEPTRS_PER_BLOCK];
struct btrfs_key_ptr ptrs[];
} __attribute__ ((__packed__));

/*
Expand Down Expand Up @@ -186,28 +188,28 @@ static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)

static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
{
return le64_to_cpu(n->blockptrs[nr]);
return le64_to_cpu(n->ptrs[nr].blockptr);
}

static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
u64 val)
{
n->blockptrs[nr] = cpu_to_le64(val);
n->ptrs[nr].blockptr = cpu_to_le64(val);
}

static inline u16 btrfs_item_offset(struct btrfs_item *item)
static inline u32 btrfs_item_offset(struct btrfs_item *item)
{
return le16_to_cpu(item->offset);
return le32_to_cpu(item->offset);
}

static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
{
item->offset = cpu_to_le16(val);
item->offset = cpu_to_le32(val);
}

static inline u16 btrfs_item_end(struct btrfs_item *item)
static inline u32 btrfs_item_end(struct btrfs_item *item)
{
return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
}

static inline u16 btrfs_item_size(struct btrfs_item *item)
Expand Down Expand Up @@ -390,20 +392,26 @@ static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
s->blocks_used = cpu_to_le64(val);
}

static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
{
return le16_to_cpu(s->blocksize);
return le32_to_cpu(s->blocksize);
}

static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
u16 val)
u32 val)
{
s->blocksize = cpu_to_le32(val);
}

static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
{
s->blocksize = cpu_to_le16(val);
return (u8 *)l->items;
}

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

struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
Expand All @@ -416,7 +424,7 @@ int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
void *data, int data_size);
int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
int btrfs_finish_extent_commit(struct btrfs_root *root);
int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
Expand Down
42 changes: 23 additions & 19 deletions fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
{
struct btrfs_buffer *buf;
int ret;
buf = malloc(sizeof(struct btrfs_buffer));

buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
if (!buf)
return buf;
allocated_blocks++;
Expand Down Expand Up @@ -84,7 +85,7 @@ struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)

struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
{
loff_t offset = blocknr * BTRFS_BLOCKSIZE;
loff_t offset = blocknr * root->blocksize;
struct btrfs_buffer *buf;
int ret;

Expand All @@ -95,8 +96,8 @@ struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
buf = alloc_tree_block(root, blocknr);
if (!buf)
return NULL;
ret = pread(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
if (ret != BTRFS_BLOCKSIZE) {
ret = pread(root->fp, &buf->node, root->blocksize, offset);
if (ret != root->blocksize) {
free(buf);
return NULL;
}
Expand Down Expand Up @@ -127,13 +128,13 @@ int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
{
u64 blocknr = buf->blocknr;
loff_t offset = blocknr * BTRFS_BLOCKSIZE;
loff_t offset = blocknr * root->blocksize;
int ret;

if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
BUG();
ret = pwrite(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
if (ret != BTRFS_BLOCKSIZE)
ret = pwrite(root->fp, &buf->node, root->blocksize, offset);
if (ret != root->blocksize)
return ret;
return 0;
}
Expand Down Expand Up @@ -215,34 +216,37 @@ int btrfs_commit_transaction(struct btrfs_root *root,
return ret;
}

static int __setup_root(struct btrfs_root *root, u64 objectid, int fp)
static int __setup_root(struct btrfs_super_block *super,
struct btrfs_root *root, u64 objectid, int fp)
{
INIT_LIST_HEAD(&root->trans);
INIT_LIST_HEAD(&root->cache);
root->cache_size = 0;
root->fp = fp;
root->node = NULL;
root->commit_root = NULL;
root->blocksize = btrfs_super_blocksize(super);
root->ref_cows = 0;
memset(&root->current_insert, 0, sizeof(root->current_insert));
memset(&root->last_insert, 0, sizeof(root->last_insert));
memset(&root->root_key, 0, sizeof(root->root_key));
memset(&root->root_item, 0, sizeof(root->root_item));
return 0;
}

static int find_and_setup_root(struct btrfs_root *tree_root, u64 objectid,
struct btrfs_root *root, int fp)
static int find_and_setup_root(struct btrfs_super_block *super,
struct btrfs_root *tree_root, u64 objectid,
struct btrfs_root *root, int fp)
{
int ret;

__setup_root(root, objectid, fp);
__setup_root(super, root, objectid, fp);
ret = btrfs_find_last_root(tree_root, objectid,
&root->root_item, &root->root_key);
BUG_ON(ret);

root->node = read_tree_block(root,
btrfs_root_blocknr(&root->root_item));
root->ref_cows = 0;
BUG_ON(!root->node);
return 0;
}
Expand Down Expand Up @@ -277,28 +281,28 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);

ret = pread(fp, super, sizeof(struct btrfs_super_block),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret == 0 || btrfs_super_root(super) == 0) {
printf("making new FS!\n");
ret = mkfs(fp, 0, BTRFS_BLOCKSIZE);
ret = mkfs(fp, 0, 1024);
if (ret)
return NULL;
ret = pread(fp, super, sizeof(struct btrfs_super_block),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret != sizeof(struct btrfs_super_block))
return NULL;
}
BUG_ON(ret < 0);

__setup_root(tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
__setup_root(super, tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
BUG_ON(!tree_root->node);

ret = find_and_setup_root(tree_root, BTRFS_EXTENT_TREE_OBJECTID,
ret = find_and_setup_root(super, tree_root, BTRFS_EXTENT_TREE_OBJECTID,
extent_root, fp);
BUG_ON(ret);

ret = find_and_setup_root(tree_root, BTRFS_FS_TREE_OBJECTID,
ret = find_and_setup_root(super, tree_root, BTRFS_FS_TREE_OBJECTID,
root, fp);
BUG_ON(ret);

Expand All @@ -313,7 +317,7 @@ int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
int ret;
btrfs_set_super_root(s, root->tree_root->node->blocknr);
ret = pwrite(root->fp, s, sizeof(*s),
BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
BTRFS_SUPER_INFO_OFFSET);
if (ret != sizeof(*s)) {
fprintf(stderr, "failed to write new super block err %d\n", ret);
return ret;
Expand Down
9 changes: 4 additions & 5 deletions fs/btrfs/disk-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
struct btrfs_buffer {
u64 blocknr;
int count;
struct list_head dirty;
struct list_head cache;
union {
struct btrfs_node node;
struct btrfs_leaf leaf;
};
struct list_head dirty;
struct list_head cache;
};

struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr);
Expand All @@ -24,9 +24,8 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *s);
int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s);
void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf);
int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s);
int mkfs(int fd, u64 num_blocks, u16 blocksize);

int mkfs(int fd, u64 num_blocks, u32 blocksize);

#define BTRFS_SUPER_INFO_OFFSET(bs) (16 * (bs))
#define BTRFS_SUPER_INFO_OFFSET (16 * 1024)

#endif
6 changes: 2 additions & 4 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
struct btrfs_key key;
struct btrfs_root *extent_root = root->extent_root;
int ret;
struct btrfs_item *item;
struct btrfs_extent_item *ei;
struct btrfs_key ins;
u32 refs;
Expand All @@ -161,9 +160,8 @@ static int __free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks)
printf("failed to find %Lu\n", key.objectid);
BUG();
}
item = path.nodes[0]->leaf.items + path.slots[0];
ei = (struct btrfs_extent_item *)(path.nodes[0]->leaf.data +
btrfs_item_offset(item));
ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
struct btrfs_extent_item);
BUG_ON(ei->refs == 0);
refs = btrfs_extent_refs(ei) - 1;
btrfs_set_extent_refs(ei, refs);
Expand Down
Loading

0 comments on commit 123abc8

Please sign in to comment.