Commit d1877155 authored by Tigran Aivazian's avatar Tigran Aivazian Committed by Linus Torvalds

bfs: extra sanity checking and static inode bitmap

Strengthen validation of BFS superblock against corruption.  Make
in-core inode bitmap static part of superblock info structure.  Print a
warning when mounting a BFS filesystem created with "-N 512" option as
only 510 files can be created in the root directory.  Make the kernel
messages more uniform.  Update the 'prefix' passed to bfs_dump_imap() to
match the current naming of operations.  White space and comments
cleanup.

Link: http://lkml.kernel.org/r/CAK+_RLkFZMduoQF36wZFd3zLi-6ZutWKsydjeHFNdtRvZZEb4w@mail.gmail.comSigned-off-by: default avatarTigran Aivazian <aivazian.tigran@gmail.com>
Reported-by: default avatarTetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 655c16a8
/* SPDX-License-Identifier: GPL-2.0 */ /* SPDX-License-Identifier: GPL-2.0 */
/* /*
* fs/bfs/bfs.h * fs/bfs/bfs.h
* Copyright (C) 1999 Tigran Aivazian <tigran@veritas.com> * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
*/ */
#ifndef _FS_BFS_BFS_H #ifndef _FS_BFS_BFS_H
#define _FS_BFS_BFS_H #define _FS_BFS_BFS_H
#include <linux/bfs_fs.h> #include <linux/bfs_fs.h>
/* In theory BFS supports up to 512 inodes, numbered from 2 (for /) up to 513 inclusive.
In actual fact, attempting to create the 512th inode (i.e. inode No. 513 or file No. 511)
will fail with ENOSPC in bfs_add_entry(): the root directory cannot contain so many entries, counting '..'.
So, mkfs.bfs(8) should really limit its -N option to 511 and not 512. For now, we just print a warning
if a filesystem is mounted with such "impossible to fill up" number of inodes */
#define BFS_MAX_LASTI 513
/* /*
* BFS file system in-core superblock info * BFS file system in-core superblock info
*/ */
...@@ -17,7 +24,7 @@ struct bfs_sb_info { ...@@ -17,7 +24,7 @@ struct bfs_sb_info {
unsigned long si_freei; unsigned long si_freei;
unsigned long si_lf_eblk; unsigned long si_lf_eblk;
unsigned long si_lasti; unsigned long si_lasti;
unsigned long *si_imap; DECLARE_BITMAP(si_imap, BFS_MAX_LASTI+1);
struct mutex bfs_lock; struct mutex bfs_lock;
}; };
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
/* /*
* fs/bfs/dir.c * fs/bfs/dir.c
* BFS directory operations. * BFS directory operations.
* Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com> * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
* Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
*/ */
#include <linux/time.h> #include <linux/time.h>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/* /*
* fs/bfs/file.c * fs/bfs/file.c
* BFS file operations. * BFS file operations.
* Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com> * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
* *
* Make the file block allocation algorithm understand the size * Make the file block allocation algorithm understand the size
* of the underlying block device. * of the underlying block device.
......
/* /*
* fs/bfs/inode.c * fs/bfs/inode.c
* BFS superblock and inode operations. * BFS superblock and inode operations.
* Copyright (C) 1999-2006 Tigran Aivazian <aivazian.tigran@gmail.com> * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
* From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds. * From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds.
* * Made endianness-clean by Andrew Stribblehill <ads@wompom.org>, 2005.
* Made endianness-clean by Andrew Stribblehill <ads@wompom.org>, 2005.
*/ */
#include <linux/module.h> #include <linux/module.h>
...@@ -118,12 +117,12 @@ static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc) ...@@ -118,12 +117,12 @@ static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc)
{ {
struct bfs_sb_info *info = BFS_SB(inode->i_sb); struct bfs_sb_info *info = BFS_SB(inode->i_sb);
unsigned int ino = (u16)inode->i_ino; unsigned int ino = (u16)inode->i_ino;
unsigned long i_sblock; unsigned long i_sblock;
struct bfs_inode *di; struct bfs_inode *di;
struct buffer_head *bh; struct buffer_head *bh;
int err = 0; int err = 0;
dprintf("ino=%08x\n", ino); dprintf("ino=%08x\n", ino);
di = find_inode(inode->i_sb, ino, &bh); di = find_inode(inode->i_sb, ino, &bh);
if (IS_ERR(di)) if (IS_ERR(di))
...@@ -144,7 +143,7 @@ static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc) ...@@ -144,7 +143,7 @@ static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc)
di->i_atime = cpu_to_le32(inode->i_atime.tv_sec); di->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
di->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); di->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
di->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); di->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
i_sblock = BFS_I(inode)->i_sblock; i_sblock = BFS_I(inode)->i_sblock;
di->i_sblock = cpu_to_le32(i_sblock); di->i_sblock = cpu_to_le32(i_sblock);
di->i_eblock = cpu_to_le32(BFS_I(inode)->i_eblock); di->i_eblock = cpu_to_le32(BFS_I(inode)->i_eblock);
di->i_eoffset = cpu_to_le32(i_sblock * BFS_BSIZE + inode->i_size - 1); di->i_eoffset = cpu_to_le32(i_sblock * BFS_BSIZE + inode->i_size - 1);
...@@ -188,13 +187,13 @@ static void bfs_evict_inode(struct inode *inode) ...@@ -188,13 +187,13 @@ static void bfs_evict_inode(struct inode *inode)
mark_buffer_dirty(bh); mark_buffer_dirty(bh);
brelse(bh); brelse(bh);
if (bi->i_dsk_ino) { if (bi->i_dsk_ino) {
if (bi->i_sblock) if (bi->i_sblock)
info->si_freeb += bi->i_eblock + 1 - bi->i_sblock; info->si_freeb += bi->i_eblock + 1 - bi->i_sblock;
info->si_freei++; info->si_freei++;
clear_bit(ino, info->si_imap); clear_bit(ino, info->si_imap);
bfs_dump_imap("delete_inode", s); bfs_dump_imap("evict_inode", s);
} }
/* /*
* If this was the last file, make the previous block * If this was the last file, make the previous block
...@@ -214,7 +213,6 @@ static void bfs_put_super(struct super_block *s) ...@@ -214,7 +213,6 @@ static void bfs_put_super(struct super_block *s)
return; return;
mutex_destroy(&info->bfs_lock); mutex_destroy(&info->bfs_lock);
kfree(info->si_imap);
kfree(info); kfree(info);
s->s_fs_info = NULL; s->s_fs_info = NULL;
} }
...@@ -311,8 +309,7 @@ void bfs_dump_imap(const char *prefix, struct super_block *s) ...@@ -311,8 +309,7 @@ void bfs_dump_imap(const char *prefix, struct super_block *s)
else else
strcat(tmpbuf, "0"); strcat(tmpbuf, "0");
} }
printf("BFS-fs: %s: lasti=%08lx <%s>\n", printf("%s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
prefix, BFS_SB(s)->si_lasti, tmpbuf);
free_page((unsigned long)tmpbuf); free_page((unsigned long)tmpbuf);
#endif #endif
} }
...@@ -322,7 +319,7 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -322,7 +319,7 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
struct buffer_head *bh, *sbh; struct buffer_head *bh, *sbh;
struct bfs_super_block *bfs_sb; struct bfs_super_block *bfs_sb;
struct inode *inode; struct inode *inode;
unsigned i, imap_len; unsigned i;
struct bfs_sb_info *info; struct bfs_sb_info *info;
int ret = -EINVAL; int ret = -EINVAL;
unsigned long i_sblock, i_eblock, i_eoff, s_size; unsigned long i_sblock, i_eblock, i_eoff, s_size;
...@@ -341,8 +338,7 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -341,8 +338,7 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
bfs_sb = (struct bfs_super_block *)sbh->b_data; bfs_sb = (struct bfs_super_block *)sbh->b_data;
if (le32_to_cpu(bfs_sb->s_magic) != BFS_MAGIC) { if (le32_to_cpu(bfs_sb->s_magic) != BFS_MAGIC) {
if (!silent) if (!silent)
printf("No BFS filesystem on %s (magic=%08x)\n", printf("No BFS filesystem on %s (magic=%08x)\n", s->s_id, le32_to_cpu(bfs_sb->s_magic));
s->s_id, le32_to_cpu(bfs_sb->s_magic));
goto out1; goto out1;
} }
if (BFS_UNCLEAN(bfs_sb, s) && !silent) if (BFS_UNCLEAN(bfs_sb, s) && !silent)
...@@ -351,18 +347,16 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -351,18 +347,16 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
s->s_magic = BFS_MAGIC; s->s_magic = BFS_MAGIC;
if (le32_to_cpu(bfs_sb->s_start) > le32_to_cpu(bfs_sb->s_end) || if (le32_to_cpu(bfs_sb->s_start) > le32_to_cpu(bfs_sb->s_end) ||
le32_to_cpu(bfs_sb->s_start) < BFS_BSIZE) { le32_to_cpu(bfs_sb->s_start) < sizeof(struct bfs_super_block) + sizeof(struct bfs_dirent)) {
printf("Superblock is corrupted\n"); printf("Superblock is corrupted on %s\n", s->s_id);
goto out1; goto out1;
} }
info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / sizeof(struct bfs_inode) + BFS_ROOT_INO - 1;
sizeof(struct bfs_inode) if (info->si_lasti == BFS_MAX_LASTI)
+ BFS_ROOT_INO - 1; printf("WARNING: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id);
imap_len = (info->si_lasti / 8) + 1; else if (info->si_lasti > BFS_MAX_LASTI) {
info->si_imap = kzalloc(imap_len, GFP_KERNEL | __GFP_NOWARN); printf("Impossible last inode number %lu > %d on %s\n", info->si_lasti, BFS_MAX_LASTI, s->s_id);
if (!info->si_imap) {
printf("Cannot allocate %u bytes\n", imap_len);
goto out1; goto out1;
} }
for (i = 0; i < BFS_ROOT_INO; i++) for (i = 0; i < BFS_ROOT_INO; i++)
...@@ -372,26 +366,25 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -372,26 +366,25 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
inode = bfs_iget(s, BFS_ROOT_INO); inode = bfs_iget(s, BFS_ROOT_INO);
if (IS_ERR(inode)) { if (IS_ERR(inode)) {
ret = PTR_ERR(inode); ret = PTR_ERR(inode);
goto out2; goto out1;
} }
s->s_root = d_make_root(inode); s->s_root = d_make_root(inode);
if (!s->s_root) { if (!s->s_root) {
ret = -ENOMEM; ret = -ENOMEM;
goto out2; goto out1;
} }
info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1) >> BFS_BSIZE_BITS; info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1) >> BFS_BSIZE_BITS;
info->si_freeb = (le32_to_cpu(bfs_sb->s_end) + 1 info->si_freeb = (le32_to_cpu(bfs_sb->s_end) + 1 - le32_to_cpu(bfs_sb->s_start)) >> BFS_BSIZE_BITS;
- le32_to_cpu(bfs_sb->s_start)) >> BFS_BSIZE_BITS;
info->si_freei = 0; info->si_freei = 0;
info->si_lf_eblk = 0; info->si_lf_eblk = 0;
/* can we read the last block? */ /* can we read the last block? */
bh = sb_bread(s, info->si_blocks - 1); bh = sb_bread(s, info->si_blocks - 1);
if (!bh) { if (!bh) {
printf("Last block not available: %lu\n", info->si_blocks - 1); printf("Last block not available on %s: %lu\n", s->s_id, info->si_blocks - 1);
ret = -EIO; ret = -EIO;
goto out3; goto out2;
} }
brelse(bh); brelse(bh);
...@@ -425,11 +418,11 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -425,11 +418,11 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
(i_eoff != le32_to_cpu(-1) && i_eoff > s_size) || (i_eoff != le32_to_cpu(-1) && i_eoff > s_size) ||
i_sblock * BFS_BSIZE > i_eoff) { i_sblock * BFS_BSIZE > i_eoff) {
printf("Inode 0x%08x corrupted\n", i); printf("Inode 0x%08x corrupted on %s\n", i, s->s_id);
brelse(bh); brelse(bh);
ret = -EIO; ret = -EIO;
goto out3; goto out2;
} }
if (!di->i_ino) { if (!di->i_ino) {
...@@ -445,14 +438,12 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent) ...@@ -445,14 +438,12 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
} }
brelse(bh); brelse(bh);
brelse(sbh); brelse(sbh);
bfs_dump_imap("read_super", s); bfs_dump_imap("fill_super", s);
return 0; return 0;
out3: out2:
dput(s->s_root); dput(s->s_root);
s->s_root = NULL; s->s_root = NULL;
out2:
kfree(info->si_imap);
out1: out1:
brelse(sbh); brelse(sbh);
out: out:
...@@ -482,7 +473,7 @@ static int __init init_bfs_fs(void) ...@@ -482,7 +473,7 @@ static int __init init_bfs_fs(void)
int err = init_inodecache(); int err = init_inodecache();
if (err) if (err)
goto out1; goto out1;
err = register_filesystem(&bfs_fs_type); err = register_filesystem(&bfs_fs_type);
if (err) if (err)
goto out; goto out;
return 0; return 0;
......
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/* /*
* include/linux/bfs_fs.h - BFS data structures on disk. * include/linux/bfs_fs.h - BFS data structures on disk.
* Copyright (C) 1999 Tigran Aivazian <tigran@veritas.com> * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
*/ */
#ifndef _LINUX_BFS_FS_H #ifndef _LINUX_BFS_FS_H
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment