From: Tigran Aivazian Subject: [PATCH 4.19.6 1/2] BFS updates Make in-core inode bitmap static part of superblock info structure an. print a warning when mounting a BFS filesystem created with "-N 512" option as only 510 files can be created in the root directory. Signed-off-by: Tigran Aivazian Cc: stable@vger.kernel.org --- bfs.h | 9 ++++++++- inode.c | 27 +++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) --- fs/bfs/bfs.h.0 2018-12-02 20:33:02.252710291 +0000 +++ fs/bfs/bfs.h 2018-12-02 20:34:34.041246489 +0000 @@ -8,6 +8,13 @@ #include +/* 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 */ @@ -17,7 +24,7 @@ unsigned long si_freei; unsigned long si_lf_eblk; unsigned long si_lasti; - unsigned long *si_imap; + DECLARE_BITMAP(si_imap, BFS_MAX_LASTI+1); struct mutex bfs_lock; }; --- fs/bfs/inode.c.0 2018-12-02 20:34:03.211740877 +0000 +++ fs/bfs/inode.c 2018-12-02 20:36:54.508963813 +0000 @@ -214,7 +214,6 @@ return; mutex_destroy(&info->bfs_lock); - kfree(info->si_imap); kfree(info); s->s_fs_info = NULL; } @@ -322,7 +321,7 @@ struct buffer_head *bh, *sbh; struct bfs_super_block *bfs_sb; struct inode *inode; - unsigned i, imap_len; + unsigned i; struct bfs_sb_info *info; int ret = -EINVAL; unsigned long i_sblock, i_eblock, i_eoff, s_size; @@ -356,13 +355,11 @@ goto out1; } - info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / - sizeof(struct bfs_inode) - + BFS_ROOT_INO - 1; - imap_len = (info->si_lasti / 8) + 1; - info->si_imap = kzalloc(imap_len, GFP_KERNEL | __GFP_NOWARN); - if (!info->si_imap) { - printf("Cannot allocate %u bytes\n", imap_len); + info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / sizeof(struct bfs_inode) + BFS_ROOT_INO - 1; + if (info->si_lasti == BFS_MAX_LASTI) + printf("WARNING: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id); + else if (info->si_lasti > BFS_MAX_LASTI) { + printf("Impossible last inode number %lu > %d on %s\n", info->si_lasti, BFS_MAX_LASTI, s->s_id); goto out1; } for (i = 0; i < BFS_ROOT_INO; i++) @@ -372,12 +369,12 @@ inode = bfs_iget(s, BFS_ROOT_INO); if (IS_ERR(inode)) { ret = PTR_ERR(inode); - goto out2; + goto out1; } s->s_root = d_make_root(inode); if (!s->s_root) { ret = -ENOMEM; - goto out2; + goto out1; } info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1) >> BFS_BSIZE_BITS; @@ -391,7 +388,7 @@ if (!bh) { printf("Last block not available: %lu\n", info->si_blocks - 1); ret = -EIO; - goto out3; + goto out2; } brelse(bh); @@ -429,7 +426,7 @@ brelse(bh); ret = -EIO; - goto out3; + goto out2; } if (!di->i_ino) { @@ -448,11 +445,9 @@ bfs_dump_imap("read_super", s); return 0; -out3: +out2: dput(s->s_root); s->s_root = NULL; -out2: - kfree(info->si_imap); out1: brelse(sbh); out: