Write a module to create a small file system called "myfs" that does only a few operations including read and write. First, define your file system type using:
static struct file_system_type myfs_type = {
.owner = THIS_MODULE,
.name = "myfs",
.mount = myfs_mount,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
See static struct file_system_type ext4_fs_type defined in fs/ext4/super.c as an example.
Write a module to register/unregister your own file system:
module_init(myfs_init_fs)
module_exit(mtfs_exit_fs)
myfs_init_fs will register myfs_type defined above while myfs_exit_fs will unregister.
See module_init(ext4_init_fs) and module_exit(ext4_exit_fs) defined in fs/ext4/super.c as an example. 3318-4292
Implement myfs_mount() defined in myfs_type just like ext4_mount, except you need to pass your own func pointer myfs_fill_super which will be called in mount_bdev().
As we discussed in class, global variable super_blocks holds a list of super blocks using list_head and mount_bdev() gets a superblock which needs to be filled by myfs_fill_super. Hence, you need to implement myfs_fill_super(). Start with simple_fill_super() as an example to implement your own. ext4_fill_super is almost 1000 lines which you don't want to bother at this moment.
Add your own super_operations to super_block and again the starting point is simple_super_operations while "super_operations ext4_sops" exists for ext4. simple_super_operations has nothing much in it so you need to add a couple of fields such as .alloc_inode by consulting ext4_ops.
static struct super_operations myfs_super_operations = {
.statfs = simple_statfs,
.alloc_inode = ext4_alloc_inode,
};
Add inode_operations to super_block as well. Again as simple_dir_inode_operations has not much in it, you need to check with ext4_ops to add a few fields.
static struct inode_operations myfs_inode_operations = {
.lookup = simple_lookup,
.link = simple_link,
.mkdir = simple_mkdir,
};
Do similiar for basic file_operations for myfs_file_ops with a couple of additional fields:
static struct file_operations myfs_file_operations = {
.open = myfs_open,
.read = myfs_read_file,
.write = myfs_write_file,
};
Don't forget to put in a magic number to distinguish your file system with others. See #define EXT4_SUPER_MAGIC 0xEF53 as an example.
#define MYFS_SUPER_MAGIC 0x????
Afer all is said and done, /sbin/insmod the ko file and "mount -t myfs myfs /mnt";.
Issue the following commands:
cat /proc/filesystems;
ls -l /mnt;
mount;
cat /mnt/myfile0;
stat -f myfile0;
ln /mnt/myfile0 /mnt/link0 ...
Show all the output results including linking