Skip to contentSkip to conversation

Add support for VMUFAT filesystem

PATCH In review Adrian McMenamin 3 participants 10 messages Apr 11 – Apr 19

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

cover Add support for VMUFAT filesystem

SEGA Dreamcasts come with a "visual memory unit" (VMU) which contains a slab of flash memory to save games playable on the VMU or data from Dreamcast games.

The VMU uses a file allocation based filesystem - which we label VMUFAT - to organise the data. With a control block, a file allocation table and a directory.

All physical VMUs are of the same size (256 blocks of 512 octets) but the filesystem itself is extendable to other sizes and need not be tied to the VMU.

These patches implement VMUFAT as a Linux filesystem independent of hardware.

It seems obvious almost all users will be those working with Linux on Dreamcasts or Dreamcast emulators but there is no dependence on SH or on the MTD layer.

VMUFAT has been tested on a 32 bit Dreamcast and on x86_64 systems.

Tools to format a filesystem are available here:
https://github.com/mcmenaminadrian/mkfs.vmufat

Adrian McMenamin (5):
Add vmufat super.c
Add vmufat inode.c
Add vmufat header
Add vmufat documentation
miscellaneous vmufat changes and additions

 Documentation/filesystems/index.rst  |    1 +
 Documentation/filesystems/vmufat.rst |  127 ++++
 fs/Kconfig                           |    1 +
 fs/Makefile                          |    1 +
 fs/vmufat/Kconfig                    |   14 +
 fs/vmufat/Makefile                   |    7 +
 fs/vmufat/inode.c                    | 1017 ++++++++++++++++++++++++++
 fs/vmufat/super.c                    |  581 +++++++++++++++
 fs/vmufat/vmufat.h                   |  126 ++++
 include/uapi/linux/magic.h           |    1 +
 10 files changed, 1876 insertions(+)
 create mode 100644 Documentation/filesystems/vmufat.rst
 create mode 100644 fs/vmufat/Kconfig
 create mode 100644 fs/vmufat/Makefile
 create mode 100644 fs/vmufat/inode.c
 create mode 100644 fs/vmufat/super.c
 create mode 100644 fs/vmufat/vmufat.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 Add vmufat super.c

super.c provides superblock services for VMUFAT.

A VMUFAT filesystem includes a system block (at block 255 on a factory supplied VMU) which contains information about the volume size, the file allocation table and the directory. This code accesses this information.

Using this information super.c also handles basic creation and removal of files (represented as inodes, though there are no physical inodes on the underlying medium).

It also initialises (and destroys) caches of inode structures and block lists (each inode structure holds a linked list of the blocks used by a file).

The code supports, as default behaviour, the Dreamcast policy of placing executables in lower block numbers and data files in higher block numbers but it will not fail if this policy cannot be followed.

---
 fs/vmufat/super.c | 581 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 581 insertions(+)
 create mode 100644 fs/vmufat/super.c
