Commit f433dc56 authored by Dmitri Vorobiev's avatar Dmitri Vorobiev Committed by Linus Torvalds

Fixes to the BFS filesystem driver

I found a few bugs in the BFS driver.  Detailed description of the bugs as
well as the steps to reproduce the errors are given in the kernel bugzilla.
 Please follow these links for more information:

http://bugzilla.kernel.org/show_bug.cgi?id=9363
http://bugzilla.kernel.org/show_bug.cgi?id=9364
http://bugzilla.kernel.org/show_bug.cgi?id=9365
http://bugzilla.kernel.org/show_bug.cgi?id=9366

This patch fixes the bugs described above.  Besides, the patch introduces
coding style changes to make the BFS driver conform to the requirements
specified for Linux kernel code.  Finally, I made a few cosmetic changes
such as removal of trivial debug output.

Also, the patch removes the fields `si_lf_ioff' and `si_lf_sblk' of the
in-core superblock structure.  These fields are initialized but never
actually used.

If you are wondering why I need BFS, here is the answer: I am using this
driver in the context of Linux kernel classes I am teaching in the Moscow
State University and in the International Institute of Information
Technology in Pune, India.
Signed-off-by: default avatarDmitri Vorobiev <dmitri.vorobiev@gmail.com>
Cc: Tigran Aivazian <tigran@veritas.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent cfb52856
......@@ -14,8 +14,6 @@ struct bfs_sb_info {
unsigned long si_blocks;
unsigned long si_freeb;
unsigned long si_freei;
unsigned long si_lf_ioff;
unsigned long si_lf_sblk;
unsigned long si_lf_eblk;
unsigned long si_lasti;
unsigned long * si_imap;
......@@ -39,7 +37,7 @@ static inline struct bfs_sb_info *BFS_SB(struct super_block *sb)
static inline struct bfs_inode_info *BFS_I(struct inode *inode)
{
return list_entry(inode, struct bfs_inode_info, vfs_inode);
return container_of(inode, struct bfs_inode_info, vfs_inode);
}
......
This diff is collapsed.
......@@ -2,6 +2,11 @@
* fs/bfs/file.c
* BFS file operations.
* Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
*
* Make the file block allocation algorithm understand the size
* of the underlying block device.
* Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
*
*/
#include <linux/fs.h>
......@@ -27,7 +32,8 @@ const struct file_operations bfs_file_operations = {
.splice_read = generic_file_splice_read,
};
static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
static int bfs_move_block(unsigned long from, unsigned long to,
struct super_block *sb)
{
struct buffer_head *bh, *new;
......@@ -43,21 +49,22 @@ static int bfs_move_block(unsigned long from, unsigned long to, struct super_blo
}
static int bfs_move_blocks(struct super_block *sb, unsigned long start,
unsigned long end, unsigned long where)
unsigned long end, unsigned long where)
{
unsigned long i;
dprintf("%08lx-%08lx->%08lx\n", start, end, where);
for (i = start; i <= end; i++)
if(bfs_move_block(i, where + i, sb)) {
dprintf("failed to move block %08lx -> %08lx\n", i, where + i);
dprintf("failed to move block %08lx -> %08lx\n", i,
where + i);
return -EIO;
}
return 0;
}
static int bfs_get_block(struct inode * inode, sector_t block,
struct buffer_head * bh_result, int create)
static int bfs_get_block(struct inode *inode, sector_t block,
struct buffer_head *bh_result, int create)
{
unsigned long phys;
int err;
......@@ -66,9 +73,6 @@ static int bfs_get_block(struct inode * inode, sector_t block,
struct bfs_inode_info *bi = BFS_I(inode);
struct buffer_head *sbh = info->si_sbh;
if (block > info->si_blocks)
return -EIO;
phys = bi->i_sblock + block;
if (!create) {
if (phys <= bi->i_eblock) {
......@@ -79,21 +83,29 @@ static int bfs_get_block(struct inode * inode, sector_t block,
return 0;
}
/* if the file is not empty and the requested block is within the range
of blocks allocated for this file, we can grant it */
if (inode->i_size && phys <= bi->i_eblock) {
/*
* If the file is not empty and the requested block is within the
* range of blocks allocated for this file, we can grant it.
*/
if (bi->i_sblock && (phys <= bi->i_eblock)) {
dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
create, (unsigned long)block, phys);
map_bh(bh_result, sb, phys);
return 0;
}
/* the rest has to be protected against itself */
/* The file will be extended, so let's see if there is enough space. */
if (phys >= info->si_blocks)
return -ENOSPC;
/* The rest has to be protected against itself. */
lock_kernel();
/* if the last data block for this file is the last allocated
block, we can extend the file trivially, without moving it
anywhere */
/*
* If the last data block for this file is the last allocated
* block, we can extend the file trivially, without moving it
* anywhere.
*/
if (bi->i_eblock == info->si_lf_eblk) {
dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
create, (unsigned long)block, phys);
......@@ -106,13 +118,19 @@ static int bfs_get_block(struct inode * inode, sector_t block,
goto out;
}
/* Ok, we have to move this entire file to the next free block */
/* Ok, we have to move this entire file to the next free block. */
phys = info->si_lf_eblk + 1;
if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
if (phys + block >= info->si_blocks) {
err = -ENOSPC;
goto out;
}
if (bi->i_sblock) {
err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
bi->i_eblock, phys);
bi->i_eblock, phys);
if (err) {
dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
dprintf("failed to move ino=%08lx -> fs corruption\n",
inode->i_ino);
goto out;
}
} else
......@@ -124,8 +142,10 @@ static int bfs_get_block(struct inode * inode, sector_t block,
phys += block;
info->si_lf_eblk = bi->i_eblock = phys;
/* this assumes nothing can write the inode back while we are here
* and thus update inode->i_blocks! (XXX)*/
/*
* This assumes nothing can write the inode back while we are here
* and thus update inode->i_blocks! (XXX)
*/
info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
mark_inode_dirty(inode);
mark_buffer_dirty(sbh);
......
......@@ -30,25 +30,26 @@ MODULE_LICENSE("GPL");
#define dprintf(x...)
#endif
void dump_imap(const char *prefix, struct super_block * s);
void dump_imap(const char *prefix, struct super_block *s);
static void bfs_read_inode(struct inode * inode)
static void bfs_read_inode(struct inode *inode)
{
unsigned long ino = inode->i_ino;
struct bfs_inode * di;
struct buffer_head * bh;
struct bfs_inode *di;
struct buffer_head *bh;
int block, off;
if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(inode->i_sb)->si_lasti)) {
printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
make_bad_inode(inode);
return;
}
block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
block = (ino - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
bh = sb_bread(inode->i_sb, block);
if (!bh) {
printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id,
ino);
make_bad_inode(inode);
return;
}
......@@ -56,7 +57,7 @@ static void bfs_read_inode(struct inode * inode)
off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
di = (struct bfs_inode *)bh->b_data + off;
inode->i_mode = 0x0000FFFF & le32_to_cpu(di->i_mode);
inode->i_mode = 0x0000FFFF & le32_to_cpu(di->i_mode);
if (le32_to_cpu(di->i_vtype) == BFS_VDIR) {
inode->i_mode |= S_IFDIR;
inode->i_op = &bfs_dir_inops;
......@@ -70,48 +71,48 @@ static void bfs_read_inode(struct inode * inode)
BFS_I(inode)->i_sblock = le32_to_cpu(di->i_sblock);
BFS_I(inode)->i_eblock = le32_to_cpu(di->i_eblock);
BFS_I(inode)->i_dsk_ino = le16_to_cpu(di->i_ino);
inode->i_uid = le32_to_cpu(di->i_uid);
inode->i_gid = le32_to_cpu(di->i_gid);
inode->i_nlink = le32_to_cpu(di->i_nlink);
inode->i_size = BFS_FILESIZE(di);
inode->i_blocks = BFS_FILEBLOCKS(di);
if (inode->i_size || inode->i_blocks) dprintf("Registered inode with %lld size, %ld blocks\n", inode->i_size, inode->i_blocks);
inode->i_atime.tv_sec = le32_to_cpu(di->i_atime);
inode->i_mtime.tv_sec = le32_to_cpu(di->i_mtime);
inode->i_ctime.tv_sec = le32_to_cpu(di->i_ctime);
inode->i_atime.tv_nsec = 0;
inode->i_mtime.tv_nsec = 0;
inode->i_ctime.tv_nsec = 0;
BFS_I(inode)->i_dsk_ino = le16_to_cpu(di->i_ino); /* can be 0 so we store a copy */
brelse(bh);
}
static int bfs_write_inode(struct inode * inode, int unused)
static int bfs_write_inode(struct inode *inode, int unused)
{
unsigned int ino = (u16)inode->i_ino;
unsigned long i_sblock;
struct bfs_inode * di;
struct buffer_head * bh;
struct bfs_inode *di;
struct buffer_head *bh;
int block, off;
dprintf("ino=%08x\n", ino);
if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(inode->i_sb)->si_lasti)) {
printf("Bad inode number %s:%08x\n", inode->i_sb->s_id, ino);
return -EIO;
}
lock_kernel();
block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
block = (ino - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
bh = sb_bread(inode->i_sb, block);
if (!bh) {
printf("Unable to read inode %s:%08x\n", inode->i_sb->s_id, ino);
printf("Unable to read inode %s:%08x\n",
inode->i_sb->s_id, ino);
unlock_kernel();
return -EIO;
}
off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
di = (struct bfs_inode *)bh->b_data + off;
if (ino == BFS_ROOT_INO)
......@@ -133,27 +134,26 @@ static int bfs_write_inode(struct inode * inode, int unused)
di->i_eoffset = cpu_to_le32(i_sblock * BFS_BSIZE + inode->i_size - 1);
mark_buffer_dirty(bh);
dprintf("Written ino=%d into %d:%d\n",le16_to_cpu(di->i_ino),block,off);
brelse(bh);
unlock_kernel();
return 0;
}
static void bfs_delete_inode(struct inode * inode)
static void bfs_delete_inode(struct inode *inode)
{
unsigned long ino = inode->i_ino;
struct bfs_inode * di;
struct buffer_head * bh;
struct bfs_inode *di;
struct buffer_head *bh;
int block, off;
struct super_block * s = inode->i_sb;
struct bfs_sb_info * info = BFS_SB(s);
struct bfs_inode_info * bi = BFS_I(inode);
struct super_block *s = inode->i_sb;
struct bfs_sb_info *info = BFS_SB(s);
struct bfs_inode_info *bi = BFS_I(inode);
dprintf("ino=%08lx\n", ino);
truncate_inode_pages(&inode->i_data, 0);
if (ino < BFS_ROOT_INO || ino > info->si_lasti) {
if ((ino < BFS_ROOT_INO) || (ino > info->si_lasti)) {
printf("invalid ino=%08lx\n", ino);
return;
}
......@@ -162,31 +162,35 @@ static void bfs_delete_inode(struct inode * inode)
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
lock_kernel();
mark_inode_dirty(inode);
block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
block = (ino - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
bh = sb_bread(s, block);
if (!bh) {
printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
printf("Unable to read inode %s:%08lx\n",
inode->i_sb->s_id, ino);
unlock_kernel();
return;
}
off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
di = (struct bfs_inode *) bh->b_data + off;
off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
di = (struct bfs_inode *)bh->b_data + off;
memset((void *)di, 0, sizeof(struct bfs_inode));
mark_buffer_dirty(bh);
brelse(bh);
if (bi->i_dsk_ino) {
info->si_freeb += 1 + bi->i_eblock - bi->i_sblock;
info->si_freeb += BFS_FILEBLOCKS(bi);
info->si_freei++;
clear_bit(ino, info->si_imap);
dump_imap("delete_inode", s);
}
di->i_ino = 0;
di->i_sblock = 0;
mark_buffer_dirty(bh);
brelse(bh);
/* if this was the last file, make the previous
block "last files last block" even if there is no real file there,
saves us 1 gap */
if (info->si_lf_eblk == BFS_I(inode)->i_eblock) {
info->si_lf_eblk = BFS_I(inode)->i_sblock - 1;
/*
* If this was the last file, make the previous block
* "last block of the last file" even if there is no
* real file there, saves us 1 gap.
*/
if (info->si_lf_eblk == bi->i_eblock) {
info->si_lf_eblk = bi->i_sblock - 1;
mark_buffer_dirty(info->si_sbh);
}
unlock_kernel();
......@@ -228,7 +232,7 @@ static void bfs_write_super(struct super_block *s)
unlock_kernel();
}
static struct kmem_cache * bfs_inode_cachep;
static struct kmem_cache *bfs_inode_cachep;
static struct inode *bfs_alloc_inode(struct super_block *sb)
{
......@@ -279,7 +283,7 @@ static const struct super_operations bfs_sops = {
.statfs = bfs_statfs,
};
void dump_imap(const char *prefix, struct super_block * s)
void dump_imap(const char *prefix, struct super_block *s)
{
#ifdef DEBUG
int i;
......@@ -287,25 +291,26 @@ void dump_imap(const char *prefix, struct super_block * s)
if (!tmpbuf)
return;
for (i=BFS_SB(s)->si_lasti; i>=0; i--) {
if (i > PAGE_SIZE-100) break;
for (i = BFS_SB(s)->si_lasti; i >= 0; i--) {
if (i > PAGE_SIZE - 100) break;
if (test_bit(i, BFS_SB(s)->si_imap))
strcat(tmpbuf, "1");
else
strcat(tmpbuf, "0");
}
printk(KERN_ERR "BFS-fs: %s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
printf("BFS-fs: %s: lasti=%08lx <%s>\n",
prefix, BFS_SB(s)->si_lasti, tmpbuf);
free_page((unsigned long)tmpbuf);
#endif
}
static int bfs_fill_super(struct super_block *s, void *data, int silent)
{
struct buffer_head * bh;
struct bfs_super_block * bfs_sb;
struct inode * inode;
struct buffer_head *bh;
struct bfs_super_block *bfs_sb;
struct inode *inode;
unsigned i, imap_len;
struct bfs_sb_info * info;
struct bfs_sb_info *info;
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info)
......@@ -329,14 +334,14 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
s->s_magic = BFS_MAGIC;
info->si_sbh = bh;
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_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);
if (!info->si_imap)
goto out;
for (i=0; i<BFS_ROOT_INO; i++)
for (i = 0; i < BFS_ROOT_INO; i++)
set_bit(i, info->si_imap);
s->s_op = &bfs_sops;
......@@ -352,16 +357,15 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
goto out;
}
info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1)>>BFS_BSIZE_BITS; /* for statfs(2) */
info->si_freeb = (le32_to_cpu(bfs_sb->s_end) + 1 - le32_to_cpu(bfs_sb->s_start))>>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
- le32_to_cpu(bfs_sb->s_start)) >> BFS_BSIZE_BITS;
info->si_freei = 0;
info->si_lf_eblk = 0;
info->si_lf_sblk = 0;
info->si_lf_ioff = 0;
bh = NULL;
for (i=BFS_ROOT_INO; i<=info->si_lasti; i++) {
for (i = BFS_ROOT_INO; i <= info->si_lasti; i++) {
struct bfs_inode *di;
int block = (i - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
int block = (i - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
int off = (i - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
unsigned long sblock, eblock;
......@@ -384,11 +388,8 @@ static int bfs_fill_super(struct super_block *s, void *data, int silent)
sblock = le32_to_cpu(di->i_sblock);
eblock = le32_to_cpu(di->i_eblock);
if (eblock > info->si_lf_eblk) {
if (eblock > info->si_lf_eblk)
info->si_lf_eblk = eblock;
info->si_lf_sblk = sblock;
info->si_lf_ioff = BFS_INO2OFF(i);
}
}
brelse(bh);
if (!(s->s_flags & MS_RDONLY)) {
......
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