btrfs-progs/disk-io.c

500 lines
13 KiB
C
Raw Normal View History

2007-06-12 13:07:11 +00:00
/*
* Copyright (C) 2007 Oracle. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
2007-04-23 19:56:27 +00:00
#define _XOPEN_SOURCE 600
#define __USE_XOPEN2K
2007-02-02 14:18:22 +00:00
#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"
2007-03-16 20:20:31 +00:00
#include "transaction.h"
2007-06-28 20:20:29 +00:00
#include "crc32c.h"
2007-02-02 14:18:22 +00:00
2007-10-15 20:25:14 +00:00
static u64 allocated_bytes = 0;
2007-03-01 23:59:40 +00:00
int cache_max = 10000;
2007-02-02 14:18:22 +00:00
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh,
u64 logical)
{
2007-06-09 13:22:37 +00:00
bh->fd = root->fs_info->fp;
2007-10-15 20:25:14 +00:00
bh->dev_bytenr = logical;
2007-06-09 13:22:37 +00:00
return 0;
}
2007-03-13 14:46:10 +00:00
static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
2007-02-02 14:18:22 +00:00
{
2007-10-15 20:25:14 +00:00
if (buf->bytenr != btrfs_header_bytenr(&buf->node.header))
BUG();
2007-04-05 18:29:12 +00:00
if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
sizeof(buf->node.header.fsid)))
BUG();
return 0;
2007-02-02 14:18:22 +00:00
}
2007-03-13 14:46:10 +00:00
static int free_some_buffers(struct btrfs_root *root)
2007-03-01 23:59:40 +00:00
{
struct list_head *node, *next;
2007-03-13 14:46:10 +00:00
struct btrfs_buffer *b;
if (root->fs_info->cache_size < cache_max)
2007-03-01 23:59:40 +00:00
return 0;
list_for_each_safe(node, next, &root->fs_info->cache) {
2007-03-13 14:46:10 +00:00
b = list_entry(node, struct btrfs_buffer, cache);
2007-03-01 23:59:40 +00:00
if (b->count == 1) {
BUG_ON(!list_empty(&b->dirty));
list_del_init(&b->cache);
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, b);
if (root->fs_info->cache_size < cache_max)
break;
2007-03-01 23:59:40 +00:00
}
}
return 0;
}
2007-10-15 20:25:14 +00:00
struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 bytenr,
u32 blocksize)
2007-02-02 14:18:22 +00:00
{
2007-03-13 14:46:10 +00:00
struct btrfs_buffer *buf;
2007-02-02 14:18:22 +00:00
int ret;
2007-03-14 18:14:43 +00:00
2007-10-15 20:25:14 +00:00
buf = malloc(sizeof(struct btrfs_buffer) + blocksize);
2007-02-02 14:18:22 +00:00
if (!buf)
return buf;
2007-10-15 20:25:14 +00:00
allocated_bytes += blocksize;
buf->bytenr = bytenr;
2007-03-01 23:59:40 +00:00
buf->count = 2;
2007-10-15 20:25:14 +00:00
buf->size = blocksize;
2007-03-01 23:59:40 +00:00
INIT_LIST_HEAD(&buf->dirty);
free_some_buffers(root);
2007-02-02 14:18:22 +00:00
radix_tree_preload(GFP_KERNEL);
2007-10-15 20:25:14 +00:00
ret = radix_tree_insert(&root->fs_info->cache_radix, bytenr, buf);
2007-02-02 14:18:22 +00:00
radix_tree_preload_end();
list_add_tail(&buf->cache, &root->fs_info->cache);
2007-10-15 20:25:14 +00:00
root->fs_info->cache_size += blocksize;
2007-02-02 14:18:22 +00:00
if (ret) {
free(buf);
return NULL;
}
return buf;
}
2007-10-15 20:25:14 +00:00
struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 bytenr,
u32 blocksize)
2007-02-02 14:18:22 +00:00
{
2007-03-13 14:46:10 +00:00
struct btrfs_buffer *buf;
2007-10-15 20:25:14 +00:00
buf = radix_tree_lookup(&root->fs_info->cache_radix, bytenr);
if (buf) {
buf->count++;
} else {
2007-10-15 20:25:14 +00:00
buf = alloc_tree_block(root, bytenr, blocksize);
if (!buf) {
BUG();
return NULL;
}
2007-02-02 14:18:22 +00:00
}
return buf;
}
2007-10-15 20:25:14 +00:00
struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
u32 blocksize)
2007-02-02 14:18:22 +00:00
{
2007-03-13 14:46:10 +00:00
struct btrfs_buffer *buf;
2007-02-02 14:18:22 +00:00
int ret;
2007-10-15 20:25:14 +00:00
buf = radix_tree_lookup(&root->fs_info->cache_radix, bytenr);
2007-02-02 14:18:22 +00:00
if (buf) {
buf->count++;
2007-04-26 20:46:06 +00:00
if (check_tree_block(root, buf))
BUG();
} else {
2007-10-15 20:25:14 +00:00
buf = alloc_tree_block(root, bytenr, blocksize);
if (!buf)
return NULL;
2007-10-15 20:25:14 +00:00
btrfs_map_bh_to_logical(root, buf, bytenr);
ret = pread(buf->fd, &buf->node, blocksize,
buf->dev_bytenr);
if (ret != blocksize) {
free(buf);
return NULL;
}
2007-04-26 20:46:06 +00:00
if (check_tree_block(root, buf))
BUG();
2007-02-02 14:18:22 +00:00
}
return buf;
}
2007-03-16 20:20:31 +00:00
int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_buffer *buf)
2007-03-01 23:59:40 +00:00
{
if (!list_empty(&buf->dirty))
return 0;
list_add_tail(&buf->dirty, &root->fs_info->trans);
2007-03-01 23:59:40 +00:00
buf->count++;
2007-04-26 20:46:06 +00:00
if (check_tree_block(root, buf))
BUG();
2007-03-01 23:59:40 +00:00
return 0;
}
2007-03-16 20:20:31 +00:00
int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_buffer *buf)
2007-03-01 23:59:40 +00:00
{
if (!list_empty(&buf->dirty)) {
list_del_init(&buf->dirty);
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, buf);
2007-03-01 23:59:40 +00:00
}
return 0;
}
2007-06-28 20:20:29 +00:00
int btrfs_csum_node(struct btrfs_root *root, struct btrfs_node *node)
{
u32 crc;
2007-10-15 20:25:14 +00:00
size_t len = btrfs_level_size(root, btrfs_header_level(&node->header)) -
BTRFS_CSUM_SIZE;
2007-06-28 20:20:29 +00:00
crc = crc32c(0, (char *)(node) + BTRFS_CSUM_SIZE, len);
memcpy(node->header.csum, &crc, BTRFS_CRC32_SIZE);
return 0;
}
int btrfs_csum_super(struct btrfs_root *root, struct btrfs_super_block *super)
{
u32 crc;
2007-10-15 20:24:39 +00:00
char block[root->sectorsize];
size_t len = root->sectorsize - BTRFS_CSUM_SIZE;
2007-06-28 20:20:29 +00:00
2007-10-15 20:24:39 +00:00
memset(block, 0, root->sectorsize);
2007-06-28 20:20:29 +00:00
memcpy(block, super, sizeof(*super));
crc = crc32c(0, block + BTRFS_CSUM_SIZE, len);
memcpy(super->csum, &crc, BTRFS_CRC32_SIZE);
return 0;
}
2007-03-16 20:20:31 +00:00
int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_buffer *buf)
2007-02-02 14:18:22 +00:00
{
int ret;
2007-10-15 20:25:14 +00:00
if (buf->bytenr != btrfs_header_bytenr(&buf->node.header))
2007-02-02 14:18:22 +00:00
BUG();
2007-10-15 20:25:14 +00:00
btrfs_map_bh_to_logical(root, buf, buf->bytenr);
2007-04-26 20:46:06 +00:00
if (check_tree_block(root, buf))
BUG();
2007-06-28 20:20:29 +00:00
btrfs_csum_node(root, &buf->node);
2007-10-15 20:25:14 +00:00
ret = pwrite(buf->fd, &buf->node, buf->size,
buf->dev_bytenr);
if (ret != buf->size)
2007-02-02 14:18:22 +00:00
return ret;
return 0;
}
2007-03-16 20:20:31 +00:00
static int __commit_transaction(struct btrfs_trans_handle *trans, struct
btrfs_root *root)
2007-03-01 23:59:40 +00:00
{
2007-03-13 14:46:10 +00:00
struct btrfs_buffer *b;
2007-03-01 23:59:40 +00:00
int ret = 0;
int wret;
while(!list_empty(&root->fs_info->trans)) {
b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
dirty);
2007-03-01 23:59:40 +00:00
list_del_init(&b->dirty);
2007-03-16 20:20:31 +00:00
wret = write_tree_block(trans, root, b);
2007-03-01 23:59:40 +00:00
if (wret)
ret = wret;
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, b);
2007-03-01 23:59:40 +00:00
}
return ret;
}
static int commit_tree_roots(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info)
{
int ret;
2007-10-15 20:25:14 +00:00
u64 old_extent_bytenr;
struct btrfs_root *tree_root = fs_info->tree_root;
struct btrfs_root *extent_root = fs_info->extent_root;
2007-04-26 20:46:06 +00:00
btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
while(1) {
2007-10-15 20:25:14 +00:00
old_extent_bytenr = btrfs_root_bytenr(&extent_root->root_item);
if (old_extent_bytenr == extent_root->node->bytenr)
break;
2007-10-15 20:25:14 +00:00
btrfs_set_root_bytenr(&extent_root->root_item,
extent_root->node->bytenr);
extent_root->root_item.level =
btrfs_header_level(&extent_root->node->node.header);
2007-03-16 20:20:31 +00:00
ret = btrfs_update_root(trans, tree_root,
&extent_root->root_key,
&extent_root->root_item);
BUG_ON(ret);
2007-04-26 20:46:06 +00:00
btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
}
return 0;
}
2007-03-16 20:20:31 +00:00
int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
btrfs_root *root, struct btrfs_super_block *s)
2007-03-01 23:59:40 +00:00
{
2007-03-07 01:08:01 +00:00
int ret = 0;
struct btrfs_buffer *snap = root->commit_root;
struct btrfs_key snap_key;
2007-03-07 01:08:01 +00:00
if (root->commit_root == root->node)
return 0;
memcpy(&snap_key, &root->root_key, sizeof(snap_key));
root->root_key.offset++;
2007-10-15 20:25:14 +00:00
btrfs_set_root_bytenr(&root->root_item, root->node->bytenr);
root->root_item.level =
btrfs_header_level(&root->node->node.header);
ret = btrfs_insert_root(trans, root->fs_info->tree_root,
&root->root_key, &root->root_item);
BUG_ON(ret);
ret = commit_tree_roots(trans, root->fs_info);
BUG_ON(ret);
ret = __commit_transaction(trans, root);
BUG_ON(ret);
2007-03-16 20:20:31 +00:00
write_ctree_super(trans, root, s);
btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
root->commit_root = root->node;
root->node->count++;
2007-03-16 20:20:31 +00:00
ret = btrfs_drop_snapshot(trans, root, snap);
BUG_ON(ret);
ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
BUG_ON(ret);
2007-03-20 19:57:25 +00:00
root->fs_info->generation = root->root_key.offset + 1;
2007-03-01 23:59:40 +00:00
return ret;
}
2007-03-14 18:14:43 +00:00
static int __setup_root(struct btrfs_super_block *super,
struct btrfs_root *root,
struct btrfs_fs_info *fs_info,
u64 objectid, int fp)
2007-02-20 21:40:44 +00:00
{
2007-02-21 22:04:57 +00:00
root->node = NULL;
2007-03-07 01:08:01 +00:00
root->commit_root = NULL;
2007-10-15 20:24:39 +00:00
root->sectorsize = btrfs_super_sectorsize(super);
root->nodesize = btrfs_super_nodesize(super);
root->leafsize = btrfs_super_leafsize(super);
2007-03-14 18:14:43 +00:00
root->ref_cows = 0;
root->fs_info = fs_info;
memset(&root->root_key, 0, sizeof(root->root_key));
memset(&root->root_item, 0, sizeof(root->root_item));
root->root_key.objectid = objectid;
return 0;
}
2007-10-15 20:25:14 +00:00
struct btrfs_buffer *read_root_block(struct btrfs_root *root, u64 bytenr,
u8 level)
{
struct btrfs_buffer *node;
u32 size = btrfs_level_size(root, level);
node = read_tree_block(root, bytenr, size);
BUG_ON(!node);
return node;
}
2007-03-14 18:14:43 +00:00
static int find_and_setup_root(struct btrfs_super_block *super,
struct btrfs_root *tree_root,
struct btrfs_fs_info *fs_info,
u64 objectid,
2007-03-14 18:14:43 +00:00
struct btrfs_root *root, int fp)
{
int ret;
__setup_root(super, root, fs_info, objectid, fp);
ret = btrfs_find_last_root(tree_root, objectid,
&root->root_item, &root->root_key);
BUG_ON(ret);
2007-10-15 20:25:14 +00:00
root->node = read_root_block(root,
btrfs_root_bytenr(&root->root_item),
root->root_item.level);
BUG_ON(!root->node);
2007-02-20 21:40:44 +00:00
return 0;
}
2007-03-13 14:46:10 +00:00
struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
2007-03-21 15:13:29 +00:00
{
int fp;
fp = open(filename, O_CREAT | O_RDWR, 0600);
if (fp < 0) {
return NULL;
}
return open_ctree_fd(fp, super);
}
struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
2007-02-02 14:18:22 +00:00
{
2007-03-13 14:46:10 +00:00
struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
2007-02-02 14:18:22 +00:00
int ret;
INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
2007-04-26 20:46:06 +00:00
INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
INIT_LIST_HEAD(&fs_info->trans);
INIT_LIST_HEAD(&fs_info->cache);
pending_tree_init(&fs_info->pending_tree);
pending_tree_init(&fs_info->pinned_tree);
pending_tree_init(&fs_info->del_pending);
fs_info->cache_size = 0;
fs_info->fp = fp;
fs_info->running_transaction = NULL;
fs_info->fs_root = root;
fs_info->tree_root = tree_root;
fs_info->extent_root = extent_root;
fs_info->last_inode_alloc = 0;
fs_info->last_inode_alloc_dirid = 0;
2007-03-21 00:35:03 +00:00
fs_info->disk_super = super;
memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
2007-03-13 14:46:10 +00:00
ret = pread(fp, super, sizeof(struct btrfs_super_block),
2007-03-14 18:14:43 +00:00
BTRFS_SUPER_INFO_OFFSET);
if (ret == 0 || btrfs_super_root(super) == 0) {
2007-03-21 00:35:03 +00:00
BUG();
return NULL;
2007-02-20 21:40:44 +00:00
}
BUG_ON(ret < 0);
__setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
2007-10-15 20:25:14 +00:00
tree_root->node = read_root_block(tree_root, btrfs_super_root(super),
btrfs_super_root_level(super));
BUG_ON(!tree_root->node);
ret = find_and_setup_root(super, tree_root, fs_info,
BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
BUG_ON(ret);
ret = find_and_setup_root(super, tree_root, fs_info,
BTRFS_FS_TREE_OBJECTID, root, fp);
BUG_ON(ret);
2007-03-07 01:08:01 +00:00
root->commit_root = root->node;
root->node->count++;
root->ref_cows = 1;
2007-03-20 19:57:25 +00:00
root->fs_info->generation = root->root_key.offset + 1;
2007-04-26 20:46:06 +00:00
btrfs_read_block_groups(root);
2007-02-02 14:18:22 +00:00
return root;
}
2007-03-16 20:20:31 +00:00
int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_super_block *s)
2007-02-02 14:18:22 +00:00
{
int ret;
2007-06-28 20:20:29 +00:00
2007-10-15 20:25:14 +00:00
btrfs_set_super_root(s, root->fs_info->tree_root->node->bytenr);
btrfs_set_super_root_level(s,
btrfs_header_level(&root->fs_info->tree_root->node->node.header));
2007-06-28 20:20:29 +00:00
btrfs_csum_super(root, s);
ret = pwrite(root->fs_info->fp, s, sizeof(*s),
2007-03-14 18:14:43 +00:00
BTRFS_SUPER_INFO_OFFSET);
2007-02-21 22:04:57 +00:00
if (ret != sizeof(*s)) {
fprintf(stderr, "failed to write new super block err %d\n", ret);
2007-02-02 14:18:22 +00:00
return ret;
2007-02-21 22:04:57 +00:00
}
return 0;
}
2007-03-13 14:46:10 +00:00
static int drop_cache(struct btrfs_root *root)
2007-03-01 23:59:40 +00:00
{
while(!list_empty(&root->fs_info->cache)) {
struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
struct btrfs_buffer,
cache);
2007-03-01 23:59:40 +00:00
list_del_init(&b->cache);
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, b);
2007-03-01 23:59:40 +00:00
}
return 0;
}
2007-03-13 14:46:10 +00:00
int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
2007-02-21 22:04:57 +00:00
{
int ret;
2007-03-16 20:20:31 +00:00
struct btrfs_trans_handle *trans;
trans = root->fs_info->running_transaction;
2007-03-16 20:20:31 +00:00
btrfs_commit_transaction(trans, root, s);
ret = commit_tree_roots(trans, root->fs_info);
BUG_ON(ret);
ret = __commit_transaction(trans, root);
BUG_ON(ret);
2007-03-16 20:20:31 +00:00
write_ctree_super(trans, root, s);
2007-03-01 23:59:40 +00:00
drop_cache(root);
BUG_ON(!list_empty(&root->fs_info->trans));
2007-03-01 23:59:40 +00:00
2007-04-26 20:46:06 +00:00
btrfs_free_block_groups(root->fs_info);
close(root->fs_info->fp);
2007-02-21 22:04:57 +00:00
if (root->node)
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, root->node);
if (root->fs_info->extent_root->node)
btrfs_block_release(root->fs_info->extent_root,
root->fs_info->extent_root->node);
if (root->fs_info->tree_root->node)
btrfs_block_release(root->fs_info->tree_root,
root->fs_info->tree_root->node);
2007-03-13 14:46:10 +00:00
btrfs_block_release(root, root->commit_root);
2007-02-21 22:04:57 +00:00
free(root);
2007-10-15 20:25:14 +00:00
printf("on close %llu blocks are allocated\n",
(unsigned long long)allocated_bytes);
2007-02-02 14:18:22 +00:00
return 0;
}
2007-03-13 14:46:10 +00:00
void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
2007-02-02 14:18:22 +00:00
{
buf->count--;
2007-02-21 22:04:57 +00:00
if (buf->count < 0)
BUG();
2007-02-02 14:18:22 +00:00
if (buf->count == 0) {
2007-03-02 21:08:05 +00:00
BUG_ON(!list_empty(&buf->cache));
BUG_ON(!list_empty(&buf->dirty));
if (!radix_tree_lookup(&root->fs_info->cache_radix,
2007-10-15 20:25:14 +00:00
buf->bytenr))
2007-02-02 14:18:22 +00:00
BUG();
2007-10-15 20:25:14 +00:00
radix_tree_delete(&root->fs_info->cache_radix, buf->bytenr);
BUG_ON(allocated_bytes == 0);
allocated_bytes -= buf->size;
BUG_ON(root->fs_info->cache_size == 0);
root->fs_info->cache_size -= buf->size;
2007-02-02 14:18:22 +00:00
memset(buf, 0, sizeof(*buf));
free(buf);
}
}