From 356bceb273855d9619c574d1b7394b53e0fa1e2a 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: Wed, 24 Jul 2013 17:29:50 +0530 Subject: [PATCH] read support for existing files --- README | 59 +++++++++++++++++++++++++++---------------------- mkfs-simplefs.c | 2 +- simple.c | 55 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/README b/README index 2d972ef..40b5a8b 100644 --- a/README +++ b/README @@ -14,12 +14,14 @@ To test: --------- -~/src/simplefs> make -~/src/simplefs> dd bs=4096 count=100 if=/dev/zero of=image +psankar@linux-9dni:~/src/simplefs> make + +psankar@linux-9dni:~/src/simplefs> dd bs=4096 count=100 if=/dev/zero of=image 100+0 records in 100+0 records out -409600 bytes (410 kB) copied, 0.00175839 s, 233 MB/s -~/src/simplefs> ./mkfs-simplefs image +409600 bytes (410 kB) copied, 0.00106026 s, 386 MB/s + +psankar@linux-9dni:~/src/simplefs> ./mkfs-simplefs image Super block written succesfully root directory inode written succesfully welcomefile inode written succesfully @@ -27,28 +29,31 @@ inode store padding bytes (after the two inodes) written sucessfully root directory datablocks (name+inode_no pair for welcomefile) written succesfully padding after the rootdirectory children written succesfully welcomefilebody has been written succesfully -~/src/simplefs> +psankar@linux-9dni:~/src/simplefs> -linux-okb0:/home/psankar/src/simplefs # insmod simplefs.ko;mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/;dmesg -[ 8717.049630] Sucessfully registered simplefs -[ 8717.056140] The magic number obtained in disk is: [268640275] -[ 8717.056147] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. -[ 8717.056183] simplefs is succesfully mounted on [/dev/loop4] -linux-okb0:/home/psankar/src/simplefs # cd mount -linux-okb0:/home/psankar/src/simplefs/mount # ls -vanakkam -linux-okb0:/home/psankar/src/simplefs/mount # ls -lh -total 0 ----------- 1 root root 0 Apr 23 14:25 vanakkam -linux-okb0:/home/psankar/src/simplefs/mount # cd .. -linux-okb0:/home/psankar/src/simplefs # umount mount -linux-okb0:/home/psankar/src/simplefs # rmmod simplefs.ko -linux-okb0:/home/psankar/src/simplefs # dmesg -c -[ 8717.049630] Sucessfully registered simplefs -[ 8717.056140] The magic number obtained in disk is: [268640275] -[ 8717.056147] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. -[ 8717.056183] simplefs is succesfully mounted on [/dev/loop4] -[ 8735.146533] simplefs superblock is destroyed. Unmount succesful. -[ 8740.101526] Sucessfully unregistered simplefs -linux-okb0:/home/psankar/src/simplefs # +Now as root: + +linux-9dni:/home/psankar/src/simplefs # insmod simplefs.ko ; mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/ ; dmesg +[51422.091945] Sucessfully registered simplefs +[51422.094713] The magic number obtained in disk is: [268640275] +[51422.094719] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. +[51422.094744] simplefs is succesfully mounted on [/dev/loop5] + +linux-9dni:/home/psankar/src/simplefs # cat mount/vanakkam +Love is God. God is Love. Anbe Murugan. + +linux-9dni:/home/psankar/src/simplefs # cp mount/vanakkam . + +linux-9dni:/home/psankar/src/simplefs # cat vanakkam +Love is God. God is Love. Anbe Murugan. + +linux-9dni:/home/psankar/src/simplefs # umount mount; rmmod simplefs.ko ; dmesg +[51422.091945] Sucessfully registered simplefs +[51422.094713] The magic number obtained in disk is: [268640275] +[51422.094719] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. +[51422.094744] simplefs is succesfully mounted on [/dev/loop5] +[51469.971919] simplefs superblock is destroyed. Unmount succesful. +[51469.977426] Sucessfully unregistered simplefs + +linux-9dni:/home/psankar/src/simplefs # diff --git a/mkfs-simplefs.c b/mkfs-simplefs.c index 1c68491..1729742 100644 --- a/mkfs-simplefs.c +++ b/mkfs-simplefs.c @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) struct simplefs_inode welcomefile_inode; char welcomefile_name[] = "vanakkam"; - char welcomefile_body[] = "Love is God. God is Love. Anbe Murugan."; + char welcomefile_body[] = "Love is God. God is Love. Anbe Murugan.\n"; const uint64_t WELCOMEFILE_INODE_NUMBER = 2; const uint64_t WELCOMEFILE_DATABLOCK_NUMBER = 3; diff --git a/simple.c b/simple.c index 59550a4..19804f7 100644 --- a/simple.c +++ b/simple.c @@ -81,6 +81,52 @@ struct simplefs_inode *simplefs_get_inode(struct super_block *sb, return NULL; } +ssize_t simplefs_read(struct file * filp, char __user * buf, size_t len, + loff_t * ppos) +{ + /* Hack to make sure that we answer the read call only once and not loop infinitely. + * We need to implement support for filesize in inode to remove this hack */ + static int done = 0; + + /* After the commit dd37978c5 in the upstream linux kernel, + * we can use just filp->f_inode instead of the + * f->f_path.dentry->d_inode redirection */ + struct simplefs_inode *inode = + SIMPLEFS_INODE(filp->f_path.dentry->d_inode); + struct buffer_head *bh; + + char *buffer; + int nbytes; + + if (done) { + done = 0; + return 0; + } + + bh = (struct buffer_head *)sb_bread(filp->f_path.dentry->d_inode->i_sb, + inode->data_block_number); + buffer = (char *)bh->b_data; + nbytes = min(strlen(buffer), len); + + if (copy_to_user(buf, buffer, nbytes)) { + brelse(bh); + printk(KERN_ERR + "Error copying file contents to the userspace buffer\n"); + return -EFAULT; + } + + brelse(bh); + + *ppos += nbytes; + + done = 1; + return nbytes; +} + +const struct file_operations simplefs_file_operations = { + .read = simplefs_read +}; + const struct file_operations simplefs_dir_operations = { .owner = THIS_MODULE, .readdir = simplefs_readdir, @@ -119,7 +165,14 @@ struct dentry *simplefs_lookup(struct inode *parent_inode, inode_init_owner(inode, parent_inode, sfs_inode->mode); inode->i_sb = sb; inode->i_op = &simplefs_inode_ops; - inode->i_fop = &simplefs_dir_operations; + + if (S_ISDIR(inode->i_mode)) + inode->i_fop = &simplefs_dir_operations; + else if (S_ISREG(inode->i_mode)) + inode->i_fop = &simplefs_file_operations; + else + printk(KERN_ERR + "Unknown inode type. Neither a directory nor a file"); /* FIXME: We should store these times to disk and retrieve them */ inode->i_atime = inode->i_mtime = inode->i_ctime =