Skip to content

Commit f433dc5

Browse files
dmvoLinus Torvalds
authored andcommitted
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>
1 parent cfb5285 commit f433dc5

4 files changed

Lines changed: 184 additions & 155 deletions

File tree

‎fs/bfs/bfs.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ struct bfs_sb_info {
1414
unsigned long si_blocks;
1515
unsigned long si_freeb;
1616
unsigned long si_freei;
17-
unsigned long si_lf_ioff;
18-
unsigned long si_lf_sblk;
1917
unsigned long si_lf_eblk;
2018
unsigned long si_lasti;
2119
unsigned long * si_imap;
@@ -39,7 +37,7 @@ static inline struct bfs_sb_info *BFS_SB(struct super_block *sb)
3937

4038
static inline struct bfs_inode_info *BFS_I(struct inode *inode)
4139
{
42-
return list_entry(inode, struct bfs_inode_info, vfs_inode);
40+
return container_of(inode, struct bfs_inode_info, vfs_inode);
4341
}
4442

4543

‎fs/bfs/dir.c‎

Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,32 @@
2121
#define dprintf(x...)
2222
#endif
2323

24-
static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino);
25-
static struct buffer_head * bfs_find_entry(struct inode * dir,
26-
const unsigned char * name, int namelen, struct bfs_dirent ** res_dir);
24+
static int bfs_add_entry(struct inode *dir, const unsigned char *name,
25+
int namelen, int ino);
26+
static struct buffer_head *bfs_find_entry(struct inode *dir,
27+
const unsigned char *name, int namelen,
28+
struct bfs_dirent **res_dir);
2729

28-
static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
30+
static int bfs_readdir(struct file *f, void *dirent, filldir_t filldir)
2931
{
30-
struct inode * dir = f->f_path.dentry->d_inode;
31-
struct buffer_head * bh;
32-
struct bfs_dirent * de;
32+
struct inode *dir = f->f_path.dentry->d_inode;
33+
struct buffer_head *bh;
34+
struct bfs_dirent *de;
3335
unsigned int offset;
3436
int block;
3537

3638
lock_kernel();
3739

38-
if (f->f_pos & (BFS_DIRENT_SIZE-1)) {
39-
printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
40-
dir->i_sb->s_id, dir->i_ino);
40+
if (f->f_pos & (BFS_DIRENT_SIZE - 1)) {
41+
printf("Bad f_pos=%08lx for %s:%08lx\n",
42+
(unsigned long)f->f_pos,
43+
dir->i_sb->s_id, dir->i_ino);
4144
unlock_kernel();
4245
return -EBADF;
4346
}
4447

4548
while (f->f_pos < dir->i_size) {
46-
offset = f->f_pos & (BFS_BSIZE-1);
49+
offset = f->f_pos & (BFS_BSIZE - 1);
4750
block = BFS_I(dir)->i_sblock + (f->f_pos >> BFS_BSIZE_BITS);
4851
bh = sb_bread(dir->i_sb, block);
4952
if (!bh) {
@@ -54,15 +57,17 @@ static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
5457
de = (struct bfs_dirent *)(bh->b_data + offset);
5558
if (de->ino) {
5659
int size = strnlen(de->name, BFS_NAMELEN);
57-
if (filldir(dirent, de->name, size, f->f_pos, le16_to_cpu(de->ino), DT_UNKNOWN) < 0) {
60+
if (filldir(dirent, de->name, size, f->f_pos,
61+
le16_to_cpu(de->ino),
62+
DT_UNKNOWN) < 0) {
5863
brelse(bh);
5964
unlock_kernel();
6065
return 0;
6166
}
6267
}
6368
offset += BFS_DIRENT_SIZE;
6469
f->f_pos += BFS_DIRENT_SIZE;
65-
} while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
70+
} while ((offset < BFS_BSIZE) && (f->f_pos < dir->i_size));
6671
brelse(bh);
6772
}
6873

@@ -78,13 +83,13 @@ const struct file_operations bfs_dir_operations = {
7883

7984
extern void dump_imap(const char *, struct super_block *);
8085

81-
static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
82-
struct nameidata *nd)
86+
static int bfs_create(struct inode *dir, struct dentry *dentry, int mode,
87+
struct nameidata *nd)
8388
{
8489
int err;
85-
struct inode * inode;
86-
struct super_block * s = dir->i_sb;
87-
struct bfs_sb_info * info = BFS_SB(s);
90+
struct inode *inode;
91+
struct super_block *s = dir->i_sb;
92+
struct bfs_sb_info *info = BFS_SB(s);
8893
unsigned long ino;
8994

9095
inode = new_inode(s);
@@ -97,7 +102,7 @@ static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
97102
iput(inode);
98103
return -ENOSPC;
99104
}
100-
set_bit(ino, info->si_imap);
105+
set_bit(ino, info->si_imap);
101106
info->si_freei--;
102107
inode->i_uid = current->fsuid;
103108
inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
@@ -113,9 +118,10 @@ static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
113118
BFS_I(inode)->i_eblock = 0;
114119
insert_inode_hash(inode);
115120
mark_inode_dirty(inode);
116-
dump_imap("create",s);
121+
dump_imap("create", s);
117122

118-
err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
123+
err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len,
124+
inode->i_ino);
119125
if (err) {
120126
inode_dec_link_count(inode);
121127
iput(inode);
@@ -127,11 +133,12 @@ static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
127133
return 0;
128134
}
129135

