Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
obj-m += simplefs.o
simplefs-objs := fs.o super.o inode.o file.o dir.o extent.o
simplefs-objs := fs.o super.o inode.o file.o dir.o extent.o hash.o

KDIR ?= /lib/modules/$(shell uname -r)/build

Expand Down
8 changes: 5 additions & 3 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
for (; remained_nr_files && ei < SIMPLEFS_MAX_EXTENTS; ei++) {
if (eblock->extents[ei].ee_start == 0)
continue;

int ei_nr = eblock->extents[ei].nr_files;
/* Iterate over blocks in one extent */
for (bi = 0; bi < eblock->extents[ei].ee_len && remained_nr_files;
for (bi = 0;
bi < eblock->extents[ei].ee_len && remained_nr_files && ei_nr;
bi++) {
bh2 = sb_bread(sb, eblock->extents[ei].ee_start + bi);
if (!bh2) {
Expand All @@ -80,12 +81,13 @@ static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
continue;
}

for (fi = 0; fi < SIMPLEFS_FILES_PER_BLOCK;) {
for (fi = 0; fi < SIMPLEFS_FILES_PER_BLOCK && dblock->nr_files;) {
if (dblock->files[fi].inode != 0) {
if (offset) {
offset--;
} else {
remained_nr_files--;
ei_nr--;
if (!dir_emit(ctx, dblock->files[fi].filename,
SIMPLEFS_FILENAME_LEN,
dblock->files[fi].inode, DT_UNKNOWN)) {
Expand Down
15 changes: 15 additions & 0 deletions hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <linux/dcache.h>
#include <linux/types.h>
#include "simplefs.h"

uint32_t simplefs_hash(struct dentry *dentry)
{
const char *str = dentry->d_name.name;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unkeyed FNV-1a is vulnerable to HashDoS. FNV-1a is deterministic and trivially reversible. Attackers can craft filenames that all collide, forcing O(n) linear scans on every directory operation.

Consider using the kernel's full_name_hash() which is SipHash-based with a per-boot random key, or at minimum incorporate a per-superblock random salt.

Copy link
Copy Markdown
Collaborator Author

@RoyWFHuang RoyWFHuang Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our previous discussion

full_name_hash() is designed for VFS dentry caching, not for on-disk indexing. It uses a per-boot salt that changes on reboot. Consider: what happens to hash placement after reboot?

If full_name_hash() returns different values after reboot (it does), lookups will start from wrong positions. The linear probe fallback saves correctness, but destroys the performance benefit.

Use a deterministic hash like FNV-1a or djb2.

I believe we should stick with this approach to ensure consistency across reboots.

uint64_t h = 0xcbf29ce484222325ULL;
while (*str) {
h ^= (unsigned char) (*str++);
h *= 0x100000001b3ULL;
}
/* Fold high 32 bits into low 32 bits to mix the full 64-bit result */
return (uint32_t) (h ^ (h >> 32));
}
Loading
Loading