fs/vmufat/super.c new file +581 -0
new file mode 100644
index 000000000000..69f4852f3d03
@@ -0,0 +1,581 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * VMUFAT file system
+ *
+ * Copyright (C) 2002-2012, 2025, 2026	Adrian McMenamin
+ * Copyright (C) 2002			Paul Mundt
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/bcd.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/magic.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/statfs.h>
+#include <linux/buffer_head.h>
+#include <linux/fs_context.h>
+#include "vmufat.h"
+
+static struct kmem_cache *vmufat_inode_cachep;
+struct kmem_cache *vmufat_blist_cachep;
+static const struct super_operations vmufat_super_operations;
+extern int *day_n;
+extern const struct inode_operations vmufat_inode_operations;
+extern const struct file_operations vmufat_file_operations;
+extern const struct address_space_operations vmufat_address_space_operations;
+extern const struct file_operations vmufat_file_dir_operations;
+
+static time64_t vmufat_get_date(struct buffer_head *bh, int offset)
+{
+	int century, year, month, day, hour, minute, second;
+	century = bcd2bin(bh->b_data[offset++]);
+	year = bcd2bin(bh->b_data[offset++]);
+	month = bcd2bin(bh->b_data[offset++]);
+	day = bcd2bin(bh->b_data[offset++]);
+	hour = bcd2bin(bh->b_data[offset++]);
+	minute = bcd2bin(bh->b_data[offset++]);
+	second = bcd2bin(bh->b_data[offset]);
+
+	return mktime64(century * 100 + year, month, day, hour, minute,
+		second);
+}
+
+static struct inode *vmufat_alloc_inode(struct super_block *sb)
+{
+	struct vmufat_inode *vi = kmem_cache_alloc(vmufat_inode_cachep,
+		GFP_KERNEL);
+	if (!vi)
+		return NULL;
+
+	INIT_LIST_HEAD(&vi->blocks);
+	return &vi->vfs_inode;
+}
+
+static void vmufat_destroy_inode(struct inode *in)
+{
+	struct vmufat_inode *vi;
+	struct vmufat_block *iter, *iter2;
+	vi = VMUFAT_I(in);
+	if (!vi)
+		return;
+
+	list_for_each_entry_safe(iter, iter2, &vi->blocks, b_list) {
+		list_del(&iter->b_list);
+		kmem_cache_free(vmufat_blist_cachep, iter);
+	}
+	kmem_cache_free(vmufat_inode_cachep, vi);
+}
+
+struct inode *vmufat_get_inode(struct super_block *sb, long ino)
+{
+	struct buffer_head *bh = NULL;
+	int error = 0, i, j;
+	int offsetindir;
+	struct inode *inode;
+	struct memcard *vmudetails;
+	long superblock_bno;
+	struct timespec64 current_time;
+
+	inode = iget_locked(sb, ino);
+	if (!inode) {
+		error = -ENOENT;
+		goto reterror;
+	}
+	vmudetails = sb->s_fs_info;
+	if (!vmudetails) {
+		error = -ENOMEM;
+		goto reterror;
+	}
+	superblock_bno = vmudetails->sb_bnum;
+
+	if (inode_state_read_once(inode) & I_NEW) {
+		inode->i_uid = current_fsuid();
+		inode->i_gid = current_fsgid();
+		inode->i_mode &= ~S_IFMT;
+		if (inode->i_ino == superblock_bno) {
+			bh = vmufat_sb_bread(sb, inode->i_ino);
+			if (!bh) {
+				error = -ENOENT;
+				goto failed;
+			}
+			inode->i_ctime_sec = inode->i_mtime_sec =
+				vmufat_get_date(bh, VMUFAT_SB_DATEOFFSET);
+
+			/* Mark as a directory */
+			inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
+			inode->i_op = &vmufat_inode_operations;
+			inode->i_fop = &vmufat_file_dir_operations;
+		} else {
+			/* Mark file as regular type */
+			inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
+
+			/* Scan through the directory to find matching file */
+			for (i = 0; i < vmudetails->dir_len; i++) {
+				brelse(bh);
+				bh = vmufat_sb_bread(sb,
+					vmudetails->dir_bnum - i);
+				if (!bh) {
+					error = -ENOMEM;
+					goto failed;
+				}
+				for (j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++)
+				{
+					u16 ino_read = le16_to_cpu(((u16 *)
+						bh->b_data)[j * 
+						VMU_DIR_RECORD_LEN16 + 
+						VMUFAT_FIRSTBLOCK_OFFSET16]);
+					if (ino_read == ino) {
+							goto found;
+					}
+				}
+			}
+			error = -ENOENT;
+			goto failed;
+found:
+			/* identified the correct directory entry */
+			offsetindir = j * VMU_DIR_RECORD_LEN;
+			inode->i_ctime_sec = inode->i_mtime_sec =
+				vmufat_get_date(bh,
+					offsetindir + VMUFAT_FILE_DATEOFFSET);
+			/* Execute if a game, write if not copy protected */
+			inode->i_mode &= ~(S_IWUGO | S_IXUGO);
+			inode->i_mode |= S_IRUGO;
+			/* Mode - is it write protected? */
+			if ((((u8 *) bh->b_data)[0x01 + offsetindir] == 0x00) &
+				~(sb->s_flags & SB_RDONLY)) {
+				inode->i_mode |= S_IWUSR;
+			}
+			/* Is file executible - ie a game */
+			if ((((u8 *) bh->b_data)[offsetindir] == 0xcc) &
+				~(sb->s_flags & SB_NOEXEC)) {
+				inode->i_mode |= S_IXUSR;
+			}
+			inode->i_blocks =
+				le16_to_cpu(((u16 *) bh->b_data)
+				[offsetindir / 2 + 0x0C]);
+			inode->i_size = inode->i_blocks * sb->s_blocksize;
+			inode->i_mapping->a_ops =
+				&vmufat_address_space_operations;
+			inode->i_op = &vmufat_inode_operations;
+			inode->i_fop = &vmufat_file_operations;
+			error = vmufat_list_blocks(inode);
+			if (error)
+				goto failed;
+		}
+		ktime_get_coarse_real_ts64(&current_time);
+		inode->i_atime_sec = current_time.tv_sec;
+		unlock_new_inode(inode);
+	}
+	brelse(bh);
+	return inode;
+
+failed:
+	iget_failed(inode);
+reterror:
+	brelse(bh);
+	return ERR_PTR(error);
+}
+
+static void vmufat_put_super(struct super_block *sb)
+{
+	sb->s_dev = 0;
+	kfree(sb->s_fs_info);
+}
+
+static void vmufat_count_freeblocks(struct super_block *sb,
+					struct kstatfs *kstatbuf)
+{
+	int i, free = 0;
+	struct memcard *vmudetails = sb->s_fs_info;
+
+	/* Look through the FAT */
+	for (i = 0; i < vmudetails->numblocks; i++) {
+		if (vmufat_get_fat(sb, i) == VMUFAT_UNALLOCATED) {
+			free++;
+		}
+	}
+	kstatbuf->f_bfree = free;
+	kstatbuf->f_bavail = free;
+	kstatbuf->f_blocks = vmudetails->sb_bnum + 1;
+}
+
+int vmufat_count_files(struct super_block *sb)
+{
+	int error = -1;
+	int files_found = 0;
+	struct buffer_head *bh = NULL;
+	struct memcard *vmudetails;
+	// get directory
+	vmudetails = sb->s_fs_info;
+	if (!vmudetails) {
+		return error;
+	}
+	for (int i = vmudetails->dir_bnum;
+		i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			error = -EIO;
+			return error;
+		}
+		for (int j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			int record_offset = j * VMU_DIR_RECORD_LEN;
+			if (bh->b_data[record_offset] != 0) {
+				files_found++;
+			}
+		}
+	}
+	return files_found;
+}
+
+static int vmufat_count_file_space(struct super_block *sb)
+{
+	int error = -1;
+	int files_available = 0;
+	struct buffer_head *bh = NULL;
+	struct memcard *vmudetails;
+	// get directory
+	if (!sb) {
+		return error;
+	}
+	vmudetails = sb->s_fs_info;
+	if (!vmudetails) {
+		return error;
+	}
+	for (int i = vmudetails->dir_bnum;
+		i > vmudetails->numblocks; i--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			error = -EIO;
+			return error;
+		}
+		for (int j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			int record_offset = j * VMU_DIR_RECORD_LEN;
+			if (bh->b_data[record_offset] == 0) {
+				files_available++;
+			}
+		}
+	}
+	return files_available;
+}
+
+static int vmufat_statfs(struct dentry *dentry, struct kstatfs *kstatbuf)
+{
+	struct super_block *sb;
+	sb = dentry->d_sb;
+	vmufat_count_freeblocks(sb, kstatbuf);
+	kstatbuf->f_type = VMUFAT_SUPER_MAGIC;
+	kstatbuf->f_bsize = sb->s_blocksize;
+	kstatbuf->f_namelen = VMUFAT_NAMELEN;
+	kstatbuf->f_files = vmufat_count_files(sb);
+	kstatbuf->f_ffree = vmufat_count_file_space(sb);
+	if (kstatbuf->f_ffree > kstatbuf->f_bfree) {
+		kstatbuf->f_ffree = kstatbuf->f_bfree;
+	}
+	return 0;
+}
+
+/* Remove inode from memory */
+static void vmufat_evict_inode(struct inode *in)
+{
+	truncate_inode_pages(&in->i_data, 0);
+	invalidate_inode_buffers(in);
+	in->i_size = 0;
+	clear_inode(in);
+}
+
+/*
+ * There are no inodes on the medium - vmufat_write_inode
+ * updates the directory entry
+ */
+static int vmufat_write_inode(struct inode *in, struct writeback_control *wbc)
+{
+	struct buffer_head *bh = NULL;
+	unsigned long inode_num;
+	int i, j; 
+	int found = 0;
+	int pos, pos16;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct timespec64 current_time;
+	sb = in->i_sb;
+	vmudetails = sb->s_fs_info;
+
+	/* As most real world devices are flash we
+	 * won't update the superblock every time */
+	if (in->i_ino == vmudetails->sb_bnum)
+		return 0;
+	if (in->i_ino == VMUFAT_ZEROBLOCK)
+		inode_num = 0;
+	else
+		inode_num = in->i_ino;
+
+	/* update the directory and inode details */
+	/* Now search for the directory entry */
+	mutex_lock(&vmudetails->mutex);
+	for (i = vmudetails->dir_bnum;
+		i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			mutex_unlock(&vmudetails->mutex);
+			return -ENXIO;
+		}
+		for (j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			pos = j * VMU_DIR_RECORD_LEN;
+			pos16 = j * VMU_DIR_RECORD_LEN16;
+			if (bh->b_data[pos] == 0) {
+				mutex_unlock(&vmudetails->mutex);
+				brelse(bh);
+				return -ENOENT;
+			}
+			if (le16_to_cpu(((u16 *)bh->b_data)
+				[pos16 + VMUFAT_FIRSTBLOCK_OFFSET16])
+				== inode_num) {
+				found = 1;
+				goto found;
+			}
+		}
+		brelse(bh);
+	}
+found:
+	if (found == 0) {
+		mutex_unlock(&vmudetails->mutex);
+		return -ENXIO;
+	}
+	/* BCD timestamp it */
+	ktime_get_coarse_real_ts64(&current_time);
+	in->i_mtime_sec = current_time.tv_sec;
+	vmufat_save_bcd(in, bh->b_data, pos);
+	mutex_unlock(&vmudetails->mutex);
+	mark_buffer_dirty(bh);
+	brelse(bh);
+	return 0;
+}
+
+static int check_sb_format(struct buffer_head *bh)
+{
+	return (((u32 *) bh->b_data)[0] == VMUFAT_SUPER_MAGIC &&
+		((u32 *) bh->b_data)[1] == VMUFAT_SUPER_MAGIC &&
+		((u32 *) bh->b_data)[2] == VMUFAT_SUPER_MAGIC &&
+		((u32 *) bh->b_data)[3] == VMUFAT_SUPER_MAGIC);
+}
+
+static void vmufat_populate_vmudata(struct memcard *vmudata,
+		struct buffer_head *bh, int test_sz)
+{
+	vmudata->sb_bnum = test_sz - 1;
+	vmudata->fat_bnum =
+		le16_to_cpu(((u16 *) bh->b_data)[VMU_LOCATION_FAT]);
+	vmudata->fat_len =
+		le16_to_cpu(((u16 *) bh->b_data)[VMU_LOCATION_FATLEN]);
+	vmudata->dir_bnum =
+		le16_to_cpu(((u16 *) bh->b_data)[VMU_LOCATION_DIR]);
+	vmudata->dir_len =
+		le16_to_cpu(((u16 *) bh->b_data)[VMU_LOCATION_DIRLEN]);
+	if (test_sz == VMU_PHYS_SZ) {
+		vmudata->numblocks =
+		le16_to_cpu(((u16 *) bh->b_data)[VMU_LOCATION_USRLEN]);
+		if (vmudata->numblocks == 0) {
+			vmudata->numblocks =
+				vmudata->dir_bnum;
+		}
+	} else {
+	/* return the true number of user available blocks - physical VMUs
+ 	 * return a neat 200 and ignore 40 blocks of usable space -
+ 	 * we get round that in a hardware neutral way */
+		vmudata->numblocks =
+			vmudata->dir_bnum;
+	}
+}
+
+static int vmufat_get_size(struct super_block *sb, struct buffer_head **bh)
+{
+	int i;
+	for (i = VMUFAT_MIN_BLK; i <= VMUFAT_MAX_BLK; i = i * 2) {
+		brelse(*bh);
+		*bh = vmufat_sb_bread(sb, i - 1);
+		if (*bh == NULL) {
+			return -EIO;
+		}
+		if (check_sb_format(*bh))
+			break;
+	}
+	if (i > VMUFAT_MAX_BLK) {
+		return -ENOENT;
+	}
+	return i;
+}
+
+static int vmufat_fill_super(struct super_block *sb, struct fs_context *fc)
+{
+	struct buffer_head *bh = NULL;
+	struct memcard *vmudata = NULL;
+	int test_sz;
+	struct inode *root_i;
+	int ret = 0;
+
+	vmudata = kzalloc(sizeof(struct memcard), GFP_KERNEL);
+	if (!vmudata) {
+		return -ENOMEM;
+	}
+	sb->s_fs_info = vmudata;
+	sb_set_blocksize(sb, VMU_BLK_SZ);
+	sb->s_blocksize_bits = ilog2(VMU_BLK_SZ);
+	sb->s_magic = VMUFAT_SUPER_MAGIC;
+	sb->s_op = &vmufat_super_operations;
+
+	/* 
+	 * Hardware VMUs are 256 blocks in size but
+	 * the specification allows for other sizes
+	 */
+	test_sz = vmufat_get_size(sb, &bh);
+	if (test_sz < VMUFAT_MIN_BLK) {
+		printk(KERN_ERR "VMUFAT: attempted to mount corrupted vmufat "
+			"or non-vmufat volume as vmufat.\n");
+		ret = test_sz;
+		goto freebh_out;
+	}
+
+	vmufat_populate_vmudata(vmudata, bh, test_sz);
+	mutex_init(&vmudata->mutex);
+
+	root_i = vmufat_get_inode(sb, vmudata->sb_bnum);
+	if (!root_i) {
+		printk(KERN_ERR "VMUFAT: get root inode failed\n");
+		ret = -ENOMEM;
+		goto freebh_out;
+	}
+	if (IS_ERR(root_i)) {
+		printk(KERN_ERR "VMUFAT: get root"
+			" inode failed - error 0x%lX\n",
+			PTR_ERR(root_i));
+		ret = PTR_ERR(root_i);
+		goto freebh_out;
+	}
+
+	sb->s_root = d_make_root(root_i);
+	if (!sb->s_root) {
+		ret = -EIO;
+		goto freebh_out;
+	}
+	else 
+		goto out;
+
+freebh_out:
+	brelse(bh);
+	kfree(vmudata);
+out:
+	return ret;
+}
+
+static void init_once(void *foo)
+{
+	struct vmufat_inode *vi = foo;
+	vi->nblcks = 0;
+	inode_init_once(&vi->vfs_inode);
+}
+
+static int init_inodecache(void)
+{
+	vmufat_blist_cachep = kmem_cache_create("vmufat_block_cache",
+		sizeof(struct vmufat_block), 0, SLAB_RECLAIM_ACCOUNT, 0);
+	if (!vmufat_blist_cachep) {
+		printk(KERN_CRIT
+			"VMUFAT: Could not create block list cache.\n");
+		return -ENOMEM;
+	}
+	vmufat_inode_cachep = kmem_cache_create("vmufat_inode_cache",
+		sizeof(struct vmufat_inode), 0, SLAB_RECLAIM_ACCOUNT,
+		init_once);
+	if (!vmufat_inode_cachep) {
+		printk(KERN_CRIT
+			"VMUFAT: Could not create inode cache.\n");
+		kmem_cache_destroy(vmufat_inode_cachep);
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+static void destroy_inodecache(void)
+{
+	// ensure the caches are up to date before we try to destroy them
+	rcu_barrier();
+	kmem_cache_destroy(vmufat_inode_cachep);
+	rcu_barrier();
+	kmem_cache_destroy(vmufat_blist_cachep);
+}
+
+static const struct super_operations vmufat_super_operations = {
+	.alloc_inode =		vmufat_alloc_inode,
+	.destroy_inode =	vmufat_destroy_inode,
+	.write_inode =		vmufat_write_inode,
+	.evict_inode =		vmufat_evict_inode,
+	.put_super =		vmufat_put_super,
+	.statfs =		vmufat_statfs,
+};
+
+static int vmufat_get_tree(struct fs_context *fc)
+{
+	return get_tree_bdev(fc, vmufat_fill_super);
+}
+
+static const struct fs_context_operations vmufat_fs_context_ops = {
+	.get_tree	= vmufat_get_tree,
+};
+
+static int vmufat_init_fs_context(struct fs_context *fc)
+{
+	fc->ops = &vmufat_fs_context_ops;
+	return 0;
+}
+
+static struct file_system_type vmufat_fs_type = {
+	.owner =			THIS_MODULE,
+	.name = 			"vmufat",
+	.kill_sb = 			kill_block_super,
+	.fs_flags =			FS_REQUIRES_DEV,
+	.init_fs_context =		vmufat_init_fs_context,
+};
+
+static int __init init_vmufat_fs(void)
+{
+	int err;
+	err = init_inodecache();
+	if (err)
+		return err;
+	else
+		return register_filesystem(&vmufat_fs_type);
+}
+
+static void __exit exit_vmufat_fs(void)
+{
+	destroy_inodecache();
+	// ensure all caches removed before we disappear
+	rcu_barrier();
+	unregister_filesystem(&vmufat_fs_type);
+}
+
+module_init(init_vmufat_fs);
+module_exit(exit_vmufat_fs);
+
+MODULE_DESCRIPTION("Filesystem used in Sega Dreamcast VMU");
+MODULE_AUTHOR("Adrian McMenamin <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/5 Add vmufat inode.c

In reply to Adrian McMenamin

inode.c supports inode operations on the VMUFAT filesystem.

There are no physical inodes on VMUFAT, as files are referenced via a file allocation table.

The file system is not particularly sophisticated and many operations are executed by generic functions.

This code does, though, oversee creation of new files on the volume, their removal and reading files off the volume.

Physical VMUs use flash and so, as a general rule, the filesystem code avoids excessive writes to the medium, though this may not be an issue for VMUFAT volumes on other media.

This code also handles timestamping of files, and uses code that will work on both 32- and 64-bit systems.

---
 fs/vmufat/inode.c | 1017 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1017 insertions(+)
 create mode 100644 fs/vmufat/inode.c
fs/vmufat/inode.c new file +1017 -0
new file mode 100644
index 000000000000..62f3e371393e
@@ -0,0 +1,1017 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * VMUFAT file system
+ *
+ * Copyright (C) 2002-2012, 2025, 2026	Adrian McMenamin
+ * Copyright (C) 2002			Paul Mundt
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/bcd.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/magic.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/statfs.h>
+#include <linux/buffer_head.h>
+#include <linux/mpage.h>
+#include "vmufat.h"
+
+const struct inode_operations vmufat_inode_operations;
+const struct file_operations vmufat_file_operations;
+const struct address_space_operations vmufat_address_space_operations;
+const struct file_operations vmufat_file_dir_operations;
+extern struct kmem_cache *vmufat_blist_cachep;
+/* Linear day numbers of the respective 1sts in non-leap years. */
+int day_n[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
+
+static struct dentry *vmufat_inode_lookup(struct inode *in, struct dentry *dent,
+	unsigned int ignored)
+{
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct buffer_head *bh = NULL;
+	struct inode *ino;
+	int i, j;
+	int error = 0;
+
+	if (dent->d_name.len > VMUFAT_NAMELEN) {
+		return ERR_PTR(-ENAMETOOLONG);
+	}
+	sb = in->i_sb;
+	vmudetails = sb->s_fs_info;
+
+	for (i = vmudetails->dir_bnum;
+		i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			return ERR_PTR(-EIO);
+		}
+		for (j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			int record_offset = j * VMU_DIR_RECORD_LEN;
+			if (bh->b_data[record_offset] == 0)
+				goto insert_negative;
+			if (memcmp(dent->d_name.name,
+				bh->b_data + record_offset +
+				VMUFAT_NAME_OFFSET, dent->d_name.len) == 0) {
+				ino = vmufat_get_inode(sb,
+					le16_to_cpu(((u16 *) bh->b_data)
+					[(record_offset / 2)
+					+ VMUFAT_FIRSTBLOCK_OFFSET16]));
+				if (IS_ERR(ino)) {
+					error = PTR_ERR(ino);
+					goto out;
+				} else if (!ino) {
+					error = -EACCES;
+					goto out;
+				}
+				d_add(dent, ino);
+				goto out;
+			}
+		}
+	}
+insert_negative:
+	d_add(dent, NULL); /* Did not find the file */
+out:
+	brelse(bh);
+	return ERR_PTR(error);
+}
+
+
+/* Search for free block in inclusive range */
+static int vmufat_get_freeblock(int start, int end, struct buffer_head *bh)
+{
+	int i, ret = -1;
+	__le16 fatdata;
+	if (start < end) {
+		for (i = start; i <= end; i++) {
+			fatdata = le16_to_cpu(((u16 *)bh->b_data)[i]);
+			if (fatdata == VMUFAT_UNALLOCATED) {
+				ret = i;
+				break;
+			}
+		}
+	} else {
+		for (i = start; i >= end; i--) {
+			fatdata = le16_to_cpu(((u16 *)bh->b_data)[i]);
+			if (fatdata == VMUFAT_UNALLOCATED) {
+				ret = i;
+				break;
+			}
+		}
+	}
+	return ret;
+}
+
+/*
+ * Find a block marked free in the FAT - for exec files
+ * Dreamcasts expect data and exec files to be stored differently
+ * - we attempt to replicate that with *enforcing* a policy
+ */
+static int vmufat_find_free_forward(struct super_block *sb)
+{
+	struct memcard *vmudetails;
+	int testblk, fatblk, i;
+	struct buffer_head *bh_fat;
+	vmudetails = sb->s_fs_info;
+	fatblk = vmudetails->fat_bnum;
+	for (i = 0; i < vmudetails->fat_len; i++) {
+		bh_fat = vmufat_sb_bread(sb, fatblk + i);
+		if (!bh_fat) {
+			return -EIO;
+		}
+		testblk = vmufat_get_freeblock(0, VMU_BLK_SZ16 - 1, bh_fat);
+		brelse(bh_fat);
+		if (testblk >= 0 && testblk + (i * VMU_BLK_SZ16) < 
+			vmudetails->numblocks) {
+			return testblk + (i * VMU_BLK_SZ16);
+		}
+	}
+	printk(KERN_INFO "VMUFAT: volume is full, cannot save game file\n");
+	return -ENOSPC;
+}
+
+/* And for data files*/
+static int vmufat_find_free_backward(struct super_block *sb)
+{
+	struct memcard *vmudetails;
+	int testblk;
+	int i, readblk;
+	struct buffer_head *bh_fat;
+
+	vmudetails = sb->s_fs_info;
+	readblk = (vmudetails->numblocks - 1) % VMU_BLK_SZ16;
+
+	for (i = vmudetails->fat_len - 1; i >= 0; i--) {
+		bh_fat = vmufat_sb_bread(sb, i + vmudetails->fat_bnum);
+		if (!bh_fat) {
+			return -EIO;
+		}
+		testblk = vmufat_get_freeblock(readblk, 0, bh_fat);
+		brelse(bh_fat);
+		if (testblk >= 0) {
+			return testblk + (i * VMU_BLK_SZ16);
+		}
+		readblk = VMU_BLK_SZ16 - 1;
+	}
+	printk(KERN_INFO "VMUFAT: volume is full, cannot save data file\n");
+	return -ENOSPC;
+}
+
+/* read the FAT for a given block */
+u16 vmufat_get_fat(struct super_block *sb, long block)
+{
+	struct buffer_head *bufhead;
+	int offset;
+	u16 block_content = VMUFAT_ERROR;
+	struct memcard *vmudetails = sb->s_fs_info;
+
+	/* which block in the FAT */
+	offset = block / VMU_BLK_SZ16;
+	if (offset >= vmudetails->fat_len)
+		goto out;
+	/* fat_bnum points to lowest block in FAT */
+	bufhead = vmufat_sb_bread(sb, offset + vmudetails->fat_bnum);
+	if (!bufhead)
+		goto out;
+	/* look inside the block */
+	block_content = le16_to_cpu(((u16 *)bufhead->b_data)
+		[block % VMU_BLK_SZ16]);
+	brelse(bufhead);
+out:
+	return block_content;
+}
+
+/* set the FAT for a given block */
+static int vmufat_set_fat(struct super_block *sb, long block, u16 data)
+{
+	struct buffer_head *bh;
+	int offset;
+	struct memcard *vmudetails = sb->s_fs_info;
+
+	offset = block / VMU_BLK_SZ16;
+	if (offset >= vmudetails->fat_len) {
+		return -EINVAL;
+	}
+	bh = vmufat_sb_bread(sb, offset + vmudetails->fat_bnum);
+	if (!bh) {
+		return -EIO;
+	}
+	((u16 *) bh->b_data)[block % VMU_BLK_SZ16] = cpu_to_le16(data);
+	mark_buffer_dirty(bh);
+	brelse(bh);
+	return 0;
+}
+
+/* No real time clock or real time clock read fails */
+static void vmufat_save_bcd_no_rtc(struct inode *in, char *bh, int index_to_dir)
+{
+	u32 years, days, seconds;
+	unsigned char bcd_century, nl_day, bcd_month;
+	unsigned char u8year;
+	time64_t unix_date, substitute_date;
+	unix_date = in->i_mtime_sec;
+	substitute_date = unix_date;
+	do_div(substitute_date, SECONDS_PER_DAY);
+	days = (u32)substitute_date; /* cast not an issue for next 5 million years+ */
+	years = days / DAYS_PER_YEAR;
+	/* 1 Jan gets 1 day later after every leap year */
+	if ((years + 3) / 4 + DAYS_PER_YEAR * years >= days)
+		years--;
+	/* rebase days to account for leap years */
+	days -= (years + 3) / 4 + DAYS_PER_YEAR * years;
+	// 2100 is not a leap year
+	if (years >= CENTURY22 + 1) {
+		days--;
+	}
+	/* 1 Jan is Day 1 */
+	days++;
+	if (days == (FEB28 + 1) && !(years % 4) && (years != CENTURY22)) {
+		nl_day = days;
+		bcd_month = 2;
+	} else {
+		nl_day = (years % 4) || days <= FEB28 ? days : days - 1;
+		for (bcd_month = 0; bcd_month < 12; bcd_month++) {
+			if (day_n[bcd_month] > nl_day)
+				break;
+		}
+	}
+
+	bcd_century = 19;
+	if (years > 29)
+		bcd_century += 1 + (years - CENTURY21)/100;
+
+	bh[index_to_dir + VMUFAT_DIR_CENT] = bin2bcd(bcd_century);
+	u8year = years + START_OF_EPOCH; /* account for epoch */
+	if (u8year > 99)
+		u8year = u8year - 100;
+
+	bh[index_to_dir + VMUFAT_DIR_YEAR] = bin2bcd(u8year);
+	bh[index_to_dir + VMUFAT_DIR_MONTH] = bin2bcd(bcd_month);
+	bh[index_to_dir + VMUFAT_DIR_DAY] =
+	    bin2bcd(days - day_n[bcd_month - 1]);
+	substitute_date = unix_date;
+	do_div(substitute_date, SECONDS_PER_HOUR);
+	bh[index_to_dir + VMUFAT_DIR_HOUR] =
+	    bin2bcd(do_div(substitute_date, HOURS_PER_DAY));
+	seconds = do_div(unix_date, SIXTY_MINS_OR_SECS);
+	bh[index_to_dir + VMUFAT_DIR_MIN] =
+		bin2bcd(unix_date);
+	bh[index_to_dir + VMUFAT_DIR_SEC] =
+		bin2bcd(seconds);
+}
+
+#ifdef CONFIG_RTC_CLASS
+static void vmufat_save_bcd_rtc(struct rtc_device *rtc, struct inode *in,
+	char *bh, int index_to_dir)
+{
+	struct rtc_time now;
+
+	if (rtc_read_time(rtc, &now))
+		vmufat_save_bcd_no_rtc(in, bh, index_to_dir);
+	else {
+		bh[index_to_dir + VMUFAT_DIR_CENT] = bin2bcd((char)((now.tm_year + 1900)/100));
+		bh[index_to_dir + VMUFAT_DIR_YEAR] =
+			bin2bcd((char)(now.tm_year % 100));
+		bh[index_to_dir + VMUFAT_DIR_MONTH] = bin2bcd((char)(now.tm_mon + 1));
+		bh[index_to_dir + VMUFAT_DIR_DAY] = bin2bcd((char)(now.tm_mday));
+		bh[index_to_dir + VMUFAT_DIR_HOUR] = bin2bcd((char)(now.tm_hour));
+		bh[index_to_dir + VMUFAT_DIR_MIN] = bin2bcd((char)(now.tm_min));
+		bh[index_to_dir + VMUFAT_DIR_SEC] = bin2bcd((char)(now.tm_sec));
+		bh[index_to_dir + VMUFAT_DIR_DOW] = bin2bcd((char)((now.tm_wday + 6))
+			% DAYS_PER_WEEK);
+	}
+}
+#endif
+
+/*
+ * write out the date in bcd format
+ * in the appropriate part of the
+ * directory entry
+ */
+void vmufat_save_bcd(struct inode *in, char *bh, int index_to_dir)
+{
+#ifdef CONFIG_RTC_CLASS
+	struct rtc_device *rtc;
+	rtc = rtc_class_open("rtc0");
+	if (!rtc)
+#endif
+		vmufat_save_bcd_no_rtc(in, bh, index_to_dir);
+#ifdef CONFIG_RTC_CLASS
+	else {
+		vmufat_save_bcd_rtc(rtc, in, bh, index_to_dir);
+		rtc_class_close(rtc);
+	}
+#endif
+}
+
+static int vmufat_allocate_inode(umode_t imode,
+		struct super_block *sb, struct inode *in)
+{
+	struct vmufat_inode *vin = VMUFAT_I(in);
+	int error = 0;
+	/* Executable files should be at the start of the volume if possible */
+	if (imode & EXEC) {
+		vin->ft = GAME;
+		if (vmufat_get_fat(sb, 0) != VMUFAT_UNALLOCATED) {
+			/* Warning for anyone concerned about playable games */
+			printk(KERN_WARNING 
+				"VMUFAT: Warning cannot write excutable "
+				"file to block 0: already allocated.\n");
+		}
+		else {
+			in->i_ino = VMUFAT_ZEROBLOCK;
+			return error;
+		}
+		error = vmufat_find_free_forward(sb);
+	} else {
+		vin->ft = DATA;
+		error = vmufat_find_free_backward(sb);
+	}
+	if (error == 0) {
+		in->i_ino = VMUFAT_ZEROBLOCK;
+	} else if (error > 0) {
+		in->i_ino = error;
+	}
+	return error;
+}
+
+static void vmufat_setup_inode(struct inode *in, struct super_block *sb)
+{
+	simple_inode_init_ts(in);
+	in->i_blocks = 1;
+	in->i_sb = sb;
+	in->i_op = &vmufat_inode_operations;
+	in->i_fop = &vmufat_file_operations;
+	in->i_mapping->a_ops = &vmufat_address_space_operations;
+	insert_inode_hash(in);
+}
+
+static void vmu_handle_zeroblock(int recno, struct buffer_head *bh, int ino)
+{
+	/* offset and header offset settings */
+	if (ino != VMUFAT_ZEROBLOCK) {
+		((u16 *) bh->b_data)[recno + VMUFAT_START_OFFSET16] =
+			cpu_to_le16(ino);
+		((u16 *) bh->b_data)[recno + VMUFAT_HEADER_OFFSET16] = 0;
+	} else {
+		((u16 *) bh->b_data)[recno + VMUFAT_START_OFFSET16] = 0;
+		((u16 *) bh->b_data)[recno + VMUFAT_HEADER_OFFSET16] =
+			cpu_to_le16(1);
+	}
+}
+
+static void vmu_write_name(int recno, struct buffer_head *bh, char *name,
+	int len)
+{
+	if ((bh->b_size - recno - VMUFAT_NAME_OFFSET) >= VMUFAT_NAMELEN &&
+		(bh->b_size - recno - VMUFAT_NAME_OFFSET) >= len) {
+		memset((char *)(bh->b_data + recno + VMUFAT_NAME_OFFSET),
+			'\0', VMUFAT_NAMELEN);
+		memcpy((char *)(bh->b_data + recno + VMUFAT_NAME_OFFSET),
+			name, len);
+	}
+}
+
+static int vmufat_inode_create(struct mnt_idmap *idmap, struct inode *dir,
+	struct dentry *de, umode_t imode, bool excl)
+{
+	int i, j, entry, error = 0, freeblock;
+	struct inode *inode;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct buffer_head *bh = NULL;
+
+	sb = dir->i_sb;
+	vmudetails = sb->s_fs_info;
+	inode = new_inode(sb);
+	if (!inode) {
+		error = -ENOSPC;
+		goto out;
+	}
+
+	mutex_lock(&vmudetails->mutex);
+	freeblock = vmufat_allocate_inode(imode, sb, inode);
+	if (freeblock < 0) {
+		mutex_unlock(&vmudetails->mutex);
+		error = freeblock;
+		goto clean_inode;
+	}
+	/* mark as single block file - may grow later */
+	error = vmufat_set_fat(sb, freeblock, VMUFAT_FILE_END);
+	mutex_unlock(&vmudetails->mutex);
+	if (error)
+		goto clean_inode;
+	inode_init_owner(&nop_mnt_idmap, inode, dir, imode);
+	vmufat_setup_inode(inode, sb);
+
+	/* Write to the directory
+	 * Now search for space for the directory entry */
+	mutex_lock(&vmudetails->mutex);
+	for (i = vmudetails->dir_bnum;
+		i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			mutex_unlock(&vmudetails->mutex);
+			error = -EIO;
+			goto clean_fat;
+		}
+		for (j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			entry = j * VMU_DIR_RECORD_LEN;
+			if (((bh->b_data)[entry]) == 0) {
+				goto dir_space_found;
+			}
+		}
+	}
+	goto clean_fat;
+dir_space_found:
+	/* Have the directory entry
+	 * so now update it */
+	if (imode & EXEC)
+		bh->b_data[entry] = VMU_GAME;
+	else
+		bh->b_data[entry] = VMU_DATA;
+
+	/* copy protection settings */
+	if (bh->b_data[entry + 1] != (char) NOCOPY)
+		bh->b_data[entry + 1] = (char) CANCOPY;
+
+	vmu_handle_zeroblock(entry / 2, bh, inode->i_ino);
+	vmu_write_name(entry, bh, (char *) de->d_name.name, de->d_name.len);
+
+	/* BCD timestamp it */
+	vmufat_save_bcd(inode, bh->b_data, entry);
+
+	((u16 *) bh->b_data)[entry / 2 + VMUFAT_SIZE_OFFSET16] =
+	    cpu_to_le16(inode->i_blocks);
+	mark_buffer_dirty(bh);
+	brelse(bh);
+
+	error = vmufat_list_blocks(inode);
+	if (error)
+		goto clean_fat;
+	mutex_unlock(&vmudetails->mutex);
+	mark_inode_dirty(inode);
+	d_instantiate(de, inode);
+	return error;
+
+clean_fat:
+	vmufat_set_fat(sb, freeblock, VMUFAT_UNALLOCATED);
+	mutex_unlock(&vmudetails->mutex);
+clean_inode:
+	iput(inode);
+out:
+	if (error < 0)
+		printk(KERN_ERR "VMUFAT: inode creation fails with error"
+			" %i\n", error);
+	return error;
+}
+
+static int vmufat_readdir(struct file *filp, struct dir_context *ctx)
+{
+	int filenamelen, index, j, k;
+	int error = 0;
+	struct vmufat_file_info *saved_file = NULL;
+	struct dentry *dentry;
+	struct inode *inode;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct buffer_head *bh = NULL;
+
+	inode = file_inode(filp);
+	dentry = filp->f_path.dentry;
+	sb = inode->i_sb;
+	vmudetails = sb->s_fs_info;
+	index = ctx->pos;
+	/* handle . for this directory and .. for parent */
+	switch ((unsigned int) ctx->pos) {
+	case 0:
+		error = ctx->actor(ctx, ".", 1, index++, inode->i_ino, DT_DIR);
+		ctx->pos++;
+		if (error < 0)
+			return error;
+		fallthrough;
+	case 1:
+		error = ctx->actor(ctx, "..", 2, index++,
+			    dentry->d_parent->d_inode->i_ino, DT_DIR);
+		ctx->pos++;
+		if (error < 0)
+			return error;
+	default:
+		break;
+	}
+
+	saved_file =
+	    kmalloc(sizeof(struct vmufat_file_info), GFP_KERNEL);
+	if (!saved_file) {
+		return -ENOMEM;
+	}
+
+	for (j = vmudetails->dir_bnum -
+		(index - 2) / VMU_DIR_ENTRIES_PER_BLOCK;
+		j > vmudetails->dir_bnum - vmudetails->dir_len; j--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, j);
+		if (!bh) {
+			return -EIO;
+		}
+		for (k = (index - 2) % VMU_DIR_ENTRIES_PER_BLOCK;
+			k < VMU_DIR_ENTRIES_PER_BLOCK; k++) {
+			int pos, pos16;
+			pos = k * VMU_DIR_RECORD_LEN;
+			pos16 = k * VMU_DIR_RECORD_LEN16;
+			saved_file->ftype = bh->b_data[pos];
+			if (saved_file->ftype == 0)
+				goto finish;
+			saved_file->fblk =
+				le16_to_cpu(((u16 *) bh->b_data)
+				[pos16 + VMUFAT_START_OFFSET16]);
+			if (saved_file->fblk == 0)
+				saved_file->fblk = VMUFAT_ZEROBLOCK;
+			if ((bh->b_size - pos - VMUFAT_NAME_OFFSET) >=
+				VMUFAT_NAMELEN) {
+				memcpy(saved_file->fname,
+				bh->b_data + pos + VMUFAT_NAME_OFFSET,
+				VMUFAT_NAMELEN);
+			}
+			filenamelen = strnlen(saved_file->fname,
+						VMUFAT_NAMELEN);
+			error = ctx->actor(ctx, saved_file->fname, filenamelen,
+				index++, saved_file->fblk, DT_REG);
+			ctx->pos++;
+			if (error < 0)
+				goto finish;
+		}
+	}
+finish:
+	ctx->pos = index;
+	kfree(saved_file);
+	brelse(bh);
+	return error;
+}
+
+
+int vmufat_list_blocks(struct inode *in)
+{
+	struct vmufat_inode *vi;
+	struct super_block *sb;
+	long nextblock;
+	long ino;
+	struct memcard *vmudetails;
+	int error = -EINVAL;
+	struct vmufat_block *iter, *iter2, *vbl;
+	u16 fatdata;
+
+	vi = VMUFAT_I(in);
+	if (!vi)
+		return error;
+	sb = in->i_sb;
+	ino = in->i_ino;
+	vmudetails = sb->s_fs_info;
+	error = 0;
+	nextblock = ino;
+	if (nextblock == VMUFAT_ZEROBLOCK)
+		nextblock = 0;
+
+	/* Delete any previous list of blocks */
+	list_for_each_entry_safe(iter, iter2, &vi->blocks, b_list) {
+		list_del(&iter->b_list);
+		kmem_cache_free(vmufat_blist_cachep, iter);
+	}
+	vi->nblcks = 0;
+	do {
+		vbl = kmem_cache_alloc(vmufat_blist_cachep,
+			GFP_KERNEL);
+		if (!vbl) {
+			error = -ENOMEM;
+			goto unwind_out;
+		}
+		INIT_LIST_HEAD(&vbl->b_list);
+		vbl->bno = nextblock;
+		list_add_tail(&vbl->b_list, &vi->blocks);
+		vi->nblcks++;
+
+		/* Find next block in the FAT - if there is one */
+		fatdata = vmufat_get_fat(sb, nextblock);
+		if (fatdata == VMUFAT_UNALLOCATED) {
+			printk(KERN_ERR "VMUFAT: FAT table appears to have"
+				" been corrupted.\n");
+			error = -EIO;
+			goto unwind_out;
+		}
+		if (fatdata == VMUFAT_FILE_END)
+			break;
+		nextblock = fatdata;
+	} while (1);
+	return error;
+
+unwind_out:
+	list_for_each_entry_safe(iter, iter2, &vi->blocks, b_list) {
+		list_del(&iter->b_list);
+		kmem_cache_free(vmufat_blist_cachep, iter);
+	}
+	return error;
+}
+
+static int vmufat_clean_fat(struct super_block *sb, int inum)
+{
+	int error = 0;
+	u16 fatword, nextword;
+
+	nextword = inum;
+	do {
+		fatword = vmufat_get_fat(sb, nextword);
+		if (fatword == VMUFAT_ERROR) {
+			error = -EIO;
+			printk(KERN_ERR
+				"VMUFAT: Failure while cleaning FAT.\n");
+			break;
+		}
+		error = vmufat_set_fat(sb, nextword, VMUFAT_UNALLOCATED);
+		if (error)
+			break;
+		if (fatword == VMUFAT_FILE_END)
+			break;
+		nextword = fatword;
+	} while (1);
+
+	return error;
+}
+
+/*
+ * Delete inode by marking space as free in FAT
+ * no need to waste time and effort by actually
+ * wiping underlying data on media
+ */
+static void vmufat_remove_inode(struct inode *in)
+{
+	struct buffer_head *bh = NULL, *bh_old = NULL;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	int i, j, k, l, x;
+	bool found, first = true;
+
+	if (in->i_ino == VMUFAT_ZEROBLOCK)
+		in->i_ino = 0;
+	sb = in->i_sb;
+	vmudetails = sb->s_fs_info;
+	if (in->i_ino > vmudetails->fat_len * sb->s_blocksize / 2) {
+		printk(KERN_ERR "VMUFAT: attempting to delete"
+			"inode beyond device size");
+		return;
+	}
+
+	mutex_lock(&vmudetails->mutex);
+	if (vmufat_clean_fat(sb, in->i_ino)) {
+		mutex_unlock(&vmudetails->mutex);
+		goto failure;
+	}
+
+	/* Now clean the directory entry
+	 * Have to walk through entries
+	 * to find the appropriate entry */
+	for (x = vmudetails->dir_bnum;
+		x > vmudetails->dir_bnum - vmudetails->dir_len; x--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, x);
+		if (!bh) {
+			mutex_unlock(&vmudetails->mutex);
+			goto failure;
+		}
+		for (j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			if (bh->b_data[j * VMU_DIR_RECORD_LEN] == 0) {
+				mutex_unlock(&vmudetails->mutex);
+				brelse(bh);
+				goto failure;
+			}
+			if (le16_to_cpu(((u16 *) bh->b_data)
+				[j * VMU_DIR_RECORD_LEN16 +
+				VMUFAT_FIRSTBLOCK_OFFSET16]) == in->i_ino) {
+				found = true;
+				goto found_bk;
+			}
+		}
+	}
+found_bk:
+	// actually found nothing - an error or failure of some sort
+	if (!found) {
+		brelse(bh);
+		goto failure;
+	}
+	// have found the directory entry, so first we clean it out
+	for (i = 0; i < VMU_DIR_RECORD_LEN; i++) {
+		bh->b_data[i + (j * VMU_DIR_RECORD_LEN)] = 0;
+	}
+	mark_buffer_dirty(bh);
+	// now test if there are further records
+	found = false;
+	for (i = x; i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		brelse(bh_old);
+		bh_old = vmufat_sb_bread(sb, i);
+		if (first) {
+			for (l = j; l < VMU_DIR_ENTRIES_PER_BLOCK; l++) {
+				if (first) {
+					first = false;
+					continue;
+				}
+				if (bh_old->b_data[l * VMU_DIR_RECORD_LEN]
+					== 0) {
+					found = true;
+					goto found_final;
+				}
+			}
+		} else {
+			for (l = 0; l < VMU_DIR_ENTRIES_PER_BLOCK; l++) {
+				if (bh_old->b_data[l * VMU_DIR_RECORD_LEN]
+					== 0) {
+					found = true;
+					goto found_final;
+				}
+			}
+		}
+	}
+found_final:
+	if (!found) {
+		// cleaned entry was final entry
+		mutex_unlock(&vmudetails->mutex);
+		brelse(bh_old);
+		brelse(bh);
+		return;
+	}
+	// copy the final entry into the empty slot and zero the final entry
+	if (l == VMU_DIR_ENTRIES_PER_BLOCK) {
+		l = 0;
+		brelse(bh_old);
+		bh_old = vmufat_sb_bread(sb, i - 1);
+	} else {
+		l--;
+	}
+	for (k = 0; k < VMU_DIR_RECORD_LEN; k++) {
+		bh->b_data[k + (j * VMU_DIR_RECORD_LEN)] =
+			bh_old->b_data[k + (l * VMU_DIR_RECORD_LEN)];
+		bh_old->b_data[k + (l * VMU_DIR_RECORD_LEN)] = 0;
+	}
+	mark_buffer_dirty(bh);
+	mark_buffer_dirty(bh_old);
+	mutex_unlock(&vmudetails->mutex);
+	brelse(bh);
+	brelse(bh_old);
+	return;
+
+failure:
+	printk(KERN_WARNING "VMUFAT: Failure to read volume,"
+		" could not delete inode - filesystem may be damaged\n");
+	return;
+}
+
+/*
+ * vmufat_unlink - delete a file pointed to
+ * by the dentry (only one directory in a
+ * vmufat fs so safe to ignore the inode
+ * supplied here
+ */
+static int vmufat_unlink(struct inode *dir, struct dentry *dentry)
+{
+	struct inode *in;
+	in = dentry->d_inode;
+	vmufat_remove_inode(in);
+	return 0;
+}
+
+/* Update the directory record */
+static int vmufat_increment_filesize(struct inode *inode)
+{
+	struct super_block *sb = inode->i_sb;
+	struct buffer_head *bh = NULL;
+	struct memcard *vmudetails = sb->s_fs_info;
+	unsigned long ino_num = inode->i_ino;
+	int error = 0;
+
+	if (ino_num == VMUFAT_ZEROBLOCK) {
+		ino_num = 0;
+	}
+
+	for (int i = vmudetails->dir_bnum;
+		i > vmudetails->dir_bnum - vmudetails->dir_len; i--) {
+		brelse(bh);
+		bh = vmufat_sb_bread(sb, i);
+		if (!bh) {
+			error = -EIO;
+			return error;
+		}
+		for (int j = 0; j < VMU_DIR_ENTRIES_PER_BLOCK; j++) {
+			int record_offset = j * VMU_DIR_RECORD_LEN;
+			if (bh->b_data[record_offset] == 0) {
+				continue;
+			}
+			if (le16_to_cpu(((u16 *) bh->b_data)
+				[(j * VMU_DIR_RECORD_LEN16) +
+				VMUFAT_FIRSTBLOCK_OFFSET16]) != ino_num) {
+					continue;
+			}
+			int current_count = le16_to_cpu(((u16 *) bh->b_data)
+				[(j * VMU_DIR_RECORD_LEN16) + VMUFAT_SIZE_OFFSET16]);
+			current_count++;
+			((u16 *) bh->b_data)
+				[(j * VMU_DIR_RECORD_LEN16) + VMUFAT_SIZE_OFFSET16] =
+				cpu_to_le16(current_count);
+			mark_buffer_dirty(bh);
+			goto done;
+		}
+	}
+done:
+	brelse(bh);
+	return error;
+}
+
+static int vmufat_get_block(struct inode *inode, sector_t iblock,
+	struct buffer_head *bh_result, int create)
+{
+	struct vmufat_inode *vin;
+	struct list_head *vlist;
+	struct vmufat_block *vblk;
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	int cural;
+	int finblk, nxtblk, exeblk;
+	struct list_head *iter;
+	sector_t cntdwn = iblock;
+	sector_t phys;
+	int error = -EINVAL;
+
+	vin = VMUFAT_I(inode);
+	if (!vin || vin->nblcks <= 0)
+		return -EINVAL;
+	vlist = &vin->blocks;
+	sb = inode->i_sb;
+	vmudetails = sb->s_fs_info;
+
+	if (iblock < vin->nblcks) {
+		/* block is already here so read it into the buffer head */
+		list_for_each(iter, vlist) {
+			if (cntdwn-- == 0)
+				break;
+		}
+		vblk = list_entry(iter, struct vmufat_block, b_list);
+		clear_buffer_new(bh_result);
+		error = 0;
+		phys = vblk->bno;
+		goto got_it;
+	}
+	if (!create)
+		goto out;
+	/*
+	 * check not looking for a block too far
+	 * beyond the end of the existing file
+	 */
+	if (iblock > vin->nblcks)
+		goto out;
+
+	/* if looking for a block that is not current - allocate it */
+	cural = vin->nblcks;
+	list_for_each(iter, vlist) {
+		if (cural-- == 1)
+			break;
+	}
+	vblk = list_entry(iter, struct vmufat_block, b_list);
+	finblk = vblk->bno;
+
+	mutex_lock(&vmudetails->mutex);
+	/* Exec files have to be linear on real VMUs */
+	/* But this is policy so we warn but don't fail */
+	if (inode->i_ino == 0) {
+		exeblk = vmufat_get_fat(sb, finblk + 1);
+		if (exeblk != VMUFAT_UNALLOCATED) {
+			mutex_unlock(&vmudetails->mutex);
+			printk(KERN_WARNING "VMUFAT: Cannot allocate linear "
+				"space needed for executible\n");
+			mutex_lock(&vmudetails->mutex);
+			nxtblk = vmufat_find_free_forward(sb);
+			if (nxtblk < 0) {
+				mutex_unlock(&vmudetails->mutex);
+				return nxtblk;
+			}
+		}
+		else {
+			nxtblk = finblk + 1;
+		}
+	} else {
+		if (vin->ft == GAME) {
+			nxtblk = vmufat_find_free_forward(sb);
+		} else {
+			nxtblk = vmufat_find_free_backward(sb);
+		}
+		if (nxtblk < 0) {
+			mutex_unlock(&vmudetails->mutex);
+			return nxtblk;
+		}
+	}
+	error = vmufat_set_fat(sb, finblk, nxtblk);
+	if (!error) {
+		error = vmufat_increment_filesize(inode);
+	}
+	if (error) {
+		mutex_unlock(&vmudetails->mutex);
+		return error;
+	}
+	error = vmufat_set_fat(sb, nxtblk, VMUFAT_FILE_END);
+	mutex_unlock(&vmudetails->mutex);
+	if (error)
+		return error;
+	error = vmufat_list_blocks(inode);
+	mark_inode_dirty(inode);
+	if (error)
+		return error;
+	set_buffer_new(bh_result);
+	phys = nxtblk;
+	error = 0;
+got_it:
+	map_bh(bh_result, sb, phys);
+out:
+	return error;
+}
+
+static int vmufat_writepages(struct address_space *mapping, struct writeback_control *wbc)
+{
+	return mpage_writepages(mapping, wbc, vmufat_get_block);
+}
+
+static int vmufat_write_begin(const struct kiocb *iocb, struct address_space *mapping,
+		loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
+{
+	*foliop = NULL;
+	return block_write_begin(mapping, pos, len, foliop, vmufat_get_block);
+}
+
+static int vmufat_read_folio(struct file *file, struct folio *folio)
+{
+	return block_read_full_folio(folio, vmufat_get_block);
+}
+
+static int vmufat_getattr(struct mnt_idmap *idmap, const struct path *path,
+	struct kstat *stat, u32 request_mask, unsigned int query_flags)
+{
+	struct super_block *sb;
+	struct memcard *vmudetails;
+	struct inode *inode = d_inode(path->dentry);
+	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
+	/* correct for superblock to give directory size */
+	/* as all metadata combined */
+	sb = inode->i_sb;
+	if (sb) {
+		vmudetails = sb->s_fs_info;
+		if (vmudetails && vmudetails->sb_bnum == inode->i_ino) {
+			stat->size = (vmudetails->fat_len + vmudetails->dir_len + 1) * 
+				VMU_BLK_SZ;
+			stat->nlink = vmufat_count_files(sb) + 2;
+		}
+	}
+	return 0;
+}
+
+const struct address_space_operations
+		vmufat_address_space_operations = {
+	.read_folio =	vmufat_read_folio,
+	.writepages =	vmufat_writepages,
+	.write_begin =	vmufat_write_begin,
+	.write_end =	generic_write_end,
+};
+
+const struct inode_operations vmufat_inode_operations = {
+	.lookup =	vmufat_inode_lookup,
+	.create =	vmufat_inode_create,
+	.unlink =	vmufat_unlink,
+	.getattr =	vmufat_getattr,
+};
+
+const struct file_operations vmufat_file_dir_operations = {
+	.read =			generic_read_dir,
+	.iterate_shared =	vmufat_readdir,
+	.fsync =		generic_file_fsync,
+	.llseek =		generic_file_llseek,
+};
+
+const struct file_operations vmufat_file_operations = {
+	.llseek =	generic_file_llseek,
+	.read_iter =	generic_file_read_iter,
+	.write_iter =	generic_file_write_iter,
+	.fsync =	generic_file_fsync,
+};
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/5 Add vmufat header

In reply to Adrian McMenamin

vmufat.h provides constants and common data structures to inode.c and super.c as well as a wrapper around calls to sb_read.

---
 fs/vmufat/vmufat.h | 126 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 126 insertions(+)
 create mode 100644 fs/vmufat/vmufat.h
fs/vmufat/vmufat.h new file +126 -0
new file mode 100644
index 000000000000..369ee59738ee
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Header file for VMUFAT filesystem */
+#ifndef _VMUFAT_H_
+#define _VMUFAT_H_
+
+/* maximum length of file name */
+#define VMUFAT_NAMELEN			12
+
+/* Some GNU utils might not list files with inode num 0 */
+#define VMUFAT_ZEROBLOCK		0x10000
+#define VMU_BLK_SZ			0x200
+#define	VMU_BLK_SZ16			0x100
+#define VMU_PHYS_SZ			0x100
+
+/* file allocation table markers */
+#define VMUFAT_FILE_END			0xFFFA
+#define VMUFAT_UNALLOCATED		0xFFFC
+#define VMUFAT_ERROR			0xFFFF
+
+/* parameters for possible VMU volume sizes */
+#define VMUFAT_MIN_BLK			0x04
+#define VMUFAT_MAX_BLK			0x10000
+
+/* specifcations for directory entries */
+#define VMU_DIR_RECORD_LEN		0x20
+#define VMU_DIR_RECORD_LEN16		0x10
+#define VMU_DIR_ENTRIES_PER_BLOCK	0x10
+#define VMUFAT_NAME_OFFSET		0x04
+#define VMUFAT_FIRSTBLOCK_OFFSET16	0x01
+#define VMUFAT_START_OFFSET16		0x01
+#define VMUFAT_SIZE_OFFSET16		0x0C
+#define VMUFAT_HEADER_OFFSET16		0x0D
+
+/* File types used in directory */
+#define VMU_GAME			0xCC
+#define VMU_DATA			0x33
+
+/* filesystem locations marked in the root block */
+#define VMU_LOCATION_FAT		0x23
+#define VMU_LOCATION_FATLEN		0x24
+#define VMU_LOCATION_DIR		0x25
+#define VMU_LOCATION_DIRLEN		0x26
+#define VMU_LOCATION_USRLEN		0x28 /* set to 200 by real hardware */
+
+/* date offsets */
+#define VMUFAT_SB_DATEOFFSET	0x30
+#define VMUFAT_FILE_DATEOFFSET	0x10
+
+#define EXEC				0111
+#define NOCOPY				0xFF
+#define CANCOPY				0x00
+
+u16 vmufat_get_fat(struct super_block *sb, long block);
+void vmufat_save_bcd(struct inode *in, char *bh, int index_to_dir);
+struct inode *vmufat_get_inode(struct super_block *sb, long ino);
+int vmufat_list_blocks(struct inode *in);
+
+enum vmufat_date {
+	VMUFAT_DIR_CENT	=		0x10,
+	VMUFAT_DIR_YEAR,
+	VMUFAT_DIR_MONTH,
+	VMUFAT_DIR_DAY,
+	VMUFAT_DIR_HOUR,
+	VMUFAT_DIR_MIN,
+	VMUFAT_DIR_SEC,
+	VMUFAT_DIR_DOW
+};
+
+/* constants for BCD conversion */
+#define SECONDS_PER_DAY			86400
+#define DAYS_PER_YEAR			365
+#define SECONDS_PER_HOUR		3600
+#define HOURS_PER_DAY			24
+#define SIXTY_MINS_OR_SECS		60
+#define FEB28				59
+#define DAYS_PER_WEEK			7
+#define START_OF_EPOCH			70
+#define CENTURY21			30
+#define CENTURY22			130
+
+struct memcard {
+	unsigned int sb_bnum;
+	unsigned int fat_bnum;
+	unsigned int fat_len;
+	unsigned int dir_bnum;
+	unsigned int dir_len;
+	unsigned int numblocks;
+	struct mutex mutex;
+};
+
+struct vmufat_block {
+	struct list_head b_list;
+	int bno;
+};
+
+struct vmufat_inode {
+	struct list_head blocks;
+	unsigned int nblcks;
+	enum vmufat_file_type {
+		GAME = 1,
+		DATA = 2
+	} ft;
+	struct inode vfs_inode;
+};
+
+static inline struct vmufat_inode *VMUFAT_I(struct inode *in)
+{
+	return container_of(in, struct vmufat_inode, vfs_inode);
+}
+
+struct vmufat_file_info {
+	u8 ftype;
+	int fblk;
+	char fname[VMUFAT_NAMELEN];
+};
+
+static struct buffer_head *vmufat_sb_bread(struct super_block *sb,
+	sector_t block)
+{
+	if (!sb)
+		return NULL;
+	return sb_bread(sb, block);
+}
+
+int vmufat_count_files(struct super_block *sb);
+#endif
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/5 Add vmufat documentation

In reply to Adrian McMenamin

vmufat.rst documents the VMUFAT filesystem

---
 Documentation/filesystems/vmufat.rst | 127 +++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100644 Documentation/filesystems/vmufat.rst
Documentation/filesystems/vmufat.rst new file +127 -0
new file mode 100644
index 000000000000..f2dfb15c0cfb
@@ -0,0 +1,127 @@
+==================
+VMUFAT FILESYSTEM
+==================
+
+VMUFAT is the simple file allocation table (FAT) based filesystem used in Sega
+Dreamcast Visual Memory Units (VMUs) and various Dreamcast emulators and
+Android apps etc.
+
+It is not recommended for general use, but does not require a Dreamcast.
+
+All the physical VMU devices that were made were of the same size - 256 blocks
+of 512 octets (8 bit bytes) - 128KB in total. But the specification supports a 
+wider range of sizes and the filesystem in the Linux kernel is capable of
+handling volumes of a size between 256 blocks and 65536 blocks.
+
+The standard 256 block VMU is described below:
+
++----------+---------------------------------------------+
+| BLOCK NO |   CONTENT                                   |
++==========+=============================================+
+| 0-199    |   Space used by Dreamcast to save files     |
++----------+---------------------------------------------+
+| 200-240  |   Space used by the Dreamcast system        |
++----------+---------------------------------------------+
+| 241-253  |   Directory (can hold 208 file records)     |
++----------+---------------------------------------------+
+| 254      |   File Allocation Table (FAT)               |
++----------+---------------------------------------------+
+| 255      |   Root Block                                |
++----------+---------------------------------------------+
+
+The standard VMU filesystem has 241 blocks which can be used for file storage
+but a Dreamcast will only use 200 of these. The other 41 are for Dreamcast system
+use, though the filesystem can access them if set up as a general file store.
+If a device is formatted as strictly compatible with the Dreamcast those blocks
+will not be accessible. Most use cases will want that strict compatibility.
+
+An executable file (generally a game written in the native machine code of
+the VMU's microcontroller) must begin at block 0 and be stored linearly in
+the volume. The filesystem driver seeks to match that policy but it is not
+absolutely constrained by it.
+
+=========
+Directory
+=========
+
+The directory contains records 32 octets long (format detail below from Marcus
+Comstedt's website - http://mc.pp.se/dc/vms/flashmem.html)
+
+0x00      : file type (0x00 = no file, 0x33 = data, 0xcc = game)
+
+0x01      : copy protect (0x00 = copy ok, 0xff = copy protected)
+
+0x02-0x03 : 16 bits (little endian) : location of first block
+
+0x04-0x0f : ASCII string : filename (12 characters)
+
+0x10-0x17 : Binary Coded Decimal timestamp: file creation time
+
+0x18-0x19 : 16 bits (little endian) : file size (in blocks)
+
+0x1a-0x1b : 16 bits (little endian) : offset of header (in blocks)
+
+
+Header positioning is a matter for executable files written in native
+code for a physical VMU (an 8 bit Sanyo microcontroller).
+
+BCD dates are encoded as follows:
+
+Century prefix (eg., 20)
+
+Year (eg., 12)
+
+Month (eg., 02)
+
+Day in month (eg., 29)
+
+Hour (eg., 20)
+
+Minute (eg., 49)
+
+Second (eg., 22)
+
+Day of week (Monday is 00, Sunday is 06)
+
+=====================
+File allocation table
+=====================
+
+Every block in the volume is mapped to the FAT eg., block 0 of the VMU maps to
+the first 16 bits of the FAT and so on. Blocks marked 0xFFFC are empty, blocks
+allocated to a file are either numbered with with next block or, if the final
+block are marked 0xFFFA.
+
+==========
+Root block
+==========
+
+The first 16 octets are marked as 0x55 - we use this to provide the *magic
+number* for this filesystem. The octets at 0x10 - 0x14 are used to determine
+the colour the representation of the VMU is displayed in by a Dreamcast and our
+filesystem does not touch these.
+
+Octets 0x30 - 0x37 contain the BCD timestamp of the VMU.
+
+Octets 0x46 - 0x47 contain the location (little endian format) of the FAT
+
+Octets 0x48 - 0x49 contain the size (little endian) of the FAT
+
+Octets 0x4A - 0x4B contain the location (little endian) of the Directory
+
+Octets 0x4C - 0x4D contain the size (little endian) of the Directory
+
+Octets 0x4E - 0x4F is Dreamcast specific (icon shape)
+
+Octets 0x50 - 0x51 contain the number (little endian) of user blocks - NB this
+is marked as 200 in a physical VMU. A filesystem formatted for strict
+compatibility will respect this boundary, one formatted as a general
+file store will not and will instead seek to use all writeable blocks.
+
+====================
+Formatting tool
+====================
+
+A formatting tool is available at https://github.com/mcmenaminadrian/mkfs.vmufat
+
+A physical, factory-set, VMU is unlikely to require reformatting, of course.
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/5 miscellaneous vmufat changes and additions

In reply to Adrian McMenamin

Documentation/filesystems/index.rst
Update the documentation index file to link to the VMUFAT documentation..

fs/Kconfig
Update the fs Kconfig to make the VMUFAT option available.

fs/Makefile
Update the fs Makefile to allow VMUFAT to be built.

fs/vmufat/Kconfig
Advise kernel builders on whether or not to build VMUFAT code.

fs/vmufat/Makefile
Add a makefile to build the VMUFAT code as a built-in or a module.

include/uapi/linux/magic.h
0x55555555 is used mark the VMUFAT superblock, add it as an indentifying magic number.

---
 Documentation/filesystems/index.rst |  1 +
 fs/Kconfig                          |  1 +
 fs/Makefile                         |  1 +
 fs/vmufat/Kconfig                   | 14 ++++++++++++++
 fs/vmufat/Makefile                  |  7 +++++++
 include/uapi/linux/magic.h          |  1 +
 6 files changed, 25 insertions(+)
 create mode 100644 fs/vmufat/Kconfig
 create mode 100644 fs/vmufat/Makefile
Documentation/filesystems/index.rst +1 -0
index f4873197587d..893b585d17a9 100644
@@ -120,5 +120,6 @@ Documentation for filesystem implementations.
    udf
    virtiofs
    vfat
+   vmufat
    xfs/index
    zonefs
fs/Kconfig +1 -0
index 0bfdaecaa877..89a37c9c58a0 100644
@@ -332,6 +332,7 @@ source "fs/pstore/Kconfig"
 source "fs/ufs/Kconfig"
 source "fs/erofs/Kconfig"
 source "fs/vboxsf/Kconfig"
+source "fs/vmufat/Kconfig"
 
 endif # MISC_FILESYSTEMS
 
fs/Makefile +1 -0
index cf4a745e9679..3cdc3d7b804c 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
 obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
 obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
 obj-$(CONFIG_RESCTRL_FS)	+= resctrl/
+obj-$(CONFIG_VMUFAT_FS)		+= vmufat/
fs/vmufat/Kconfig new file +14 -0
new file mode 100644
index 000000000000..92553a246911
@@ -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 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 000000000000..da423b0c7eec
@@ -0,0 +1,7 @@
+#
+# Makefile for VMUFAT filesystem
+#
+
+obj-$(CONFIG_VMUFAT_FS) += vmufat.o
+
+vmufat-y := inode.o super.o
include/uapi/linux/magic.h +1 -0
index 4f2da935a76c..7a3ab866ef48 100644
@@ -54,6 +54,7 @@
 #define QNX4_SUPER_MAGIC	0x002f		/* qnx4 fs detection */
 #define QNX6_SUPER_MAGIC	0x68191122	/* qnx6 fs detection */
 #define AFS_FS_MAGIC		0x6B414653
+#define VMUFAT_SUPER_MAGIC	0x55555555  /* opening bytes of root */
 
 
 #define REISERFS_SUPER_MAGIC	0x52654973	/* used by gcc */
Signature · 1 line
2.43.0
#6 lore raw
In reply to Adrian McMenamin
Quoted 10 lines · Adrian McMenamin

SEGA Dreamcasts come with a "visual memory unit" (VMU) which contains a slab of flash memory to save games playable on the VMU or data from Dreamcast games.

The VMU uses a file allocation based filesystem - which we label VMUFAT - to organise the data. With a control block, a file allocation table and a directory.

All physical VMUs are of the same size (256 blocks of 512 octets) but the filesystem itself is extendable to other sizes and need not be tied to the VMU.

These patches implement VMUFAT as a Linux filesystem independent of hardware.

Isn't this filesystem a great target for FUSE? You definitely don't need super high performance here.

Also, you're missing the fs maintainers. Adding them.

Quoted 40 lines

It seems obvious almost all users will be those working with Linux on Dreamcasts or Dreamcast emulators but there is no dependence on SH or on the MTD layer.

VMUFAT has been tested on a 32 bit Dreamcast and on x86_64 systems.

Tools to format a filesystem are available here:
https://github.com/mcmenaminadrian/mkfs.vmufat

Adrian McMenamin (5):
Add vmufat super.c
Add vmufat inode.c
Add vmufat header
Add vmufat documentation
miscellaneous vmufat changes and additions

 Documentation/filesystems/index.rst  |    1 +
 Documentation/filesystems/vmufat.rst |  127 ++++
 fs/Kconfig                           |    1 +
 fs/Makefile                          |    1 +
 fs/vmufat/Kconfig                    |   14 +
 fs/vmufat/Makefile                   |    7 +
 fs/vmufat/inode.c                    | 1017 ++++++++++++++++++++++++++
 fs/vmufat/super.c                    |  581 +++++++++++++++
 fs/vmufat/vmufat.h                   |  126 ++++
 include/uapi/linux/magic.h           |    1 +
 10 files changed, 1876 insertions(+)
 create mode 100644 Documentation/filesystems/vmufat.rst
 create mode 100644 fs/vmufat/Kconfig
 create mode 100644 fs/vmufat/Makefile
 create mode 100644 fs/vmufat/inode.c
 create mode 100644 fs/vmufat/super.c
 create mode 100644 fs/vmufat/vmufat.h
Signature · 1 line
2.43.0
Signature · 1 line
Pedro
#7 lore raw
Quoted 5 lines · Pedro Falcato
Quoted 3 lines · Adrian McMenamin

SEGA Dreamcasts come with a "visual memory unit" (VMU) which contains a slab of flash memory to save games playable on the VMU or data from Dreamcast games.

...

Quoted 8 lines
Quoted 2 lines

These patches implement VMUFAT as a Linux filesystem independent of hardware.

Isn't this filesystem a great target for FUSE? You definitely don't need super high performance here.

Also, you're missing the fs maintainers. Adding them.

Apologies for the long delay in replying - I was hoping that maybe someone else would have a view rather than my rather inevitable "can this go in mainline, please?".

I'm not saying you've done this, but in some online fora, this patch has been discussed solely as a Dreamcast add-on - when the key point is it is hardware independent. Dreamcast users who really want to can already get what's on their VMU via the VMU mtd layer code and then, again if they really wanted to, manipulate that at the byte level. Having a file system is about people on other platforms being able to access and manipulate VMU contents (easily) too.

It's not a complex piece of code so the maintenance requirement is low and if it were to break in future it wouldn't drag anything else down, and nor is it an impediment to general kernel development (so the other comparison I've seen, with the i486 code) also seems misplaced to me.

My contribution to Linux is small - this is the first patch from me for over a decade but I'd hope the pioneering spirit of "because its there" still lives on.

Quoted 1 line

Pedro

Adrian

reviewer
#8 lore raw
Quoted 10 lines · Adrian McMenamin
Quoted 5 lines · Pedro Falcato
Quoted 3 lines · Adrian McMenamin

SEGA Dreamcasts come with a "visual memory unit" (VMU) which contains a slab of flash memory to save games playable on the VMU or data from Dreamcast games.

...

Quoted 3 lines
Quoted 2 lines

These patches implement VMUFAT as a Linux filesystem independent of hardware.

Quoted 8 lines
Quoted 4 lines

Isn't this filesystem a great target for FUSE? You definitely don't need super high performance here.

Also, you're missing the fs maintainers. Adding them.

Apologies for the long delay in replying - I was hoping that maybe someone else would have a view rather than my rather inevitable "can this go in mainline, please?".

Thanks for your work and the filesystem driver but I have to say I agree with Pedro here. A FUSE driver is a much better way of providing access to a filesystem like this. Each filesystem in the kernel adds certain maintenance burden to treewide changes as it needs to be adapted as well and over the years the niche filesystems that have accumulated in the kernel make the treewide changes rather painful and we are rather slowly removing such filesystems from the kernel than adding more.

With a FUSE driver you get very similar user experience as with in-kernel driver - you can just mount the device and tinker with the content - while keeping your code in a userspace library which removes the maintenance burden for the kernel and frankly makes your life simpler as well. The performance is somewhat lower due to additional communication and process switching but for cases like your filesystem I don't expect it to matter.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#9 lore raw
Quoted 8 lines · Jan Kara

With a FUSE driver you get very similar user experience as with in-kernel driver - you can just mount the device and tinker with the content - while keeping your code in a userspace library which removes the maintenance burden for the kernel and frankly makes your life simpler as well. The performance is somewhat lower due to additional communication and process switching but for cases like your filesystem I don't expect it to matter.

Well, I have to admit defeat.

I have discovered there is already a FUSE implementation -
https://github.com/RossMeikleham/FUSE-VMU (I've not tested it but have no reason to think it doesn't work)

I will be keeping my code as an out of tree kernel option for now though/therefore.

Adrian