提交 eb60ceac 编写于 作者: C Chris Mason 提交者: David Woodhouse

Btrfs: Add backing store, memory management

Signed-off-by: NChris Mason <chris.mason@oracle.com>
上级 4920c9ac
ctree: ctree.o
gcc -g -O2 -Wall -o ctree ctree.c
CFLAGS= -g -Wall
.c.o:
$(CC) $(CFLAGS) -c $<
ctree: ctree.o disk-io.h ctree.h disk-io.o radix-tree.o radix-tree.h
gcc $(CFLAGS) -o ctree ctree.o disk-io.o radix-tree.o
clean:
rm ctree ctree.o
rm ctree *.o
此差异已折叠。
#ifndef __CTREE__
#define __CTREE__
#define CTREE_BLOCKSIZE 4096
struct key {
u64 objectid;
u32 flags;
u64 offset;
} __attribute__ ((__packed__));
struct header {
u64 fsid[2]; /* FS specific uuid */
u64 blocknr;
u64 parentid;
u32 csum;
u32 ham;
u16 nritems;
u16 flags;
} __attribute__ ((__packed__));
#define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \
(sizeof(struct key) + sizeof(u64)))
#define LEVEL_BITS 3
#define MAX_LEVEL (1 << LEVEL_BITS)
#define node_level(f) ((f) & (MAX_LEVEL-1))
#define is_leaf(f) (node_level(f) == 0)
struct tree_buffer;
struct ctree_root {
struct tree_buffer *node;
int fp;
struct radix_tree_root cache_radix;
};
struct item {
struct key key;
u16 offset;
u16 size;
} __attribute__ ((__packed__));
#define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header))
struct leaf {
struct header header;
union {
struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
u8 data[CTREE_BLOCKSIZE-sizeof(struct header)];
};
} __attribute__ ((__packed__));
struct node {
struct header header;
struct key keys[NODEPTRS_PER_BLOCK];
u64 blockptrs[NODEPTRS_PER_BLOCK];
} __attribute__ ((__packed__));
struct ctree_path {
struct tree_buffer *nodes[MAX_LEVEL];
int slots[MAX_LEVEL];
};
#endif
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
static int allocated_blocks = 0;
struct ctree_header {
u64 root_block;
} __attribute__ ((__packed__));
static int get_free_block(struct ctree_root *root, u64 *block)
{
struct stat st;
int ret;
st.st_size = 0;
ret = fstat(root->fp, &st);
if (st.st_size > sizeof(struct ctree_header)) {
*block = (st.st_size -
sizeof(struct ctree_header)) / CTREE_BLOCKSIZE;
} else {
*block = 0;
}
ret = ftruncate(root->fp, sizeof(struct ctree_header) + (*block + 1) *
CTREE_BLOCKSIZE);
return ret;
}
struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
{
struct tree_buffer *buf;
int ret;
buf = malloc(sizeof(struct tree_buffer));
if (!buf)
return buf;
allocated_blocks++;
buf->blocknr = blocknr;
buf->count = 1;
radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
radix_tree_preload_end();
if (ret) {
free(buf);
return NULL;
}
return buf;
}
struct tree_buffer *alloc_free_block(struct ctree_root *root)
{
u64 free_block;
int ret;
struct tree_buffer * buf;
ret = get_free_block(root, &free_block);
if (ret) {
BUG();
return NULL;
}
buf = alloc_tree_block(root, free_block);
if (!buf)
BUG();
return buf;
}
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
{
loff_t offset = blocknr * CTREE_BLOCKSIZE + sizeof(struct ctree_header);
struct tree_buffer *buf;
int ret;
buf = radix_tree_lookup(&root->cache_radix, blocknr);
if (buf) {
buf->count++;
if (buf->blocknr != blocknr)
BUG();
if (buf->blocknr != buf->node.header.blocknr)
BUG();
return buf;
}
buf = alloc_tree_block(root, blocknr);
if (!buf)
return NULL;
ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
if (ret != CTREE_BLOCKSIZE) {
free(buf);
return NULL;
}
if (buf->blocknr != buf->node.header.blocknr)
BUG();
return buf;
}
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
u64 blocknr = buf->blocknr;
loff_t offset = blocknr * CTREE_BLOCKSIZE + sizeof(struct ctree_header);
int ret;
if (buf->blocknr != buf->node.header.blocknr)
BUG();
ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
if (ret != CTREE_BLOCKSIZE)
return ret;
if (buf == root->node)
return update_root_block(root);
return 0;
}
struct ctree_root *open_ctree(char *filename)
{
struct ctree_root *root = malloc(sizeof(struct ctree_root));
int fp;
u64 root_block;
int ret;
fp = open(filename, O_CREAT | O_RDWR);
if (fp < 0) {
free(root);
return NULL;
}
root->fp = fp;
INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
ret = pread(fp, &root_block, sizeof(u64), 0);
if (ret == sizeof(u64)) {
printf("reading root node at block %lu\n", root_block);
root->node = read_tree_block(root, root_block);
} else
root->node = NULL;
return root;
}
int close_ctree(struct ctree_root *root)
{
close(root->fp);
if (root->node)
tree_block_release(root, root->node);
free(root);
printf("on close %d blocks are allocated\n", allocated_blocks);
return 0;
}
int update_root_block(struct ctree_root *root)
{
int ret;
u64 root_block = root->node->blocknr;
ret = pwrite(root->fp, &root_block, sizeof(u64), 0);
if (ret != sizeof(u64))
return ret;
return 0;
}
void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
{
buf->count--;
if (buf->count == 0) {
if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
BUG();
radix_tree_delete(&root->cache_radix, buf->blocknr);
memset(buf, 0, sizeof(*buf));
free(buf);
BUG_ON(allocated_blocks == 0);
allocated_blocks--;
}
}
#ifndef __DISKIO__
#define __DISKIO__
struct tree_buffer {
u64 blocknr;
int count;
union {
struct node node;
struct leaf leaf;
};
};
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr);
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf);
struct ctree_root *open_ctree(char *filename);
int close_ctree(struct ctree_root *root);
void tree_block_release(struct ctree_root *root, struct tree_buffer *buf);
struct tree_buffer *alloc_free_block(struct ctree_root *root);
int update_root_block(struct ctree_root *root);
#endif
......@@ -6,6 +6,7 @@
#define BITS_PER_LONG 64
#define __GFP_BITS_SHIFT 20
#define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
#define GFP_KERNEL 0
#define __read_mostly
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define __force
......
此差异已折叠。
/*
* Copyright (C) 2001 Momchil Velikov
* Portions Copyright (C) 2001 Christoph Hellwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _LINUX_RADIX_TREE_H
#define _LINUX_RADIX_TREE_H
#define RADIX_TREE_MAX_TAGS 2
/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
struct radix_tree_root {
unsigned int height;
gfp_t gfp_mask;
struct radix_tree_node *rnode;
};
#define RADIX_TREE_INIT(mask) { \
.height = 0, \
.gfp_mask = (mask), \
.rnode = NULL, \
}
#define RADIX_TREE(name, mask) \
struct radix_tree_root name = RADIX_TREE_INIT(mask)
#define INIT_RADIX_TREE(root, mask) \
do { \
(root)->height = 0; \
(root)->gfp_mask = (mask); \
(root)->rnode = NULL; \
} while (0)
int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
void *radix_tree_delete(struct radix_tree_root *, unsigned long);
unsigned int
radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
unsigned long first_index, unsigned int max_items);
int radix_tree_preload(gfp_t gfp_mask);
void radix_tree_init(void);
void *radix_tree_tag_set(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
void *radix_tree_tag_clear(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
int radix_tree_tag_get(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
unsigned int
radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
unsigned long first_index, unsigned int max_items,
unsigned int tag);
int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
static inline void radix_tree_preload_end(void)
{
preempt_enable();
}
#endif /* _LINUX_RADIX_TREE_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册