Skip to content

Commit

Permalink
ocfs2: Make ocfs2_extent_tree the first-class representation of a tree.
Browse files Browse the repository at this point in the history
We now have three different kinds of extent trees in ocfs2: inode data
(dinode), extended attributes (xattr_tree), and extended attribute
values (xattr_value).  There is a nice abstraction for them,
ocfs2_extent_tree, but it is hidden in alloc.c.  All the calling
functions have to pick amongst a varied API and pass in type bits and
often extraneous pointers.

A better way is to make ocfs2_extent_tree a first-class object.
Everyone converts their object to an ocfs2_extent_tree() via the
ocfs2_get_*_extent_tree() calls, then uses the ocfs2_extent_tree for all
tree calls to alloc.c.

This simplifies a lot of callers, making for readability.  It also
provides an easy way to add additional extent tree types, as they only
need to be defined in alloc.c with a ocfs2_get_<new>_extent_tree()
function.

Signed-off-by: Joel Becker <joel.becker@oracle.com>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
  • Loading branch information
Joel Becker authored and Mark Fasheh committed Oct 13, 2008
1 parent 1e61ee7 commit f99b9b7
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 332 deletions.
300 changes: 90 additions & 210 deletions fs/ocfs2/alloc.c

Large diffs are not rendered by default.

111 changes: 62 additions & 49 deletions fs/ocfs2/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,66 @@
#ifndef OCFS2_ALLOC_H
#define OCFS2_ALLOC_H

enum ocfs2_extent_tree_type {
OCFS2_DINODE_EXTENT = 0,
OCFS2_XATTR_VALUE_EXTENT,
OCFS2_XATTR_TREE_EXTENT,
};

/*
* For xattr tree leaf, we limit the leaf byte size to be 64K.
*/
#define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536

/*
* ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
* the b-tree operations in ocfs2. Now all the b-tree operations are not
* limited to ocfs2_dinode only. Any data which need to allocate clusters
* to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
* and operation.
*
* ocfs2_extent_tree becomes the first-class object for extent tree
* manipulation. Callers of the alloc.c code need to fill it via one of
* the ocfs2_get_*_extent_tree() operations below.
*
* ocfs2_extent_tree contains info for the root of the b-tree, it must have a
* root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
* functions.
* ocfs2_extent_tree_operations abstract the normal operations we do for
* the root of extent b-tree.
*/
struct ocfs2_extent_tree_operations;
struct ocfs2_extent_tree {
struct ocfs2_extent_tree_operations *et_ops;
struct buffer_head *et_root_bh;
struct ocfs2_extent_list *et_root_el;
void *et_object;
unsigned int et_max_leaf_clusters;
};

/*
* ocfs2_*_get_extent_tree() will fill an ocfs2_extent_tree from the
* specified object buffer. The bh is referenced until
* ocfs2_put_extent_tree().
*/
void ocfs2_get_dinode_extent_tree(struct ocfs2_extent_tree *et,
struct inode *inode,
struct buffer_head *bh);
void ocfs2_get_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
struct inode *inode,
struct buffer_head *bh);
void ocfs2_get_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
struct inode *inode,
struct buffer_head *bh,
struct ocfs2_xattr_value_root *xv);
void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et);

struct ocfs2_alloc_context;
int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
handle_t *handle,
struct inode *inode,
struct buffer_head *root_bh,
u32 cpos,
u64 start_blk,
u32 new_clusters,
u8 flags,
struct ocfs2_alloc_context *meta_ac);
int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
handle_t *handle,
struct inode *inode,
struct buffer_head *root_bh,
u32 cpos,
u64 start_blk,
u32 new_clusters,
u8 flags,
struct ocfs2_alloc_context *meta_ac,
struct ocfs2_xattr_value_root *xv);
int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
handle_t *handle,
struct inode *inode,
struct buffer_head *root_bh,
u32 cpos,
u64 start_blk,
u32 new_clusters,
u8 flags,
struct ocfs2_alloc_context *meta_ac);
int ocfs2_insert_extent(struct ocfs2_super *osb,
handle_t *handle,
struct inode *inode,
struct ocfs2_extent_tree *et,
u32 cpos,
u64 start_blk,
u32 new_clusters,
u8 flags,
struct ocfs2_alloc_context *meta_ac);