130-
static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
136+
static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
137+
struct nameidata *nd)
131138
{
132-
struct inode * inode = NULL;
133-
struct buffer_head * bh;
134-
struct bfs_dirent * de;
139+
struct inode *inode = NULL;
140+
struct buffer_head *bh;
141+
struct bfs_dirent *de;
135142

136143
if (dentry->d_name.len > BFS_NAMELEN)
137144
return ERR_PTR(-ENAMETOOLONG);
@@ -152,13 +159,15 @@ static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, st
152159
return NULL;
153160
}
154161

155-
static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
162+
static int bfs_link(struct dentry *old, struct inode *dir,
163+
struct dentry *new)
156164
{
157-
struct inode * inode = old->d_inode;
165+
struct inode *inode = old->d_inode;
158166
int err;
159167

160168
lock_kernel();
161-
err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
169+
err = bfs_add_entry(dir, new->d_name.name, new->d_name.len,
170+
inode->i_ino);
162171
if (err) {
163172
unlock_kernel();
164173
return err;
@@ -172,23 +181,23 @@ static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new
172181
return 0;
173182
}
174183

175-
176-
static int bfs_unlink(struct inode * dir, struct dentry * dentry)
184+
static int bfs_unlink(struct inode *dir, struct dentry *dentry)
177185
{
178186
int error = -ENOENT;
179-
struct inode * inode;
180-
struct buffer_head * bh;
181-
struct bfs_dirent * de;
187+
struct inode *inode;
188+
struct buffer_head *bh;
189+
struct bfs_dirent *de;
182190

183191
inode = dentry->d_inode;
184192
lock_kernel();
185193
bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
186-
if (!bh || le16_to_cpu(de->ino) != inode->i_ino)
194+
if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
187195
goto out_brelse;
188196

189197
if (!inode->i_nlink) {
190-
printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode->i_sb->s_id,
191-
inode->i_ino, inode->i_nlink);
198+
printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
199+
inode->i_sb->s_id, inode->i_ino,
200+
inode->i_nlink);
192201
inode->i_nlink = 1;
193202
}
194203
de->ino = 0;
@@ -205,12 +214,12 @@ static int bfs_unlink(struct inode * dir, struct dentry * dentry)
205214
return error;
206215
}
207216

208-
static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
209-
struct inode * new_dir, struct dentry * new_dentry)
217+
static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
218+
struct inode *new_dir, struct dentry *new_dentry)
210219
{
211-
struct inode * old_inode, * new_inode;
212-
struct buffer_head * old_bh, * new_bh;
213-
struct bfs_dirent * old_de, * new_de;
220+
struct inode *old_inode, *new_inode;
221+
struct buffer_head *old_bh, *new_bh;
222+
struct bfs_dirent *old_de, *new_de;
214223
int error = -ENOENT;
215224

216225
old_bh = new_bh = NULL;
@@ -223,7 +232,7 @@ static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
223232
old_dentry->d_name.name,
224233
old_dentry->d_name.len, &old_de);
225234

226-
if (!old_bh || le16_to_cpu(old_de->ino) != old_inode->i_ino)
235+
if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
227236
goto end_rename;
228237

229238
error = -EPERM;
@@ -239,7 +248,8 @@ static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
239248
if (!new_bh) {
240249
error = bfs_add_entry(new_dir,
241250
new_dentry->d_name.name,
242-
new_dentry->d_name.len, old_inode->i_ino);
251+
new_dentry->d_name.len,
252+
old_inode->i_ino);
243253
if (error)
244254
goto end_rename;
245255
}
@@ -268,11 +278,12 @@ const struct inode_operations bfs_dir_inops = {
268278
.rename = bfs_rename,
269279
};
270280

