On Tue, Apr 30, 2024 at 02:49:21PM +0200, Christoph Hellwig wrote: > Add a common helper for the calculation of the overhead when converting > a shortform to block format directory. > > Signed-off-by: Christoph Hellwig <hch@xxxxxx> > --- > fs/xfs/libxfs/xfs_dir2_block.c | 4 ++-- > fs/xfs/libxfs/xfs_dir2_priv.h | 12 ++++++++++++ > fs/xfs/libxfs/xfs_dir2_sf.c | 12 ++++-------- > 3 files changed, 18 insertions(+), 10 deletions(-) > > diff --git a/fs/xfs/libxfs/xfs_dir2_block.c b/fs/xfs/libxfs/xfs_dir2_block.c > index 378d3aefdd9ced..a19744cb43ca0c 100644 > --- a/fs/xfs/libxfs/xfs_dir2_block.c > +++ b/fs/xfs/libxfs/xfs_dir2_block.c > @@ -1109,8 +1109,8 @@ xfs_dir2_sf_to_block( > /* > * Compute size of block "tail" area. > */ > - i = (uint)sizeof(*btp) + > - (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); > + i = xfs_dir2_block_overhead(sfp->count); > + > /* > * The whole thing is initialized to free by the init routine. > * Say we're using the leaf and tail area. > diff --git a/fs/xfs/libxfs/xfs_dir2_priv.h b/fs/xfs/libxfs/xfs_dir2_priv.h > index 1e4401f9ec936e..bfbc73251f275a 100644 > --- a/fs/xfs/libxfs/xfs_dir2_priv.h > +++ b/fs/xfs/libxfs/xfs_dir2_priv.h > @@ -205,4 +205,16 @@ xfs_dahash_t xfs_dir2_hashname(struct xfs_mount *mp, > enum xfs_dacmp xfs_dir2_compname(struct xfs_da_args *args, > const unsigned char *name, int len); > > +/* > + * Overhead if we converted a shortform directory to block format. > + * > + * The extra two entries are because "." and ".." don't have real entries in > + * the shortform format. > + */ > +static inline unsigned int xfs_dir2_block_overhead(unsigned int count) > +{ > + return (count + 2) * sizeof(struct xfs_dir2_leaf_entry) + > + sizeof(struct xfs_dir2_block_tail); I could've sworn there's a helper to compute this, but as I cannot find it I guess I'll let that go. Reviewed-by: Darrick J. Wong <djwong@xxxxxxxxxx> --D > +} > + > #endif /* __XFS_DIR2_PRIV_H__ */ > diff --git a/fs/xfs/libxfs/xfs_dir2_sf.c b/fs/xfs/libxfs/xfs_dir2_sf.c > index 21e04594606b89..1e1dcdf83b8f95 100644 > --- a/fs/xfs/libxfs/xfs_dir2_sf.c > +++ b/fs/xfs/libxfs/xfs_dir2_sf.c > @@ -629,9 +629,7 @@ xfs_dir2_sf_addname_pick( > * Calculate data bytes used excluding the new entry, if this > * was a data block (block form directory). > */ > - used = offset + > - (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) + > - (uint)sizeof(xfs_dir2_block_tail_t); > + used = offset + xfs_dir2_block_overhead(sfp->count + 1); > /* > * If it won't fit in a block form then we can't insert it, > * we'll go back, convert to block, then try the insert and convert > @@ -691,9 +689,7 @@ xfs_dir2_sf_check( > } > ASSERT(i8count == sfp->i8count); > ASSERT((char *)sfep - (char *)sfp == dp->i_disk_size); > - ASSERT(offset + > - (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) + > - (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize); > + ASSERT(offset + xfs_dir2_block_overhead(sfp->count)); > } > #endif /* DEBUG */ > > @@ -782,8 +778,8 @@ xfs_dir2_sf_verify( > return __this_address; > > /* Make sure this whole thing ought to be in local format. */ > - if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) + > - (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize) > + if (offset + xfs_dir2_block_overhead(sfp->count) > > + mp->m_dir_geo->blksize) > return __this_address; > > return NULL; > -- > 2.39.2 > >