From 6f1a62a173a28b29f10f0dd3a3bae164a9246a66 Mon Sep 17 00:00:00 2001 From: Seiyeon Cho Date: Wed, 24 Apr 2024 12:27:18 +0900 Subject: [PATCH 1/3] Use binary search for searching extent --- extent.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/extent.c b/extent.c index cef8b0a..c371f81 100644 --- a/extent.c +++ b/extent.c @@ -6,19 +6,69 @@ /* Search for the extent containing the target block. * Returns the first unused file index if not found. * Returns -1 if the target block is out of range. - * TODO: Implement binary search for efficiency. + * Binary search is used for efficiency. */ uint32_t simplefs_ext_search(struct simplefs_file_ei_block *index, uint32_t iblock) { - uint32_t i; - for (i = 0; i < SIMPLEFS_MAX_EXTENTS; i++) { - uint32_t block = index->extents[i].ee_block; - uint32_t len = index->extents[i].ee_len; - if (index->extents[i].ee_start == 0 || - (iblock >= block && iblock < block + len)) - return i; + /* first, find the first unused file index with binary search. + * It'll be our right boundary for actual binary search + * and return value when file index is not found. + */ + uint32_t start = 0; + uint32_t end = SIMPLEFS_MAX_EXTENTS - 1; + uint32_t boundary; + uint32_t end_block; + uint32_t end_len; + + while (start < end) { + uint32_t mid = start + (end - start) / 2; + if (index->extents[mid].ee_start == 0) { + end = mid; + } else { + start = mid + 1; + } + } + + if (index->extents[end].ee_start == 0) { + boundary = end; + } else { + /* File index full */ + boundary = end + 1; + } + + if (boundary == 0) { + /* No used file index */ + return boundary; + } + + /* try finding target block using binary search */ + start = 0; + end = boundary - 1; + while (start < end) { + uint32_t mid = start + (end - start) / 2; + uint32_t block = index->extents[mid].ee_block; + uint32_t len = index->extents[mid].ee_len; + if (iblock >= block && iblock < block + len) { + /* found before search finished */ + return mid; + } + if (iblock < block) { + end = mid; + } else { + start = mid + 1; + } } - return -1; -} + /* return 'end' if it directs to valid block + * return 'boundary' if index is not found + * and eiblock has remaining space */ + end_block = index->extents[end].ee_block; + end_len = index->extents[end].ee_len; + if (iblock >= end_block && iblock < end_len) { + return end; + } else if (boundary < SIMPLEFS_MAX_EXTENTS) { + return boundary; + } + return boundary; +} \ No newline at end of file From 6bcbe74d375129fd2232d49362cc6725a8dea9e9 Mon Sep 17 00:00:00 2001 From: "Tse-Chia.Chang" Date: Thu, 2 May 2024 22:41:46 +0800 Subject: [PATCH 2/3] Fix typo --- simplefs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplefs.h b/simplefs.h index 29fa3d9..84d5e60 100644 --- a/simplefs.h +++ b/simplefs.h @@ -123,7 +123,7 @@ extern const struct address_space_operations simplefs_aops; extern uint32_t simplefs_ext_search(struct simplefs_file_ei_block *index, uint32_t iblock); -/* Getters for superbock and inode */ +/* Getters for superblock and inode */ #define SIMPLEFS_SB(sb) (sb->s_fs_info) #define SIMPLEFS_INODE(inode) \ (container_of(inode, struct simplefs_inode_info, vfs_inode)) From 56ea86a1df6e1063f09fdb24599b7b5a4ed085d6 Mon Sep 17 00:00:00 2001 From: "Tse-Chia.Chang" Date: Fri, 3 May 2024 12:12:33 +0800 Subject: [PATCH 3/3] Fix errors raised by missing-prototypes Fix compile time errors "no previous prototype for simplefs_mount" and "no previous prototype for simplefs_kill_sb" --- simplefs.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/simplefs.h b/simplefs.h index 84d5e60..158468d 100644 --- a/simplefs.h +++ b/simplefs.h @@ -108,12 +108,19 @@ struct simplefs_dir_block { /* superblock functions */ int simplefs_fill_super(struct super_block *sb, void *data, int silent); +void simplefs_kill_sb(struct super_block *sb); /* inode functions */ int simplefs_init_inode_cache(void); void simplefs_destroy_inode_cache(void); struct inode *simplefs_iget(struct super_block *sb, unsigned long ino); +/* dentry function */ +struct dentry *simplefs_mount(struct file_system_type *fs_type, + int flags, + const char *dev_name, + void *data); + /* file functions */ extern const struct file_operations simplefs_file_ops; extern const struct file_operations simplefs_dir_ops;