mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-24 17:59:32 +00:00
Btrfs: Create extent_buffer interface for large blocksizes
Signed-off-by: Chris Mason <chris.mason@oracle.com>
This commit is contained in:
parent
50b78c24d5
commit
5f39d397df
22 changed files with 2814 additions and 2108 deletions
|
@ -23,4 +23,6 @@ modules_install:
|
|||
clean:
|
||||
$(MAKE) -C $(KERNELDIR) M=`pwd` clean
|
||||
|
||||
tester:
|
||||
$(MAKE) -C $(KERNELDIR) M=`pwd` tree-defrag.o transaction.o sysfs.o super.o root-tree.o inode-map.o inode-item.o inode.o file-item.o file.o extent_map.o disk-io.o ctree.o dir-item.o extent-tree.o
|
||||
endif
|
||||
|
|
1500
fs/btrfs/ctree.c
1500
fs/btrfs/ctree.c
File diff suppressed because it is too large
Load diff
871
fs/btrfs/ctree.h
871
fs/btrfs/ctree.h
File diff suppressed because it is too large
Load diff
|
@ -33,7 +33,7 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
|
|||
int ret;
|
||||
char *ptr;
|
||||
struct btrfs_item *item;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
|
||||
if (ret == -EEXIST) {
|
||||
|
@ -49,11 +49,11 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
|
|||
if (ret < 0)
|
||||
return ERR_PTR(ret);
|
||||
WARN_ON(ret > 0);
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
item = leaf->items + path->slots[0];
|
||||
leaf = path->nodes[0];
|
||||
item = btrfs_item_nr(leaf, path->slots[0]);
|
||||
ptr = btrfs_item_ptr(leaf, path->slots[0], char);
|
||||
BUG_ON(data_size > btrfs_item_size(item));
|
||||
ptr += btrfs_item_size(item) - data_size;
|
||||
BUG_ON(data_size > btrfs_item_size(leaf, item));
|
||||
ptr += btrfs_item_size(leaf, item) - data_size;
|
||||
return (struct btrfs_dir_item *)ptr;
|
||||
}
|
||||
|
||||
|
@ -65,12 +65,13 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
int ret2 = 0;
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_dir_item *dir_item;
|
||||
char *name_ptr;
|
||||
struct extent_buffer *leaf;
|
||||
unsigned long name_ptr;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_disk_key disk_key;
|
||||
u32 data_size;
|
||||
|
||||
key.objectid = dir;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
|
||||
ret = btrfs_name_hash(name, name_len, &key.offset);
|
||||
BUG_ON(ret);
|
||||
|
@ -85,14 +86,16 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
goto out;
|
||||
}
|
||||
|
||||
btrfs_cpu_key_to_disk(&dir_item->location, location);
|
||||
btrfs_set_dir_type(dir_item, type);
|
||||
btrfs_set_dir_flags(dir_item, 0);
|
||||
btrfs_set_dir_name_len(dir_item, name_len);
|
||||
name_ptr = (char *)(dir_item + 1);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_cpu_key_to_disk(&disk_key, location);
|
||||
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
|
||||
btrfs_set_dir_type(leaf, dir_item, type);
|
||||
btrfs_set_dir_flags(leaf, dir_item, 0);
|
||||
btrfs_set_dir_name_len(leaf, dir_item, name_len);
|
||||
name_ptr = (unsigned long)(dir_item + 1);
|
||||
|
||||
btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
write_extent_buffer(leaf, name, name_ptr, name_len);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
|
||||
second_insert:
|
||||
/* FIXME, use some real flag for selecting the extra index */
|
||||
|
@ -110,13 +113,15 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
ret2 = PTR_ERR(dir_item);
|
||||
goto out;
|
||||
}
|
||||
btrfs_cpu_key_to_disk(&dir_item->location, location);
|
||||
btrfs_set_dir_type(dir_item, type);
|
||||
btrfs_set_dir_flags(dir_item, 0);
|
||||
btrfs_set_dir_name_len(dir_item, name_len);
|
||||
name_ptr = (char *)(dir_item + 1);
|
||||
btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_cpu_key_to_disk(&disk_key, location);
|
||||
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
|
||||
btrfs_set_dir_type(leaf, dir_item, type);
|
||||
btrfs_set_dir_flags(leaf, dir_item, 0);
|
||||
btrfs_set_dir_name_len(leaf, dir_item, name_len);
|
||||
name_ptr = (unsigned long)(dir_item + 1);
|
||||
write_extent_buffer(leaf, name, name_ptr, name_len);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
out:
|
||||
btrfs_free_path(path);
|
||||
if (ret)
|
||||
|
@ -136,14 +141,15 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_key key;
|
||||
int ins_len = mod < 0 ? -1 : 0;
|
||||
int cow = mod != 0;
|
||||
struct btrfs_disk_key *found_key;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct btrfs_key found_key;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
key.objectid = dir;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
|
||||
|
||||
ret = btrfs_name_hash(name, name_len, &key.offset);
|
||||
BUG_ON(ret);
|
||||
|
||||
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
|
||||
if (ret < 0)
|
||||
return ERR_PTR(ret);
|
||||
|
@ -152,12 +158,13 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
|
|||
return NULL;
|
||||
path->slots[0]--;
|
||||
}
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
found_key = &leaf->items[path->slots[0]].key;
|
||||
|
||||
if (btrfs_disk_key_objectid(found_key) != dir ||
|
||||
btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
|
||||
btrfs_disk_key_offset(found_key) != key.offset)
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
|
||||
if (found_key.objectid != dir ||
|
||||
btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
|
||||
found_key.offset != key.offset)
|
||||
return NULL;
|
||||
|
||||
return btrfs_match_dir_item_name(root, path, name, name_len);
|
||||
|
@ -176,7 +183,6 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
|
|||
int cow = mod != 0;
|
||||
|
||||
key.objectid = dir;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
|
||||
key.offset = objectid;
|
||||
|
||||
|
@ -193,21 +199,22 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
|
|||
const char *name, int name_len)
|
||||
{
|
||||
struct btrfs_dir_item *dir_item;
|
||||
char *name_ptr;
|
||||
unsigned long name_ptr;
|
||||
u32 total_len;
|
||||
u32 cur = 0;
|
||||
u32 this_len;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
leaf = path->nodes[0];
|
||||
dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
|
||||
total_len = btrfs_item_size(leaf->items + path->slots[0]);
|
||||
total_len = btrfs_item_size_nr(leaf, path->slots[0]);
|
||||
while(cur < total_len) {
|
||||
this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item);
|
||||
name_ptr = (char *)(dir_item + 1);
|
||||
this_len = sizeof(*dir_item) +
|
||||
btrfs_dir_name_len(leaf, dir_item);
|
||||
name_ptr = (unsigned long)(dir_item + 1);
|
||||
|
||||
if (btrfs_dir_name_len(dir_item) == name_len &&
|
||||
memcmp(name_ptr, name, name_len) == 0)
|
||||
if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
|
||||
memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
|
||||
return dir_item;
|
||||
|
||||
cur += this_len;
|
||||
|
@ -223,20 +230,23 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_dir_item *di)
|
||||
{
|
||||
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
u32 sub_item_len;
|
||||
u32 item_len;
|
||||
int ret = 0;
|
||||
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
sub_item_len = sizeof(*di) + btrfs_dir_name_len(di);
|
||||
item_len = btrfs_item_size(leaf->items + path->slots[0]);
|
||||
if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) {
|
||||
leaf = path->nodes[0];
|
||||
sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di);
|
||||
item_len = btrfs_item_size_nr(leaf, path->slots[0]);
|
||||
if (sub_item_len == item_len) {
|
||||
ret = btrfs_del_item(trans, root, path);
|
||||
} else {
|
||||
char *ptr = (char *)di;
|
||||
char *start = btrfs_item_ptr(leaf, path->slots[0], char);
|
||||
btrfs_memmove(root, leaf, ptr, ptr + sub_item_len,
|
||||
/* MARKER */
|
||||
unsigned long ptr = (unsigned long)di;
|
||||
unsigned long start;
|
||||
|
||||
start = btrfs_item_ptr_offset(leaf, path->slots[0]);
|
||||
memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
|
||||
item_len - (ptr + sub_item_len - start));
|
||||
ret = btrfs_truncate_item(trans, root, path,
|
||||
item_len - sub_item_len);
|
||||
|
|
|
@ -23,138 +23,132 @@
|
|||
#include <linux/swap.h>
|
||||
#include <linux/radix-tree.h>
|
||||
#include <linux/writeback.h>
|
||||
#include <linux/buffer_head.h> // for block_sync_page
|
||||
#include "ctree.h"
|
||||
#include "disk-io.h"
|
||||
#include "transaction.h"
|
||||
#include "btrfs_inode.h"
|
||||
|
||||
u64 bh_blocknr(struct buffer_head *bh)
|
||||
#if 0
|
||||
static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
|
||||
{
|
||||
return bh->b_blocknr;
|
||||
}
|
||||
|
||||
static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
|
||||
{
|
||||
struct btrfs_node *node = btrfs_buffer_node(buf);
|
||||
if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
|
||||
printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
|
||||
(unsigned long long)bh_blocknr(buf),
|
||||
(unsigned long long)btrfs_header_blocknr(&node->header));
|
||||
if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
|
||||
printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
|
||||
(unsigned long long)extent_buffer_blocknr(buf),
|
||||
(unsigned long long)btrfs_header_blocknr(buf));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
|
||||
struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
|
||||
u64 blocknr)
|
||||
{
|
||||
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);
|
||||
struct page *page;
|
||||
struct buffer_head *bh;
|
||||
struct buffer_head *head;
|
||||
struct buffer_head *ret = NULL;
|
||||
|
||||
|
||||
page = find_lock_page(mapping, index);
|
||||
if (!page)
|
||||
return NULL;
|
||||
|
||||
if (!page_has_buffers(page))
|
||||
goto out_unlock;
|
||||
|
||||
head = page_buffers(page);
|
||||
bh = head;
|
||||
do {
|
||||
if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
|
||||
ret = bh;
|
||||
get_bh(bh);
|
||||
goto out_unlock;
|
||||
}
|
||||
bh = bh->b_this_page;
|
||||
} while (bh != head);
|
||||
out_unlock:
|
||||
unlock_page(page);
|
||||
page_cache_release(page);
|
||||
return ret;
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
return find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
|
||||
blocknr * root->sectorsize,
|
||||
root->sectorsize, GFP_NOFS);
|
||||
}
|
||||
|
||||
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
|
||||
u64 logical)
|
||||
{
|
||||
if (logical == 0) {
|
||||
bh->b_bdev = NULL;
|
||||
bh->b_blocknr = 0;
|
||||
set_buffer_mapped(bh);
|
||||
} else {
|
||||
map_bh(bh, root->fs_info->sb, logical);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
|
||||
struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
|
||||
u64 blocknr)
|
||||
{
|
||||
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);
|
||||
struct page *page;
|
||||
struct buffer_head *bh;
|
||||
struct buffer_head *head;
|
||||
struct buffer_head *ret = NULL;
|
||||
int err;
|
||||
u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
return alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
|
||||
blocknr * root->sectorsize,
|
||||
root->sectorsize, GFP_NOFS);
|
||||
}
|
||||
|
||||
page = find_or_create_page(mapping, index, GFP_NOFS);
|
||||
if (!page)
|
||||
return NULL;
|
||||
struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
|
||||
size_t page_offset, u64 start, u64 end,
|
||||
int create)
|
||||
{
|
||||
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
|
||||
struct extent_map *em;
|
||||
int ret;
|
||||
|
||||
if (!page_has_buffers(page))
|
||||
create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
|
||||
head = page_buffers(page);
|
||||
bh = head;
|
||||
do {
|
||||
if (!buffer_mapped(bh)) {
|
||||
err = btrfs_map_bh_to_logical(root, bh, first_block);
|
||||
BUG_ON(err);
|
||||
}
|
||||
if (bh_blocknr(bh) == blocknr) {
|
||||
ret = bh;
|
||||
get_bh(bh);
|
||||
goto out_unlock;
|
||||
}
|
||||
bh = bh->b_this_page;
|
||||
first_block++;
|
||||
} while (bh != head);
|
||||
out_unlock:
|
||||
unlock_page(page);
|
||||
if (ret)
|
||||
touch_buffer(ret);
|
||||
page_cache_release(page);
|
||||
again:
|
||||
em = lookup_extent_mapping(em_tree, start, end);
|
||||
if (em) {
|
||||
goto out;
|
||||
}
|
||||
em = alloc_extent_map(GFP_NOFS);
|
||||
if (!em) {
|
||||
em = ERR_PTR(-ENOMEM);
|
||||
goto out;
|
||||
}
|
||||
em->start = 0;
|
||||
em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
|
||||
em->block_start = 0;
|
||||
em->block_end = em->end;
|
||||
em->bdev = inode->i_sb->s_bdev;
|
||||
ret = add_extent_mapping(em_tree, em);
|
||||
if (ret == -EEXIST) {
|
||||
free_extent_map(em);
|
||||
em = NULL;
|
||||
goto again;
|
||||
} else if (ret) {
|
||||
em = ERR_PTR(ret);
|
||||
}
|
||||
out:
|
||||
return em;
|
||||
}
|
||||
|
||||
static int btree_writepage(struct page *page, struct writeback_control *wbc)
|
||||
{
|
||||
struct extent_map_tree *tree;
|
||||
tree = &BTRFS_I(page->mapping->host)->extent_tree;
|
||||
return extent_write_full_page(tree, page, btree_get_extent, wbc);
|
||||
}
|
||||
int btree_readpage(struct file *file, struct page *page)
|
||||
{
|
||||
struct extent_map_tree *tree;
|
||||
tree = &BTRFS_I(page->mapping->host)->extent_tree;
|
||||
return extent_read_full_page(tree, page, btree_get_extent);
|
||||
}
|
||||
|
||||
static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
|
||||
{
|
||||
struct extent_map_tree *tree;
|
||||
int ret;
|
||||
|
||||
BUG_ON(page->private != 1);
|
||||
tree = &BTRFS_I(page->mapping->host)->extent_tree;
|
||||
ret = try_release_extent_mapping(tree, page);
|
||||
if (ret == 1) {
|
||||
ClearPagePrivate(page);
|
||||
set_page_private(page, 0);
|
||||
page_cache_release(page);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int btree_get_block(struct inode *inode, sector_t iblock,
|
||||
struct buffer_head *bh, int create)
|
||||
static void btree_invalidatepage(struct page *page, unsigned long offset)
|
||||
{
|
||||
int err;
|
||||
struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
|
||||
err = btrfs_map_bh_to_logical(root, bh, iblock);
|
||||
return err;
|
||||
struct extent_map_tree *tree;
|
||||
tree = &BTRFS_I(page->mapping->host)->extent_tree;
|
||||
extent_invalidatepage(tree, page, offset);
|
||||
btree_releasepage(page, GFP_NOFS);
|
||||
}
|
||||
|
||||
int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
|
||||
char *result)
|
||||
{
|
||||
return 0;
|
||||
#if 0
|
||||
u32 crc;
|
||||
crc = crc32c(0, data, len);
|
||||
memcpy(result, &crc, BTRFS_CRC32_SIZE);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
|
||||
#if 0
|
||||
static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
|
||||
int verify)
|
||||
{
|
||||
return 0;
|
||||
char result[BTRFS_CRC32_SIZE];
|
||||
int ret;
|
||||
struct btrfs_node *node;
|
||||
|
@ -176,7 +170,9 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
static int btree_writepage(struct page *page, struct writeback_control *wbc)
|
||||
{
|
||||
struct buffer_head *bh;
|
||||
|
@ -195,87 +191,65 @@ static int btree_writepage(struct page *page, struct writeback_control *wbc)
|
|||
} while (bh != head);
|
||||
return block_write_full_page(page, btree_get_block, wbc);
|
||||
}
|
||||
|
||||
static int btree_readpage(struct file * file, struct page * page)
|
||||
{
|
||||
return block_read_full_page(page, btree_get_block);
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct address_space_operations btree_aops = {
|
||||
.readpage = btree_readpage,
|
||||
.writepage = btree_writepage,
|
||||
.releasepage = btree_releasepage,
|
||||
.invalidatepage = btree_invalidatepage,
|
||||
.sync_page = block_sync_page,
|
||||
};
|
||||
|
||||
int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
|
||||
{
|
||||
struct buffer_head *bh = NULL;
|
||||
struct extent_buffer *buf = NULL;
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
int ret = 0;
|
||||
|
||||
bh = btrfs_find_create_tree_block(root, blocknr);
|
||||
if (!bh)
|
||||
buf = btrfs_find_create_tree_block(root, blocknr);
|
||||
if (!buf)
|
||||
return 0;
|
||||
if (buffer_uptodate(bh)) {
|
||||
ret = 1;
|
||||
goto done;
|
||||
}
|
||||
if (test_set_buffer_locked(bh)) {
|
||||
ret = 1;
|
||||
goto done;
|
||||
}
|
||||
if (!buffer_uptodate(bh)) {
|
||||
get_bh(bh);
|
||||
bh->b_end_io = end_buffer_read_sync;
|
||||
submit_bh(READ, bh);
|
||||
} else {
|
||||
unlock_buffer(bh);
|
||||
ret = 1;
|
||||
}
|
||||
done:
|
||||
brelse(bh);
|
||||
read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
|
||||
buf, 0);
|
||||
free_extent_buffer(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
|
||||
struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
|
||||
{
|
||||
struct buffer_head *bh = NULL;
|
||||
struct extent_buffer *buf = NULL;
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
|
||||
bh = btrfs_find_create_tree_block(root, blocknr);
|
||||
if (!bh)
|
||||
return bh;
|
||||
if (buffer_uptodate(bh))
|
||||
goto uptodate;
|
||||
lock_buffer(bh);
|
||||
if (!buffer_uptodate(bh)) {
|
||||
get_bh(bh);
|
||||
bh->b_end_io = end_buffer_read_sync;
|
||||
submit_bh(READ, bh);
|
||||
wait_on_buffer(bh);
|
||||
if (!buffer_uptodate(bh))
|
||||
goto fail;
|
||||
} else {
|
||||
unlock_buffer(bh);
|
||||
}
|
||||
uptodate:
|
||||
if (!buffer_checked(bh)) {
|
||||
csum_tree_block(root, bh, 1);
|
||||
set_buffer_checked(bh);
|
||||
}
|
||||
if (check_tree_block(root, bh))
|
||||
goto fail;
|
||||
return bh;
|
||||
fail:
|
||||
brelse(bh);
|
||||
return NULL;
|
||||
buf = btrfs_find_create_tree_block(root, blocknr);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
|
||||
buf, 1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
||||
struct buffer_head *buf)
|
||||
struct extent_buffer *buf)
|
||||
{
|
||||
WARN_ON(atomic_read(&buf->b_count) == 0);
|
||||
lock_buffer(buf);
|
||||
clear_buffer_dirty(buf);
|
||||
unlock_buffer(buf);
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wait_on_tree_block_writeback(struct btrfs_root *root,
|
||||
struct extent_buffer *buf)
|
||||
{
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
|
||||
buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
|
||||
{
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -287,7 +261,9 @@ static int __setup_root(int blocksize,
|
|||
root->node = NULL;
|
||||
root->inode = NULL;
|
||||
root->commit_root = NULL;
|
||||
root->blocksize = blocksize;
|
||||
root->sectorsize = blocksize;
|
||||
root->nodesize = blocksize;
|
||||
root->leafsize = blocksize;
|
||||
root->ref_cows = 0;
|
||||
root->fs_info = fs_info;
|
||||
root->objectid = objectid;
|
||||
|
@ -332,7 +308,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
|
|||
struct btrfs_root *root;
|
||||
struct btrfs_root *tree_root = fs_info->tree_root;
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
u64 highest_inode;
|
||||
int ret = 0;
|
||||
|
||||
|
@ -361,11 +337,10 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
|
|||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
memcpy(&root->root_item,
|
||||
btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
|
||||
l = path->nodes[0];
|
||||
read_extent_buffer(l, &root->root_item,
|
||||
btrfs_item_ptr_offset(l, path->slots[0]),
|
||||
sizeof(root->root_item));
|
||||
memcpy(&root->root_key, location, sizeof(*location));
|
||||
ret = 0;
|
||||
out:
|
||||
btrfs_release_path(root, path);
|
||||
|
@ -406,21 +381,21 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
|
|||
(unsigned long)root->root_key.objectid,
|
||||
root);
|
||||
if (ret) {
|
||||
brelse(root->node);
|
||||
free_extent_buffer(root->node);
|
||||
kfree(root);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
|
||||
ret = btrfs_set_root_name(root, name, namelen);
|
||||
if (ret) {
|
||||
brelse(root->node);
|
||||
free_extent_buffer(root->node);
|
||||
kfree(root);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
|
||||
ret = btrfs_sysfs_add_root(root);
|
||||
if (ret) {
|
||||
brelse(root->node);
|
||||
free_extent_buffer(root->node);
|
||||
kfree(root->name);
|
||||
kfree(root);
|
||||
return ERR_PTR(ret);
|
||||
|
@ -471,6 +446,9 @@ struct btrfs_root *open_ctree(struct super_block *sb)
|
|||
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;
|
||||
extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
|
||||
fs_info->btree_inode->i_mapping,
|
||||
GFP_NOFS);
|
||||
fs_info->do_barriers = 1;
|
||||
fs_info->closing = 0;
|
||||
|
||||
|
@ -493,10 +471,14 @@ struct btrfs_root *open_ctree(struct super_block *sb)
|
|||
|
||||
if (!fs_info->sb_buffer)
|
||||
goto fail_iput;
|
||||
disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
|
||||
fs_info->disk_super = disk_super;
|
||||
memcpy(&fs_info->super_copy, disk_super, sizeof(fs_info->super_copy));
|
||||
|
||||
read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
|
||||
sizeof(fs_info->super_copy));
|
||||
|
||||
read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
|
||||
(unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
|
||||
BTRFS_FSID_SIZE);
|
||||
disk_super = &fs_info->super_copy;
|
||||
if (!btrfs_super_root(disk_super))
|
||||
goto fail_sb_buffer;
|
||||
|
||||
|
@ -530,9 +512,9 @@ struct btrfs_root *open_ctree(struct super_block *sb)
|
|||
return tree_root;
|
||||
|
||||
fail_tree_root:
|
||||
btrfs_block_release(tree_root, tree_root->node);
|
||||
free_extent_buffer(tree_root->node);
|
||||
fail_sb_buffer:
|
||||
btrfs_block_release(tree_root, fs_info->sb_buffer);
|
||||
free_extent_buffer(fs_info->sb_buffer);
|
||||
fail_iput:
|
||||
iput(fs_info->btree_inode);
|
||||
fail:
|
||||
|
@ -546,31 +528,13 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
*root)
|
||||
{
|
||||
int ret;
|
||||
struct buffer_head *bh = root->fs_info->sb_buffer;
|
||||
struct extent_buffer *super = root->fs_info->sb_buffer;
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
|
||||
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;
|
||||
get_bh(bh);
|
||||
if (root->fs_info->do_barriers)
|
||||
ret = submit_bh(WRITE_BARRIER, bh);
|
||||
else
|
||||
ret = submit_bh(WRITE, bh);
|
||||
if (ret == -EOPNOTSUPP) {
|
||||
get_bh(bh);
|
||||
lock_buffer(bh);
|
||||
set_buffer_uptodate(bh);
|
||||
root->fs_info->do_barriers = 0;
|
||||
ret = submit_bh(WRITE, bh);
|
||||
}
|
||||
wait_on_buffer(bh);
|
||||
if (!buffer_uptodate(bh)) {
|
||||
WARN_ON(1);
|
||||
return -EIO;
|
||||
}
|
||||
return 0;
|
||||
set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
|
||||
ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
|
||||
super->start, super->len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
|
||||
|
@ -581,9 +545,9 @@ int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
|
|||
if (root->inode)
|
||||
iput(root->inode);
|
||||
if (root->node)
|
||||
brelse(root->node);
|
||||
free_extent_buffer(root->node);
|
||||
if (root->commit_root)
|
||||
brelse(root->commit_root);
|
||||
free_extent_buffer(root->commit_root);
|
||||
if (root->name)
|
||||
kfree(root->name);
|
||||
kfree(root);
|
||||
|
@ -629,12 +593,10 @@ int close_ctree(struct btrfs_root *root)
|
|||
mutex_unlock(&fs_info->fs_mutex);
|
||||
|
||||
if (fs_info->extent_root->node)
|
||||
btrfs_block_release(fs_info->extent_root,
|
||||
fs_info->extent_root->node);
|
||||
free_extent_buffer(fs_info->extent_root->node);
|
||||
if (fs_info->tree_root->node)
|
||||
btrfs_block_release(fs_info->tree_root,
|
||||
fs_info->tree_root->node);
|
||||
btrfs_block_release(root, fs_info->sb_buffer);
|
||||
free_extent_buffer(fs_info->tree_root->node);
|
||||
free_extent_buffer(fs_info->sb_buffer);
|
||||
truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
|
||||
iput(fs_info->btree_inode);
|
||||
|
||||
|
@ -645,25 +607,32 @@ int close_ctree(struct btrfs_root *root)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void btrfs_mark_buffer_dirty(struct buffer_head *bh)
|
||||
int btrfs_buffer_uptodate(struct extent_buffer *buf)
|
||||
{
|
||||
struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
|
||||
u64 transid = btrfs_header_generation(btrfs_buffer_header(bh));
|
||||
struct inode *btree_inode = buf->pages[0]->mapping->host;
|
||||
return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
|
||||
}
|
||||
|
||||
WARN_ON(!atomic_read(&bh->b_count));
|
||||
int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
|
||||
{
|
||||
struct inode *btree_inode = buf->pages[0]->mapping->host;
|
||||
return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
|
||||
buf);
|
||||
}
|
||||
|
||||
void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
|
||||
{
|
||||
struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
|
||||
u64 transid = btrfs_header_generation(buf);
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
|
||||
if (transid != root->fs_info->generation) {
|
||||
printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
|
||||
(unsigned long long)bh->b_blocknr,
|
||||
(unsigned long long)extent_buffer_blocknr(buf),
|
||||
transid, root->fs_info->generation);
|
||||
WARN_ON(1);
|
||||
}
|
||||
mark_buffer_dirty(bh);
|
||||
}
|
||||
|
||||
void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
|
||||
{
|
||||
brelse(buf);
|
||||
set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
|
||||
}
|
||||
|
||||
void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
|
||||
|
|
|
@ -19,68 +19,35 @@
|
|||
#ifndef __DISKIO__
|
||||
#define __DISKIO__
|
||||
|
||||
#include <linux/buffer_head.h>
|
||||
|
||||
#define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
|
||||
|
||||
enum btrfs_bh_state_bits {
|
||||
BH_Checked = BH_PrivateStart,
|
||||
BH_Defrag,
|
||||
BH_DefragDone,
|
||||
};
|
||||
BUFFER_FNS(Checked, checked);
|
||||
BUFFER_FNS(Defrag, defrag);
|
||||
BUFFER_FNS(DefragDone, defrag_done);
|
||||
|
||||
static inline struct btrfs_node *btrfs_buffer_node(struct buffer_head *bh)
|
||||
{
|
||||
return (struct btrfs_node *)bh->b_data;
|
||||
}
|
||||
|
||||
static inline struct btrfs_leaf *btrfs_buffer_leaf(struct buffer_head *bh)
|
||||
{
|
||||
return (struct btrfs_leaf *)bh->b_data;
|
||||
}
|
||||
|
||||
static inline struct btrfs_header *btrfs_buffer_header(struct buffer_head *bh)
|
||||
{
|
||||
return &((struct btrfs_node *)bh->b_data)->header;
|
||||
}
|
||||
|
||||
struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr);
|
||||
struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr);
|
||||
int readahead_tree_block(struct btrfs_root *root, u64 blocknr);
|
||||
struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
|
||||
u64 blocknr);
|
||||
int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
||||
struct buffer_head *buf);
|
||||
int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
||||
struct buffer_head *buf);
|
||||
struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
|
||||
u64 blocknr);
|
||||
int clean_tree_block(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, struct buffer_head *buf);
|
||||
int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root);
|
||||
struct btrfs_root *root, struct extent_buffer *buf);
|
||||
struct btrfs_root *open_ctree(struct super_block *sb);
|
||||
int close_ctree(struct btrfs_root *root);
|
||||
void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf);
|
||||
int write_ctree_super(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root);
|
||||
struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr);
|
||||
int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
|
||||
char *result);
|
||||
struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
|
||||
u64 blocknr);
|
||||
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
|
||||
struct btrfs_key *location,
|
||||
const char *name, int namelen);
|
||||
struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
|
||||
struct btrfs_key *location);
|
||||
u64 bh_blocknr(struct buffer_head *bh);
|
||||
int btrfs_insert_dev_radix(struct btrfs_root *root,
|
||||
struct block_device *bdev,
|
||||
u64 device_id,
|
||||
u64 block_start,
|
||||
u64 num_blocks);
|
||||
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
|
||||
u64 logical);
|
||||
void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr);
|
||||
int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root);
|
||||
void btrfs_mark_buffer_dirty(struct buffer_head *bh);
|
||||
void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
|
||||
int btrfs_buffer_uptodate(struct extent_buffer *buf);
|
||||
int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
|
||||
int wait_on_tree_block_writeback(struct btrfs_root *root,
|
||||
struct extent_buffer *buf);
|
||||
#endif
|
||||
|
|
|
@ -33,7 +33,7 @@ static int cache_block_group(struct btrfs_root *root,
|
|||
struct btrfs_path *path;
|
||||
int ret;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
struct radix_tree_root *extent_radix;
|
||||
int slot;
|
||||
u64 i;
|
||||
|
@ -56,7 +56,6 @@ static int cache_block_group(struct btrfs_root *root,
|
|||
path->reada = 2;
|
||||
first_free = block_group->key.objectid;
|
||||
key.objectid = block_group->key.objectid;
|
||||
key.flags = 0;
|
||||
key.offset = 0;
|
||||
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
|
||||
|
@ -69,9 +68,9 @@ static int cache_block_group(struct btrfs_root *root,
|
|||
path->slots[0]--;
|
||||
|
||||
while(1) {
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
leaf = path->nodes[0];
|
||||
slot = path->slots[0];
|
||||
if (slot >= btrfs_header_nritems(&leaf->header)) {
|
||||
if (slot >= btrfs_header_nritems(leaf)) {
|
||||
ret = btrfs_next_leaf(root, path);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
|
@ -82,7 +81,7 @@ static int cache_block_group(struct btrfs_root *root,
|
|||
}
|
||||
}
|
||||
|
||||
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
|
||||
btrfs_item_key_to_cpu(leaf, &key, slot);
|
||||
if (key.objectid < block_group->key.objectid) {
|
||||
if (key.objectid + key.offset > first_free)
|
||||
first_free = key.objectid + key.offset;
|
||||
|
@ -116,8 +115,7 @@ static int cache_block_group(struct btrfs_root *root,
|
|||
hole_size = block_group->key.objectid +
|
||||
block_group->key.offset - last;
|
||||
for (i = 0; i < hole_size; i++) {
|
||||
set_radix_bit(extent_radix,
|
||||
last + i);
|
||||
set_radix_bit(extent_radix, last + i);
|
||||
}
|
||||
}
|
||||
block_group->cached = 1;
|
||||
|
@ -366,7 +364,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_path *path;
|
||||
int ret;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
struct btrfs_extent_item *item;
|
||||
u32 refs;
|
||||
|
||||
|
@ -375,7 +373,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
|
|||
return -ENOMEM;
|
||||
|
||||
key.objectid = blocknr;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
|
||||
key.offset = num_blocks;
|
||||
ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
|
||||
|
@ -386,10 +383,10 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
|
|||
BUG();
|
||||
}
|
||||
BUG_ON(ret != 0);
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
l = path->nodes[0];
|
||||
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
|
||||
refs = btrfs_extent_refs(item);
|
||||
btrfs_set_extent_refs(item, refs + 1);
|
||||
refs = btrfs_extent_refs(l, item);
|
||||
btrfs_set_extent_refs(l, item, refs + 1);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
|
||||
btrfs_release_path(root->fs_info->extent_root, path);
|
||||
|
@ -414,23 +411,25 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_path *path;
|
||||
int ret;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
struct btrfs_extent_item *item;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
key.objectid = blocknr;
|
||||
key.offset = num_blocks;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
|
||||
ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
|
||||
0, 0);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
if (ret != 0)
|
||||
if (ret != 0) {
|
||||
btrfs_print_leaf(root, path->nodes[0]);
|
||||
printk("failed to find block number %Lu\n", blocknr);
|
||||
BUG();
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
}
|
||||
l = path->nodes[0];
|
||||
item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
|
||||
*refs = btrfs_extent_refs(item);
|
||||
*refs = btrfs_extent_refs(l, item);
|
||||
out:
|
||||
btrfs_free_path(path);
|
||||
return 0;
|
||||
|
@ -439,16 +438,16 @@ static int lookup_extent_ref(struct btrfs_trans_handle *trans,
|
|||
int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root)
|
||||
{
|
||||
return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
|
||||
return btrfs_inc_extent_ref(trans, root,
|
||||
extent_buffer_blocknr(root->node), 1);
|
||||
}
|
||||
|
||||
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
||||
struct buffer_head *buf)
|
||||
struct extent_buffer *buf)
|
||||
{
|
||||
u64 blocknr;
|
||||
struct btrfs_node *buf_node;
|
||||
struct btrfs_leaf *buf_leaf;
|
||||
struct btrfs_disk_key *key;
|
||||
u32 nritems;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_file_extent_item *fi;
|
||||
int i;
|
||||
int leaf;
|
||||
|
@ -458,31 +457,31 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
|||
|
||||
if (!root->ref_cows)
|
||||
return 0;
|
||||
buf_node = btrfs_buffer_node(buf);
|
||||
leaf = btrfs_is_leaf(buf_node);
|
||||
buf_leaf = btrfs_buffer_leaf(buf);
|
||||
for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
|
||||
|
||||
leaf = btrfs_is_leaf(buf);
|
||||
nritems = btrfs_header_nritems(buf);
|
||||
for (i = 0; i < nritems; i++) {
|
||||
if (leaf) {
|
||||
u64 disk_blocknr;
|
||||
key = &buf_leaf->items[i].key;
|
||||
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
|
||||
btrfs_item_key_to_cpu(buf, &key, i);
|
||||
if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
|
||||
continue;
|
||||
fi = btrfs_item_ptr(buf_leaf, i,
|
||||
fi = btrfs_item_ptr(buf, i,
|
||||
struct btrfs_file_extent_item);
|
||||
if (btrfs_file_extent_type(fi) ==
|
||||
if (btrfs_file_extent_type(buf, fi) ==
|
||||
BTRFS_FILE_EXTENT_INLINE)
|
||||
continue;
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
|
||||
if (disk_blocknr == 0)
|
||||
continue;
|
||||
ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
|
||||
btrfs_file_extent_disk_num_blocks(fi));
|
||||
btrfs_file_extent_disk_num_blocks(buf, fi));
|
||||
if (ret) {
|
||||
faili = i;
|
||||
goto fail;
|
||||
}
|
||||
} else {
|
||||
blocknr = btrfs_node_blockptr(buf_node, i);
|
||||
blocknr = btrfs_node_blockptr(buf, i);
|
||||
ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
|
||||
if (ret) {
|
||||
faili = i;
|
||||
|
@ -496,22 +495,23 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
|||
for (i =0; i < faili; i++) {
|
||||
if (leaf) {
|
||||
u64 disk_blocknr;
|
||||
key = &buf_leaf->items[i].key;
|
||||
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
|
||||
btrfs_item_key_to_cpu(buf, &key, i);
|
||||
if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
|
||||
continue;
|
||||
fi = btrfs_item_ptr(buf_leaf, i,
|
||||
fi = btrfs_item_ptr(buf, i,
|
||||
struct btrfs_file_extent_item);
|
||||
if (btrfs_file_extent_type(fi) ==
|
||||
if (btrfs_file_extent_type(buf, fi) ==
|
||||
BTRFS_FILE_EXTENT_INLINE)
|
||||
continue;
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
|
||||
if (disk_blocknr == 0)
|
||||
continue;
|
||||
err = btrfs_free_extent(trans, root, disk_blocknr,
|
||||
btrfs_file_extent_disk_num_blocks(fi), 0);
|
||||
btrfs_file_extent_disk_num_blocks(buf,
|
||||
fi), 0);
|
||||
BUG_ON(err);
|
||||
} else {
|
||||
blocknr = btrfs_node_blockptr(buf_node, i);
|
||||
blocknr = btrfs_node_blockptr(buf, i);
|
||||
err = btrfs_free_extent(trans, root, blocknr, 1, 0);
|
||||
BUG_ON(err);
|
||||
}
|
||||
|
@ -527,16 +527,18 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
|
|||
int ret;
|
||||
int pending_ret;
|
||||
struct btrfs_root *extent_root = root->fs_info->extent_root;
|
||||
struct btrfs_block_group_item *bi;
|
||||
unsigned long bi;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
|
||||
if (ret < 0)
|
||||
goto fail;
|
||||
BUG_ON(ret);
|
||||
bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
|
||||
struct btrfs_block_group_item);
|
||||
memcpy(bi, &cache->item, sizeof(*bi));
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
|
||||
leaf = path->nodes[0];
|
||||
bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
|
||||
write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
btrfs_release_path(extent_root, path);
|
||||
fail:
|
||||
finish_current_insert(trans, extent_root);
|
||||
|
@ -768,11 +770,11 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
|
|||
unsigned long gang[8];
|
||||
struct btrfs_fs_info *info = extent_root->fs_info;
|
||||
|
||||
btrfs_set_extent_refs(&extent_item, 1);
|
||||
btrfs_set_stack_extent_refs(&extent_item, 1);
|
||||
ins.offset = 1;
|
||||
ins.flags = 0;
|
||||
btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
|
||||
btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
|
||||
btrfs_set_stack_extent_owner(&extent_item,
|
||||
extent_root->root_key.objectid);
|
||||
|
||||
while(1) {
|
||||
ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
|
||||
|
@ -795,23 +797,20 @@ static int finish_current_insert(struct btrfs_trans_handle *trans, struct
|
|||
static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
|
||||
{
|
||||
int err;
|
||||
struct btrfs_header *header;
|
||||
struct buffer_head *bh;
|
||||
struct extent_buffer *buf;
|
||||
|
||||
if (!pending) {
|
||||
bh = btrfs_find_tree_block(root, blocknr);
|
||||
if (bh) {
|
||||
if (buffer_uptodate(bh)) {
|
||||
buf = btrfs_find_tree_block(root, blocknr);
|
||||
if (buf) {
|
||||
if (btrfs_buffer_uptodate(buf)) {
|
||||
u64 transid =
|
||||
root->fs_info->running_transaction->transid;
|
||||
header = btrfs_buffer_header(bh);
|
||||
if (btrfs_header_generation(header) ==
|
||||
transid) {
|
||||
btrfs_block_release(root, bh);
|
||||
if (btrfs_header_generation(buf) == transid) {
|
||||
free_extent_buffer(buf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
btrfs_block_release(root, bh);
|
||||
free_extent_buffer(buf);
|
||||
}
|
||||
err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
|
||||
if (!err) {
|
||||
|
@ -839,12 +838,12 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
struct btrfs_key key;
|
||||
struct btrfs_fs_info *info = root->fs_info;
|
||||
struct btrfs_root *extent_root = info->extent_root;
|
||||
struct extent_buffer *leaf;
|
||||
int ret;
|
||||
struct btrfs_extent_item *ei;
|
||||
u32 refs;
|
||||
|
||||
key.objectid = blocknr;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
|
||||
key.offset = num_blocks;
|
||||
|
||||
|
@ -856,12 +855,16 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
if (ret < 0)
|
||||
return ret;
|
||||
BUG_ON(ret);
|
||||
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
|
||||
|
||||
leaf = path->nodes[0];
|
||||
ei = btrfs_item_ptr(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);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
refs = btrfs_extent_refs(leaf, ei);
|
||||
BUG_ON(refs == 0);
|
||||
refs -= 1;
|
||||
btrfs_set_extent_refs(leaf, ei, refs);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
|
||||
if (refs == 0) {
|
||||
u64 super_blocks_used, root_blocks_used;
|
||||
|
||||
|
@ -876,8 +879,8 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
super_blocks_used - num_blocks);
|
||||
|
||||
/* block accounting for root item */
|
||||
root_blocks_used = btrfs_root_blocks_used(&root->root_item);
|
||||
btrfs_set_root_blocks_used(&root->root_item,
|
||||
root_blocks_used = btrfs_root_used(&root->root_item);
|
||||
btrfs_set_root_used(&root->root_item,
|
||||
root_blocks_used - num_blocks);
|
||||
|
||||
ret = btrfs_del_item(trans, extent_root, path);
|
||||
|
@ -984,7 +987,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
u64 test_block;
|
||||
u64 orig_search_start = search_start;
|
||||
int start_found;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
struct btrfs_root * root = orig_root->fs_info->extent_root;
|
||||
struct btrfs_fs_info *info = root->fs_info;
|
||||
int total_needed = num_blocks;
|
||||
|
@ -994,10 +997,10 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
int wrapped = 0;
|
||||
|
||||
WARN_ON(num_blocks < 1);
|
||||
ins->flags = 0;
|
||||
btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
|
||||
|
||||
level = btrfs_header_level(btrfs_buffer_header(root->node));
|
||||
level = btrfs_header_level(root->node);
|
||||
|
||||
if (search_end == (u64)-1)
|
||||
search_end = btrfs_super_total_blocks(&info->super_copy);
|
||||
if (hint_block) {
|
||||
|
@ -1034,8 +1037,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
path->slots[0]--;
|
||||
}
|
||||
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
|
||||
l = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(l, &key, path->slots[0]);
|
||||
|
||||
/*
|
||||
* a rare case, go back one key if we hit a block group item
|
||||
* instead of an extent item
|
||||
|
@ -1055,9 +1059,9 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
}
|
||||
|
||||
while (1) {
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
l = path->nodes[0];
|
||||
slot = path->slots[0];
|
||||
if (slot >= btrfs_header_nritems(&l->header)) {
|
||||
if (slot >= btrfs_header_nritems(l)) {
|
||||
ret = btrfs_next_leaf(root, path);
|
||||
if (ret == 0)
|
||||
continue;
|
||||
|
@ -1075,7 +1079,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
goto check_pending;
|
||||
}
|
||||
|
||||
btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
|
||||
btrfs_item_key_to_cpu(l, &key, slot);
|
||||
if (key.objectid >= search_start && key.objectid > last_block &&
|
||||
start_found) {
|
||||
if (last_block < search_start)
|
||||
|
@ -1183,8 +1187,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_root *extent_root = info->extent_root;
|
||||
struct btrfs_extent_item extent_item;
|
||||
|
||||
btrfs_set_extent_refs(&extent_item, 1);
|
||||
btrfs_set_extent_owner(&extent_item, owner);
|
||||
btrfs_set_stack_extent_refs(&extent_item, 1);
|
||||
btrfs_set_stack_extent_owner(&extent_item, owner);
|
||||
|
||||
WARN_ON(num_blocks < 1);
|
||||
ret = find_free_extent(trans, root, num_blocks, empty_size,
|
||||
|
@ -1201,8 +1205,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
|
|||
num_blocks);
|
||||
|
||||
/* block accounting for root item */
|
||||
root_blocks_used = btrfs_root_blocks_used(&root->root_item);
|
||||
btrfs_set_root_blocks_used(&root->root_item, root_blocks_used +
|
||||
root_blocks_used = btrfs_root_used(&root->root_item);
|
||||
btrfs_set_root_used(&root->root_item, root_blocks_used +
|
||||
num_blocks);
|
||||
|
||||
if (root == extent_root) {
|
||||
|
@ -1241,13 +1245,13 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
|
|||
* helper function to allocate a block for a given tree
|
||||
* returns the tree buffer or NULL.
|
||||
*/
|
||||
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, u64 hint,
|
||||
u64 empty_size)
|
||||
struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, u64 hint,
|
||||
u64 empty_size)
|
||||
{
|
||||
struct btrfs_key ins;
|
||||
int ret;
|
||||
struct buffer_head *buf;
|
||||
struct extent_buffer *buf;
|
||||
|
||||
ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
|
||||
1, empty_size, hint, (u64)-1, &ins, 0);
|
||||
|
@ -1260,53 +1264,57 @@ struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
|
|||
btrfs_free_extent(trans, root, ins.objectid, 1, 0);
|
||||
return ERR_PTR(-ENOMEM);
|
||||
}
|
||||
WARN_ON(buffer_dirty(buf));
|
||||
set_buffer_uptodate(buf);
|
||||
btrfs_set_buffer_uptodate(buf);
|
||||
set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
|
||||
buf->start + buf->len - 1, GFP_NOFS);
|
||||
/*
|
||||
set_buffer_checked(buf);
|
||||
set_buffer_defrag(buf);
|
||||
set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
|
||||
*/
|
||||
/* FIXME!!!!!!!!!!!!!!!!
|
||||
set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
|
||||
*/
|
||||
trans->blocks_used++;
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int drop_leaf_ref(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, struct buffer_head *cur)
|
||||
struct btrfs_root *root, struct extent_buffer *leaf)
|
||||
{
|
||||
struct btrfs_disk_key *key;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_file_extent_item *fi;
|
||||
int i;
|
||||
int nritems;
|
||||
int ret;
|
||||
|
||||
BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
|
||||
leaf = btrfs_buffer_leaf(cur);
|
||||
nritems = btrfs_header_nritems(&leaf->header);
|
||||
BUG_ON(!btrfs_is_leaf(leaf));
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
for (i = 0; i < nritems; i++) {
|
||||
u64 disk_blocknr;
|
||||
key = &leaf->items[i].key;
|
||||
if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
|
||||
|
||||
btrfs_item_key_to_cpu(leaf, &key, i);
|
||||
if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
|
||||
continue;
|
||||
fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
|
||||
if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
|
||||
if (btrfs_file_extent_type(leaf, fi) ==
|
||||
BTRFS_FILE_EXTENT_INLINE)
|
||||
continue;
|
||||
/*
|
||||
* FIXME make sure to insert a trans record that
|
||||
* repeats the snapshot del on crash
|
||||
*/
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
|
||||
disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
|
||||
if (disk_blocknr == 0)
|
||||
continue;
|
||||
ret = btrfs_free_extent(trans, root, disk_blocknr,
|
||||
btrfs_file_extent_disk_num_blocks(fi),
|
||||
0);
|
||||
btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
|
||||
BUG_ON(ret);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void reada_walk_down(struct btrfs_root *root,
|
||||
struct btrfs_node *node)
|
||||
struct extent_buffer *node)
|
||||
{
|
||||
int i;
|
||||
u32 nritems;
|
||||
|
@ -1314,7 +1322,7 @@ static void reada_walk_down(struct btrfs_root *root,
|
|||
int ret;
|
||||
u32 refs;
|
||||
|
||||
nritems = btrfs_header_nritems(&node->header);
|
||||
nritems = btrfs_header_nritems(node);
|
||||
for (i = 0; i < nritems; i++) {
|
||||
blocknr = btrfs_node_blockptr(node, i);
|
||||
ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
|
||||
|
@ -1337,16 +1345,17 @@ static void reada_walk_down(struct btrfs_root *root,
|
|||
static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
||||
*root, struct btrfs_path *path, int *level)
|
||||
{
|
||||
struct buffer_head *next;
|
||||
struct buffer_head *cur;
|
||||
struct extent_buffer *next;
|
||||
struct extent_buffer *cur;
|
||||
u64 blocknr;
|
||||
int ret;
|
||||
u32 refs;
|
||||
|
||||
WARN_ON(*level < 0);
|
||||
WARN_ON(*level >= BTRFS_MAX_LEVEL);
|
||||
ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
|
||||
1, &refs);
|
||||
ret = lookup_extent_ref(trans, root,
|
||||
extent_buffer_blocknr(path->nodes[*level]),
|
||||
1, &refs);
|
||||
BUG_ON(ret);
|
||||
if (refs > 1)
|
||||
goto out;
|
||||
|
@ -1360,21 +1369,20 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
cur = path->nodes[*level];
|
||||
|
||||
if (*level > 0 && path->slots[*level] == 0)
|
||||
reada_walk_down(root, btrfs_buffer_node(cur));
|
||||
reada_walk_down(root, cur);
|
||||
|
||||
if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
|
||||
if (btrfs_header_level(cur) != *level)
|
||||
WARN_ON(1);
|
||||
|
||||
if (path->slots[*level] >=
|
||||
btrfs_header_nritems(btrfs_buffer_header(cur)))
|
||||
btrfs_header_nritems(cur))
|
||||
break;
|
||||
if (*level == 0) {
|
||||
ret = drop_leaf_ref(trans, root, cur);
|
||||
BUG_ON(ret);
|
||||
break;
|
||||
}
|
||||
blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
|
||||
path->slots[*level]);
|
||||
blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
|
||||
ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
|
||||
BUG_ON(ret);
|
||||
if (refs != 1) {
|
||||
|
@ -1384,8 +1392,8 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
continue;
|
||||
}
|
||||
next = btrfs_find_tree_block(root, blocknr);
|
||||
if (!next || !buffer_uptodate(next)) {
|
||||
brelse(next);
|
||||
if (!next || !btrfs_buffer_uptodate(next)) {
|
||||
free_extent_buffer(next);
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
next = read_tree_block(root, blocknr);
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
|
@ -1395,7 +1403,7 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
BUG_ON(ret);
|
||||
if (refs != 1) {
|
||||
path->slots[*level]++;
|
||||
brelse(next);
|
||||
free_extent_buffer(next);
|
||||
ret = btrfs_free_extent(trans, root,
|
||||
blocknr, 1, 1);
|
||||
BUG_ON(ret);
|
||||
|
@ -1404,17 +1412,17 @@ static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
}
|
||||
WARN_ON(*level <= 0);
|
||||
if (path->nodes[*level-1])
|
||||
btrfs_block_release(root, path->nodes[*level-1]);
|
||||
free_extent_buffer(path->nodes[*level-1]);
|
||||
path->nodes[*level-1] = next;
|
||||
*level = btrfs_header_level(btrfs_buffer_header(next));
|
||||
*level = btrfs_header_level(next);
|
||||
path->slots[*level] = 0;
|
||||
}
|
||||
out:
|
||||
WARN_ON(*level < 0);
|
||||
WARN_ON(*level >= BTRFS_MAX_LEVEL);
|
||||
ret = btrfs_free_extent(trans, root,
|
||||
bh_blocknr(path->nodes[*level]), 1, 1);
|
||||
btrfs_block_release(root, path->nodes[*level]);
|
||||
extent_buffer_blocknr(path->nodes[*level]), 1, 1);
|
||||
free_extent_buffer(path->nodes[*level]);
|
||||
path->nodes[*level] = NULL;
|
||||
*level += 1;
|
||||
BUG_ON(ret);
|
||||
|
@ -1436,24 +1444,24 @@ static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
|
||||
for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
|
||||
slot = path->slots[i];
|
||||
if (slot < btrfs_header_nritems(
|
||||
btrfs_buffer_header(path->nodes[i])) - 1) {
|
||||
struct btrfs_node *node;
|
||||
node = btrfs_buffer_node(path->nodes[i]);
|
||||
if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
|
||||
struct extent_buffer *node;
|
||||
struct btrfs_disk_key disk_key;
|
||||
node = path->nodes[i];
|
||||
path->slots[i]++;
|
||||
*level = i;
|
||||
WARN_ON(*level == 0);
|
||||
btrfs_node_key(node, &disk_key, path->slots[i]);
|
||||
memcpy(&root_item->drop_progress,
|
||||
&node->ptrs[path->slots[i]].key,
|
||||
sizeof(root_item->drop_progress));
|
||||
&disk_key, sizeof(disk_key));
|
||||
root_item->drop_level = i;
|
||||
return 0;
|
||||
} else {
|
||||
ret = btrfs_free_extent(trans, root,
|
||||
bh_blocknr(path->nodes[*level]),
|
||||
1, 1);
|
||||
extent_buffer_blocknr(path->nodes[*level]),
|
||||
1, 1);
|
||||
BUG_ON(ret);
|
||||
btrfs_block_release(root, path->nodes[*level]);
|
||||
free_extent_buffer(path->nodes[*level]);
|
||||
path->nodes[*level] = NULL;
|
||||
*level = i + 1;
|
||||
}
|
||||
|
@ -1480,15 +1488,15 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
|
||||
level = btrfs_header_level(btrfs_buffer_header(root->node));
|
||||
level = btrfs_header_level(root->node);
|
||||
orig_level = level;
|
||||
if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
|
||||
path->nodes[level] = root->node;
|
||||
path->slots[level] = 0;
|
||||
} else {
|
||||
struct btrfs_key key;
|
||||
struct btrfs_disk_key *found_key;
|
||||
struct btrfs_node *node;
|
||||
struct btrfs_disk_key found_key;
|
||||
struct extent_buffer *node;
|
||||
|
||||
btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
|
||||
level = root_item->drop_level;
|
||||
|
@ -1498,10 +1506,10 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
ret = wret;
|
||||
goto out;
|
||||
}
|
||||
node = btrfs_buffer_node(path->nodes[level]);
|
||||
found_key = &node->ptrs[path->slots[level]].key;
|
||||
WARN_ON(memcmp(found_key, &root_item->drop_progress,
|
||||
sizeof(*found_key)));
|
||||
node = path->nodes[level];
|
||||
btrfs_node_key(node, &found_key, path->slots[level]);
|
||||
WARN_ON(memcmp(&found_key, &root_item->drop_progress,
|
||||
sizeof(found_key)));
|
||||
}
|
||||
while(1) {
|
||||
wret = walk_down_tree(trans, root, path, &level);
|
||||
|
@ -1516,12 +1524,12 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
if (wret < 0)
|
||||
ret = wret;
|
||||
ret = -EAGAIN;
|
||||
get_bh(root->node);
|
||||
extent_buffer_get(root->node);
|
||||
break;
|
||||
}
|
||||
for (i = 0; i <= orig_level; i++) {
|
||||
if (path->nodes[i]) {
|
||||
btrfs_block_release(root, path->nodes[i]);
|
||||
free_extent_buffer(path->nodes[i]);
|
||||
path->nodes[i] = 0;
|
||||
}
|
||||
}
|
||||
|
@ -1581,13 +1589,12 @@ int btrfs_read_block_groups(struct btrfs_root *root)
|
|||
struct btrfs_path *path;
|
||||
int ret;
|
||||
int err = 0;
|
||||
struct btrfs_block_group_item *bi;
|
||||
struct btrfs_block_group_cache *cache;
|
||||
struct btrfs_fs_info *info = root->fs_info;
|
||||
struct radix_tree_root *radix;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_key found_key;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
u64 group_size_blocks;
|
||||
u64 used;
|
||||
|
||||
|
@ -1596,7 +1603,6 @@ int btrfs_read_block_groups(struct btrfs_root *root)
|
|||
root = info->extent_root;
|
||||
key.objectid = 0;
|
||||
key.offset = group_size_blocks;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -1610,18 +1616,18 @@ int btrfs_read_block_groups(struct btrfs_root *root)
|
|||
err = ret;
|
||||
break;
|
||||
}
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
btrfs_disk_key_to_cpu(&found_key,
|
||||
&leaf->items[path->slots[0]].key);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
cache = kmalloc(sizeof(*cache), GFP_NOFS);
|
||||
if (!cache) {
|
||||
err = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
bi = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_block_group_item);
|
||||
if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
|
||||
read_extent_buffer(leaf, &cache->item,
|
||||
btrfs_item_ptr_offset(leaf, path->slots[0]),
|
||||
sizeof(cache->item));
|
||||
if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
|
||||
radix = &info->block_group_data_radix;
|
||||
cache->data = 1;
|
||||
} else {
|
||||
|
@ -1629,7 +1635,6 @@ int btrfs_read_block_groups(struct btrfs_root *root)
|
|||
cache->data = 0;
|
||||
}
|
||||
|
||||
memcpy(&cache->item, bi, sizeof(*bi));
|
||||
memcpy(&cache->key, &found_key, sizeof(found_key));
|
||||
cache->last_alloc = cache->key.objectid;
|
||||
cache->first_free = cache->key.objectid;
|
||||
|
@ -1640,11 +1645,12 @@ int btrfs_read_block_groups(struct btrfs_root *root)
|
|||
|
||||
key.objectid = found_key.objectid + found_key.offset;
|
||||
btrfs_release_path(root, path);
|
||||
|
||||
ret = radix_tree_insert(radix, found_key.objectid +
|
||||
found_key.offset - 1,
|
||||
(void *)cache);
|
||||
BUG_ON(ret);
|
||||
used = btrfs_block_group_used(bi);
|
||||
used = btrfs_block_group_used(&cache->item);
|
||||
if (used < div_factor(key.offset, 8)) {
|
||||
radix_tree_tag_set(radix, found_key.objectid +
|
||||
found_key.offset - 1,
|
||||
|
|
|
@ -26,16 +26,6 @@ struct tree_entry {
|
|||
struct rb_node rb_node;
|
||||
};
|
||||
|
||||
/* bits for the extent state */
|
||||
#define EXTENT_DIRTY 1
|
||||
#define EXTENT_WRITEBACK (1 << 1)
|
||||
#define EXTENT_UPTODATE (1 << 2)
|
||||
#define EXTENT_LOCKED (1 << 3)
|
||||
#define EXTENT_NEW (1 << 4)
|
||||
#define EXTENT_DELALLOC (1 << 5)
|
||||
|
||||
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
|
||||
|
||||
void __init extent_map_init(void)
|
||||
{
|
||||
extent_map_cache = btrfs_cache_create("extent_map",
|
||||
|
@ -223,7 +213,8 @@ int add_extent_mapping(struct extent_map_tree *tree,
|
|||
if (rb)
|
||||
prev = rb_entry(rb, struct extent_map, rb_node);
|
||||
if (prev && prev->end + 1 == em->start &&
|
||||
((em->block_start == 0 && prev->block_start == 0) ||
|
||||
((em->block_start == EXTENT_MAP_HOLE &&
|
||||
prev->block_start == EXTENT_MAP_HOLE) ||
|
||||
(em->block_start == prev->block_end + 1))) {
|
||||
em->start = prev->start;
|
||||
em->block_start = prev->block_start;
|
||||
|
@ -926,6 +917,40 @@ int set_range_writeback(struct extent_map_tree *tree, u64 start, u64 end)
|
|||
}
|
||||
EXPORT_SYMBOL(set_range_writeback);
|
||||
|
||||
int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
|
||||
u64 *start_ret, u64 *end_ret, int bits)
|
||||
{
|
||||
struct rb_node *node;
|
||||
struct extent_state *state;
|
||||
int ret = 1;
|
||||
|
||||
write_lock_irq(&tree->lock);
|
||||
/*
|
||||
* this search will find all the extents that end after
|
||||
* our range starts.
|
||||
*/
|
||||
node = tree_search(&tree->state, start);
|
||||
if (!node || IS_ERR(node)) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
state = rb_entry(node, struct extent_state, rb_node);
|
||||
if (state->state & bits) {
|
||||
*start_ret = state->start;
|
||||
*end_ret = state->end;
|
||||
ret = 0;
|
||||
}
|
||||
node = rb_next(node);
|
||||
if (!node)
|
||||
break;
|
||||
}
|
||||
out:
|
||||
write_unlock_irq(&tree->lock);
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(find_first_extent_bit);
|
||||
|
||||
u64 find_lock_delalloc_range(struct extent_map_tree *tree,
|
||||
u64 start, u64 lock_start, u64 *end, u64 max_bytes)
|
||||
{
|
||||
|
@ -1450,7 +1475,7 @@ int extent_read_full_page(struct extent_map_tree *tree, struct page *page,
|
|||
em = NULL;
|
||||
|
||||
/* we've found a hole, just zero and go on */
|
||||
if (block_start == 0) {
|
||||
if (block_start == EXTENT_MAP_HOLE) {
|
||||
zero_user_page(page, page_offset, iosize, KM_USER0);
|
||||
set_extent_uptodate(tree, cur, cur + iosize - 1,
|
||||
GFP_NOFS);
|
||||
|
@ -1593,7 +1618,8 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
|
|||
free_extent_map(em);
|
||||
em = NULL;
|
||||
|
||||
if (block_start == 0 || block_start == EXTENT_MAP_INLINE) {
|
||||
if (block_start == EXTENT_MAP_HOLE ||
|
||||
block_start == EXTENT_MAP_INLINE) {
|
||||
clear_extent_dirty(tree, cur,
|
||||
cur + iosize - 1, GFP_NOFS);
|
||||
cur = cur + iosize;
|
||||
|
@ -1630,7 +1656,6 @@ int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
|
|||
nr++;
|
||||
}
|
||||
done:
|
||||
WARN_ON(test_range_bit(tree, start, page_end, EXTENT_DIRTY, 0));
|
||||
unlock_extent(tree, start, page_end, GFP_NOFS);
|
||||
unlock_page(page);
|
||||
return 0;
|
||||
|
@ -1827,8 +1852,623 @@ sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
|
|||
|
||||
// XXX(hch): block 0 is valid in some cases, e.g. XFS RT device
|
||||
if (em->block_start == EXTENT_MAP_INLINE ||
|
||||
em->block_start == 0)
|
||||
return 0;
|
||||
em->block_start == EXTENT_MAP_HOLE)
|
||||
return 0;
|
||||
|
||||
return (em->block_start + start - em->start) >> inode->i_blkbits;
|
||||
}
|
||||
|
||||
struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
|
||||
u64 start, unsigned long len,
|
||||
gfp_t mask)
|
||||
{
|
||||
unsigned long num_pages = ((start + len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(start >> PAGE_CACHE_SHIFT) + 1;
|
||||
unsigned long i;
|
||||
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
||||
struct extent_buffer *eb;
|
||||
struct page *p;
|
||||
struct address_space *mapping = tree->mapping;
|
||||
int uptodate = 0;
|
||||
|
||||
eb = kzalloc(EXTENT_BUFFER_SIZE(num_pages), mask);
|
||||
if (!eb || IS_ERR(eb))
|
||||
return NULL;
|
||||
|
||||
eb->start = start;
|
||||
eb->len = len;
|
||||
atomic_set(&eb->refs, 1);
|
||||
|
||||
for (i = 0; i < num_pages; i++, index++) {
|
||||
p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
|
||||
if (!p)
|
||||
goto fail;
|
||||
eb->pages[i] = p;
|
||||
if (!PageUptodate(p))
|
||||
uptodate = 0;
|
||||
unlock_page(p);
|
||||
}
|
||||
if (uptodate)
|
||||
eb->flags |= EXTENT_UPTODATE;
|
||||
return eb;
|
||||
fail:
|
||||
free_extent_buffer(eb);
|
||||
return NULL;
|
||||
}
|
||||
EXPORT_SYMBOL(alloc_extent_buffer);
|
||||
|
||||
struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
|
||||
u64 start, unsigned long len,
|
||||
gfp_t mask)
|
||||
{
|
||||
unsigned long num_pages = ((start + len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(start >> PAGE_CACHE_SHIFT) + 1;
|
||||
unsigned long i;
|
||||
unsigned long index = start >> PAGE_CACHE_SHIFT;
|
||||
struct extent_buffer *eb;
|
||||
struct page *p;
|
||||
struct address_space *mapping = tree->mapping;
|
||||
|
||||
eb = kzalloc(EXTENT_BUFFER_SIZE(num_pages), mask);
|
||||
if (!eb || IS_ERR(eb))
|
||||
return NULL;
|
||||
|
||||
eb->start = start;
|
||||
eb->len = len;
|
||||
atomic_set(&eb->refs, 1);
|
||||
|
||||
for (i = 0; i < num_pages; i++, index++) {
|
||||
p = find_get_page(mapping, index);
|
||||
if (!p)
|
||||
goto fail;
|
||||
eb->pages[i] = p;
|
||||
}
|
||||
return eb;
|
||||
fail:
|
||||
free_extent_buffer(eb);
|
||||
return NULL;
|
||||
}
|
||||
EXPORT_SYMBOL(find_extent_buffer);
|
||||
|
||||
void free_extent_buffer(struct extent_buffer *eb)
|
||||
{
|
||||
unsigned long i;
|
||||
unsigned long num_pages;
|
||||
|
||||
if (!eb)
|
||||
return;
|
||||
|
||||
if (!atomic_dec_and_test(&eb->refs))
|
||||
return;
|
||||
|
||||
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(eb->start >> PAGE_CACHE_SHIFT) + 1;
|
||||
|
||||
for (i = 0; i < num_pages; i++) {
|
||||
if (eb->pages[i])
|
||||
page_cache_release(eb->pages[i]);
|
||||
}
|
||||
kfree(eb);
|
||||
}
|
||||
EXPORT_SYMBOL(free_extent_buffer);
|
||||
|
||||
int clear_extent_buffer_dirty(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb)
|
||||
{
|
||||
int set;
|
||||
unsigned long i;
|
||||
unsigned long num_pages;
|
||||
struct page *page;
|
||||
|
||||
u64 start = eb->start;
|
||||
u64 end = start + eb->len - 1;
|
||||
|
||||
set = clear_extent_dirty(tree, start, end, GFP_NOFS);
|
||||
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(eb->start >> PAGE_CACHE_SHIFT) + 1;
|
||||
|
||||
for (i = 0; i < num_pages; i++) {
|
||||
page = eb->pages[i];
|
||||
lock_page(page);
|
||||
/*
|
||||
* if we're on the last page or the first page and the
|
||||
* block isn't aligned on a page boundary, do extra checks
|
||||
* to make sure we don't clean page that is partially dirty
|
||||
*/
|
||||
if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
|
||||
((i == num_pages - 1) &&
|
||||
((eb->start + eb->len - 1) & (PAGE_CACHE_SIZE - 1)))) {
|
||||
start = page->index << PAGE_CACHE_SHIFT;
|
||||
end = start + PAGE_CACHE_SIZE - 1;
|
||||
if (test_range_bit(tree, start, end,
|
||||
EXTENT_DIRTY, 0)) {
|
||||
unlock_page(page);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
clear_page_dirty_for_io(page);
|
||||
unlock_page(page);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(clear_extent_buffer_dirty);
|
||||
|
||||
int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb)
|
||||
{
|
||||
return wait_on_extent_writeback(tree, eb->start,
|
||||
eb->start + eb->len - 1);
|
||||
}
|
||||
EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
|
||||
|
||||
int set_extent_buffer_dirty(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb)
|
||||
{
|
||||
return set_range_dirty(tree, eb->start, eb->start + eb->len - 1);
|
||||
}
|
||||
EXPORT_SYMBOL(set_extent_buffer_dirty);
|
||||
|
||||
int set_extent_buffer_uptodate(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb)
|
||||
{
|
||||
unsigned long i;
|
||||
struct page *page;
|
||||
unsigned long num_pages;
|
||||
|
||||
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(eb->start >> PAGE_CACHE_SHIFT) + 1;
|
||||
|
||||
set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
|
||||
GFP_NOFS);
|
||||
for (i = 0; i < num_pages; i++) {
|
||||
page = eb->pages[i];
|
||||
if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
|
||||
((i == num_pages - 1) &&
|
||||
((eb->start + eb->len - 1) & (PAGE_CACHE_SIZE - 1)))) {
|
||||
check_page_uptodate(tree, page);
|
||||
continue;
|
||||
}
|
||||
SetPageUptodate(page);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(set_extent_buffer_uptodate);
|
||||
|
||||
int extent_buffer_uptodate(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb)
|
||||
{
|
||||
if (eb->flags & EXTENT_UPTODATE)
|
||||
return 1;
|
||||
return test_range_bit(tree, eb->start, eb->start + eb->len - 1,
|
||||
EXTENT_UPTODATE, 1);
|
||||
}
|
||||
EXPORT_SYMBOL(extent_buffer_uptodate);
|
||||
|
||||
int read_extent_buffer_pages(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb, int wait)
|
||||
{
|
||||
unsigned long i;
|
||||
struct page *page;
|
||||
int err;
|
||||
int ret = 0;
|
||||
unsigned long num_pages;
|
||||
|
||||
if (eb->flags & EXTENT_UPTODATE)
|
||||
return 0;
|
||||
|
||||
if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
|
||||
EXTENT_UPTODATE, 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
num_pages = ((eb->start + eb->len - 1) >> PAGE_CACHE_SHIFT) -
|
||||
(eb->start >> PAGE_CACHE_SHIFT) + 1;
|
||||
for (i = 0; i < num_pages; i++) {
|
||||
page = eb->pages[i];
|
||||
if (PageUptodate(page)) {
|
||||
continue;
|
||||
}
|
||||
if (!wait) {
|
||||
if (TestSetPageLocked(page)) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
lock_page(page);
|
||||
}
|
||||
if (!PageUptodate(page)) {
|
||||
err = page->mapping->a_ops->readpage(NULL, page);
|
||||
if (err) {
|
||||
ret = err;
|
||||
}
|
||||
} else {
|
||||
unlock_page(page);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret || !wait) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_pages; i++) {
|
||||
page = eb->pages[i];
|
||||
wait_on_page_locked(page);
|
||||
if (!PageUptodate(page)) {
|
||||
ret = -EIO;
|
||||
}
|
||||
}
|
||||
eb->flags |= EXTENT_UPTODATE;
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(read_extent_buffer_pages);
|
||||
|
||||
void read_extent_buffer(struct extent_buffer *eb, void *dstv,
|
||||
unsigned long start,
|
||||
unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t offset;
|
||||
struct page *page;
|
||||
char *kaddr;
|
||||
char *dst = (char *)dstv;
|
||||
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
WARN_ON(start > eb->len);
|
||||
WARN_ON(start + len > eb->start + eb->len);
|
||||
|
||||
page = eb->pages[i];
|
||||
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
if (i == 0)
|
||||
offset += start_offset;
|
||||
|
||||
while(len > 0) {
|
||||
WARN_ON(!PageUptodate(page));
|
||||
|
||||
cur = min(len, (PAGE_CACHE_SIZE - offset));
|
||||
// kaddr = kmap_atomic(page, KM_USER0);
|
||||
kaddr = page_address(page);
|
||||
memcpy(dst, kaddr + offset, cur);
|
||||
// kunmap_atomic(kaddr, KM_USER0);
|
||||
|
||||
dst += cur;
|
||||
len -= cur;
|
||||
offset = 0;
|
||||
i++;
|
||||
page = eb->pages[i];
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(read_extent_buffer);
|
||||
|
||||
int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
|
||||
char **token, char **map,
|
||||
unsigned long *map_start,
|
||||
unsigned long *map_len, int km)
|
||||
{
|
||||
size_t offset;
|
||||
char *kaddr;
|
||||
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
WARN_ON(start > eb->len);
|
||||
|
||||
if (i == 0) {
|
||||
offset = start_offset;
|
||||
*map_start = 0;
|
||||
} else {
|
||||
offset = 0;
|
||||
*map_start = (i << PAGE_CACHE_SHIFT) - offset;
|
||||
}
|
||||
|
||||
// kaddr = kmap_atomic(eb->pages[i], km);
|
||||
kaddr = page_address(eb->pages[i]);
|
||||
*token = kaddr;
|
||||
*map = kaddr + offset;
|
||||
*map_len = PAGE_CACHE_SIZE - offset;
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(map_extent_buffer);
|
||||
|
||||
void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
|
||||
{
|
||||
// kunmap_atomic(token, km);
|
||||
}
|
||||
EXPORT_SYMBOL(unmap_extent_buffer);
|
||||
|
||||
int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
|
||||
unsigned long start,
|
||||
unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t offset;
|
||||
struct page *page;
|
||||
char *kaddr;
|
||||
char *ptr = (char *)ptrv;
|
||||
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
||||
int ret = 0;
|
||||
|
||||
WARN_ON(start > eb->len);
|
||||
WARN_ON(start + len > eb->start + eb->len);
|
||||
|
||||
page = eb->pages[i];
|
||||
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
if (i == 0)
|
||||
offset += start_offset;
|
||||
|
||||
while(len > 0) {
|
||||
WARN_ON(!PageUptodate(page));
|
||||
|
||||
cur = min(len, (PAGE_CACHE_SIZE - offset));
|
||||
|
||||
// kaddr = kmap_atomic(page, KM_USER0);
|
||||
kaddr = page_address(page);
|
||||
ret = memcmp(ptr, kaddr + offset, cur);
|
||||
// kunmap_atomic(kaddr, KM_USER0);
|
||||
if (ret)
|
||||
break;
|
||||
|
||||
ptr += cur;
|
||||
len -= cur;
|
||||
offset = 0;
|
||||
i++;
|
||||
page = eb->pages[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(memcmp_extent_buffer);
|
||||
|
||||
void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
|
||||
unsigned long start, unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t offset;
|
||||
struct page *page;
|
||||
char *kaddr;
|
||||
char *src = (char *)srcv;
|
||||
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
WARN_ON(start > eb->len);
|
||||
WARN_ON(start + len > eb->start + eb->len);
|
||||
|
||||
page = eb->pages[i];
|
||||
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
if (i == 0)
|
||||
offset += start_offset;
|
||||
|
||||
while(len > 0) {
|
||||
WARN_ON(!PageUptodate(page));
|
||||
|
||||
cur = min(len, PAGE_CACHE_SIZE - offset);
|
||||
// kaddr = kmap_atomic(page, KM_USER0);
|
||||
kaddr = page_address(page);
|
||||
memcpy(kaddr + offset, src, cur);
|
||||
// kunmap_atomic(kaddr, KM_USER0);
|
||||
|
||||
src += cur;
|
||||
len -= cur;
|
||||
offset = 0;
|
||||
i++;
|
||||
page = eb->pages[i];
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(write_extent_buffer);
|
||||
|
||||
void memset_extent_buffer(struct extent_buffer *eb, char c,
|
||||
unsigned long start, unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t offset;
|
||||
struct page *page;
|
||||
char *kaddr;
|
||||
size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
WARN_ON(start > eb->len);
|
||||
WARN_ON(start + len > eb->start + eb->len);
|
||||
|
||||
page = eb->pages[i];
|
||||
offset = start & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
if (i == 0)
|
||||
offset += start_offset;
|
||||
|
||||
while(len > 0) {
|
||||
WARN_ON(!PageUptodate(page));
|
||||
|
||||
cur = min(len, PAGE_CACHE_SIZE - offset);
|
||||
// kaddr = kmap_atomic(page, KM_USER0);
|
||||
kaddr = page_address(page);
|
||||
memset(kaddr + offset, c, cur);
|
||||
// kunmap_atomic(kaddr, KM_USER0);
|
||||
|
||||
len -= cur;
|
||||
offset = 0;
|
||||
i++;
|
||||
page = eb->pages[i];
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(memset_extent_buffer);
|
||||
|
||||
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
|
||||
unsigned long dst_offset, unsigned long src_offset,
|
||||
unsigned long len)
|
||||
{
|
||||
u64 dst_len = dst->len;
|
||||
size_t cur;
|
||||
size_t offset;
|
||||
struct page *page;
|
||||
char *kaddr;
|
||||
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
WARN_ON(src->len != dst_len);
|
||||
|
||||
offset = dst_offset & ((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
if (i == 0)
|
||||
offset += start_offset;
|
||||
|
||||
while(len > 0) {
|
||||
page = dst->pages[i];
|
||||
WARN_ON(!PageUptodate(page));
|
||||
|
||||
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
|
||||
|
||||
// kaddr = kmap_atomic(page, KM_USER1);
|
||||
kaddr = page_address(page);
|
||||
read_extent_buffer(src, kaddr + offset, src_offset, cur);
|
||||
// kunmap_atomic(kaddr, KM_USER1);
|
||||
|
||||
src_offset += cur;
|
||||
len -= cur;
|
||||
offset = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(copy_extent_buffer);
|
||||
|
||||
static void move_pages(struct page *dst_page, struct page *src_page,
|
||||
unsigned long dst_off, unsigned long src_off,
|
||||
unsigned long len)
|
||||
{
|
||||
// char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
|
||||
char *dst_kaddr = page_address(dst_page);
|
||||
if (dst_page == src_page) {
|
||||
memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
|
||||
} else {
|
||||
// char *src_kaddr = kmap_atomic(src_page, KM_USER1);
|
||||
char *src_kaddr = page_address(src_page);
|
||||
char *p = dst_kaddr + dst_off + len;
|
||||
char *s = src_kaddr + src_off + len;
|
||||
|
||||
while (len--)
|
||||
*--p = *--s;
|
||||
|
||||
// kunmap_atomic(src_kaddr, KM_USER1);
|
||||
}
|
||||
// kunmap_atomic(dst_kaddr, KM_USER0);
|
||||
}
|
||||
|
||||
static void copy_pages(struct page *dst_page, struct page *src_page,
|
||||
unsigned long dst_off, unsigned long src_off,
|
||||
unsigned long len)
|
||||
{
|
||||
//kmap_atomic(dst_page, KM_USER0);
|
||||
char *dst_kaddr = page_address(dst_page);
|
||||
char *src_kaddr;
|
||||
|
||||
if (dst_page != src_page)
|
||||
src_kaddr = page_address(src_page); // kmap_atomic(src_page, KM_USER1);
|
||||
else
|
||||
src_kaddr = dst_kaddr;
|
||||
|
||||
memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
|
||||
/*
|
||||
kunmap_atomic(dst_kaddr, KM_USER0);
|
||||
if (dst_page != src_page)
|
||||
kunmap_atomic(src_kaddr, KM_USER1);
|
||||
*/
|
||||
}
|
||||
|
||||
void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
||||
unsigned long src_offset, unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t dst_off_in_page;
|
||||
size_t src_off_in_page;
|
||||
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long dst_i;
|
||||
unsigned long src_i;
|
||||
|
||||
if (src_offset + len > dst->len) {
|
||||
printk("memmove bogus src_offset %lu move len %lu len %lu\n",
|
||||
src_offset, len, dst->len);
|
||||
BUG_ON(1);
|
||||
}
|
||||
if (dst_offset + len > dst->len) {
|
||||
printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
|
||||
dst_offset, len, dst->len);
|
||||
BUG_ON(1);
|
||||
}
|
||||
|
||||
while(len > 0) {
|
||||
dst_off_in_page = dst_offset &
|
||||
((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
src_off_in_page = src_offset &
|
||||
((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
|
||||
dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
|
||||
src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
if (src_i == 0)
|
||||
src_off_in_page += start_offset;
|
||||
if (dst_i == 0)
|
||||
dst_off_in_page += start_offset;
|
||||
|
||||
cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
|
||||
src_off_in_page));
|
||||
cur = min(cur, (unsigned long)(PAGE_CACHE_SIZE -
|
||||
dst_off_in_page));
|
||||
|
||||
copy_pages(dst->pages[dst_i], dst->pages[src_i],
|
||||
dst_off_in_page, src_off_in_page, cur);
|
||||
|
||||
src_offset += cur;
|
||||
dst_offset += cur;
|
||||
len -= cur;
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(memcpy_extent_buffer);
|
||||
|
||||
void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
||||
unsigned long src_offset, unsigned long len)
|
||||
{
|
||||
size_t cur;
|
||||
size_t dst_off_in_page;
|
||||
size_t src_off_in_page;
|
||||
unsigned long dst_end = dst_offset + len - 1;
|
||||
unsigned long src_end = src_offset + len - 1;
|
||||
size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
|
||||
unsigned long dst_i;
|
||||
unsigned long src_i;
|
||||
|
||||
if (src_offset + len > dst->len) {
|
||||
printk("memmove bogus src_offset %lu move len %lu len %lu\n",
|
||||
src_offset, len, dst->len);
|
||||
BUG_ON(1);
|
||||
}
|
||||
if (dst_offset + len > dst->len) {
|
||||
printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
|
||||
dst_offset, len, dst->len);
|
||||
BUG_ON(1);
|
||||
}
|
||||
if (dst_offset < src_offset) {
|
||||
memcpy_extent_buffer(dst, dst_offset, src_offset, len);
|
||||
return;
|
||||
}
|
||||
while(len > 0) {
|
||||
dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
|
||||
src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
|
||||
|
||||
dst_off_in_page = dst_end &
|
||||
((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
src_off_in_page = src_end &
|
||||
((unsigned long)PAGE_CACHE_SIZE - 1);
|
||||
|
||||
if (src_i == 0)
|
||||
src_off_in_page += start_offset;
|
||||
if (dst_i == 0)
|
||||
dst_off_in_page += start_offset;
|
||||
|
||||
cur = min(len, src_off_in_page + 1);
|
||||
cur = min(cur, dst_off_in_page + 1);
|
||||
// printk("move pages orig dst %lu src %lu len %lu, this %lu %lu %lu\n", dst_offset, src_offset, len, dst_off_in_page - cur + 1, src_off_in_page - cur + 1, cur);
|
||||
move_pages(dst->pages[dst_i], dst->pages[src_i],
|
||||
dst_off_in_page - cur + 1,
|
||||
src_off_in_page - cur + 1, cur);
|
||||
|
||||
dst_end -= cur - 1;
|
||||
src_end -= cur - 1;
|
||||
len -= cur;
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL(memmove_extent_buffer);
|
||||
|
|
|
@ -3,9 +3,20 @@
|
|||
|
||||
#include <linux/rbtree.h>
|
||||
|
||||
#define EXTENT_MAP_HOLE (u64)-3
|
||||
#define EXTENT_MAP_INLINE (u64)-2
|
||||
#define EXTENT_MAP_DELALLOC (u64)-1
|
||||
|
||||
/* bits for the extent state */
|
||||
#define EXTENT_DIRTY 1
|
||||
#define EXTENT_WRITEBACK (1 << 1)
|
||||
#define EXTENT_UPTODATE (1 << 2)
|
||||
#define EXTENT_LOCKED (1 << 3)
|
||||
#define EXTENT_NEW (1 << 4)
|
||||
#define EXTENT_DELALLOC (1 << 5)
|
||||
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
|
||||
|
||||
|
||||
struct extent_map_ops {
|
||||
int (*fill_delalloc)(struct inode *inode, u64 start, u64 end);
|
||||
int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
|
||||
|
@ -51,10 +62,13 @@ struct extent_state {
|
|||
struct list_head list;
|
||||
};
|
||||
|
||||
#define EXTENT_BUFFER_SIZE(nr) (sizeof(struct extent_buffer) + \
|
||||
(nr - 1) * sizeof(struct page *))
|
||||
struct extent_buffer {
|
||||
u64 start;
|
||||
u64 end; /* inclusive */
|
||||
char *addr;
|
||||
unsigned long len;
|
||||
atomic_t refs;
|
||||
int flags;
|
||||
struct page *pages[];
|
||||
};
|
||||
|
||||
|
@ -87,8 +101,12 @@ int set_extent_new(struct extent_map_tree *tree, u64 start, u64 end,
|
|||
gfp_t mask);
|
||||
int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
|
||||
gfp_t mask);
|
||||
int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
|
||||
gfp_t mask);
|
||||
int set_extent_delalloc(struct extent_map_tree *tree, u64 start, u64 end,
|
||||
gfp_t mask);
|
||||
int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
|
||||
u64 *start_ret, u64 *end_ret, int bits);
|
||||
int extent_invalidatepage(struct extent_map_tree *tree,
|
||||
struct page *page, unsigned long offset);
|
||||
int extent_write_full_page(struct extent_map_tree *tree, struct page *page,
|
||||
|
@ -106,4 +124,57 @@ int set_range_dirty(struct extent_map_tree *tree, u64 start, u64 end);
|
|||
int set_state_private(struct extent_map_tree *tree, u64 start, u64 private);
|
||||
int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private);
|
||||
void set_page_extent_mapped(struct page *page);
|
||||
|
||||
struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
|
||||
u64 start, unsigned long len,
|
||||
gfp_t mask);
|
||||
struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
|
||||
u64 start, unsigned long len,
|
||||
gfp_t mask);
|
||||
void free_extent_buffer(struct extent_buffer *eb);
|
||||
int read_extent_buffer_pages(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb, int wait);
|
||||
|
||||
static inline void extent_buffer_get(struct extent_buffer *eb)
|
||||
{
|
||||
atomic_inc(&eb->refs);
|
||||
}
|
||||
|
||||
static inline u64 extent_buffer_blocknr(struct extent_buffer *eb)
|
||||
{
|
||||
return eb->start / 4096;
|
||||
}
|
||||
|
||||
int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
|
||||
unsigned long start,
|
||||
unsigned long len);
|
||||
void read_extent_buffer(struct extent_buffer *eb, void *dst,
|
||||
unsigned long start,
|
||||
unsigned long len);
|
||||
void write_extent_buffer(struct extent_buffer *eb, const void *src,
|
||||
unsigned long start, unsigned long len);
|
||||
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
|
||||
unsigned long dst_offset, unsigned long src_offset,
|
||||
unsigned long len);
|
||||
void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
||||
unsigned long src_offset, unsigned long len);
|
||||
void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
|
||||
unsigned long src_offset, unsigned long len);
|
||||
void memset_extent_buffer(struct extent_buffer *eb, char c,
|
||||
unsigned long start, unsigned long len);
|
||||
int wait_on_extent_buffer_writeback(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb);
|
||||
int clear_extent_buffer_dirty(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb);
|
||||
int set_extent_buffer_dirty(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb);
|
||||
int set_extent_buffer_uptodate(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb);
|
||||
int extent_buffer_uptodate(struct extent_map_tree *tree,
|
||||
struct extent_buffer *eb);
|
||||
int map_extent_buffer(struct extent_buffer *eb, unsigned long offset,
|
||||
char **token, char **map,
|
||||
unsigned long *map_start,
|
||||
unsigned long *map_len, int km);
|
||||
void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km);
|
||||
#endif
|
||||
|
|
|
@ -34,12 +34,12 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_file_extent_item *item;
|
||||
struct btrfs_key file_key;
|
||||
struct btrfs_path *path;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
file_key.objectid = objectid;
|
||||
file_key.offset = pos;
|
||||
file_key.flags = 0;
|
||||
btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
|
||||
|
||||
ret = btrfs_insert_empty_item(trans, root, path, &file_key,
|
||||
|
@ -47,15 +47,16 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
|
|||
if (ret < 0)
|
||||
goto out;
|
||||
BUG_ON(ret);
|
||||
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
|
||||
leaf = path->nodes[0];
|
||||
item = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_disk_blocknr(item, offset);
|
||||
btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks);
|
||||
btrfs_set_file_extent_offset(item, 0);
|
||||
btrfs_set_file_extent_num_blocks(item, num_blocks);
|
||||
btrfs_set_file_extent_generation(item, trans->transid);
|
||||
btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
btrfs_set_file_extent_disk_blocknr(leaf, item, offset);
|
||||
btrfs_set_file_extent_disk_num_blocks(leaf, item, disk_num_blocks);
|
||||
btrfs_set_file_extent_offset(leaf, item, 0);
|
||||
btrfs_set_file_extent_num_blocks(leaf, item, num_blocks);
|
||||
btrfs_set_file_extent_generation(leaf, item, trans->transid);
|
||||
btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
out:
|
||||
btrfs_free_path(path);
|
||||
return ret;
|
||||
|
@ -71,32 +72,30 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_key file_key;
|
||||
struct btrfs_key found_key;
|
||||
struct btrfs_csum_item *item;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
u64 csum_offset = 0;
|
||||
int csums_in_item;
|
||||
|
||||
file_key.objectid = objectid;
|
||||
file_key.offset = offset;
|
||||
file_key.flags = 0;
|
||||
btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
|
||||
ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
|
||||
if (ret < 0)
|
||||
goto fail;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
leaf = path->nodes[0];
|
||||
if (ret > 0) {
|
||||
ret = 1;
|
||||
if (path->slots[0] == 0)
|
||||
goto fail;
|
||||
path->slots[0]--;
|
||||
btrfs_disk_key_to_cpu(&found_key,
|
||||
&leaf->items[path->slots[0]].key);
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
|
||||
found_key.objectid != objectid) {
|
||||
goto fail;
|
||||
}
|
||||
csum_offset = (offset - found_key.offset) >>
|
||||
root->fs_info->sb->s_blocksize_bits;
|
||||
csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
|
||||
csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
|
||||
csums_in_item /= BTRFS_CRC32_SIZE;
|
||||
|
||||
if (csum_offset >= csums_in_item) {
|
||||
|
@ -127,7 +126,6 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
|
|||
|
||||
file_key.objectid = objectid;
|
||||
file_key.offset = offset;
|
||||
file_key.flags = 0;
|
||||
btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
|
||||
ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
|
||||
return ret;
|
||||
|
@ -138,12 +136,14 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
|
|||
u64 objectid, u64 offset,
|
||||
char *data, size_t len)
|
||||
{
|
||||
return 0;
|
||||
#if 0
|
||||
int ret;
|
||||
struct btrfs_key file_key;
|
||||
struct btrfs_key found_key;
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_csum_item *item;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
u64 csum_offset;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -161,8 +161,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
|
|||
if (ret == -EFBIG) {
|
||||
u32 item_size;
|
||||
/* we found one, but it isn't big enough yet */
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
item_size = btrfs_item_size(leaf->items + path->slots[0]);
|
||||
leaf = path->nodes[0];
|
||||
item_size = btrfs_item_size_nr(leaf, path->slots[0]);
|
||||
if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
|
||||
/* already at max size, make a new one */
|
||||
goto insert;
|
||||
|
@ -188,8 +188,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
|
|||
goto insert;
|
||||
}
|
||||
path->slots[0]--;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
csum_offset = (offset - found_key.offset) >>
|
||||
root->fs_info->sb->s_blocksize_bits;
|
||||
if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
|
||||
|
@ -197,10 +197,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
|
|||
csum_offset >= MAX_CSUM_ITEMS(root)) {
|
||||
goto insert;
|
||||
}
|
||||
if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
|
||||
if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
|
||||
BTRFS_CRC32_SIZE) {
|
||||
u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
|
||||
diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
|
||||
diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
|
||||
if (diff != BTRFS_CRC32_SIZE)
|
||||
goto insert;
|
||||
ret = btrfs_extend_item(trans, root, path, diff);
|
||||
|
@ -220,21 +220,20 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
|
|||
goto fail;
|
||||
}
|
||||
csum:
|
||||
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
|
||||
struct btrfs_csum_item);
|
||||
leaf = path->nodes[0];
|
||||
item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
|
||||
ret = 0;
|
||||
item = (struct btrfs_csum_item *)((unsigned char *)item +
|
||||
csum_offset * BTRFS_CRC32_SIZE);
|
||||
found:
|
||||
btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
|
||||
path->nodes[0]->b_data,
|
||||
root->fs_info->sb->s_blocksize);
|
||||
/* FIXME!!!!!!!!!!!! */
|
||||
ret = btrfs_csum_data(root, data, len, &item->csum);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
fail:
|
||||
btrfs_release_path(root, path);
|
||||
btrfs_free_path(path);
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
|
||||
|
@ -242,21 +241,21 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
|
|||
u64 isize)
|
||||
{
|
||||
struct btrfs_key key;
|
||||
struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
struct extent_buffer *leaf = path->nodes[0];
|
||||
int slot = path->slots[0];
|
||||
int ret;
|
||||
u32 new_item_size;
|
||||
u64 new_item_span;
|
||||
u64 blocks;
|
||||
|
||||
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
|
||||
btrfs_item_key_to_cpu(leaf, &key, slot);
|
||||
if (isize <= key.offset)
|
||||
return 0;
|
||||
new_item_span = isize - key.offset;
|
||||
blocks = (new_item_span + root->blocksize - 1) >>
|
||||
blocks = (new_item_span + root->sectorsize - 1) >>
|
||||
root->fs_info->sb->s_blocksize_bits;
|
||||
new_item_size = blocks * BTRFS_CRC32_SIZE;
|
||||
if (new_item_size >= btrfs_item_size(leaf->items + slot))
|
||||
if (new_item_size >= btrfs_item_size_nr(leaf, slot))
|
||||
return 0;
|
||||
ret = btrfs_truncate_item(trans, root, path, new_item_size);
|
||||
BUG_ON(ret);
|
||||
|
|
127
fs/btrfs/file.c
127
fs/btrfs/file.c
|
@ -16,7 +16,6 @@
|
|||
* Boston, MA 021110-1307, USA.
|
||||
*/
|
||||
|
||||
#include <linux/buffer_head.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/pagemap.h>
|
||||
#include <linux/highmem.h>
|
||||
|
@ -88,7 +87,9 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
|
|||
{
|
||||
struct btrfs_key key;
|
||||
struct btrfs_path *path;
|
||||
char *ptr, *kaddr;
|
||||
struct extent_buffer *leaf;
|
||||
char *kaddr;
|
||||
unsigned long ptr;
|
||||
struct btrfs_file_extent_item *ei;
|
||||
u32 datasize;
|
||||
int err = 0;
|
||||
|
@ -102,7 +103,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
|
|||
|
||||
key.objectid = inode->i_ino;
|
||||
key.offset = offset;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
|
||||
BUG_ON(size >= PAGE_CACHE_SIZE);
|
||||
datasize = btrfs_file_extent_calc_inline_size(size);
|
||||
|
@ -113,18 +113,17 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
|
|||
err = ret;
|
||||
goto fail;
|
||||
}
|
||||
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0], struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_generation(ei, trans->transid);
|
||||
btrfs_set_file_extent_type(ei,
|
||||
BTRFS_FILE_EXTENT_INLINE);
|
||||
leaf = path->nodes[0];
|
||||
ei = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_generation(leaf, ei, trans->transid);
|
||||
btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
|
||||
ptr = btrfs_file_extent_inline_start(ei);
|
||||
|
||||
kaddr = kmap_atomic(page, KM_USER0);
|
||||
btrfs_memcpy(root, path->nodes[0]->b_data,
|
||||
ptr, kaddr + page_offset, size);
|
||||
write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
|
||||
kunmap_atomic(kaddr, KM_USER0);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
fail:
|
||||
btrfs_free_path(path);
|
||||
return err;
|
||||
|
@ -156,8 +155,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
|
|||
|
||||
em->bdev = inode->i_sb->s_bdev;
|
||||
|
||||
start_pos = pos & ~((u64)root->blocksize - 1);
|
||||
num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
|
||||
start_pos = pos & ~((u64)root->sectorsize - 1);
|
||||
num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
|
||||
inode->i_blkbits;
|
||||
|
||||
down_read(&BTRFS_I(inode)->root->snap_sem);
|
||||
|
@ -184,7 +183,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
|
|||
if (inode->i_size < start_pos) {
|
||||
u64 last_pos_in_file;
|
||||
u64 hole_size;
|
||||
u64 mask = root->blocksize - 1;
|
||||
u64 mask = root->sectorsize - 1;
|
||||
last_pos_in_file = (isize + mask) & ~mask;
|
||||
hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
|
||||
|
||||
|
@ -227,8 +226,8 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
|
|||
/* step one, delete the existing extents in this range */
|
||||
/* FIXME blocksize != pagesize */
|
||||
err = btrfs_drop_extents(trans, root, inode, start_pos,
|
||||
(pos + write_bytes + root->blocksize -1) &
|
||||
~((u64)root->blocksize - 1), &hint_block);
|
||||
(pos + write_bytes + root->sectorsize -1) &
|
||||
~((u64)root->sectorsize - 1), &hint_block);
|
||||
if (err)
|
||||
goto failed;
|
||||
|
||||
|
@ -288,7 +287,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
{
|
||||
int ret;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
int slot;
|
||||
struct btrfs_file_extent_item *extent;
|
||||
u64 extent_end = 0;
|
||||
|
@ -327,10 +326,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
found_extent = 0;
|
||||
found_inline = 0;
|
||||
extent = NULL;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
leaf = path->nodes[0];
|
||||
slot = path->slots[0];
|
||||
ret = 0;
|
||||
btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
|
||||
btrfs_item_key_to_cpu(leaf, &key, slot);
|
||||
if (key.offset >= end || key.objectid != inode->i_ino) {
|
||||
goto out;
|
||||
}
|
||||
|
@ -344,17 +343,18 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
|
||||
extent = btrfs_item_ptr(leaf, slot,
|
||||
struct btrfs_file_extent_item);
|
||||
found_type = btrfs_file_extent_type(extent);
|
||||
found_type = btrfs_file_extent_type(leaf, extent);
|
||||
if (found_type == BTRFS_FILE_EXTENT_REG) {
|
||||
extent_end = key.offset +
|
||||
(btrfs_file_extent_num_blocks(extent) <<
|
||||
(btrfs_file_extent_num_blocks(leaf, extent) <<
|
||||
inode->i_blkbits);
|
||||
found_extent = 1;
|
||||
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
|
||||
struct btrfs_item *item;
|
||||
item = btrfs_item_nr(leaf, slot);
|
||||
found_inline = 1;
|
||||
extent_end = key.offset +
|
||||
btrfs_file_extent_inline_len(leaf->items +
|
||||
slot);
|
||||
btrfs_file_extent_inline_len(leaf, item);
|
||||
}
|
||||
} else {
|
||||
extent_end = search_start;
|
||||
|
@ -365,8 +365,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
search_start >= extent_end) {
|
||||
int nextret;
|
||||
u32 nritems;
|
||||
nritems = btrfs_header_nritems(
|
||||
btrfs_buffer_header(path->nodes[0]));
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
if (slot >= nritems - 1) {
|
||||
nextret = btrfs_next_leaf(root, path);
|
||||
if (nextret)
|
||||
|
@ -380,7 +379,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
|
||||
/* FIXME, there's only one inline extent allowed right now */
|
||||
if (found_inline) {
|
||||
u64 mask = root->blocksize - 1;
|
||||
u64 mask = root->sectorsize - 1;
|
||||
search_start = (extent_end + mask) & ~mask;
|
||||
} else
|
||||
search_start = extent_end;
|
||||
|
@ -388,10 +387,13 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
if (end < extent_end && end >= key.offset) {
|
||||
if (found_extent) {
|
||||
u64 disk_blocknr =
|
||||
btrfs_file_extent_disk_blocknr(extent);
|
||||
btrfs_file_extent_disk_blocknr(leaf,extent);
|
||||
u64 disk_num_blocks =
|
||||
btrfs_file_extent_disk_num_blocks(extent);
|
||||
memcpy(&old, extent, sizeof(old));
|
||||
btrfs_file_extent_disk_num_blocks(leaf,
|
||||
extent);
|
||||
read_extent_buffer(leaf, &old,
|
||||
(unsigned long)extent,
|
||||
sizeof(old));
|
||||
if (disk_blocknr != 0) {
|
||||
ret = btrfs_inc_extent_ref(trans, root,
|
||||
disk_blocknr, disk_num_blocks);
|
||||
|
@ -406,20 +408,24 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
u64 new_num;
|
||||
u64 old_num;
|
||||
keep = 1;
|
||||
WARN_ON(start & (root->blocksize - 1));
|
||||
WARN_ON(start & (root->sectorsize - 1));
|
||||
if (found_extent) {
|
||||
new_num = (start - key.offset) >>
|
||||
inode->i_blkbits;
|
||||
old_num = btrfs_file_extent_num_blocks(extent);
|
||||
old_num = btrfs_file_extent_num_blocks(leaf,
|
||||
extent);
|
||||
*hint_block =
|
||||
btrfs_file_extent_disk_blocknr(extent);
|
||||
if (btrfs_file_extent_disk_blocknr(extent)) {
|
||||
btrfs_file_extent_disk_blocknr(leaf,
|
||||
extent);
|
||||
if (btrfs_file_extent_disk_blocknr(leaf,
|
||||
extent)) {
|
||||
inode->i_blocks -=
|
||||
(old_num - new_num) << 3;
|
||||
}
|
||||
btrfs_set_file_extent_num_blocks(extent,
|
||||
btrfs_set_file_extent_num_blocks(leaf,
|
||||
extent,
|
||||
new_num);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
} else {
|
||||
WARN_ON(1);
|
||||
}
|
||||
|
@ -431,13 +437,17 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
u64 extent_num_blocks = 0;
|
||||
if (found_extent) {
|
||||
disk_blocknr =
|
||||
btrfs_file_extent_disk_blocknr(extent);
|
||||
btrfs_file_extent_disk_blocknr(leaf,
|
||||
extent);
|
||||
disk_num_blocks =
|
||||
btrfs_file_extent_disk_num_blocks(extent);
|
||||
btrfs_file_extent_disk_num_blocks(leaf,
|
||||
extent);
|
||||
extent_num_blocks =
|
||||
btrfs_file_extent_num_blocks(extent);
|
||||
btrfs_file_extent_num_blocks(leaf,
|
||||
extent);
|
||||
*hint_block =
|
||||
btrfs_file_extent_disk_blocknr(extent);
|
||||
btrfs_file_extent_disk_blocknr(leaf,
|
||||
extent);
|
||||
}
|
||||
ret = btrfs_del_item(trans, root, path);
|
||||
/* TODO update progress marker and return */
|
||||
|
@ -464,42 +474,37 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_key ins;
|
||||
ins.objectid = inode->i_ino;
|
||||
ins.offset = end;
|
||||
ins.flags = 0;
|
||||
btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
|
||||
btrfs_release_path(root, path);
|
||||
ret = btrfs_insert_empty_item(trans, root, path, &ins,
|
||||
sizeof(*extent));
|
||||
|
||||
leaf = path->nodes[0];
|
||||
if (ret) {
|
||||
btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0]));
|
||||
printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.flags, ins.offset, start, end, key.offset, extent_end, keep);
|
||||
btrfs_print_leaf(root, leaf);
|
||||
printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
|
||||
}
|
||||
BUG_ON(ret);
|
||||
extent = btrfs_item_ptr(
|
||||
btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_disk_blocknr(extent,
|
||||
btrfs_file_extent_disk_blocknr(&old));
|
||||
btrfs_set_file_extent_disk_num_blocks(extent,
|
||||
btrfs_file_extent_disk_num_blocks(&old));
|
||||
extent = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
write_extent_buffer(leaf, &old,
|
||||
(unsigned long)extent, sizeof(old));
|
||||
|
||||
btrfs_set_file_extent_offset(extent,
|
||||
btrfs_file_extent_offset(&old) +
|
||||
btrfs_set_file_extent_offset(leaf, extent,
|
||||
le64_to_cpu(old.offset) +
|
||||
((end - key.offset) >> inode->i_blkbits));
|
||||
WARN_ON(btrfs_file_extent_num_blocks(&old) <
|
||||
WARN_ON(le64_to_cpu(old.num_blocks) <
|
||||
(extent_end - end) >> inode->i_blkbits);
|
||||
btrfs_set_file_extent_num_blocks(extent,
|
||||
btrfs_set_file_extent_num_blocks(leaf, extent,
|
||||
(extent_end - end) >> inode->i_blkbits);
|
||||
|
||||
btrfs_set_file_extent_type(extent,
|
||||
btrfs_set_file_extent_type(leaf, extent,
|
||||
BTRFS_FILE_EXTENT_REG);
|
||||
btrfs_set_file_extent_generation(extent,
|
||||
btrfs_file_extent_generation(&old));
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
if (btrfs_file_extent_disk_blocknr(&old) != 0) {
|
||||
if (le64_to_cpu(old.disk_blocknr) != 0) {
|
||||
inode->i_blocks +=
|
||||
btrfs_file_extent_num_blocks(extent) << 3;
|
||||
btrfs_file_extent_num_blocks(leaf,
|
||||
extent) << 3;
|
||||
}
|
||||
ret = 0;
|
||||
goto out;
|
||||
|
@ -529,8 +534,8 @@ static int prepare_pages(struct btrfs_root *root,
|
|||
u64 num_blocks;
|
||||
u64 start_pos;
|
||||
|
||||
start_pos = pos & ~((u64)root->blocksize - 1);
|
||||
num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
|
||||
start_pos = pos & ~((u64)root->sectorsize - 1);
|
||||
num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
|
||||
inode->i_blkbits;
|
||||
|
||||
memset(pages, 0, num_pages * sizeof(struct page *));
|
||||
|
|
|
@ -20,24 +20,18 @@
|
|||
#include "disk-io.h"
|
||||
#include "transaction.h"
|
||||
|
||||
int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
|
||||
*root, u64 objectid, struct btrfs_inode_item
|
||||
*inode_item)
|
||||
int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root,
|
||||
struct btrfs_path *path, u64 objectid)
|
||||
{
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_key key;
|
||||
int ret;
|
||||
key.objectid = objectid;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
|
||||
key.offset = 0;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
ret = btrfs_insert_item(trans, root, &key, inode_item,
|
||||
sizeof(*inode_item));
|
||||
btrfs_release_path(root, path);
|
||||
btrfs_free_path(path);
|
||||
ret = btrfs_insert_empty_item(trans, root, path, &key,
|
||||
sizeof(struct btrfs_inode_item));
|
||||
if (ret == 0 && objectid > root->highest_inode)
|
||||
root->highest_inode = objectid;
|
||||
return ret;
|
||||
|
@ -51,15 +45,15 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
int cow = mod != 0;
|
||||
int ret;
|
||||
int slot;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_key found_key;
|
||||
|
||||
ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
|
||||
if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
|
||||
location->offset == (u64)-1 && path->slots[0] != 0) {
|
||||
slot = path->slots[0] - 1;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
||||
if (found_key.objectid == location->objectid &&
|
||||
btrfs_key_type(&found_key) == btrfs_key_type(location)) {
|
||||
path->slots[0]--;
|
||||
|
|
|
@ -24,8 +24,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
|
|||
{
|
||||
struct btrfs_path *path;
|
||||
int ret;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
struct btrfs_key search_key;
|
||||
struct btrfs_key found_key;
|
||||
int slot;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -39,8 +40,9 @@ int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
|
|||
BUG_ON(ret == 0);
|
||||
if (path->slots[0] > 0) {
|
||||
slot = path->slots[0] - 1;
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
*objectid = btrfs_disk_key_objectid(&l->items[slot].key);
|
||||
l = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(l, &found_key, slot);
|
||||
*objectid = found_key.objectid;
|
||||
} else {
|
||||
*objectid = BTRFS_FIRST_FREE_OBJECTID;
|
||||
}
|
||||
|
@ -64,13 +66,12 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
|
|||
int slot = 0;
|
||||
u64 last_ino = 0;
|
||||
int start_found;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
struct btrfs_key search_key;
|
||||
u64 search_start = dirid;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
search_key.flags = 0;
|
||||
search_start = root->last_inode_alloc;
|
||||
search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
|
||||
search_key.objectid = search_start;
|
||||
|
@ -86,9 +87,9 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
|
|||
path->slots[0]--;
|
||||
|
||||
while (1) {
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
l = path->nodes[0];
|
||||
slot = path->slots[0];
|
||||
if (slot >= btrfs_header_nritems(&l->header)) {
|
||||
if (slot >= btrfs_header_nritems(l)) {
|
||||
ret = btrfs_next_leaf(root, path);
|
||||
if (ret == 0)
|
||||
continue;
|
||||
|
@ -103,7 +104,7 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
|
|||
last_ino : search_start;
|
||||
goto found;
|
||||
}
|
||||
btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
|
||||
btrfs_item_key_to_cpu(l, &key, slot);
|
||||
if (key.objectid >= search_start) {
|
||||
if (start_found) {
|
||||
if (last_ino < search_start)
|
||||
|
|
435
fs/btrfs/inode.c
435
fs/btrfs/inode.c
|
@ -159,10 +159,8 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
|
|||
|
||||
int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
|
||||
{
|
||||
char csum[BTRFS_CRC32_SIZE];
|
||||
size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
|
||||
struct inode *inode = page->mapping->host;
|
||||
struct btrfs_root *root = BTRFS_I(inode)->root;
|
||||
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
|
||||
char *kaddr;
|
||||
u64 private;
|
||||
|
@ -173,11 +171,15 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
|
|||
if (ret) {
|
||||
goto zeroit;
|
||||
}
|
||||
/*
|
||||
struct btrfs_root *root = BTRFS_I(inode)->root;
|
||||
char csum[BTRFS_CRC32_SIZE];
|
||||
ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
|
||||
BUG_ON(ret);
|
||||
if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
|
||||
goto zeroit;
|
||||
}
|
||||
*/
|
||||
kunmap_atomic(kaddr, KM_IRQ0);
|
||||
return 0;
|
||||
|
||||
|
@ -192,7 +194,9 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
|
|||
void btrfs_read_locked_inode(struct inode *inode)
|
||||
{
|
||||
struct btrfs_path *path;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_inode_item *inode_item;
|
||||
struct btrfs_inode_timespec *tspec;
|
||||
struct btrfs_root *root = BTRFS_I(inode)->root;
|
||||
struct btrfs_key location;
|
||||
u64 alloc_group_block;
|
||||
|
@ -205,29 +209,37 @@ void btrfs_read_locked_inode(struct inode *inode)
|
|||
|
||||
memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
|
||||
ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
|
||||
if (ret) {
|
||||
if (ret)
|
||||
goto make_bad;
|
||||
}
|
||||
inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0],
|
||||
struct btrfs_inode_item);
|
||||
|
||||
inode->i_mode = btrfs_inode_mode(inode_item);
|
||||
inode->i_nlink = btrfs_inode_nlink(inode_item);
|
||||
inode->i_uid = btrfs_inode_uid(inode_item);
|
||||
inode->i_gid = btrfs_inode_gid(inode_item);
|
||||
inode->i_size = btrfs_inode_size(inode_item);
|
||||
inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
|
||||
inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
|
||||
inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
|
||||
inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
|
||||
inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
|
||||
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
|
||||
inode->i_blocks = btrfs_inode_nblocks(inode_item);
|
||||
inode->i_generation = btrfs_inode_generation(inode_item);
|
||||
leaf = path->nodes[0];
|
||||
inode_item = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_inode_item);
|
||||
|
||||
inode->i_mode = btrfs_inode_mode(leaf, inode_item);
|
||||
inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
|
||||
inode->i_uid = btrfs_inode_uid(leaf, inode_item);
|
||||
inode->i_gid = btrfs_inode_gid(leaf, inode_item);
|
||||
inode->i_size = btrfs_inode_size(leaf, inode_item);
|
||||
|
||||
tspec = btrfs_inode_atime(inode_item);
|
||||
inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
|
||||
inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
|
||||
|
||||
tspec = btrfs_inode_mtime(inode_item);
|
||||
inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
|
||||
inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
|
||||
|
||||
tspec = btrfs_inode_ctime(inode_item);
|
||||
inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
|
||||
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
|
||||
|
||||
inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
|
||||
inode->i_generation = btrfs_inode_generation(leaf, inode_item);
|
||||
inode->i_rdev = 0;
|
||||
rdev = btrfs_inode_rdev(inode_item);
|
||||
alloc_group_block = btrfs_inode_block_group(inode_item);
|
||||
rdev = btrfs_inode_rdev(leaf, inode_item);
|
||||
|
||||
alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
|
||||
BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
|
||||
alloc_group_block);
|
||||
|
||||
|
@ -267,24 +279,35 @@ void btrfs_read_locked_inode(struct inode *inode)
|
|||
make_bad_inode(inode);
|
||||
}
|
||||
|
||||
static void fill_inode_item(struct btrfs_inode_item *item,
|
||||
static void fill_inode_item(struct extent_buffer *leaf,
|
||||
struct btrfs_inode_item *item,
|
||||
struct inode *inode)
|
||||
{
|
||||
btrfs_set_inode_uid(item, inode->i_uid);
|
||||
btrfs_set_inode_gid(item, inode->i_gid);
|
||||
btrfs_set_inode_size(item, inode->i_size);
|
||||
btrfs_set_inode_mode(item, inode->i_mode);
|
||||
btrfs_set_inode_nlink(item, inode->i_nlink);
|
||||
btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
|
||||
btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
|
||||
btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
|
||||
btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
|
||||
btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
|
||||
btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
|
||||
btrfs_set_inode_nblocks(item, inode->i_blocks);
|
||||
btrfs_set_inode_generation(item, inode->i_generation);
|
||||
btrfs_set_inode_rdev(item, inode->i_rdev);
|
||||
btrfs_set_inode_block_group(item,
|
||||
btrfs_set_inode_uid(leaf, item, inode->i_uid);
|
||||
btrfs_set_inode_gid(leaf, item, inode->i_gid);
|
||||
btrfs_set_inode_size(leaf, item, inode->i_size);
|
||||
btrfs_set_inode_mode(leaf, item, inode->i_mode);
|
||||
btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
|
||||
|
||||
btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
|
||||
inode->i_atime.tv_sec);
|
||||
btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
|
||||
inode->i_atime.tv_nsec);
|
||||
|
||||
btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
|
||||
inode->i_mtime.tv_sec);
|
||||
btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
|
||||
inode->i_mtime.tv_nsec);
|
||||
|
||||
btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
|
||||
inode->i_ctime.tv_sec);
|
||||
btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
|
||||
inode->i_ctime.tv_nsec);
|
||||
|
||||
btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
|
||||
btrfs_set_inode_generation(leaf, item, inode->i_generation);
|
||||
btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
|
||||
btrfs_set_inode_block_group(leaf, item,
|
||||
BTRFS_I(inode)->block_group->key.objectid);
|
||||
}
|
||||
|
||||
|
@ -294,6 +317,7 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
|
|||
{
|
||||
struct btrfs_inode_item *inode_item;
|
||||
struct btrfs_path *path;
|
||||
struct extent_buffer *leaf;
|
||||
int ret;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -306,12 +330,12 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
|
|||
goto failed;
|
||||
}
|
||||
|
||||
inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0],
|
||||
leaf = path->nodes[0];
|
||||
inode_item = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_inode_item);
|
||||
|
||||
fill_inode_item(inode_item, inode);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
fill_inode_item(leaf, inode_item, inode);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
btrfs_set_inode_last_trans(trans, inode);
|
||||
ret = 0;
|
||||
failed:
|
||||
|
@ -330,8 +354,9 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
|
|||
const char *name = dentry->d_name.name;
|
||||
int name_len = dentry->d_name.len;
|
||||
int ret = 0;
|
||||
u64 objectid;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_dir_item *di;
|
||||
struct btrfs_key key;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
if (!path) {
|
||||
|
@ -349,14 +374,15 @@ static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
|
|||
ret = -ENOENT;
|
||||
goto err;
|
||||
}
|
||||
objectid = btrfs_disk_key_objectid(&di->location);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_dir_item_key_to_cpu(leaf, di, &key);
|
||||
ret = btrfs_delete_one_dir_name(trans, root, path, di);
|
||||
if (ret)
|
||||
goto err;
|
||||
btrfs_release_path(root, path);
|
||||
|
||||
di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
|
||||
objectid, name, name_len, -1);
|
||||
key.objectid, name, name_len, -1);
|
||||
if (IS_ERR(di)) {
|
||||
ret = PTR_ERR(di);
|
||||
goto err;
|
||||
|
@ -391,12 +417,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
|
|||
root = BTRFS_I(dir)->root;
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
|
||||
btrfs_set_trans_block_group(trans, dir);
|
||||
ret = btrfs_unlink_trans(trans, root, dir, dentry);
|
||||
nr = trans->blocks_used;
|
||||
|
||||
btrfs_end_transaction(trans, root);
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
btrfs_btree_balance_dirty(root, nr);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -411,7 +440,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
|
|||
struct btrfs_trans_handle *trans;
|
||||
struct btrfs_key found_key;
|
||||
int found_type;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
char *goodnames = "..";
|
||||
unsigned long nr;
|
||||
|
||||
|
@ -419,10 +448,11 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
|
|||
BUG_ON(!path);
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
|
||||
btrfs_set_trans_block_group(trans, dir);
|
||||
key.objectid = inode->i_ino;
|
||||
key.offset = (u64)-1;
|
||||
key.flags = (u32)-1;
|
||||
key.type = (u8)-1;
|
||||
while(1) {
|
||||
ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
|
||||
if (ret < 0) {
|
||||
|
@ -435,9 +465,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
|
|||
goto out;
|
||||
}
|
||||
path->slots[0]--;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
btrfs_disk_key_to_cpu(&found_key,
|
||||
&leaf->items[path->slots[0]].key);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
found_type = btrfs_key_type(&found_key);
|
||||
if (found_key.objectid != inode->i_ino) {
|
||||
err = -ENOENT;
|
||||
|
@ -513,9 +542,9 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
|
|||
int ret;
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_disk_key *found_key;
|
||||
struct btrfs_key found_key;
|
||||
u32 found_type;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_file_extent_item *fi;
|
||||
u64 extent_start = 0;
|
||||
u64 extent_num_blocks = 0;
|
||||
|
@ -527,10 +556,12 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
|
|||
path = btrfs_alloc_path();
|
||||
path->reada = -1;
|
||||
BUG_ON(!path);
|
||||
|
||||
/* FIXME, add redo link to tree so we don't leak on crash */
|
||||
key.objectid = inode->i_ino;
|
||||
key.offset = (u64)-1;
|
||||
key.flags = (u32)-1;
|
||||
key.type = (u8)-1;
|
||||
|
||||
while(1) {
|
||||
btrfs_init_path(path);
|
||||
fi = NULL;
|
||||
|
@ -542,27 +573,28 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
|
|||
BUG_ON(path->slots[0] == 0);
|
||||
path->slots[0]--;
|
||||
}
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
found_key = &leaf->items[path->slots[0]].key;
|
||||
found_type = btrfs_disk_key_type(found_key);
|
||||
leaf = path->nodes[0];
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
found_type = btrfs_key_type(&found_key);
|
||||
|
||||
if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
|
||||
if (found_key.objectid != inode->i_ino)
|
||||
break;
|
||||
|
||||
if (found_type != BTRFS_CSUM_ITEM_KEY &&
|
||||
found_type != BTRFS_DIR_ITEM_KEY &&
|
||||
found_type != BTRFS_DIR_INDEX_KEY &&
|
||||
found_type != BTRFS_EXTENT_DATA_KEY)
|
||||
break;
|
||||
|
||||
item_end = btrfs_disk_key_offset(found_key);
|
||||
item_end = found_key.offset;
|
||||
if (found_type == BTRFS_EXTENT_DATA_KEY) {
|
||||
fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0],
|
||||
fi = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
if (btrfs_file_extent_type(fi) !=
|
||||
if (btrfs_file_extent_type(leaf, fi) !=
|
||||
BTRFS_FILE_EXTENT_INLINE) {
|
||||
item_end += btrfs_file_extent_num_blocks(fi) <<
|
||||
inode->i_blkbits;
|
||||
item_end +=
|
||||
btrfs_file_extent_num_blocks(leaf, fi) <<
|
||||
inode->i_blkbits;
|
||||
}
|
||||
}
|
||||
if (found_type == BTRFS_CSUM_ITEM_KEY) {
|
||||
|
@ -583,7 +615,7 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
|
|||
btrfs_set_key_type(&key, found_type);
|
||||
continue;
|
||||
}
|
||||
if (btrfs_disk_key_offset(found_key) >= inode->i_size)
|
||||
if (found_key.offset >= inode->i_size)
|
||||
del_item = 1;
|
||||
else
|
||||
del_item = 0;
|
||||
|
@ -591,30 +623,31 @@ static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
|
|||
|
||||
/* FIXME, shrink the extent if the ref count is only 1 */
|
||||
if (found_type == BTRFS_EXTENT_DATA_KEY &&
|
||||
btrfs_file_extent_type(fi) !=
|
||||
btrfs_file_extent_type(leaf, fi) !=
|
||||
BTRFS_FILE_EXTENT_INLINE) {
|
||||
u64 num_dec;
|
||||
extent_start = btrfs_file_extent_disk_blocknr(fi);
|
||||
extent_start = btrfs_file_extent_disk_blocknr(leaf, fi);
|
||||
if (!del_item) {
|
||||
u64 orig_num_blocks =
|
||||
btrfs_file_extent_num_blocks(fi);
|
||||
btrfs_file_extent_num_blocks(leaf, fi);
|
||||
extent_num_blocks = inode->i_size -
|
||||
btrfs_disk_key_offset(found_key) +
|
||||
root->blocksize - 1;
|
||||
found_key.offset + root->sectorsize - 1;
|
||||
extent_num_blocks >>= inode->i_blkbits;
|
||||
btrfs_set_file_extent_num_blocks(fi,
|
||||
btrfs_set_file_extent_num_blocks(leaf, fi,
|
||||
extent_num_blocks);
|
||||
num_dec = (orig_num_blocks -
|
||||
extent_num_blocks) << 3;
|
||||
if (extent_start != 0) {
|
||||
inode->i_blocks -= num_dec;
|
||||
}
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
} else {
|
||||
extent_num_blocks =
|
||||
btrfs_file_extent_disk_num_blocks(fi);
|
||||
btrfs_file_extent_disk_num_blocks(leaf,
|
||||
fi);
|
||||
/* FIXME blocksize != 4096 */
|
||||
num_dec = btrfs_file_extent_num_blocks(fi) << 3;
|
||||
num_dec = btrfs_file_extent_num_blocks(leaf,
|
||||
fi) << 3;
|
||||
if (extent_start != 0) {
|
||||
found_extent = 1;
|
||||
inode->i_blocks -= num_dec;
|
||||
|
@ -725,7 +758,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
|
|||
struct btrfs_root *root = BTRFS_I(inode)->root;
|
||||
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
|
||||
|
||||
u64 mask = root->blocksize - 1;
|
||||
u64 mask = root->sectorsize - 1;
|
||||
u64 pos = (inode->i_size + mask) & ~mask;
|
||||
u64 block_end = attr->ia_size | mask;
|
||||
u64 hole_size;
|
||||
|
@ -771,9 +804,11 @@ void btrfs_delete_inode(struct inode *inode)
|
|||
if (is_bad_inode(inode)) {
|
||||
goto no_delete;
|
||||
}
|
||||
|
||||
inode->i_size = 0;
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
|
||||
btrfs_set_trans_block_group(trans, inode);
|
||||
ret = btrfs_truncate_in_trans(trans, root, inode);
|
||||
if (ret)
|
||||
|
@ -782,6 +817,7 @@ void btrfs_delete_inode(struct inode *inode)
|
|||
if (ret)
|
||||
goto no_delete_lock;
|
||||
nr = trans->blocks_used;
|
||||
|
||||
btrfs_end_transaction(trans, root);
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
btrfs_btree_balance_dirty(root, nr);
|
||||
|
@ -819,7 +855,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
|
|||
ret = 0;
|
||||
goto out;
|
||||
}
|
||||
btrfs_disk_key_to_cpu(location, &di->location);
|
||||
btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
|
||||
out:
|
||||
btrfs_release_path(root, path);
|
||||
btrfs_free_path(path);
|
||||
|
@ -856,7 +892,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
|
|||
|
||||
ri = &(*sub_root)->root_item;
|
||||
location->objectid = btrfs_root_dirid(ri);
|
||||
location->flags = 0;
|
||||
btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
|
||||
location->offset = 0;
|
||||
|
||||
|
@ -908,11 +943,14 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
|
|||
|
||||
if (dentry->d_name.len > BTRFS_NAME_LEN)
|
||||
return ERR_PTR(-ENAMETOOLONG);
|
||||
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
ret = btrfs_inode_by_name(dir, dentry, &location);
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
|
||||
if (ret < 0)
|
||||
return ERR_PTR(ret);
|
||||
|
||||
inode = NULL;
|
||||
if (location.objectid) {
|
||||
ret = fixup_tree_root_location(root, &location, &sub_root,
|
||||
|
@ -952,10 +990,11 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||
struct btrfs_item *item;
|
||||
struct btrfs_dir_item *di;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_key found_key;
|
||||
struct btrfs_path *path;
|
||||
int ret;
|
||||
u32 nritems;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
int slot;
|
||||
int advance;
|
||||
unsigned char d_type;
|
||||
|
@ -964,15 +1003,19 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||
u32 di_total;
|
||||
u32 di_len;
|
||||
int key_type = BTRFS_DIR_INDEX_KEY;
|
||||
char tmp_name[32];
|
||||
char *name_ptr;
|
||||
int name_len;
|
||||
|
||||
/* FIXME, use a real flag for deciding about the key type */
|
||||
if (root->fs_info->tree_root == root)
|
||||
key_type = BTRFS_DIR_ITEM_KEY;
|
||||
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
key.objectid = inode->i_ino;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, key_type);
|
||||
key.offset = filp->f_pos;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
path->reada = 2;
|
||||
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
||||
|
@ -980,16 +1023,16 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||
goto err;
|
||||
advance = 0;
|
||||
while(1) {
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
nritems = btrfs_header_nritems(&leaf->header);
|
||||
leaf = path->nodes[0];
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
slot = path->slots[0];
|
||||
if (advance || slot >= nritems) {
|
||||
if (slot >= nritems -1) {
|
||||
ret = btrfs_next_leaf(root, path);
|
||||
if (ret)
|
||||
break;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
nritems = btrfs_header_nritems(&leaf->header);
|
||||
leaf = path->nodes[0];
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
slot = path->slots[0];
|
||||
} else {
|
||||
slot++;
|
||||
|
@ -997,28 +1040,48 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||
}
|
||||
}
|
||||
advance = 1;
|
||||
item = leaf->items + slot;
|
||||
if (btrfs_disk_key_objectid(&item->key) != key.objectid)
|
||||
item = btrfs_item_nr(leaf, slot);
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
||||
|
||||
if (found_key.objectid != key.objectid)
|
||||
break;
|
||||
if (btrfs_disk_key_type(&item->key) != key_type)
|
||||
if (btrfs_key_type(&found_key) != key_type)
|
||||
break;
|
||||
if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
|
||||
if (found_key.offset < filp->f_pos)
|
||||
continue;
|
||||
filp->f_pos = btrfs_disk_key_offset(&item->key);
|
||||
|
||||
filp->f_pos = found_key.offset;
|
||||
advance = 1;
|
||||
di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
|
||||
di_cur = 0;
|
||||
di_total = btrfs_item_size(leaf->items + slot);
|
||||
di_total = btrfs_item_size(leaf, item);
|
||||
while(di_cur < di_total) {
|
||||
d_type = btrfs_filetype_table[btrfs_dir_type(di)];
|
||||
over = filldir(dirent, (const char *)(di + 1),
|
||||
btrfs_dir_name_len(di),
|
||||
btrfs_disk_key_offset(&item->key),
|
||||
btrfs_disk_key_objectid(&di->location),
|
||||
struct btrfs_key location;
|
||||
|
||||
name_len = btrfs_dir_name_len(leaf, di);
|
||||
if (name_len < 32) {
|
||||
name_ptr = tmp_name;
|
||||
} else {
|
||||
name_ptr = kmalloc(name_len, GFP_NOFS);
|
||||
BUG_ON(!name_ptr);
|
||||
}
|
||||
read_extent_buffer(leaf, name_ptr,
|
||||
(unsigned long)(di + 1), name_len);
|
||||
|
||||
d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
|
||||
btrfs_dir_item_key_to_cpu(leaf, di, &location);
|
||||
|
||||
over = filldir(dirent, name_ptr, name_len,
|
||||
found_key.offset,
|
||||
location.objectid,
|
||||
d_type);
|
||||
|
||||
if (name_ptr != tmp_name)
|
||||
kfree(name_ptr);
|
||||
|
||||
if (over)
|
||||
goto nopos;
|
||||
di_len = btrfs_dir_name_len(di) + sizeof(*di);
|
||||
di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
|
||||
di_cur += di_len;
|
||||
di = (struct btrfs_dir_item *)((char *)di + di_len);
|
||||
}
|
||||
|
@ -1075,11 +1138,15 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
|
|||
int mode)
|
||||
{
|
||||
struct inode *inode;
|
||||
struct btrfs_inode_item inode_item;
|
||||
struct btrfs_inode_item *inode_item;
|
||||
struct btrfs_key *location;
|
||||
struct btrfs_path *path;
|
||||
int ret;
|
||||
int owner;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
|
||||
inode = new_inode(root->fs_info->sb);
|
||||
if (!inode)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
@ -1095,24 +1162,32 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
|
|||
group = btrfs_find_block_group(root, group, 0, 0, owner);
|
||||
BTRFS_I(inode)->block_group = group;
|
||||
|
||||
ret = btrfs_insert_empty_inode(trans, root, path, objectid);
|
||||
if (ret)
|
||||
goto fail;
|
||||
|
||||
inode->i_uid = current->fsuid;
|
||||
inode->i_gid = current->fsgid;
|
||||
inode->i_mode = mode;
|
||||
inode->i_ino = objectid;
|
||||
inode->i_blocks = 0;
|
||||
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
|
||||
fill_inode_item(&inode_item, inode);
|
||||
inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
|
||||
struct btrfs_inode_item);
|
||||
fill_inode_item(path->nodes[0], inode_item, inode);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
btrfs_free_path(path);
|
||||
|
||||
location = &BTRFS_I(inode)->location;
|
||||
location->objectid = objectid;
|
||||
location->flags = 0;
|
||||
location->offset = 0;
|
||||
btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
|
||||
|
||||
ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
|
||||
if (ret)
|
||||
return ERR_PTR(ret);
|
||||
insert_inode_hash(inode);
|
||||
return inode;
|
||||
fail:
|
||||
btrfs_free_path(path);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
|
||||
static inline u8 btrfs_inode_type(struct inode *inode)
|
||||
|
@ -1127,8 +1202,8 @@ static int btrfs_add_link(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_key key;
|
||||
struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
|
||||
struct inode *parent_inode;
|
||||
|
||||
key.objectid = inode->i_ino;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
|
||||
key.offset = 0;
|
||||
|
||||
|
@ -1285,14 +1360,18 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
|
|||
inc_nlink(inode);
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
|
||||
btrfs_set_trans_block_group(trans, dir);
|
||||
atomic_inc(&inode->i_count);
|
||||
err = btrfs_add_nondir(trans, dentry, inode);
|
||||
|
||||
if (err)
|
||||
drop_inode = 1;
|
||||
|
||||
dir->i_sb->s_dirt = 1;
|
||||
btrfs_update_inode_block_group(trans, dir);
|
||||
err = btrfs_update_inode(trans, root, inode);
|
||||
|
||||
if (err)
|
||||
drop_inode = 1;
|
||||
|
||||
|
@ -1321,13 +1400,13 @@ static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
|
|||
|
||||
key.objectid = objectid;
|
||||
key.offset = 0;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
|
||||
|
||||
ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
|
||||
&key, BTRFS_FT_DIR);
|
||||
if (ret)
|
||||
goto error;
|
||||
|
||||
key.objectid = dirid;
|
||||
ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
|
||||
&key, BTRFS_FT_DIR);
|
||||
|
@ -1350,6 +1429,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
|
|||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
btrfs_set_trans_block_group(trans, dir);
|
||||
|
||||
if (IS_ERR(trans)) {
|
||||
err = PTR_ERR(trans);
|
||||
goto out_unlock;
|
||||
|
@ -1367,6 +1447,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
|
|||
err = PTR_ERR(inode);
|
||||
goto out_fail;
|
||||
}
|
||||
|
||||
drop_on_err = 1;
|
||||
inode->i_op = &btrfs_dir_inode_operations;
|
||||
inode->i_fop = &btrfs_dir_file_operations;
|
||||
|
@ -1380,9 +1461,11 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
|
|||
err = btrfs_update_inode(trans, root, inode);
|
||||
if (err)
|
||||
goto out_fail;
|
||||
|
||||
err = btrfs_add_link(trans, dentry, inode);
|
||||
if (err)
|
||||
goto out_fail;
|
||||
|
||||
d_instantiate(dentry, inode);
|
||||
drop_on_err = 0;
|
||||
dir->i_sb->s_dirt = 1;
|
||||
|
@ -1392,6 +1475,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
|
|||
out_fail:
|
||||
nr = trans->blocks_used;
|
||||
btrfs_end_transaction(trans, root);
|
||||
|
||||
out_unlock:
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
if (drop_on_err)
|
||||
|
@ -1415,8 +1499,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
struct btrfs_path *path;
|
||||
struct btrfs_root *root = BTRFS_I(inode)->root;
|
||||
struct btrfs_file_extent_item *item;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct btrfs_disk_key *found_key;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_key found_key;
|
||||
struct extent_map *em = NULL;
|
||||
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
|
||||
struct btrfs_trans_handle *trans = NULL;
|
||||
|
@ -1436,8 +1520,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
err = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
em->start = 0;
|
||||
em->end = 0;
|
||||
em->start = EXTENT_MAP_HOLE;
|
||||
em->end = EXTENT_MAP_HOLE;
|
||||
}
|
||||
em->bdev = inode->i_sb->s_bdev;
|
||||
ret = btrfs_lookup_file_extent(NULL, root, path,
|
||||
|
@ -1453,25 +1537,27 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
path->slots[0]--;
|
||||
}
|
||||
|
||||
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
|
||||
leaf = path->nodes[0];
|
||||
item = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
blocknr = btrfs_file_extent_disk_blocknr(item);
|
||||
blocknr += btrfs_file_extent_offset(item);
|
||||
|
||||
blocknr = btrfs_file_extent_disk_blocknr(leaf, item);
|
||||
blocknr += btrfs_file_extent_offset(leaf, item);
|
||||
|
||||
/* are we inside the extent that was found? */
|
||||
found_key = &leaf->items[path->slots[0]].key;
|
||||
found_type = btrfs_disk_key_type(found_key);
|
||||
if (btrfs_disk_key_objectid(found_key) != objectid ||
|
||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||
found_type = btrfs_key_type(&found_key);
|
||||
if (found_key.objectid != objectid ||
|
||||
found_type != BTRFS_EXTENT_DATA_KEY) {
|
||||
goto not_found;
|
||||
}
|
||||
|
||||
found_type = btrfs_file_extent_type(item);
|
||||
extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
|
||||
found_type = btrfs_file_extent_type(leaf, item);
|
||||
extent_start = found_key.offset;
|
||||
if (found_type == BTRFS_FILE_EXTENT_REG) {
|
||||
extent_end = extent_start +
|
||||
(btrfs_file_extent_num_blocks(item) << inode->i_blkbits);
|
||||
(btrfs_file_extent_num_blocks(leaf, item) <<
|
||||
inode->i_blkbits);
|
||||
err = 0;
|
||||
if (start < extent_start || start >= extent_end) {
|
||||
em->start = start;
|
||||
|
@ -1484,28 +1570,29 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
}
|
||||
goto not_found_em;
|
||||
}
|
||||
if (btrfs_file_extent_disk_blocknr(item) == 0) {
|
||||
if (btrfs_file_extent_disk_blocknr(leaf, item) == 0) {
|
||||
em->start = extent_start;
|
||||
em->end = extent_end - 1;
|
||||
em->block_start = 0;
|
||||
em->block_end = 0;
|
||||
em->block_start = EXTENT_MAP_HOLE;
|
||||
em->block_end = EXTENT_MAP_HOLE;
|
||||
goto insert;
|
||||
}
|
||||
em->block_start = blocknr << inode->i_blkbits;
|
||||
em->block_end = em->block_start +
|
||||
(btrfs_file_extent_num_blocks(item) <<
|
||||
(btrfs_file_extent_num_blocks(leaf, item) <<
|
||||
inode->i_blkbits) - 1;
|
||||
em->start = extent_start;
|
||||
em->end = extent_end - 1;
|
||||
goto insert;
|
||||
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
|
||||
char *ptr;
|
||||
unsigned long ptr;
|
||||
char *map;
|
||||
u32 size;
|
||||
|
||||
size = btrfs_file_extent_inline_len(leaf->items +
|
||||
path->slots[0]);
|
||||
extent_end = extent_start | ((u64)root->blocksize - 1);
|
||||
size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
|
||||
path->slots[0]));
|
||||
|
||||
extent_end = extent_start | ((u64)root->sectorsize - 1);
|
||||
if (start < extent_start || start >= extent_end) {
|
||||
em->start = start;
|
||||
if (start < extent_start) {
|
||||
|
@ -1517,18 +1604,21 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
}
|
||||
goto not_found_em;
|
||||
}
|
||||
|
||||
em->block_start = EXTENT_MAP_INLINE;
|
||||
em->block_end = EXTENT_MAP_INLINE;
|
||||
em->start = extent_start;
|
||||
em->end = extent_end;
|
||||
|
||||
if (!page) {
|
||||
goto insert;
|
||||
}
|
||||
|
||||
ptr = btrfs_file_extent_inline_start(item);
|
||||
map = kmap(page);
|
||||
memcpy(map + page_offset, ptr, size);
|
||||
read_extent_buffer(leaf, map + page_offset, ptr, size);
|
||||
memset(map + page_offset + size, 0,
|
||||
root->blocksize - (page_offset + size));
|
||||
root->sectorsize - (page_offset + size));
|
||||
flush_dcache_page(page);
|
||||
kunmap(page);
|
||||
set_extent_uptodate(em_tree, extent_start,
|
||||
|
@ -1542,8 +1632,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
|
|||
em->start = start;
|
||||
em->end = end;
|
||||
not_found_em:
|
||||
em->block_start = 0;
|
||||
em->block_end = 0;
|
||||
em->block_start = EXTENT_MAP_HOLE;
|
||||
em->block_end = EXTENT_MAP_HOLE;
|
||||
insert:
|
||||
btrfs_release_path(root, path);
|
||||
if (em->start > start || em->end < start) {
|
||||
|
@ -1712,6 +1802,7 @@ static void btrfs_truncate(struct inode *inode)
|
|||
ret = btrfs_truncate_in_trans(trans, root, inode);
|
||||
btrfs_update_inode(trans, root, inode);
|
||||
nr = trans->blocks_used;
|
||||
|
||||
ret = btrfs_end_transaction(trans, root);
|
||||
BUG_ON(ret);
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
|
@ -1731,8 +1822,7 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
|
|||
struct btrfs_key key;
|
||||
struct btrfs_root_item root_item;
|
||||
struct btrfs_inode_item *inode_item;
|
||||
struct buffer_head *subvol;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
struct btrfs_root *new_root;
|
||||
struct inode *inode;
|
||||
struct inode *dir;
|
||||
|
@ -1746,34 +1836,37 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
|
|||
trans = btrfs_start_transaction(root, 1);
|
||||
BUG_ON(!trans);
|
||||
|
||||
subvol = btrfs_alloc_free_block(trans, root, 0, 0);
|
||||
if (IS_ERR(subvol))
|
||||
return PTR_ERR(subvol);
|
||||
leaf = btrfs_buffer_leaf(subvol);
|
||||
btrfs_set_header_nritems(&leaf->header, 0);
|
||||
btrfs_set_header_level(&leaf->header, 0);
|
||||
btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
|
||||
btrfs_set_header_generation(&leaf->header, trans->transid);
|
||||
btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
|
||||
memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
|
||||
sizeof(leaf->header.fsid));
|
||||
btrfs_mark_buffer_dirty(subvol);
|
||||
leaf = btrfs_alloc_free_block(trans, root, 0, 0);
|
||||
if (IS_ERR(leaf))
|
||||
return PTR_ERR(leaf);
|
||||
|
||||
btrfs_set_header_nritems(leaf, 0);
|
||||
btrfs_set_header_level(leaf, 0);
|
||||
btrfs_set_header_blocknr(leaf, extent_buffer_blocknr(leaf));
|
||||
btrfs_set_header_generation(leaf, trans->transid);
|
||||
btrfs_set_header_owner(leaf, root->root_key.objectid);
|
||||
write_extent_buffer(leaf, root->fs_info->fsid,
|
||||
(unsigned long)btrfs_header_fsid(leaf),
|
||||
BTRFS_FSID_SIZE);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
|
||||
inode_item = &root_item.inode;
|
||||
memset(inode_item, 0, sizeof(*inode_item));
|
||||
btrfs_set_inode_generation(inode_item, 1);
|
||||
btrfs_set_inode_size(inode_item, 3);
|
||||
btrfs_set_inode_nlink(inode_item, 1);
|
||||
btrfs_set_inode_nblocks(inode_item, 1);
|
||||
btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
|
||||
inode_item->generation = cpu_to_le64(1);
|
||||
inode_item->size = cpu_to_le64(3);
|
||||
inode_item->nlink = cpu_to_le32(1);
|
||||
inode_item->nblocks = cpu_to_le64(1);
|
||||
inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
|
||||
|
||||
btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
|
||||
btrfs_set_root_blocknr(&root_item, extent_buffer_blocknr(leaf));
|
||||
btrfs_set_root_refs(&root_item, 1);
|
||||
btrfs_set_root_blocks_used(&root_item, 0);
|
||||
btrfs_set_root_used(&root_item, 0);
|
||||
|
||||
memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
|
||||
root_item.drop_level = 0;
|
||||
brelse(subvol);
|
||||
subvol = NULL;
|
||||
|
||||
free_extent_buffer(leaf);
|
||||
leaf = NULL;
|
||||
|
||||
ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
|
||||
0, &objectid);
|
||||
|
@ -1784,7 +1877,6 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
|
|||
|
||||
key.objectid = objectid;
|
||||
key.offset = 1;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
|
||||
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
|
||||
&root_item);
|
||||
|
@ -1845,7 +1937,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
|
|||
struct btrfs_trans_handle *trans;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_root_item new_root_item;
|
||||
struct buffer_head *tmp;
|
||||
struct extent_buffer *tmp;
|
||||
int ret;
|
||||
int err;
|
||||
u64 objectid;
|
||||
|
@ -1876,10 +1968,11 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
|
|||
|
||||
key.objectid = objectid;
|
||||
key.offset = 1;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
|
||||
|
||||
btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
|
||||
btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
|
||||
btrfs_set_root_blocknr(&new_root_item,
|
||||
extent_buffer_blocknr(root->node));
|
||||
|
||||
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
|
||||
&new_root_item);
|
||||
|
@ -1904,8 +1997,10 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
|
|||
fail:
|
||||
nr = trans->blocks_used;
|
||||
err = btrfs_commit_transaction(trans, root);
|
||||
|
||||
if (err && !ret)
|
||||
ret = err;
|
||||
|
||||
mutex_unlock(&root->fs_info->fs_mutex);
|
||||
up_write(&root->snap_sem);
|
||||
btrfs_btree_balance_dirty(root, nr);
|
||||
|
@ -1986,7 +2081,7 @@ static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
|
|||
|
||||
if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
|
||||
return -EFAULT;
|
||||
|
||||
|
||||
namelen = strlen(vol_args.name);
|
||||
if (namelen > BTRFS_VOL_NAME_MAX)
|
||||
return -EINVAL;
|
||||
|
@ -2164,8 +2259,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
|
|||
new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
|
||||
return -ENOTEMPTY;
|
||||
}
|
||||
|
||||
mutex_lock(&root->fs_info->fs_mutex);
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
|
||||
btrfs_set_trans_block_group(trans, new_dir);
|
||||
path = btrfs_alloc_path();
|
||||
if (!path) {
|
||||
|
@ -2177,9 +2274,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
|
|||
old_dir->i_ctime = old_dir->i_mtime = ctime;
|
||||
new_dir->i_ctime = new_dir->i_mtime = ctime;
|
||||
old_inode->i_ctime = ctime;
|
||||
|
||||
if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
|
||||
struct btrfs_key *location = &BTRFS_I(new_dir)->location;
|
||||
u64 old_parent_oid;
|
||||
struct btrfs_key old_parent_key;
|
||||
di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
|
||||
"..", 2, -1);
|
||||
if (IS_ERR(di)) {
|
||||
|
@ -2190,7 +2288,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
|
|||
ret = -ENOENT;
|
||||
goto out_fail;
|
||||
}
|
||||
old_parent_oid = btrfs_disk_key_objectid(&di->location);
|
||||
btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
|
||||
ret = btrfs_del_item(trans, root, path);
|
||||
if (ret) {
|
||||
goto out_fail;
|
||||
|
@ -2199,7 +2297,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
|
|||
|
||||
di = btrfs_lookup_dir_index_item(trans, root, path,
|
||||
old_inode->i_ino,
|
||||
old_parent_oid,
|
||||
old_parent_key.objectid,
|
||||
"..", 2, -1);
|
||||
if (IS_ERR(di)) {
|
||||
ret = PTR_ERR(di);
|
||||
|
@ -2257,8 +2355,9 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
|
|||
u64 objectid;
|
||||
int name_len;
|
||||
int datasize;
|
||||
char *ptr;
|
||||
unsigned long ptr;
|
||||
struct btrfs_file_extent_item *ei;
|
||||
struct extent_buffer *leaf;
|
||||
unsigned long nr;
|
||||
|
||||
name_len = strlen(symname) + 1;
|
||||
|
@ -2302,7 +2401,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
|
|||
BUG_ON(!path);
|
||||
key.objectid = inode->i_ino;
|
||||
key.offset = 0;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
|
||||
datasize = btrfs_file_extent_calc_inline_size(name_len);
|
||||
err = btrfs_insert_empty_item(trans, root, path, &key,
|
||||
|
@ -2311,16 +2409,17 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
|
|||
drop_inode = 1;
|
||||
goto out_unlock;
|
||||
}
|
||||
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0], struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_generation(ei, trans->transid);
|
||||
btrfs_set_file_extent_type(ei,
|
||||
leaf = path->nodes[0];
|
||||
ei = btrfs_item_ptr(leaf, path->slots[0],
|
||||
struct btrfs_file_extent_item);
|
||||
btrfs_set_file_extent_generation(leaf, ei, trans->transid);
|
||||
btrfs_set_file_extent_type(leaf, ei,
|
||||
BTRFS_FILE_EXTENT_INLINE);
|
||||
ptr = btrfs_file_extent_inline_start(ei);
|
||||
btrfs_memcpy(root, path->nodes[0]->b_data,
|
||||
ptr, symname, name_len);
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
write_extent_buffer(leaf, symname, ptr, name_len);
|
||||
btrfs_mark_buffer_dirty(leaf);
|
||||
btrfs_free_path(path);
|
||||
|
||||
inode->i_op = &btrfs_symlink_inode_operations;
|
||||
inode->i_mapping->a_ops = &btrfs_symlink_aops;
|
||||
inode->i_size = name_len - 1;
|
||||
|
|
|
@ -20,10 +20,10 @@
|
|||
#include "disk-io.h"
|
||||
#include "print-tree.h"
|
||||
|
||||
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
|
||||
void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
|
||||
{
|
||||
int i;
|
||||
u32 nr = btrfs_header_nritems(&l->header);
|
||||
u32 nr = btrfs_header_nritems(l);
|
||||
struct btrfs_item *item;
|
||||
struct btrfs_extent_item *ei;
|
||||
struct btrfs_root_item *ri;
|
||||
|
@ -31,119 +31,113 @@ void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
|
|||
struct btrfs_inode_item *ii;
|
||||
struct btrfs_block_group_item *bi;
|
||||
struct btrfs_file_extent_item *fi;
|
||||
struct btrfs_key key;
|
||||
struct btrfs_key found_key;
|
||||
u32 type;
|
||||
|
||||
printk("leaf %llu total ptrs %d free space %d\n",
|
||||
(unsigned long long)btrfs_header_blocknr(&l->header), nr,
|
||||
(unsigned long long)btrfs_header_blocknr(l), nr,
|
||||
btrfs_leaf_free_space(root, l));
|
||||
for (i = 0 ; i < nr ; i++) {
|
||||
item = l->items + i;
|
||||
type = btrfs_disk_key_type(&item->key);
|
||||
item = btrfs_item_nr(l, i);
|
||||
btrfs_item_key_to_cpu(l, &key, i);
|
||||
type = btrfs_key_type(&key);
|
||||
printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
|
||||
i,
|
||||
(unsigned long long)btrfs_disk_key_objectid(&item->key),
|
||||
btrfs_disk_key_flags(&item->key),
|
||||
(unsigned long long)btrfs_disk_key_offset(&item->key),
|
||||
btrfs_item_offset(item),
|
||||
btrfs_item_size(item));
|
||||
(unsigned long long)key.objectid, type,
|
||||
(unsigned long long)key.offset,
|
||||
btrfs_item_offset(l, item), btrfs_item_size(l, item));
|
||||
switch (type) {
|
||||
case BTRFS_INODE_ITEM_KEY:
|
||||
ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
|
||||
printk("\t\tinode generation %llu size %llu mode %o\n",
|
||||
(unsigned long long)btrfs_inode_generation(ii),
|
||||
(unsigned long long)btrfs_inode_size(ii),
|
||||
btrfs_inode_mode(ii));
|
||||
(unsigned long long)btrfs_inode_generation(l, ii),
|
||||
(unsigned long long)btrfs_inode_size(l, ii),
|
||||
btrfs_inode_mode(l, ii));
|
||||
break;
|
||||
case BTRFS_DIR_ITEM_KEY:
|
||||
di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
|
||||
btrfs_dir_item_key_to_cpu(l, di, &found_key);
|
||||
printk("\t\tdir oid %llu flags %u type %u\n",
|
||||
(unsigned long long)btrfs_disk_key_objectid(
|
||||
&di->location),
|
||||
btrfs_dir_flags(di),
|
||||
btrfs_dir_type(di));
|
||||
printk("\t\tname %.*s\n",
|
||||
btrfs_dir_name_len(di),(char *)(di + 1));
|
||||
(unsigned long long)found_key.objectid,
|
||||
btrfs_dir_flags(l, di),
|
||||
btrfs_dir_type(l, di));
|
||||
break;
|
||||
case BTRFS_ROOT_ITEM_KEY:
|
||||
ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
|
||||
printk("\t\troot data blocknr %llu refs %u\n",
|
||||
(unsigned long long)btrfs_root_blocknr(ri),
|
||||
btrfs_root_refs(ri));
|
||||
(unsigned long long)btrfs_disk_root_blocknr(l, ri),
|
||||
btrfs_disk_root_refs(l, ri));
|
||||
break;
|
||||
case BTRFS_EXTENT_ITEM_KEY:
|
||||
ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
|
||||
printk("\t\textent data refs %u\n",
|
||||
btrfs_extent_refs(ei));
|
||||
btrfs_extent_refs(l, ei));
|
||||
break;
|
||||
|
||||
case BTRFS_EXTENT_DATA_KEY:
|
||||
fi = btrfs_item_ptr(l, i,
|
||||
struct btrfs_file_extent_item);
|
||||
if (btrfs_file_extent_type(fi) ==
|
||||
if (btrfs_file_extent_type(l, fi) ==
|
||||
BTRFS_FILE_EXTENT_INLINE) {
|
||||
printk("\t\tinline extent data size %u\n",
|
||||
btrfs_file_extent_inline_len(l->items + i));
|
||||
btrfs_file_extent_inline_len(l, item));
|
||||
break;
|
||||
}
|
||||
printk("\t\textent data disk block %llu nr %llu\n",
|
||||
(unsigned long long)btrfs_file_extent_disk_blocknr(fi),
|
||||
(unsigned long long)btrfs_file_extent_disk_num_blocks(fi));
|
||||
(unsigned long long)btrfs_file_extent_disk_blocknr(l, fi),
|
||||
(unsigned long long)btrfs_file_extent_disk_num_blocks(l, fi));
|
||||
printk("\t\textent data offset %llu nr %llu\n",
|
||||
(unsigned long long)btrfs_file_extent_offset(fi),
|
||||
(unsigned long long)btrfs_file_extent_num_blocks(fi));
|
||||
(unsigned long long)btrfs_file_extent_offset(l, fi),
|
||||
(unsigned long long)btrfs_file_extent_num_blocks(l, fi));
|
||||
break;
|
||||
case BTRFS_BLOCK_GROUP_ITEM_KEY:
|
||||
bi = btrfs_item_ptr(l, i,
|
||||
struct btrfs_block_group_item);
|
||||
printk("\t\tblock group used %llu\n",
|
||||
(unsigned long long)btrfs_block_group_used(bi));
|
||||
break;
|
||||
case BTRFS_STRING_ITEM_KEY:
|
||||
printk("\t\titem data %.*s\n", btrfs_item_size(item),
|
||||
btrfs_leaf_data(l) + btrfs_item_offset(item));
|
||||
(unsigned long long)btrfs_disk_block_group_used(l, bi));
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t)
|
||||
void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
|
||||
{
|
||||
int i;
|
||||
u32 nr;
|
||||
struct btrfs_node *c;
|
||||
struct btrfs_key key;
|
||||
|
||||
if (!t)
|
||||
if (!c)
|
||||
return;
|
||||
c = btrfs_buffer_node(t);
|
||||
nr = btrfs_header_nritems(&c->header);
|
||||
nr = btrfs_header_nritems(c);
|
||||
if (btrfs_is_leaf(c)) {
|
||||
btrfs_print_leaf(root, (struct btrfs_leaf *)c);
|
||||
btrfs_print_leaf(root, c);
|
||||
return;
|
||||
}
|
||||
printk("node %llu level %d total ptrs %d free spc %u\n",
|
||||
(unsigned long long)btrfs_header_blocknr(&c->header),
|
||||
btrfs_header_level(&c->header), nr,
|
||||
(unsigned long long)btrfs_header_blocknr(c),
|
||||
btrfs_header_level(c), nr,
|
||||
(u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
|
||||
for (i = 0; i < nr; i++) {
|
||||
btrfs_node_key_to_cpu(c, &key, i);
|
||||
printk("\tkey %d (%llu %u %llu) block %llu\n",
|
||||
i,
|
||||
(unsigned long long)c->ptrs[i].key.objectid,
|
||||
c->ptrs[i].key.flags,
|
||||
(unsigned long long)c->ptrs[i].key.offset,
|
||||
(unsigned long long)key.objectid,
|
||||
key.type,
|
||||
(unsigned long long)key.offset,
|
||||
(unsigned long long)btrfs_node_blockptr(c, i));
|
||||
}
|
||||
for (i = 0; i < nr; i++) {
|
||||
struct buffer_head *next_buf = read_tree_block(root,
|
||||
struct extent_buffer *next = read_tree_block(root,
|
||||
btrfs_node_blockptr(c, i));
|
||||
struct btrfs_node *next = btrfs_buffer_node(next_buf);
|
||||
if (btrfs_is_leaf(next) &&
|
||||
btrfs_header_level(&c->header) != 1)
|
||||
btrfs_header_level(c) != 1)
|
||||
BUG();
|
||||
if (btrfs_header_level(&next->header) !=
|
||||
btrfs_header_level(&c->header) - 1)
|
||||
if (btrfs_header_level(next) !=
|
||||
btrfs_header_level(c) - 1)
|
||||
BUG();
|
||||
btrfs_print_tree(root, next_buf);
|
||||
btrfs_block_release(root, next_buf);
|
||||
btrfs_print_tree(root, next);
|
||||
free_extent_buffer(next);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,6 @@
|
|||
|
||||
#ifndef __PRINT_TREE_
|
||||
#define __PRINT_TREE_
|
||||
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l);
|
||||
void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t);
|
||||
void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l);
|
||||
void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *t);
|
||||
#endif
|
||||
|
|
|
@ -26,12 +26,13 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
|
|||
{
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_key search_key;
|
||||
struct btrfs_leaf *l;
|
||||
struct btrfs_key found_key;
|
||||
struct extent_buffer *l;
|
||||
int ret;
|
||||
int slot;
|
||||
|
||||
search_key.objectid = objectid;
|
||||
search_key.flags = (u32)-1;
|
||||
search_key.type = (u8)-1;
|
||||
search_key.offset = (u64)-1;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -39,17 +40,19 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
|
|||
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
|
||||
BUG_ON(ret == 0);
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
l = path->nodes[0];
|
||||
BUG_ON(path->slots[0] == 0);
|
||||
slot = path->slots[0] - 1;
|
||||
if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
|
||||
btrfs_item_key_to_cpu(l, &found_key, slot);
|
||||
if (found_key.objectid != objectid) {
|
||||
ret = 1;
|
||||
goto out;
|
||||
}
|
||||
memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
|
||||
sizeof(*item));
|
||||
btrfs_disk_key_to_cpu(key, &l->items[slot].key);
|
||||
read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
|
||||
sizeof(*item));
|
||||
memcpy(key, &found_key, sizeof(found_key));
|
||||
ret = 0;
|
||||
out:
|
||||
btrfs_release_path(root, path);
|
||||
|
@ -62,10 +65,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
*item)
|
||||
{
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_leaf *l;
|
||||
struct extent_buffer *l;
|
||||
int ret;
|
||||
int slot;
|
||||
struct btrfs_root_item *update_item;
|
||||
unsigned long ptr;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
|
@ -73,10 +76,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
|
|||
if (ret < 0)
|
||||
goto out;
|
||||
BUG_ON(ret != 0);
|
||||
l = btrfs_buffer_leaf(path->nodes[0]);
|
||||
l = path->nodes[0];
|
||||
slot = path->slots[0];
|
||||
update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
|
||||
btrfs_memcpy(root, l, update_item, item, sizeof(*item));
|
||||
ptr = btrfs_item_ptr_offset(l, slot);
|
||||
write_extent_buffer(l, item, ptr, sizeof(*item));
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
out:
|
||||
btrfs_release_path(root, path);
|
||||
|
@ -103,11 +106,10 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
|
|||
struct btrfs_path *path;
|
||||
int ret;
|
||||
u32 nritems;
|
||||
struct btrfs_leaf *leaf;
|
||||
struct extent_buffer *leaf;
|
||||
int slot;
|
||||
|
||||
key.objectid = objectid;
|
||||
key.flags = 0;
|
||||
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
|
||||
key.offset = 0;
|
||||
path = btrfs_alloc_path();
|
||||
|
@ -117,19 +119,19 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
|
|||
if (ret < 0)
|
||||
goto err;
|
||||
while(1) {
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
nritems = btrfs_header_nritems(&leaf->header);
|
||||
leaf = path->nodes[0];
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
slot = path->slots[0];
|
||||
if (slot >= nritems) {
|
||||
ret = btrfs_next_leaf(root, path);
|
||||
if (ret)
|
||||
break;
|
||||
leaf = btrfs_buffer_leaf(path->nodes[0]);
|
||||
nritems = btrfs_header_nritems(&leaf->header);
|
||||
leaf = path->nodes[0];
|
||||
nritems = btrfs_header_nritems(leaf);
|
||||
slot = path->slots[0];
|
||||
}
|
||||
item = leaf->items + slot;
|
||||
btrfs_disk_key_to_cpu(&key, &item->key);
|
||||
item = btrfs_item_nr(leaf, slot);
|
||||
btrfs_item_key_to_cpu(leaf, &key, slot);
|
||||
if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
|
||||
goto next;
|
||||
|
||||
|
@ -140,7 +142,7 @@ int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
|
|||
break;
|
||||
|
||||
ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
|
||||
if (btrfs_root_refs(ri) != 0)
|
||||
if (btrfs_disk_root_refs(leaf, ri) != 0)
|
||||
goto next;
|
||||
|
||||
dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
|
||||
|
@ -170,6 +172,7 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
|||
int ret;
|
||||
u32 refs;
|
||||
struct btrfs_root_item *ri;
|
||||
struct extent_buffer *leaf;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
BUG_ON(!path);
|
||||
|
@ -177,10 +180,10 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
|
|||
if (ret < 0)
|
||||
goto out;
|
||||
BUG_ON(ret != 0);
|
||||
ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
|
||||
path->slots[0], struct btrfs_root_item);
|
||||
leaf = path->nodes[0];
|
||||
ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
|
||||
|
||||
refs = btrfs_root_refs(ri);
|
||||
refs = btrfs_disk_root_refs(leaf, ri);
|
||||
BUG_ON(refs != 0);
|
||||
ret = btrfs_del_item(trans, root, path);
|
||||
out:
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
#include "ioctl.h"
|
||||
#include "print-tree.h"
|
||||
|
||||
#define BTRFS_SUPER_MAGIC 0x9123682E
|
||||
#define BTRFS_SUPER_MAGIC 0x9123683E
|
||||
|
||||
static struct super_operations btrfs_super_ops;
|
||||
|
||||
|
@ -115,13 +115,12 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
|
|||
return -EIO;
|
||||
}
|
||||
sb->s_fs_info = tree_root;
|
||||
disk_super = tree_root->fs_info->disk_super;
|
||||
disk_super = &tree_root->fs_info->super_copy;
|
||||
inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
|
||||
tree_root);
|
||||
bi = BTRFS_I(inode);
|
||||
bi->location.objectid = inode->i_ino;
|
||||
bi->location.offset = 0;
|
||||
bi->location.flags = 0;
|
||||
bi->root = tree_root;
|
||||
|
||||
btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
|
||||
|
@ -281,6 +280,7 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type,
|
|||
error_bdev:
|
||||
close_bdev_excl(bdev);
|
||||
error:
|
||||
printk("get_sb failed\n");
|
||||
return error;
|
||||
}
|
||||
/* end copy & paste */
|
||||
|
@ -295,6 +295,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type,
|
|||
ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
|
||||
btrfs_fill_super, mnt,
|
||||
subvol_name ? subvol_name : "default");
|
||||
printk("btrfs_get_sb returns %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,31 +31,31 @@
|
|||
static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
|
||||
{
|
||||
return snprintf(buf, PAGE_SIZE, "%llu\n",
|
||||
(unsigned long long)btrfs_root_blocks_used(&root->root_item));
|
||||
(unsigned long long)btrfs_root_used(&root->root_item));
|
||||
}
|
||||
|
||||
static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
|
||||
{
|
||||
return snprintf(buf, PAGE_SIZE, "%llu\n",
|
||||
(unsigned long long)btrfs_root_block_limit(&root->root_item));
|
||||
(unsigned long long)btrfs_root_limit(&root->root_item));
|
||||
}
|
||||
|
||||
static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
|
||||
{
|
||||
return snprintf(buf, PAGE_SIZE, "%llu\n",
|
||||
(unsigned long long)btrfs_super_blocks_used(fs->disk_super));
|
||||
(unsigned long long)btrfs_super_blocks_used(&fs->super_copy));
|
||||
}
|
||||
|
||||
static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
|
||||
{
|
||||
return snprintf(buf, PAGE_SIZE, "%llu\n",
|
||||
(unsigned long long)btrfs_super_total_blocks(fs->disk_super));
|
||||
(unsigned long long)btrfs_super_total_blocks(&fs->super_copy));
|
||||
}
|
||||
|
||||
static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
|
||||
{
|
||||
return snprintf(buf, PAGE_SIZE, "%llu\n",
|
||||
(unsigned long long)btrfs_super_blocksize(fs->disk_super));
|
||||
(unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
|
||||
}
|
||||
|
||||
/* this is for root attrs (subvols/snapshots) */
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <linux/fs.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/writeback.h>
|
||||
#include <linux/pagemap.h>
|
||||
#include "ctree.h"
|
||||
#include "disk-io.h"
|
||||
#include "transaction.h"
|
||||
|
@ -66,7 +67,9 @@ static int join_transaction(struct btrfs_root *root)
|
|||
cur_trans->commit_done = 0;
|
||||
cur_trans->start_time = get_seconds();
|
||||
list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
|
||||
init_bit_radix(&cur_trans->dirty_pages);
|
||||
extent_map_tree_init(&cur_trans->dirty_pages,
|
||||
root->fs_info->btree_inode->i_mapping,
|
||||
GFP_NOFS);
|
||||
} else {
|
||||
cur_trans->num_writers++;
|
||||
cur_trans->num_joined++;
|
||||
|
@ -88,7 +91,7 @@ static int record_root_in_trans(struct btrfs_root *root)
|
|||
(unsigned long)root->root_key.objectid,
|
||||
BTRFS_ROOT_DEFRAG_TAG);
|
||||
root->commit_root = root->node;
|
||||
get_bh(root->node);
|
||||
extent_buffer_get(root->node);
|
||||
} else {
|
||||
WARN_ON(1);
|
||||
}
|
||||
|
@ -144,29 +147,30 @@ int btrfs_end_transaction(struct btrfs_trans_handle *trans,
|
|||
int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root)
|
||||
{
|
||||
unsigned long gang[16];
|
||||
int ret;
|
||||
int i;
|
||||
int err;
|
||||
int werr = 0;
|
||||
struct extent_map_tree *dirty_pages;
|
||||
struct page *page;
|
||||
struct radix_tree_root *dirty_pages;
|
||||
struct inode *btree_inode = root->fs_info->btree_inode;
|
||||
u64 start;
|
||||
u64 end;
|
||||
unsigned long index;
|
||||
|
||||
if (!trans || !trans->transaction) {
|
||||
return filemap_write_and_wait(btree_inode->i_mapping);
|
||||
}
|
||||
dirty_pages = &trans->transaction->dirty_pages;
|
||||
while(1) {
|
||||
ret = find_first_radix_bit(dirty_pages, gang,
|
||||
0, ARRAY_SIZE(gang));
|
||||
if (!ret)
|
||||
ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
|
||||
EXTENT_DIRTY);
|
||||
if (ret)
|
||||
break;
|
||||
for (i = 0; i < ret; i++) {
|
||||
/* FIXME EIO */
|
||||
clear_radix_bit(dirty_pages, gang[i]);
|
||||
page = find_lock_page(btree_inode->i_mapping,
|
||||
gang[i]);
|
||||
clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
|
||||
while(start <= end) {
|
||||
index = start >> PAGE_CACHE_SHIFT;
|
||||
start = (index + 1) << PAGE_CACHE_SHIFT;
|
||||
page = find_lock_page(btree_inode->i_mapping, index);
|
||||
if (!page)
|
||||
continue;
|
||||
if (PageWriteback(page)) {
|
||||
|
@ -202,10 +206,11 @@ int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
|
|||
btrfs_write_dirty_block_groups(trans, extent_root);
|
||||
while(1) {
|
||||
old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
|
||||
if (old_extent_block == bh_blocknr(extent_root->node))
|
||||
if (old_extent_block ==
|
||||
extent_buffer_blocknr(extent_root->node))
|
||||
break;
|
||||
btrfs_set_root_blocknr(&extent_root->root_item,
|
||||
bh_blocknr(extent_root->node));
|
||||
extent_buffer_blocknr(extent_root->node));
|
||||
ret = btrfs_update_root(trans, tree_root,
|
||||
&extent_root->root_key,
|
||||
&extent_root->root_item);
|
||||
|
@ -279,9 +284,9 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans,
|
|||
(unsigned long)root->root_key.objectid,
|
||||
BTRFS_ROOT_TRANS_TAG);
|
||||
if (root->commit_root == root->node) {
|
||||
WARN_ON(bh_blocknr(root->node) !=
|
||||
WARN_ON(extent_buffer_blocknr(root->node) !=
|
||||
btrfs_root_blocknr(&root->root_item));
|
||||
brelse(root->commit_root);
|
||||
free_extent_buffer(root->commit_root);
|
||||
root->commit_root = NULL;
|
||||
|
||||
/* make sure to update the root on disk
|
||||
|
@ -310,7 +315,7 @@ static int add_dirty_roots(struct btrfs_trans_handle *trans,
|
|||
|
||||
root->root_key.offset = root->fs_info->generation;
|
||||
btrfs_set_root_blocknr(&root->root_item,
|
||||
bh_blocknr(root->node));
|
||||
extent_buffer_blocknr(root->node));
|
||||
err = btrfs_insert_root(trans, root->fs_info->tree_root,
|
||||
&root->root_key,
|
||||
&root->root_item);
|
||||
|
@ -389,10 +394,10 @@ int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
|
|||
for (i = 0; i < ret; i++) {
|
||||
root = gang[i];
|
||||
last = root->root_key.objectid + 1;
|
||||
btrfs_defrag_root(root, 1);
|
||||
// btrfs_defrag_root(root, 1);
|
||||
}
|
||||
}
|
||||
btrfs_defrag_root(info->extent_root, 1);
|
||||
// btrfs_defrag_root(info->extent_root, 1);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
@ -414,7 +419,7 @@ static int drop_dirty_roots(struct btrfs_root *tree_root,
|
|||
dirty = list_entry(list->next, struct dirty_root, list);
|
||||
list_del_init(&dirty->list);
|
||||
|
||||
num_blocks = btrfs_root_blocks_used(&dirty->root->root_item);
|
||||
num_blocks = btrfs_root_used(&dirty->root->root_item);
|
||||
root = dirty->latest_root;
|
||||
|
||||
while(1) {
|
||||
|
@ -441,11 +446,11 @@ static int drop_dirty_roots(struct btrfs_root *tree_root,
|
|||
}
|
||||
BUG_ON(ret);
|
||||
|
||||
num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item);
|
||||
blocks_used = btrfs_root_blocks_used(&root->root_item);
|
||||
num_blocks -= btrfs_root_used(&dirty->root->root_item);
|
||||
blocks_used = btrfs_root_used(&root->root_item);
|
||||
if (num_blocks) {
|
||||
record_root_in_trans(root);
|
||||
btrfs_set_root_blocks_used(&root->root_item,
|
||||
btrfs_set_root_used(&root->root_item,
|
||||
blocks_used - num_blocks);
|
||||
}
|
||||
ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
|
||||
|
@ -553,9 +558,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
|
|||
btrfs_set_super_generation(&root->fs_info->super_copy,
|
||||
cur_trans->transid);
|
||||
btrfs_set_super_root(&root->fs_info->super_copy,
|
||||
bh_blocknr(root->fs_info->tree_root->node));
|
||||
memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
|
||||
sizeof(root->fs_info->super_copy));
|
||||
extent_buffer_blocknr(root->fs_info->tree_root->node));
|
||||
|
||||
write_extent_buffer(root->fs_info->sb_buffer,
|
||||
&root->fs_info->super_copy, 0,
|
||||
sizeof(root->fs_info->super_copy));
|
||||
|
||||
btrfs_copy_pinned(root, &pinned_copy);
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ struct btrfs_transaction {
|
|||
int use_count;
|
||||
int commit_done;
|
||||
struct list_head list;
|
||||
struct radix_tree_root dirty_pages;
|
||||
struct extent_map_tree dirty_pages;
|
||||
unsigned long start_time;
|
||||
wait_queue_head_t writer_wait;
|
||||
wait_queue_head_t commit_wait;
|
||||
|
@ -83,5 +83,6 @@ int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest,
|
|||
int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info);
|
||||
int btrfs_defrag_root(struct btrfs_root *root, int cacheonly);
|
||||
int btrfs_clean_old_snapshots(struct btrfs_root *root);
|
||||
|
||||
int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root);
|
||||
#endif
|
||||
|
|
|
@ -23,14 +23,14 @@
|
|||
#include "transaction.h"
|
||||
|
||||
static void reada_defrag(struct btrfs_root *root,
|
||||
struct btrfs_node *node)
|
||||
struct extent_buffer *node)
|
||||
{
|
||||
int i;
|
||||
u32 nritems;
|
||||
u64 blocknr;
|
||||
int ret;
|
||||
|
||||
nritems = btrfs_header_nritems(&node->header);
|
||||
nritems = btrfs_header_nritems(node);
|
||||
for (i = 0; i < nritems; i++) {
|
||||
blocknr = btrfs_node_blockptr(node, i);
|
||||
ret = readahead_tree_block(root, blocknr);
|
||||
|
@ -44,8 +44,8 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_path *path, int *level,
|
||||
int cache_only, u64 *last_ret)
|
||||
{
|
||||
struct buffer_head *next;
|
||||
struct buffer_head *cur;
|
||||
struct extent_buffer *next;
|
||||
struct extent_buffer *cur;
|
||||
u64 blocknr;
|
||||
int ret = 0;
|
||||
int is_extent = 0;
|
||||
|
@ -62,13 +62,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
|
|||
cur = path->nodes[*level];
|
||||
|
||||
if (!cache_only && *level > 1 && path->slots[*level] == 0)
|
||||
reada_defrag(root, btrfs_buffer_node(cur));
|
||||
reada_defrag(root, cur);
|
||||
|
||||
if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
|
||||
if (btrfs_header_level(cur) != *level)
|
||||
WARN_ON(1);
|
||||
|
||||
if (path->slots[*level] >=
|
||||
btrfs_header_nritems(btrfs_buffer_header(cur)))
|
||||
btrfs_header_nritems(cur))
|
||||
break;
|
||||
|
||||
if (*level == 1) {
|
||||
|
@ -80,14 +80,13 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
|
|||
|
||||
break;
|
||||
}
|
||||
blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
|
||||
path->slots[*level]);
|
||||
blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
|
||||
|
||||
if (cache_only) {
|
||||
next = btrfs_find_tree_block(root, blocknr);
|
||||
if (!next || !buffer_uptodate(next) ||
|
||||
buffer_locked(next) || !buffer_defrag(next)) {
|
||||
brelse(next);
|
||||
/* FIXME, test for defrag */
|
||||
if (!next || !btrfs_buffer_uptodate(next)) {
|
||||
free_extent_buffer(next);
|
||||
path->slots[*level]++;
|
||||
continue;
|
||||
}
|
||||
|
@ -106,16 +105,18 @@ static int defrag_walk_down(struct btrfs_trans_handle *trans,
|
|||
|
||||
WARN_ON(*level <= 0);
|
||||
if (path->nodes[*level-1])
|
||||
btrfs_block_release(root, path->nodes[*level-1]);
|
||||
free_extent_buffer(path->nodes[*level-1]);
|
||||
path->nodes[*level-1] = next;
|
||||
*level = btrfs_header_level(btrfs_buffer_header(next));
|
||||
*level = btrfs_header_level(next);
|
||||
path->slots[*level] = 0;
|
||||
}
|
||||
WARN_ON(*level < 0);
|
||||
WARN_ON(*level >= BTRFS_MAX_LEVEL);
|
||||
#if 0
|
||||
clear_buffer_defrag(path->nodes[*level]);
|
||||
clear_buffer_defrag_done(path->nodes[*level]);
|
||||
btrfs_block_release(root, path->nodes[*level]);
|
||||
#endif
|
||||
free_extent_buffer(path->nodes[*level]);
|
||||
path->nodes[*level] = NULL;
|
||||
*level += 1;
|
||||
WARN_ON(ret);
|
||||
|
@ -129,24 +130,25 @@ static int defrag_walk_up(struct btrfs_trans_handle *trans,
|
|||
{
|
||||
int i;
|
||||
int slot;
|
||||
struct btrfs_node *node;
|
||||
struct extent_buffer *node;
|
||||
|
||||
for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
|
||||
slot = path->slots[i];
|
||||
if (slot < btrfs_header_nritems(
|
||||
btrfs_buffer_header(path->nodes[i])) - 1) {
|
||||
if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
|
||||
path->slots[i]++;
|
||||
*level = i;
|
||||
node = btrfs_buffer_node(path->nodes[i]);
|
||||
node = path->nodes[i];
|
||||
WARN_ON(i == 0);
|
||||
btrfs_disk_key_to_cpu(&root->defrag_progress,
|
||||
&node->ptrs[path->slots[i]].key);
|
||||
btrfs_node_key_to_cpu(node, &root->defrag_progress,
|
||||
path->slots[i]);
|
||||
root->defrag_level = i;
|
||||
return 0;
|
||||
} else {
|
||||
/*
|
||||
clear_buffer_defrag(path->nodes[*level]);
|
||||
clear_buffer_defrag_done(path->nodes[*level]);
|
||||
btrfs_block_release(root, path->nodes[*level]);
|
||||
*/
|
||||
free_extent_buffer(path->nodes[*level]);
|
||||
path->nodes[*level] = NULL;
|
||||
*level = i + 1;
|
||||
}
|
||||
|
@ -158,7 +160,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
|
|||
struct btrfs_root *root, int cache_only)
|
||||
{
|
||||
struct btrfs_path *path = NULL;
|
||||
struct buffer_head *tmp;
|
||||
struct extent_buffer *tmp;
|
||||
int ret = 0;
|
||||
int wret;
|
||||
int level;
|
||||
|
@ -172,17 +174,18 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
|
|||
|
||||
if (root->ref_cows == 0 && !is_extent)
|
||||
goto out;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
level = btrfs_header_level(btrfs_buffer_header(root->node));
|
||||
level = btrfs_header_level(root->node);
|
||||
orig_level = level;
|
||||
if (level == 0) {
|
||||
goto out;
|
||||
}
|
||||
if (root->defrag_progress.objectid == 0) {
|
||||
get_bh(root->node);
|
||||
extent_buffer_get(root->node);
|
||||
ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
|
||||
BUG_ON(ret);
|
||||
ret = btrfs_realloc_node(trans, root, root->node, cache_only,
|
||||
|
@ -200,12 +203,15 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
|
|||
|
||||
if (is_extent)
|
||||
btrfs_extent_post_op(trans, root);
|
||||
|
||||
if (wret < 0) {
|
||||
ret = wret;
|
||||
goto out;
|
||||
}
|
||||
|
||||
while(level > 0 && !path->nodes[level])
|
||||
level--;
|
||||
|
||||
if (!path->nodes[level]) {
|
||||
ret = 0;
|
||||
goto out;
|
||||
|
@ -230,7 +236,7 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
|
|||
}
|
||||
for (i = 0; i <= orig_level; i++) {
|
||||
if (path->nodes[i]) {
|
||||
btrfs_block_release(root, path->nodes[i]);
|
||||
free_extent_buffer(path->nodes[i]);
|
||||
path->nodes[i] = 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue