Files
zvfs/zvfs.h

96 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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)
extern const char *json_file;
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_bs_dev *bs_dev;
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_s *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 actual_io_count;
const uint8_t *write_staging_buf;
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 open(const char *path, int flags, ...);
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
int close(int fd);
int unlink(const char *name);
#endif