readdir and lookup support so ls works fine without errors

This commit is contained in:
Sankar சங்கர் 2013-04-23 14:27:02 +05:30
parent 233991006f
commit b5aa56df68
2 changed files with 72 additions and 50 deletions

44
README
View File

@ -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 #

View File

@ -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)