Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
Coldwings committed Jan 30, 2023
1 parent 3a54cfc commit ff12687
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 565 deletions.
6 changes: 2 additions & 4 deletions Makefile
@@ -1,19 +1,17 @@
CONFIG_ZFILE_CLEANUP_CACHE=y
CONFIG_ZFILE_READAHEAD=y
CONFIG_OVBD_DEBUG=n
CONFIG_ZFILE_HEAD_OVERWRITE=n

MYPROC=vbd
obj-m += vbd.o

ccflags-y := -std=gnu11 -Wno-declaration-after-statement -O3
ccflags-y := -std=gnu11 -Wno-declaration-after-statement -O2

ccflags-$(CONFIG_OVBD_DEBUG) += -DOVBD_DEBUG
ccflags-$(CONFIG_ZFILE_CLEANUP_CACHE) += -DZFILE_CLEANUP_CACHE
ccflags-$(CONFIG_ZFILE_READAHEAD) += -DZFILE_READAHEAD
ccflags-$(CONFIG_ZFILE_HEAD_OVERWRITE) += -DZFILE_HEAD_OVERWRITE

vbd-y := dm-ovbd.o dm-lsmt.o dm-zfile.o lsmt.o zfile.o vfsfile.o blkfile.o
vbd-y := dm-ovbd.o dm-lsmt.o dm-zfile.o lsmt.o zfile.o blkfile.o


export KROOT=/lib/modules/$(shell uname -r)/build
Expand Down
30 changes: 21 additions & 9 deletions blkfile.c
@@ -1,7 +1,18 @@
#include "blkfile.h"
#include "log-format.h"
#include "dm-ovbd.h"
#include <linux/dm-bufio.h>

struct blkdev_as_vfile {
vfile_operations *ops;
struct block_device *blkdev;
loff_t len;
struct dm_bufio_client *c;
};

static struct block_device *blkdev_getblkdev(struct vfile *f)
{
return ((struct blkdev_as_vfile *)f)->blkdev;
}

// special helper
// access blockdev data by sync
// copy to buffer
Expand Down Expand Up @@ -50,7 +61,7 @@ static ssize_t sync_read_blkdev(struct blkdev_as_vfile *f, void *buf,
static size_t blkdev_len(struct vfile *ctx)
{
struct blkdev_as_vfile *bf = (struct blkdev_as_vfile *)ctx;
PRINT_INFO("blkdev_len %lld\n", bf->len);
pr_info("blkdev_len %lld\n", bf->len);
return bf->len;
}

Expand Down Expand Up @@ -88,15 +99,15 @@ static void blkdev_close(struct vfile *ctx)
return;
}

