From aff88a37b29e404e62269d64f7ab10e7966a693b Mon Sep 17 00:00:00 2001 From: Ryusuke Konishi Date: Wed, 28 Sep 2016 20:24:12 +0900 Subject: [PATCH] segment.c: add helper to calculate summary size for each file Add a function to calculate the size of finfo and binfo structures that a given file occupies within segment summary information, where we make the helper separating the logic from nilfs_file_next(). Signed-off-by: Ryusuke Konishi --- lib/segment.c | 75 ++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/lib/segment.c b/lib/segment.c index e473402..8473abe 100644 --- a/lib/segment.c +++ b/lib/segment.c @@ -149,6 +149,46 @@ static void nilfs_file_adjust_finfo_position(struct nilfs_file *file, } } +static size_t nilfs_binfo_total_size(size_t offset, __u32 blksize, + unsigned int binfosize, __u32 blkcnt) +{ + __u32 rest, binfo_per_block; + + rest = blksize - (offset & (blksize - 1)); + if (binfosize * blkcnt <= rest) + return binfosize * blkcnt; + + blkcnt -= rest / binfosize; + binfo_per_block = blksize / binfosize; + return rest + (size_t)(blkcnt / binfo_per_block) * blksize + + (blkcnt % binfo_per_block) * binfosize; +} + +static size_t nilfs_file_info_size(struct nilfs_file *file) +{ + const __u32 blksize = 1UL << file->psegment->blkbits; + unsigned int dsize, nsize; + __u32 nblocks, ndatablk; + size_t delta; + + if (file->use_real_blocknr) { + dsize = NILFS_BINFO_DAT_DATA_SIZE; + nsize = NILFS_BINFO_DAT_NODE_SIZE; + } else { + dsize = NILFS_BINFO_DATA_SIZE; + nsize = NILFS_BINFO_NODE_SIZE; + } + + nblocks = le32_to_cpu(file->finfo->fi_nblocks); + ndatablk = le32_to_cpu(file->finfo->fi_ndatablk); + delta = sizeof(struct nilfs_finfo); + delta += nilfs_binfo_total_size(file->offset + delta, blksize, dsize, + ndatablk); + delta += nilfs_binfo_total_size(file->offset + delta, blksize, nsize, + nblocks - ndatablk); + return delta; +} + void nilfs_file_init(struct nilfs_file *file, const struct nilfs_psegment *pseg) { @@ -175,45 +215,14 @@ int nilfs_file_is_end(struct nilfs_file *file) return file->index >= file->nfinfo; } -static size_t nilfs_binfo_total_size(size_t offset, __u32 blksize, - unsigned int binfosize, __u32 blkcnt) -{ - __u32 rest, binfo_per_block; - - rest = blksize - (offset & (blksize - 1)); - if (binfosize * blkcnt <= rest) - return binfosize * blkcnt; - - blkcnt -= rest / binfosize; - binfo_per_block = blksize / binfosize; - return rest + (size_t)(blkcnt / binfo_per_block) * blksize + - (blkcnt % binfo_per_block) * binfosize; -} - void nilfs_file_next(struct nilfs_file *file) { const __u32 blksize = 1UL << file->psegment->blkbits; - const __u32 nblocks = le32_to_cpu(file->finfo->fi_nblocks); - unsigned int dsize, nsize; - __u32 ndatablk; size_t delta; - file->blocknr += nblocks; + file->blocknr += le32_to_cpu(file->finfo->fi_nblocks); - if (file->use_real_blocknr) { - dsize = NILFS_BINFO_DAT_DATA_SIZE; - nsize = NILFS_BINFO_DAT_NODE_SIZE; - } else { - dsize = NILFS_BINFO_DATA_SIZE; - nsize = NILFS_BINFO_NODE_SIZE; - } - - ndatablk = le32_to_cpu(file->finfo->fi_ndatablk); - delta = sizeof(struct nilfs_finfo); - delta += nilfs_binfo_total_size(file->offset + delta, - blksize, dsize, ndatablk); - delta += nilfs_binfo_total_size(file->offset + delta, - blksize, nsize, nblocks - ndatablk); + delta = nilfs_file_info_size(file); file->offset += delta; file->finfo = (void *)file->finfo + delta;