Skip to contentSkip to conversation

filesystem: VMUFAT filesystem

RFC PATCH In review Adrian McMenamin 5 participants 7 messages Feb 14, 2009 – Mar 5, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 filesystem: VMUFAT filesystem

The SEGA Dreamcast visual memory unit implements a file allocation table based filesystem which is somewhat similar to FAT16. This filesystem code implements that filesystem and I have used it to successfully manage Dreamcast VMUs and VMU images mounted via loop.

You can read more about the filesystem here:
http://mc.pp.se/dc/vms/flashmem.html

I intend to fully document the filesystem and write an appropriate user tool to create a drive image, but in the meantime here is an early cut of the filesystem itself for comment.

(A version of this for the 2.4 kernel existed in the Dreamcast CVS on Sourceforge but wasn't truly hardware independent).

 fs/Kconfig         |    1 +
 fs/Makefile        |    1 +
 fs/vmufat/Kconfig  |   14 +
 fs/vmufat/Makefile |    7 +
 fs/vmufat/inode.c  |  702 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/vmufat/super.c  |  533 +++++++++++++++++++++++++++++++++++++++
 fs/vmufat/vmufat.h |   58 +++++

---

fs/Kconfig +1 -0
index 93945dd..6c108b1 100644
@@ -222,6 +222,7 @@ source "fs/qnx4/Kconfig"
 source "fs/romfs/Kconfig"
 source "fs/sysv/Kconfig"
 source "fs/ufs/Kconfig"
+source "fs/vmufat/Kconfig"
 
 endif # MISC_FILESYSTEMS
 
fs/Makefile +1 -0
index 38bc735..196c9dc 100644
@@ -122,3 +122,4 @@ obj-$(CONFIG_DEBUG_FS)		+= debugfs/
 obj-$(CONFIG_OCFS2_FS)		+= ocfs2/
 obj-$(CONFIG_BTRFS_FS)		+= btrfs/
 obj-$(CONFIG_GFS2_FS)           += gfs2/
+obj-$(CONFIG_VMUFAT_FS)		+= vmufat/
fs/vmufat/Kconfig new file +14 -0
new file mode 100644
index 0000000..bdad4c6
@@ -0,0 +1,14 @@
+config VMUFAT_FS
+	tristate "Dreamcast VMU FAT filesystem"
+	depends on BLOCK
+	help
+	  This implements the simple FAT type filesystem found on SEGA
+	  Dreamcast visual memory units.
+
+	  Dreamcast users who want to mount their VMUs to view the native
+	  filesystem will say 'Y' here. The filesystem is hardware independent
+	  but is not recommended for any serious use in other circumstances, so
+	  just about everyone else should say 'N'.
+
+	  To compile this as a module say 'M' here. The module will be called
+	  vmufat
fs/vmufat/Makefile new file +7 -0
new file mode 100644
index 0000000..e7f9bbf
@@ -0,0 +1,7 @@
+#
+# Makefile for VMUFAT filesystem
+#
+
+obj-$(CONFIG_VMUFAT_FS) += vmufat.o
+
+vmufat-objs := super.o inode.o
fs/vmufat/inode.c new file +702 -0

702 lines not on this page · raw message

fs/vmufat/super.c new file +533 -0

533 lines not on this page · raw message

fs/vmufat/vmufat.h new file +58 -0

58 lines not on this page · raw message

#1 lore raw

Hi Adrian.

Quoted 18 lines · Adrian McMenamin

The SEGA Dreamcast visual memory unit implements a file allocation table based filesystem which is somewhat similar to FAT16. This filesystem code implements that filesystem and I have used it to successfully manage Dreamcast VMUs and VMU images mounted via loop.

You can read more about the filesystem here:
http://mc.pp.se/dc/vms/flashmem.html

I intend to fully document the filesystem and write an appropriate user tool to create a drive image, but in the meantime here is an early cut of the filesystem itself for comment.

fs/vmufat/inode.c new file +6 -0
+struct dentry *vmufat_inode_lookup(struct inode *in, struct dentry *dent,
+	struct nameidata *nd)
+{
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct buffer_head *bh;

fs/vmufat/inode.c line 33

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+	struct inode *ino;
+	char name[VMUFAT_NAMELEN];
+	long blck_read;
+	int error = 0, fno = 0, filenamelen;
+
+	if (dent->d_name.len > VMUFAT_NAMELEN) {
+		error = -ENAMETOOLONG;
+		goto out;
+	}
+

Cool, only 12 bytes names.

fs/vmufat/inode.c line 35

Quoted 2 lines
fs/vmufat/inode.c new file +2 -0
+	sb = in->i_sb;
+	vmudetails = (struct memcard *)sb->s_fs_info;

No need for the additional cast.

fs/vmufat/inode.c line 46

Quoted 11 lines
fs/vmufat/inode.c new file +11 -0
+	blck_read = vmudetails->dir_bnum;
+
+	bh = vmufat_sb_bread(sb, blck_read);
+	if (!bh) {
+		error = -EIO;
+		goto out;
+	}
+
+	do {
+		/* Have we got a file? */
+		if (((__u8 *)bh->b_data)[(fno % 0x10) * 0x20] == 0) {

This % and * pattern is used all over the place, can it be into some common helper?

fs/vmufat/inode.c line 55

Quoted 9 lines
fs/vmufat/inode.c new file +9 -0
+			error = -ENOENT;
+			goto release_bh;
+		}
+
+		/* get file name */
+		memcpy(name,
+		       bh->b_data + 4 + (fno % 0x10) * 0x20, VMUFAT_NAMELEN);
+		/* do names match ?*/
+		filenamelen = strlen(dent->d_name.name);

You can use dent->d_name.len

Quoted 19 lines
fs/vmufat/inode.c new file +19 -0
+static int vmufat_inode_create(struct inode *dir, struct dentry *de,
+		int imode, struct nameidata *nd)
+{
+	/* Create an inode */
+	int y, z;
+	long nextblock, blck_read;
+	struct inode *inode;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct buffer_head *bh_fat, *bh;
+	unsigned long unix_date;
+	int year, day, nl_day, month;	/*inspired by FAT driver */
+	__u16 fatdata;
+	__u8 century, u8year;
+
+
+	if (de->d_name.len > VMUFAT_NAMELEN)
+		return -ENAMETOOLONG;
+

fs/vmufat/inode.c line 127

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+	sb = dir->i_sb;
+	vmudetails = (struct memcard *) sb->s_fs_info;
+
+	inode = new_inode(sb);
+	if (!inode)
+		return -ENOSPC;
+
+	/* Walk through blocks looking for place to write
+	* Is this an executible file? */
+	if (imode & 73) {	/*Octal 111 */

Some nice-looking define for the mask?

fs/vmufat/inode.c line 133

Quoted 6 lines
fs/vmufat/inode.c new file +6 -0
+		inode->i_ino = VMUFAT_ZEROBLOCK;
+		/* But this already allocated? */
+		bh_fat =
+		    vmufat_sb_bread(sb, vmudetails->fat_bnum);
+		if (!bh_fat)
+			return -EIO;

Inode leak.

fs/vmufat/inode.c line 141

Quoted 8 lines
fs/vmufat/inode.c new file +8 -0
+
+		fatdata = ((__u16 *) bh_fat->b_data)[0];
+		if (fatdata != 0xfffc) {
+			printk(KERN_ERR
+				"vmufat: cannot write executible file to"
+				" filesystem - block 0 already allocated.\n");
+			brelse(bh_fat);
+			return -ENOSPC;

Inode leak. There are several others in the error pathes below. Please use goto and single exit point.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		}
+

Lost newline.

Quoted 52 lines
fs/vmufat/inode.c new file +52 -0
+	} else {
+		/*Look for a free block in the FAT */
+		nextblock = vmudetails->numblocks - 1;
+		bh_fat =
+		    vmufat_sb_bread(sb, vmudetails->fat_bnum);
+		if (!bh_fat)
+			return -EIO;
+
+		do {
+			fatdata = ((__u16 *) bh_fat->b_data)[nextblock];
+			if (fatdata == 0xfffc)
+				break;	/*empty block */
+			if (--nextblock < 0)
+				break;
+		} while (1);
+
+		if (nextblock < 0) {
+			iput(inode);
+			brelse(bh_fat);
+			return -ENOSPC;
+		}
+		inode->i_ino = nextblock;
+	}
+	brelse(bh_fat);
+
+	inode->i_uid = 0;
+	inode->i_gid = 0;
+	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
+	inode->i_mode = imode;
+	inode->i_blocks = 0;
+	inode->i_sb = sb;
+	insert_inode_hash(inode);
+	mark_inode_dirty(inode);
+	inode->i_op = &vmufat_inode_operations;
+	inode->i_fop = &vmufat_file_operations;
+
+	/* Write to the directory
+	* Now search for space for the directory entry */
+	blck_read = vmudetails->dir_bnum;
+	bh = vmufat_sb_bread(sb, blck_read);
+	if (!bh)
+		return -EIO;
+
+	for (y = 0; y < (vmudetails->dir_len * 0x10); y++) {
+		if ((y / 0x10) > (vmudetails->dir_bnum - blck_read)) {
+			brelse(bh);
+			blck_read--;
+			bh = vmufat_sb_bread(sb, blck_read);
+			if (!bh)
+				return -EIO;
+		}
+		if (((bh->b_data)[(y % 0x10) * 0x20]) == 0)

fs/vmufat/inode.c line 205

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+			break;
+	}
+	/* Have the directory entry
+	 * so now update it */
+	z = (y % 0x10) * 0x20;
+	if (inode->i_ino != VMUFAT_ZEROBLOCK)
+		bh->b_data[z] = 0x33;	/* data file */
+	else
+		bh->b_data[z] = 0xcc;
+

Plsase create a symbolic names for the common constants.

Quoted 66 lines
fs/vmufat/inode.c new file +66 -0
+	if ((bh->b_data[z + 1] != (char) 0x00) &&
+		(bh->b_data[z + 1] != (char) 0xff))
+		bh->b_data[z + 1] = (char) 0x00;
+
+	if (inode->i_ino != VMUFAT_ZEROBLOCK) {
+		((__u16 *) bh->b_data)[z / 2 + 1] =
+		    cpu_to_le16(inode->i_ino);
+		((__u16 *) bh->b_data)[z / 2 + 0x0D] = 0;
+	} else {
+		((__u16 *) bh->b_data)[z / 2 + 1] = 0;
+		((__u16 *) bh->b_data)[z / 2 + 0x0D] = 1;
+	}
+
+	/* Name */
+	memset((char *) (bh->b_data + z + 0x04), '\0', 0x0C);
+	memcpy((char *) (bh->b_data + z + 0x04), ((de->d_name).name),
+	       de->d_name.len);
+
+	/* BCD timestamp it */
+	unix_date = CURRENT_TIME.tv_sec;
+	day = unix_date / 86400 - 3652;
+	year = day / 365;
+
+	if ((year + 3) / 4 + 365 * year > day)
+		year--;
+
+	day -= (year + 3) / 4 + 365 * year;
+	if (day == 59 && !(year & 3)) {
+		nl_day = day;
+		month = 2;
+	} else {
+		nl_day = (year & 3) || day <= 59 ? day : day - 1;
+		for (month = 0; month < 12; month++)
+			if (day_n[month] > nl_day)
+				break;
+	}
+
+	century = 19;
+	if (year > 19)
+		century = 20;
+
+	bh->b_data[z + 0x10] = bcd_from_u8(century);
+	u8year = year + 80;
+	if (u8year > 99)
+		u8year = u8year - 100;
+
+	bh->b_data[z + 0x11] = bcd_from_u8(u8year);
+	bh->b_data[z + 0x12] = bcd_from_u8((__u8) month);
+	bh->b_data[z + 0x13] =
+	    bcd_from_u8((__u8) day - day_n[month - 1] + 1);
+	bh->b_data[z + 0x14] =
+	    bcd_from_u8((__u8) ((unix_date / 3600) % 24));
+	bh->b_data[z + 0x15] = bcd_from_u8((__u8) ((unix_date / 60) % 60));
+	bh->b_data[z + 0x16] = bcd_from_u8((__u8) (unix_date % 60));
+
+	((__u16 *) bh->b_data)[z / 2 + 0x0C] =
+	    cpu_to_le16(inode->i_blocks);
+
+	inode->i_mtime.tv_sec = unix_date;
+	mark_buffer_dirty(bh);
+	brelse(bh);
+
+	d_instantiate(de, inode);
+
+	return 0;
+}

fs/vmufat/inode.c line 281

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+
+
+static int vmufat_inode_rename(struct inode *in_source,
+			      struct dentry *de_source,
+			      struct inode *in_target,
+			      struct dentry *de_target)
+{
+	return -EPERM;
+}
+

Just do not implement the function if it is not supported.

Quoted 36 lines
fs/vmufat/inode.c new file +36 -0
+const struct inode_operations vmufat_inode_operations = {
+	.lookup =	vmufat_inode_lookup,
+	.unlink =	vmufat_inode_unlink,
+	.create =	vmufat_inode_create,
+	.rename =	vmufat_inode_rename,
+};
+
+
+static int vmufat_readdir(struct file *filp, void *dirent, filldir_t filldir)
+{
+	int filenamelen, i;
+	struct vmufat_file_info *saved_file = NULL;
+	struct dentry *dentry = filp->f_dentry;
+	struct inode *inode = dentry->d_inode;
+	struct super_block *sb = inode->i_sb;
+	struct memcard *vmudetails =
+	    ((struct memcard *) sb->s_fs_info);
+	struct buffer_head *bh;
+
+	int blck_read = vmudetails->dir_bnum;
+	bh = vmufat_sb_bread(sb, blck_read);
+	if (!bh)
+		return -EIO;
+
+	i = filp->f_pos;
+
+	switch ((unsigned int) filp->f_pos) {
+	case 0:
+		if (filldir(dirent, ".", 1, i++, inode->i_ino, DT_DIR) < 0)
+			goto finish;
+
+		filp->f_pos++;
+	case 1:
+		if (filldir(dirent, "..", 2, i++,
+			    dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
+			goto finish;

fs/vmufat/inode.c line 327

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+
+		filp->f_pos++;
+	default:
+		break;
+	}
+
+	/* wander through the Directory and find files */
+	if ((i - 2) > (vmudetails->dir_len * 0x10)) {
+		brelse(bh);
+		return -1;

-ENOENT?

fs/vmufat/inode.c line 332

Quoted 5 lines
fs/vmufat/inode.c new file +5 -0
+	}
+
+	saved_file =
+	    kmalloc(sizeof(struct vmufat_file_info), GFP_KERNEL);
+

Need to check the return value.

Quoted 15 lines
fs/vmufat/inode.c new file +15 -0
+	do {
+		if ((i - 2) / 0x10 > (vmudetails->dir_bnum - blck_read)) {
+			/* move to next block in directory */
+			brelse(bh);
+			blck_read--;
+			bh = vmufat_sb_bread(sb, blck_read);
+			if (!bh) {
+				kfree(saved_file);
+				return -EIO;
+			}
+		}
+
+		saved_file->ftype = bh->b_data[((i - 2) % 0x10) * 0x20];
+
+		if (saved_file->ftype == 0)

fs/vmufat/inode.c line 357

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+			break;
+
+		saved_file->fblk =
+		    le16_to_cpu(((__u16 *) bh->b_data)[1 +
+				((i - 2) % 0x10) * 0x10]);
+		if (saved_file->fblk == 0)
+			saved_file->fblk = VMUFAT_ZEROBLOCK;
+
+		memcpy(saved_file->fname,
+		       bh->b_data + 4 + ((i - 2) % 0x10) * 0x20, 12);

VMUFAT_NAMELEN instead of 12?

Quoted 27 lines
fs/vmufat/inode.c new file +27 -0
+		filenamelen = strlen(saved_file->fname);
+		if (filenamelen > VMUFAT_NAMELEN)
+			filenamelen = VMUFAT_NAMELEN;
+		if (filldir
+		    (dirent, saved_file->fname, filenamelen, i++,
+		     saved_file->fblk, DT_REG) < 0) {
+			goto finish;
+		}
+
+		filp->f_pos++;
+	} while (1);
+
+finish:
+
+	kfree(saved_file);
+	brelse(bh);
+
+	return 0;
+}
+
+const struct file_operations vmufat_file_dir_operations = {
+	.owner =	THIS_MODULE,
+	.read =		generic_read_dir,
+	.readdir =	vmufat_readdir,
+	.fsync =	file_fsync,
+};
+

fs/vmufat/inode.c line 394

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+static int vmufat_game_write(struct file *file, const char *buf, char *writebuf,
+		     size_t count, loff_t *ppos)
+{
+	__u16 fatdata;
+	struct buffer_head *bh_fat, *bh;
+	struct inode *in = (file->f_dentry)->d_inode;
+	struct super_block *sb = in->i_sb;
+	struct memcard *vmudetails =
+	    ((struct memcard *) sb->s_fs_info);
+

You may want to create a helper function for this common access pattern or just dereference void s_fs_info pointer without the cast.

fs/vmufat/inode.c line 407

Quoted 13 lines
fs/vmufat/inode.c new file +13 -0
+	unsigned long blkoffset = *ppos >> in->i_sb->s_blocksize_bits;
+	if (blkoffset < 1) {
+		/* Additional sanity check */
+		kfree(writebuf);
+		return -EIO;
+	}
+
+	/* Is the next block free in the VMU? */
+	bh_fat = vmufat_sb_bread(in->i_sb, vmudetails->fat_bnum);
+	if (!bh_fat) {
+		kfree(writebuf);
+		return -EIO;
+	}

Please use single exit path and number of gotos, this allows to eliminate errors and makes code more readable.

Quoted 19 lines
fs/vmufat/inode.c new file +19 -0
+	fatdata = ((__u16 *) bh_fat->b_data)[(__u16) blkoffset];
+
+	if (fatdata != 0xfffc) {
+		printk(KERN_ERR
+			"vmufat: Cannot save game file - insufficient"
+			" linear space\n");
+		kfree(writebuf);
+		return -EFBIG;
+	}
+
+	/*Now we have the space - write the block out */
+	bh = vmufat_sb_bread(sb, blkoffset);
+	if (!bh) {
+		brelse(bh_fat);
+		kfree(writebuf);
+		return -EIO;
+	}
+	if (count < sb->s_blocksize)
+		memset((char *) (bh->b_data), '\0', sb->s_blocksize);

fs/vmufat/inode.c line 436

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+	memcpy((char *) (bh->b_data), writebuf, count);
+	mark_buffer_dirty(bh);
+	brelse(bh);
+	in->i_size += count;
+	in->i_blocks++;
+
+	/*Now update the FAT */
+	((__u16 *) (bh_fat->b_data))[(__u16) blkoffset] = 0xfffa;
+	((__u16 *) (bh_fat->b_data))[((__u16) (blkoffset - 1))] =
+	    cpu_to_le16(blkoffset);

Please dereference it to the u16 pointer earlier.

Quoted 16 lines
fs/vmufat/inode.c new file +16 -0
+	mark_buffer_dirty(bh_fat);
+
+	brelse(bh_fat);
+
+	kfree(writebuf);
+	*ppos += count;
+	return count;
+}
+
+static ssize_t vmufat_file_write(struct file *file, const char *buf, size_t count,
+			 loff_t *ppos)
+{
+	struct buffer_head *bh, *bh_fat;
+	unsigned long inode_num, currentblock, nextblock;
+	char *writebuf;
+	int previousblock;

fs/vmufat/inode.c line 462

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+	__u16 fatdata;
+	struct inode *in = (file->f_dentry)->d_inode;
+	struct super_block *sb = in->i_sb;
+	struct memcard *vmudetails =
+	    ((struct memcard *) sb->s_fs_info);
+	unsigned long blkoffset = *ppos >> in->i_sb->s_blocksize_bits;
+
+	if ((ssize_t) count < 0)
+		return -ENOENT;
+

Why is this needed?

fs/vmufat/inode.c line 473

Quoted 11 lines
fs/vmufat/inode.c new file +11 -0
+	/* We will assume that all files -
+	 * unless having inode value of 0
+	 * are data files
+	 */
+
+	/* copy buffer */
+	if (count > sb->s_blocksize)
+		count = sb->s_blocksize;
+	writebuf = kzalloc(count, GFP_KERNEL);
+	copy_from_user(writebuf, buf, count);
+

Need to check return value for both calls
in case of fault or low memory.

Quoted 6 lines
fs/vmufat/inode.c new file +6 -0
+	/* Handle game files */
+	inode_num = in->i_ino;
+	if (inode_num == VMUFAT_ZEROBLOCK)
+		inode_num = 0;
+
+	/*Start by writing out to first block */

fs/vmufat/inode.c line 489

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+	if (blkoffset == 0) {
+		currentblock = inode_num;
+		bh = vmufat_sb_bread(sb, inode_num);
+		if (!bh) {
+			kfree(writebuf);
+			return -EIO;
+		}
+		if (count < sb->s_blocksize)
+			memset((char *) (bh->b_data), '\0', sb->s_blocksize);
+		memcpy((char *) (bh->b_data), writebuf, count);

No need for the cast.

That's all for now. Overall there is a fair numebr of leaks because of the missed freeing in the error path and lots of duplicated code because of the multiple error exit points. Also you frequently do not check the return value of the functions which may fail.

Signature · 1 line
	Evgeniy Polyakov
#2 lore raw
Quoted 1 line · Evgeniy Polyakov

Hi Adrian.

Hi

Quoted 9 lines

That's all for now. Overall there is a fair numebr of leaks because of the missed freeing in the error path and lots of duplicated code because of the multiple error exit points. Also you frequently do not check the return value of the functions which may fail.

--
Evgeniy Polyakov

Thanks for the comments. Very useful. I ported this over from some truly abysmal code I'd written years ago and clearly didn't do a good enough job :)

#3 lore raw
In reply to Adrian McMenamin

fs/vmufat/inode.c line 17

Quoted 9 lines · Adrian McMenamin
fs/vmufat/inode.c new file +9 -0
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/blkdev.h>
+#include <linux/buffer_head.h>
+#include <linux/mtd/mtd.h>
+#include "vmufat.h"
+

Why do you have an mtd include?

Quoted 3 lines
fs/vmufat/inode.c new file +3 -0
+struct dentry *vmufat_inode_lookup(struct inode *in, struct dentry *dent,
+	struct nameidata *nd)
+{

[snip]

fs/vmufat/inode.c line 35

Quoted 2 lines
fs/vmufat/inode.c new file +2 -0
+	sb = in->i_sb;
+	vmudetails = (struct memcard *)sb->s_fs_info;

All of your sb->s_fs_info casts are superfluous. Do not cast void pointers.

fs/vmufat/inode.c line 64

Quoted 4 lines
fs/vmufat/inode.c new file +4 -0
+			if (!ino || IS_ERR(ino)) {
+				error = -EACCES;
+				goto release_bh;
+			}

In the IS_ERR case, you can use PTR_ERR for setting the error value. Throughout most of this code you completely ignore the error value that is handed down and invent your own instead, making debugging far more painful than it needs to be.

fs/vmufat/inode.c line 75

Quoted 4 lines
fs/vmufat/inode.c new file +4 -0
+		/* do we need to move to the next block in the directory? */
+		if ((fno / 0x10) > (vmudetails->dir_bnum - blck_read)) {
+			brelse(bh);
+			blck_read--;

Was the extra vowel really that much more work to write out?

Quoted 27 lines
fs/vmufat/inode.c new file +27 -0
+	/* BCD timestamp it */
+	unix_date = CURRENT_TIME.tv_sec;
+	day = unix_date / 86400 - 3652;
+	year = day / 365;
+
+	if ((year + 3) / 4 + 365 * year > day)
+		year--;
+
+	day -= (year + 3) / 4 + 365 * year;
+	if (day == 59 && !(year & 3)) {
+		nl_day = day;
+		month = 2;
+	} else {
+		nl_day = (year & 3) || day <= 59 ? day : day - 1;
+		for (month = 0; month < 12; month++)
+			if (day_n[month] > nl_day)
+				break;
+	}
+
+	century = 19;
+	if (year > 19)
+		century = 20;
+
+	bh->b_data[z + 0x10] = bcd_from_u8(century);
+	u8year = year + 80;
+	if (u8year > 99)
+		u8year = u8year - 100;

fs/vmufat/inode.c line 260

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+
+	bh->b_data[z + 0x11] = bcd_from_u8(u8year);
+	bh->b_data[z + 0x12] = bcd_from_u8((__u8) month);
+	bh->b_data[z + 0x13] =
+	    bcd_from_u8((__u8) day - day_n[month - 1] + 1);
+	bh->b_data[z + 0x14] =
+	    bcd_from_u8((__u8) ((unix_date / 3600) % 24));
+	bh->b_data[z + 0x15] = bcd_from_u8((__u8) ((unix_date / 60) % 60));
+	bh->b_data[z + 0x16] = bcd_from_u8((__u8) (unix_date % 60));
+

This should be abstracted out in to a separate function, and you can get rid of most of this hand-rolled time mangling by using the rtc lib routines.

Additionally, all of the bcd conversion code is superfluous, since you can include linux/bcd.h and use bin2bcd and bcd2bin directly.

fs/vmufat/inode.c line 281

Quoted 8 lines
fs/vmufat/inode.c new file +8 -0
+static int vmufat_inode_rename(struct inode *in_source,
+			      struct dentry *de_source,
+			      struct inode *in_target,
+			      struct dentry *de_target)
+{
+	return -EPERM;
+}
+

Just get rid of this if you aren't going to support it.

fs/vmufat/inode.c line 332

Quoted 3 lines
fs/vmufat/inode.c new file +3 -0
+	saved_file =
+	    kmalloc(sizeof(struct vmufat_file_info), GFP_KERNEL);
+

Error handling?

fs/vmufat/inode.c line 530

Quoted 3 lines
fs/vmufat/inode.c new file +3 -0
+	}
+	/* Now every other block */
+	else {

Please refrain from adopting magical coding styles.

fs/vmufat/inode.c line 83

Quoted 8 lines
fs/vmufat/inode.c new file +8 -0
+			}
+
+		}
+
+
+	}
+
+

Lots of superfluous whitespace.

fs/vmufat/inode.c line 650

Quoted 3 lines
fs/vmufat/inode.c new file +3 -0
+	readbuf = kzalloc(count, GFP_KERNEL);
+	/* Traverse through FAT to read the blocks in */
+	x = 0;

Again, no error handling.

fs/vmufat/inode.c line 694

Quoted 5 lines
fs/vmufat/inode.c new file +5 -0
+static int vmufat_file_open(struct inode *in, struct file *f)
+{
+	return 0;
+}
+

You should be able to kill this off, also.

Quoted 5 lines
fs/vmufat/super.c new file +5 -0
+__u8 bcd_from_u8(__u8 num)
+{
+	__u8 topnib = num / 10;
+	__u8 botnib = num % 10;
+	return topnib << 4 | botnib;

fs/vmufat/super.c line 41

Quoted 10 lines
fs/vmufat/super.c new file +10 -0
+}
+
+inline int int_from_bcd(__u8 bcd)
+{
+	int topnib = (bcd >> 4) & 0x000f;
+	int botnib = bcd & 0x000f;
+
+	return (topnib * 10) + botnib;
+}
+

As already mentioned, these are already in linux/bcd.h.

fs/vmufat/super.c line 199

Quoted 6 lines
fs/vmufat/super.c new file +6 -0
+static void vmufat_put_super(struct super_block *sb)
+{
+	struct memcard *vmudetails;
+	sb->s_dev = 0;
+	vmudetails = (struct memcard *) (sb->s_fs_info);
+	kfree(vmudetails);

vmudetails is completely unused here, just kfree sb->s_fs_info.

Quoted 29 lines
fs/vmufat/super.c new file +29 -0
+	/* BCD timestamp it */
+	unix_date = CURRENT_TIME.tv_sec;
+	day = unix_date / 86400 - 3652;
+	year = day / 365;
+	if ((year + 3) / 4 + 365 * year > day)
+		year--;
+	day -= (year + 3) / 4 + 365 * year;
+	if (day == 59 && !(year & 3)) {
+		nl_day = day;
+		month = 2;
+	} else {
+		nl_day = (year & 3) || day <= 59 ? day : day - 1;
+		for (month = 0; month < 12; month++)
+			if (day_n[month] > nl_day)
+				break;
+	}
+
+	century = 19;
+	if (year > 19)
+		century = 20;
+	bh->b_data[z + 0x10] = bcd_from_u8(century);
+	u8year = year + 80;
+	if (u8year > 99)
+		u8year = u8year - 100;
+	bh->b_data[z + 0x11] = bcd_from_u8(u8year);
+	bh->b_data[z + 0x12] = bcd_from_u8((__u8) month);
+	bh->b_data[z + 0x13] =
+	    bcd_from_u8((__u8) day - day_n[month - 1] + 1);
+	bh->b_data[z + 0x14] =

fs/vmufat/super.c line 407

Quoted 10 lines
fs/vmufat/super.c new file +10 -0
+	    bcd_from_u8((__u8) ((unix_date / 3600) % 24));
+	bh->b_data[z + 0x15] = bcd_from_u8((__u8) ((unix_date / 60) % 60));
+	bh->b_data[z + 0x16] = bcd_from_u8((__u8) (unix_date % 60));
+
+	((__u16 *) bh->b_data)[z / 2 + 0x0C] = cpu_to_le16(in->i_blocks);
+	if (inode_num != 0)
+		((__u16 *) bh->b_data)[z / 2 + 0x0D] = 0;
+	else /* game */
+		((__u16 *) bh->b_data)[z / 2 + 0x0D] = cpu_to_le16(1);
+	in->i_mtime.tv_sec = unix_date;

Again, all of this can be simplified using rtc lib and bcd routines.

fs/vmufat/super.c line 424

Quoted 4 lines
fs/vmufat/super.c new file +4 -0
+static int check_sb_format(struct buffer_head *bh)
+{
+	__u32 s_magic = 0x55555555;
+

This magic value is used in lots of places, you should have a define for it, and add it to linux/magic.h.

fs/vmufat/super.c line 429

Quoted 5 lines
fs/vmufat/super.c new file +5 -0
+	if (!((((__u32 *) bh->b_data)[0] == s_magic)
+	      && (((__u32 *) bh->b_data)[1] == s_magic)
+	      && (((__u32 *) bh->b_data)[2] == s_magic)
+	      && (s_magic == ((__u32 *) bh->b_data)[3])))
+		return 0;

&&'s at the end of the line. You can also switch the if to a return and kill off the else.

fs/vmufat/super.c line 467

Quoted 3 lines
fs/vmufat/super.c new file +3 -0
+	vmudata = kmalloc(sizeof(struct memcard), GFP_KERNEL);
+
+	/* user blocks */

No error handling again. You will want to verify all of your kmallocs, since you seem to have this issue a lot.

fs/vmufat/super.c line 481

Quoted 4 lines
fs/vmufat/super.c new file +4 -0
+	while ((erasesize /= 2) != 0)
+		log_2++;	/* thanks to MR Brown */
+
+	sb->s_blocksize_bits = log_2;

ilog2()?

I've tried to skip over the bits already noted by Evgeniy, but you may have already fixed up some of the issues noted above anyways.

Also, in the next iteration of this patch, please do not break the Cc list and send multiple times to different lists, it makes it very difficult to follow what is going on, especially if the threads of conversation diverge.

#4 lore raw
Quoted 11 lines · Paul Mundt

fs/vmufat/inode.c line 17

Quoted 9 lines · Adrian McMenamin
fs/vmufat/inode.c new file +9 -0
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/blkdev.h>
+#include <linux/buffer_head.h>
+#include <linux/mtd/mtd.h>
+#include "vmufat.h"
+

Why do you have an mtd include?

Ah, a left over from when this was first written and tied to the hardware

...

Quoted 19 lines
Quoted 5 lines
fs/vmufat/inode.c new file +5 -0
+
+	bh->b_data[z + 0x10] = bcd_from_u8(century);
+	u8year = year + 80;
+	if (u8year > 99)
+		u8year = u8year - 100;

fs/vmufat/inode.c line 260

Quoted 10 lines
fs/vmufat/inode.c new file +10 -0
+
+	bh->b_data[z + 0x11] = bcd_from_u8(u8year);
+	bh->b_data[z + 0x12] = bcd_from_u8((__u8) month);
+	bh->b_data[z + 0x13] =
+	    bcd_from_u8((__u8) day - day_n[month - 1] + 1);
+	bh->b_data[z + 0x14] =
+	    bcd_from_u8((__u8) ((unix_date / 3600) % 24));
+	bh->b_data[z + 0x15] = bcd_from_u8((__u8) ((unix_date / 60) % 60));
+	bh->b_data[z + 0x16] = bcd_from_u8((__u8) (unix_date % 60));
+

This should be abstracted out in to a separate function, and you can get rid of most of this hand-rolled time mangling by using the rtc lib routines.

Quoted 3 lines

Additionally, all of the bcd conversion code is superfluous, since you can include linux/bcd.h and use bin2bcd and bcd2bin directly.

Another left over. In the 2.4 kernel there were several bespoke bcd conversion functions and I just wrote another one and didn't even think to check if a generic one had been added to the 2.6 kernel.

Quoted 3 lines

fs/vmufat/super.c line 481

Quoted 1 line
fs/vmufat/super.c new file +1 -0
+	sb->s_blocksize_bits = log_2;

ilog2()?

Again, from what I can see this wasn't available kernel wide back in the 2.4 days.

Quoted 8 lines

I've tried to skip over the bits already noted by Evgeniy, but you may have already fixed up some of the issues noted above anyways.

Also, in the next iteration of this patch, please do not break the Cc list and send multiple times to different lists, it makes it very difficult to follow what is going on, especially if the threads of conversation diverge.

Thanks. I have already fixed some of this, as you say. Will get on with tidying the rest of it.

#5 lore raw
In reply to Adrian McMenamin

Hi!

fs/vmufat/inode.c line 46

Quoted 3 lines
fs/vmufat/inode.c new file +3 -0
+	do {
+		/* Have we got a file? */
+		if (((__u8 *)bh->b_data)[(fno % 0x10) * 0x20] == 0) {

u8 should be ok, no need for underscores. Applies to whole file.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#6 lore raw

2009/2/27 Pavel Machek <[email protected]>:

Quoted 9 lines

Hi!

Quoted 3 lines

+     do {
+             /* Have we got a file? */
+             if (((__u8 *)bh->b_data)[(fno % 0x10) * 0x20] == 0) {

u8 should be ok, no need for underscores. Applies to whole file.

In retrospect although this v1 worked (after a fashion), it was pretty broken and so I am working on a fairly big rewrite. I was originally hoping for something that might be ready to go into 2.6.30 but I think that is gone now.
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html