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: Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
Cc: Tigran Aivazian <tigran@veritas.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Dmitri Vorobiev 2007-11-14 16:59:47 -08:00 committed by Linus Torvalds
parent cfb5285660
commit f433dc5634
4 changed files with 184 additions and 155 deletions

View File

@ -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);
}

View File

@ -21,9 +21,11 @@
#define dprintf(x...)
#endif
static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino);
static int bfs_add_entry(struct inode *dir, const unsigned char *name,
int namelen, int ino);
static struct buffer_head *bfs_find_entry(struct inode *dir,
const unsigned char * name, int namelen, struct bfs_dirent ** res_dir);
const unsigned char *name, int namelen,
struct bfs_dirent **res_dir);
static int bfs_readdir(struct file *f, void *dirent, filldir_t filldir)
{
@ -36,7 +38,8 @@ static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
lock_kernel();
if (f->f_pos & (BFS_DIRENT_SIZE - 1)) {
printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
printf("Bad f_pos=%08lx for %s:%08lx\n",
(unsigned long)f->f_pos,
dir->i_sb->s_id, dir->i_ino);
unlock_kernel();
return -EBADF;
@ -54,7 +57,9 @@ static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
de = (struct bfs_dirent *)(bh->b_data + offset);
if (de->ino) {
int size = strnlen(de->name, BFS_NAMELEN);
if (filldir(dirent, de->name, size, f->f_pos, le16_to_cpu(de->ino), DT_UNKNOWN) < 0) {
if (filldir(dirent, de->name, size, f->f_pos,
le16_to_cpu(de->ino),
DT_UNKNOWN) < 0) {
brelse(bh);
unlock_kernel();
return 0;
@ -62,7 +67,7 @@ static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
}
offset += BFS_DIRENT_SIZE;
f->f_pos += BFS_DIRENT_SIZE;
} while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
} while ((offset < BFS_BSIZE) && (f->f_pos < dir->i_size));
brelse(bh);
}
@ -115,7 +120,8 @@ static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
mark_inode_dirty(inode);
dump_imap("create", s);
err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len,
inode->i_ino);
if (err) {
inode_dec_link_count(inode);
iput(inode);
@ -127,7 +133,8 @@ static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
return 0;
}
static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
{
struct inode *inode = NULL;
struct buffer_head *bh;
@ -152,13 +159,15 @@ static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, st
return NULL;
}
static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
static int bfs_link(struct dentry *old, struct inode *dir,
struct dentry *new)
{
struct inode *inode = old->d_inode;
int err;
lock_kernel();
err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
err = bfs_add_entry(dir, new->d_name.name, new->d_name.len,
inode->i_ino);
if (err) {
unlock_kernel();
return err;
@ -172,7 +181,6 @@ static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new
return 0;
}
static int bfs_unlink(struct inode *dir, struct dentry *dentry)
{
int error = -ENOENT;
@ -183,12 +191,13 @@ static int bfs_unlink(struct inode * dir, struct dentry * dentry)
inode = dentry->d_inode;
lock_kernel();
bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
if (!bh || le16_to_cpu(de->ino) != inode->i_ino)
if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
goto out_brelse;
if (!inode->i_nlink) {
printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode->i_sb->s_id,
inode->i_ino, inode->i_nlink);
printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
inode->i_sb->s_id, inode->i_ino,
inode->i_nlink);
inode->i_nlink = 1;
}
de->ino = 0;
@ -223,7 +232,7 @@ static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
old_dentry->d_name.name,
old_dentry->d_name.len, &old_de);
if (!old_bh || le16_to_cpu(old_de->ino) != old_inode->i_ino)
if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
goto end_rename;
error = -EPERM;
@ -239,7 +248,8 @@ static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
if (!new_bh) {
error = bfs_add_entry(new_dir,
new_dentry->d_name.name,
new_dentry->d_name.len, old_inode->i_ino);
new_dentry->d_name.len,
old_inode->i_ino);
if (error)
goto end_rename;
}
@ -268,11 +278,12 @@ const struct inode_operations bfs_dir_inops = {
.rename = bfs_rename,
};
static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino)
static int bfs_add_entry(struct inode *dir, const unsigned char *name,
int namelen, int ino)
{
struct buffer_head *bh;
struct bfs_dirent *de;
int block, sblock, eblock, off, eoff;
int block, sblock, eblock, off, pos;
int i;
dprintf("name=%s, namelen=%d\n", name, namelen);
@ -284,19 +295,15 @@ static int bfs_add_entry(struct inode * dir, const unsigned char * name, int nam
sblock = BFS_I(dir)->i_sblock;
eblock = BFS_I(dir)->i_eblock;
eoff = dir->i_size % BFS_BSIZE;
for (block = sblock; block <= eblock; block++) {
bh = sb_bread(dir->i_sb, block);
if (!bh)
return -ENOSPC;
for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
de = (struct bfs_dirent *)(bh->b_data + off);
if (block==eblock && off>=eoff) {
/* Do not read/interpret the garbage in the end of eblock. */
de->ino = 0;
}
if (!de->ino) {
if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
pos = (block - sblock) * BFS_BSIZE + off;
if (pos >= dir->i_size) {
dir->i_size += BFS_DIRENT_SIZE;
dir->i_ctime = CURRENT_TIME_SEC;
}
@ -304,7 +311,8 @@ static int bfs_add_entry(struct inode * dir, const unsigned char * name, int nam
mark_inode_dirty(dir);
de->ino = cpu_to_le16((u16)ino);
for (i = 0; i < BFS_NAMELEN; i++)
de->name[i] = (i < namelen) ? name[i] : 0;
de->name[i] =
(i < namelen) ? name[i] : 0;
mark_buffer_dirty(bh);
brelse(bh);
return 0;
@ -315,25 +323,26 @@ static int bfs_add_entry(struct inode * dir, const unsigned char * name, int nam
return -ENOSPC;
}
static inline int bfs_namecmp(int len, const unsigned char * name, const char * buffer)
static inline int bfs_namecmp(int len, const unsigned char *name,
const char *buffer)
{
if (len < BFS_NAMELEN && buffer[len])
if ((len < BFS_NAMELEN) && buffer[len])
return 0;
return !memcmp(name, buffer, len);
}
static struct buffer_head *bfs_find_entry(struct inode *dir,
const unsigned char * name, int namelen, struct bfs_dirent ** res_dir)
const unsigned char *name, int namelen,
struct bfs_dirent **res_dir)
{
unsigned long block, offset;
struct buffer_head * bh;
unsigned long block = 0, offset = 0;
struct buffer_head *bh = NULL;
struct bfs_dirent *de;
*res_dir = NULL;
if (namelen > BFS_NAMELEN)
return NULL;
bh = NULL;
block = offset = 0;
while (block * BFS_BSIZE + offset < dir->i_size) {
if (!bh) {
bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
@ -344,7 +353,8 @@ static struct buffer_head * bfs_find_entry(struct inode * dir,
}
de = (struct bfs_dirent *)(bh->b_data + offset);
offset += BFS_DIRENT_SIZE;
if (le16_to_cpu(de->ino) && bfs_namecmp(namelen, name, de->name)) {
if (le16_to_cpu(de->ino) &&
bfs_namecmp(namelen, name, de->name)) {
*res_dir = de;
return bh;
}

View File

@ -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;
@ -50,7 +56,8 @@ static int bfs_move_blocks(struct super_block *sb, unsigned long start,
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;
@ -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);
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);

View File

@ -39,7 +39,7 @@ static void bfs_read_inode(struct inode * inode)
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;
@ -48,7 +48,8 @@ static void bfs_read_inode(struct inode * inode)
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;
}
@ -70,19 +71,18 @@ 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);
}
@ -97,7 +97,7 @@ static int bfs_write_inode(struct inode * inode, int unused)
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;
}
@ -106,7 +106,8 @@ static int bfs_write_inode(struct inode * inode, int unused)
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;
}
@ -133,7 +134,6 @@ 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;
@ -153,7 +153,7 @@ static void bfs_delete_inode(struct inode * inode)
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;
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;
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();
@ -294,7 +298,8 @@ void dump_imap(const char *prefix, struct super_block * s)
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
}
@ -329,10 +334,10 @@ 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)
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;
imap_len = (info->si_lasti / 8) + 1;
info->si_imap = kzalloc(imap_len, GFP_KERNEL);
if (!info->si_imap)
goto out;
@ -352,12 +357,11 @@ 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++) {
struct bfs_inode *di;
@ -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)) {