enum ocfs2_alloc_restarted {
RESTART_NONE = 0,
RESTART_TRANS,
Expand All @@ -76,32 +96,25 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
u32 *logical_offset,
u32 clusters_to_add,
int mark_unwritten,
struct buffer_head *root_bh,
struct ocfs2_extent_list *root_el,
struct ocfs2_extent_tree *et,
handle_t *handle,
struct ocfs2_alloc_context *data_ac,
struct ocfs2_alloc_context *meta_ac,
enum ocfs2_alloc_restarted *reason_ret,
enum ocfs2_extent_tree_type type,
void *private);
enum ocfs2_alloc_restarted *reason_ret);
struct ocfs2_cached_dealloc_ctxt;
int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
int ocfs2_mark_extent_written(struct inode *inode,
struct ocfs2_extent_tree *et,
handle_t *handle, u32 cpos, u32 len, u32 phys,
struct ocfs2_alloc_context *meta_ac,
struct ocfs2_cached_dealloc_ctxt *dealloc,
enum ocfs2_extent_tree_type et_type,
void *private);
int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
struct ocfs2_cached_dealloc_ctxt *dealloc);
int ocfs2_remove_extent(struct inode *inode,
struct ocfs2_extent_tree *et,
u32 cpos, u32 len, handle_t *handle,
struct ocfs2_alloc_context *meta_ac,
struct ocfs2_cached_dealloc_ctxt *dealloc,
enum ocfs2_extent_tree_type et_type,
void *private);
struct ocfs2_cached_dealloc_ctxt *dealloc);
int ocfs2_num_free_extents(struct ocfs2_super *osb,
struct inode *inode,
struct buffer_head *root_bh,
enum ocfs2_extent_tree_type et_type,
void *private);
struct ocfs2_extent_tree *et);

/*
* how many new metadata chunks would an allocation need at maximum?
Expand Down
16 changes: 10 additions & 6 deletions fs/ocfs2/aops.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ static int ocfs2_write_cluster(struct address_space *mapping,
int ret, i, new, should_zero = 0;
u64 v_blkno, p_blkno;
struct inode *inode = mapping->host;
struct ocfs2_extent_tree et;

new = phys == 0 ? 1 : 0;
if (new || unwritten)
Expand Down Expand Up @@ -1276,10 +1277,11 @@ static int ocfs2_write_cluster(struct address_space *mapping,
goto out;
}
} else if (unwritten) {
ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
ocfs2_get_dinode_extent_tree(&et, inode, wc->w_di_bh);
ret = ocfs2_mark_extent_written(inode, &et,
wc->w_handle, cpos, 1, phys,
meta_ac, &wc->w_dealloc,
OCFS2_DINODE_EXTENT, NULL);
meta_ac, &wc->w_dealloc);
ocfs2_put_extent_tree(&et);
if (ret < 0) {
mlog_errno(ret);
goto out;
Expand Down Expand Up @@ -1666,6 +1668,7 @@ int ocfs2_write_begin_nolock(struct address_space *mapping,
struct ocfs2_alloc_context *data_ac = NULL;
struct ocfs2_alloc_context *meta_ac = NULL;
handle_t *handle;
struct ocfs2_extent_tree et;

ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
if (ret) {
Expand Down Expand Up @@ -1719,10 +1722,11 @@ int ocfs2_write_begin_nolock(struct address_space *mapping,
(long long)i_size_read(inode), le32_to_cpu(di->i_clusters),
clusters_to_alloc, extents_to_split);

ret = ocfs2_lock_allocators(inode, wc->w_di_bh, &di->id2.i_list,
ocfs2_get_dinode_extent_tree(&et, inode, wc->w_di_bh);
ret = ocfs2_lock_allocators(inode, &et,
clusters_to_alloc, extents_to_split,
&data_ac, &meta_ac,
OCFS2_DINODE_EXTENT, NULL);
&data_ac, &meta_ac);
ocfs2_put_extent_tree(&et);
if (ret) {
mlog_errno(ret);
goto out;
Expand Down
20 changes: 12 additions & 8 deletions fs/ocfs2/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,9 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
struct buffer_head *dirdata_bh = NULL;
struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
handle_t *handle;
struct ocfs2_extent_tree et;

ocfs2_get_dinode_extent_tree(&et, dir, di_bh);

alloc = ocfs2_clusters_for_bytes(sb, bytes);

Expand Down Expand Up @@ -1305,8 +1308,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
* This should never fail as our extent list is empty and all
* related blocks have been journaled already.
*/
ret = ocfs2_dinode_insert_extent(osb, handle, dir, di_bh, 0, blkno,
len, 0, NULL);
ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
0, NULL);
if (ret) {
mlog_errno(ret);
goto out_commit;
Expand Down Expand Up @@ -1337,8 +1340,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
}
blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);

ret = ocfs2_dinode_insert_extent(osb, handle, dir, di_bh, 1,
blkno, len, 0, NULL);
ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
blkno, len, 0, NULL);
if (ret) {
mlog_errno(ret);
goto out_commit;
Expand All @@ -1360,6 +1363,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,

brelse(dirdata_bh);

ocfs2_put_extent_tree(&et);
return ret;
}

Expand Down Expand Up @@ -1437,6 +1441,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
struct buffer_head *new_bh = NULL;
struct ocfs2_dir_entry * de;
struct super_block *sb = osb->sb;
struct ocfs2_extent_tree et;

mlog_entry_void();

