Skip to content

Commit

Permalink
btrfs_get_block, file read/write
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Chris Mason authored and David Woodhouse committed Mar 26, 2007
1 parent 4730a4b commit dee26a9
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 41 deletions.
1 change: 1 addition & 0 deletions fs/btrfs/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Add block mapping tree (simple dm layer)
* Add simple tree locking (semaphore per tree)
* Make allocator smarter
* Make directory hashing work on 32 bit
* Do actual block accounting
* Check compat and incompat flags on the inode
* Add virtual filesystems, mountable snapshots
Expand Down
26 changes: 23 additions & 3 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct btrfs_file_extent_item {
__le64 disk_blocknr;
__le64 disk_num_blocks;
/*
* the logical offset in file bytes (no csums)
* the logical offset in file blocks (no csums)
* this extent record is for. This allows a file extent to point
* into the middle of an existing extent on disk, sharing it
* between two snapshots (useful if some bytes in the middle of the
Expand Down Expand Up @@ -812,12 +812,19 @@ static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
((type *)(btrfs_leaf_data(leaf) + \
btrfs_item_offset((leaf)->items + (slot))))

/* extent-item.c */
struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 num_blocks, u64 search_start, u64
search_end, u64 owner, struct btrfs_key *ins);
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct buffer_head *buf);
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 blocknr, u64 num_blocks, int pin);
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
btrfs_root *root);
/* ctree.c */
int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_path *p, int
ins_len, int cow);
Expand All @@ -834,8 +841,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct buffer_head *snap);
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
btrfs_root *root);
/* root-item.c */
int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_key *key);
int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
Expand All @@ -846,6 +852,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
*item);
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
btrfs_root_item *item, struct btrfs_key *key);
/* dir-item.c */
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, const char *name, int name_len, u64 dir, u64
objectid, u8 type);
Expand All @@ -854,6 +861,7 @@ int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
const char *name, int name_len, int mod);
int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
const char *name, int name_len);
/* inode-map.c */
int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
struct btrfs_root *fs_root,
u64 dirid, u64 *objectid);
Expand All @@ -863,9 +871,21 @@ int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_path *path,
u64 objectid, int mod);
/* inode-item.c */
int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 objectid, struct btrfs_inode_item
*inode_item);
int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, u64 objectid, int mod);

/* file-item.c */
int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
u64 objectid, u64 offset,
u64 num_blocks, u64 hint_block,
u64 *result);
int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path, u64 objectid,
u64 blocknr, u64 num_blocks, int mod);
#endif
4 changes: 2 additions & 2 deletions fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*
* returns 0 if everything worked, non-zero otherwise.
*/
static int alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 num_blocks, u64 search_start, u64
search_end, u64 owner, struct btrfs_key *ins)
{
Expand Down Expand Up @@ -458,7 +458,7 @@ struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
int ret;
struct buffer_head *buf;

ret = alloc_extent(trans, root, 1, 0, (unsigned long)-1,
ret = btrfs_alloc_extent(trans, root, 1, 0, (unsigned long)-1,
btrfs_header_parentid(btrfs_buffer_header(root->node)), &ins);
if (ret) {
BUG();
Expand Down
52 changes: 50 additions & 2 deletions fs/btrfs/file-item.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"

int btrfs_create_file(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 dirid, u64 *objectid)
int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
u64 objectid, u64 offset,
u64 num_blocks, u64 hint_block,
u64 *result)
{
struct btrfs_key ins;
int ret = 0;
struct btrfs_file_extent_item *item;
struct btrfs_key file_key;
struct btrfs_path path;

btrfs_init_path(&path);
ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block,
(u64)-1, objectid, &ins);
BUG_ON(ret);
file_key.objectid = objectid;
file_key.offset = offset;
file_key.flags = 0;
btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);

ret = btrfs_insert_empty_item(trans, root, &path, &file_key,
sizeof(*item));
item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
struct btrfs_file_extent_item);
btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
btrfs_set_file_extent_disk_num_blocks(item, ins.offset);
btrfs_set_file_extent_offset(item, 0);
btrfs_set_file_extent_num_blocks(item, ins.offset);
mark_buffer_dirty(path.nodes[0]);
*result = ins.objectid;
btrfs_release_path(root, &path);
return 0;
}

int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path, u64 objectid,
u64 blocknr, u64 num_blocks, int mod)
{
int ret;
struct btrfs_key file_key;
int ins_len = mod < 0 ? -1 : 0;
int cow = mod != 0;

file_key.objectid = objectid;
file_key.offset = blocknr;
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;
}
Loading

0 comments on commit dee26a9

Please sign in to comment.