Files
zvfs/zv2404fs.c
2024-08-08 13:56:24 +00:00

279 lines
5.6 KiB
C
Executable File

#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 BUFFER_SIZE 512
#if 0
struct spdk_blob_store *blobstore;
spdk_blob_id blob_id;
struct spdk_blob *blob;
uint64_t free_cluster_count;
uint64_t num_cluster;
uint8_t *write_buffer;
uint8_t *read_buffer;
struct spdk_io_channel *channel;
#else
//
typedef struct zv2404fs_context_s {
struct spdk_blob_store *blobstore;
spdk_blob_id blob_id;
struct spdk_blob *blob;
uint64_t unit_size;
uint64_t free_cluster_count;
uint64_t num_cluster;
uint8_t *write_buffer;
uint8_t *read_buffer;
struct spdk_io_channel *channel;
} zv2404fs_context_t;
#endif
static void zv2404fs_spdk_bs_unload_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
spdk_app_stop(0);
spdk_free(ctx->write_buffer);
spdk_free(ctx->read_buffer);
free(ctx);
}
static void zv2404fs_spdk_blob_delete_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
if (ctx->blobstore) {
if (ctx->channel) {
spdk_bs_free_io_channel(ctx->channel);
}
spdk_bs_unload(ctx->blobstore, zv2404fs_spdk_bs_unload_cb, ctx);
}
}
static void zv2404fs_spdk_blob_close_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
spdk_bs_delete_blob(ctx->blobstore, ctx->blob_id, zv2404fs_spdk_blob_delete_cb, ctx);
}
static void zv2404fs_spdk_blob_read_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
SPDK_NOTICELOG("READ BUFFER : %s\n", ctx->read_buffer);
spdk_blob_close(ctx->blob, zv2404fs_spdk_blob_close_cb, ctx);
}
static void zv2404fs_spdk_blob_write_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
SPDK_NOTICELOG("WRITE COMPLETE\n");
uint8_t *rbuffer = spdk_malloc(512, 0x1000, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
if (rbuffer == NULL) {
spdk_app_stop(0);
return ;
}
memset(rbuffer, 0x0, BUFFER_SIZE);
ctx->read_buffer = rbuffer;
spdk_blob_io_read(ctx->blob, ctx->channel, rbuffer, 0, 1, zv2404fs_spdk_blob_read_cb, ctx);
}
static void zv2404fs_spdk_blob_sync_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
uint8_t *wbuffer = spdk_malloc(512, 0x1000, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
if (wbuffer == NULL) {
spdk_app_stop(0);
return ;
}
memset(wbuffer, 'A', BUFFER_SIZE);
*(wbuffer+BUFFER_SIZE-1) = '\0';
ctx->write_buffer = wbuffer;
struct spdk_io_channel *chan = spdk_bs_alloc_io_channel(ctx->blobstore);
if (chan == NULL) {
spdk_app_stop(0);
return ;
}
ctx->channel = chan;
spdk_blob_io_write(ctx->blob, ctx->channel, wbuffer, 0, 1, zv2404fs_spdk_blob_write_cb, ctx);
}
static void zv2404fs_spdk_blob_resize_cb(void *arg, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
uint64_t total = spdk_blob_get_num_clusters(ctx->blob);
ctx->num_cluster = total;
SPDK_NOTICELOG("resize blob :%"PRIu64"\n", total);
spdk_blob_sync_md(ctx->blob, zv2404fs_spdk_blob_sync_cb, ctx);
}
static void zv2404fs_spdk_bs_open_blob_cb(void *arg, struct spdk_blob *blb, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
ctx->blob = blb;
uint64_t free_cluster = spdk_bs_free_cluster_count(ctx->blobstore); //
SPDK_NOTICELOG("free_cluster : %"PRIu64"\n", free_cluster);
ctx->free_cluster_count = free_cluster;
spdk_blob_resize(blb, free_cluster, zv2404fs_spdk_blob_resize_cb, ctx);
}
static void zv2404fs_spdk_bs_create_blob_cb(void *arg, spdk_blob_id blobid, int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
ctx->blob_id = blobid;
SPDK_NOTICELOG("blobid : %"PRIu64"\n", blobid);
spdk_bs_open_blob(ctx->blobstore, blobid, zv2404fs_spdk_bs_open_blob_cb, ctx);
}
static void zv2404fs_spdk_bs_init_cb(void *arg, struct spdk_blob_store *bs,
int bserrno) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
uint64_t io_unit_size = spdk_bs_get_io_unit_size(bs);
SPDK_NOTICELOG("io_unit_size : %"PRIu64"\n", io_unit_size);
ctx->unit_size = io_unit_size;
ctx ->blobstore = bs;
spdk_bs_create_blob(bs, zv2404fs_spdk_bs_create_blob_cb, ctx);
}
static void zv2404_spdk_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
void *event_ctx) {
SPDK_NOTICELOG("zv2404_spdk_bdev_event_cb\n");
}
static void zv2404fs_entry(void *arg) {
zv2404fs_context_t *ctx = (zv2404fs_context_t*)arg;
struct spdk_bs_dev *bs_dev = NULL;
int rc = spdk_bdev_create_bs_dev_ext("Malloc0", zv2404_spdk_bdev_event_cb, NULL, &bs_dev);
if (rc != 0) {
spdk_app_stop(0);
}
//int *count = malloc(sizeof(int));
//*count = 2404;
spdk_bs_init(bs_dev, NULL, zv2404fs_spdk_bs_init_cb, ctx);
//sleep(10);
SPDK_NOTICELOG("zv2404fs_entry\n");
}
// open
// read
// write
// close
#if 1
int main(int argc, char *argv[]) {
if (argc != 2) return -1;
struct spdk_app_opts opts = {};
spdk_app_opts_init(&opts, sizeof(opts));
opts.name = "zv2404fs";
opts.json_config_file = argv[1];
zv2404fs_context_t *ctx = calloc(1, sizeof(zv2404fs_context_t));
spdk_app_start(&opts, zv2404fs_entry, ctx);
SPDK_NOTICELOG("hello spdk\n");
}
#else
// posix
int main() {
int fd = open("a.txt", O_CREAT | O_READ);
char *buffer = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
write(fd, buffer, strlen(buffer));
char rbuffer[128] = {0};
read(fd, buffer, 128);
close();
}
#endif