mirror of
https://github.com/psankar/simplefs.git
synced 2025-07-25 06:54:50 +02:00
Code for: [Un]Register filesystem, [un]mount simplefs, Superblock placeholder
This commit is contained in:
parent
1b67698838
commit
24df8c8c44
37
README
37
README
@ -6,4 +6,39 @@ The source files are licensed under Creative Commons Zero License.
|
|||||||
More information at: http://creativecommons.org/publicdomain/zero/1.0/
|
More information at: http://creativecommons.org/publicdomain/zero/1.0/
|
||||||
Full license text at: http://creativecommons.org/publicdomain/zero/1.0/legalcode
|
Full license text at: http://creativecommons.org/publicdomain/zero/1.0/legalcode
|
||||||
|
|
||||||
To compile, install linux kernel sources and run make from the checkedout directory.
|
To compile:
|
||||||
|
------------
|
||||||
|
install linux kernel sources and run make from the checkedout directory.
|
||||||
|
|
||||||
|
To test:
|
||||||
|
---------
|
||||||
|
|
||||||
|
One-time-activity:
|
||||||
|
|
||||||
|
linux-okb0:/home/psankar/src/simplefs > dd bs=1M count=100 if=/dev/zero of=image
|
||||||
|
linux-okb0:/home/psankar/src/simplefs > mkdir mount
|
||||||
|
|
||||||
|
Repeat everytime after making changes to the source:
|
||||||
|
|
||||||
|
linux-okb0:/home/psankar/src/simplefs > make
|
||||||
|
linux-okb0:/home/psankar/src/simplefs > su - ; # switch to root user if not already, or use sudo
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # insmod simplefs.ko
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||||
|
[ 7467.426112] Sucessfully registered simplefs
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||||
|
[ 7467.426112] Sucessfully registered simplefs
|
||||||
|
[ 7478.223432] simplefs is succesfully mounted on [/dev/loop1]
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # umount /home/psankar/src/simplefs/mount/
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||||
|
[ 7467.426112] Sucessfully registered simplefs
|
||||||
|
[ 7478.223432] simplefs is succesfully mounted on [/dev/loop1]
|
||||||
|
[ 7498.889439] simplefs superblock is destroyed. Unmount succesful.
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # rmmod simplefs.ko
|
||||||
|
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||||
|
[ 7467.426112] Sucessfully registered simplefs
|
||||||
|
[ 7478.223432] simplefs is succesfully mounted on [/dev/loop1]
|
||||||
|
[ 7498.889439] simplefs superblock is destroyed. Unmount succesful.
|
||||||
|
[ 7506.906067] Sucessfully unregistered simplefs
|
||||||
|
linux-okb0:/home/psankar/src/simplefs #
|
||||||
|
107
simple.c
107
simple.c
@ -7,16 +7,117 @@
|
|||||||
|
|
||||||
#include <linux/init.h>
|
#include <linux/init.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
|
#include <linux/fs.h>
|
||||||
|
|
||||||
|
/* This function creates, configures and returns an inode,
|
||||||
|
* for the asked file (or) directory (differentiated by the mode param),
|
||||||
|
* under the directory specified by the dir param
|
||||||
|
* on the device dev, managed by the superblock sb param) */
|
||||||
|
struct inode *simplefs_get_inode(struct super_block *sb,
|
||||||
|
const struct inode *dir, umode_t mode,
|
||||||
|
dev_t dev)
|
||||||
|
{
|
||||||
|
struct inode *inode = new_inode(sb);
|
||||||
|
|
||||||
|
if (inode) {
|
||||||
|
inode->i_ino = get_next_ino();
|
||||||
|
inode_init_owner(inode, dir, mode);
|
||||||
|
|
||||||
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
||||||
|
|
||||||
|
switch (mode & S_IFMT) {
|
||||||
|
case S_IFDIR:
|
||||||
|
/* i_nlink will be initialized to 1 in the inode_init_always function
|
||||||
|
* (that gets called inside the new_inode function),
|
||||||
|
* We change it to 2 for directories, for covering the "." entry */
|
||||||
|
inc_nlink(inode);
|
||||||
|
break;
|
||||||
|
case S_IFREG:
|
||||||
|
case S_IFLNK:
|
||||||
|
default:
|
||||||
|
printk(KERN_ERR
|
||||||
|
"simplefs can create meaningful inode for only root directory at the moment\n");
|
||||||
|
return NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This function, as the name implies, Makes the super_block valid and
|
||||||
|
* fills filesystem specific information in the super block */
|
||||||
|
int simplefs_fill_super(struct super_block *sb, void *data, int silent)
|
||||||
|
{
|
||||||
|
struct inode *inode;
|
||||||
|
|
||||||
|
/* A magic number that uniquely identifies our filesystem type */
|
||||||
|
sb->s_magic = 0x10032013;
|
||||||
|
|
||||||
|
inode = simplefs_get_inode(sb, NULL, S_IFDIR, 0);
|
||||||
|
sb->s_root = d_make_root(inode);
|
||||||
|
if (!sb->s_root)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct dentry *simplefs_mount(struct file_system_type *fs_type,
|
||||||
|
int flags, const char *dev_name,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
struct dentry *ret;
|
||||||
|
|
||||||
|
ret = mount_bdev(fs_type, flags, dev_name, data, simplefs_fill_super);
|
||||||
|
|
||||||
|
if (unlikely(IS_ERR(ret)))
|
||||||
|
printk(KERN_ERR "Error mounting simplefs");
|
||||||
|
else
|
||||||
|
printk(KERN_INFO "simplefs is succesfully mounted on [%s]\n",
|
||||||
|
dev_name);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void simplefs_kill_superblock(struct super_block *s)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO
|
||||||
|
"simplefs superblock is destroyed. Unmount succesful.\n");
|
||||||
|
/* This is just a dummy function as of now. As our filesystem gets matured,
|
||||||
|
* we will do more meaningful operations here */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct file_system_type simplefs_fs_type = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.name = "simplefs",
|
||||||
|
.mount = simplefs_mount,
|
||||||
|
.kill_sb = simplefs_kill_superblock,
|
||||||
|
};
|
||||||
|
|
||||||
static int simplefs_init(void)
|
static int simplefs_init(void)
|
||||||
{
|
{
|
||||||
printk(KERN_ALERT "Hello World\n");
|
int ret;
|
||||||
return 0;
|
|
||||||
|
ret = register_filesystem(&simplefs_fs_type);
|
||||||
|
if (likely(ret == 0))
|
||||||
|
printk(KERN_INFO "Sucessfully registered simplefs\n");
|
||||||
|
else
|
||||||
|
printk(KERN_ERR "Failed to register simplefs. Error:[%d]", ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void simplefs_exit(void)
|
static void simplefs_exit(void)
|
||||||
{
|
{
|
||||||
printk(KERN_ALERT "Goodbye World\n");
|
int ret;
|
||||||
|
|
||||||
|
ret = unregister_filesystem(&simplefs_fs_type);
|
||||||
|
|
||||||
|
if (likely(ret == 0))
|
||||||
|
printk(KERN_INFO "Sucessfully unregistered simplefs\n");
|
||||||
|
else
|
||||||
|
printk(KERN_ERR "Failed to unregister simplefs. Error:[%d]",
|
||||||
|
ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(simplefs_init);
|
module_init(simplefs_init);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user