Skip to content

Commit ca74c03

Browse files
committed
Use hash func to boost file creation and lookup
Introduce a hash-based mechanism to speed up file creation and lookup operations. The hash function enables faster access to extent and logical block extent index, improving overall filesystem performance. hash_code = file_hash(file_name); extent index = hash_code / SIMPLEFS_MAX_BLOCKS_PER_EXTENT block index = hash_code % SIMPLEFS_MAX_BLOCKS_PER_EXTENT;
1 parent 9013e3f commit ca74c03

File tree

10 files changed

+420
-239
lines changed

10 files changed

+420
-239
lines changed

.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Automatically generated file; DO NOT EDIT.
3+
# Configuration
4+
#
5+
6+
#
7+
# Simplefs Configuration
8+
#
9+
# CONFIG_SIMPLEFS_HASH_HASH64 is not set
10+
CONFIG_SIMPLEFS_HASH_FNV1A=y
11+
# CONFIG_SIMPLEFS_HASH_FULLNAME is not set

Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
menu "Simplefs Configuration"
2+
3+
choice
4+
prompt "Select hash method"
5+
default SIMPLEFS_HASH_HASH64
6+
7+
config SIMPLEFS_HASH_HASH64
8+
bool "Use hash64 (CRC32 + hash_64)"
9+
10+
config SIMPLEFS_HASH_FNV1A
11+
bool "Use FNV-1a 64-bit hash"
12+
13+
config SIMPLEFS_HASH_FULLNAME
14+
bool "Use full_name_hash (Linux built-in)"
15+
endchoice
16+
17+
endmenu

Makefile

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
obj-m += simplefs.o
2-
simplefs-objs := fs.o super.o inode.o file.o dir.o extent.o
3-
2+
simplefs-objs := fs.o super.o inode.o file.o dir.o extent.o symlink.o hash.o
43
KDIR ?= /lib/modules/$(shell uname -r)/build
54

5+
CONFIG := .config
6+
CONFIG_H := config.h
7+
68
MKFS = mkfs.simplefs
9+
MENUCONFIG := kconfig-mconf
710

8-
all: $(MKFS)
11+
all: $(MKFS) $(CONFIG_H)
912
make -C $(KDIR) M=$(PWD) modules
1013

1114
IMAGE ?= test.img
@@ -23,6 +26,26 @@ $(IMAGE): $(MKFS)
2326
dd if=/dev/zero of=${IMAGE} bs=1M count=${IMAGESIZE}
2427
./$< $(IMAGE)
2528

29+
$(CONFIG_H): $(CONFIG)
30+
@echo "/*" > $(CONFIG_H)
31+
@echo " * Automatically generated file; DO NOT EDIT." >> $(CONFIG_H)
32+
@echo " * Configuration" >> $(CONFIG_H)
33+
@echo " */" >> $(CONFIG_H)
34+
@echo "" >> $(CONFIG_H)
35+
@sed \
36+
-e '/^$$/d' \
37+
-e '/^[[:space:]]*# *$$/d' \
38+
-e '/^[[:space:]]*# Automatically/d' \
39+
-e '/^[[:space:]]*# Configuration/d' \
40+
-e '/^[[:space:]]*# \(CONFIG_[A-Z0-9_]*\) is not set/ s//#undef \1/' \
41+
-e '/^[[:space:]]*CONFIG_[A-Z0-9_]*=y/ s/^[[:space:]]*\(CONFIG_[A-Z0-9_]*\)=y/#define \1 1/' \
42+
-e '/^[[:space:]]*CONFIG_[A-Z0-9_]*=[0-9]\+/ s/^[[:space:]]*\(CONFIG_[A-Z0-9_]*\)=\([0-9]*\)/#define \1 \2/' \
43+
-e '/^[[:space:]]*# / s/^[[:space:]]*# \(.*\)/\/\* \1 \*\//' \
44+
$(CONFIG) >> $(CONFIG_H)
45+
46+
menuconfig:
47+
$(MENUCONFIG) Kconfig
48+
2649
journal: $(JOURNAL)
2750

2851
$(JOURNAL):
@@ -35,6 +58,9 @@ check: all
3558
clean:
3659
make -C $(KDIR) M=$(PWD) clean
3760
rm -f *~ $(PWD)/*.ur-safe
38-
rm -f $(MKFS) $(IMAGE) $(JOURNAL)
61+
rm -f $(MKFS) $(IMAGE) $(JOURNAL) config.h
62+
63+
distclean: clean
64+
rm -f $(CONFIG)
3965

40-
.PHONY: all clean journal
66+
.PHONY: all clean journal menuconfig distclean

bitmap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ static inline uint32_t get_free_blocks(struct super_block *sb, uint32_t len)
4848
struct simplefs_sb_info *sbi = SIMPLEFS_SB(sb);
4949
uint32_t ret = get_first_free_bits(sbi->bfree_bitmap, sbi->nr_blocks, len);
5050
uint32_t i;
51+
struct buffer_head *bh;
5152
if (!ret) /* No enough free blocks */
5253
return 0;
5354

5455
sbi->nr_free_blocks -= len;
55-
struct buffer_head *bh;
5656
for (i = 0; i < len; i++) {
5757
bh = sb_bread(sb, ret + i);
5858
if (!bh) {

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
8181
continue;
8282
}
8383

84-
for (fi = 0; fi < SIMPLEFS_FILES_PER_BLOCK;) {
84+
for (fi = 0; fi < SIMPLEFS_FILES_PER_BLOCK && dblock->nr_files;) {
8585
if (dblock->files[fi].inode != 0) {
8686
if (offset) {
8787
offset--;

hash.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <linux/crc32.h>
2+
#include <linux/hash.h>
3+
#include <linux/stringhash.h>
4+
#include <linux/types.h>
5+
#include "config.h"
6+
#include "simplefs.h"
7+
8+
9+
#if defined(CONFIG_SIMPLEFS_HASH_HASH64)
10+
static uint64_t _hash64(const char *name, size_t len)
11+
{
12+
uint32_t val = crc32(0, name, len);
13+
return hash_64(
14+
val, ilog2(SIMPLEFS_MAX_EXTENTS * SIMPLEFS_MAX_BLOCKS_PER_EXTENT));
15+
}
16+
#elif defined(CONFIG_SIMPLEFS_HASH_FNV1A)
17+
static uint64_t _fnv1a_64(const char *str)
18+
{
19+
uint64_t h = 0xcbf29ce484222325ULL;
20+
while (*str) {
21+
h ^= (unsigned char) (*str++);
22+
h *= 0x100000001b3ULL;
23+
}
24+
return h;
25+
}
26+
#endif
27+
28+
uint32_t simplefs_hash(struct dentry *dentry)
29+
{
30+
#if defined(CONFIG_SIMPLEFS_HASH_HASH64)
31+
return _hash64(dentry->d_name.name, strlen(dentry->d_name.name));
32+
#elif defined(CONFIG_SIMPLEFS_HASH_FNV1A)
33+
return _fnv1a_64(dentry->d_name.name);
34+
#elif defined(CONFIG_SIMPLEFS_HASH_FULLNAME)
35+
return full_name_hash(dentry, dentry->d_name.name,
36+
strlen(dentry->d_name.name));
37+
#else
38+
#error "No hash method selected!"
39+
#endif
40+
}

0 commit comments

Comments
 (0)