From 5b5062b46ebd16c00e50421db6f5cfe1f9cb5883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sankar=20=E0=AE=9A=E0=AE=99=E0=AF=8D=E0=AE=95=E0=AE=B0?= =?UTF-8?q?=E0=AF=8D?= Date: Sat, 30 Mar 2013 00:13:38 +0530 Subject: [PATCH] mkfs-simplefs: An utility to create simplefs on a device. --- Makefile | 8 +++++++- README | 25 +++++++++++-------------- mkfs-simplefs.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ simple.c | 4 +++- simple.h | 11 +++++++++++ 5 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 mkfs-simplefs.c create mode 100644 simple.h diff --git a/Makefile b/Makefile index 0e14332..591554f 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,14 @@ obj-m := simplefs.o simplefs-objs := simple.o -all: +all: ko mkfs-simplefs + +ko: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules +mkfs-simplefs_SOURCES: + mkfs-simplefs.c simple.h + clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean + rm mkfs-simplefs diff --git a/README b/README index fad4385..2523fff 100644 --- a/README +++ b/README @@ -15,7 +15,7 @@ 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 > dd bs=4096 count=100 if=/dev/zero of=image linux-okb0:/home/psankar/src/simplefs > mkdir mount Repeat everytime after making changes to the source: @@ -23,17 +23,14 @@ 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 -c -linux-okb0:/home/psankar/src/simplefs # insmod simplefs.ko ; mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/ ; dmesg -[ 5788.269663] Sucessfully registered simplefs -[ 5788.274803] simplefs is succesfully mounted on [/dev/loop2] -linux-okb0:/home/psankar/src/simplefs # cd mount/ -linux-okb0:/home/psankar/src/simplefs/mount # ls -linux-okb0:/home/psankar/src/simplefs/mount # cd .. -linux-okb0:/home/psankar/src/simplefs # umount mount ; rmmod simplefs.ko -linux-okb0:/home/psankar/src/simplefs # dmesg -[ 5788.269663] Sucessfully registered simplefs -[ 5788.274803] simplefs is succesfully mounted on [/dev/loop2] -[ 5814.008604] simplefs superblock is destroyed. Unmount succesful. -[ 5814.014065] Sucessfully unregistered simplefs +# Creating a loop back device and formatting it with our shiny, brand new simplefs + +linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0 +losetup: /dev/loop0: No such file or directory +linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0 image +linux-okb0:/home/psankar/src/simplefs # ./mkfs-simplefs /dev/loop0 +Super block written succesfully +linux-okb0:/home/psankar/src/simplefs # losetup -d /dev/loop0 +linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0 +losetup: /dev/loop0: No such file or directory linux-okb0:/home/psankar/src/simplefs # diff --git a/mkfs-simplefs.c b/mkfs-simplefs.c new file mode 100644 index 0000000..04ce92e --- /dev/null +++ b/mkfs-simplefs.c @@ -0,0 +1,44 @@ +#include "simple.h" + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int fd; + ssize_t ret; + struct simplefs_super_block sb; + + if (argc != 2) { + printf("Usage: mkfs-simplefs \n"); + return -1; + } + + fd = open(argv[1], O_RDWR); + if (fd == -1) { + perror("Error opening the device"); + return -1; + } + + sb.version = 1; + sb.magic = SIMPLEFS_MAGIC; + sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE; + sb.free_blocks = ~0; + + ret = write(fd, (char *)&sb, sizeof(sb)); + + /* Just a redundant check. Not required ideally. */ + if (ret != SIMPLEFS_DEFAULT_BLOCK_SIZE) + printf + ("bytes written [%d] are not equal to the default block size", + (int)ret); + else + printf("Super block written succesfully"); + + close(fd); + + return 0; +} diff --git a/simple.c b/simple.c index a4bc36f..69c82c2 100644 --- a/simple.c +++ b/simple.c @@ -9,6 +9,8 @@ #include #include +#include "simple.h" + static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir) { /* ls will list nothing as of now. @@ -76,7 +78,7 @@ 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; + sb->s_magic = SIMPLEFS_MAGIC; inode = simplefs_get_inode(sb, NULL, S_IFDIR, 0); inode->i_op = &simplefs_inode_ops; diff --git a/simple.h b/simple.h new file mode 100644 index 0000000..1e54c7b --- /dev/null +++ b/simple.h @@ -0,0 +1,11 @@ +const int SIMPLEFS_MAGIC = 0x10032013; +const int SIMPLEFS_DEFAULT_BLOCK_SIZE = 4 * 1024; + +struct simplefs_super_block { + unsigned int version; + unsigned int magic; + unsigned int block_size; + unsigned int free_blocks; + + char padding[ (4 * 1024) - (4 * sizeof(unsigned int))]; +};