From 7610120b102d916cd586073bd32506144c73dba3 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 12 Jul 2014 17:38:22 +0400 Subject: [PATCH] mkfs: split into more helpers (and drop malloc just do seek instead) --- mkfs-simplefs.c | 269 +++++++++++++++++++++++++----------------------- 1 file changed, 141 insertions(+), 128 deletions(-) diff --git a/mkfs-simplefs.c b/mkfs-simplefs.c index e59f964..63b4dcc 100644 --- a/mkfs-simplefs.c +++ b/mkfs-simplefs.c @@ -9,22 +9,138 @@ #include "simple.h" +const uint64_t WELCOMEFILE_DATABLOCK_NUMBER = 3; +const uint64_t WELCOMEFILE_INODE_NUMBER = 2; + +static int write_superblock(int fd) +{ + struct simplefs_super_block sb; + ssize_t ret; + + sb.version = 1; + sb.magic = SIMPLEFS_MAGIC; + sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE; + + /* One inode for rootdirectory and another for a welcome file that we are going to create */ + sb.inodes_count = 2; + + /* FIXME: Free blocks management is not implemented yet */ + sb.free_blocks = ~0; + sb.free_blocks &= ~(1 << WELCOMEFILE_DATABLOCK_NUMBER); + + ret = write(fd, &sb, sizeof(sb)); + if (ret != SIMPLEFS_DEFAULT_BLOCK_SIZE) { + printf + ("bytes written [%d] are not equal to the default block size\n", + (int)ret); + return -1; + } + + printf("Super block written succesfully\n"); + return 0; +} + +static int write_inode_store(int fd) +{ + ssize_t ret; + + struct simplefs_inode root_inode; + + root_inode.mode = S_IFDIR; + root_inode.inode_no = SIMPLEFS_ROOTDIR_INODE_NUMBER; + root_inode.data_block_number = SIMPLEFS_ROOTDIR_DATABLOCK_NUMBER; + root_inode.dir_children_count = 1; + + ret = write(fd, &root_inode, sizeof(root_inode)); + + if (ret != sizeof(root_inode)) { + printf + ("The inode store was not written properly. Retry your mkfs\n"); + return -1; + } + + printf("root directory inode written succesfully\n"); + return 0; +} + +static int write_inode(int fd, const struct simplefs_inode *i) +{ + off_t nbytes; + ssize_t ret; + + ret = write(fd, i, sizeof(*i)); + if (ret != sizeof(*i)) { + printf + ("The welcomefile inode was not written properly. Retry your mkfs\n"); + return -1; + } + printf("welcomefile inode written succesfully\n"); + + nbytes = SIMPLEFS_DEFAULT_BLOCK_SIZE - sizeof(*i) - sizeof(*i); + ret = lseek(fd, nbytes, SEEK_CUR); + if (ret == (off_t)-1) { + printf + ("The padding bytes are not written properly. Retry your mkfs\n"); + return -1; + } + + printf + ("inode store padding bytes (after the two inodes) written sucessfully\n"); + return 0; +} +int write_dirent(int fd, const struct simplefs_dir_record *record) +{ + ssize_t nbytes = sizeof(*record), ret; + + ret = write(fd, record, nbytes); + if (ret != nbytes) { + printf + ("Writing the rootdirectory datablock (name+inode_no pair for welcomefile) has failed\n"); + return -1; + } + printf + ("root directory datablocks (name+inode_no pair for welcomefile) written succesfully\n"); + + nbytes = SIMPLEFS_DEFAULT_BLOCK_SIZE - sizeof(*record); + ret = lseek(fd, nbytes, SEEK_CUR); + if (ret == (off_t)-1) { + printf + ("Writing the padding for rootdirectory children datablock has failed\n"); + return -1; + } + printf + ("padding after the rootdirectory children written succesfully\n"); + return 0; +} +int write_block(int fd, char *block, size_t len) +{ + ssize_t ret; + + ret = write(fd, block, len); + if (ret != len) { + printf("Writing file body has failed\n"); + return -1; + } + printf("block has been written succesfully\n"); + return 0; +} + int main(int argc, char *argv[]) { - int fd, nbytes; + int fd; ssize_t ret; - struct simplefs_super_block sb; - 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.\n"; - const uint64_t WELCOMEFILE_INODE_NUMBER = 2; - const uint64_t WELCOMEFILE_DATABLOCK_NUMBER = 3; - - char *block_padding = NULL; - - struct simplefs_dir_record record; + struct simplefs_inode welcome = { + .mode = S_IFREG, + .inode_no = WELCOMEFILE_INODE_NUMBER, + .data_block_number = WELCOMEFILE_DATABLOCK_NUMBER, + .file_size = sizeof(welcomefile_body), + }; + struct simplefs_dir_record record = { + .filename = "vanakkam", + .inode_no = WELCOMEFILE_INODE_NUMBER, + }; if (argc != 2) { printf("Usage: mkfs-simplefs \n"); @@ -37,126 +153,23 @@ int main(int argc, char *argv[]) return -1; } - /* Begin writing of Block 0 - Super Block */ - sb.version = 1; - sb.magic = SIMPLEFS_MAGIC; - sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE; + ret = 1; + do { + if (write_superblock(fd)) + break; + if (write_inode_store(fd)) + break; - /* One inode for rootdirectory and another for a welcome file that we are going to create */ - sb.inodes_count = 2; + if (write_inode(fd, &welcome)) + break; + if (write_dirent(fd, &record)) + break; + if (write_block(fd, welcomefile_body, welcome.file_size)) + break; - /* FIXME: Free blocks management is not implemented yet */ - sb.free_blocks = ~0; - sb.free_blocks &= ~(1 << WELCOMEFILE_DATABLOCK_NUMBER); + ret = 0; + } while (0); - ret = write(fd, (char *)&sb, sizeof(sb)); - - if (ret != SIMPLEFS_DEFAULT_BLOCK_SIZE) { - printf - ("bytes written [%d] are not equal to the default block size\n", - (int)ret); - ret = -1; - goto exit; - } - - printf("Super block written succesfully\n"); - /* End of writing of Block 0 - Super block */ - - /* Begin writing of Block 1 - Inode Store */ - - root_inode.mode = S_IFDIR; - root_inode.inode_no = SIMPLEFS_ROOTDIR_INODE_NUMBER; - root_inode.data_block_number = SIMPLEFS_ROOTDIR_DATABLOCK_NUMBER; - root_inode.dir_children_count = 1; - - ret = write(fd, (char *)&root_inode, sizeof(root_inode)); - - if (ret != sizeof(root_inode)) { - printf - ("The inode store was not written properly. Retry your mkfs\n"); - ret = -1; - goto exit; - } - 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"); - - /* End of writing of Block 1 - inode Store */ - - /* Begin writing of Block 2 - Root Directory datablocks */ - 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"); - /* End of writing of Block 2 - Root directory contents */ - - /* Begin writing of Block 3 - Welcome file contents */ - 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"); - /* End of writing of Block 3 - Welcome file contents */ - - ret = 0; - -exit: - if (block_padding) { - free(block_padding); - } close(fd); return ret; }