Tao Ma
2008-Jun-27 06:39 UTC
[Ocfs2-devel] [PATCH 0/15] ocfs2: Add extended attributes for ocfs2. V2
Hi all, This is the V2 of Extended Attributes support for ocfs2. They are modified according Mark's review advice and the old last patch has been splitted into 6 small patches. thanks Mark for your review. Regards, Tiger and Tao
Tao Ma
2008-Jun-27 06:59 UTC
[Ocfs2-devel] [PATCH 01/15] Modify ocfs2_num_free_extents for future xattr usage. v2
ocfs2_num_free_extents is used to find the free extent record number. The old parameter is "ocfs2_dinode", and it isn't suitable for xattr_value. So change it to "buffer_head *". ocfs2_lock_allocator is also modified in this patch because we need "buffer_head *" to call ocfs2_num_free_extents. Signed-off-by: Tao Ma <tao.ma at oracle.com> Signed-off-by: Mark Fasheh <mfasheh at suse.com> --- fs/ocfs2/alloc.c | 3 ++- fs/ocfs2/alloc.h | 2 +- fs/ocfs2/aops.c | 5 +++-- fs/ocfs2/dir.c | 3 ++- fs/ocfs2/file.c | 11 ++++++----- fs/ocfs2/file.h | 2 +- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index 10bfb46..c74711f 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -368,12 +368,13 @@ struct ocfs2_merge_ctxt { */ int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, - struct ocfs2_dinode *fe) + struct buffer_head *bh) { int retval; struct ocfs2_extent_list *el; struct ocfs2_extent_block *eb; struct buffer_head *eb_bh = NULL; + struct ocfs2_dinode *fe = (struct ocfs2_dinode *)bh->b_data; mlog_entry_void(); diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h index 42ff94b..758dbda 100644 --- a/fs/ocfs2/alloc.h +++ b/fs/ocfs2/alloc.h @@ -47,7 +47,7 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, struct ocfs2_cached_dealloc_ctxt *dealloc); int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, - struct ocfs2_dinode *fe); + struct buffer_head *bh); /* how many new metadata chunks would an allocation need at maximum? */ static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe) { diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 17964c0..6d933df 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1702,8 +1702,9 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, * ocfs2_lock_allocators(). It greatly over-estimates * the work to be done. */ - ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc, - extents_to_split, &data_ac, &meta_ac); + ret = ocfs2_lock_allocators(inode, wc->w_di_bh, + clusters_to_alloc, extents_to_split, + &data_ac, &meta_ac); if (ret) { mlog_errno(ret); goto out; diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 8a18758..8a14fff 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1474,7 +1474,8 @@ 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, fe); + num_free_extents = ocfs2_num_free_extents(osb, dir, + parent_fe_bh); if (num_free_extents < 0) { status = num_free_extents; mlog_errno(status); diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index 57e0d30..3993312 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -521,7 +521,7 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, if (mark_unwritten) flags = OCFS2_EXT_UNWRITTEN; - free_extents = ocfs2_num_free_extents(osb, inode, fe); + free_extents = ocfs2_num_free_extents(osb, inode, fe_bh); if (free_extents < 0) { status = free_extents; mlog_errno(status); @@ -609,7 +609,7 @@ leave: * File systems which don't support holes call this from * ocfs2_extend_allocation(). */ -int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, +int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *di_bh, u32 clusters_to_add, u32 extents_to_split, struct ocfs2_alloc_context **data_ac, struct ocfs2_alloc_context **meta_ac) @@ -617,6 +617,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, int ret = 0, num_free_extents; unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; *meta_ac = NULL; if (data_ac) @@ -629,7 +630,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, (unsigned long long)OCFS2_I(inode)->ip_blkno, (long long)i_size_read(inode), le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split); - num_free_extents = ocfs2_num_free_extents(osb, inode, di); + num_free_extents = ocfs2_num_free_extents(osb, inode, di_bh); if (num_free_extents < 0) { ret = num_free_extents; mlog_errno(ret); @@ -724,7 +725,7 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, restart_all: BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); - status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac, + status = ocfs2_lock_allocators(inode, bh, clusters_to_add, 0, &data_ac, &meta_ac); if (status) { mlog_errno(status); @@ -1395,7 +1396,7 @@ static int __ocfs2_remove_inode_range(struct inode *inode, struct ocfs2_alloc_context *meta_ac = NULL; struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; - ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac); + ret = ocfs2_lock_allocators(inode, di_bh, 0, 1, NULL, &meta_ac); if (ret) { mlog_errno(ret); return ret; diff --git a/fs/ocfs2/file.h b/fs/ocfs2/file.h index 048ddca..e38ecb2 100644 --- a/fs/ocfs2/file.h +++ b/fs/ocfs2/file.h @@ -55,7 +55,7 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, enum ocfs2_alloc_restarted *reason_ret); int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to); -int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, +int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *fe, u32 clusters_to_add, u32 extents_to_split, struct ocfs2_alloc_context **data_ac, struct ocfs2_alloc_context **meta_ac); -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:00 UTC
[Ocfs2-devel] [PATCH 02/15] Use ocfs2_extent_list instead of ocfs2_dinode. v2
ocfs2_extend_meta_needed, ocfs2_calc_extend_credits and ocfs2_reserve_new_metadata are all useful for extent tree operations. But they are all limited by using ocfs2_dinode as the parameter. Change their parameter to ocfs2_extent_list so that xattr extent list can use them. Signed-off-by: Tao Ma <tao.ma at oracle.com> Signed-off-by: Mark Fasheh <mfasheh at suse.com> --- fs/ocfs2/alloc.c | 3 ++- fs/ocfs2/alloc.h | 12 +++++++++--- fs/ocfs2/aops.c | 3 ++- fs/ocfs2/dir.c | 5 +++-- fs/ocfs2/file.c | 9 +++++---- fs/ocfs2/journal.h | 17 +++++++++++------ fs/ocfs2/suballoc.c | 4 ++-- fs/ocfs2/suballoc.h | 7 ++++++- 8 files changed, 40 insertions(+), 20 deletions(-) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index c74711f..dc844df 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -4536,7 +4536,8 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, } else rightmost_el = path_leaf_el(path); - credits += path->p_tree_depth + ocfs2_extend_meta_needed(di); + credits += path->p_tree_depth + + ocfs2_extend_meta_needed(&di->id2.i_list); ret = ocfs2_extend_trans(handle, credits); if (ret) { mlog_errno(ret); diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h index 758dbda..249e79e 100644 --- a/fs/ocfs2/alloc.h +++ b/fs/ocfs2/alloc.h @@ -48,8 +48,14 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, struct buffer_head *bh); -/* how many new metadata chunks would an allocation need at maximum? */ -static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe) +/* + * how many new metadata chunks would an allocation need at maximum? + * + * Please note that the caller must make sure that root_el is the root + * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise + * the result may be wrong. + */ +static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el) { /* * Rather than do all the work of determining how much we need @@ -59,7 +65,7 @@ static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe) * new tree_depth==0 extent_block, and one block at the new * top-of-the tree. */ - return le16_to_cpu(fe->id2.i_list.l_tree_depth) + 2; + return le16_to_cpu(root_el->l_tree_depth) + 2; } void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di); diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 6d933df..f83a2a4 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1710,7 +1710,8 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, goto out; } - credits = ocfs2_calc_extend_credits(inode->i_sb, di, + credits = ocfs2_calc_extend_credits(inode->i_sb, + &di->id2.i_list, clusters_to_alloc); } diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 8a14fff..5e8cd6d 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1425,6 +1425,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb, int credits, num_free_extents, drop_alloc_sem = 0; loff_t dir_i_size; struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; + struct ocfs2_extent_list *el = &fe->id2.i_list; struct ocfs2_alloc_context *data_ac = NULL; struct ocfs2_alloc_context *meta_ac = NULL; handle_t *handle = NULL; @@ -1483,7 +1484,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb, } if (!num_free_extents) { - status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); + status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); if (status < 0) { if (status != -ENOSPC) mlog_errno(status); @@ -1498,7 +1499,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb, goto bail; } - credits = ocfs2_calc_extend_credits(sb, fe, 1); + credits = ocfs2_calc_extend_credits(sb, el, 1); } else { spin_unlock(&OCFS2_I(dir)->ip_lock); credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index 3993312..79d7da9 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -540,7 +540,7 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, goto leave; } else if ((!free_extents) && (ocfs2_alloc_context_bits_left(meta_ac) - < ocfs2_extend_meta_needed(fe))) { + < ocfs2_extend_meta_needed(&fe->id2.i_list))) { mlog(0, "filesystem is really fragmented...\n"); status = -EAGAIN; reason = RESTART_META; @@ -652,7 +652,7 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *di_bh, */ if (!num_free_extents || (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { - ret = ocfs2_reserve_new_metadata(osb, di, meta_ac); + ret = ocfs2_reserve_new_metadata(osb, &di->id2.i_list, meta_ac); if (ret < 0) { if (ret != -ENOSPC) mlog_errno(ret); @@ -732,7 +732,8 @@ restart_all: goto leave; } - credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); + credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list, + clusters_to_add); handle = ocfs2_start_trans(osb, credits); if (IS_ERR(handle)) { status = PTR_ERR(handle); @@ -790,7 +791,7 @@ restarted_transaction: mlog(0, "restarting transaction.\n"); /* TODO: This can be more intelligent. */ credits = ocfs2_calc_extend_credits(osb->sb, - fe, + &fe->id2.i_list, clusters_to_add); status = ocfs2_extend_trans(handle, credits); if (status < 0) { diff --git a/fs/ocfs2/journal.h b/fs/ocfs2/journal.h index db82be2..f1479ab 100644 --- a/fs/ocfs2/journal.h +++ b/fs/ocfs2/journal.h @@ -339,11 +339,16 @@ int ocfs2_journal_dirty_data(handle_t *handle, #define OCFS2_RENAME_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 3 \ + OCFS2_UNLINK_CREDITS) +/* + * Please note that the caller must make sure that root_el is the root + * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise + * the result may be wrong. + */ static inline int ocfs2_calc_extend_credits(struct super_block *sb, - struct ocfs2_dinode *fe, + struct ocfs2_extent_list *root_el, u32 bits_wanted) { - int bitmap_blocks, sysfile_bitmap_blocks, dinode_blocks; + int bitmap_blocks, sysfile_bitmap_blocks, extent_blocks; /* bitmap dinode, group desc. + relinked group. */ bitmap_blocks = OCFS2_SUBALLOC_ALLOC; @@ -354,16 +359,16 @@ static inline int ocfs2_calc_extend_credits(struct super_block *sb, * however many metadata chunks needed * a remaining suballoc * alloc. */ sysfile_bitmap_blocks = 1 + - (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(fe); + (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(root_el); /* this does not include *new* metadata blocks, which are - * accounted for in sysfile_bitmap_blocks. fe + + * accounted for in sysfile_bitmap_blocks. root_el + * prev. last_eb_blk + blocks along edge of tree. * calc_symlink_credits passes because we just need 1 * credit for the dinode there. */ - dinode_blocks = 1 + 1 + le16_to_cpu(fe->id2.i_list.l_tree_depth); + extent_blocks = 1 + 1 + le16_to_cpu(root_el->l_tree_depth); - return bitmap_blocks + sysfile_bitmap_blocks + dinode_blocks; + return bitmap_blocks + sysfile_bitmap_blocks + extent_blocks; } static inline int ocfs2_calc_symlink_credits(struct super_block *sb) diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c index d2d278f..af769a5 100644 --- a/fs/ocfs2/suballoc.c +++ b/fs/ocfs2/suballoc.c @@ -494,7 +494,7 @@ bail: } int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, - struct ocfs2_dinode *fe, + struct ocfs2_extent_list *root_el, struct ocfs2_alloc_context **ac) { int status; @@ -507,7 +507,7 @@ int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, goto bail; } - (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe); + (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(root_el); (*ac)->ac_which = OCFS2_AC_USE_META; slot = osb->slot_num; (*ac)->ac_group_search = ocfs2_block_group_search; diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h index 544c600..d024c69 100644 --- a/fs/ocfs2/suballoc.h +++ b/fs/ocfs2/suballoc.h @@ -59,8 +59,13 @@ static inline int ocfs2_alloc_context_bits_left(struct ocfs2_alloc_context *ac) return ac->ac_bits_wanted - ac->ac_bits_given; } +/* + * Please note that the caller must make sure that root_el is the root + * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise + * the result may be wrong. + */ int ocfs2_reserve_new_metadata(struct ocfs2_super *osb, - struct ocfs2_dinode *fe, + struct ocfs2_extent_list *root_el, struct ocfs2_alloc_context **ac); int ocfs2_reserve_new_inode(struct ocfs2_super *osb, struct ocfs2_alloc_context **ac); -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:00 UTC
[Ocfs2-devel] [PATCH 03/15] Abstract ocfs2_extent_tree in b-tree operations. v2
Modification from V1 to V2: 1. Add some small wrappers around extent tree calls. 2. Add update clusters in extent tree operations. 3. Describe the patch more clearly in the commit log. In the old extent tree operation, we take the hypothesis that we are using the ocfs2_extent_list in ocfs2_dinode as the tree root. As xattr will also use ocfs2_extent_list to store large value for a xattr entry, we refactor the tree operation so that xattr can use it directly. The refactoring includes 4 steps: 1. Abstract set/get of last_eb_blk and update_clusters since they may be stored in different location for dinode and xattr. 2. Add a new structure named ocfs2_extent_tree to indicate the extent tree the operation will work on. 3. Remove all the use of fe_bh and di, use root_bh and root_el in extent tree instead. So now all the fe_bh is replaced with et->root_bh, el with root_el accordingly. 4. Make ocfs2_lock_allocators generic. Now it is limited to be only used in file extend allocation. But the whole function is useful when we want to store large EAs. Note: This patch hasn't touch the code of ocfs2_commit_truncate since it isn't has been modified with the new tree operation. So all the truncating code deem that the tree root is ocfs2_dinode. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/alloc.c | 508 +++++++++++++++++++++++++++++++++------------------ fs/ocfs2/alloc.h | 23 ++- fs/ocfs2/aops.c | 11 +- fs/ocfs2/dir.c | 7 +- fs/ocfs2/file.c | 104 ++--------- fs/ocfs2/file.h | 4 - fs/ocfs2/suballoc.c | 82 ++++++++ fs/ocfs2/suballoc.h | 5 + 8 files changed, 456 insertions(+), 288 deletions(-) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index dc844df..90cefc5 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -49,6 +49,143 @@ #include "buffer_head_io.h" +/* + * 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 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; + +struct ocfs2_extent_tree_operations { + void (*set_last_eb_blk) (struct ocfs2_extent_tree *et, u64 blkno); + u64 (*get_last_eb_blk) (struct ocfs2_extent_tree *et); + void (*update_clusters) (struct inode *inode, + struct ocfs2_extent_tree *et, + u32 new_clusters); + int (*sanity_check) (struct inode *inode, struct ocfs2_extent_tree *et); +}; + +struct ocfs2_extent_tree { + enum ocfs2_extent_tree_type type; + struct ocfs2_extent_tree_operations *eops; + struct buffer_head *root_bh; + struct ocfs2_extent_list *root_el; +}; + +static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, + u64 blkno) +{ + struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data; + + BUG_ON(et->type != OCFS2_DINODE_EXTENT); + di->i_last_eb_blk = cpu_to_le64(blkno); +} + +static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) +{ + struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data; + + BUG_ON(et->type != OCFS2_DINODE_EXTENT); + return le64_to_cpu(di->i_last_eb_blk); +} + +static void ocfs2_dinode_update_clusters(struct inode *inode, + struct ocfs2_extent_tree *et, + u32 clusters) +{ + struct ocfs2_dinode *di + (struct ocfs2_dinode *)et->root_bh->b_data; + + le32_add_cpu(&di->i_clusters, clusters); + spin_lock(&OCFS2_I(inode)->ip_lock); + OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters); + spin_unlock(&OCFS2_I(inode)->ip_lock); +} + +static int ocfs2_dinode_sanity_check(struct inode *inode, + struct ocfs2_extent_tree *et) +{ + int ret = 0; + struct ocfs2_dinode *di; + + BUG_ON(et->type != OCFS2_DINODE_EXTENT); + + di = (struct ocfs2_dinode *)et->root_bh->b_data; + if (!OCFS2_IS_VALID_DINODE(di)) { + ret = -EIO; + ocfs2_error(inode->i_sb, + "Inode %llu has invalid path root", + (unsigned long long)OCFS2_I(inode)->ip_blkno); + } + + return ret; +} + +static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { + .set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, + .get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, + .update_clusters = ocfs2_dinode_update_clusters, + .sanity_check = ocfs2_dinode_sanity_check, +}; + +static struct ocfs2_extent_tree* + ocfs2_new_extent_tree(struct buffer_head *bh, + enum ocfs2_extent_tree_type et_type) +{ + struct ocfs2_extent_tree *et; + + et = kzalloc(sizeof(*et), GFP_NOFS); + if (!et) + return NULL; + + et->type = et_type; + get_bh(bh); + et->root_bh = bh; + + /* current we only support dinode extent. */ + BUG_ON(et->type != OCFS2_DINODE_EXTENT); + if (et_type == OCFS2_DINODE_EXTENT) { + et->root_el = &((struct ocfs2_dinode *)bh->b_data)->id2.i_list; + et->eops = &ocfs2_dinode_et_ops; + } + + return et; +} + +static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et) +{ + if (et) { + brelse(et->root_bh); + kfree(et); + } +} + +static inline void ocfs2_set_last_eb_blk(struct ocfs2_extent_tree *et, + u64 new_last_eb_blk) +{ + et->eops->set_last_eb_blk(et, new_last_eb_blk); +} + +static inline u64 ocfs2_get_last_eb_blk(struct ocfs2_extent_tree *et) +{ + return et->eops->get_last_eb_blk(et); +} + +static inline void ocfs2_update_clusters(struct inode *inode, + struct ocfs2_extent_tree *et, + u32 clusters) +{ + et->eops->update_clusters(inode, et, clusters); +} + static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, struct ocfs2_extent_block *eb); @@ -205,17 +342,6 @@ static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, } /* - * Allocate and initialize a new path based on a disk inode tree. - */ -static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh) -{ - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; - struct ocfs2_extent_list *el = &di->id2.i_list; - - return ocfs2_new_path(di_bh, el); -} - -/* * Convenience function to journal all components in a path. */ static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle, @@ -368,24 +494,33 @@ struct ocfs2_merge_ctxt { */ int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, - struct buffer_head *bh) + struct buffer_head *root_bh, + enum ocfs2_extent_tree_type type) { int retval; - struct ocfs2_extent_list *el; + struct ocfs2_extent_list *el = NULL; struct ocfs2_extent_block *eb; struct buffer_head *eb_bh = NULL; - struct ocfs2_dinode *fe = (struct ocfs2_dinode *)bh->b_data; + u64 last_eb_blk = 0; mlog_entry_void(); - if (!OCFS2_IS_VALID_DINODE(fe)) { - OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); - retval = -EIO; - goto bail; + if (type == OCFS2_DINODE_EXTENT) { + struct ocfs2_dinode *fe + (struct ocfs2_dinode *)root_bh->b_data; + if (!OCFS2_IS_VALID_DINODE(fe)) { + OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); + retval = -EIO; + goto bail; + } + + if (fe->i_last_eb_blk) + last_eb_blk = le64_to_cpu(fe->i_last_eb_blk); + el = &fe->id2.i_list; } - if (fe->i_last_eb_blk) { - retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), + if (last_eb_blk) { + retval = ocfs2_read_block(osb, last_eb_blk, &eb_bh, OCFS2_BH_CACHED, inode); if (retval < 0) { mlog_errno(retval); @@ -393,8 +528,7 @@ int ocfs2_num_free_extents(struct ocfs2_super *osb, } eb = (struct ocfs2_extent_block *) eb_bh->b_data; el = &eb->h_list; - } else - el = &fe->id2.i_list; + } BUG_ON(el->l_tree_depth != 0); @@ -532,7 +666,7 @@ static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) static int ocfs2_add_branch(struct ocfs2_super *osb, handle_t *handle, struct inode *inode, - struct buffer_head *fe_bh, + struct ocfs2_extent_tree *et, struct buffer_head *eb_bh, struct buffer_head **last_eb_bh, struct ocfs2_alloc_context *meta_ac) @@ -541,7 +675,6 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, u64 next_blkno, new_last_eb_blk; struct buffer_head *bh; struct buffer_head **new_eb_bhs = NULL; - struct ocfs2_dinode *fe; struct ocfs2_extent_block *eb; struct ocfs2_extent_list *eb_el; struct ocfs2_extent_list *el; @@ -551,13 +684,11 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, BUG_ON(!last_eb_bh || !*last_eb_bh); - fe = (struct ocfs2_dinode *) fe_bh->b_data; - if (eb_bh) { eb = (struct ocfs2_extent_block *) eb_bh->b_data; el = &eb->h_list; } else - el = &fe->id2.i_list; + el = et->root_el; /* we never add a branch to a leaf. */ BUG_ON(!el->l_tree_depth); @@ -647,7 +778,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, mlog_errno(status); goto bail; } - status = ocfs2_journal_access(handle, inode, fe_bh, + status = ocfs2_journal_access(handle, inode, et->root_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (status < 0) { mlog_errno(status); @@ -663,7 +794,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, } /* Link the new branch into the rest of the tree (el will - * either be on the fe, or the extent block passed in. */ + * either be on the root_bh, or the extent block passed in. */ i = le16_to_cpu(el->l_next_free_rec); el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); @@ -672,7 +803,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, /* fe needs a new last extent block pointer, as does the * next_leaf on the previously last-extent-block. */ - fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk); + ocfs2_set_last_eb_blk(et, new_last_eb_blk); eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); @@ -680,7 +811,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb, status = ocfs2_journal_dirty(handle, *last_eb_bh); if (status < 0) mlog_errno(status); - status = ocfs2_journal_dirty(handle, fe_bh); + status = ocfs2_journal_dirty(handle, et->root_bh); if (status < 0) mlog_errno(status); if (eb_bh) { @@ -718,16 +849,15 @@ bail: static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, handle_t *handle, struct inode *inode, - struct buffer_head *fe_bh, + struct ocfs2_extent_tree *et, struct ocfs2_alloc_context *meta_ac, struct buffer_head **ret_new_eb_bh) { int status, i; u32 new_clusters; struct buffer_head *new_eb_bh = NULL; - struct ocfs2_dinode *fe; struct ocfs2_extent_block *eb; - struct ocfs2_extent_list *fe_el; + struct ocfs2_extent_list *root_el; struct ocfs2_extent_list *eb_el; mlog_entry_void(); @@ -747,8 +877,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, } eb_el = &eb->h_list; - fe = (struct ocfs2_dinode *) fe_bh->b_data; - fe_el = &fe->id2.i_list; + root_el = et->root_el; status = ocfs2_journal_access(handle, inode, new_eb_bh, OCFS2_JOURNAL_ACCESS_CREATE); @@ -757,11 +886,11 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, goto bail; } - /* copy the fe data into the new extent block */ - eb_el->l_tree_depth = fe_el->l_tree_depth; - eb_el->l_next_free_rec = fe_el->l_next_free_rec; - for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) - eb_el->l_recs[i] = fe_el->l_recs[i]; + /* copy the root extent list data into the new extent block */ + eb_el->l_tree_depth = root_el->l_tree_depth; + eb_el->l_next_free_rec = root_el->l_next_free_rec; + for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) + eb_el->l_recs[i] = root_el->l_recs[i]; status = ocfs2_journal_dirty(handle, new_eb_bh); if (status < 0) { @@ -769,7 +898,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, goto bail; } - status = ocfs2_journal_access(handle, inode, fe_bh, + status = ocfs2_journal_access(handle, inode, et->root_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (status < 0) { mlog_errno(status); @@ -778,21 +907,21 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, new_clusters = ocfs2_sum_rightmost_rec(eb_el); - /* update fe now */ - le16_add_cpu(&fe_el->l_tree_depth, 1); - fe_el->l_recs[0].e_cpos = 0; - fe_el->l_recs[0].e_blkno = eb->h_blkno; - fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); - for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) - memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); - fe_el->l_next_free_rec = cpu_to_le16(1); + /* update root_bh now */ + le16_add_cpu(&root_el->l_tree_depth, 1); + root_el->l_recs[0].e_cpos = 0; + root_el->l_recs[0].e_blkno = eb->h_blkno; + root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); + for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) + memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); + root_el->l_next_free_rec = cpu_to_le16(1); /* If this is our 1st tree depth shift, then last_eb_blk * becomes the allocated extent block */ - if (fe_el->l_tree_depth == cpu_to_le16(1)) - fe->i_last_eb_blk = eb->h_blkno; + if (root_el->l_tree_depth == cpu_to_le16(1)) + ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); - status = ocfs2_journal_dirty(handle, fe_bh); + status = ocfs2_journal_dirty(handle, et->root_bh); if (status < 0) { mlog_errno(status); goto bail; @@ -818,22 +947,21 @@ bail: * 1) a lowest extent block is found, then we pass it back in * *lowest_eb_bh and return '0' * - * 2) the search fails to find anything, but the dinode has room. We + * 2) the search fails to find anything, but the root_el has room. We * pass NULL back in *lowest_eb_bh, but still return '0' * - * 3) the search fails to find anything AND the dinode is full, in + * 3) the search fails to find anything AND the root_el is full, in * which case we return > 0 * * return status < 0 indicates an error. */ static int ocfs2_find_branch_target(struct ocfs2_super *osb, struct inode *inode, - struct buffer_head *fe_bh, + struct ocfs2_extent_tree *et, struct buffer_head **target_bh) { int status = 0, i; u64 blkno; - struct ocfs2_dinode *fe; struct ocfs2_extent_block *eb; struct ocfs2_extent_list *el; struct buffer_head *bh = NULL; @@ -843,8 +971,7 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb, *target_bh = NULL; - fe = (struct ocfs2_dinode *) fe_bh->b_data; - el = &fe->id2.i_list; + el = et->root_el; while(le16_to_cpu(el->l_tree_depth) > 1) { if (le16_to_cpu(el->l_next_free_rec) == 0) { @@ -896,8 +1023,8 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb, /* If we didn't find one and the fe doesn't have any room, * then return '1' */ - if (!lowest_bh - && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count)) + el = et->root_el; + if (!lowest_bh && (el->l_next_free_rec == el->l_count)) status = 1; *target_bh = lowest_bh; @@ -920,19 +1047,19 @@ bail: * *last_eb_bh will be updated by ocfs2_add_branch(). */ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, - struct buffer_head *di_bh, int *final_depth, + struct ocfs2_extent_tree *et, int *final_depth, struct buffer_head **last_eb_bh, struct ocfs2_alloc_context *meta_ac) { int ret, shift; - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; - int depth = le16_to_cpu(di->id2.i_list.l_tree_depth); + struct ocfs2_extent_list *el = et->root_el; + int depth = le16_to_cpu(el->l_tree_depth); struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); struct buffer_head *bh = NULL; BUG_ON(meta_ac == NULL); - shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh); + shift = ocfs2_find_branch_target(osb, inode, et, &bh); if (shift < 0) { ret = shift; mlog_errno(ret); @@ -949,7 +1076,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, /* ocfs2_shift_tree_depth will return us a buffer with * the new extent block (so we can pass that to * ocfs2_add_branch). */ - ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh, + ret = ocfs2_shift_tree_depth(osb, handle, inode, et, meta_ac, &bh); if (ret < 0) { mlog_errno(ret); @@ -976,7 +1103,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, /* call ocfs2_add_branch to add the final part of the tree with * the new data. */ mlog(0, "add branch. bh = %p\n", bh); - ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh, + ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh, meta_ac); if (ret < 0) { mlog_errno(ret); @@ -2068,11 +2195,11 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, struct ocfs2_path *right_path, int subtree_index, struct ocfs2_cached_dealloc_ctxt *dealloc, - int *deleted) + int *deleted, + struct ocfs2_extent_tree *et) { int ret, i, del_right_subtree = 0, right_has_empty = 0; - struct buffer_head *root_bh, *di_bh = path_root_bh(right_path); - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; + struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path); struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; struct ocfs2_extent_block *eb; @@ -2124,7 +2251,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, * We have to update i_last_eb_blk during the meta * data delete. */ - ret = ocfs2_journal_access(handle, inode, di_bh, + ret = ocfs2_journal_access(handle, inode, et_root_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (ret) { mlog_errno(ret); @@ -2199,7 +2326,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, ocfs2_update_edge_lengths(inode, handle, left_path); eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; - di->i_last_eb_blk = eb->h_blkno; + ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); /* * Removal of the extent in the left leaf was skipped @@ -2209,7 +2336,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, if (right_has_empty) ocfs2_remove_empty_extent(left_leaf_el); - ret = ocfs2_journal_dirty(handle, di_bh); + ret = ocfs2_journal_dirty(handle, et_root_bh); if (ret) mlog_errno(ret); @@ -2332,7 +2459,8 @@ static int __ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, int orig_credits, struct ocfs2_path *path, struct ocfs2_cached_dealloc_ctxt *dealloc, - struct ocfs2_path **empty_extent_path) + struct ocfs2_path **empty_extent_path, + struct ocfs2_extent_tree *et) { int ret, subtree_root, deleted; u32 right_cpos; @@ -2405,7 +2533,7 @@ static int __ocfs2_rotate_tree_left(struct inode *inode, ret = ocfs2_rotate_subtree_left(inode, handle, left_path, right_path, subtree_root, - dealloc, &deleted); + dealloc, &deleted, et); if (ret == -EAGAIN) { /* * The rotation has to temporarily stop due to @@ -2448,29 +2576,20 @@ out: } static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, - struct ocfs2_path *path, - struct ocfs2_cached_dealloc_ctxt *dealloc) + struct ocfs2_path *path, + struct ocfs2_cached_dealloc_ctxt *dealloc, + struct ocfs2_extent_tree *et) { int ret, subtree_index; u32 cpos; struct ocfs2_path *left_path = NULL; - struct ocfs2_dinode *di; struct ocfs2_extent_block *eb; struct ocfs2_extent_list *el; - /* - * XXX: This code assumes that the root is an inode, which is - * true for now but may change as tree code gets generic. - */ - di = (struct ocfs2_dinode *)path_root_bh(path)->b_data; - if (!OCFS2_IS_VALID_DINODE(di)) { - ret = -EIO; - ocfs2_error(inode->i_sb, - "Inode %llu has invalid path root", - (unsigned long long)OCFS2_I(inode)->ip_blkno); - goto out; - } + ret = et->eops->sanity_check(inode, et); + if (ret) + goto out; /* * There's two ways we handle this depending on * whether path is the only existing one. @@ -2527,7 +2646,7 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, ocfs2_update_edge_lengths(inode, handle, left_path); eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; - di->i_last_eb_blk = eb->h_blkno; + ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); } else { /* * 'path' is also the leftmost path which @@ -2538,12 +2657,12 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, */ ocfs2_unlink_path(inode, handle, dealloc, path, 1); - el = &di->id2.i_list; + el = et->root_el; el->l_tree_depth = 0; el->l_next_free_rec = 0; memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); - di->i_last_eb_blk = 0; + ocfs2_set_last_eb_blk(et, 0); } ocfs2_journal_dirty(handle, path_root_bh(path)); @@ -2571,7 +2690,8 @@ out: */ static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, struct ocfs2_path *path, - struct ocfs2_cached_dealloc_ctxt *dealloc) + struct ocfs2_cached_dealloc_ctxt *dealloc, + struct ocfs2_extent_tree *et) { int ret, orig_credits = handle->h_buffer_credits; struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; @@ -2585,7 +2705,7 @@ static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, if (path->p_tree_depth == 0) { rightmost_no_delete: /* - * In-inode extents. This is trivially handled, so do + * Inline extents. This is trivially handled, so do * it up front. */ ret = ocfs2_rotate_rightmost_leaf_left(inode, handle, @@ -2639,7 +2759,7 @@ rightmost_no_delete: */ ret = ocfs2_remove_rightmost_path(inode, handle, path, - dealloc); + dealloc, et); if (ret) mlog_errno(ret); goto out; @@ -2651,7 +2771,7 @@ rightmost_no_delete: */ try_rotate: ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path, - dealloc, &restart_path); + dealloc, &restart_path, et); if (ret && ret != -EAGAIN) { mlog_errno(ret); goto out; @@ -2663,7 +2783,7 @@ try_rotate: ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, tmp_path, dealloc, - &restart_path); + &restart_path, et); if (ret && ret != -EAGAIN) { mlog_errno(ret); goto out; @@ -2949,6 +3069,7 @@ static int ocfs2_merge_rec_left(struct inode *inode, handle_t *handle, struct ocfs2_extent_rec *split_rec, struct ocfs2_cached_dealloc_ctxt *dealloc, + struct ocfs2_extent_tree *et, int index) { int ret, i, subtree_index = 0, has_empty_extent = 0; @@ -3069,7 +3190,8 @@ static int ocfs2_merge_rec_left(struct inode *inode, le16_to_cpu(el->l_next_free_rec) == 1) { ret = ocfs2_remove_rightmost_path(inode, handle, - right_path, dealloc); + right_path, + dealloc, et); if (ret) { mlog_errno(ret); goto out; @@ -3096,7 +3218,8 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, int split_index, struct ocfs2_extent_rec *split_rec, struct ocfs2_cached_dealloc_ctxt *dealloc, - struct ocfs2_merge_ctxt *ctxt) + struct ocfs2_merge_ctxt *ctxt, + struct ocfs2_extent_tree *et) { int ret = 0; @@ -3114,7 +3237,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, * illegal. */ ret = ocfs2_rotate_tree_left(inode, handle, path, - dealloc); + dealloc, et); if (ret) { mlog_errno(ret); goto out; @@ -3157,7 +3280,8 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); /* The merge left us with an empty extent, remove it. */ - ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); + ret = ocfs2_rotate_tree_left(inode, handle, path, + dealloc, et); if (ret) { mlog_errno(ret); goto out; @@ -3171,7 +3295,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, */ ret = ocfs2_merge_rec_left(inode, path, handle, rec, - dealloc, + dealloc, et, split_index); if (ret) { @@ -3180,7 +3304,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, } ret = ocfs2_rotate_tree_left(inode, handle, path, - dealloc); + dealloc, et); /* * Error from this last rotate is not critical, so * print but don't bubble it up. @@ -3200,7 +3324,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, ret = ocfs2_merge_rec_left(inode, path, handle, split_rec, - dealloc, + dealloc, et, split_index); if (ret) { mlog_errno(ret); @@ -3223,7 +3347,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode, * our leaf. Try to rotate it away. */ ret = ocfs2_rotate_tree_left(inode, handle, path, - dealloc); + dealloc, et); if (ret) mlog_errno(ret); ret = 0; @@ -3357,16 +3481,6 @@ rotate: ocfs2_rotate_leaf(el, insert_rec); } -static inline void ocfs2_update_dinode_clusters(struct inode *inode, - struct ocfs2_dinode *di, - u32 clusters) -{ - le32_add_cpu(&di->i_clusters, clusters); - spin_lock(&OCFS2_I(inode)->ip_lock); - OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters); - spin_unlock(&OCFS2_I(inode)->ip_lock); -} - static void ocfs2_adjust_rightmost_records(struct inode *inode, handle_t *handle, struct ocfs2_path *path, @@ -3568,8 +3682,8 @@ static void ocfs2_split_record(struct inode *inode, } /* - * This function only does inserts on an allocation b-tree. For dinode - * lists, ocfs2_insert_at_leaf() is called directly. + * This function only does inserts on an allocation b-tree. For tree + * depth = 0, ocfs2_insert_at_leaf() is called directly. * * right_path is the path we want to do the actual insert * in. left_path should only be passed in if we need to update that @@ -3666,7 +3780,7 @@ out: static int ocfs2_do_insert_extent(struct inode *inode, handle_t *handle, - struct buffer_head *di_bh, + struct ocfs2_extent_tree *et, struct ocfs2_extent_rec *insert_rec, struct ocfs2_insert_type *type) { @@ -3674,13 +3788,11 @@ static int ocfs2_do_insert_extent(struct inode *inode, u32 cpos; struct ocfs2_path *right_path = NULL; struct ocfs2_path *left_path = NULL; - struct ocfs2_dinode *di; struct ocfs2_extent_list *el; - di = (struct ocfs2_dinode *) di_bh->b_data; - el = &di->id2.i_list; + el = et->root_el; - ret = ocfs2_journal_access(handle, inode, di_bh, + ret = ocfs2_journal_access(handle, inode, et->root_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (ret) { mlog_errno(ret); @@ -3692,7 +3804,7 @@ static int ocfs2_do_insert_extent(struct inode *inode, goto out_update_clusters; } - right_path = ocfs2_new_inode_path(di_bh); + right_path = ocfs2_new_path(et->root_bh, et->root_el); if (!right_path) { ret = -ENOMEM; mlog_errno(ret); @@ -3742,7 +3854,7 @@ static int ocfs2_do_insert_extent(struct inode *inode, * ocfs2_rotate_tree_right() might have extended the * transaction without re-journaling our tree root. */ - ret = ocfs2_journal_access(handle, inode, di_bh, + ret = ocfs2_journal_access(handle, inode, et->root_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (ret) { mlog_errno(ret); @@ -3767,10 +3879,10 @@ static int ocfs2_do_insert_extent(struct inode *inode, out_update_clusters: if (type->ins_split == SPLIT_NONE) - ocfs2_update_dinode_clusters(inode, di, - le16_to_cpu(insert_rec->e_leaf_clusters)); + ocfs2_update_clusters(inode, et, + le16_to_cpu(insert_rec->e_leaf_clusters)); - ret = ocfs2_journal_dirty(handle, di_bh); + ret = ocfs2_journal_dirty(handle, et->root_bh); if (ret) mlog_errno(ret); @@ -3924,8 +4036,8 @@ static void ocfs2_figure_contig_type(struct inode *inode, * ocfs2_figure_appending_type() will figure out whether we'll have to * insert at the tail of the rightmost leaf. * - * This should also work against the dinode list for tree's with 0 - * depth. If we consider the dinode list to be the rightmost leaf node + * This should also work against the root extent list for tree's with 0 + * depth. If we consider the root extent list to be the rightmost leaf node * then the logic here makes sense. */ static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, @@ -3976,14 +4088,13 @@ set_tail_append: * structure. */ static int ocfs2_figure_insert_type(struct inode *inode, - struct buffer_head *di_bh, + struct ocfs2_extent_tree *et, struct buffer_head **last_eb_bh, struct ocfs2_extent_rec *insert_rec, int *free_records, struct ocfs2_insert_type *insert) { int ret; - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; struct ocfs2_extent_block *eb; struct ocfs2_extent_list *el; struct ocfs2_path *path = NULL; @@ -3991,7 +4102,7 @@ static int ocfs2_figure_insert_type(struct inode *inode, insert->ins_split = SPLIT_NONE; - el = &di->id2.i_list; + el = et->root_el; insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); if (el->l_tree_depth) { @@ -4002,7 +4113,7 @@ static int ocfs2_figure_insert_type(struct inode *inode, * may want it later. */ ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), - le64_to_cpu(di->i_last_eb_blk), &bh, + ocfs2_get_last_eb_blk(et), &bh, OCFS2_BH_CACHED, inode); if (ret) { mlog_exit(ret); @@ -4029,7 +4140,7 @@ static int ocfs2_figure_insert_type(struct inode *inode, return 0; } - path = ocfs2_new_inode_path(di_bh); + path = ocfs2_new_path(et->root_bh, et->root_el); if (!path) { ret = -ENOMEM; mlog_errno(ret); @@ -4079,7 +4190,8 @@ static int ocfs2_figure_insert_type(struct inode *inode, * the case that we're doing a tail append, so maybe we can * take advantage of that information somehow. */ - if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) { + if (ocfs2_get_last_eb_blk(et) =+ path_leaf_bh(path)->b_blocknr) { /* * Ok, ocfs2_find_path() returned us the rightmost * tree path. This might be an appending insert. There are @@ -4109,21 +4221,30 @@ out: int ocfs2_insert_extent(struct ocfs2_super *osb, handle_t *handle, struct inode *inode, - struct buffer_head *fe_bh, + struct buffer_head *root_bh, u32 cpos, u64 start_blk, u32 new_clusters, u8 flags, - struct ocfs2_alloc_context *meta_ac) + struct ocfs2_alloc_context *meta_ac, + enum ocfs2_extent_tree_type et_type) { int status; int uninitialized_var(free_records); struct buffer_head *last_eb_bh = NULL; struct ocfs2_insert_type insert = {0, }; struct ocfs2_extent_rec rec; + struct ocfs2_extent_tree *et = NULL; BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL); + et = ocfs2_new_extent_tree(root_bh, et_type); + if (!et) { + status = -ENOMEM; + mlog_errno(status); + goto bail; + } + mlog(0, "add %u clusters at position %u to inode %llu\n", new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); @@ -4141,7 +4262,7 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, rec.e_leaf_clusters = cpu_to_le16(new_clusters); rec.e_flags = flags; - status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec, + status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec, &free_records, &insert); if (status < 0) { mlog_errno(status); @@ -4155,7 +4276,7 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, free_records, insert.ins_tree_depth); if (insert.ins_contig == CONTIG_NONE && free_records == 0) { - status = ocfs2_grow_tree(inode, handle, fe_bh, + status = ocfs2_grow_tree(inode, handle, et, &insert.ins_tree_depth, &last_eb_bh, meta_ac); if (status) { @@ -4165,16 +4286,18 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, } /* Finally, we can add clusters. This might rotate the tree for us. */ - status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert); + status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert); if (status < 0) mlog_errno(status); - else + else if (et->type == OCFS2_DINODE_EXTENT) ocfs2_extent_map_insert_rec(inode, &rec); bail: if (last_eb_bh) brelse(last_eb_bh); + if (et) + ocfs2_free_extent_tree(et); mlog_exit(status); return status; } @@ -4202,7 +4325,7 @@ static void ocfs2_make_right_split_rec(struct super_block *sb, static int ocfs2_split_and_insert(struct inode *inode, handle_t *handle, struct ocfs2_path *path, - struct buffer_head *di_bh, + struct ocfs2_extent_tree *et, struct buffer_head **last_eb_bh, int split_index, struct ocfs2_extent_rec *orig_split_rec, @@ -4216,7 +4339,6 @@ static int ocfs2_split_and_insert(struct inode *inode, struct ocfs2_extent_rec split_rec = *orig_split_rec; struct ocfs2_insert_type insert; struct ocfs2_extent_block *eb; - struct ocfs2_dinode *di; leftright: /* @@ -4225,8 +4347,7 @@ leftright: */ rec = path_leaf_el(path)->l_recs[split_index]; - di = (struct ocfs2_dinode *)di_bh->b_data; - rightmost_el = &di->id2.i_list; + rightmost_el = et->root_el; depth = le16_to_cpu(rightmost_el->l_tree_depth); if (depth) { @@ -4237,8 +4358,8 @@ leftright: if (le16_to_cpu(rightmost_el->l_next_free_rec) = le16_to_cpu(rightmost_el->l_count)) { - ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh, - meta_ac); + ret = ocfs2_grow_tree(inode, handle, et, + &depth, last_eb_bh, meta_ac); if (ret) { mlog_errno(ret); goto out; @@ -4275,8 +4396,7 @@ leftright: do_leftright = 1; } - ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, - &insert); + ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); if (ret) { mlog_errno(ret); goto out; @@ -4318,8 +4438,9 @@ out: * of the tree is required. All other cases will degrade into a less * optimal tree layout. * - * last_eb_bh should be the rightmost leaf block for any inode with a - * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call. + * last_eb_bh should be the rightmost leaf block for any extent + * btree. Since a split may grow the tree or a merge might shrink it, + * the caller cannot trust the contents of that buffer after this call. * * This code is optimized for readability - several passes might be * made over certain portions of the tree. All of those blocks will @@ -4327,7 +4448,7 @@ out: * extra overhead is not expressed in terms of disk reads. */ static int __ocfs2_mark_extent_written(struct inode *inode, - struct buffer_head *di_bh, + struct ocfs2_extent_tree *et, handle_t *handle, struct ocfs2_path *path, int split_index, @@ -4367,10 +4488,9 @@ static int __ocfs2_mark_extent_written(struct inode *inode, */ if (path->p_tree_depth) { struct ocfs2_extent_block *eb; - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), - le64_to_cpu(di->i_last_eb_blk), + ocfs2_get_last_eb_blk(et), &last_eb_bh, OCFS2_BH_CACHED, inode); if (ret) { mlog_exit(ret); @@ -4404,7 +4524,7 @@ static int __ocfs2_mark_extent_written(struct inode *inode, if (ctxt.c_split_covers_rec) el->l_recs[split_index] = *split_rec; else - ret = ocfs2_split_and_insert(inode, handle, path, di_bh, + ret = ocfs2_split_and_insert(inode, handle, path, et, &last_eb_bh, split_index, split_rec, meta_ac); if (ret) @@ -4412,7 +4532,7 @@ static int __ocfs2_mark_extent_written(struct inode *inode, } else { ret = ocfs2_try_to_merge_extent(inode, handle, path, split_index, split_rec, - dealloc, &ctxt); + dealloc, &ctxt, et); if (ret) mlog_errno(ret); } @@ -4430,16 +4550,18 @@ out: * * The caller is responsible for passing down meta_ac if we'll need it. */ -int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, +int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh, handle_t *handle, u32 cpos, u32 len, u32 phys, struct ocfs2_alloc_context *meta_ac, - struct ocfs2_cached_dealloc_ctxt *dealloc) + struct ocfs2_cached_dealloc_ctxt *dealloc, + enum ocfs2_extent_tree_type et_type) { int ret, index; u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); struct ocfs2_extent_rec split_rec; struct ocfs2_path *left_path = NULL; struct ocfs2_extent_list *el; + struct ocfs2_extent_tree *et = NULL; mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); @@ -4453,13 +4575,21 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, goto out; } + et = ocfs2_new_extent_tree(root_bh, et_type); + if (!et) { + ret = -ENOMEM; + mlog_errno(ret); + goto out; + } + /* * XXX: This should be fixed up so that we just re-insert the * next extent records. */ - ocfs2_extent_map_trunc(inode, 0); + if (et_type == OCFS2_DINODE_EXTENT) + ocfs2_extent_map_trunc(inode, 0); - left_path = ocfs2_new_inode_path(di_bh); + left_path = ocfs2_new_path(et->root_bh, et->root_el); if (!left_path) { ret = -ENOMEM; mlog_errno(ret); @@ -4490,23 +4620,25 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; - ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path, - index, &split_rec, meta_ac, dealloc); + ret = __ocfs2_mark_extent_written(inode, et, handle, left_path, + index, &split_rec, meta_ac, + dealloc); if (ret) mlog_errno(ret); out: ocfs2_free_path(left_path); + if (et) + ocfs2_free_extent_tree(et); return ret; } -static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, +static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et, handle_t *handle, struct ocfs2_path *path, int index, u32 new_range, struct ocfs2_alloc_context *meta_ac) { int ret, depth, credits = handle->h_buffer_credits; - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; struct buffer_head *last_eb_bh = NULL; struct ocfs2_extent_block *eb; struct ocfs2_extent_list *rightmost_el, *el; @@ -4524,7 +4656,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, depth = path->p_tree_depth; if (depth > 0) { ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), - le64_to_cpu(di->i_last_eb_blk), + ocfs2_get_last_eb_blk(et), &last_eb_bh, OCFS2_BH_CACHED, inode); if (ret < 0) { mlog_errno(ret); @@ -4537,7 +4669,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, rightmost_el = path_leaf_el(path); credits += path->p_tree_depth + - ocfs2_extend_meta_needed(&di->id2.i_list); + ocfs2_extend_meta_needed(et->root_el); ret = ocfs2_extend_trans(handle, credits); if (ret) { mlog_errno(ret); @@ -4546,7 +4678,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, if (le16_to_cpu(rightmost_el->l_next_free_rec) = le16_to_cpu(rightmost_el->l_count)) { - ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh, + ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh, meta_ac); if (ret) { mlog_errno(ret); @@ -4560,7 +4692,7 @@ static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, insert.ins_split = SPLIT_RIGHT; insert.ins_tree_depth = depth; - ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert); + ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); if (ret) mlog_errno(ret); @@ -4572,7 +4704,8 @@ out: static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, struct ocfs2_path *path, int index, struct ocfs2_cached_dealloc_ctxt *dealloc, - u32 cpos, u32 len) + u32 cpos, u32 len, + struct ocfs2_extent_tree *et) { int ret; u32 left_cpos, rec_range, trunc_range; @@ -4584,7 +4717,7 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, struct ocfs2_extent_block *eb; if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { - ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); + ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et); if (ret) { mlog_errno(ret); goto out; @@ -4715,7 +4848,7 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, ocfs2_journal_dirty(handle, path_leaf_bh(path)); - ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); + ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et); if (ret) { mlog_errno(ret); goto out; @@ -4726,20 +4859,29 @@ out: return ret; } -int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, +int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh, u32 cpos, u32 len, handle_t *handle, struct ocfs2_alloc_context *meta_ac, - struct ocfs2_cached_dealloc_ctxt *dealloc) + struct ocfs2_cached_dealloc_ctxt *dealloc, + enum ocfs2_extent_tree_type et_type) { int ret, index; u32 rec_range, trunc_range; struct ocfs2_extent_rec *rec; struct ocfs2_extent_list *el; - struct ocfs2_path *path; + struct ocfs2_path *path = NULL; + struct ocfs2_extent_tree *et = NULL; + + et = ocfs2_new_extent_tree(root_bh, et_type); + if (!et) { + ret = -ENOMEM; + mlog_errno(ret); + goto out; + } ocfs2_extent_map_trunc(inode, 0); - path = ocfs2_new_inode_path(di_bh); + path = ocfs2_new_path(et->root_bh, et->root_el); if (!path) { ret = -ENOMEM; mlog_errno(ret); @@ -4792,13 +4934,13 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, - cpos, len); + cpos, len, et); if (ret) { mlog_errno(ret); goto out; } } else { - ret = ocfs2_split_tree(inode, di_bh, handle, path, index, + ret = ocfs2_split_tree(inode, et, handle, path, index, trunc_range, meta_ac); if (ret) { mlog_errno(ret); @@ -4847,7 +4989,7 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, } ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, - cpos, len); + cpos, len, et); if (ret) { mlog_errno(ret); goto out; @@ -4856,6 +4998,8 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, out: ocfs2_free_path(path); + if (et) + ocfs2_free_extent_tree(et); return ret; } @@ -6364,7 +6508,8 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode, * the in-inode data from our pages. */ ret = ocfs2_insert_extent(osb, handle, inode, di_bh, - 0, block, 1, 0, NULL); + 0, block, 1, 0, + NULL, OCFS2_DINODE_EXTENT); if (ret) { mlog_errno(ret); goto out_commit; @@ -6406,13 +6551,14 @@ int ocfs2_commit_truncate(struct ocfs2_super *osb, handle_t *handle = NULL; struct inode *tl_inode = osb->osb_tl_inode; struct ocfs2_path *path = NULL; + struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; mlog_entry_void(); new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, i_size_read(inode)); - path = ocfs2_new_inode_path(fe_bh); + path = ocfs2_new_path(fe_bh, &di->id2.i_list); if (!path) { status = -ENOMEM; mlog_errno(status); diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h index 249e79e..5a460a9 100644 --- a/fs/ocfs2/alloc.h +++ b/fs/ocfs2/alloc.h @@ -26,28 +26,37 @@ #ifndef OCFS2_ALLOC_H #define OCFS2_ALLOC_H +enum ocfs2_extent_tree_type { + OCFS2_DINODE_EXTENT = 0, +}; + struct ocfs2_alloc_context; int ocfs2_insert_extent(struct ocfs2_super *osb, handle_t *handle, struct inode *inode, - struct buffer_head *fe_bh, + struct buffer_head *root_bh, u32 cpos, u64 start_blk, u32 new_clusters, u8 flags, - struct ocfs2_alloc_context *meta_ac); + struct ocfs2_alloc_context *meta_ac, + enum ocfs2_extent_tree_type et_type); struct ocfs2_cached_dealloc_ctxt; -int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, +int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh, handle_t *handle, u32 cpos, u32 len, u32 phys, struct ocfs2_alloc_context *meta_ac, - struct ocfs2_cached_dealloc_ctxt *dealloc); -int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, + struct ocfs2_cached_dealloc_ctxt *dealloc, + enum ocfs2_extent_tree_type et_type); +int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh, u32 cpos, u32 len, handle_t *handle, struct ocfs2_alloc_context *meta_ac, - struct ocfs2_cached_dealloc_ctxt *dealloc); + struct ocfs2_cached_dealloc_ctxt *dealloc, + enum ocfs2_extent_tree_type et_type); int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, - struct buffer_head *bh); + struct buffer_head *root_bh, + enum ocfs2_extent_tree_type et_type); + /* * how many new metadata chunks would an allocation need at maximum? * diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index f83a2a4..009dcc4 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1268,7 +1268,8 @@ static int ocfs2_write_cluster(struct address_space *mapping, } else if (unwritten) { ret = ocfs2_mark_extent_written(inode, wc->w_di_bh, wc->w_handle, cpos, 1, phys, - meta_ac, &wc->w_dealloc); + meta_ac, &wc->w_dealloc, + OCFS2_DINODE_EXTENT); if (ret < 0) { mlog_errno(ret); goto out; @@ -1702,7 +1703,13 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, * ocfs2_lock_allocators(). It greatly over-estimates * the work to be done. */ - ret = ocfs2_lock_allocators(inode, wc->w_di_bh, + mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u," + " clusters_to_add = %u, extents_to_split = %u\n", + (unsigned long long)OCFS2_I(inode)->ip_blkno, + (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, clusters_to_alloc, extents_to_split, &data_ac, &meta_ac); if (ret) { diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 5e8cd6d..8c10158 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1307,7 +1307,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, * related blocks have been journaled already. */ ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0, - NULL); + NULL, OCFS2_DINODE_EXTENT); if (ret) { mlog_errno(ret); goto out; @@ -1333,7 +1333,7 @@ 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_insert_extent(osb, handle, dir, di_bh, 1, blkno, - len, 0, NULL); + len, 0, NULL, OCFS2_DINODE_EXTENT); if (ret) { mlog_errno(ret); goto out; @@ -1476,7 +1476,8 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb, 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); + parent_fe_bh, + OCFS2_DINODE_EXTENT); if (num_free_extents < 0) { status = num_free_extents; mlog_errno(status); diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index 79d7da9..cc292ee 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -521,7 +521,8 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, if (mark_unwritten) flags = OCFS2_EXT_UNWRITTEN; - free_extents = ocfs2_num_free_extents(osb, inode, fe_bh); + free_extents = ocfs2_num_free_extents(osb, inode, fe_bh, + OCFS2_DINODE_EXTENT); if (free_extents < 0) { status = free_extents; mlog_errno(status); @@ -570,7 +571,7 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); status = ocfs2_insert_extent(osb, handle, inode, fe_bh, *logical_offset, block, num_bits, - flags, meta_ac); + flags, meta_ac, OCFS2_DINODE_EXTENT); if (status < 0) { mlog_errno(status); goto leave; @@ -599,92 +600,6 @@ leave: return status; } -/* - * For a given allocation, determine which allocators will need to be - * accessed, and lock them, reserving the appropriate number of bits. - * - * Sparse file systems call this from ocfs2_write_begin_nolock() - * and ocfs2_allocate_unwritten_extents(). - * - * File systems which don't support holes call this from - * ocfs2_extend_allocation(). - */ -int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *di_bh, - u32 clusters_to_add, u32 extents_to_split, - struct ocfs2_alloc_context **data_ac, - struct ocfs2_alloc_context **meta_ac) -{ - int ret = 0, num_free_extents; - unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; - struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); - struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; - - *meta_ac = NULL; - if (data_ac) - *data_ac = NULL; - - BUG_ON(clusters_to_add != 0 && data_ac == NULL); - - mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " - "clusters_to_add = %u, extents_to_split = %u\n", - (unsigned long long)OCFS2_I(inode)->ip_blkno, (long long)i_size_read(inode), - le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split); - - num_free_extents = ocfs2_num_free_extents(osb, inode, di_bh); - if (num_free_extents < 0) { - ret = num_free_extents; - mlog_errno(ret); - goto out; - } - - /* - * Sparse allocation file systems need to be more conservative - * with reserving room for expansion - the actual allocation - * happens while we've got a journal handle open so re-taking - * a cluster lock (because we ran out of room for another - * extent) will violate ordering rules. - * - * Most of the time we'll only be seeing this 1 cluster at a time - * anyway. - * - * Always lock for any unwritten extents - we might want to - * add blocks during a split. - */ - if (!num_free_extents || - (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { - ret = ocfs2_reserve_new_metadata(osb, &di->id2.i_list, meta_ac); - if (ret < 0) { - if (ret != -ENOSPC) - mlog_errno(ret); - goto out; - } - } - - if (clusters_to_add == 0) - goto out; - - ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); - if (ret < 0) { - if (ret != -ENOSPC) - mlog_errno(ret); - goto out; - } - -out: - if (ret) { - if (*meta_ac) { - ocfs2_free_alloc_context(*meta_ac); - *meta_ac = NULL; - } - - /* - * We cannot have an error and a non null *data_ac. - */ - } - - return ret; -} - static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, u32 clusters_to_add, int mark_unwritten) { @@ -725,7 +640,13 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, restart_all: BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); - status = ocfs2_lock_allocators(inode, bh, clusters_to_add, 0, &data_ac, + mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " + "clusters_to_add = %u\n", + (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); if (status) { mlog_errno(status); @@ -1397,7 +1318,8 @@ static int __ocfs2_remove_inode_range(struct inode *inode, struct ocfs2_alloc_context *meta_ac = NULL; struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; - ret = ocfs2_lock_allocators(inode, di_bh, 0, 1, NULL, &meta_ac); + ret = ocfs2_lock_allocators(inode, di_bh, &di->id2.i_list, + 0, 1, NULL, &meta_ac); if (ret) { mlog_errno(ret); return ret; @@ -1428,7 +1350,7 @@ static int __ocfs2_remove_inode_range(struct inode *inode, } ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, - dealloc); + dealloc, OCFS2_DINODE_EXTENT); if (ret) { mlog_errno(ret); goto out_commit; diff --git a/fs/ocfs2/file.h b/fs/ocfs2/file.h index e38ecb2..e090ff2 100644 --- a/fs/ocfs2/file.h +++ b/fs/ocfs2/file.h @@ -55,10 +55,6 @@ int ocfs2_do_extend_allocation(struct ocfs2_super *osb, enum ocfs2_alloc_restarted *reason_ret); int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to); -int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *fe, - u32 clusters_to_add, u32 extents_to_split, - struct ocfs2_alloc_context **data_ac, - struct ocfs2_alloc_context **meta_ac); int ocfs2_setattr(struct dentry *dentry, struct iattr *attr); int ocfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat); diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c index af769a5..1992a6a 100644 --- a/fs/ocfs2/suballoc.c +++ b/fs/ocfs2/suballoc.c @@ -1891,3 +1891,85 @@ static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe) (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno); } } + +/* + * For a given allocation, determine which allocators will need to be + * accessed, and lock them, reserving the appropriate number of bits. + * + * Sparse file systems call this from ocfs2_write_begin_nolock() + * and ocfs2_allocate_unwritten_extents(). + * + * 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, + u32 clusters_to_add, u32 extents_to_split, + struct ocfs2_alloc_context **data_ac, + struct ocfs2_alloc_context **meta_ac) +{ + int ret = 0, num_free_extents; + unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + + *meta_ac = NULL; + if (data_ac) + *data_ac = NULL; + + BUG_ON(clusters_to_add != 0 && data_ac == NULL); + + num_free_extents = ocfs2_num_free_extents(osb, inode, root_bh, + OCFS2_DINODE_EXTENT); + if (num_free_extents < 0) { + ret = num_free_extents; + mlog_errno(ret); + goto out; + } + + /* + * Sparse allocation file systems need to be more conservative + * with reserving room for expansion - the actual allocation + * happens while we've got a journal handle open so re-taking + * a cluster lock (because we ran out of room for another + * extent) will violate ordering rules. + * + * Most of the time we'll only be seeing this 1 cluster at a time + * anyway. + * + * Always lock for any unwritten extents - we might want to + * add blocks during a split. + */ + if (!num_free_extents || + (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { + ret = ocfs2_reserve_new_metadata(osb, root_el, meta_ac); + if (ret < 0) { + if (ret != -ENOSPC) + mlog_errno(ret); + goto out; + } + } + + if (clusters_to_add == 0) + goto out; + + ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); + if (ret < 0) { + if (ret != -ENOSPC) + mlog_errno(ret); + goto out; + } + +out: + if (ret) { + if (*meta_ac) { + ocfs2_free_alloc_context(*meta_ac); + *meta_ac = NULL; + } + + /* + * We cannot have an error and a non null *data_ac. + */ + } + + return ret; +} diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h index d024c69..df19591 100644 --- a/fs/ocfs2/suballoc.h +++ b/fs/ocfs2/suballoc.h @@ -161,4 +161,9 @@ 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, + u32 clusters_to_add, u32 extents_to_split, + struct ocfs2_alloc_context **data_ac, + struct ocfs2_alloc_context **meta_ac); #endif /* _CHAINALLOC_H_ */ -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:00 UTC
[Ocfs2-devel] [PATCH 04/15] Make extend allocation generic.v2
Modification from V1 to V2: 1. Make the function name more meaningful. 2. Remove the wrong modification of "u32" in the function ocfs2_add_clusters_in_btree. The old ocfs2_do_extend_allocation is restrictly to be used in file extension. Now a new function named ocfs2_do_cluster_allocation will handle the issue of generic extend allocation and it is created in suballoc.c. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/aops.c | 8 ++-- fs/ocfs2/dir.c | 6 +- fs/ocfs2/file.c | 136 ++++++++++----------------------------------------- fs/ocfs2/file.h | 26 ++++------ fs/ocfs2/namei.c | 8 ++-- fs/ocfs2/suballoc.c | 110 +++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/suballoc.h | 18 +++++++ 7 files changed, 177 insertions(+), 135 deletions(-) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 009dcc4..c347d9c 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1245,10 +1245,10 @@ static int ocfs2_write_cluster(struct address_space *mapping, * any additional semaphores or cluster locks. */ tmp_pos = cpos; - ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode, - &tmp_pos, 1, 0, wc->w_di_bh, - wc->w_handle, data_ac, - meta_ac, NULL); + ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode, + &tmp_pos, 1, 0, wc->w_di_bh, + wc->w_handle, data_ac, + meta_ac, NULL); /* * This shouldn't happen because we must have already * calculated the correct meta data allocation required. The diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 8c10158..1245a55 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1378,9 +1378,9 @@ static int ocfs2_do_extend_dir(struct super_block *sb, if (extend) { u32 offset = OCFS2_I(dir)->ip_clusters; - status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset, - 1, 0, parent_fe_bh, handle, - data_ac, meta_ac, NULL); + status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, + 1, 0, parent_fe_bh, handle, + data_ac, meta_ac, NULL); BUG_ON(status == -EAGAIN); if (status < 0) { mlog_errno(status); diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index cc292ee..c5edae0 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -488,7 +488,7 @@ bail: } /* - * extend allocation only here. + * extend file allocation only here. * we'll update all the disk stuff, and oip->alloc_size * * expect stuff to be locked, a transaction started and enough data / @@ -497,107 +497,25 @@ bail: * Will return -EAGAIN, and a reason if a restart is needed. * If passed in, *reason will always be set, even in error. */ -int ocfs2_do_extend_allocation(struct ocfs2_super *osb, - struct inode *inode, - u32 *logical_offset, - u32 clusters_to_add, - int mark_unwritten, - struct buffer_head *fe_bh, - handle_t *handle, - struct ocfs2_alloc_context *data_ac, - struct ocfs2_alloc_context *meta_ac, - enum ocfs2_alloc_restarted *reason_ret) +int ocfs2_add_inode_data(struct ocfs2_super *osb, + struct inode *inode, + u32 *logical_offset, + u32 clusters_to_add, + int mark_unwritten, + struct buffer_head *fe_bh, + handle_t *handle, + struct ocfs2_alloc_context *data_ac, + struct ocfs2_alloc_context *meta_ac, + enum ocfs2_alloc_restarted *reason_ret) { - int status = 0; - int free_extents; struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; - enum ocfs2_alloc_restarted reason = RESTART_NONE; - u32 bit_off, num_bits; - u64 block; - u8 flags = 0; - - BUG_ON(!clusters_to_add); - - if (mark_unwritten) - flags = OCFS2_EXT_UNWRITTEN; - - free_extents = ocfs2_num_free_extents(osb, inode, fe_bh, - OCFS2_DINODE_EXTENT); - if (free_extents < 0) { - status = free_extents; - mlog_errno(status); - goto leave; - } - - /* there are two cases which could cause us to EAGAIN in the - * we-need-more-metadata case: - * 1) we haven't reserved *any* - * 2) we are so fragmented, we've needed to add metadata too - * many times. */ - if (!free_extents && !meta_ac) { - mlog(0, "we haven't reserved any metadata!\n"); - status = -EAGAIN; - reason = RESTART_META; - goto leave; - } else if ((!free_extents) - && (ocfs2_alloc_context_bits_left(meta_ac) - < ocfs2_extend_meta_needed(&fe->id2.i_list))) { - mlog(0, "filesystem is really fragmented...\n"); - status = -EAGAIN; - reason = RESTART_META; - goto leave; - } + struct ocfs2_extent_list *el = &fe->id2.i_list; - status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, - clusters_to_add, &bit_off, &num_bits); - if (status < 0) { - if (status != -ENOSPC) - mlog_errno(status); - goto leave; - } - - BUG_ON(num_bits > clusters_to_add); - - /* reserve our write early -- insert_extent may update the inode */ - status = ocfs2_journal_access(handle, inode, fe_bh, - OCFS2_JOURNAL_ACCESS_WRITE); - if (status < 0) { - mlog_errno(status); - goto leave; - } - - block = ocfs2_clusters_to_blocks(osb->sb, bit_off); - mlog(0, "Allocating %u clusters at block %u for inode %llu\n", - num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); - status = ocfs2_insert_extent(osb, handle, inode, fe_bh, - *logical_offset, block, num_bits, - flags, meta_ac, OCFS2_DINODE_EXTENT); - if (status < 0) { - mlog_errno(status); - goto leave; - } - - status = ocfs2_journal_dirty(handle, fe_bh); - if (status < 0) { - mlog_errno(status); - goto leave; - } - - clusters_to_add -= num_bits; - *logical_offset += num_bits; - - if (clusters_to_add) { - mlog(0, "need to alloc once more, clusters = %u, wanted = " - "%u\n", fe->i_clusters, clusters_to_add); - status = -EAGAIN; - reason = RESTART_TRANS; - } - -leave: - mlog_exit(status); - if (reason_ret) - *reason_ret = reason; - return status; + return 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); } static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, @@ -676,16 +594,16 @@ restarted_transaction: prev_clusters = OCFS2_I(inode)->ip_clusters; - status = ocfs2_do_extend_allocation(osb, - inode, - &logical_start, - clusters_to_add, - mark_unwritten, - bh, - handle, - data_ac, - meta_ac, - &why); + status = ocfs2_add_inode_data(osb, + inode, + &logical_start, + clusters_to_add, + mark_unwritten, + bh, + handle, + data_ac, + meta_ac, + &why); if ((status < 0) && (status != -EAGAIN)) { if (status != -ENOSPC) mlog_errno(status); diff --git a/fs/ocfs2/file.h b/fs/ocfs2/file.h index e090ff2..8eca242 100644 --- a/fs/ocfs2/file.h +++ b/fs/ocfs2/file.h @@ -31,6 +31,7 @@ extern const struct file_operations ocfs2_dops; extern const struct inode_operations ocfs2_file_iops; extern const struct inode_operations ocfs2_special_file_iops; struct ocfs2_alloc_context; +enum ocfs2_alloc_restarted; struct ocfs2_file_private { struct file *fp_file; @@ -38,21 +39,16 @@ struct ocfs2_file_private { struct ocfs2_lock_res fp_flock; }; -enum ocfs2_alloc_restarted { - RESTART_NONE = 0, - RESTART_TRANS, - RESTART_META -}; -int ocfs2_do_extend_allocation(struct ocfs2_super *osb, - struct inode *inode, - u32 *logical_offset, - u32 clusters_to_add, - int mark_unwritten, - struct buffer_head *fe_bh, - handle_t *handle, - struct ocfs2_alloc_context *data_ac, - struct ocfs2_alloc_context *meta_ac, - enum ocfs2_alloc_restarted *reason_ret); +int ocfs2_add_inode_data(struct ocfs2_super *osb, + struct inode *inode, + u32 *logical_offset, + u32 clusters_to_add, + int mark_unwritten, + struct buffer_head *fe_bh, + handle_t *handle, + struct ocfs2_alloc_context *data_ac, + struct ocfs2_alloc_context *meta_ac, + enum ocfs2_alloc_restarted *reason_ret); int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to); int ocfs2_setattr(struct dentry *dentry, struct iattr *attr); diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c index d5d808f..2cd6f50 100644 --- a/fs/ocfs2/namei.c +++ b/fs/ocfs2/namei.c @@ -1598,10 +1598,10 @@ static int ocfs2_symlink(struct inode *dir, u32 offset = 0; inode->i_op = &ocfs2_symlink_inode_operations; - status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0, - new_fe_bh, - handle, data_ac, NULL, - NULL); + status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0, + new_fe_bh, + handle, data_ac, NULL, + NULL); if (status < 0) { if (status != -ENOSPC && status != -EINTR) { mlog(ML_ERROR, diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c index 1992a6a..d82a9a0 100644 --- a/fs/ocfs2/suballoc.c +++ b/fs/ocfs2/suballoc.c @@ -1973,3 +1973,113 @@ out: return ret; } + +/* + * Allcate and add clusters into the extent b-tree. + * The new clusters(clusters_to_add) will be inserted at logical_offset. + * The extent b-tree's root is root_el and it should be in root_bh, and + * it is not limited to the file storage. Any extent tree can use this + * function if it implements the proper ocfs2_extent_tree. + */ +int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, + struct inode *inode, + u32 *logical_offset, + u32 clusters_to_add, + int mark_unwritten, + struct buffer_head *root_bh, + struct ocfs2_extent_list *root_el, + 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) +{ + int status = 0; + int free_extents; + enum ocfs2_alloc_restarted reason = RESTART_NONE; + u32 bit_off, num_bits; + u64 block; + u8 flags = 0; + + BUG_ON(!clusters_to_add); + + if (mark_unwritten) + flags = OCFS2_EXT_UNWRITTEN; + + free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type); + if (free_extents < 0) { + status = free_extents; + mlog_errno(status); + goto leave; + } + + /* there are two cases which could cause us to EAGAIN in the + * we-need-more-metadata case: + * 1) we haven't reserved *any* + * 2) we are so fragmented, we've needed to add metadata too + * many times. */ + if (!free_extents && !meta_ac) { + mlog(0, "we haven't reserved any metadata!\n"); + status = -EAGAIN; + reason = RESTART_META; + goto leave; + } else if ((!free_extents) + && (ocfs2_alloc_context_bits_left(meta_ac) + < ocfs2_extend_meta_needed(root_el))) { + mlog(0, "filesystem is really fragmented...\n"); + status = -EAGAIN; + reason = RESTART_META; + goto leave; + } + + status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, + clusters_to_add, &bit_off, &num_bits); + if (status < 0) { + if (status != -ENOSPC) + mlog_errno(status); + goto leave; + } + + BUG_ON(num_bits > clusters_to_add); + + /* reserve our write early -- insert_extent may update the inode */ + status = ocfs2_journal_access(handle, inode, root_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (status < 0) { + mlog_errno(status); + goto leave; + } + + block = ocfs2_clusters_to_blocks(osb->sb, bit_off); + mlog(0, "Allocating %u clusters at block %u for inode %llu\n", + num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); + status = ocfs2_insert_extent(osb, handle, inode, root_bh, + *logical_offset, block, num_bits, + flags, meta_ac, type); + if (status < 0) { + mlog_errno(status); + goto leave; + } + + status = ocfs2_journal_dirty(handle, root_bh); + if (status < 0) { + mlog_errno(status); + goto leave; + } + + clusters_to_add -= num_bits; + *logical_offset += num_bits; + + if (clusters_to_add) { + mlog(0, "need to alloc once more, wanted = %u\n", + clusters_to_add); + status = -EAGAIN; + reason = RESTART_TRANS; + } + +leave: + mlog_exit(status); + if (reason_ret) + *reason_ret = reason; + return status; +} diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h index df19591..19ea422 100644 --- a/fs/ocfs2/suballoc.h +++ b/fs/ocfs2/suballoc.h @@ -166,4 +166,22 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh, u32 clusters_to_add, u32 extents_to_split, struct ocfs2_alloc_context **data_ac, struct ocfs2_alloc_context **meta_ac); + +enum ocfs2_alloc_restarted { + RESTART_NONE = 0, + RESTART_TRANS, + RESTART_META +}; +int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, + struct inode *inode, + u32 *logical_offset, + u32 clusters_to_add, + int mark_unwritten, + struct buffer_head *root_bh, + struct ocfs2_extent_list *root_el, + 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); #endif /* _CHAINALLOC_H_ */ -- 1.5.4.GIT
Modification from V1 to V2: Add EA disk layout to ocfs2_fs.h. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/ocfs2_fs.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h index 52c4266..6bbeef4 100644 --- a/fs/ocfs2/ocfs2_fs.h +++ b/fs/ocfs2/ocfs2_fs.h @@ -64,6 +64,7 @@ #define OCFS2_INODE_SIGNATURE "INODE01" #define OCFS2_EXTENT_BLOCK_SIGNATURE "EXBLK01" #define OCFS2_GROUP_DESC_SIGNATURE "GROUP01" +#define OCFS2_XATTR_BLOCK_SIGNATURE "XATTR01" /* Compatibility flags */ #define OCFS2_HAS_COMPAT_FEATURE(sb,mask) \ @@ -712,6 +713,60 @@ struct ocfs2_group_desc /*40*/ __u8 bg_bitmap[0]; }; +/* + * On disk extended attribute structure for OCFS2 + * Include ocfs2_xattr_entry, ocfs2_xattr_header, ocfs2_xattr_value_root + * ocfs2_xattr_tree_root and ocfs2_xattr_block. + */ +struct ocfs2_xattr_entry { + __le32 xe_name_hash; + __le16 xe_name_offset; + __u8 xe_name_len; + __u8 xe_type : 7; + __u8 xe_local : 1; + __le64 xe_value_size; +}; + +struct ocfs2_xattr_header { + __le16 xh_count; + __le16 xh_reserved1; + __le32 xh_csum; + __le64 xh_reserved2; + struct ocfs2_xattr_entry xh_entries[0]; +}; + +struct ocfs2_xattr_value_root { +/*00*/ __le32 xr_clusters; + __le32 xr_reserved0; + __le64 xr_last_eb_blk; +/*10*/ struct ocfs2_extent_list xr_list; +}; + +struct ocfs2_xattr_tree_root { +/*00*/ __le32 xt_clusters; + __le32 xt_reserved0; + __le64 xt_last_eb_blk; +/*10*/ struct ocfs2_extent_list xt_list; +}; + +#define OCFS2_XATTR_INDEXED 0x1 + +struct ocfs2_xattr_block { +/*00*/ __u8 xb_signature[8]; + __le16 xb_suballoc_slot; + __le16 xb_suballoc_bit; + __le32 xb_fs_generation; +/*10*/ __le32 xb_csum; + __le16 xb_flags; + __le16 xb_reserved0; + __le64 xb_blkno; +/*20*/ __le64 xb_reserved1[2]; +/*30*/ union { + struct ocfs2_xattr_header xb_header; + struct ocfs2_xattr_tree_root xb_root; + } xb_attrs; +}; + #ifdef __KERNEL__ static inline int ocfs2_fast_symlink_chars(struct super_block *sb) { -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:01 UTC
[Ocfs2-devel] [PATCH 06/15] Add extent tree operation for xattr value.v2
In Mark's review, he said "wouldn't it make sense to have a couple high-level "ocfs2_foo_insert_extent" functions whcih build up anm ocfs2_extent_tree and then pass it down to the common ocfs2_insert_extent?". But in this patch, I still don't remove the "private" from the parameter. because there are too many functions use "private". So if we use an "ocfs2_extent_tree" in ocfs2_insert_extent, we should also modify ocfs2_lock_allocators, ocfs2_num_free_extents etc. They are spread widely in ocfs2 source code, and I don't want to let ocfs2_extent_tree known by every caller since it should be totally limited to the tree code itself. Mark, any suggestions here? Here is the original commit log: When storing xattr value which is too large, we will allocate some clusters for it and here ocfs2_extent_list and ocfs2_extent_rec will also be used. In order to re-use the b-tree operation code, a new parameter named "private" is added into ocfs2_extent_tree and it is used to indicate the root of ocfs2_exent_list. The reason is that we can't deduce the root from the buffer_head now. It may be in an inode, an ocfs2_xattr_block or even worse, in any place in an ocfs2_xattr_bucket. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/Makefile | 3 +- fs/ocfs2/alloc.c | 78 ++++++++++-- fs/ocfs2/alloc.h | 13 ++- fs/ocfs2/aops.c | 5 +- fs/ocfs2/cluster/masklog.c | 1 + fs/ocfs2/cluster/masklog.h | 1 + fs/ocfs2/dir.c | 8 +- fs/ocfs2/extent_map.c | 60 +++++++++ fs/ocfs2/extent_map.h | 3 + fs/ocfs2/file.c | 9 +- fs/ocfs2/suballoc.c | 13 ++- fs/ocfs2/suballoc.h | 6 +- fs/ocfs2/xattr.c | 301 ++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 469 insertions(+), 32 deletions(-) create mode 100644 fs/ocfs2/xattr.c diff --git a/fs/ocfs2/Makefile b/fs/ocfs2/Makefile index f6956de..af63980 100644 --- a/fs/ocfs2/Makefile +++ b/fs/ocfs2/Makefile @@ -34,7 +34,8 @@ ocfs2-objs := \ symlink.o \ sysfile.o \ uptodate.o \ - ver.o + ver.o \ + xattr.o \ ocfs2_stackglue-objs := stackglue.o ocfs2_stack_o2cb-objs := stack_o2cb.o diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index 90cefc5..3a06271 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -78,6 +78,7 @@ struct ocfs2_extent_tree { struct ocfs2_extent_tree_operations *eops; struct buffer_head *root_bh; struct ocfs2_extent_list *root_el; + void *private; }; static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, @@ -136,9 +137,50 @@ static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { .sanity_check = ocfs2_dinode_sanity_check, }; +static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, + u64 blkno) +{ + struct ocfs2_xattr_value_root *xv + (struct ocfs2_xattr_value_root *)et->private; + + xv->xr_last_eb_blk = cpu_to_le64(blkno); +} + +static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) +{ + struct ocfs2_xattr_value_root *xv + (struct ocfs2_xattr_value_root *) et->private; + + return le64_to_cpu(xv->xr_last_eb_blk); +} + +static void ocfs2_xattr_value_update_clusters(struct inode *inode, + struct ocfs2_extent_tree *et, + u32 clusters) +{ + struct ocfs2_xattr_value_root *xv + (struct ocfs2_xattr_value_root *)et->private; + + le32_add_cpu(&xv->xr_clusters, clusters); +} + +static int ocfs2_xattr_value_sanity_check(struct inode *inode, + struct ocfs2_extent_tree *et) +{ + return 0; +} + +static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = { + .set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, + .get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, + .update_clusters = ocfs2_xattr_value_update_clusters, + .sanity_check = ocfs2_xattr_value_sanity_check, +}; + static struct ocfs2_extent_tree* ocfs2_new_extent_tree(struct buffer_head *bh, - enum ocfs2_extent_tree_type et_type) + enum ocfs2_extent_tree_type et_type, + void *private) { struct ocfs2_extent_tree *et; @@ -149,12 +191,16 @@ static struct ocfs2_extent_tree* et->type = et_type; get_bh(bh); et->root_bh = bh; + et->private = private; - /* current we only support dinode extent. */ - BUG_ON(et->type != OCFS2_DINODE_EXTENT); if (et_type == OCFS2_DINODE_EXTENT) { et->root_el = &((struct ocfs2_dinode *)bh->b_data)->id2.i_list; et->eops = &ocfs2_dinode_et_ops; + } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) { + struct ocfs2_xattr_value_root *xv + (struct ocfs2_xattr_value_root *) private; + et->root_el = &xv->xr_list; + et->eops = &ocfs2_xattr_et_ops; } return et; @@ -495,7 +541,8 @@ struct ocfs2_merge_ctxt { int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, struct buffer_head *root_bh, - enum ocfs2_extent_tree_type type) + enum ocfs2_extent_tree_type type, + void *private) { int retval; struct ocfs2_extent_list *el = NULL; @@ -517,6 +564,12 @@ int ocfs2_num_free_extents(struct ocfs2_super *osb, if (fe->i_last_eb_blk) last_eb_blk = le64_to_cpu(fe->i_last_eb_blk); el = &fe->id2.i_list; + } else if (type == OCFS2_XATTR_VALUE_EXTENT) { + struct ocfs2_xattr_value_root *xv + (struct ocfs2_xattr_value_root *) private; + + last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk); + el = &xv->xr_list; } if (last_eb_blk) { @@ -4227,7 +4280,8 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, u32 new_clusters, u8 flags, struct ocfs2_alloc_context *meta_ac, - enum ocfs2_extent_tree_type et_type) + enum ocfs2_extent_tree_type et_type, + void *private) { int status; int uninitialized_var(free_records); @@ -4238,7 +4292,7 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL); - et = ocfs2_new_extent_tree(root_bh, et_type); + et = ocfs2_new_extent_tree(root_bh, et_type, private); if (!et) { status = -ENOMEM; mlog_errno(status); @@ -4554,7 +4608,8 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh, 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) + enum ocfs2_extent_tree_type et_type, + void *private) { int ret, index; u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); @@ -4575,7 +4630,7 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh, goto out; } - et = ocfs2_new_extent_tree(root_bh, et_type); + et = ocfs2_new_extent_tree(root_bh, et_type, private); if (!et) { ret = -ENOMEM; mlog_errno(ret); @@ -4863,7 +4918,8 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh, 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) + enum ocfs2_extent_tree_type et_type, + void *private) { int ret, index; u32 rec_range, trunc_range; @@ -4872,7 +4928,7 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh, struct ocfs2_path *path = NULL; struct ocfs2_extent_tree *et = NULL; - et = ocfs2_new_extent_tree(root_bh, et_type); + et = ocfs2_new_extent_tree(root_bh, et_type, private); if (!et) { ret = -ENOMEM; mlog_errno(ret); @@ -6509,7 +6565,7 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode, */ ret = ocfs2_insert_extent(osb, handle, inode, di_bh, 0, block, 1, 0, - NULL, OCFS2_DINODE_EXTENT); + NULL, OCFS2_DINODE_EXTENT, NULL); if (ret) { mlog_errno(ret); goto out_commit; diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h index 5a460a9..b50ace5 100644 --- a/fs/ocfs2/alloc.h +++ b/fs/ocfs2/alloc.h @@ -28,6 +28,7 @@ enum ocfs2_extent_tree_type { OCFS2_DINODE_EXTENT = 0, + OCFS2_XATTR_VALUE_EXTENT, }; struct ocfs2_alloc_context; @@ -40,22 +41,26 @@ int ocfs2_insert_extent(struct ocfs2_super *osb, u32 new_clusters, u8 flags, struct ocfs2_alloc_context *meta_ac, - enum ocfs2_extent_tree_type et_type); + enum ocfs2_extent_tree_type et_type, + void *private); struct ocfs2_cached_dealloc_ctxt; int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh, 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); + enum ocfs2_extent_tree_type et_type, + void *private); int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh, 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); + enum ocfs2_extent_tree_type et_type, + void *private); int ocfs2_num_free_extents(struct ocfs2_super *osb, struct inode *inode, struct buffer_head *root_bh, - enum ocfs2_extent_tree_type et_type); + enum ocfs2_extent_tree_type et_type, + void *private); /* * how many new metadata chunks would an allocation need at maximum? diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index c347d9c..168b64f 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1269,7 +1269,7 @@ static int ocfs2_write_cluster(struct address_space *mapping, ret = ocfs2_mark_extent_written(inode, wc->w_di_bh, wc->w_handle, cpos, 1, phys, meta_ac, &wc->w_dealloc, - OCFS2_DINODE_EXTENT); + OCFS2_DINODE_EXTENT, NULL); if (ret < 0) { mlog_errno(ret); goto out; @@ -1711,7 +1711,8 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, ret = ocfs2_lock_allocators(inode, wc->w_di_bh, &di->id2.i_list, clusters_to_alloc, extents_to_split, - &data_ac, &meta_ac); + &data_ac, &meta_ac, + OCFS2_DINODE_EXTENT, NULL); if (ret) { mlog_errno(ret); goto out; diff --git a/fs/ocfs2/cluster/masklog.c b/fs/ocfs2/cluster/masklog.c index 23c732f..d8a0cb9 100644 --- a/fs/ocfs2/cluster/masklog.c +++ b/fs/ocfs2/cluster/masklog.c @@ -109,6 +109,7 @@ static struct mlog_attribute mlog_attrs[MLOG_MAX_BITS] = { define_mask(CONN), define_mask(QUORUM), define_mask(EXPORT), + define_mask(XATTR), define_mask(ERROR), define_mask(NOTICE), define_mask(KTHREAD), diff --git a/fs/ocfs2/cluster/masklog.h b/fs/ocfs2/cluster/masklog.h index 597e064..57670c6 100644 --- a/fs/ocfs2/cluster/masklog.h +++ b/fs/ocfs2/cluster/masklog.h @@ -112,6 +112,7 @@ #define ML_CONN 0x0000000004000000ULL /* net connection management */ #define ML_QUORUM 0x0000000008000000ULL /* net connection quorum */ #define ML_EXPORT 0x0000000010000000ULL /* ocfs2 export operations */ +#define ML_XATTR 0x0000000020000000ULL /* ocfs2 extended attributes */ /* bits that are infrequently given and frequently matched in the high word */ #define ML_ERROR 0x0000000100000000ULL /* sent to KERN_ERR */ #define ML_NOTICE 0x0000000200000000ULL /* setn to KERN_NOTICE */ diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c index 1245a55..d9995d7 100644 --- a/fs/ocfs2/dir.c +++ b/fs/ocfs2/dir.c @@ -1307,7 +1307,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, * related blocks have been journaled already. */ ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0, - NULL, OCFS2_DINODE_EXTENT); + NULL, OCFS2_DINODE_EXTENT, NULL); if (ret) { mlog_errno(ret); goto out; @@ -1333,7 +1333,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_insert_extent(osb, handle, dir, di_bh, 1, blkno, - len, 0, NULL, OCFS2_DINODE_EXTENT); + len, 0, NULL, OCFS2_DINODE_EXTENT, + NULL); if (ret) { mlog_errno(ret); goto out; @@ -1477,7 +1478,8 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb, spin_unlock(&OCFS2_I(dir)->ip_lock); num_free_extents = ocfs2_num_free_extents(osb, dir, parent_fe_bh, - OCFS2_DINODE_EXTENT); + OCFS2_DINODE_EXTENT, + NULL); if (num_free_extents < 0) { status = num_free_extents; mlog_errno(status); diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c index c58668a..619b20a 100644 --- a/fs/ocfs2/extent_map.c +++ b/fs/ocfs2/extent_map.c @@ -373,6 +373,66 @@ out: return ret; } +int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster, + u32 *p_cluster, u32 *num_clusters, + struct ocfs2_extent_list *el) +{ + int ret = 0, i; + struct buffer_head *eb_bh = NULL; + struct ocfs2_extent_block *eb; + struct ocfs2_extent_rec *rec; + u32 coff; + + if (el->l_tree_depth) { + ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh); + if (ret) { + mlog_errno(ret); + goto out; + } + + eb = (struct ocfs2_extent_block *) eb_bh->b_data; + el = &eb->h_list; + + if (el->l_tree_depth) { + ocfs2_error(inode->i_sb, + "Inode %lu has non zero tree depth in " + "xattr leaf block %llu\n", inode->i_ino, + (unsigned long long)eb_bh->b_blocknr); + ret = -EROFS; + goto out; + } + } + + i = ocfs2_search_extent_list(el, v_cluster); + if (i == -1) { + ret = -EROFS; + mlog_errno(ret); + goto out; + } else { + rec = &el->l_recs[i]; + BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos)); + + if (!rec->e_blkno) { + ocfs2_error(inode->i_sb, "Inode %lu has bad extent " + "record (%u, %u, 0) in xattr", inode->i_ino, + le32_to_cpu(rec->e_cpos), + ocfs2_rec_clusters(el, rec)); + ret = -EROFS; + goto out; + } + coff = v_cluster - le32_to_cpu(rec->e_cpos); + *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb, + le64_to_cpu(rec->e_blkno)); + *p_cluster = *p_cluster + coff; + if (num_clusters) + *num_clusters = ocfs2_rec_clusters(el, rec) - coff; + } +out: + if (eb_bh) + brelse(eb_bh); + return ret; +} + int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, u32 *p_cluster, u32 *num_clusters, unsigned int *extent_flags) diff --git a/fs/ocfs2/extent_map.h b/fs/ocfs2/extent_map.h index de91e3e..d98444e 100644 --- a/fs/ocfs2/extent_map.h +++ b/fs/ocfs2/extent_map.h @@ -50,4 +50,7 @@ int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, u32 *p_cluster, int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno, u64 *ret_count, unsigned int *extent_flags); +int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster, + u32 *p_cluster, u32 *num_clusters, + struct ocfs2_extent_list *el); #endif /* _EXTENT_MAP_H */ diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index c5edae0..feeb8eb 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -515,7 +515,7 @@ int ocfs2_add_inode_data(struct ocfs2_super *osb, clusters_to_add, mark_unwritten, fe_bh, el, handle, data_ac, meta_ac, reason_ret, - OCFS2_DINODE_EXTENT); + OCFS2_DINODE_EXTENT, NULL); } static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, @@ -565,7 +565,7 @@ restart_all: clusters_to_add); status = ocfs2_lock_allocators(inode, bh, &fe->id2.i_list, clusters_to_add, 0, &data_ac, - &meta_ac); + &meta_ac, OCFS2_DINODE_EXTENT, NULL); if (status) { mlog_errno(status); goto leave; @@ -1237,7 +1237,8 @@ static int __ocfs2_remove_inode_range(struct inode *inode, struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; ret = ocfs2_lock_allocators(inode, di_bh, &di->id2.i_list, - 0, 1, NULL, &meta_ac); + 0, 1, NULL, &meta_ac, + OCFS2_DINODE_EXTENT, NULL); if (ret) { mlog_errno(ret); return ret; @@ -1268,7 +1269,7 @@ static int __ocfs2_remove_inode_range(struct inode *inode, } ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, - dealloc, OCFS2_DINODE_EXTENT); + dealloc, OCFS2_DINODE_EXTENT, NULL); if (ret) { mlog_errno(ret); goto out_commit; diff --git a/fs/ocfs2/suballoc.c b/fs/ocfs2/suballoc.c index d82a9a0..5aaafe3 100644 --- a/fs/ocfs2/suballoc.c +++ b/fs/ocfs2/suballoc.c @@ -1906,7 +1906,8 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh, struct ocfs2_extent_list *root_el, u32 clusters_to_add, u32 extents_to_split, struct ocfs2_alloc_context **data_ac, - struct ocfs2_alloc_context **meta_ac) + struct ocfs2_alloc_context **meta_ac, + enum ocfs2_extent_tree_type type, void *private) { int ret = 0, num_free_extents; unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; @@ -1919,7 +1920,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, - OCFS2_DINODE_EXTENT); + type, private); if (num_free_extents < 0) { ret = num_free_extents; mlog_errno(ret); @@ -1992,7 +1993,8 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, struct ocfs2_alloc_context *data_ac, struct ocfs2_alloc_context *meta_ac, enum ocfs2_alloc_restarted *reason_ret, - enum ocfs2_extent_tree_type type) + enum ocfs2_extent_tree_type type, + void *private) { int status = 0; int free_extents; @@ -2006,7 +2008,8 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, if (mark_unwritten) flags = OCFS2_EXT_UNWRITTEN; - free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type); + free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type, + private); if (free_extents < 0) { status = free_extents; mlog_errno(status); @@ -2055,7 +2058,7 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); status = ocfs2_insert_extent(osb, handle, inode, root_bh, *logical_offset, block, num_bits, - flags, meta_ac, type); + flags, meta_ac, type, private); if (status < 0) { mlog_errno(status); goto leave; diff --git a/fs/ocfs2/suballoc.h b/fs/ocfs2/suballoc.h index 19ea422..8f5ccca 100644 --- a/fs/ocfs2/suballoc.h +++ b/fs/ocfs2/suballoc.h @@ -165,7 +165,8 @@ int ocfs2_lock_allocators(struct inode *inode, struct buffer_head *root_bh, struct ocfs2_extent_list *root_el, u32 clusters_to_add, u32 extents_to_split, struct ocfs2_alloc_context **data_ac, - struct ocfs2_alloc_context **meta_ac); + struct ocfs2_alloc_context **meta_ac, + enum ocfs2_extent_tree_type type, void *private); enum ocfs2_alloc_restarted { RESTART_NONE = 0, @@ -183,5 +184,6 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, struct ocfs2_alloc_context *data_ac, struct ocfs2_alloc_context *meta_ac, enum ocfs2_alloc_restarted *reason_ret, - enum ocfs2_extent_tree_type type); + enum ocfs2_extent_tree_type type, + void *private); #endif /* _CHAINALLOC_H_ */ diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c new file mode 100644 index 0000000..acdce03 --- /dev/null +++ b/fs/ocfs2/xattr.c @@ -0,0 +1,301 @@ +/* -*- mode: c; c-basic-offset: 8; -*- + * vim: noexpandtab sw=8 ts=8 sts=0: + * + * xattr.c + * + * Copyright (C) 2008 Oracle. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#define MLOG_MASK_PREFIX ML_XATTR +#include <cluster/masklog.h> + +#include "ocfs2.h" +#include "alloc.h" +#include "dlmglue.h" +#include "file.h" +#include "inode.h" +#include "journal.h" +#include "ocfs2_fs.h" +#include "suballoc.h" +#include "uptodate.h" +#include "buffer_head_io.h" + +static int ocfs2_xattr_extend_allocation(struct inode *inode, + u32 clusters_to_add, + struct buffer_head *xattr_bh, + struct ocfs2_xattr_value_root *xv) +{ + int status = 0; + int restart_func = 0; + int credits = 0; + handle_t *handle = NULL; + struct ocfs2_alloc_context *data_ac = NULL; + struct ocfs2_alloc_context *meta_ac = NULL; + enum ocfs2_alloc_restarted why; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_extent_list *root_el = &xv->xr_list; + u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters); + + mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add); + +restart_all: + + status = ocfs2_lock_allocators(inode, xattr_bh, root_el, + clusters_to_add, 0, &data_ac, + &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv); + if (status) { + mlog_errno(status); + goto leave; + } + + credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add); + handle = ocfs2_start_trans(osb, credits); + if (IS_ERR(handle)) { + status = PTR_ERR(handle); + handle = NULL; + mlog_errno(status); + goto leave; + } + +restarted_transaction: + status = ocfs2_journal_access(handle, inode, xattr_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (status < 0) { + mlog_errno(status); + goto leave; + } + + prev_clusters = le32_to_cpu(xv->xr_clusters); + status = ocfs2_add_clusters_in_btree(osb, + inode, + &logical_start, + clusters_to_add, + 0, + xattr_bh, + root_el, + handle, + data_ac, + meta_ac, + &why, + OCFS2_XATTR_VALUE_EXTENT, + xv); + if ((status < 0) && (status != -EAGAIN)) { + if (status != -ENOSPC) + mlog_errno(status); + goto leave; + } + + status = ocfs2_journal_dirty(handle, xattr_bh); + if (status < 0) { + mlog_errno(status); + goto leave; + } + + clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters; + + if (why != RESTART_NONE && clusters_to_add) { + if (why == RESTART_META) { + mlog(0, "restarting function.\n"); + restart_func = 1; + } else { + BUG_ON(why != RESTART_TRANS); + + mlog(0, "restarting transaction.\n"); + /* TODO: This can be more intelligent. */ + credits = ocfs2_calc_extend_credits(osb->sb, + root_el, + clusters_to_add); + status = ocfs2_extend_trans(handle, credits); + if (status < 0) { + /* handle still has to be committed at + * this point. */ + status = -ENOMEM; + mlog_errno(status); + goto leave; + } + goto restarted_transaction; + } + } + +leave: + if (handle) { + ocfs2_commit_trans(osb, handle); + handle = NULL; + } + if (data_ac) { + ocfs2_free_alloc_context(data_ac); + data_ac = NULL; + } + if (meta_ac) { + ocfs2_free_alloc_context(meta_ac); + meta_ac = NULL; + } + if ((!status) && restart_func) { + restart_func = 0; + goto restart_all; + } + + return status; +} + +static int __ocfs2_remove_xattr_range(struct inode *inode, + struct buffer_head *root_bh, + struct ocfs2_xattr_value_root *xv, + u32 cpos, u32 phys_cpos, u32 len, + struct ocfs2_cached_dealloc_ctxt *dealloc) +{ + int ret; + u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct inode *tl_inode = osb->osb_tl_inode; + handle_t *handle; + struct ocfs2_alloc_context *meta_ac = NULL; + + ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list, + 0, 1, NULL, &meta_ac, + OCFS2_XATTR_VALUE_EXTENT, xv); + if (ret) { + mlog_errno(ret); + return ret; + } + + mutex_lock(&tl_inode->i_mutex); + + if (ocfs2_truncate_log_needs_flush(osb)) { + ret = __ocfs2_flush_truncate_log(osb); + if (ret < 0) { + mlog_errno(ret); + goto out; + } + } + + handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, root_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac, + dealloc, OCFS2_XATTR_VALUE_EXTENT, xv); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + le32_add_cpu(&xv->xr_clusters, -len); + + ret = ocfs2_journal_dirty(handle, root_bh); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); + if (ret) + mlog_errno(ret); + +out_commit: + ocfs2_commit_trans(osb, handle); +out: + mutex_unlock(&tl_inode->i_mutex); + + if (meta_ac) + ocfs2_free_alloc_context(meta_ac); + + return ret; +} + +static int ocfs2_xattr_shrink_size(struct inode *inode, + u32 old_clusters, + u32 new_clusters, + struct buffer_head *root_bh, + struct ocfs2_xattr_value_root *xv) +{ + int ret = 0; + u32 trunc_len, cpos, phys_cpos, alloc_size; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_cached_dealloc_ctxt dealloc; + + ocfs2_init_dealloc_ctxt(&dealloc); + + if (old_clusters <= new_clusters) + return 0; + + cpos = new_clusters; + trunc_len = old_clusters - new_clusters; + while (trunc_len) { + ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos, + &alloc_size, &xv->xr_list); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (alloc_size > trunc_len) + alloc_size = trunc_len; + + ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos, + phys_cpos, alloc_size, + &dealloc); + if (ret) { + mlog_errno(ret); + goto out; + } + + cpos += alloc_size; + trunc_len -= alloc_size; + } + +out: + ocfs2_schedule_truncate_log_flush(osb, 1); + ocfs2_run_deallocs(osb, &dealloc); + + return ret; +} + +static int ocfs2_xattr_value_truncate(struct inode *inode, + struct buffer_head *root_bh, + struct ocfs2_xattr_value_root *xv, + int len) +{ + int ret; + u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len); + u32 old_clusters = le32_to_cpu(xv->xr_clusters); + + if (new_clusters == old_clusters) + return 0; + + if (new_clusters > old_clusters) + ret = ocfs2_xattr_extend_allocation(inode, + new_clusters - old_clusters, + root_bh, xv); + else + ret = ocfs2_xattr_shrink_size(inode, + old_clusters, new_clusters, + root_bh, xv); + + return ret; +} -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:01 UTC
[Ocfs2-devel] [PATCH 09/15] Add helper function in uptodate for removing xattr clusters.v2
The old uptodate only handles the issue of removing one buffer_head from ocfs2 inode's buffer cache. With xattr clusters, we may need to remove some clusters from the cache. So add helper function for it. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/uptodate.c | 32 ++++++++++++++++++++++++++------ fs/ocfs2/uptodate.h | 3 +++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/fs/ocfs2/uptodate.c b/fs/ocfs2/uptodate.c index 4da8851..7345d1c 100644 --- a/fs/ocfs2/uptodate.c +++ b/fs/ocfs2/uptodate.c @@ -511,14 +511,10 @@ static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, ci->ci_num_cached--; } -/* Called when we remove a chunk of metadata from an inode. We don't - * bother reverting things to an inlined array in the case of a remove - * which moves us back under the limit. */ -void ocfs2_remove_from_cache(struct inode *inode, - struct buffer_head *bh) +void ocfs2_remove_block_from_cache(struct inode *inode, + sector_t block) { int index; - sector_t block = bh->b_blocknr; struct ocfs2_meta_cache_item *item = NULL; struct ocfs2_inode_info *oi = OCFS2_I(inode); struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; @@ -544,6 +540,30 @@ void ocfs2_remove_from_cache(struct inode *inode, kmem_cache_free(ocfs2_uptodate_cachep, item); } +/* + * Called when we remove a chunk of metadata from an inode. We don't + * bother reverting things to an inlined array in the case of a remove + * which moves us back under the limit. + */ +void ocfs2_remove_from_cache(struct inode *inode, + struct buffer_head *bh) +{ + sector_t block = bh->b_blocknr; + + return ocfs2_remove_block_from_cache(inode, block); +} + +/* Called when we remove xattr clusters from an inode. */ +void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode, + sector_t block, + u32 c_len) +{ + u64 i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len; + + for (i = 0; i < b_len; i++, block++) + ocfs2_remove_block_from_cache(inode, block); +} + int __init init_ocfs2_uptodate_cache(void) { ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate", diff --git a/fs/ocfs2/uptodate.h b/fs/ocfs2/uptodate.h index 2e73206..531b4b3 100644 --- a/fs/ocfs2/uptodate.h +++ b/fs/ocfs2/uptodate.h @@ -40,6 +40,9 @@ void ocfs2_set_new_buffer_uptodate(struct inode *inode, struct buffer_head *bh); void ocfs2_remove_from_cache(struct inode *inode, struct buffer_head *bh); +void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode, + sector_t block, + u32 c_len); int ocfs2_buffer_read_ahead(struct inode *inode, struct buffer_head *bh); -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:02 UTC
[Ocfs2-devel] [PATCH 10/15] Add xattr tree operations in ocfs2_extent_tree.v2
ocfs2_xattr_block will use ocfs2_extent_list to store large numbers of EAs. So add a new type in ocfs2_extent_tree_type and add one new ocfs_extent_tree implementation so that it can use b-tree code to handle the storage of many EAs. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/alloc.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/alloc.h | 1 + 2 files changed, 54 insertions(+), 0 deletions(-) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index 2df5a7f..d93b7da 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -177,6 +177,48 @@ static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = { .sanity_check = ocfs2_xattr_value_sanity_check, }; +static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, + u64 blkno) +{ + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *) et->root_bh->b_data; + struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; + + xt->xt_last_eb_blk = cpu_to_le64(blkno); +} + +static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) +{ + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *) et->root_bh->b_data; + struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; + + return le64_to_cpu(xt->xt_last_eb_blk); +} + +static void ocfs2_xattr_tree_update_clusters(struct inode *inode, + struct ocfs2_extent_tree *et, + u32 clusters) +{ + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)et->root_bh->b_data; + + le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); +} + +static int ocfs2_xattr_tree_sanity_check(struct inode *inode, + struct ocfs2_extent_tree *et) +{ + return 0; +} + +static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { + .set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, + .get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, + .update_clusters = ocfs2_xattr_tree_update_clusters, + .sanity_check = ocfs2_xattr_tree_sanity_check, +}; + static struct ocfs2_extent_tree* ocfs2_new_extent_tree(struct buffer_head *bh, enum ocfs2_extent_tree_type et_type, @@ -201,6 +243,11 @@ static struct ocfs2_extent_tree* (struct ocfs2_xattr_value_root *) private; et->root_el = &xv->xr_list; et->eops = &ocfs2_xattr_et_ops; + } else if (et_type == OCFS2_XATTR_TREE_EXTENT) { + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)bh->b_data; + et->root_el = &xb->xb_attrs.xb_root.xt_list; + et->eops = &ocfs2_xattr_tree_et_ops; } return et; @@ -570,6 +617,12 @@ int ocfs2_num_free_extents(struct ocfs2_super *osb, last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk); el = &xv->xr_list; + } else if (type == OCFS2_XATTR_TREE_EXTENT) { + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)root_bh->b_data; + + last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk); + el = &xb->xb_attrs.xb_root.xt_list; } if (last_eb_blk) { diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h index b50ace5..7587f0e 100644 --- a/fs/ocfs2/alloc.h +++ b/fs/ocfs2/alloc.h @@ -29,6 +29,7 @@ enum ocfs2_extent_tree_type { OCFS2_DINODE_EXTENT = 0, OCFS2_XATTR_VALUE_EXTENT, + OCFS2_XATTR_TREE_EXTENT, }; struct ocfs2_alloc_context; -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:02 UTC
[Ocfs2-devel] [PATCH 11/15] Add xattr bucket iteration for large numbers of EAs.v2
We use bucket in ocfs2 to store large numbers of EAs, and list xattrs will iterate all the buckets and list all the names one by one. This patch add the iteration for the xattr bucket. For how xattr bucket looks like and their disk layout, please see http://oss.oracle.com/osswiki/OCFS2/DesignDocs/IndexedEATrees. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/ocfs2_fs.h | 30 ++++++- fs/ocfs2/xattr.c | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++- fs/ocfs2/xattr.h | 9 ++ 3 files changed, 276 insertions(+), 5 deletions(-) diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h index b64c160..a4ebee3 100644 --- a/fs/ocfs2/ocfs2_fs.h +++ b/fs/ocfs2/ocfs2_fs.h @@ -732,7 +732,9 @@ struct ocfs2_xattr_header { __le16 xh_count; __le16 xh_reserved1; __le32 xh_csum; - __le64 xh_reserved2; + __le16 xh_offset; + __le16 xh_name_value_len; + __le32 xh_reserved2; struct ocfs2_xattr_entry xh_entries[0]; }; @@ -768,6 +770,10 @@ struct ocfs2_xattr_block { } xb_attrs; }; +#define OCFS2_XATTR_BUCKET_SIZE 4096 +#define OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET (OCFS2_XATTR_BUCKET_SIZE \ + / OCFS2_MIN_BLOCKSIZE) + #ifdef __KERNEL__ static inline int ocfs2_fast_symlink_chars(struct super_block *sb) { @@ -885,6 +891,17 @@ static inline u64 ocfs2_backup_super_blkno(struct super_block *sb, int index) return 0; } + +static inline u16 ocfs2_xattr_recs_per_xb(struct super_block *sb) +{ + int size; + + size = sb->s_blocksize - + offsetof(struct ocfs2_xattr_block, + xb_attrs.xb_root.xt_list.l_recs); + + return size / sizeof(struct ocfs2_extent_rec); +} #else static inline int ocfs2_fast_symlink_chars(int blocksize) { @@ -968,6 +985,17 @@ static inline uint64_t ocfs2_backup_super_blkno(int blocksize, int index) return 0; } + +static inline int ocfs2_xattr_recs_per_xb(int blocksize) +{ + int size; + + size = blocksize - + offsetof(struct ocfs2_xattr_block, + xb_attrs.xb_root.xt_list.l_recs); + + return size / sizeof(struct ocfs2_extent_rec); +} #endif /* __KERNEL__ */ diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 38b636c..9be7bc8 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -51,6 +51,7 @@ #include "suballoc.h" #include "uptodate.h" #include "buffer_head_io.h" +#include "super.h" #include "xattr.h" @@ -118,6 +119,11 @@ struct ocfs2_xattr_search { int not_found; }; +static int ocfs2_xattr_tree_list_index_block(struct inode *inode, + struct ocfs2_xattr_tree_root *xt, + char *buffer, + size_t buffer_size); + static inline struct xattr_handler *ocfs2_xattr_handler(int name_index) { struct xattr_handler *handler = NULL; @@ -505,7 +511,7 @@ static int ocfs2_xattr_block_list(struct inode *inode, size_t buffer_size) { struct buffer_head *blk_bh = NULL; - struct ocfs2_xattr_header *header = NULL; + struct ocfs2_xattr_block *xb; int ret = 0; if (!di->i_xattr_loc) @@ -525,10 +531,17 @@ static int ocfs2_xattr_block_list(struct inode *inode, goto cleanup; } - header = &((struct ocfs2_xattr_block *)blk_bh->b_data)-> - xb_attrs.xb_header; + xb = (struct ocfs2_xattr_block *)blk_bh->b_data; - ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size); + if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { + struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; + ret = ocfs2_xattr_list_entries(inode, header, + buffer, buffer_size); + } else { + struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; + ret = ocfs2_xattr_tree_list_index_block(inode, xt, + buffer, buffer_size); + } cleanup: if (blk_bh) brelse(blk_bh); @@ -1880,3 +1893,224 @@ cleanup: return ret; } +/* + * Find the xattr extent rec which may contains name_hash. + * e_cpos will be the first name hash of the xattr rec. + * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. + */ +static int ocfs2_xattr_get_rec(struct inode *inode, + u32 name_hash, + u64 *p_blkno, + u32 *e_cpos, + u32 *num_clusters, + struct ocfs2_extent_list *el) +{ + int ret = 0, i; + struct buffer_head *eb_bh = NULL; + struct ocfs2_extent_block *eb; + struct ocfs2_extent_rec *rec = NULL; + u64 e_blkno = 0; + + if (el->l_tree_depth) { + ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh); + if (ret) { + mlog_errno(ret); + goto out; + } + + eb = (struct ocfs2_extent_block *) eb_bh->b_data; + el = &eb->h_list; + + if (el->l_tree_depth) { + ocfs2_error(inode->i_sb, + "Inode %lu has non zero tree depth in " + "xattr tree block %llu\n", inode->i_ino, + (unsigned long long)eb_bh->b_blocknr); + ret = -EROFS; + goto out; + } + } + + for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { + rec = &el->l_recs[i]; + + if (le32_to_cpu(rec->e_cpos) <= name_hash) { + e_blkno = le64_to_cpu(rec->e_blkno); + break; + } + } + + if (!e_blkno) { + ocfs2_error(inode->i_sb, "Inode %lu has bad extent " + "record (%u, %u, 0) in xattr", inode->i_ino, + le32_to_cpu(rec->e_cpos), + ocfs2_rec_clusters(el, rec)); + ret = -EROFS; + goto out; + } + + *p_blkno = le64_to_cpu(rec->e_blkno); + *num_clusters = le16_to_cpu(rec->e_leaf_clusters); + if (e_cpos) + *e_cpos = le32_to_cpu(rec->e_cpos); +out: + if (eb_bh) + brelse(eb_bh); + return ret; +} + +static int ocfs2_iterate_xattr_buckets(struct inode *inode, + u64 blkno, + u32 clusters, + int (*func)(struct inode *inode, + struct buffer_head *header_bh, + struct ocfs2_xattr_header *xh, + void *para), + void *para) +{ + int i, j, ret = 0, alloc_bucket = 0; + char *bucket = NULL, *buf; + struct ocfs2_xattr_header *xh; + int block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); + u32 bucket_num = clusters * bpc; + struct buffer_head **bhs = NULL; + int blocksize = inode->i_sb->s_blocksize; + + mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", + clusters, blkno); + + bhs = kcalloc(block_num, sizeof(struct buffer_head *), GFP_NOFS); + if (!bhs) + return -ENOMEM; + + if (block_num > 1) { + bucket = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!bucket) { + ret = -ENOMEM; + goto out; + } + alloc_bucket = 1; + } + + for (i = 0; i < bucket_num; i++, blkno += block_num) { + ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno, block_num, + bhs, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (block_num > 1) { + buf = bucket; + for (j = 0; j < block_num; j++, buf += blocksize) + memcpy(buf, bhs[j]->b_data, blocksize); + } else + bucket = bhs[0]->b_data; + + xh = (struct ocfs2_xattr_header *)bucket; + /* + * The real bucket num in this series of blocks is stored + * in the 1st bucket. + */ + if (i == 0) + bucket_num = le16_to_cpu(xh->xh_reserved1); + + mlog(0, "iterating xattr bucket %llu\n", blkno); + if (func) { + ret = func(inode, bhs[0], xh, para); + if (ret) { + mlog_errno(ret); + break; + } + } + + for (j = 0; j < block_num; j++) { + brelse(bhs[j]); + bhs[j] = NULL; + } + } + +out: + for (j = 0; j < block_num; j++) + brelse(bhs[j]); + kfree(bhs); + + if (alloc_bucket) + kfree(bucket); + + return ret; +} + +struct ocfs2_xattr_tree_list { + char *buffer; + size_t buffer_size; +}; + +static int ocfs2_list_xattr_bucket(struct inode *inode, + struct buffer_head *header_bh, + struct ocfs2_xattr_header *xh, + void *para) +{ + int ret; + struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; + + ret = ocfs2_xattr_list_entries(inode, xh, + xl->buffer, xl->buffer_size); + + if (ret < 0) + mlog_errno(ret); + else { + if (xl->buffer) + xl->buffer += ret; + + xl->buffer_size -= ret; + ret = 0; + } + + return ret; +} + +static int ocfs2_xattr_tree_list_index_block(struct inode *inode, + struct ocfs2_xattr_tree_root *xt, + char *buffer, + size_t buffer_size) +{ + struct ocfs2_extent_list *el = &xt->xt_list; + int ret = 0; + u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; + u64 p_blkno = 0; + struct ocfs2_xattr_tree_list xl = { + .buffer = buffer, + .buffer_size = buffer_size, + }; + + if (le16_to_cpu(el->l_next_free_rec) == 0) + return 0; + + while (name_hash > 0) { + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, + &e_cpos, &num_clusters, el); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, + ocfs2_list_xattr_bucket, + &xl); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (e_cpos == 0) + break; + + name_hash = e_cpos - 1; + } + + ret = buffer_size - xl.buffer_size; +out: + return ret; +} diff --git a/fs/ocfs2/xattr.h b/fs/ocfs2/xattr.h index f565c64..a69c8aa 100644 --- a/fs/ocfs2/xattr.h +++ b/fs/ocfs2/xattr.h @@ -55,4 +55,13 @@ extern int ocfs2_xattr_set(struct inode *, int, const char *, const void *, extern int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh); extern struct xattr_handler *ocfs2_xattr_handlers[]; +static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) +{ + return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; +} + +static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) +{ + return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); +} #endif /* OCFS2_XATTR_H */ -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:02 UTC
[Ocfs2-devel] [PATCH 12/15] Add xattr find process for xattr index btree.v2
We use bucket in ocfs2 to store large numbers of EAs, so when we want to find a specified xattr, we do like this: 1. Use ocfs2_xattr_get_rec to find the xattr clusters. 2. Find the xattr bucket which may contain this xattr. 3. Iterate the bucket and find whether the xattr exist. In order to co-exist the code in ocfs2_xattr_block_get, we allocate a buffer and copy this bucket to the buffer if the xattr is found. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/xattr.c | 490 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 476 insertions(+), 14 deletions(-) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 9be7bc8..48efef5 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -112,13 +112,21 @@ struct ocfs2_xattr_search { * when extended attribute in inode, xattr_bh is equal to inode_bh. */ struct buffer_head *xattr_bh; + struct buffer_head *header_bh; struct ocfs2_xattr_header *header; void *base; void *end; struct ocfs2_xattr_entry *here; + int alloc_base; int not_found; }; +static int ocfs2_xattr_index_block_find(struct inode *inode, + struct buffer_head *root_bh, + int name_index, + const char *name, + struct ocfs2_xattr_search *xs); + static int ocfs2_xattr_tree_list_index_block(struct inode *inode, struct ocfs2_xattr_tree_root *xt, char *buffer, @@ -754,12 +762,22 @@ static int ocfs2_xattr_block_get(struct inode *inode, xs->xattr_bh = blk_bh; xb = (struct ocfs2_xattr_block *)blk_bh->b_data; - xs->header = &xb->xb_attrs.xb_header; - xs->base = (void *)xs->header; - xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; - xs->here = xs->header->xh_entries; - ret = ocfs2_xattr_find_entry(name_index, name, xs); + if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { + xs->header = &xb->xb_attrs.xb_header; + xs->base = (void *)xs->header; + xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; + xs->here = xs->header->xh_entries; + + ret = ocfs2_xattr_find_entry(name_index, name, xs); + } else { + xs->header_bh = NULL; + xs->alloc_base = 0; + ret = ocfs2_xattr_index_block_find(inode, blk_bh, + name_index, + name, xs); + } + if (ret) goto cleanup; size = le64_to_cpu(xs->here->xe_value_size); @@ -782,9 +800,13 @@ static int ocfs2_xattr_block_get(struct inode *inode, } ret = size; cleanup: - if (blk_bh) - brelse(blk_bh); + if (xs->header_bh) + brelse(xs->header_bh); + if (xs->alloc_base) + kfree(xs->base); + if (xs->xattr_bh) + brelse(xs->xattr_bh); return ret; } @@ -1627,6 +1649,7 @@ static int ocfs2_xattr_block_find(struct inode *inode, { struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; struct buffer_head *blk_bh = NULL; + struct ocfs2_xattr_block *xb; int ret = 0; if (!di->i_xattr_loc) @@ -1647,18 +1670,24 @@ static int ocfs2_xattr_block_find(struct inode *inode, } xs->xattr_bh = blk_bh; - xs->header = &((struct ocfs2_xattr_block *)blk_bh->b_data)-> - xb_attrs.xb_header; - xs->base = (void *)xs->header; - xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; - xs->here = xs->header->xh_entries; + xb = (struct ocfs2_xattr_block *)blk_bh->b_data; + + if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { + xs->header = &xb->xb_attrs.xb_header; + xs->base = (void *)xs->header; + xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; + xs->here = xs->header->xh_entries; + + ret = ocfs2_xattr_find_entry(name_index, name, xs); + } else + ret = ocfs2_xattr_index_block_find(inode, blk_bh, + name_index, + name, xs); - ret = ocfs2_xattr_find_entry(name_index, name, xs); if (ret && ret != -ENODATA) goto cleanup; xs->not_found = ret; return 0; - cleanup: if (blk_bh) brelse(blk_bh); @@ -1893,6 +1922,18 @@ cleanup: return ret; } +static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode, + int name_index, + const char *suffix_name) +{ + struct xattr_handler *handler = ocfs2_xattr_handler(name_index); + char *prefix = handler->prefix; + int prefix_len = strlen(handler->prefix); + + return ocfs2_xattr_name_hash(inode, prefix, prefix_len, + (char *)suffix_name, strlen(suffix_name)); +} + /* * Find the xattr extent rec which may contains name_hash. * e_cpos will be the first name hash of the xattr rec. @@ -1959,6 +2000,427 @@ out: return ret; } +/* + * Get the xattr entry at offset in a bucket(starting from header_bh). + * + * The bh will be set as the block which contains this entry. + * Please note that the whole xattr entry will always be in the same block. + */ +static struct ocfs2_xattr_entry* + ocfs2_get_xe_in_bucket(struct inode *inode, + struct buffer_head *header_bh, + struct buffer_head **bh, + u16 offset) +{ + int ret; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + struct ocfs2_xattr_entry *xe = NULL; + u16 xe_count = le16_to_cpu(xh->xh_count); + u16 xe_off, block_off; + size_t blocksize = inode->i_sb->s_blocksize; + u64 start_blkno = header_bh->b_blocknr; + + *bh = NULL; + + if (offset >= xe_count) + return NULL; + + xe_off = sizeof(struct ocfs2_xattr_header) + + offset * sizeof(struct ocfs2_xattr_entry); + block_off = xe_off / blocksize; + + if (block_off == 0) { + xe = &xh->xh_entries[offset]; + get_bh(header_bh); + *bh = header_bh; + } else { + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), + start_blkno + block_off, + bh, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + xe_off = xe_off % blocksize; + xe = (struct ocfs2_xattr_entry *)((*bh)->b_data + xe_off); + } + +out: + return xe; +} + +/* Get a range of bytes in the bucket and copy the bytes to store. */ +static int ocfs2_get_range_in_bucket(struct inode *inode, + struct buffer_head *header_bh, + u16 start_offset, + u16 len, + char *store) +{ + int ret; + struct buffer_head *bh = NULL; + u16 read_len = 0, read, offset = start_offset; + u16 block_off; + int blocksize = inode->i_sb->s_blocksize; + u64 start_blkno = header_bh->b_blocknr; + + if (start_offset >= OCFS2_XATTR_BUCKET_SIZE || + start_offset + len > OCFS2_XATTR_BUCKET_SIZE) + return -EINVAL; + + while (len > 0) { + block_off = start_offset / blocksize; + offset = start_offset % blocksize; + + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), + start_blkno + block_off, + &bh, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + read = (blocksize - offset) <= len ? + (blocksize - offset) : len; + memcpy(store + read_len, bh->b_data + offset, read); + read_len += read; + start_offset += read; + len -= read; + brelse(bh); + bh = NULL; + } + ret = read_len; + +out: + return ret; +} + +static inline int ocfs2_get_xe_name_in_bucket(struct inode *inode, + struct buffer_head *header_bh, + struct ocfs2_xattr_entry *xe, + char *xe_name) +{ + u16 start = le16_to_cpu(xe->xe_name_offset); + u16 len = xe->xe_name_len; + + return ocfs2_get_range_in_bucket(inode, header_bh, + start, len, xe_name); +} + +static int ocfs2_find_xe_in_bucket(struct inode *inode, + struct buffer_head *header_bh, + int name_index, + const char *name, + u32 name_hash, + u16 *xe_index, + int *found) +{ + int ret = 0, cmp = 1; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + size_t name_len = strlen(name); + u16 i, xe_count = le16_to_cpu(xh->xh_count); + struct ocfs2_xattr_entry *xe = NULL; + struct buffer_head *xe_bh = NULL; + char *xe_name = NULL; + + /* + * We don't use binary search in the bucket because there + * may be multiple entries with the same name hash. + */ + for (i = 0; i < xe_count; i++) { + xe = ocfs2_get_xe_in_bucket(inode, header_bh, &xe_bh, i); + if (!xe) { + ret = -EIO; + mlog_errno(ret); + goto out; + } + + if (name_hash > le32_to_cpu(xe->xe_name_hash)) + goto next; + else if (name_hash < le32_to_cpu(xe->xe_name_hash)) + break; + + cmp = name_index - xe->xe_type; + if (!cmp) + cmp = name_len - xe->xe_name_len; + if (cmp) + goto next; + + /* now we have to compare the xattr name. */ + xe_name = kzalloc(name_len, GFP_NOFS); + if (!xe_name) { + ret = -ENOMEM; + goto out; + } + ret = ocfs2_get_xe_name_in_bucket(inode, header_bh, + xe, xe_name); + if (ret != name_len) { + kfree(xe_name); + goto out; + } + + cmp = memcmp(name, xe_name, name_len); + kfree(xe_name); + if (cmp == 0) { + *xe_index = i; + *found = 1; + ret = 0; + break; + } +next: + brelse(xe_bh); + xe_bh = NULL; + } + +out: + brelse(xe_bh); + return ret; +} + +static int ocfs2_read_xattr_bucket(struct inode *inode, + u64 blkno, + struct buffer_head **bhs, + int new) +{ + int ret = 0; + u16 i, block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + + if (!new) + return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno, + block_num, bhs, + OCFS2_BH_CACHED, inode); + + for (i = 0; i < block_num; i++) { + bhs[i] = sb_getblk(inode->i_sb, blkno + i); + if (bhs[i] == NULL) { + ret = -EIO; + mlog_errno(ret); + break; + } + ocfs2_set_new_buffer_uptodate(inode, bhs[i]); + } + + return ret; +} + +static int ocfs2_cp_xattr_bucket_to_buffer(struct inode *inode, + u64 blkno, + char *buffer) +{ + int i, ret, block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + int blocksize = inode->i_sb->s_blocksize; + struct buffer_head **bhs = NULL; + char *target; + + bhs = kzalloc(sizeof(struct buffer_head *) * block_num, GFP_NOFS); + ret = ocfs2_read_xattr_bucket(inode, blkno, bhs, 0); + if (ret) + goto out; + + target = buffer; + for (i = 0; i < block_num; i++, target += blocksize) + memcpy(target, bhs[i]->b_data, blocksize); + +out: + if (bhs) { + for (i = 0; i < block_num; i++) + brelse(bhs[i]); + kfree(bhs); + } + return ret; +} + +/* + * Find the specided xattr entry in a series of buckets. + * This series start from p_blkno and last for num_clusters. + * The ocfs2_xattr_header.xh_reserved1 of the first bucket contains + * the num of the valid buckets. + * + * Return the buffer_head this xattr should reside in. And if the xattr's + * hash is in the gap of 2 buckets, return the lower bucket. + */ +static int ocfs2_xattr_bucket_find(struct inode *inode, + int name_index, + const char *name, + u32 name_hash, + u64 p_blkno, + u32 first_hash, + u32 num_clusters, + struct ocfs2_xattr_search *xs) +{ + int ret, found = 0; + struct buffer_head *bh = NULL; + struct buffer_head *last_bh = NULL; + struct buffer_head *lower_bh = NULL; + struct ocfs2_xattr_header *xh = NULL; + struct ocfs2_xattr_entry *xe = NULL; + u16 xh_count, xe_index = 0; + u16 block_in_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + int low_bucket = 0, bucket, high_bucket; + int blocksize = inode->i_sb->s_blocksize; + u32 last_hash; + u64 blkno; + + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, + &bh, OCFS2_BH_CACHED, inode); + if (ret) + goto out; + xh = (struct ocfs2_xattr_header *)bh->b_data; + high_bucket = le16_to_cpu(xh->xh_reserved1) - 1; + + while (low_bucket <= high_bucket) { + brelse(bh); + bh = last_bh = NULL; + bucket = (low_bucket + high_bucket) / 2; + + blkno = p_blkno + bucket * block_in_bucket; + + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, + &bh, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + xh = (struct ocfs2_xattr_header *)bh->b_data; + xe = &xh->xh_entries[0]; + if (name_hash < le32_to_cpu(xe->xe_name_hash)) { + high_bucket = bucket - 1; + continue; + } + + /* + * Check whether the hash of the last entry in our + * bucket is larger than the search one. + */ + xh_count = le16_to_cpu(xh->xh_count); + xe = ocfs2_get_xe_in_bucket(inode, bh, &last_bh, + xh_count - 1); + if (!xe) { + ret = -EIO; + mlog_errno(ret); + goto out; + } + + last_hash = le32_to_cpu(xe->xe_name_hash); + brelse(last_bh); + + /* record lower_bh which may be the insert place. */ + brelse(lower_bh); + lower_bh = bh; + bh = NULL; + + if (name_hash > le32_to_cpu(xe->xe_name_hash)) { + low_bucket = bucket + 1; + continue; + } + + /* the searched xattr should reside in this bucket if exists. */ + ret = ocfs2_find_xe_in_bucket(inode, lower_bh, + name_index, name, name_hash, + &xe_index, &found); + break; + } + + /* + * Record the bucket we have found. + * When the xattr's hash value is in the gap of 2 buckets, we will + * always set it to the previous bucket so that it does't need to + * move the xattr entries and speed up the insertion. + * Here the "header" is initialized first as header_bh->b_data so that + * the set function can use it to find the insert place. + */ + if (!lower_bh) { + /* + * We can't find any bucket whose first name_hash is less + * than the find name_hash. + */ + BUG_ON(bh->b_blocknr != p_blkno); + lower_bh = bh; + bh = NULL; + } + xs->header_bh = lower_bh; + xs->header = (struct ocfs2_xattr_header *)xs->header_bh->b_data; + lower_bh = NULL; + xs->base = NULL; + + /* + * Alloc buffer and get the xattr attribute if needed. + * If the blocksize is equal to bucket size, we don't do allocation. + */ + if (found) { + if (blocksize < OCFS2_XATTR_BUCKET_SIZE) { + xs->base = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!xs->base) { + ret = -ENOMEM; + mlog_errno(ret); + goto out; + } + xs->alloc_base = 1; + ret = ocfs2_cp_xattr_bucket_to_buffer(inode, + xs->header_bh->b_blocknr, + xs->base); + if (ret) + goto out; + } else + xs->base = xs->header_bh->b_data; + + xs->end = xs->base + OCFS2_XATTR_BUCKET_SIZE; + xs->here = &((struct ocfs2_xattr_header *)xs->base)-> + xh_entries[xe_index]; + mlog(0, "find xattr in bucket %llu, index = %u\n", + (unsigned long long)xs->header_bh->b_blocknr, xe_index); + } else + ret = -ENODATA; + +out: + brelse(bh); + brelse(lower_bh); + return ret; +} + +static int ocfs2_xattr_index_block_find(struct inode *inode, + struct buffer_head *root_bh, + int name_index, + const char *name, + struct ocfs2_xattr_search *xs) +{ + int ret; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)root_bh->b_data; + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; + struct ocfs2_extent_list *el = &xb_root->xt_list; + u64 p_blkno = 0; + u32 first_hash, num_clusters = 0; + u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name); + + if (le16_to_cpu(el->l_next_free_rec) == 0) + return -ENODATA; + + mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", + name, name_hash, name_index); + + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, + &num_clusters, el); + if (ret) { + mlog_errno(ret); + goto out; + } + + BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); + + mlog(0, "find xattr extent rec %u clusters from %llu, the first hash " + "in the rec is %u\n", num_clusters, p_blkno, first_hash); + + ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, + p_blkno, first_hash, num_clusters, xs); + +out: + return ret; +} + static int ocfs2_iterate_xattr_buckets(struct inode *inode, u64 blkno, u32 clusters, -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:02 UTC
[Ocfs2-devel] [PATCH 13/15] Enable xattr set in index btree.v2
The previous 2 patches add the ability of list/get xattr in buckets for ocfs2. This patch enables ocfs2 to store large numbers of EAs. The original design doc is written by Mark Fasheh, and it can be found in http://oss.oracle.com/osswiki/OCFS2/DesignDocs/IndexedEATrees. Just some little modifications to it. First, because the bucket size is 4K, a new field named as xh_offset is added in ocfs2_xattr_header to indicate the next valid name/value offset in abucket. It is used when we store new EA name/value. With this field, we can find the place more quickly and what's more, we don't need to sort the name/value every time to let the last entry indicate the next unused space. Considering when the blocksize is 512, we may have to update 8 blocks for one insertion if we sort name/value like the original in-inode xattr. It is definitely inefficient. Because of the new xh_offset, another field named as xh_name_value_len is also added in ocfs2_xattr_header. It records the total length of all the name/values in the bucket. We need this so that we can check it and defragment the bucket if the bucket is too much fragmented. So now the insertion will be like this: 1. xattr_index_block_find: find the right bucket by the name_hash, say bucketA. 2. check whether there is enough space in bucketA. If yes, insert it directly and modify xh_offset and xh_name_value_len accordingly. If no, check xh_name_value_len to see whether we can store this by defragment the bucket. If yes, defragment it and go on insertion. 3. If defragement doesnt' work, check whether there is new empty bucket in the clusters within this extent record. If yes, init the new bucket and move all the buckets after bucketA one by one to the next bucket. Move half of the entries in bucketA to the next bucket and go on insertion. 4. If there is no new bucket, grow the extent tree.(This should be the same as Mark has described in the design doc). As for xattr deletion, we will delete an xattr bucket when all the xattr in this bucket are removed and move all the buckets after it to the previous one. When all the xattr buckets in an extend record are freed, free this extend records from ocfs2_xattr_tree. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/xattr.c | 2248 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 2237 insertions(+), 11 deletions(-) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 48efef5..89c8c16 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -36,6 +36,7 @@ #include <linux/mount.h> #include <linux/writeback.h> #include <linux/falloc.h> +#include <linux/sort.h> #define MLOG_MASK_PREFIX ML_XATTR #include <cluster/masklog.h> @@ -132,6 +133,13 @@ static int ocfs2_xattr_tree_list_index_block(struct inode *inode, char *buffer, size_t buffer_size); +static int ocfs2_xattr_create_index_block(struct inode *inode, + struct ocfs2_xattr_search *xs); + +static int ocfs2_xattr_set_entry_index_block(struct inode *inode, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs); + static inline struct xattr_handler *ocfs2_xattr_handler(int name_index) { struct xattr_handler *handler = NULL; @@ -801,12 +809,10 @@ static int ocfs2_xattr_block_get(struct inode *inode, ret = size; cleanup: - if (xs->header_bh) - brelse(xs->header_bh); + brelse(xs->header_bh); + brelse(xs->xattr_bh); if (xs->alloc_base) kfree(xs->base); - if (xs->xattr_bh) - brelse(xs->xattr_bh); return ret; } @@ -1695,6 +1701,46 @@ cleanup: return ret; } +static int ocfs2_restore_xattr_block(struct inode *inode, + struct ocfs2_xattr_search *xs) +{ + int ret; + handle_t *handle; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; + struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; + u16 xb_flags = le16_to_cpu(xb->xb_flags); + + BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) || + le16_to_cpu(el->l_next_free_rec) != 0); + + handle = ocfs2_start_trans(osb, 1); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + handle = NULL; + goto out; + } + + ret = ocfs2_journal_access(handle, inode, xs->xattr_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret < 0) { + mlog_errno(ret); + goto out_commit; + } + + memset(&xb->xb_attrs, 0, sizeof(struct ocfs2_xattr_header)); + + xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED); + + ocfs2_journal_dirty(handle, xs->xattr_bh); + +out_commit: + ocfs2_commit_trans(osb, handle); +out: + return ret; +} + /* * ocfs2_xattr_block_set() * @@ -1793,12 +1839,26 @@ out: ocfs2_free_alloc_context(meta_ac); if (ret < 0) return ret; + } else + xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; + + if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { + /* Set extended attribute into external block */ + ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL, + xattrsize); + if (!ret || ret != -ENOSPC) + goto end; + + ret = ocfs2_xattr_create_index_block(inode, xs); + if (ret) + goto end; } - /* Set extended attribute into external block */ - ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL, - xattrsize); + ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs); + if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0) + ret = ocfs2_restore_xattr_block(inode, xs); +end: return ret; } @@ -1914,10 +1974,11 @@ int ocfs2_xattr_set(struct inode *inode, } cleanup: ocfs2_inode_unlock(inode, 1); - if (di_bh) - brelse(di_bh); - if (xbs.xattr_bh) - brelse(xbs.xattr_bh); + brelse(di_bh); + brelse(xbs.xattr_bh); + brelse(xbs.header_bh); + if (xbs.alloc_base) + kfree(xbs.base); return ret; } @@ -2576,3 +2637,2168 @@ static int ocfs2_xattr_tree_list_index_block(struct inode *inode, out: return ret; } + +static int cmp_xe(const void *a, const void *b) +{ + const struct ocfs2_xattr_entry *l = a, *r = b; + u32 l_hash = le32_to_cpu(l->xe_name_hash); + u32 r_hash = le32_to_cpu(r->xe_name_hash); + + if (l_hash > r_hash) + return 1; + if (l_hash < r_hash) + return -1; + return 0; +} + +static void swap_xe(void *a, void *b, int size) +{ + struct ocfs2_xattr_entry *l = a, *r = b, tmp; + + tmp = *l; + memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); + memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); +} + +/* + * When the ocfs2_xattr_block is filled up, new bucket will be created + * and all the xattr entries will be moved to the new bucket. + * Note: we need to sort the entries since they are not saved in order + * in the ocfs2_xattr_block. + */ +static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, + struct buffer_head *xb_bh, + struct buffer_head *xh_bh, + struct buffer_head *data_bh) +{ + int i, blocksize = inode->i_sb->s_blocksize; + u16 offset, size, off_change; + struct ocfs2_xattr_entry *xe; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xb_bh->b_data; + struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)xh_bh->b_data; + u16 count = le16_to_cpu(xb_xh->xh_count); + char *target = xh_bh->b_data, *src = xb_bh->b_data; + + mlog(0, "cp xattr from block %llu to bucket %llu\n", + (unsigned long long)xb_bh->b_blocknr, + (unsigned long long)xh_bh->b_blocknr); + + xh->xh_count = xb_xh->xh_count; + xh->xh_reserved1 = cpu_to_le16(1); + + /* + * Since the xe_name_offset is based on ocfs2_xattr_header, + * there is a offset change corresponding to the change of + * ocfs2_xattr_header's position. + */ + off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); + xe = &xb_xh->xh_entries[count-1]; + offset = le16_to_cpu(xe->xe_name_offset) + off_change; + size = blocksize - offset; + xh->xh_name_value_len = cpu_to_le16(size); + xh->xh_offset = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); + + mlog(0, "copy name/value from %u to %u, size = %u\n", offset, + le16_to_cpu(xh->xh_offset), size); + /* copy all the names and values. */ + if (data_bh) + target = data_bh->b_data; + memcpy(target + offset, src + offset, size); + + /* copy all the entries. */ + target = xh_bh->b_data; + offset = offsetof(struct ocfs2_xattr_header, xh_entries); + size = count * sizeof(struct ocfs2_xattr_entry); + memcpy(target + offset, (char *)xb_xh + offset, size); + + /* Change the xe offset for all the xe because of the move. */ + off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + + offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); + for (i = 0; i < count; i++) + le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); + + mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", + offset, size, off_change); + + sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), + cmp_xe, swap_xe); +} + +/* + * After we move xattr from block to index btree, we have to + * update ocfs2_xattr_search to the new xe and base. + */ +static int ocfs2_xattr_update_xattr_search(struct inode *inode, + struct ocfs2_xattr_search *xs, + struct buffer_head *old_bh, + struct buffer_head *new_bh) +{ + int ret = 0; + char *buf = old_bh->b_data; + struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; + struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; + int i, blocksize = inode->i_sb->s_blocksize; + + xs->header_bh = new_bh; + get_bh(new_bh); + xs->header = (struct ocfs2_xattr_header *)xs->header_bh->b_data; + + if (OCFS2_XATTR_BUCKET_SIZE != blocksize) { + xs->base = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!xs->base) + return -ENOMEM; + xs->alloc_base = 1; + ret = ocfs2_cp_xattr_bucket_to_buffer(inode, + new_bh->b_blocknr, + xs->base); + if (ret) { + mlog_errno(ret); + return ret; + } + } else + xs->base = new_bh->b_data; + xs->end = xs->base + OCFS2_XATTR_BUCKET_SIZE; + + if (!xs->not_found) { + i = xs->here - old_xh->xh_entries; + xs->here = &((struct ocfs2_xattr_header *)xs->base)-> + xh_entries[i]; + } + + return ret; +} + +static int ocfs2_xattr_create_index_block(struct inode *inode, + struct ocfs2_xattr_search *xs) +{ + int ret, credits = OCFS2_SUBALLOC_ALLOC; + u32 bit_off, len; + u64 blkno; + handle_t *handle; + struct super_block *sb = inode->i_sb; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_inode_info *oi = OCFS2_I(inode); + struct ocfs2_alloc_context *data_ac; + struct buffer_head *xh_bh = NULL, *data_bh = NULL; + struct buffer_head *xb_bh = xs->xattr_bh; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xb_bh->b_data; + struct ocfs2_xattr_tree_root *xr; + u16 xb_flags = le16_to_cpu(xb->xb_flags); + u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + + mlog(0, "create xattr index block for %llu\n", + (unsigned long long)xb_bh->b_blocknr); + + BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); + + ret = ocfs2_reserve_clusters(osb, 1, &data_ac); + if (ret) { + mlog_errno(ret); + goto out; + } + + /* + * XXX: + * Do we need this lock or should we use a new sem in xattr allocation? + */ + down_write(&oi->ip_alloc_sem); + + /* + * 3 more credits, one for xattr block update, one for the 1st block + * of the new xattr bucket and one for the value/data. + */ + credits += 3; + handle = ocfs2_start_trans(osb, credits); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + mlog_errno(ret); + goto out_sem; + } + + ret = ocfs2_journal_access(handle, inode, xb_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + /* + * The bucket may spread in many blocks, and + * we will only touch the 1st block and the last block + * in the whole bucket(one for entry and one for data. + */ + blkno = ocfs2_clusters_to_blocks(sb, bit_off); + + mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno); + + ret = ocfs2_read_block(osb, blkno, &xh_bh, + OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_journal_access(handle, inode, xh_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + if (bpb > 1) { + ret = ocfs2_read_block(osb, blkno + bpb - 1, &data_bh, + OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_journal_access(handle, inode, data_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + } + + ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh); + + ocfs2_journal_dirty(handle, xh_bh); + if (data_bh) + ocfs2_journal_dirty(handle, data_bh); + + ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh); + + /* Re-initalize the xattr block. */ + xr = &xb->xb_attrs.xb_root; + memset(xr, 0, sizeof(struct ocfs2_xattr_tree_root)); + xr->xt_clusters = cpu_to_le32(1); + xr->xt_last_eb_blk = 0; + xr->xt_list.l_tree_depth = 0; + xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); + xr->xt_list.l_next_free_rec = cpu_to_le16(1); + + memset(xr->xt_list.l_recs, 0, sizeof(struct ocfs2_extent_rec)); + xr->xt_list.l_recs[0].e_cpos = 0; + xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); + xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); + + xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); + + ret = ocfs2_journal_dirty(handle, xb_bh); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + +out_commit: + ocfs2_commit_trans(osb, handle); + +out_sem: + up_write(&oi->ip_alloc_sem); + +out: + if (data_ac) + ocfs2_free_alloc_context(data_ac); + + brelse(xh_bh); + brelse(data_bh); + + return ret; +} + +static int cmp_xe_offset(const void *a, const void *b) +{ + const struct ocfs2_xattr_entry *l = a, *r = b; + u32 l_name_offset = le16_to_cpu(l->xe_name_offset); + u32 r_name_offset = le16_to_cpu(r->xe_name_offset); + + if (l_name_offset < r_name_offset) + return 1; + if (l_name_offset > r_name_offset) + return -1; + return 0; +} + +/* + * defrag a xattr bucket if we find that the bucket has some + * holes in beteen name/value pairs. + * We will move all the name/value pairs to the end of the bucket + * so that we can spare some space for insertion. + */ +static int ocfs2_defrag_xattr_bucket(struct inode *inode, + struct buffer_head *header_bh, + char *bucket_buf, + int *free) +{ + int ret, i; + size_t end, offset, len, value_len; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + u16 count = le16_to_cpu(xh->xh_count), val_start; + char *entries, *buf, *bucket = NULL; + u64 blkno = header_bh->b_blocknr; + u16 block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + size_t blocksize = inode->i_sb->s_blocksize; + handle_t *handle; + struct buffer_head **bhs; + struct ocfs2_xattr_entry *xe; + + mlog(0, "adjust xattr bucket in %llu, count = %u, " + "xh_offset = %u, xh_name_value_len = %u.\n", + blkno, count, le16_to_cpu(xh->xh_offset), + le16_to_cpu(xh->xh_name_value_len)); + + bhs = kcalloc(block_num, sizeof(struct buffer_head *), GFP_NOFS); + if (!bhs) + return -ENOMEM; + + ret = ocfs2_read_blocks(osb, blkno, block_num, bhs, + OCFS2_BH_CACHED, inode); + if (ret) + goto out; + + /* + * In order to make the operation more efficient and generic, + * we copy all the blocks into a contiguous memory and do the + * defragment there, so if anything is error, we will not touch + * the real block. + */ + bucket = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!bucket) { + ret = -EIO; + goto out; + } + + buf = bucket; + for (i = 0; i < block_num; i++, buf += blocksize) + memcpy(buf, bhs[i]->b_data, blocksize); + + handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), block_num); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + handle = NULL; + mlog_errno(ret); + goto out; + } + + for (i = 0; i < block_num; i++) { + ret = ocfs2_journal_access(handle, inode, bhs[i], + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret < 0) { + mlog_errno(ret); + goto commit; + } + } + + xh = (struct ocfs2_xattr_header *)bucket; + entries = (char *)xh->xh_entries; + + /* + * sort all the entries by their offset. + * the largest will be the first, so that we can + * move them to the end one by one. + */ + sort(entries, count, sizeof(struct ocfs2_xattr_entry), + cmp_xe_offset, swap_xe); + + /* Move all name/values to the end of the bucket. */ + xe = xh->xh_entries; + end = OCFS2_XATTR_BUCKET_SIZE; + for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { + offset = le16_to_cpu(xe->xe_name_offset); + if (xe->xe_local) + value_len = OCFS2_XATTR_SIZE( + le64_to_cpu(xe->xe_value_size)); + else + value_len = OCFS2_XATTR_ROOT_SIZE; + len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; + + /* + * We must make sure that the xattr_value_root + * exist in the same block. So adjust end to + * the previous block end if needed. + */ + if (!xe->xe_local && + ((end -value_len) / blocksize !+ (end -1) / blocksize)) + end = end -end % blocksize; + + if (end > offset + len) { + val_start = end -len; + memmove(bucket + end -len, bucket + offset, len); + xe->xe_name_offset = cpu_to_le16(end -len); + } + end -= len; + } + + BUG_ON(le16_to_cpu(xh->xh_offset) > end); + + if (free) + *free += end -le16_to_cpu(xh->xh_offset); + if (le16_to_cpu(xh->xh_offset) == end) + goto commit; + xh->xh_offset = cpu_to_le16(end); + + /* sort the entries by their name_hash. */ + sort(entries, count, sizeof(struct ocfs2_xattr_entry), + cmp_xe, swap_xe); + + buf = bucket; + for (i = 0; i < block_num; i++, buf += blocksize) { + memcpy(bhs[i]->b_data, buf, blocksize); + ocfs2_journal_dirty(handle, bhs[i]); + } + + if (bucket_buf) + memcpy(bucket_buf, bucket, OCFS2_XATTR_BUCKET_SIZE); +commit: + ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); +out: + for (i = 0; i < block_num; i++) + brelse(bhs[i]); + + kfree(bhs); + kfree(bucket); + return ret; +} + +/* + * Move half nums of the xattr bucket in the previous cluster to this new + * cluster. We only touch the last cluster of the previous extend record. + * + * first_bh is the first buffer_head of a series of bucket in the same + * extent rec and header_bh is the header of one bucket in this cluster. + * They will be updated if we move the data header_bh contains to the new + * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster. + */ +static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, + handle_t *handle, + struct buffer_head **first_bh, + struct buffer_head **header_bh, + u64 new_blkno, + u64 prev_blkno, + u32 num_clusters, + u32 *first_hash) +{ + int i, ret, credits; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + int block_num = ocfs2_clusters_to_blocks(inode->i_sb, 1); + int bucket_num = ocfs2_xattr_buckets_per_cluster(osb); + int blocksize = inode->i_sb->s_blocksize; + struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL; + struct ocfs2_xattr_header *new_xh; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)((*first_bh)->b_data); + + BUG_ON(le16_to_cpu(xh->xh_reserved1) < bucket_num); + BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize); + + prev_bh = *first_bh; + get_bh(prev_bh); + xh = (struct ocfs2_xattr_header *)prev_bh->b_data; + + prev_blkno += (num_clusters - 1) * block_num + block_num / 2; + + mlog(0, "move half of xattrs in cluster %llu to %llu\n", + prev_blkno, new_blkno); + + /* + * We need to update the 1st half of the cluster and + * 1 more for the update of the 1st bucket of the previous + * extent record. + */ + credits = block_num / 2 + 1; + ret = ocfs2_extend_trans(handle, credits); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, prev_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out; + } + + for (i = 0; i < block_num / 2; i++, prev_blkno++, new_blkno++) { + old_bh = new_bh = NULL; + new_bh = sb_getblk(inode->i_sb, new_blkno); + if (!new_bh) { + ret = -EIO; + mlog_errno(ret); + goto out; + } + + ocfs2_set_new_buffer_uptodate(inode, new_bh); + + ret = ocfs2_journal_access(handle, inode, new_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret < 0) { + mlog_errno(ret); + brelse(new_bh); + goto out; + } + + ret = ocfs2_read_block(osb, prev_blkno, + &old_bh, OCFS2_BH_CACHED, inode); + if (ret < 0) { + mlog_errno(ret); + brelse(new_bh); + goto out; + } + + memcpy(new_bh->b_data, old_bh->b_data, blocksize); + + if (i == 0) { + new_xh = (struct ocfs2_xattr_header *)new_bh->b_data; + new_xh->xh_reserved1 = cpu_to_le16(bucket_num / 2); + if (first_hash) + *first_hash = le32_to_cpu( + new_xh->xh_entries[0].xe_name_hash); + new_first_bh = new_bh; + get_bh(new_first_bh); + } + + ocfs2_journal_dirty(handle, new_bh); + + if (*header_bh == old_bh) { + brelse(*header_bh); + *header_bh = new_bh; + get_bh(*header_bh); + + brelse(*first_bh); + *first_bh = new_first_bh; + get_bh(*first_bh); + } + brelse(new_bh); + brelse(old_bh); + } + + le16_add_cpu(&xh->xh_reserved1, -(bucket_num / 2)); + + ocfs2_journal_dirty(handle, prev_bh); +out: + brelse(new_first_bh); + return ret; +} + +/* + * Move half num of the xattrs in bucket(blk) to new bucket(new_blk). + * first_hash will record the 1st hash of the next bucket. + */ +static int ocfs2_half_xattr_bucket(struct inode *inode, + handle_t *handle, + u64 blk, + u64 new_blk, + u32 *first_hash, + int new_bucket_head) +{ + int ret, i; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + u16 count, start, len, name_value_len, xe_len, name_offset; + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + struct buffer_head **s_bhs, **t_bhs = NULL; + struct ocfs2_xattr_header *xh; + struct ocfs2_xattr_entry *xe; + char *bucket = NULL, *buffer; + int blocksize = inode->i_sb->s_blocksize; + + mlog(0, "move half of xattrs from bucket %llu to %llu\n", + blk, new_blk); + + s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS); + if (!s_bhs) + return -ENOMEM; + + ret = ocfs2_read_blocks(osb, blk, blk_per_bucket, s_bhs, + OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS); + if (!t_bhs) { + ret = -ENOMEM; + goto out; + } + + ret = ocfs2_read_blocks(osb, new_blk, blk_per_bucket, t_bhs, + OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, s_bhs[0], + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto out; + } + + for (i = 0; i < blk_per_bucket; i++) { + ret = ocfs2_journal_access(handle, inode, t_bhs[i], + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto out; + } + } + + /* + * In order to simplify the process, we copy the source bucket to a + * buffer first, adjust it and then copy it to the dest. + */ + bucket = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!bucket) { + ret = -ENOMEM; + goto out; + } + + buffer = bucket; + for (i = 0; i < blk_per_bucket; i++, buffer += blocksize) + memcpy(buffer, s_bhs[i]->b_data, blocksize); + + xh = (struct ocfs2_xattr_header *)bucket; + count = le16_to_cpu(xh->xh_count); + start = count / 2; + + /* + * Calculate the total name/value len and xh_offset for + * the source bucket first. + */ + name_offset = OCFS2_XATTR_BUCKET_SIZE; + name_value_len = 0; + for (i = 0; i < start; i++) { + xe = &xh->xh_entries[i]; + xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); + if (le64_to_cpu(xe->xe_value_size) > OCFS2_XATTR_INLINE_SIZE) + xe_len += OCFS2_XATTR_ROOT_SIZE; + else + xe_len ++ OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); + name_value_len += xe_len; + if (le16_to_cpu(xe->xe_name_offset) < name_offset) + name_offset = le16_to_cpu(xe->xe_name_offset); + } + + /* + * Now begin the modification to the dest bucket. + * + * In the dest bucket, We just move the xattr entry to the beginning + * and don't touch the name/value. So there will be some holes in the + * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is + * called. + */ + xe = &xh->xh_entries[start]; + len = sizeof(struct ocfs2_xattr_entry) * (count - start); + mlog(0, "mv xattr entry len %d from %d to %d\n", len, + (char *)xe - bucket, (char *)xh->xh_entries - bucket); + memmove((char *)xh->xh_entries, (char *)xe, len); + xe = &xh->xh_entries[count - start]; + len = sizeof(struct ocfs2_xattr_entry) * start; + memset((char *)xe, 0, len); + + le16_add_cpu(&xh->xh_count, -start); + le16_add_cpu(&xh->xh_name_value_len, -name_value_len); + + /* Calculate xh_offset for the new bucket. */ + xh->xh_offset = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); + for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { + xe = &xh->xh_entries[i]; + xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); + if (le64_to_cpu(xe->xe_value_size) > OCFS2_XATTR_INLINE_SIZE) + xe_len += OCFS2_XATTR_ROOT_SIZE; + else + xe_len ++ OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); + if (le16_to_cpu(xe->xe_name_offset) < + le16_to_cpu(xh->xh_offset)) + xh->xh_offset = xe->xe_name_offset; + } + + /* set xh->xh_reserved1 for the new xh. */ + if (new_bucket_head) + xh->xh_reserved1 = cpu_to_le16(1); + else + xh->xh_reserved1 = 0; + + buffer = bucket; + for (i = 0; i < blk_per_bucket; i++, buffer += blocksize) { + memcpy(t_bhs[i]->b_data, buffer, blocksize); + ocfs2_journal_dirty(handle, s_bhs[0]); + if (ret) + mlog_errno(ret); + } + + /* store the first_hash of the new bucket. */ + if (first_hash) + *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); + + /* Now only update the source bucket header. */ + xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data; + xh->xh_count = cpu_to_le16(start); + xh->xh_offset = cpu_to_le16(name_offset); + xh->xh_name_value_len = cpu_to_le16(name_value_len); + + ocfs2_journal_dirty(handle, s_bhs[0]); + if (ret) + mlog_errno(ret); + +out: + if (s_bhs) { + for (i = 0; i < blk_per_bucket; i++) + brelse(s_bhs[i]); + } + kfree(s_bhs); + + if (t_bhs) { + for (i = 0; i < blk_per_bucket; i++) + brelse(t_bhs[i]); + } + kfree(t_bhs); + + kfree(bucket); + + return ret; +} + +/* + * Copy xattr from one bucket to another bucket. + * + * The caller must make sure that the journal transaction + * has enough space for journaling. + */ +static int ocfs2_cp_xattr_bucket(struct inode *inode, + handle_t *handle, + u64 s_blkno, + u64 t_blkno, + int t_is_new) +{ + int ret, i; + int block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + int blocksize = inode->i_sb->s_blocksize; + struct buffer_head **s_bhs, **t_bhs = NULL; + + BUG_ON(s_blkno == t_blkno); + + mlog(0, "cp bucket %llu to %llu, target is %d\n", + s_blkno, t_blkno, t_is_new); + + s_bhs = kzalloc(sizeof(struct buffer_head *) * block_num, GFP_NOFS); + ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0); + if (ret) + goto out; + + t_bhs = kzalloc(sizeof(struct buffer_head *) * block_num, GFP_NOFS); + ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new); + if (ret) + goto out; + + for (i = 0; i < block_num; i++) { + ret = ocfs2_journal_access(handle, inode, t_bhs[i], + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) + goto out; + } + + for (i = 0; i < block_num; i++) { + memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize); + ocfs2_journal_dirty(handle, t_bhs[i]); + } + +out: + if (s_bhs) { + for (i = 0; i < block_num; i++) + brelse(s_bhs[i]); + } + kfree(s_bhs); + + if (t_bhs) { + for (i = 0; i < block_num; i++) + brelse(t_bhs[i]); + } + kfree(t_bhs); + + return ret; +} + +/* + * Copy one xattr cluster from src_blk to to_blk. + * The to_blk will become the first bucket header of the cluster, so its + * xh_reserved1 will be initialized as the bucket num in the cluster. + */ +static int ocfs2_cp_xattr_cluster(struct inode *inode, + handle_t *handle, + struct buffer_head *first_bh, + u64 src_blk, + u64 to_blk, + u32 *first_hash) +{ + int i, ret, credits; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + int block_num = ocfs2_clusters_to_blocks(inode->i_sb, 1); + int bucket_num = ocfs2_xattr_buckets_per_cluster(osb); + struct buffer_head *bh = NULL; + struct ocfs2_xattr_header *xh; + + mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk); + + /* + * We need to update the new cluster and 1 more for the update of + * the 1st bucket of the previous extent rec. + */ + credits = block_num + 1; + ret = ocfs2_extend_trans(handle, credits); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, first_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out; + } + + for (i = 0; i < bucket_num; i++) { + ret = ocfs2_cp_xattr_bucket(inode, handle, + src_blk, to_blk, 1); + if (ret) { + mlog_errno(ret); + goto out; + } + + src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); + to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); + } + + /* update the old bucket header. */ + xh = (struct ocfs2_xattr_header *)first_bh->b_data; + le16_add_cpu(&xh->xh_reserved1, -bucket_num); + + ocfs2_journal_dirty(handle, first_bh); + + /* update the new bucket header. */ + to_blk -= block_num; + ret = ocfs2_read_block(osb, to_blk, &bh, OCFS2_BH_CACHED, inode); + if (ret < 0) { + mlog_errno(ret); + goto out; + } + + xh = (struct ocfs2_xattr_header *)bh->b_data; + xh->xh_reserved1 = cpu_to_le16(bucket_num); + + ocfs2_journal_dirty(handle, bh); + + if (first_hash) + *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); +out: + brelse(bh); + return ret; +} + +/* + * Move half of the xattrs in this cluster to the new cluster. + * This function should only be called when bucket size == cluster size. + * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. + */ +static inline int ocfs2_half_xattr_cluster(struct inode *inode, + handle_t *handle, + u64 prev_blk, + u64 new_blk, + u32 *first_hash) +{ + BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); + + /* Move half of the xattr in start_blk to the next bucket. */ + return ocfs2_half_xattr_bucket(inode, handle, prev_blk, + new_blk, first_hash, 1); +} + +/* + * Move some xattrs from the old cluster to the new one since they are not + * contiguous in ocfs2 xattr tree. + * + * new_blk starts a new separate cluster, and we will move some xattrs from + * prev_blk to it. v_start will be set as the first name hash value in this + * new cluster so that it can be used as e_cpos during tree insertion and + * don't collide with our original b-tree operations. first_bh and header_bh + * will also be updated since they will be used in ocfs2_extend_xattr_bucket + * to extend the insert bucket. + * + * The problem is how much xattr should we move to the new one and when should + * we update first_bh and header_bh? + * 1. If cluster size > bucket size, that means the previous cluster has more + * than 1 bucket, so just move half nums of bucket into the new cluster and + * update the first_bh and header_bh if the insert bucket has been moved + * to the new cluster. + * 2. If cluster_size == bucket_size: + * a) If the previous extent rec has more than one cluster and the insert + * place isn't in the last cluster, copy the entire last cluster to the + * new one. This time, we don't need to upate the first_bh and header_bh + * since they will not be moved into the new cluster. + * b) Otherwise, move the bottom half of the xattrs in the last cluster into + * the new one. And we set the extend flag to zero if the insert place is + * moved into the new allocated cluster since no extend is needed. + */ +static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, + handle_t *handle, + struct buffer_head **first_bh, + struct buffer_head **header_bh, + u64 new_blk, + u64 prev_blk, + u32 prev_clusters, + u32 *v_start, + int *extend) +{ + int ret = 0; + int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); + + mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", + prev_blk, prev_clusters, new_blk); + + if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) + ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, + handle, + first_bh, + header_bh, + new_blk, + prev_blk, + prev_clusters, + v_start); + else { + u64 last_blk = prev_blk + bpc * (prev_clusters - 1); + + if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk) + ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh, + last_blk, new_blk, + v_start); + else { + ret = ocfs2_half_xattr_cluster(inode, handle, + last_blk, new_blk, + v_start); + + if ((*header_bh)->b_blocknr == last_blk && extend) + *extend = 0; + } + } + + return ret; +} + +/* + * Add a new cluster for xattr storage. + * + * If the new cluster is contiguous with the previous one, it will be + * appended to the same extent record, and num_clusters will be updated. + * + * If not, we will insert a new extent for it and move some xattrs in + * the last cluster into the new allocated one. + * first_bh is the first block of the previous extent rec and header_bh + * indicates the bucket we will insert the new xattrs. They will be updated + * when the header_bh is moved into the new cluster. + */ +static int ocfs2_add_new_xattr_cluster(struct inode *inode, + struct buffer_head *root_bh, + struct buffer_head **first_bh, + struct buffer_head **header_bh, + u32 *num_clusters, + u32 prev_cpos, + u64 prev_blkno, + int *extend) +{ + int ret, credits; + u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); + u32 prev_clusters = *num_clusters; + u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; + u64 block; + handle_t *handle = NULL; + struct buffer_head *new_bh = NULL; + struct ocfs2_alloc_context *data_ac = NULL; + struct ocfs2_alloc_context *meta_ac = NULL; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct ocfs2_xattr_header *first_xh + (struct ocfs2_xattr_header *)(*first_bh)->b_data; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)root_bh->b_data; + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; + struct ocfs2_extent_list *root_el = &xb_root->xt_list; + enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT; + + mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " + "previous xattr blkno = %llu\n", + (unsigned long long)OCFS2_I(inode)->ip_blkno, + prev_cpos, prev_blkno); + + ret = ocfs2_lock_allocators(inode, root_bh, root_el, + clusters_to_add, 0, &data_ac, + &meta_ac, type, NULL); + if (ret) { + mlog_errno(ret); + goto leave; + } + + credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add); + handle = ocfs2_start_trans(osb, credits); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + handle = NULL; + mlog_errno(ret); + goto leave; + } + + ret = ocfs2_journal_access(handle, inode, root_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret < 0) { + mlog_errno(ret); + goto leave; + } + + ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, + clusters_to_add, &bit_off, &num_bits); + if (ret < 0) { + if (ret != -ENOSPC) + mlog_errno(ret); + goto leave; + } + + BUG_ON(num_bits > clusters_to_add); + + block = ocfs2_clusters_to_blocks(osb->sb, bit_off); + mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", + num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); + + if (prev_blkno + prev_clusters * bpc == block && + le16_to_cpu(first_xh->xh_reserved1) + + num_bits * ocfs2_xattr_buckets_per_cluster(osb) > + le16_to_cpu(first_xh->xh_reserved1)) { + /* + * If this cluster is contiguous with the old one and + * adding this new cluster, we don't surpass the limit of + * xh_reserved1, cool. We will let it be initialized + * and used like other buckets in the previous cluster. + * So add it as a contiguous one. The caller will handle + * its init process. + */ + v_start = prev_cpos + prev_clusters; + *num_clusters = prev_clusters + clusters_to_add; + mlog(0, "Add contiguous %u clusters to previous extent rec.\n", + clusters_to_add); + } else { + ret = ocfs2_adjust_xattr_cross_cluster(inode, + handle, + first_bh, + header_bh, + block, + prev_blkno, + prev_clusters, + &v_start, + extend); + if (ret) { + mlog_errno(ret); + goto leave; + } + } + + mlog(0, "Insert %u clusters at block %llu for xattr at %u\n", + num_bits, block, v_start); + ret = ocfs2_insert_extent(osb, handle, inode, root_bh, + v_start, block, num_bits, + 0, meta_ac, type, NULL); + if (ret < 0) { + mlog_errno(ret); + goto leave; + } + + ret = ocfs2_journal_dirty(handle, root_bh); + if (ret < 0) { + mlog_errno(ret); + goto leave; + } + +leave: + if (handle) { + ocfs2_commit_trans(osb, handle); + handle = NULL; + } + if (data_ac) { + ocfs2_free_alloc_context(data_ac); + data_ac = NULL; + } + if (meta_ac) { + ocfs2_free_alloc_context(meta_ac); + meta_ac = NULL; + } + + brelse(new_bh); + mlog_exit(ret); + return ret; +} + +/* + * Extend a new xattr bucket and move xattrs to the end one by one until + * We meet with start_bh. Only move half of the xattrs to the bucket after it. + */ +static int ocfs2_extend_xattr_bucket(struct inode *inode, + struct buffer_head *first_bh, + struct buffer_head *start_bh, + u32 num_clusters) +{ + int ret, credits; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + u64 start_blk = start_bh->b_blocknr, end_blk; + u32 bucket_num = num_clusters * ocfs2_xattr_buckets_per_cluster(osb); + handle_t *handle; + struct ocfs2_xattr_header *first_xh + (struct ocfs2_xattr_header *)first_bh->b_data; + u16 bucket = le16_to_cpu(first_xh->xh_reserved1); + + mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " + "from %llu, len = %u\n", start_blk, + (unsigned long long)first_bh->b_blocknr, num_clusters); + + BUG_ON(bucket >= bucket_num); + + end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket; + + /* + * We will touch all the buckets after the start_bh(include it). + * Add one more bucket and modify the first_bh. + */ + credits = end_blk - start_blk + 2 * blk_per_bucket + 1; + handle = ocfs2_start_trans(osb, credits); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + handle = NULL; + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, first_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto commit; + } + + while (end_blk != start_blk) { + ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, + end_blk + blk_per_bucket, 0); + if (ret) + goto commit; + end_blk -= blk_per_bucket; + } + + /* Move half of the xattr in start_blk to the next bucket. */ + ret = ocfs2_half_xattr_bucket(inode, handle, start_blk, + start_blk + blk_per_bucket, NULL, 0); + + le16_add_cpu(&first_xh->xh_reserved1, 1); + ocfs2_journal_dirty(handle, first_bh); + +commit: + ocfs2_commit_trans(osb, handle); +out: + return ret; +} + +/* + * Add new xattr bucket in a extent record and adjust the buckets accordingly. + * xb_bh is the ocfs2_xattr_block and header_bh is one header of a bucket. + * We will move all the buckets starting from it to the next place. As for + * this one, half of its xattr will be moved to the next one. + * + * We will allocate a new cluster if current cluster is full and adjust + * header_bh and first_bh if the insert place is moved to the new cluster. + */ +static int ocfs2_add_new_xattr_bucket(struct inode *inode, + struct buffer_head *xb_bh, + struct buffer_head *header_bh) +{ + struct ocfs2_xattr_header *first_xh = NULL; + struct buffer_head *first_bh = NULL; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xb_bh->b_data; + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; + struct ocfs2_extent_list *el = &xb_root->xt_list; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); + struct super_block *sb = inode->i_sb; + struct ocfs2_super *osb = OCFS2_SB(sb); + int ret, bucket_num, extend = 1; + u64 p_blkno; + u32 e_cpos, num_clusters; + + mlog(0, "Add new xattr bucket starting form %llu\n", + (unsigned long long)header_bh->b_blocknr); + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, + &num_clusters, el); + if (ret) + goto out; + + ret = ocfs2_read_block(osb, p_blkno, + &first_bh, OCFS2_BH_CACHED, inode); + if (ret) + goto out; + + bucket_num = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; + first_xh = (struct ocfs2_xattr_header *)first_bh->b_data; + + if (bucket_num == le16_to_cpu(first_xh->xh_reserved1)) { + ret = ocfs2_add_new_xattr_cluster(inode, + xb_bh, + &first_bh, + &header_bh, + &num_clusters, + e_cpos, + p_blkno, + &extend); + if (ret) { + mlog_errno(ret); + goto out; + } + } + + if (extend) + ret = ocfs2_extend_xattr_bucket(inode, + first_bh, + header_bh, + num_clusters); + if (ret) + mlog_errno(ret); +out: + brelse(first_bh); + return ret; +} + +/* + * Handle the normal xattr set, including replace, delete and new. + * When the bucket is empty, "is_empty" is set and the caller can + * free this bucket. + * + * Note: "local" indicates the real data's locality. So we can't + * just its bucket locality by its length. + */ +static void ocfs2_xattr_set_entry_normal(struct inode *inode, + char *bucket, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs, + u32 name_hash, + int local, + int *is_empty) +{ + struct ocfs2_xattr_entry *last, *xe; + int name_len = strlen(xi->name); + struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; + u16 count = le16_to_cpu(xh->xh_count); + size_t blocksize = inode->i_sb->s_blocksize; + void *val; + size_t offs, size, new_size; + + last = &xh->xh_entries[count]; + if (!xs->not_found) { + xe = xs->here; + offs = le16_to_cpu(xe->xe_name_offset); + val = xs->base + offs; + if (xe->xe_local) + size = OCFS2_XATTR_SIZE(name_len) + + OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); + else + size = OCFS2_XATTR_SIZE(name_len) + + OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); + + /* + * If the new value will be stored outside, xi->value has been + * initalized as an empty ocfs2_xattr_value_root, and the same + * goes with xi->value_len, so we can set new_size safely here. + * See ocfs2_xattr_set_in_bucket. + */ + new_size = OCFS2_XATTR_SIZE(name_len) + + OCFS2_XATTR_SIZE(xi->value_len); + + le16_add_cpu(&xh->xh_name_value_len, -size); + if (xi->value) { + if (new_size > size) + goto set_new_name_value; + + /* + * We must make sure that the xattr_value_root exist in + * the same block and if the old place doesn't meet with + * our need, we have to alloc a new space in the bucket. + */ + if (!local && offs / blocksize !+ (offs + new_size - 1) / blocksize) + goto set_new_name_value; + + /* Now replace the old value with new one. */ + if (local) + xe->xe_value_size = cpu_to_le64(xi->value_len); + else + xe->xe_value_size = 0; + + memset(val + OCFS2_XATTR_SIZE(name_len), 0, + size - OCFS2_XATTR_SIZE(name_len)); + if (OCFS2_XATTR_SIZE(xi->value_len) > 0) + memcpy(val + OCFS2_XATTR_SIZE(name_len), + xi->value, xi->value_len); + + le16_add_cpu(&xh->xh_name_value_len, new_size); + xe->xe_local = local; + return; + } else { + /* Remove the old entry. */ + last -= 1; + memmove(xe, xe + 1, + (void *)last - (void *)xe); + memset(last, 0, sizeof(struct ocfs2_xattr_entry)); + le16_add_cpu(&xh->xh_count, -1); + if (xh->xh_count == 0 && is_empty) + *is_empty = 1; + return; + } + } else { + /* find a new entry for insert. */ + int low = 0, high = count - 1, tmp; + struct ocfs2_xattr_entry *tmp_xe; + + while (low <= high) { + tmp = (low + high) / 2; + tmp_xe = &xh->xh_entries[tmp]; + + if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) + low = tmp + 1; + else if (name_hash < + le32_to_cpu(tmp_xe->xe_name_hash)) + high = tmp - 1; + else + break; + } + + xe = &xh->xh_entries[low]; + if (low != count) + memmove(xe + 1, xe, (void *)last - (void *)xe); + + le16_add_cpu(&xh->xh_count, 1); + memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); + xe->xe_name_hash = cpu_to_le32(name_hash); + xe->xe_name_len = name_len; + xe->xe_type = xi->name_index; + } + +set_new_name_value: + /* Insert the new name+value. */ + size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); + /* + * We must make sure that the xattr_value_root + * exist in the same block. + */ + offs = le16_to_cpu(xh->xh_offset); + if (!local) { + u16 val_start = offs - OCFS2_XATTR_ROOT_SIZE; + + if (val_start >> inode->i_sb->s_blocksize_bits !+ (offs - 1) >> inode->i_sb->s_blocksize_bits) { + offs = offs - offs % blocksize; + xh->xh_offset = cpu_to_le16(offs); + } + } + val = xs->base + offs - size; + xe->xe_name_offset = cpu_to_le16(offs - size); + + memset(val, 0, size); + memcpy(val, xi->name, name_len); + memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); + + xe->xe_value_size = cpu_to_le64(xi->value_len); + xe->xe_local = local; + xs->here = xe; + le16_add_cpu(&xh->xh_offset, -size); + le16_add_cpu(&xh->xh_name_value_len, size); + + return; +} + +static int ocfs2_xattr_bucket_handle_journal(struct inode *inode, + handle_t *handle, + struct ocfs2_xattr_search *xs, + struct buffer_head **bhs, + u16 bh_num) +{ + int ret = 0, i, len, off, block_off, block_end; + struct ocfs2_xattr_entry *xe = xs->here; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)xs->base; + u16 xh_count = le16_to_cpu(xh->xh_count); + size_t blocksize = inode->i_sb->s_blocksize; + char touched[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET]; + + memset(touched, 0, sizeof(touched)); + + /* + * First calculate all the blocks we should journal_access + * and journal_dirty. The first block should always be touched. + */ + touched[0] = 1; + + /* calc the data first. */ + off = le16_to_cpu(xe->xe_name_offset); + block_off = off >> inode->i_sb->s_blocksize_bits; + len = OCFS2_XATTR_SIZE(xe->xe_name_len); + if (xe->xe_local) + len += OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); + else + len += OCFS2_XATTR_ROOT_SIZE; + off += len - 1; + block_end = off >> inode->i_sb->s_blocksize_bits; + for (i = block_off; i <= block_end; i++) + touched[i] = 1; + + /* Now the xe_entry. */ + off = (char *)xe - (char *)xh; + block_off = off >> inode->i_sb->s_blocksize_bits; + len = ((char *)&xh->xh_entries[xh_count]) - (char *)xe; + block_end = (off + len - 1) >> inode->i_sb->s_blocksize_bits; + for (i = block_off; i <= block_end; i++) + touched[i] = 1; + + for (i = 0; i < bh_num; i++) { + if (!touched[i]) + continue; + + ret = ocfs2_journal_access(handle, inode, bhs[i], + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto out; + } + } + + for (i = 0; i < bh_num; i++) { + if (!touched[i]) + continue; + + memcpy(bhs[i]->b_data, xs->base + i * blocksize, blocksize); + ret = ocfs2_journal_dirty(handle, bhs[i]); + if (ret) + mlog_errno(ret); + } +out: + return ret; +} + +/* + * Set the xattr entry in the specified bucket. + * The bucket is indicated by xs->header_bh and it should have the enough + * space for the xattr insertion. + */ +static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs, + u32 name_hash, + int local, + int *bucket_empty) +{ + int i, ret; + handle_t *handle = NULL; + struct buffer_head **bhs = NULL; + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + size_t blocksize = inode->i_sb->s_blocksize; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + u64 blk = xs->header_bh->b_blocknr; + char *buf; + + mlog(0, "Set xattr entry len = %d index = %d in bucket %llu\n", + xi->value_len, xi->name_index, blk); + + bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS); + if (!bhs) + return -ENOMEM; + + ret = ocfs2_read_blocks(osb, blk, blk_per_bucket, + bhs, OCFS2_BH_CACHED, inode); + if (ret) + goto out; + + if (!xs->base) { + /* we should already set xs->base if we have found the xattr. */ + BUG_ON(!xs->not_found); + + if (blocksize < OCFS2_XATTR_BUCKET_SIZE) { + /* + * This is a new entry and we haven't find it before, + * So base isn't set in entry_find. + */ + xs->base = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); + if (!xs->base) { + ret = -ENOMEM; + goto out; + } + xs->alloc_base = 1; + + buf = xs->base; + for (i = 0; i < blk_per_bucket; i++, buf += blocksize) + memcpy(buf, bhs[i]->b_data, blocksize); + } else + xs->base = bhs[0]->b_data; + } + + handle = ocfs2_start_trans(osb, blk_per_bucket); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + handle = NULL; + mlog_errno(ret); + goto out; + } + + ocfs2_xattr_set_entry_normal(inode, xs->base, xi, xs, + name_hash, local, bucket_empty); + + /*Only access and dirty the blocks we have touched in set xattr. */ + ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs, + bhs, blk_per_bucket); + if (ret) + mlog_errno(ret); +out: + ocfs2_commit_trans(osb, handle); + + if (bhs) { + for (i = 0; i < blk_per_bucket; i++) + brelse(bhs[i]); + kfree(bhs); + } + + mlog_exit(ret); + return ret; +} + +static int ocfs2_xattr_value_update_size(struct inode *inode, + struct buffer_head *xe_bh, + struct ocfs2_xattr_entry *xe, + u64 new_size) +{ + int ret; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + handle_t *handle = NULL; + + handle = ocfs2_start_trans(osb, 1); + if (handle == NULL) { + ret = -ENOMEM; + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, xe_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret < 0) { + mlog_errno(ret); + goto out_commit; + } + + xe->xe_value_size = cpu_to_le64(new_size); + + ret = ocfs2_journal_dirty(handle, xe_bh); + if (ret < 0) + mlog_errno(ret); + +out_commit: + ocfs2_commit_trans(osb, handle); +out: + return ret; +} + +/* + * Truncate the specified xe_off entry in xattr bucket. + * bucket is indicated by header_bh and len is the new length. + * Both the ocfs2_xattr_value_root and the entry will be updated here. + * + * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. + */ +static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, + struct buffer_head *header_bh, + int xe_off, + int len, + char *new_xe, + char *new_xv) +{ + int ret, offset; + u64 value_blk; + struct buffer_head *value_bh = NULL, *xe_bh = NULL; + struct ocfs2_xattr_value_root *xv; + struct ocfs2_xattr_entry *xe; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + size_t blocksize = inode->i_sb->s_blocksize; + + if (blocksize == OCFS2_XATTR_BUCKET_SIZE) { + xe_bh = header_bh; + get_bh(xe_bh); + xe = &xh->xh_entries[xe_off]; + } else { + xe = ocfs2_get_xe_in_bucket(inode, header_bh, + &xe_bh, xe_off); + if (!xe) { + ret = -EIO; + goto out; + } + } + + BUG_ON(!xe || xe->xe_local); + + offset = le16_to_cpu(xe->xe_name_offset) + + OCFS2_XATTR_SIZE(xe->xe_name_len); + + value_blk = offset / blocksize; + + /* We don't allow ocfs2_xattr_value to be stored in different block. */ + BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); + value_blk += header_bh->b_blocknr; + + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk, + &value_bh, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + goto out; + } + + xv = (struct ocfs2_xattr_value_root *) + (value_bh->b_data + offset % blocksize); + + mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", + xe_off, (unsigned long long)header_bh->b_blocknr, len); + ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_xattr_value_update_size(inode, xe_bh, xe, len); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (new_xe && new_xe != (char *)xe) + memcpy(new_xe, xe, sizeof(struct ocfs2_xattr_entry)); + if (new_xv && new_xv != (char *)xv) + memcpy(new_xv, xv, OCFS2_XATTR_ROOT_SIZE); +out: + brelse(xe_bh); + brelse(value_bh); + return ret; +} + +static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, + struct ocfs2_xattr_search *xs, + int len) +{ + int ret, offset; + struct ocfs2_xattr_entry *xe = xs->here; + struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; + u16 val_offset = le16_to_cpu(xe->xe_name_offset) + + OCFS2_XATTR_SIZE(xe->xe_name_len); + + BUG_ON(!xs->base || !xe || xe->xe_local); + + offset = xe - xh->xh_entries; + ret = ocfs2_xattr_bucket_value_truncate(inode, xs->header_bh, + offset, len, (char *)xe, + xs->base + val_offset); + if (ret) + mlog_errno(ret); + + return ret; +} + +static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, + struct ocfs2_xattr_search *xs, + char *val, + int value_len) +{ + int offset; + struct ocfs2_xattr_value_root *xv; + struct ocfs2_xattr_entry *xe = xs->here; + + BUG_ON(!xs->base || !xe || xe->xe_local); + + offset = le16_to_cpu(xe->xe_name_offset) + + OCFS2_XATTR_SIZE(xe->xe_name_len); + + xv = (struct ocfs2_xattr_value_root *)(xs->base + offset); + + return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len); +} + +/* + * Remove the xattr bucket pointed by bucket_bh. + * All the buckets after it in the same xattr extent rec will be + * move forward one by one. + */ +static int ocfs2_rm_xattr_bucket(struct inode *inode, + struct buffer_head *first_bh, + struct buffer_head *bucket_bh) +{ + int ret = 0, credits; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)first_bh->b_data; + u16 bucket_num = le16_to_cpu(xh->xh_reserved1); + u64 end, start = bucket_bh->b_blocknr; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + handle_t *handle; + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); + + end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket; + + mlog(0, "rm xattr bucket %llu\n", + (unsigned long long)bucket_bh->b_blocknr); + /* + * We need to update the first xattr_header and all the buckets starting + * from start in this xattr rec. + * + * XXX: Should we empty the old last bucket here? + */ + credits = 1 + end -start; + handle = ocfs2_start_trans(osb, credits); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + mlog_errno(ret); + return ret; + } + + ret = ocfs2_journal_access(handle, inode, first_bh, + OCFS2_JOURNAL_ACCESS_CREATE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + + while (start < end) { + ret = ocfs2_cp_xattr_bucket(inode, handle, + start + blk_per_bucket, + start, 0); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + start += blk_per_bucket; + } + + /* update the first_bh. */ + xh->xh_reserved1 = cpu_to_le16(bucket_num - 1); + ocfs2_journal_dirty(handle, first_bh); + +out_commit: + ocfs2_commit_trans(osb, handle); + return ret; +} + +static int ocfs2_rm_xattr_cluster(struct inode *inode, + struct buffer_head *root_bh, + u64 blkno, + u32 cpos, + u32 len) +{ + int ret; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + struct inode *tl_inode = osb->osb_tl_inode; + handle_t *handle; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)root_bh->b_data; + struct ocfs2_extent_list *root_el = &xb->xb_attrs.xb_root.xt_list; + struct ocfs2_alloc_context *meta_ac = NULL; + struct ocfs2_cached_dealloc_ctxt dealloc; + + ocfs2_init_dealloc_ctxt(&dealloc); + + mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", + cpos, len, (unsigned long long)blkno); + + ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len); + + ret = ocfs2_lock_allocators(inode, root_bh, root_el, + 0, 1, NULL, &meta_ac, + OCFS2_XATTR_TREE_EXTENT, NULL); + if (ret) { + mlog_errno(ret); + return ret; + } + + mutex_lock(&tl_inode->i_mutex); + + if (ocfs2_truncate_log_needs_flush(osb)) { + ret = __ocfs2_flush_truncate_log(osb); + if (ret < 0) { + mlog_errno(ret); + goto out; + } + } + + handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); + if (handle == NULL) { + ret = -ENOMEM; + mlog_errno(ret); + goto out; + } + + ret = ocfs2_journal_access(handle, inode, root_bh, + OCFS2_JOURNAL_ACCESS_WRITE); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac, + &dealloc, OCFS2_XATTR_TREE_EXTENT, NULL); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); + + ret = ocfs2_journal_dirty(handle, root_bh); + if (ret) { + mlog_errno(ret); + goto out_commit; + } + + ret = ocfs2_truncate_log_append(osb, handle, blkno, len); + if (ret) + mlog_errno(ret); + +out_commit: + ocfs2_commit_trans(osb, handle); +out: + ocfs2_schedule_truncate_log_flush(osb, 1); + + mutex_unlock(&tl_inode->i_mutex); + + if (meta_ac) + ocfs2_free_alloc_context(meta_ac); + + ocfs2_run_deallocs(osb, &dealloc); + + return ret; +} + +/* + * Free the xattr bucket indicated by xs->header_bh and if all the buckets + * in the clusters is free, free the clusters also. + */ +static int ocfs2_xattr_bucket_shrink(struct inode *inode, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs, + u32 name_hash) +{ + int ret; + u32 e_cpos, num_clusters; + u64 p_blkno; + struct buffer_head *first_bh = NULL; + struct ocfs2_xattr_header *first_xh; + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; + + BUG_ON(xs->header->xh_count != 0); + + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, + &e_cpos, &num_clusters, + &xb->xb_attrs.xb_root.xt_list); + if (ret) { + mlog_errno(ret); + return ret; + } + + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, + &first_bh, OCFS2_BH_CACHED, inode); + if (ret) { + mlog_errno(ret); + return ret; + } + + ret = ocfs2_rm_xattr_bucket(inode, first_bh, xs->header_bh); + if (ret) { + mlog_errno(ret); + goto out; + } + + first_xh = (struct ocfs2_xattr_header *)first_bh->b_data; + if (first_xh->xh_reserved1 == 0) + ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh, + p_blkno, e_cpos, + num_clusters); + +out: + brelse(first_bh); + return ret; +} + +/* + * Set the xattr name/value in the bucket specified in xs. + * + * As the new value in xi may be stored in the bucket or in an outside cluster, + * we divide the whole process into 3 steps: + * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) + * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) + * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) + */ +static int ocfs2_xattr_set_in_bucket(struct inode *inode, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs) +{ + int ret, local = 1, bucket_empty = 0; + size_t value_len; + char *val = (char *)xi->value; + struct ocfs2_xattr_entry *xe = xs->here; + u32 name_hash = ocfs2_xattr_hash_by_name(inode, + xi->name_index, xi->name); + + if (!xs->not_found && !xe->xe_local) { + /* + * We need to truncate the xattr storage first. + * + * If both the old and new value are stored to + * outside block, we only need to truncate + * the storage and then set the value outside. + * + * If the new value should be stored within block, + * we should free all the outside block first and + * the modification to the xattr block will be done + * by following steps. + */ + if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) + value_len = xi->value_len; + else + value_len = 0; + + ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, + value_len); + if (ret) + goto out; + + if (value_len) + goto set_value_outside; + } + + value_len = xi->value_len; + /* So we have to handle the inside block change now. */ + if (value_len > OCFS2_XATTR_INLINE_SIZE) { + /* + * If the new value will be stored outside of block, + * initalize a new empty value root and insert it first. + */ + local = 0; + xi->value = &def_xv; + xi->value_len = OCFS2_XATTR_ROOT_SIZE; + } + + ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, + local, &bucket_empty); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (value_len > OCFS2_XATTR_INLINE_SIZE) { + /* allocate the space now for the outside block storage. */ + ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, + value_len); + if (ret) { + mlog_errno(ret); + goto out; + } + } else { + if (bucket_empty) + ret = ocfs2_xattr_bucket_shrink(inode, xi, + xs, name_hash); + goto out; + } + +set_value_outside: + ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len); +out: + return ret; +} + +/* check whether the xattr bucket is filled up with the same hash value. */ +static int ocfs2_check_xattr_bucket_collision(struct inode *inode, + struct buffer_head *header_bh) +{ + int ret = 0; + struct ocfs2_xattr_header *xh + (struct ocfs2_xattr_header *)header_bh->b_data; + u16 count = le16_to_cpu(xh->xh_count); + struct buffer_head *xe_bh = NULL; + struct ocfs2_xattr_entry *xe; + + xe = ocfs2_get_xe_in_bucket(inode, header_bh, &xe_bh, count - 1); + if (!xe) + return -EIO; + + if (xe->xe_name_hash == xh->xh_entries[0].xe_name_hash) { + mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " + "hash = %u\n", (unsigned long long)header_bh->b_blocknr, + le32_to_cpu(xe->xe_name_hash)); + ret = -ENOSPC; + } + + brelse(xe_bh); + return ret; +} + +static int ocfs2_xattr_set_entry_index_block(struct inode *inode, + struct ocfs2_xattr_info *xi, + struct ocfs2_xattr_search *xs) +{ + struct ocfs2_xattr_header *xh; + struct ocfs2_xattr_entry *xe; + u16 count, header_size, xh_offset; + int free, max_free, need, old; + size_t value_size = 0, name_len = strlen(xi->name); + size_t blocksize = inode->i_sb->s_blocksize; + int ret, allocation = 0, new_outside = 0; + + mlog_entry("Set xattr %s in xattr index block\n", xi->name); + +try_again: + xh = xs->header; + count = le16_to_cpu(xh->xh_count); + xh_offset = le16_to_cpu(xh->xh_offset); + header_size = sizeof(struct ocfs2_xattr_header) + + count * sizeof(struct ocfs2_xattr_entry); + free = xh_offset - header_size; + max_free = OCFS2_XATTR_BUCKET_SIZE - + le16_to_cpu(xh->xh_name_value_len) - header_size; + + if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { + new_outside = 1; + value_size = OCFS2_XATTR_ROOT_SIZE; + } else if (xi->value) + value_size = OCFS2_XATTR_SIZE(xi->value_len); + + if (xs->not_found) { + need = sizeof(struct ocfs2_xattr_entry) + + OCFS2_XATTR_SIZE(name_len) + value_size; + /* We have to handle xattr value root alignment. */ + if (new_outside && + xh_offset % blocksize < OCFS2_XATTR_ROOT_SIZE) + free -= xh_offset % blocksize; + } else { + need = value_size + OCFS2_XATTR_SIZE(name_len);; + + /* + * We only replace the old value if the new length is smaller + * than the old one. Otherwise we will allocate new space in the + * bucket to store it. + * + * If the new value will be stored outside and the old value + * is an in-bucket xattr, there are some cases that old space + * isn't suitable(e.g, the space is cross-block and the new + * xattr value root can't be stored in the same block), + * so calculate "need" in this case. + */ + xe = xs->here; + if (xe->xe_local) + old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); + else + old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); + + if (old >= value_size && (!new_outside || !xe->xe_local)) + need = 0; + } + + mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " + "need = %d, max_free = %d, xh_offset = %u, xh_name_value_len = %u" + "\n", xs->not_found, (unsigned long long)xs->header_bh->b_blocknr, + free, need, max_free, le16_to_cpu(xh->xh_offset), + le16_to_cpu(xh->xh_name_value_len)); + + if (free < need) { + if (need <= max_free) { + /* + * We can create the space by defragment. Since only the + * name/value will be moved, the xe shouldn't be changed + * in xs. + */ + ret = ocfs2_defrag_xattr_bucket(inode, xs->header_bh, + xs->base, &free); + if (ret) { + mlog_errno(ret); + goto out; + } + + if (free >= need) + goto xattr_set; + + mlog(0, "Can't get enough space for xattr insert by " + "defragment. Need %u bytes, but we have %d, so " + "allocate new bucket for it.\n", need, free); + } + + /* + * We have to add new buckets or clusters and one + * allocation should leave us enough space for insert. + */ + BUG_ON(allocation); + + /* + * We do not allow for overlapping ranges between buckets. And + * the maximum number of collisions we will allow for then is + * one bucket's worth, so check it here whether we need to + * add a new bucket for the insert. + */ + ret = ocfs2_check_xattr_bucket_collision(inode, xs->header_bh); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_add_new_xattr_bucket(inode, + xs->xattr_bh, + xs->header_bh); + if (ret) { + mlog_errno(ret); + goto out; + } + + brelse(xs->header_bh); + xs->header_bh = NULL; + if (xs->alloc_base) { + kfree(xs->base); + xs->base = NULL; + xs->alloc_base = 0; + } + ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, + xi->name_index, + xi->name, xs); + if (ret && ret != -ENODATA) + goto out; + xs->not_found = ret; + allocation = 1; + goto try_again; + } + +xattr_set: + ret = ocfs2_xattr_set_in_bucket(inode, xi, xs); +out: + mlog_exit(ret); + return ret; +} -- 1.5.4.GIT
Tao Ma
2008-Jun-27 07:03 UTC
[Ocfs2-devel] [PATCH 14/15] Delete all xattr buckets in inode removal.v2
In inode removal, we need to iterate all the buckets, remove all the outside value storage for individual xattr and delete the xattr buckets. Signed-off-by: Tao Ma <tao.ma at oracle.com> --- fs/ocfs2/xattr.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 82 insertions(+), 4 deletions(-) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 89c8c16..4d1a895 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -140,6 +140,9 @@ static int ocfs2_xattr_set_entry_index_block(struct inode *inode, struct ocfs2_xattr_info *xi, struct ocfs2_xattr_search *xs); +static int ocfs2_delete_xattr_index_block(struct inode *inode, + struct buffer_head *xb_bh); + static inline struct xattr_handler *ocfs2_xattr_handler(int name_index) { struct xattr_handler *handler = NULL; @@ -1456,13 +1459,14 @@ static int ocfs2_xattr_block_remove(struct inode *inode, struct buffer_head *blk_bh) { struct ocfs2_xattr_block *xb; - struct ocfs2_xattr_header *header; int ret = 0; xb = (struct ocfs2_xattr_block *)blk_bh->b_data; - header = &(xb->xb_attrs.xb_header); - - ret = ocfs2_remove_value_outside(inode, blk_bh, header); + if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { + struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header); + ret = ocfs2_remove_value_outside(inode, blk_bh, header); + } else + ret = ocfs2_delete_xattr_index_block(inode, blk_bh); return ret; } @@ -4802,3 +4806,77 @@ out: mlog_exit(ret); return ret; } + +static int ocfs2_delete_xattr_in_bucket(struct inode *inode, + struct buffer_head *header_bh, + struct ocfs2_xattr_header *xh, + void *para) +{ + int ret = 0; + u16 i, count = le16_to_cpu(xh->xh_count); + struct ocfs2_xattr_entry *xe; + + for (i = 0; i < count; i++) { + xe = &xh->xh_entries[i]; + if (xe->xe_local) + continue; + + ret = ocfs2_xattr_bucket_value_truncate(inode, + header_bh, + i, 0, + NULL, + NULL); + if (ret) { + mlog_errno(ret); + break; + } + } + + return ret; +} + +static int ocfs2_delete_xattr_index_block(struct inode *inode, + struct buffer_head *xb_bh) +{ + struct ocfs2_xattr_block *xb + (struct ocfs2_xattr_block *)xb_bh->b_data; + struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; + int ret = 0; + u32 name_hash = UINT_MAX, e_cpos, num_clusters; + u64 p_blkno; + + if (le16_to_cpu(el->l_next_free_rec) == 0) + return 0; + + while (name_hash > 0) { + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, + &e_cpos, &num_clusters, el); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, + ocfs2_delete_xattr_in_bucket, + NULL); + if (ret) { + mlog_errno(ret); + goto out; + } + + ret = ocfs2_rm_xattr_cluster(inode, xb_bh, + p_blkno, e_cpos, num_clusters); + if (ret) { + mlog_errno(ret); + break; + } + + if (e_cpos == 0) + break; + + name_hash = e_cpos - 1; + } + +out: + return ret; +} -- 1.5.4.GIT
Tiger Yang
2008-Jun-27 07:27 UTC
[Ocfs2-devel] [PATCH 07/15] ocfs2: reserve inline space for extended attribute v2
This patch reserve some space in inode block for extended attribute. That space used to store extent list or inline data. Signed-off-by: Tiger Yang <tiger.yang at oracle.com> --- fs/ocfs2/alloc.c | 31 +++++++++++++++++++++++-------- fs/ocfs2/ocfs2.h | 3 +++ fs/ocfs2/ocfs2_fs.h | 36 ++++++++++++++++++++++++++++++++++-- fs/ocfs2/super.c | 2 ++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c index 3a06271..2df5a7f 100644 --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -6417,26 +6417,40 @@ out: return ret; } -static void ocfs2_zero_dinode_id2(struct inode *inode, struct ocfs2_dinode *di) +static void ocfs2_zero_dinode_id2_with_xattr(struct super_block *sb, + struct ocfs2_dinode *di, + int xattrsize) { - unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; - - memset(&di->id2, 0, blocksize - offsetof(struct ocfs2_dinode, id2)); + if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) + memset(&di->id2, 0, sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2) - + xattrsize); + else + memset(&di->id2, 0, sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2)); } void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di) { - ocfs2_zero_dinode_id2(inode, di); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + int xattrsize = osb->s_xattr_inline_size; + int recs = ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, + di, + xattrsize); + + ocfs2_zero_dinode_id2_with_xattr(inode->i_sb, di, xattrsize); di->id2.i_list.l_tree_depth = 0; di->id2.i_list.l_next_free_rec = 0; - di->id2.i_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(inode->i_sb)); + di->id2.i_list.l_count = cpu_to_le16(recs); } void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) { struct ocfs2_inode_info *oi = OCFS2_I(inode); struct ocfs2_inline_data *idata = &di->id2.i_data; + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + int xattrsize = osb->s_xattr_inline_size; spin_lock(&oi->ip_lock); oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; @@ -6447,9 +6461,10 @@ void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) * We clear the entire i_data structure here so that all * fields can be properly initialized. */ - ocfs2_zero_dinode_id2(inode, di); + ocfs2_zero_dinode_id2_with_xattr(inode->i_sb, di, xattrsize); - idata->id_count = cpu_to_le16(ocfs2_max_inline_data(inode->i_sb)); + idata->id_count = cpu_to_le16( + ocfs2_max_inline_data_with_xattr(inode->i_sb, di, xattrsize)); } int ocfs2_convert_inline_data_to_extents(struct inode *inode, diff --git a/fs/ocfs2/ocfs2.h b/fs/ocfs2/ocfs2.h index 3169237..5bf04ef 100644 --- a/fs/ocfs2/ocfs2.h +++ b/fs/ocfs2/ocfs2.h @@ -178,6 +178,8 @@ enum ocfs2_mount_options #define OCFS2_OSB_HARD_RO 0x0002 #define OCFS2_OSB_ERROR_FS 0x0004 #define OCFS2_DEFAULT_ATIME_QUANTUM 60 +/* Inline extended attribute size (in bytes) */ +#define OCFS2_MIN_XATTR_INLINE_SIZE 256 struct ocfs2_journal; struct ocfs2_slot_info; @@ -227,6 +229,7 @@ struct ocfs2_super int s_sectsize_bits; int s_clustersize; int s_clustersize_bits; + int s_xattr_inline_size; atomic_t vol_state; struct mutex recovery_lock; diff --git a/fs/ocfs2/ocfs2_fs.h b/fs/ocfs2/ocfs2_fs.h index 6bbeef4..b64c160 100644 --- a/fs/ocfs2/ocfs2_fs.h +++ b/fs/ocfs2/ocfs2_fs.h @@ -641,11 +641,12 @@ struct ocfs2_dinode { __le32 i_atime_nsec; __le32 i_ctime_nsec; __le32 i_mtime_nsec; - __le32 i_attr; +/*70*/ __le32 i_attr; __le16 i_orphaned_slot; /* Only valid when OCFS2_ORPHANED_FL was set in i_flags */ __le16 i_dyn_features; -/*70*/ __le64 i_reserved2[8]; + __le64 i_xattr_loc; +/*80*/ __le64 i_reserved2[7]; /*B8*/ union { __le64 i_pad1; /* Generic way to refer to this 64bit union */ @@ -780,6 +781,19 @@ static inline int ocfs2_max_inline_data(struct super_block *sb) offsetof(struct ocfs2_dinode, id2.i_data.id_data); } +static inline int ocfs2_max_inline_data_with_xattr(struct super_block *sb, + struct ocfs2_dinode *di, + int xattrsize) +{ + if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) + return sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2.i_data.id_data) - + xattrsize; + else + return sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2.i_data.id_data); +} + static inline int ocfs2_extent_recs_per_inode(struct super_block *sb) { int size; @@ -790,6 +804,24 @@ static inline int ocfs2_extent_recs_per_inode(struct super_block *sb) return size / sizeof(struct ocfs2_extent_rec); } +static inline int ocfs2_extent_recs_per_inode_with_xattr( + struct super_block *sb, + struct ocfs2_dinode *di, + int xattrsize) +{ + int size; + + if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) + size = sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2.i_list.l_recs) - + xattrsize; + else + size = sb->s_blocksize - + offsetof(struct ocfs2_dinode, id2.i_list.l_recs); + + return size / sizeof(struct ocfs2_extent_rec); +} + static inline int ocfs2_chain_recs_per_inode(struct super_block *sb) { int size; diff --git a/fs/ocfs2/super.c b/fs/ocfs2/super.c index df63ba2..c0a1cdf 100644 --- a/fs/ocfs2/super.c +++ b/fs/ocfs2/super.c @@ -1421,6 +1421,8 @@ static int ocfs2_initialize_super(struct super_block *sb, osb->slot_num = OCFS2_INVALID_SLOT; + osb->s_xattr_inline_size = OCFS2_MIN_XATTR_INLINE_SIZE; + osb->local_alloc_state = OCFS2_LA_UNUSED; osb->local_alloc_bh = NULL; -- 1.5.4.4
Mark Fasheh
2008-Jul-09 23:10 UTC
[Ocfs2-devel] [PATCH 06/15] Add extent tree operation for xattr value.v2
On Fri, Jun 27, 2008 at 03:01:27PM +0800, Tao Ma wrote:> In Mark's review, he said "wouldn't it make sense to have a couple high-level > "ocfs2_foo_insert_extent" functions whcih build up anm ocfs2_extent_tree and > then pass it down to the common ocfs2_insert_extent?". But in this patch, I > still don't remove the "private" from the parameter. because there are too > many functions use "private". So if we use an "ocfs2_extent_tree" in > ocfs2_insert_extent, we should also modify ocfs2_lock_allocators, > ocfs2_num_free_extents etc. They are spread widely in ocfs2 source code, and I > don't want to let ocfs2_extent_tree known by every caller since it should be > totally limited to the tree code itself. Mark, any suggestions here?I kind of liked what you had come to already: http://oss.oracle.com/pipermail/ocfs2-devel/2008-June/002332.html I only meant that we should have some thin wrappers around ocfs2_insert_extent. We can leave the other functions as-is for now until we have time to figure out how to make it nicer. Does that make sense to you? --Mark -- Mark Fasheh
Mark Fasheh
2008-Jul-10 23:15 UTC
[Ocfs2-devel] [PATCH 09/15] Add helper function in uptodate for removing xattr clusters.v2
On Fri, Jun 27, 2008 at 03:01:49PM +0800, Tao Ma wrote:> diff --git a/fs/ocfs2/uptodate.c b/fs/ocfs2/uptodate.c > index 4da8851..7345d1c 100644 > --- a/fs/ocfs2/uptodate.c > +++ b/fs/ocfs2/uptodate.c > @@ -511,14 +511,10 @@ static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, > ci->ci_num_cached--; > } > > -/* Called when we remove a chunk of metadata from an inode. We don't > - * bother reverting things to an inlined array in the case of a remove > - * which moves us back under the limit. */ > -void ocfs2_remove_from_cache(struct inode *inode, > - struct buffer_head *bh) > +void ocfs2_remove_block_from_cache(struct inode *inode, > + sector_t block)This new function should be static. --Mark -- Mark Fasheh
Mark Fasheh
2008-Jul-10 23:19 UTC
[Ocfs2-devel] [PATCH 09/15] Add helper function in uptodate for removing xattr clusters.v2
On Fri, Jun 27, 2008 at 03:01:49PM +0800, Tao Ma wrote:> diff --git a/fs/ocfs2/uptodate.c b/fs/ocfs2/uptodate.c > index 4da8851..7345d1c 100644 > --- a/fs/ocfs2/uptodate.c > +++ b/fs/ocfs2/uptodate.c > @@ -511,14 +511,10 @@ static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, > ci->ci_num_cached--; > } > > -/* Called when we remove a chunk of metadata from an inode. We don't > - * bother reverting things to an inlined array in the case of a remove > - * which moves us back under the limit. */ > -void ocfs2_remove_from_cache(struct inode *inode, > - struct buffer_head *bh) > +void ocfs2_remove_block_from_cache(struct inode *inode, > + sector_t block)This new function should be static. --Mark -- Mark Fasheh
Mark Fasheh
2008-Jul-11 23:59 UTC
[Ocfs2-devel] [PATCH 12/15] Add xattr find process for xattr index btree.v2
On Fri, Jun 27, 2008 at 03:02:35PM +0800, Tao Ma wrote:> +static int ocfs2_cp_xattr_bucket_to_buffer(struct inode *inode, > + u64 blkno, > + char *buffer) > +{ > + int i, ret, block_num = ocfs2_blocks_per_xattr_bucket(inode->i_sb); > + int blocksize = inode->i_sb->s_blocksize; > + struct buffer_head **bhs = NULL; > + char *target; > + > + bhs = kzalloc(sizeof(struct buffer_head *) * block_num, GFP_NOFS); > + ret = ocfs2_read_xattr_bucket(inode, blkno, bhs, 0); > + if (ret) > + goto out; > + > + target = buffer; > + for (i = 0; i < block_num; i++, target += blocksize) > + memcpy(target, bhs[i]->b_data, blocksize); > + > +out: > + if (bhs) { > + for (i = 0; i < block_num; i++) > + brelse(bhs[i]); > + kfree(bhs); > + } > + return ret; > +} > + > +/* > + * Find the specided xattr entry in a series of buckets. > + * This series start from p_blkno and last for num_clusters. > + * The ocfs2_xattr_header.xh_reserved1 of the first bucket contains > + * the num of the valid buckets. > + * > + * Return the buffer_head this xattr should reside in. And if the xattr's > + * hash is in the gap of 2 buckets, return the lower bucket. > + */ > +static int ocfs2_xattr_bucket_find(struct inode *inode, > + int name_index, > + const char *name, > + u32 name_hash, > + u64 p_blkno, > + u32 first_hash, > + u32 num_clusters, > + struct ocfs2_xattr_search *xs) > +{ > + int ret, found = 0; > + struct buffer_head *bh = NULL; > + struct buffer_head *last_bh = NULL; > + struct buffer_head *lower_bh = NULL; > + struct ocfs2_xattr_header *xh = NULL; > + struct ocfs2_xattr_entry *xe = NULL; > + u16 xh_count, xe_index = 0; > + u16 block_in_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); > + int low_bucket = 0, bucket, high_bucket; > + int blocksize = inode->i_sb->s_blocksize; > + u32 last_hash; > + u64 blkno; > + > + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, > + &bh, OCFS2_BH_CACHED, inode); > + if (ret) > + goto out; > + xh = (struct ocfs2_xattr_header *)bh->b_data; > + high_bucket = le16_to_cpu(xh->xh_reserved1) - 1; > + > + while (low_bucket <= high_bucket) { > + brelse(bh); > + bh = last_bh = NULL; > + bucket = (low_bucket + high_bucket) / 2; > + > + blkno = p_blkno + bucket * block_in_bucket; > + > + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, > + &bh, OCFS2_BH_CACHED, inode); > + if (ret) { > + mlog_errno(ret); > + goto out; > + } > + > + xh = (struct ocfs2_xattr_header *)bh->b_data; > + xe = &xh->xh_entries[0]; > + if (name_hash < le32_to_cpu(xe->xe_name_hash)) { > + high_bucket = bucket - 1; > + continue; > + }This function looks like it's doing a lot of random I/O. What about sucking up some large numbers of (contiguous) blocks with a readahead request before going into this function? The beauty of how our readhead works is that you wouldn't have to change a single line of code here... -- Mark Fasheh
Mark Fasheh
2008-Jul-17 00:30 UTC
[Ocfs2-devel] [PATCH 05/15] Add xattr header in ocfs2.v2
On Fri, Jun 27, 2008 at 03:01:10PM +0800, Tao Ma wrote:> Modification from V1 to V2: > Add EA disk layout to ocfs2_fs.h.By the way - I just realized that none of these structures have per-field annotations. We absolutely need those. --Mark -- Mark Fasheh
Mark Fasheh
2008-Jul-18 00:30 UTC
[Ocfs2-devel] [PATCH 13/15] Enable xattr set in index btree.v2
On Fri, Jun 27, 2008 at 03:02:54PM +0800, Tao Ma wrote:> +static int ocfs2_xattr_create_index_block(struct inode *inode, > + struct ocfs2_xattr_search *xs) > +{ > + int ret, credits = OCFS2_SUBALLOC_ALLOC; > + u32 bit_off, len; > + u64 blkno; > + handle_t *handle; > + struct super_block *sb = inode->i_sb; > + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); > + struct ocfs2_inode_info *oi = OCFS2_I(inode); > + struct ocfs2_alloc_context *data_ac; > + struct buffer_head *xh_bh = NULL, *data_bh = NULL; > + struct buffer_head *xb_bh = xs->xattr_bh; > + struct ocfs2_xattr_block *xb > + (struct ocfs2_xattr_block *)xb_bh->b_data; > + struct ocfs2_xattr_tree_root *xr; > + u16 xb_flags = le16_to_cpu(xb->xb_flags); > + u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb); > + > + mlog(0, "create xattr index block for %llu\n", > + (unsigned long long)xb_bh->b_blocknr); > + > + BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); > + > + ret = ocfs2_reserve_clusters(osb, 1, &data_ac); > + if (ret) { > + mlog_errno(ret); > + goto out; > + } > + > + /* > + * XXX: > + * Do we need this lock or should we use a new sem in xattr allocation? > + */ > + down_write(&oi->ip_alloc_sem);We can use this for now, and move to a dedicated mutex if performance becomes a problem later. You should update the comment to say this :)> + /* > + * 3 more credits, one for xattr block update, one for the 1st block > + * of the new xattr bucket and one for the value/data. > + */ > + credits += 3; > + handle = ocfs2_start_trans(osb, credits); > + if (IS_ERR(handle)) { > + ret = PTR_ERR(handle); > + mlog_errno(ret); > + goto out_sem; > + } > + > + ret = ocfs2_journal_access(handle, inode, xb_bh, > + OCFS2_JOURNAL_ACCESS_CREATE); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + } > + > + ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + } > + > + /* > + * The bucket may spread in many blocks, and > + * we will only touch the 1st block and the last block > + * in the whole bucket(one for entry and one for data. > + */ > + blkno = ocfs2_clusters_to_blocks(sb, bit_off); > + > + mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno); > + > + ret = ocfs2_read_block(osb, blkno, &xh_bh, > + OCFS2_BH_CACHED, inode); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + }Ok, you're still reading in newly allocated blocks. Don't do this - you're doing I/O for nothing. Instead, you want to use sb_getblk() and ocfs2_set_new_buffer_uptodate(), like we do elsewhere. This goes for all parts of the newly allocated cluster you'll be updating in this function. If we know that the disk block does not have valid data or that we will be replacing an entire block (as opposed to just changing part of it), then it's better to allocate the buffer with sb_getblk() and just force it's up to date state with ocfs2_set_new_buffer_uptodate().> + > + ret = ocfs2_journal_access(handle, inode, xh_bh, > + OCFS2_JOURNAL_ACCESS_CREATE); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + } > + > + if (bpb > 1) { > + ret = ocfs2_read_block(osb, blkno + bpb - 1, &data_bh, > + OCFS2_BH_CACHED, inode); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + } > + > + ret = ocfs2_journal_access(handle, inode, data_bh, > + OCFS2_JOURNAL_ACCESS_CREATE); > + if (ret) { > + mlog_errno(ret); > + goto out_commit; > + } > + } > + > + ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh); > + > + ocfs2_journal_dirty(handle, xh_bh); > + if (data_bh) > + ocfs2_journal_dirty(handle, data_bh); > + > + ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh); > + > + /* Re-initalize the xattr block. */ > + xr = &xb->xb_attrs.xb_root; > + memset(xr, 0, sizeof(struct ocfs2_xattr_tree_root)); > + xr->xt_clusters = cpu_to_le32(1); > + xr->xt_last_eb_blk = 0; > + xr->xt_list.l_tree_depth = 0; > + xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); > + xr->xt_list.l_next_free_rec = cpu_to_le16(1); > + > + memset(xr->xt_list.l_recs, 0, sizeof(struct ocfs2_extent_rec));Why not just memset the entire block and re-initialize it? That way, all unused space is zero'd for us from the start. By the way, were the other newly allocated blocks cleared too? This is extremely important. If we are not initializing ALL unused space to zero, than we will _never_ be able to expand these structures in the future.> +/* > + * Add a new cluster for xattr storage. > + * > + * If the new cluster is contiguous with the previous one, it will be > + * appended to the same extent record, and num_clusters will be updated. > + * > + * If not, we will insert a new extent for it and move some xattrs in > + * the last cluster into the new allocated one. > + * first_bh is the first block of the previous extent rec and header_bh > + * indicates the bucket we will insert the new xattrs. They will be updated > + * when the header_bh is moved into the new cluster. > + */ > +static int ocfs2_add_new_xattr_cluster(struct inode *inode, > + struct buffer_head *root_bh, > + struct buffer_head **first_bh, > + struct buffer_head **header_bh, > + u32 *num_clusters, > + u32 prev_cpos, > + u64 prev_blkno, > + int *extend) > +{ > + int ret, credits; > + u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); > + u32 prev_clusters = *num_clusters; > + u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; > + u64 block; > + handle_t *handle = NULL; > + struct buffer_head *new_bh = NULL; > + struct ocfs2_alloc_context *data_ac = NULL; > + struct ocfs2_alloc_context *meta_ac = NULL; > + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); > + struct ocfs2_xattr_header *first_xh > + (struct ocfs2_xattr_header *)(*first_bh)->b_data; > + struct ocfs2_xattr_block *xb > + (struct ocfs2_xattr_block *)root_bh->b_data; > + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; > + struct ocfs2_extent_list *root_el = &xb_root->xt_list; > + enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT; > + > + mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " > + "previous xattr blkno = %llu\n", > + (unsigned long long)OCFS2_I(inode)->ip_blkno, > + prev_cpos, prev_blkno); > + > + ret = ocfs2_lock_allocators(inode, root_bh, root_el, > + clusters_to_add, 0, &data_ac, > + &meta_ac, type, NULL); > + if (ret) { > + mlog_errno(ret); > + goto leave; > + } > + > + credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add); > + handle = ocfs2_start_trans(osb, credits); > + if (IS_ERR(handle)) { > + ret = PTR_ERR(handle); > + handle = NULL; > + mlog_errno(ret); > + goto leave; > + } > + > + ret = ocfs2_journal_access(handle, inode, root_bh, > + OCFS2_JOURNAL_ACCESS_WRITE); > + if (ret < 0) { > + mlog_errno(ret); > + goto leave; > + } > + > + ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, > + clusters_to_add, &bit_off, &num_bits); > + if (ret < 0) { > + if (ret != -ENOSPC) > + mlog_errno(ret); > + goto leave; > + } > + > + BUG_ON(num_bits > clusters_to_add); > + > + block = ocfs2_clusters_to_blocks(osb->sb, bit_off); > + mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", > + num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); > + > + if (prev_blkno + prev_clusters * bpc == block && > + le16_to_cpu(first_xh->xh_reserved1) + > + num_bits * ocfs2_xattr_buckets_per_cluster(osb) > > + le16_to_cpu(first_xh->xh_reserved1)) { > + /* > + * If this cluster is contiguous with the old one and > + * adding this new cluster, we don't surpass the limit of > + * xh_reserved1, cool. We will let it be initialized > + * and used like other buckets in the previous cluster. > + * So add it as a contiguous one. The caller will handle > + * its init process. > + */We also need to limit the maximum size of a btree leaf, otherwise we'll lose the benefits of hashing because we'll have to search large leaves. What the maximum size is, I'm not sure of. I think maybe 64k (or clustersize, if it's bigger) might be a good start. Btw, this means that if a leaf gets too big, you might have to defeat the btree record merging code :) So, basically if the leave is below a certain size, you allow it to be merged. Above that size though, and we actually want a new record. I think it should be pretty easy though to tell ocfs2_insert_extent to not attempt merging an extent, even if it's contiguous. --Mark -- Mark Fasheh