Expand Down Expand Up @@ -1480,10 +1485,9 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
spin_lock(&OCFS2_I(dir)->ip_lock);
if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
spin_unlock(&OCFS2_I(dir)->ip_lock);
num_free_extents = ocfs2_num_free_extents(osb, dir,
parent_fe_bh,
OCFS2_DINODE_EXTENT,
NULL);
ocfs2_get_dinode_extent_tree(&et, dir, parent_fe_bh);
num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
ocfs2_put_extent_tree(&et);
if (num_free_extents < 0) {
status = num_free_extents;
mlog_errno(status);
Expand Down
36 changes: 22 additions & 14 deletions fs/ocfs2/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,17 @@ int ocfs2_add_inode_data(struct ocfs2_super *osb,
struct ocfs2_alloc_context *meta_ac,
enum ocfs2_alloc_restarted *reason_ret)
{
struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
struct ocfs2_extent_list *el = &fe->id2.i_list;
int ret;
struct ocfs2_extent_tree et;

return ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
ocfs2_get_dinode_extent_tree(&et, inode, fe_bh);
ret = ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
clusters_to_add, mark_unwritten,
fe_bh, el, handle,
data_ac, meta_ac, reason_ret,
OCFS2_DINODE_EXTENT, NULL);
&et, handle,
data_ac, meta_ac, reason_ret);
ocfs2_put_extent_tree(&et);

return ret;
}

static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
Expand All @@ -533,6 +536,7 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
struct ocfs2_alloc_context *meta_ac = NULL;
enum ocfs2_alloc_restarted why;
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
struct ocfs2_extent_tree et;

mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);

Expand Down Expand Up @@ -564,9 +568,10 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
(unsigned long long)OCFS2_I(inode)->ip_blkno,
(long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
clusters_to_add);
status = ocfs2_lock_allocators(inode, bh, &fe->id2.i_list,
clusters_to_add, 0, &data_ac,
&meta_ac, OCFS2_DINODE_EXTENT, NULL);
ocfs2_get_dinode_extent_tree(&et, inode, bh);
status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
&data_ac, &meta_ac);
ocfs2_put_extent_tree(&et);
if (status) {
mlog_errno(status);
goto leave;
Expand Down Expand Up @@ -1236,11 +1241,13 @@ static int __ocfs2_remove_inode_range(struct inode *inode,
handle_t *handle;
struct ocfs2_alloc_context *meta_ac = NULL;
struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
struct ocfs2_extent_tree et;

ocfs2_get_dinode_extent_tree(&et, inode, di_bh);

ret = ocfs2_lock_allocators(inode, di_bh, &di->id2.i_list,
0, 1, NULL, &meta_ac,
OCFS2_DINODE_EXTENT, NULL);
ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
if (ret) {
ocfs2_put_extent_tree(&et);
mlog_errno(ret);
return ret;
}
Expand Down Expand Up @@ -1269,8 +1276,8 @@ static int __ocfs2_remove_inode_range(struct inode *inode,
goto out;
}

ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
dealloc, OCFS2_DINODE_EXTENT, NULL);
ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
dealloc);
if (ret) {
mlog_errno(ret);
goto out_commit;
Expand All @@ -1297,6 +1304,7 @@ static int __ocfs2_remove_inode_range(struct inode *inode,
if (meta_ac)
ocfs2_free_alloc_context(meta_ac);

ocfs2_put_extent_tree(&et);
return ret;
}

Expand Down
12 changes: 5 additions & 7 deletions fs/ocfs2/suballoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1914,12 +1914,11 @@ static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
* File systems which don't support holes call this from
* ocfs2_extend_allocation().
*/
int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh,
struct ocfs2_extent_list *root_el,
int ocfs2_lock_allocators(struct inode *inode,
struct ocfs2_extent_tree *et,
u32 clusters_to_add, u32 extents_to_split,
struct ocfs2_alloc_context **data_ac,
struct ocfs2_alloc_context **meta_ac,
enum ocfs2_extent_tree_type type, void *private)
struct ocfs2_alloc_context **meta_ac)
{
int ret = 0, num_free_extents;
unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
Expand All @@ -1931,8 +1930,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh,

BUG_ON(clusters_to_add != 0 && data_ac == NULL);

num_free_extents = ocfs2_num_free_extents(osb, inode, root_bh,
type, private);
num_free_extents = ocfs2_num_free_extents(osb, inode, et);
if (num_free_extents < 0) {
ret = num_free_extents;
mlog_errno(ret);
Expand All @@ -1954,7 +1952,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh,
*/
if (!num_free_extents ||
(ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
ret = ocfs2_reserve_new_metadata(osb, root_el, meta_ac);
ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
if (ret < 0) {
if (ret != -ENOSPC)
mlog_errno(ret);
Expand Down
6 changes: 2 additions & 4 deletions fs/ocfs2/suballoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster);
int ocfs2_check_group_descriptor(struct super_block *sb,
struct ocfs2_dinode *di,
struct ocfs2_group_desc *gd);
int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh,
struct ocfs2_extent_list *root_el,
int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_extent_tree *et,
u32 clusters_to_add, u32 extents_to_split,
struct ocfs2_alloc_context **data_ac,
struct ocfs2_alloc_context **meta_ac,
enum ocfs2_extent_tree_type type, void *private);
struct ocfs2_alloc_context **meta_ac);
#endif /* _CHAINALLOC_H_ */
Loading

0 comments on commit f99b9b7

Please sign in to comment.