mirror of
https://github.com/psankar/simplefs.git
synced 2025-07-25 23:14:33 +02:00
mkfs creates a welcome file. readdir(ls) tries to list this file
This commit is contained in:
parent
fec3cb7f9b
commit
f5cf481557
@ -4,15 +4,27 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "simple.h"
|
#include "simple.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int fd;
|
int fd, nbytes;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
struct simplefs_super_block sb;
|
struct simplefs_super_block sb;
|
||||||
struct simplefs_inode root_inode;
|
struct simplefs_inode root_inode;
|
||||||
|
struct simplefs_inode welcomefile_inode;
|
||||||
|
|
||||||
|
char welcomefile_name[] = "vanakkam";
|
||||||
|
char welcomefile_body[] = "Love is God. God is Love. Anbe Murugan.";
|
||||||
|
const uint64_t WELCOMEFILE_INODE_NUMBER = 2;
|
||||||
|
const uint64_t WELCOMEFILE_DATABLOCK_NUMBER = 3;
|
||||||
|
|
||||||
|
char *block_padding;
|
||||||
|
|
||||||
|
struct simplefs_dir_record record;
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
printf("Usage: mkfs-simplefs <device>\n");
|
printf("Usage: mkfs-simplefs <device>\n");
|
||||||
@ -29,8 +41,10 @@ int main(int argc, char *argv[])
|
|||||||
sb.magic = SIMPLEFS_MAGIC;
|
sb.magic = SIMPLEFS_MAGIC;
|
||||||
sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE;
|
sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE;
|
||||||
|
|
||||||
/* Only the root dir will have an inode now */
|
/* One inode for rootdirectory and another for a welcome file that we are going to create */
|
||||||
sb.inodes_count = 1;
|
sb.inodes_count = 2;
|
||||||
|
|
||||||
|
/* FIXME: Free blocks management is not implemented yet */
|
||||||
sb.free_blocks = ~0;
|
sb.free_blocks = ~0;
|
||||||
|
|
||||||
ret = write(fd, (char *)&sb, sizeof(sb));
|
ret = write(fd, (char *)&sb, sizeof(sb));
|
||||||
@ -48,7 +62,7 @@ int main(int argc, char *argv[])
|
|||||||
root_inode.mode = S_IFDIR;
|
root_inode.mode = S_IFDIR;
|
||||||
root_inode.inode_no = SIMPLEFS_ROOTDIR_INODE_NUMBER;
|
root_inode.inode_no = SIMPLEFS_ROOTDIR_INODE_NUMBER;
|
||||||
root_inode.data_block_number = SIMPLEFS_ROOTDIR_DATABLOCK_NUMBER;
|
root_inode.data_block_number = SIMPLEFS_ROOTDIR_DATABLOCK_NUMBER;
|
||||||
root_inode.dir_children_count = 0;
|
root_inode.dir_children_count = 1;
|
||||||
|
|
||||||
ret = write(fd, (char *)&root_inode, sizeof(root_inode));
|
ret = write(fd, (char *)&root_inode, sizeof(root_inode));
|
||||||
|
|
||||||
@ -57,7 +71,65 @@ int main(int argc, char *argv[])
|
|||||||
ret = -1;
|
ret = -1;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
printf("inode store written succesfully\n");
|
printf("root directory inode written succesfully\n");
|
||||||
|
|
||||||
|
welcomefile_inode.mode = S_IFREG;
|
||||||
|
welcomefile_inode.inode_no = WELCOMEFILE_INODE_NUMBER;
|
||||||
|
welcomefile_inode.data_block_number = WELCOMEFILE_DATABLOCK_NUMBER;
|
||||||
|
welcomefile_inode.file_size = sizeof(welcomefile_body);
|
||||||
|
ret = write(fd, (char *)&welcomefile_inode, sizeof(root_inode));
|
||||||
|
|
||||||
|
if (ret != sizeof(root_inode)) {
|
||||||
|
printf("The welcomefile inode was not written properly. Retry your mkfs\n");
|
||||||
|
ret = -1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
printf("welcomefile inode written succesfully\n");
|
||||||
|
|
||||||
|
nbytes = SIMPLEFS_DEFAULT_BLOCK_SIZE - sizeof(root_inode) - sizeof(welcomefile_inode);
|
||||||
|
block_padding = malloc(nbytes);
|
||||||
|
|
||||||
|
ret = write(fd, block_padding, nbytes);
|
||||||
|
|
||||||
|
if (ret != nbytes) {
|
||||||
|
printf("The padding bytes are not written properly. Retry your mkfs\n");
|
||||||
|
ret = -1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
printf("inode store padding bytes (after the two inodes) written sucessfully\n");
|
||||||
|
|
||||||
|
strcpy(record.filename, welcomefile_name);
|
||||||
|
record.inode_no = WELCOMEFILE_INODE_NUMBER;
|
||||||
|
nbytes = sizeof(record);
|
||||||
|
|
||||||
|
ret = write(fd, (char *)&record, nbytes);
|
||||||
|
if (ret != nbytes) {
|
||||||
|
printf("Writing the rootdirectory datablock (name+inode_no pair for welcomefile) has failed\n");
|
||||||
|
ret = -1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
printf("root directory datablocks (name+inode_no pair for welcomefile) written succesfully\n");
|
||||||
|
|
||||||
|
nbytes = SIMPLEFS_DEFAULT_BLOCK_SIZE - sizeof(record);
|
||||||
|
block_padding = realloc(block_padding, nbytes);
|
||||||
|
|
||||||
|
ret = write(fd, block_padding, nbytes);
|
||||||
|
if (ret != nbytes) {
|
||||||
|
printf("Writing the padding for rootdirectory children datablock has failed\n");
|
||||||
|
ret = -1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
printf("padding after the rootdirectory children written succesfully\n");
|
||||||
|
|
||||||
|
nbytes = sizeof(welcomefile_body);
|
||||||
|
ret = write(fd, welcomefile_body, nbytes);
|
||||||
|
if (ret != nbytes) {
|
||||||
|
printf("Writing welcomefile body has failed\n");
|
||||||
|
ret = -1;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
printf("welcomefilebody has been written succesfully\n");
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
10
simple.c
10
simple.c
@ -23,7 +23,12 @@ static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||||||
struct simplefs_dir_record *record;
|
struct simplefs_dir_record *record;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
printk(KERN_INFO "We are inside readdir. The pos[%lld], inode number[%lu], superblock magic [%lu]\n", pos, inode->i_ino, sb->s_magic);
|
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");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
sfs_inode = SIMPLEFS_INODE(inode);
|
sfs_inode = SIMPLEFS_INODE(inode);
|
||||||
|
|
||||||
@ -38,11 +43,12 @@ static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
|||||||
for (i=0; i < sfs_inode->dir_children_count; i++) {
|
for (i=0; i < sfs_inode->dir_children_count; i++) {
|
||||||
printk(KERN_INFO "Got filename: %s\n", record->filename);
|
printk(KERN_INFO "Got filename: %s\n", record->filename);
|
||||||
filldir(dirent, record->filename, SIMPLEFS_FILENAME_MAXLEN, pos, record->inode_no, DT_UNKNOWN);
|
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);
|
pos += sizeof(struct simplefs_dir_record);
|
||||||
record ++;
|
record ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct file_operations simplefs_dir_operations = {
|
const struct file_operations simplefs_dir_operations = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user