From b5aa56df68011d556c2c1f43842b1abb67ce3b58 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: Tue, 23 Apr 2013 14:27:02 +0530 Subject: [PATCH] readdir and lookup support so ls works fine without errors --- README | 44 ++++++++++++-------------------- simple.c | 78 ++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 50 deletions(-) diff --git a/README b/README index 024391a..2d972ef 100644 --- a/README +++ b/README @@ -29,38 +29,26 @@ padding after the rootdirectory children written succesfully welcomefilebody has been written succesfully ~/src/simplefs> -# switch to root user (or use with 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 -[15446.634827] Sucessfully registered simplefs -[15446.650771] The magic number obtained in disk is: [268640275] -[15446.650775] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. -[15446.650789] simplefs is succesfully mounted on [/dev/loop12] -linux-okb0:/home/psankar/src/simplefs # cd mount/ +[ 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 -ls: cannot access vanakkam: No such file or directory vanakkam -linux-okb0:/home/psankar/src/simplefs/mount # dmesg -[15446.634827] Sucessfully registered simplefs -[15446.650771] The magic number obtained in disk is: [268640275] -[15446.650775] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. -[15446.650789] simplefs is succesfully mounted on [/dev/loop12] -[15452.332645] We are inside readdir. The pos[0], inode number[1], superblock magic [268640275] inodesize [0] -[15452.332658] Got filename: vanakkam -[15452.332839] We are inside readdir. The pos[264], inode number[1], superblock magic [268640275] inodesize [0] -[15452.332844] pos seem to be non-zero which means we have already filled in all the details +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 -[15446.634827] Sucessfully registered simplefs -[15446.650771] The magic number obtained in disk is: [268640275] -[15446.650775] simplefs filesystem of version [1] formatted with a block size of [4096] detected in the device. -[15446.650789] simplefs is succesfully mounted on [/dev/loop12] -[15452.332645] We are inside readdir. The pos[0], inode number[1], superblock magic [268640275] inodesize [0] -[15452.332658] Got filename: vanakkam -[15452.332839] We are inside readdir. The pos[264], inode number[1], superblock magic [268640275] inodesize [0] -[15452.332844] pos seem to be non-zero which means we have already filled in all the details -[15462.135603] simplefs superblock is destroyed. Unmount succesful. -[15467.279797] Sucessfully unregistered simplefs +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 # diff --git a/simple.c b/simple.c index 883dd5b..1ef5b70 100644 --- a/simple.c +++ b/simple.c @@ -23,10 +23,10 @@ static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir) struct simplefs_dir_record *record; int i; - printk(KERN_INFO "We are inside readdir. The pos[%lld], inode number[%lu], superblock magic [%lu] inodesize [%lld]\n", pos, inode->i_ino, sb->s_magic, inode->i_size); - if (pos) { - printk(KERN_INFO "pos seem to be non-zero which means we have already filled in all the details\n"); + /* FIXME: We use a hack of reading pos to figure if we have filled in all data. + * We should probably fix this to work in a cursor based model and + * use the tokens correctly to not fill too many data in each cursor based call */ return 0; } @@ -41,7 +41,6 @@ static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir) record = (struct simplefs_dir_record *) bh->b_data; for (i=0; i < sfs_inode->dir_children_count; i++) { - printk(KERN_INFO "Got filename: %s\n", record->filename); filldir(dirent, record->filename, SIMPLEFS_FILENAME_MAXLEN, pos, record->inode_no, DT_UNKNOWN); filp->f_pos += sizeof(struct simplefs_dir_record); pos += sizeof(struct simplefs_dir_record); @@ -51,24 +50,6 @@ static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir) return 0; } -const struct file_operations simplefs_dir_operations = { - .owner = THIS_MODULE, - .readdir = simplefs_readdir, -}; - -struct dentry *simplefs_lookup(struct inode *parent_inode, - struct dentry *child_dentry, unsigned int flags) -{ - /* The lookup function is used for dentry association. - * As of now, we don't deal with dentries in simplefs. - * So we will keep this simple for now and revisit later */ - return NULL; -} - -static struct inode_operations simplefs_inode_ops = { - .lookup = simplefs_lookup, -}; - /* This functions returns a simplefs_inode with the given inode_no * from the inode store, if it exists. */ struct simplefs_inode * simplefs_get_inode(struct super_block *sb, uint64_t inode_no) @@ -96,6 +77,59 @@ struct simplefs_inode * simplefs_get_inode(struct super_block *sb, uint64_t inod return NULL; } +const struct file_operations simplefs_dir_operations = { + .owner = THIS_MODULE, + .readdir = simplefs_readdir, +}; + +struct dentry *simplefs_lookup(struct inode *parent_inode, + struct dentry *child_dentry, unsigned int flags); + +static struct inode_operations simplefs_inode_ops = { + .lookup = simplefs_lookup, +}; + +struct dentry *simplefs_lookup(struct inode *parent_inode, + struct dentry *child_dentry, unsigned int flags) +{ + struct simplefs_inode *parent = SIMPLEFS_INODE(parent_inode); + struct super_block *sb = parent_inode->i_sb; + struct buffer_head *bh; + struct simplefs_dir_record *record; + int i; + + bh = (struct buffer_head *)sb_bread(sb, parent->data_block_number); + record = (struct simplefs_dir_record *) bh->b_data; + for (i=0; i < parent->dir_children_count; i++) { + if (!strcmp(record->filename, child_dentry->d_name.name)) { + + struct inode *inode; + struct simplefs_inode *sfs_inode; + + /* FIXME: This simplefs_inode is leaking */ + sfs_inode = simplefs_get_inode(sb, record->inode_no); + + /* FIXME: This inode is leaking */ + inode = new_inode(sb); + inode->i_ino = record->inode_no; + 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; + + /* FIXME: We should store these times to disk and retrieve them */ + inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; + + inode->i_private = sfs_inode; + + d_add(child_dentry, inode); + return NULL; + } + } + + return NULL; +} + /* 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)