static struct vfile_op blkdev_op = {
static vfile_operations blkdev_op = {
.blkdev = blkdev_getblkdev,
.len = blkdev_len,
.pread = blkdev_pread,
.pread_async = NULL,
.bio_remap = NULL,
.close = blkdev_close,
};

IFile *open_blkdev_as_vfile(struct block_device *blk, loff_t len)
vfile *open_blkdev_as_vfile(struct block_device *blk, loff_t len)
{
if (IS_ERR(blk)) {
return NULL;
Expand All @@ -105,7 +116,8 @@ IFile *open_blkdev_as_vfile(struct block_device *blk, loff_t len)
kzalloc(sizeof(struct blkdev_as_vfile), GFP_KERNEL);
if (!ret)
return NULL;
ret->vfile.op = &blkdev_op;
ret->ops = &blkdev_op;
ret->blkdev = blk;
ret->c = dm_bufio_client_create(blk, 4096, 2, 0, NULL, NULL);
if (IS_ERR(ret->c)) {
goto errout;
Expand All @@ -114,8 +126,8 @@ IFile *open_blkdev_as_vfile(struct block_device *blk, loff_t len)
if (len == -1)
len = get_capacity(blk->bd_disk) << SECTOR_SHIFT;
ret->len = len;
// PRINT_INFO("open as vfile dev %p\n", ret->dev);
return (IFile *)ret;
// pr_info("open as vfile dev %p\n", ret->dev);
return (vfile *)ret;
errout:
kfree(ret);
return NULL;
Expand Down
15 changes: 0 additions & 15 deletions blkfile.h

This file was deleted.

57 changes: 23 additions & 34 deletions dm-lsmt.c
@@ -1,20 +1,9 @@
#include <linux/bio.h>
#include <linux/device-mapper.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/printk.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#include "lsmt.h"
#include "blkfile.h"
#include "log-format.h"
#include "dm-ovbd.h"

struct lsmt_dm_target {
struct dm_dev *dev[256];
IFile *lsmt;
IFile *bf[256];
vfile *lsmt;
vfile *bf[256];
unsigned int nr;
};

Expand All @@ -23,16 +12,16 @@ static int lsmt_target_map(struct dm_target *ti, struct bio *bio)
struct lsmt_dm_target *mdt = (struct lsmt_dm_target *)ti->private;

if (!mdt) {
PRINT_ERROR("LSMT DM Target not ready!!\n");
pr_err("LSMT DM Target not ready!!\n");
return DM_MAPIO_REQUEUE;
}

switch (bio_op(bio)) {
case REQ_OP_READ:
return mdt->lsmt->op->bio_remap((struct vfile *)mdt->lsmt, bio,
mdt->dev, mdt->nr);
return mdt->lsmt->ops->bio_remap((struct vfile *)mdt->lsmt, bio,
mdt->dev, mdt->nr);
}
PRINT_ERROR("DM_MAPIO_KILL %s:%d op=%d sts=%d\n", __FILE__, __LINE__,
pr_err("DM_MAPIO_KILL %s:%d op=%d sts=%d\n", __FILE__, __LINE__,
bio_op(bio), bio->bi_status);
return DM_MAPIO_KILL;
}
Expand All @@ -41,7 +30,7 @@ static int lsmt_target_end_io(struct dm_target *ti, struct bio *bio,
blk_status_t *error)
{
if (bio->bi_status != BLK_STS_OK) {
PRINT_ERROR("DONE NOT OK %s:%d op=%d sts=%d\n", __FILE__, __LINE__,
pr_err("DONE NOT OK %s:%d op=%d sts=%d\n", __FILE__, __LINE__,
bio_op(bio), bio->bi_status);
return DM_ENDIO_REQUEUE;
}
Expand All @@ -58,18 +47,18 @@ static int lsmt_target_ctr(struct dm_target *ti, unsigned int argc, char **argv)
int ret;
int i;

PRINT_INFO("\n >>in function lsmt_target_ctr \n");
pr_info("\n >>in function lsmt_target_ctr \n");

if (argc < 2) {
PRINT_INFO("\n Invalid no.of arguments.\n");
pr_info("\n Invalid no.of arguments.\n");
ti->error = "Invalid argument count";
return -EINVAL;
}

mdt = kmalloc(sizeof(struct lsmt_dm_target), GFP_KERNEL);

if (mdt == NULL) {
PRINT_INFO("\n Mdt is null\n");
pr_info("\n Mdt is null\n");
ti->error = "dm-lsmt_target: Cannot allocate context";
return -ENOMEM;
}
Expand All @@ -82,7 +71,7 @@ static int lsmt_target_ctr(struct dm_target *ti, unsigned int argc, char **argv)
pr_warn("Invalid parameter");
goto error_out;
}
PRINT_INFO("\nlsmt-md: load dev %s\n", devname);
pr_info("\nlsmt-md: load dev %s\n", devname);
if (dm_get_device(ti, devname, dm_table_get_mode(ti->table),
&mdt->dev[i])) {
ti->error = "dm-lsmt_target: Device lookup failed";
Expand All @@ -94,8 +83,8 @@ static int lsmt_target_ctr(struct dm_target *ti, unsigned int argc, char **argv)
goto error_out;
}
mdt->bf[i] = open_blkdev_as_vfile(mdt->dev[i]->bdev, len);
PRINT_INFO("lsmt: file %d size %lu", i,
mdt->bf[i]->op->len(mdt->bf[i]));
pr_info("lsmt: file %d size %lu", i,
mdt->bf[i]->ops->len(mdt->bf[i]));
}
mdt->nr = i;

Expand All @@ -106,18 +95,18 @@ static int lsmt_target_ctr(struct dm_target *ti, unsigned int argc, char **argv)
goto error_out;
}

PRINT_INFO("dm-lsmt: blk size is %lu\n",
mdt->lsmt->op->len((struct vfile *)mdt->lsmt));
pr_info("dm-lsmt: blk size is %lu\n",
mdt->lsmt->ops->len((struct vfile *)mdt->lsmt));

ti->private = mdt;

PRINT_INFO("\n>>out function lsmt_target_ctr \n");
pr_info("\n>>out function lsmt_target_ctr \n");
return 0;

error_out:
for (i = 0; i < mdt->nr; i++) {
if (mdt->bf[i])
mdt->bf[i]->op->close((struct vfile *)mdt->bf[i]);
mdt->bf[i]->ops->close((struct vfile *)mdt->bf[i]);
}

for (i = 0; i < mdt->nr; i++) {
Expand All @@ -126,22 +115,22 @@ static int lsmt_target_ctr(struct dm_target *ti, unsigned int argc, char **argv)
}
bad:
kfree(mdt);
PRINT_INFO("\n>>out function lsmt_target_ctr with error \n");
pr_info("\n>>out function lsmt_target_ctr with error \n");
return -EINVAL;
}

static void lsmt_target_dtr(struct dm_target *ti)
{
struct lsmt_dm_target *mdt = (struct lsmt_dm_target *)ti->private;
unsigned int i = 0;
PRINT_INFO("\n<<in function lsmt_target_dtr \n");
pr_info("\n<<in function lsmt_target_dtr \n");
if (mdt->lsmt)
mdt->lsmt->op->close((struct vfile *)mdt->lsmt);
mdt->lsmt->ops->close((struct vfile *)mdt->lsmt);
for (i = 0; i < mdt->nr; i++) {
dm_put_device(ti, mdt->dev[i]);
}
kfree(mdt);
PRINT_INFO("\n>>out function lsmt_target_dtr \n");
pr_info("\n>>out function lsmt_target_dtr \n");
}

static struct target_type lsmt_target = {
Expand All @@ -160,7 +149,7 @@ int init_lsmt_target(void)
int result;
result = dm_register_target(&lsmt_target);
if (result < 0)
PRINT_INFO("\n Error in registering target \n");
pr_info("\n Error in registering target \n");
return 0;
}

Expand Down
7 changes: 0 additions & 7 deletions dm-lsmt.h

This file was deleted.

6 changes: 4 additions & 2 deletions dm-ovbd.c
@@ -1,3 +1,5 @@
#include <linux/version.h>
#include <linux/module.h>
#include "dm-ovbd.h"

static ovbd_context global_ovbd_context;
Expand All @@ -13,7 +15,7 @@ int init_ovbd_target(void)
goto error_out;
if (init_zfile_target() < 0)
goto error_out;
PRINT_INFO("OVBD initialized");
pr_info("OVBD initialized");
return 0;
error_out:
destroy_workqueue(global_ovbd_context.wq);
Expand All @@ -27,7 +29,7 @@ void cleanup_ovbd_target(void)
flush_workqueue(global_ovbd_context.wq);
destroy_workqueue(global_ovbd_context.wq);
global_ovbd_context.wq = NULL;
PRINT_INFO("OVBD cleared");
pr_info("OVBD cleared");
}

ovbd_context* get_ovbd_context() {
Expand Down
37 changes: 32 additions & 5 deletions dm-ovbd.h
@@ -1,15 +1,42 @@
#ifndef __DM_OVBD_HEADER__
#define __DM_OVBD_HEADER__

#include "log-format.h"
#include "dm-lsmt.h"
#include "dm-zfile.h"
#include <linux/module.h>
#include <linux/device-mapper.h>
#include <linux/bio.h>

typedef struct ovbd_context {
struct workqueue_struct *wq;
} ovbd_context;

ovbd_context* get_ovbd_context(void);
ovbd_context *get_ovbd_context(void);

int init_lsmt_target(void);

void cleanup_lsmt_target(void);

int init_zfile_target(void);

void cleanup_zfile_target(void);

struct vfile;

typedef struct vfile_operations {
struct block_device *(*blkdev)(struct vfile *);
size_t (*len)(struct vfile *);
ssize_t (*pread)(struct vfile *, void *, size_t, loff_t);
int (*bio_remap)(struct vfile *, struct bio *, struct dm_dev **,
unsigned int);
void (*close)(struct vfile *);
} vfile_operations;

typedef struct vfile {
vfile_operations *ops;
} vfile;

vfile *open_blkdev_as_vfile(struct block_device *blk, loff_t len);

vfile *zfile_open(struct vfile *file);

vfile *lsmt_open_files(vfile *zf[], int n);

#endif

0 comments on commit ff12687

Please sign in to comment.