Skip to content

Commit

Permalink
Merge pull request torvalds#164 from eldev/lkl_disk_remove
Browse files Browse the repository at this point in the history
lkl: Add lkl_disk_remove function
  • Loading branch information
Octavian Purdila committed Jun 7, 2016
2 parents 70fda2d + e05e51a commit 202e4f5
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions tools/lkl/cptofs.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static int searchdir(const char *src, const char *dst, const char *match)

int main(int argc, char **argv)
{
union lkl_disk disk;
struct lkl_disk disk;
long ret;
char mpoint[32], src_path[PATH_MAX], dst_path[PATH_MAX];
char *src_path_dir, *src_path_base;
Expand All @@ -426,7 +426,7 @@ int main(int argc, char **argv)
goto out;
}

ret = lkl_disk_add(disk);
ret = lkl_disk_add(&disk);
if (ret < 0) {
fprintf(stderr, "can't add disk: %s\n", lkl_strerror(ret));
goto out_close;
Expand Down
4 changes: 2 additions & 2 deletions tools/lkl/fs2tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ static int searchdir(const char *fsimg_path, const char *path)

int main(int argc, char **argv)
{
union lkl_disk disk;
struct lkl_disk disk;
long ret;
char mpoint[32];
unsigned int disk_id;
Expand All @@ -353,7 +353,7 @@ int main(int argc, char **argv)
goto out;
}

ret = lkl_disk_add(disk);
ret = lkl_disk_add(&disk);
if (ret < 0) {
fprintf(stderr, "can't add disk: %s\n", lkl_strerror(ret));
goto out_close;
Expand Down
22 changes: 18 additions & 4 deletions tools/lkl/include/lkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ void lkl_perror(char *msg, int err);
/**
* lkl_disk - host disk handle
*
* @dev - a pointer to 'virtio_blk_dev' structure for this disk
* @fd - a POSIX file descriptor that can be used by preadv/pwritev
* @handle - an NT file handle that can be used by ReadFile/WriteFile
*/
union lkl_disk {
int fd;
void *handle;
struct lkl_disk {
void *dev;
union {
int fd;
void *handle;
};
};

/**
Expand All @@ -88,7 +92,17 @@ union lkl_disk {
* @disk - the host disk handle
* @returns a disk id (0 is valid) or a strictly negative value in case of error
*/
int lkl_disk_add(union lkl_disk disk);
int lkl_disk_add(struct lkl_disk *disk);

/**
* lkl_disk_remove - remove a disk
*
* This function makes a cleanup of the @disk's virtio_dev structure
* that was initialized by lkl_disk_add before.
*
* @disk - the host disk handle
*/
void lkl_disk_remove(struct lkl_disk disk);

/**
* lkl_mount_dev - mount a disk
Expand Down
4 changes: 2 additions & 2 deletions tools/lkl/include/lkl_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ struct lkl_blk_req {
};

struct lkl_dev_blk_ops {
int (*get_capacity)(union lkl_disk disk, unsigned long long *res);
int (*get_capacity)(struct lkl_disk disk, unsigned long long *res);
#define LKL_DEV_BLK_STATUS_OK 0
#define LKL_DEV_BLK_STATUS_IOERR 1
#define LKL_DEV_BLK_STATUS_UNSUP 2
int (*request)(union lkl_disk disk, struct lkl_blk_req *req);
int (*request)(struct lkl_disk disk, struct lkl_blk_req *req);
};

struct lkl_netdev {
Expand Down
4 changes: 2 additions & 2 deletions tools/lkl/lib/nt-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ struct lkl_host_operations lkl_host_ops = {
.gettid = gettid,
};

int handle_get_capacity(union lkl_disk disk, unsigned long long *res)
int handle_get_capacity(struct lkl_disk disk, unsigned long long *res)
{
LARGE_INTEGER tmp;

Expand All @@ -248,7 +248,7 @@ int handle_get_capacity(union lkl_disk disk, unsigned long long *res)
return 0;
}

static int blk_request(union lkl_disk disk, struct lkl_blk_req *req)
static int blk_request(struct lkl_disk disk, struct lkl_blk_req *req)
{
unsigned long long offset = req->sector * 512;
OVERLAPPED ov = { 0, };
Expand Down
6 changes: 3 additions & 3 deletions tools/lkl/lib/posix-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ struct lkl_host_operations lkl_host_ops = {
.gettid = _gettid,
};

static int fd_get_capacity(union lkl_disk disk, unsigned long long *res)
static int fd_get_capacity(struct lkl_disk disk, unsigned long long *res)
{
off_t off;

Expand All @@ -338,7 +338,7 @@ static int fd_get_capacity(union lkl_disk disk, unsigned long long *res)
return 0;
}

static int do_rw(ssize_t (*fn)(), union lkl_disk disk, struct lkl_blk_req *req)
static int do_rw(ssize_t (*fn)(), struct lkl_disk disk, struct lkl_blk_req *req)
{
off_t off = req->sector * 512;
void *addr;
Expand Down Expand Up @@ -370,7 +370,7 @@ static int do_rw(ssize_t (*fn)(), union lkl_disk disk, struct lkl_blk_req *req)
return ret;
}

static int blk_request(union lkl_disk disk, struct lkl_blk_req *req)
static int blk_request(struct lkl_disk disk, struct lkl_blk_req *req)
{
int err = 0;

Expand Down
23 changes: 19 additions & 4 deletions tools/lkl/lib/virtio_blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct virtio_blk_dev {
struct virtio_dev dev;
struct lkl_virtio_blk_config config;
struct lkl_dev_blk_ops *ops;
union lkl_disk disk;
struct lkl_disk disk;
};

struct virtio_blk_req_trailer {
Expand Down Expand Up @@ -67,7 +67,8 @@ static struct virtio_dev_ops blk_ops = {
.enqueue = blk_enqueue,
};

int lkl_disk_add(union lkl_disk disk)

int lkl_disk_add(struct lkl_disk *disk)
{
struct virtio_blk_dev *dev;
unsigned long long capacity;
Expand All @@ -78,6 +79,8 @@ int lkl_disk_add(union lkl_disk disk)
if (!dev)
return -LKL_ENOMEM;

disk->dev = dev;

dev->dev.device_id = LKL_VIRTIO_ID_BLOCK;
dev->dev.vendor_id = 0;
dev->dev.device_features = 0;
Expand All @@ -86,9 +89,9 @@ int lkl_disk_add(union lkl_disk disk)
dev->dev.config_len = sizeof(dev->config);
dev->dev.ops = &blk_ops;
dev->ops = &lkl_dev_blk_ops;
dev->disk = disk;
dev->disk = *disk;

ret = dev->ops->get_capacity(disk, &capacity);
ret = dev->ops->get_capacity(*disk, &capacity);
if (ret) {
ret = -LKL_ENOMEM;
goto out_free;
Expand All @@ -106,3 +109,15 @@ int lkl_disk_add(union lkl_disk disk)

return ret;
}

void lkl_disk_remove(struct lkl_disk disk)
{
struct virtio_blk_dev *dev;

dev = (struct virtio_blk_dev *)disk.dev;
if (!dev)
return;

virtio_dev_cleanup(&dev->dev);
lkl_host_ops.mem_free(dev);
}
4 changes: 2 additions & 2 deletions tools/lkl/lklfuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct lklfuse {
const char *file;
const char *log;
const char *type;
union lkl_disk disk;
struct lkl_disk disk;
int disk_id;
int ro;
int mb;
Expand Down Expand Up @@ -585,7 +585,7 @@ int main(int argc, char **argv)

lklfuse.disk.fd = ret;

ret = lkl_disk_add(lklfuse.disk);
ret = lkl_disk_add(&lklfuse.disk);
if (ret < 0) {
fprintf(stderr, "can't add disk: %s\n", lkl_strerror(ret));
goto out_close_disk;
Expand Down
5 changes: 3 additions & 2 deletions tools/lkl/tests/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ int test_stat(char *str, int len)
return TEST_FAILURE;
}

static union lkl_disk disk;
static struct lkl_disk disk;
static int disk_id = -1;

int test_disk_add(char *str, int len)
Expand All @@ -291,7 +291,7 @@ int test_disk_add(char *str, int len)
#endif
goto out_unlink;

disk_id = lkl_disk_add(disk);
disk_id = lkl_disk_add(&disk);
if (disk_id < 0)
goto out_close;

Expand Down Expand Up @@ -821,6 +821,7 @@ int main(int argc, char **argv)

lkl_sys_halt();

lkl_disk_remove(disk);
close(disk.fd);

return g_test_pass;
Expand Down

0 comments on commit 202e4f5

Please sign in to comment.