The super_block fields s_blocksize and s_blocksize_bits always
reflect the actual configured blocksize for a filesystem.

Use these in all calculations where blocksize is required.
This allows us to easily change the blocksize in a later patch.

Note that I cannot determine what happens if !CONFIG_BLOCK, as
I have no access to such a system. Out of an abundance of caution,
I have left all !CONFIG_BLOCK codepaths in their original state.

Signed-off-by: Sven Van Asbroeck <[email protected]>
---
 fs/romfs/storage.c | 25 +++++++++++++------------
 fs/romfs/super.c   |  9 ++++++++-
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/fs/romfs/storage.c b/fs/romfs/storage.c
index 6b2b4362089e..5e84efadac3f 100644
--- a/fs/romfs/storage.c
+++ b/fs/romfs/storage.c
@@ -109,9 +109,9 @@ static int romfs_blk_read(struct super_block *sb, unsigned 
long pos,
 
        /* copy the string up to blocksize bytes at a time */
        while (buflen > 0) {
-               offset = pos & (ROMBSIZE - 1);
-               segment = min_t(size_t, buflen, ROMBSIZE - offset);
-               bh = sb_bread(sb, pos >> ROMBSBITS);
+               offset = pos & (sb->s_blocksize - 1);
+               segment = min_t(size_t, buflen, sb->s_blocksize - offset);
+               bh = sb_bread(sb, pos >> sb->s_blocksize_bits);
                if (!bh)
                        return -EIO;
                memcpy(buf, bh->b_data + offset, segment);
@@ -138,9 +138,9 @@ static ssize_t romfs_blk_strnlen(struct super_block *sb,
 
        /* scan the string up to blocksize bytes at a time */
        while (limit > 0) {
-               offset = pos & (ROMBSIZE - 1);
-               segment = min_t(size_t, limit, ROMBSIZE - offset);
-               bh = sb_bread(sb, pos >> ROMBSBITS);
+               offset = pos & (sb->s_blocksize - 1);
+               segment = min_t(size_t, limit, sb->s_blocksize - offset);
+               bh = sb_bread(sb, pos >> sb->s_blocksize_bits);
                if (!bh)
                        return -EIO;
                buf = bh->b_data + offset;
@@ -170,9 +170,9 @@ static int romfs_blk_strcmp(struct super_block *sb, 
unsigned long pos,
 
        /* compare string up to a block at a time */
        while (size > 0) {
-               offset = pos & (ROMBSIZE - 1);
-               segment = min_t(size_t, size, ROMBSIZE - offset);
-               bh = sb_bread(sb, pos >> ROMBSBITS);
+               offset = pos & (sb->s_blocksize - 1);
+               segment = min_t(size_t, size, sb->s_blocksize - offset);
+               bh = sb_bread(sb, pos >> sb->s_blocksize_bits);
                if (!bh)
                        return -EIO;
                matched = (memcmp(bh->b_data + offset, str, segment) == 0);
@@ -180,7 +180,8 @@ static int romfs_blk_strcmp(struct super_block *sb, 
unsigned long pos,
                size -= segment;
                pos += segment;
                str += segment;
-               if (matched && size == 0 && offset + segment < ROMBSIZE) {
+               if (matched && size == 0 &&
+                               offset + segment < sb->s_blocksize) {
                        if (!bh->b_data[offset + segment])
                                terminated = true;
                        else
@@ -194,8 +195,8 @@ static int romfs_blk_strcmp(struct super_block *sb, 
unsigned long pos,
        if (!terminated) {
                /* the terminating NUL must be on the first byte of the next
                 * block */
-               BUG_ON((pos & (ROMBSIZE - 1)) != 0);
-               bh = sb_bread(sb, pos >> ROMBSBITS);
+               BUG_ON((pos & (sb->s_blocksize - 1)) != 0);
+               bh = sb_bread(sb, pos >> sb->s_blocksize_bits);
                if (!bh)
                        return -EIO;
                matched = !bh->b_data[0];
diff --git a/fs/romfs/super.c b/fs/romfs/super.c
index e582d001f792..6fecdea791f1 100644
--- a/fs/romfs/super.c
+++ b/fs/romfs/super.c
@@ -411,10 +411,17 @@ static int romfs_statfs(struct dentry *dentry, struct 
kstatfs *buf)
 
        buf->f_type = ROMFS_MAGIC;
        buf->f_namelen = ROMFS_MAXFN;
-       buf->f_bsize = ROMBSIZE;
        buf->f_bfree = buf->f_bavail = buf->f_ffree;
+#ifdef CONFIG_BLOCK
+       buf->f_bsize = sb->s_blocksize;
+       buf->f_blocks =
+               (romfs_maxsize(dentry->d_sb) + sb->s_blocksize - 1) >>
+               sb->s_blocksize_bits;
+#else
+       buf->f_bsize = ROMBSIZE;
        buf->f_blocks =
                (romfs_maxsize(dentry->d_sb) + ROMBSIZE - 1) >> ROMBSBITS;
+#endif
        buf->f_fsid.val[0] = (u32)id;
        buf->f_fsid.val[1] = (u32)(id >> 32);
        return 0;
-- 
2.17.1

Reply via email to