Commit ed2ff2cb authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: pretend page cache & commit code

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 79f95c82
......@@ -250,6 +250,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
tree_block_release(root, mid_buf);
/* once for the root ptr */
tree_block_release(root, mid_buf);
clean_tree_block(root, mid_buf);
return free_extent(root, blocknr, 1);
}
parent = &parent_buf->node;
......@@ -280,6 +281,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
if (right->header.nritems == 0) {
u64 blocknr = right_buf->blocknr;
tree_block_release(root, right_buf);
clean_tree_block(root, right_buf);
right_buf = NULL;
right = NULL;
wret = del_ptr(root, path, level + 1, pslot + 1);
......@@ -291,7 +293,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
} else {
memcpy(parent->keys + pslot + 1, right->keys,
sizeof(struct key));
wret = write_tree_block(root, parent_buf);
wret = dirty_tree_block(root, parent_buf);
if (wret)
ret = wret;
}
......@@ -316,6 +318,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
/* we've managed to empty the middle node, drop it */
u64 blocknr = mid_buf->blocknr;
tree_block_release(root, mid_buf);
clean_tree_block(root, mid_buf);
mid_buf = NULL;
mid = NULL;
wret = del_ptr(root, path, level + 1, pslot);
......@@ -327,7 +330,7 @@ static int balance_level(struct ctree_root *root, struct ctree_path *path,
} else {
/* update the parent key to reflect our changes */
memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
wret = write_tree_block(root, parent_buf);
wret = dirty_tree_block(root, parent_buf);
if (wret)
ret = wret;
}
......@@ -458,7 +461,7 @@ static int fixup_low_keys(struct ctree_root *root,
break;
t = &path->nodes[i]->node;
memcpy(t->keys + tslot, key, sizeof(*key));
wret = write_tree_block(root, path->nodes[i]);
wret = dirty_tree_block(root, path->nodes[i]);
if (wret)
ret = wret;
if (tslot != 0)
......@@ -508,11 +511,11 @@ static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
src->header.nritems -= push_items;
dst->header.nritems += push_items;
wret = write_tree_block(root, src_buf);
wret = dirty_tree_block(root, src_buf);
if (wret < 0)
ret = wret;
wret = write_tree_block(root, dst_buf);
wret = dirty_tree_block(root, dst_buf);
if (wret < 0)
ret = wret;
return ret;
......@@ -566,11 +569,11 @@ static int balance_node_right(struct ctree_root *root,
src->header.nritems -= push_items;
dst->header.nritems += push_items;
wret = write_tree_block(root, src_buf);
wret = dirty_tree_block(root, src_buf);
if (wret < 0)
ret = wret;
wret = write_tree_block(root, dst_buf);
wret = dirty_tree_block(root, dst_buf);
if (wret < 0)
ret = wret;
return ret;
......@@ -612,7 +615,7 @@ static int insert_new_root(struct ctree_root *root,
tree_block_release(root, root->node);
root->node = t;
t->count++;
write_tree_block(root, t);
dirty_tree_block(root, t);
path->nodes[level] = t;
path->slots[level] = 0;
return 0;
......@@ -652,7 +655,7 @@ static int insert_ptr(struct ctree_root *root,
lower->header.nritems++;
if (lower->keys[1].objectid == 0)
BUG();
write_tree_block(root, path->nodes[level]);
dirty_tree_block(root, path->nodes[level]);
return 0;
}
......@@ -698,10 +701,10 @@ static int split_node(struct ctree_root *root, struct ctree_path *path,
c->header.nritems = mid;
ret = 0;
wret = write_tree_block(root, t);
wret = dirty_tree_block(root, t);
if (wret)
ret = wret;
wret = write_tree_block(root, split_buffer);
wret = dirty_tree_block(root, split_buffer);
if (wret)
ret = wret;
wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
......@@ -815,11 +818,11 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
}
left->header.nritems -= push_items;
write_tree_block(root, left_buf);
write_tree_block(root, right_buf);
dirty_tree_block(root, left_buf);
dirty_tree_block(root, right_buf);
memcpy(upper->node.keys + slot + 1,
&right->items[0].key, sizeof(struct key));
write_tree_block(root, upper);
dirty_tree_block(root, upper);
/* then fixup the leaf pointer in the path */
if (path->slots[0] >= left->header.nritems) {
path->slots[0] -= left->header.nritems;
......@@ -909,10 +912,10 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
push_space = right->items[i].offset;
}
wret = write_tree_block(root, t);
wret = dirty_tree_block(root, t);
if (wret)
ret = wret;
wret = write_tree_block(root, right_buf);
wret = dirty_tree_block(root, right_buf);
if (wret)
ret = wret;
......@@ -1019,10 +1022,10 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path,
right_buffer->blocknr, path->slots[1] + 1, 1);
if (wret)
ret = wret;
wret = write_tree_block(root, right_buffer);
wret = dirty_tree_block(root, right_buffer);
if (wret)
ret = wret;
wret = write_tree_block(root, l_buf);
wret = dirty_tree_block(root, l_buf);
if (wret)
ret = wret;
......@@ -1062,12 +1065,14 @@ int insert_item(struct ctree_root *root, struct key *key,
ret = search_slot(root, key, &path, data_size);
if (ret == 0) {
release_path(root, &path);
return -EEXIST;
}
if (ret < 0) {
release_path(root, &path);
ret = -EEXIST;
wret = commit_transaction(root);
if (wret)
ret = wret;
return ret;
}
if (ret < 0)
goto out;
slot_orig = path.slots[0];
leaf_buf = path.nodes[0];
......@@ -1113,14 +1118,18 @@ int insert_item(struct ctree_root *root, struct key *key,
if (slot == 0)
ret = fixup_low_keys(root, &path, key, 1);
wret = write_tree_block(root, leaf_buf);
wret = dirty_tree_block(root, leaf_buf);
if (wret)
ret = wret;
if (leaf_free_space(leaf) < 0)
BUG();
check_leaf(&path, 0);
out:
release_path(root, &path);
wret = commit_transaction(root);
if (wret)
ret = wret;
return ret;
}
......@@ -1160,7 +1169,7 @@ static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
if (wret)
ret = wret;
}
wret = write_tree_block(root, parent);
wret = dirty_tree_block(root, parent);
if (wret)
ret = wret;
return ret;
......@@ -1203,8 +1212,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
if (leaf->header.nritems == 0) {
if (leaf_buf == root->node) {
leaf->header.flags = node_level(0);
write_tree_block(root, leaf_buf);
dirty_tree_block(root, leaf_buf);
} else {
clean_tree_block(root, leaf_buf);
wret = del_ptr(root, path, 1, path->slots[1]);
if (wret)
ret = wret;
......@@ -1220,7 +1230,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
if (wret)
ret = wret;
}
wret = write_tree_block(root, leaf_buf);
wret = dirty_tree_block(root, leaf_buf);
if (wret)
ret = wret;
......@@ -1242,6 +1252,7 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
}
if (leaf->header.nritems == 0) {
u64 blocknr = leaf_buf->blocknr;
clean_tree_block(root, leaf_buf);
wret = del_ptr(root, path, 1, slot);
if (wret)
ret = wret;
......@@ -1254,6 +1265,9 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
}
}
}
wret = commit_transaction(root);
if (wret)
ret = wret;
return ret;
}
......
#ifndef __CTREE__
#define __CTREE__
#include "list.h"
#define CTREE_BLOCKSIZE 1024
/*
......@@ -53,6 +55,9 @@ struct ctree_root {
struct key current_insert;
int fp;
struct radix_tree_root cache_radix;
struct list_head trans;
struct list_head cache;
int cache_size;
};
/*
......
......@@ -11,6 +11,8 @@
#include "disk-io.h"
static int allocated_blocks = 0;
int cache_size = 0;
int cache_max = 10000;
static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
......@@ -21,6 +23,25 @@ static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
return 0;
}
static int free_some_buffers(struct ctree_root *root)
{
struct list_head *node, *next;
struct tree_buffer *b;
if (root->cache_size < cache_max)
return 0;
list_for_each_safe(node, next, &root->cache) {
b = list_entry(node, struct tree_buffer, cache);
if (b->count == 1) {
BUG_ON(!list_empty(&b->dirty));
list_del_init(&b->cache);
tree_block_release(root, b);
if (root->cache_size < cache_max)
return 0;
}
}
return 0;
}
struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
{
struct tree_buffer *buf;
......@@ -30,10 +51,14 @@ struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
return buf;
allocated_blocks++;
buf->blocknr = blocknr;
buf->count = 1;
buf->count = 2;
INIT_LIST_HEAD(&buf->dirty);
free_some_buffers(root);
radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
radix_tree_preload_end();
list_add_tail(&buf->cache, &root->cache);
root->cache_size++;
if (ret) {
free(buf);
return NULL;
......@@ -57,7 +82,6 @@ struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
return buf;
}
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
{
loff_t offset = blocknr * CTREE_BLOCKSIZE;
......@@ -82,6 +106,24 @@ struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
return buf;
}
int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
if (!list_empty(&buf->dirty))
return 0;
list_add_tail(&buf->dirty, &root->trans);
buf->count++;
return 0;
}
int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
if (!list_empty(&buf->dirty)) {
list_del_init(&buf->dirty);
tree_block_release(root, buf);
}
return 0;
}
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
{
u64 blocknr = buf->blocknr;
......@@ -96,9 +138,37 @@ int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
return 0;
}
static int __commit_transaction(struct ctree_root *root)
{
struct tree_buffer *b;
int ret = 0;
int wret;
while(!list_empty(&root->trans)) {
b = list_entry(root->trans.next, struct tree_buffer, dirty);
list_del_init(&b->dirty);
wret = write_tree_block(root, b);
if (wret)
ret = wret;
tree_block_release(root, b);
}
return ret;
}
int commit_transaction(struct ctree_root *root)
{
int ret;
ret = __commit_transaction(root);
if (!ret && root != root->extent_root)
ret = __commit_transaction(root->extent_root);
BUG_ON(ret);
return ret;
}
static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
struct ctree_root_info *info, int fp)
{
INIT_LIST_HEAD(&root->trans);
INIT_LIST_HEAD(&root->cache);
root->fp = fp;
root->node = NULL;
root->node = read_tree_block(root, info->tree_root);
......@@ -157,8 +227,23 @@ int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
return 0;
}
static int drop_cache(struct ctree_root *root)
{
while(!list_empty(&root->cache)) {
struct tree_buffer *b = list_entry(root->cache.next,
struct tree_buffer, cache);
list_del_init(&b->cache);
tree_block_release(root, b);
}
return 0;
}
int close_ctree(struct ctree_root *root)
{
drop_cache(root->extent_root);
drop_cache(root);
BUG_ON(!list_empty(&root->trans));
BUG_ON(!list_empty(&root->extent_root->trans));
close(root->fp);
if (root->node)
tree_block_release(root, root->node);
......@@ -182,6 +267,8 @@ void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
free(buf);
BUG_ON(allocated_blocks == 0);
allocated_blocks--;
BUG_ON(root->cache_size == 0);
root->cache_size--;
}
}
#ifndef __DISKIO__
#define __DISKIO__
#include "list.h"
struct tree_buffer {
u64 blocknr;
......@@ -8,11 +9,16 @@ struct tree_buffer {
struct node node;
struct leaf leaf;
};
struct list_head dirty;
struct list_head cache;
};
struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr);
struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr);
int write_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf);
int commit_transaction(struct ctree_root *root);
struct ctree_root *open_ctree(char *filename, struct ctree_super_block *s);
int close_ctree(struct ctree_root *root);
void tree_block_release(struct ctree_root *root, struct tree_buffer *buf);
......
This diff is collapsed.
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