--- fs/ext4/ext4.h | 6 +++++ fs/ext4/ext4_extents.h | 10 +++++++++ fs/ext4/ext4_jbd2.h | 3 ++ fs/ext4/extents.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/super.c | 12 +++++++++++ 5 files changed, 81 insertions(+) --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1826,6 +1829,9 @@ extern void ext4_add_groupblocks(handle_ ext4_fsblk_t block, unsigned long count); extern int ext4_trim_fs(struct super_block *, struct fstrim_range *); +extern void ext4_mb_discard_inode_preallocations(struct inode *); + + /* inode.c */ struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int, int *); --- a/fs/ext4/ext4_extents.h +++ b/fs/ext4/ext4_extents.h @@ -58,6 +58,13 @@ */ #define EXT_STATS_ +/* + * define EXT4_ALLOC_NEEDED to 0 since block bitmap, group desc. and sb + * are now accounted in ext4_ext_calc_credits_for_insert() + */ +#define EXT4_ALLOC_NEEDED 0 +#define HAVE_EXT_PREPARE_CB_EXTENT +#define HAVE_EXT4_EXT_PBLOCK /* * ext4_inode has i_block array (60 bytes total). @@ -241,6 +248,7 @@ static inline ext4_fsblk_t ext4_ext_pblo block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; return block; } +#define ext_pblock(ex) ext4_ext_pblock(ex) /* * ext4_idx_pblock: @@ -287,6 +295,8 @@ extern int ext4_extent_tree_init(handle_ extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int num, struct ext4_ext_path *path); +extern int ext4_ext_calc_credits_for_insert(struct inode *, + struct ext4_ext_path *); extern int ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, struct ext4_extent *ex2); --- a/fs/ext4/ext4_jbd2.h +++ b/fs/ext4/ext4_jbd2.h @@ -175,6 +177,7 @@ static inline void ext4_journal_callback list_del_init(&jce->jce_list); spin_unlock(&sbi->s_md_lock); } +#define HAVE_EXT4_JOURNAL_CALLBACK_ADD int ext4_mark_iloc_dirty(handle_t *handle, --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -2205,6 +2205,56 @@ int ext4_ext_calc_credits_for_single_ext } /* + * This routine returns max. credits extent tree can consume. + * It should be OK for low-performance paths like ->writepage() + * To allow many writing process to fit a single transaction, + * caller should calculate credits under truncate_mutex and + * pass actual path. + */ +int ext4_ext_calc_credits_for_insert(struct inode *inode, + struct ext4_ext_path *path) +{ + int depth, needed; + + if (path) { + /* probably there is space in leaf? */ + depth = path->p_depth; + if (le16_to_cpu(path[depth].p_hdr->eh_entries) + < le16_to_cpu(path[depth].p_hdr->eh_max)) + return 1; + } + + /* + * given 32bit logical block (4294967296 blocks), max. tree + * can be 4 levels in depth -- 4 * 340^4 == 53453440000. + * let's also add one more level for imbalance. + */ + depth = 5; + + /* allocation of new data block(s) */ + needed = 2; + + /* + * tree can be full, so it'd need to grow in depth: + * we need one credit to modify old root, credits for + * new root will be added in split accounting + */ + needed += 1; + /* + * Index split can happen, we'd need: + * allocate intermediate indexes (bitmap + group) + * + change two blocks at each level, but root (already included) + */ + needed += (depth * 2) + (depth * 2); + + /* any allocation modifies superblock */ + needed += 1; + + return needed; +} +EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert); + +/* * How many index/leaf blocks need to change/allocate to modify nrblocks? * * if nrblocks are fit in a single extent (chunk flag is 1), then