271-
static int bfs_add_entry(struct inode * dir, const unsigned char * name, int namelen, int ino)
281+
static int bfs_add_entry(struct inode *dir, const unsigned char *name,
282+
int namelen, int ino)
272283
{
273-
struct buffer_head * bh;
274-
struct bfs_dirent * de;
275-
int block, sblock, eblock, off, eoff;
284+
struct buffer_head *bh;
285+
struct bfs_dirent *de;
286+
int block, sblock, eblock, off, pos;
276287
int i;
277288

278289
dprintf("name=%s, namelen=%d\n", name, namelen);
@@ -284,27 +295,24 @@ static int bfs_add_entry(struct inode * dir, const unsigned char * name, int nam
284295

285296
sblock = BFS_I(dir)->i_sblock;
286297
eblock = BFS_I(dir)->i_eblock;
287-
eoff = dir->i_size % BFS_BSIZE;
288-
for (block=sblock; block<=eblock; block++) {
298+
for (block = sblock; block <= eblock; block++) {
289299
bh = sb_bread(dir->i_sb, block);
290-
if(!bh)
300+
if (!bh)
291301
return -ENOSPC;
292-
for (off=0; off<BFS_BSIZE; off+=BFS_DIRENT_SIZE) {
302+
for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
293303
de = (struct bfs_dirent *)(bh->b_data + off);
294-
if (block==eblock && off>=eoff) {
295-
/* Do not read/interpret the garbage in the end of eblock. */
296-
de->ino = 0;
297-
}
298304
if (!de->ino) {
299-
if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
305+
pos = (block - sblock) * BFS_BSIZE + off;
306+
if (pos >= dir->i_size) {
300307
dir->i_size += BFS_DIRENT_SIZE;
301308
dir->i_ctime = CURRENT_TIME_SEC;
302309
}
303310
dir->i_mtime = CURRENT_TIME_SEC;
304311
mark_inode_dirty(dir);
305312
de->ino = cpu_to_le16((u16)ino);
306-
for (i=0; i<BFS_NAMELEN; i++)
307-
de->name[i] = (i < namelen) ? name[i] : 0;
313+
for (i = 0; i < BFS_NAMELEN; i++)
314+
de->name[i] =
315+
(i < namelen) ? name[i] : 0;
308316
mark_buffer_dirty(bh);
309317
brelse(bh);
310318
return 0;
@@ -315,25 +323,26 @@ static int bfs_add_entry(struct inode * dir, const unsigned char * name, int nam
315323
return -ENOSPC;
316324
}
317325

318-
static inline int bfs_namecmp(int len, const unsigned char * name, const char * buffer)
326+
static inline int bfs_namecmp(int len, const unsigned char *name,
327+
const char *buffer)
319328
{
320-
if (len < BFS_NAMELEN && buffer[len])
329+
if ((len < BFS_NAMELEN) && buffer[len])
321330
return 0;
322331
return !memcmp(name, buffer, len);
323332
}
324333

325-
static struct buffer_head * bfs_find_entry(struct inode * dir,
326-
const unsigned char * name, int namelen, struct bfs_dirent ** res_dir)
334+
static struct buffer_head *bfs_find_entry(struct inode *dir,
335+
const unsigned char *name, int namelen,
336+
struct bfs_dirent **res_dir)
327337
{
328-
unsigned long block, offset;
329-
struct buffer_head * bh;
330-
struct bfs_dirent * de;
338+
unsigned long block = 0, offset = 0;
339+
struct buffer_head *bh = NULL;
340+
struct bfs_dirent *de;
331341

332342
*res_dir = NULL;
333343
if (namelen > BFS_NAMELEN)
334344
return NULL;
335-
bh = NULL;
336-
block = offset = 0;
345+
337346
while (block * BFS_BSIZE + offset < dir->i_size) {
338347
if (!bh) {
339348
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,
344353
}
345354
de = (struct bfs_dirent *)(bh->b_data + offset);
346355
offset += BFS_DIRENT_SIZE;
347-
if (le16_to_cpu(de->ino) && bfs_namecmp(namelen, name, de->name)) {
356+
if (le16_to_cpu(de->ino) &&
357+
bfs_namecmp(namelen, name, de->name)) {
348358
*res_dir = de;
349359
return bh;
350360
}

0 commit comments

Comments
 (0)