zvfs: 重构核心读写逻辑并新增 POSIX hook;引入目录元数据与伪 fd;分离创建、打开、关闭、删除的逻辑;

This commit is contained in:
2026-02-22 14:46:17 +00:00
parent f216f73dda
commit 31dc307d0b
17 changed files with 1931 additions and 1727 deletions

91
zvfs.h Normal file
View File

@@ -0,0 +1,91 @@
#ifndef __ZVFS_HOOK_H__
#define __ZVFS_HOOK_H__
#include <stdio.h>
#include <spdk/event.h>
#include <spdk/blob.h>
#include <spdk/bdev.h>
#include <spdk/blob_bdev.h>
#include <spdk/env.h>
#define ZVFS_MAX_FILES 1024
#define ZVFS_MAX_FD 64
#define BUFFER_SIZE (1024*8)
static const char *json_file = "/home/lian/share/10.1-spdk/zvfs/zvfs.json";
extern struct spdk_thread *global_thread;
static const int WAITER_MAX_TIME = 100000;
/* 目录项(内存中的目录) */
typedef struct {
char filename[256];
spdk_blob_id blob_id;
uint64_t file_size; // 文件逻辑大小(字节)
uint64_t allocated_clusters; // 已分配的cluster数量
bool is_valid; // false 表示已删除
int32_t open_count; // 打开的文件句柄数量
} zvfs_dirent_t;
/* 文件系统全局结构 */
typedef struct zvfs_s {
struct spdk_blob_store *bs;
struct spdk_io_channel *channel;
struct spdk_blob *super_blob; // 承载目录日志的blob
uint64_t io_unit_size; // page大小单位字节
/* 目录 */
zvfs_dirent_t *dirents[ZVFS_MAX_FILES]; // 目录项数组 #define ZVFS_MAX_FILES 1024
uint32_t dirent_count; // 当前有效项数
/* 伪FD表 */
struct zvfs_file *fd_table[ZVFS_MAX_FD]; // // e.g., #define ZVFS_MAX_FD 64
int fd_base; // 伪FD起始值如1000
int openfd_count;
/* 元数据 */
uint32_t magic; // 0x5A563146 (ZV1F)
uint32_t version; // 1
bool finished;
} zvfs_t;
/* 打开的文件句柄 */
typedef struct zvfs_file_s {
zvfs_t *fs;
spdk_blob_id blob_id;
struct spdk_blob *blob;
zvfs_dirent_t *dirent; // 指回目录项 file_size/allocated_clusters
uint64_t current_offset; // 当前读写位置
int flags; // O_RDONLY / O_RDWR / O_CREAT 等
int pseudo_fd;
/* 临时DMA缓冲区可选每个file一个避免每次malloc */
void *dma_buf;
uint64_t dma_buf_size;
size_t io_count;
bool finished;
} zvfs_file_t;
bool waiter(struct spdk_thread *thread, spdk_msg_fn start_fn, void *ctx, bool *finished);
int zvfs_env_setup(void);
int zvfs_mount(struct zvfs_s *fs);
int zvfs_umount(struct zvfs_s *fs);
int zvfs_create(struct zvfs_file_s *file);
int zvfs_open(struct zvfs_file_s *file);
int zvfs_read(struct zvfs_file_s *file, uint8_t *buffer, size_t count);
int zvfs_write(struct zvfs_file_s *file, const uint8_t *buffer, size_t count);
int zvfs_close(struct zvfs_file_s *file);
int zvfs_delete(struct zvfs_file_s *file);
/* POSIX hook APIzvfs_hook.c 实现) */
int zvfs_open_hook(const char *path, int flags, ...);
ssize_t zvfs_read_hook(int fd, void *buf, size_t count);
ssize_t zvfs_write_hook(int fd, const void *buf, size_t count);
int zvfs_close_hook(int fd);
#endif