forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bcache: Use bcache without formatting existing device
v2: Fixed small typo Introducing a bcache control device (/dev/bcache_ctrl) that allows communicating to the driver from user space via IOCTL. The only IOCTL commands currently implemented, receives a struct cache_sb and uses it to register the backing device. Signed-off-by: Andrea Tomassetti <andrea.tomassetti@devo.com>
- Loading branch information
1 parent
3bf7edc
commit b5073e4
Showing
7 changed files
with
200 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include <linux/cdev.h> | ||
#include <linux/fs.h> | ||
#include <linux/vmalloc.h> | ||
|
||
#include "control.h" | ||
|
||
struct bch_ctrl_device { | ||
struct cdev cdev; | ||
struct class *class; | ||
dev_t dev; | ||
}; | ||
|
||
static struct bch_ctrl_device _control_device; | ||
|
||
/* this handles IOCTL for /dev/bcache_ctrl */ | ||
/*********************************************/ | ||
long bch_service_ioctl_ctrl(struct file *filp, unsigned int cmd, | ||
unsigned long arg) | ||
{ | ||
int retval = 0; | ||
|
||
if (_IOC_TYPE(cmd) != BCH_IOCTL_MAGIC) | ||
return -EINVAL; | ||
|
||
if (!capable(CAP_SYS_ADMIN)) { | ||
/* Must be root to issue ioctls */ | ||
return -EPERM; | ||
} | ||
|
||
switch (cmd) { | ||
case BCH_IOCTL_REGISTER_DEVICE: { | ||
struct bch_register_device *cmd_info; | ||
|
||
cmd_info = vmalloc(sizeof(struct bch_register_device)); | ||
if (!cmd_info) | ||
return -ENOMEM; | ||
|
||
if (copy_from_user(cmd_info, (void __user *)arg, | ||
sizeof(struct bch_register_device))) { | ||
pr_err("Cannot copy cmd info from user space\n"); | ||
vfree(cmd_info); | ||
return -EINVAL; | ||
} | ||
|
||
retval = register_bcache_ioctl(cmd_info); | ||
|
||
vfree(cmd_info); | ||
return retval; | ||
} | ||
|
||
default: | ||
return -EINVAL; | ||
} | ||
} | ||
|
||
static const struct file_operations _ctrl_dev_fops = { | ||
.owner = THIS_MODULE, | ||
.unlocked_ioctl = bch_service_ioctl_ctrl | ||
}; | ||
|
||
int __init bch_ctrl_device_init(void) | ||
{ | ||
struct bch_ctrl_device *ctrl = &_control_device; | ||
struct device *device; | ||
int result = 0; | ||
|
||
result = alloc_chrdev_region(&ctrl->dev, 0, 1, "bcache"); | ||
if (result) { | ||
pr_err("Cannot allocate control chrdev number.\n"); | ||
goto error_alloc_chrdev_region; | ||
} | ||
|
||
cdev_init(&ctrl->cdev, &_ctrl_dev_fops); | ||
|
||
result = cdev_add(&ctrl->cdev, ctrl->dev, 1); | ||
if (result) { | ||
pr_err("Cannot add control chrdev.\n"); | ||
goto error_cdev_add; | ||
} | ||
|
||
ctrl->class = class_create(THIS_MODULE, "bcache"); | ||
if (IS_ERR(ctrl->class)) { | ||
pr_err("Cannot create control chrdev class.\n"); | ||
result = PTR_ERR(ctrl->class); | ||
goto error_class_create; | ||
} | ||
|
||
device = device_create(ctrl->class, NULL, ctrl->dev, NULL, | ||
"bcache_ctrl"); | ||
if (IS_ERR(device)) { | ||
pr_err("Cannot create control chrdev.\n"); | ||
result = PTR_ERR(device); | ||
goto error_device_create; | ||
} | ||
|
||
return result; | ||
|
||
error_device_create: | ||
class_destroy(ctrl->class); | ||
error_class_create: | ||
cdev_del(&ctrl->cdev); | ||
error_cdev_add: | ||
unregister_chrdev_region(ctrl->dev, 1); | ||
error_alloc_chrdev_region: | ||
return result; | ||
} | ||
|
||
void bch_ctrl_device_deinit(void) | ||
{ | ||
struct bch_ctrl_device *ctrl = &_control_device; | ||
|
||
device_destroy(ctrl->class, ctrl->dev); | ||
class_destroy(ctrl->class); | ||
cdev_del(&ctrl->cdev); | ||
unregister_chrdev_region(ctrl->dev, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
#ifndef __BCACHE_CONTROL_H__ | ||
#define __BCACHE_CONTROL_H__ | ||
|
||
#include "ioctl_codes.h" | ||
|
||
int __init bch_ctrl_device_init(void); | ||
void bch_ctrl_device_deinit(void); | ||
|
||
ssize_t register_bcache_ioctl(struct bch_register_device *brd); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
#ifndef __BCACHE_IOCTL_CODES_H__ | ||
#define __BCACHE_IOCTL_CODES_H__ | ||
|
||
#include <linux/ioctl.h> | ||
#include <linux/types.h> | ||
|
||
struct bch_register_device { | ||
const char *dev_name; | ||
size_t size; | ||
struct cache_sb *sb; | ||
}; | ||
|
||
#define BCH_IOCTL_MAGIC (0xBC) | ||
|
||
/** Start new cache instance, load cache or recover cache */ | ||
#define BCH_IOCTL_REGISTER_DEVICE _IOWR(BCH_IOCTL_MAGIC, 1, struct bch_register_device) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters