Initial commit

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga
2019-03-29 08:39:34 +01:00
commit 94e8ca09e0
140 changed files with 37144 additions and 0 deletions

12
modules/cas_disk/Makefile Normal file
View File

@@ -0,0 +1,12 @@
#
# Copyright(c) 2012-2019 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause-Clear
#
include $(M)/config.mk
obj-m := cas_disk.o
cas_disk-objs = main.o
cas_disk-objs += disk.o
cas_disk-objs += exp_obj.o
cas_disk-objs += sysfs.o

253
modules/cas_disk/cas_disk.h Normal file
View File

@@ -0,0 +1,253 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __CASDISK_H__
#define __CASDISK_H__
#include <linux/blkdev.h>
/**
* Version of cas_disk interface
*/
#define CASDSK_IFACE_VERSION 2
struct casdsk_disk;
#define CASDSK_BIO_NOT_HANDLED 0
#define CASDSK_BIO_HANDLED 1
struct casdsk_exp_obj_ops {
/**
* @brief Prepare request queue of exported object (top) block device.
* Could be NULL.
*/
int (*prepare_queue)(struct casdsk_disk *dsk, struct request_queue *q,
void *private);
/**
* @brief Cleanup request queue of exported object (top) block device.
* Could be NULL.
*/
void (*cleanup_queue)(struct casdsk_disk *dsk, struct request_queue *q,
void *private);
/**
* @brief Set geometry of exported object (top) block device.
* Could be NULL.
*/
int (*set_geometry)(struct casdsk_disk *dsk, void *private);
/**
* @brief make_request_fn of exported object (top) block device.
* Called by cas_disk when cas_disk device is in attached mode.
*
* @return casdsk_BIO_HANDLED when bio was handled.
* Otherwise casdsk_BIO_NOT_HANDLED. In this case bio will be submitted
* to I/O scheduler and should be handled by request_fn.
*/
int (*make_request_fn)(struct casdsk_disk *dsk, struct request_queue *q,
struct bio *bio, void *private);
/**
* @brief request_fn of exported object (top) block device.
* Called by cas_disk when cas_disk device is in attached mode.
*/
void (*request_fn)(struct casdsk_disk *dsk, struct request_queue *q,
void *private);
/**
* @brief prep_rq_fn of exported object (top) block device.
* Called by cas_disk when cas_disk device is in attached mode.
*/
int (*prep_rq_fn)(struct casdsk_disk *dsk, struct request_queue *q,
struct request *rq, void *private);
/**
* @brief ioctl handler of exported object (top) block device.
* Called by cas_disk when cas_disk device is in attached mode.
*/
int (*ioctl)(struct casdsk_disk *dsk, unsigned int cmd, unsigned long arg,
void *private);
};
/**
* Stored configuration buffer description
*/
struct casdsk_props_conf {
void *buffer;
size_t size;
uint16_t crc;
};
/**
* @brief Get version of cas_disk interface
* @return cas_disk interface version
*/
uint32_t casdsk_get_version(void);
/**
* @brief Store configuration buffers in cas_disk
* @param n_blobs Number of configuration buffers
* @param blobs Array of configuration buffers structures
*/
void casdsk_store_config(size_t n_blobs, struct casdsk_props_conf *blobs);
/**
* @brief Get previously stored configuration buffers
* @param blobs Where to store pointer to configuration buffers array
* @return Number of stored configuration buffers
*/
size_t casdsk_get_stored_config(struct casdsk_props_conf **blobs);
/**
* @brief Free resources related to stored configuration buffers
*/
void casdsk_free_stored_config(void);
/**
* @brief Open block device
* @param path Path to block device
* @param private Private data
* @return Pointer to casdsk_disk related to opened block device
*/
struct casdsk_disk *casdsk_disk_open(const char *path, void *private);
/**
* @brief Claim previously opened block device (holded by cas_disk)
* @param path Path to block device
* @param private Private data
* @return Pointer to casdsk_disk structure related to block device, or NULL
* if device is not opened by cas_disk.
*/
struct casdsk_disk *casdsk_disk_claim(const char *path, void *private);
/**
* @brief Close block device and remove from cas_disk
* @param dsk Pointer to casdsk_disk structure related to block device
* which should be closed.
*/
void casdsk_disk_close(struct casdsk_disk *dsk);
/**
* @brief Get block_device structure of bottom block device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return Pointer to block_device structure of bottom block device
*/
struct block_device *casdsk_disk_get_blkdev(struct casdsk_disk *dsk);
/**
* @brief Get request queue of bottom block device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return Pointer to reqest_queue structure of bottom block device
*/
struct request_queue *casdsk_disk_get_queue(struct casdsk_disk *dsk);
/**
* @brief Get gendisk structure of bottom block device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return Pointer to gendisk structure of bottom block device
*/
struct gendisk *casdsk_disk_get_gendisk(struct casdsk_disk *dsk);
/**
* @brief Prepare cas_disk device to switch to pass-through mode
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_disk_set_pt(struct casdsk_disk *dsk);
/**
* @brief Prepare cas_disk device to switch to attached mode
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_disk_set_attached(struct casdsk_disk *dsk);
/**
* @brief Revert cas_disk device back to attached mode
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_disk_clear_pt(struct casdsk_disk *dsk);
/**
* @brief Dettach cas from cas_disk device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_disk_dettach(struct casdsk_disk *dsk);
/**
* @brief Attach cas to cas_disk device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @param owner Pointer to cas module
* @param ops Pointer to structure with callback functions
* @return 0 if success, errno if failure
*/
int casdsk_disk_attach(struct casdsk_disk *dsk, struct module *owner,
struct casdsk_exp_obj_ops *ops);
/**
* @brief Create exported object (top device)
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @param dev_name Name of exported object (top device)
* @param owner Pointer to cas module
* @param ops Pointer to structure with callback functions
* @return 0 if success, errno if failure
*/
int casdsk_exp_obj_create(struct casdsk_disk *dsk, const char *dev_name,
struct module *owner, struct casdsk_exp_obj_ops *ops);
/**
* @brief Get request queue of exported object (top) block device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return Pointer to reqest_queue structure of top block device
*/
struct request_queue *casdsk_exp_obj_get_queue(struct casdsk_disk *dsk);
/**
* @brief Get gendisk structure of exported object (top) block device
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return Pointer to gendisk structure of top block device
*/
struct gendisk *casdsk_exp_obj_get_gendisk(struct casdsk_disk *dsk);
/**
* @brief Activate exported object (make it visible to OS
* and allow I/O handling)
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_exp_obj_activate(struct casdsk_disk *dsk);
/**
* @brief Check if exported object is active
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return true if exported object is active
*/
bool casdsk_exp_obj_activated(struct casdsk_disk *ds);
/**
* @brief Lock exported object
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_exp_obj_lock(struct casdsk_disk *dsk);
/**
* @brief Unlock exported object
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_exp_obj_unlock(struct casdsk_disk *dsk);
/**
* @brief Destroy exported object
* @param dsk Pointer to casdsk_disk structure related to cas_disk device
* @return 0 if success, errno if failure
*/
int casdsk_exp_obj_destroy(struct casdsk_disk *dsk);
#endif

View File

@@ -0,0 +1,93 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __CASDISK_DEFS_H__
#define __CASDISK_DEFS_H__
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <cas_version.h>
struct casdsk_stored_config {
size_t n_blobs;
struct casdsk_props_conf *blobs;
};
struct casdsk_module {
struct mutex lock;
struct list_head disk_list;
uint32_t next_disk_id;
int disk_major;
int next_minor;
struct kmem_cache *disk_cache;
struct kmem_cache *exp_obj_cache;
struct kmem_cache *pt_io_ctx_cache;
struct kmem_cache *pending_rqs_cache;
struct kobject kobj;
struct casdsk_stored_config config;
};
extern struct casdsk_module *casdsk_module;
/* prefixes for messages */
#define CASDSK_LOGO "CAS Disk"
#define CASDSK_PREFIX_SHORT "[" CASDSK_LOGO "] "
#define CASDSK_PREFIX_LONG "Cache Acceleration Software Linux"
#define CASDSK_KERN_EMERG KERN_EMERG""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_ALERT KERN_ALERT""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_CRIT KERN_CRIT""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_ERR KERN_ERR""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_WARNING KERN_WARNING""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_NOTICE KERN_NOTICE""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_INFO KERN_INFO""CASDSK_PREFIX_SHORT
#define CASDSK_KERN_DEBUG KERN_DEBUG""CASDSK_PREFIX_SHORT
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 37)
static inline struct block_device *open_bdev_exclusive(const char *path,
fmode_t mode,
void *holder)
{
return blkdev_get_by_path(path, mode | FMODE_EXCL, holder);
}
static inline void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
{
blkdev_put(bdev, mode | FMODE_EXCL);
}
static inline int bd_claim_by_disk(struct block_device *bdev, void *holder,
struct gendisk *disk)
{
return bd_link_disk_holder(bdev, disk);
}
static inline void bd_release_from_disk(struct block_device *bdev,
struct gendisk *disk)
{
return bd_unlink_disk_holder(bdev, disk);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
#define KRETURN(x) ({ return (x); })
#define MAKE_RQ_RET_TYPE blk_qc_t
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
#define KRETURN(x) return
#define MAKE_RQ_RET_TYPE void
#else
#define KRETURN(x) ({ return (x); })
#define MAKE_RQ_RET_TYPE int
#endif
#include "debug.h"
#endif

45
modules/cas_disk/debug.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __CASDISK_DEBUG_H__
#define __CASDISK_DEBUG_H__
#undef CASDSK_DEBUG
#ifdef CASDSK_DEBUG
#define CASDSK_DEBUG_TRACE() \
printk(CASDSK_KERN_INFO "%s\n", __func__)
#define CASDSK_DEBUG_DISK_TRACE(dsk) \
printk(CASDSK_KERN_INFO "[%u] %s\n", dsk->id, __func__)
#define CASDSK_DEBUG_MSG(msg) \
printk(CASDSK_KERN_INFO "%s - %s\n", __func__, msg)
#define CASDSK_DEBUG_PARAM(format, ...) \
printk(CASDSK_KERN_INFO "%s - "format"\n", \
__func__, ##__VA_ARGS__)
#define CASDSK_DEBUG_DISK(dsk, format, ...) \
printk(CASDSK_KERN_INFO "[%u] %s - "format"\n", \
dsk->id, \
__func__, ##__VA_ARGS__)
#define CASDSK_DEBUG_ERROR(error, ...) \
CASDSK_DEBUG_PARAM("ERROR(%d) "error, __LINE__, ##__VA_ARGS__)
#define CASDSK_DEBUG_DISK_ERROR(dsk, error, ...) \
CASDSK_DEBUG_DISK(dsk, "ERROR(%d) "error, __LINE__, ##__VA_ARGS__)
#else
#define CASDSK_DEBUG_TRACE()
#define CASDSK_DEBUG_DISK_TRACE(dsk)
#define CASDSK_DEBUG_MSG(msg)
#define CASDSK_DEBUG_PARAM(format, ...)
#define CASDSK_DEBUG_DISK(dsk, format, ...)
#define CASDSK_DEBUG_ERROR(error, ...)
#define CASDSK_DEBUG_DISK_ERROR(dsk, error, ...)
#endif
#endif

452
modules/cas_disk/disk.c Normal file
View File

@@ -0,0 +1,452 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include "cas_disk_defs.h"
#include "cas_cache.h"
#include "disk.h"
#include "exp_obj.h"
#include "sysfs.h"
#define CASDSK_DISK_OPEN_FMODE (FMODE_READ | FMODE_WRITE)
static const char * const _casdsk_disk_modes[] = {
[CASDSK_MODE_UNKNOWN] = "unknown",
[CASDSK_MODE_PT] = "pass-through",
[CASDSK_MODE_ATTACHED] = "attached",
[CASDSK_MODE_TRANS_TO_PT] = "attached -> pass-through",
[CASDSK_MODE_TRANS_TO_ATTACHED] = "pass-through -> attached"
};
static void _casdsk_disk_release(struct kobject *kobj)
{
struct casdsk_disk *dsk;
BUG_ON(!kobj);
dsk = casdsk_kobj_to_disk(kobj);
BUG_ON(!dsk);
CASDSK_DEBUG_DISK_TRACE(dsk);
kfree(dsk->path);
kmem_cache_free(casdsk_module->disk_cache, dsk);
}
static ssize_t _casdsk_disk_mode_show(struct kobject *kobj, char *page)
{
struct casdsk_disk *dsk = casdsk_kobj_to_disk(kobj);
CASDSK_DEBUG_DISK_TRACE(dsk);
return scnprintf(page, PAGE_SIZE, "%s",
_casdsk_disk_modes[atomic_read(&dsk->mode)]);
}
static struct casdsk_attribute _casdsk_disk_mode_attr =
__ATTR(mode, S_IRUGO, _casdsk_disk_mode_show, NULL);
static struct attribute *_casdsk_disk_attrs[] = {
&_casdsk_disk_mode_attr.attr,
NULL
};
static struct kobj_type casdsk_disk_ktype = {
.release = _casdsk_disk_release,
.sysfs_ops = &casdsk_sysfs_ops,
.default_attrs = _casdsk_disk_attrs
};
int __init casdsk_init_disks(void)
{
CASDSK_DEBUG_TRACE();
casdsk_module->next_disk_id = 1;
INIT_LIST_HEAD(&casdsk_module->disk_list);
casdsk_module->disk_major = register_blkdev(casdsk_module->disk_major,
"cas");
if (casdsk_module->disk_major <= 0) {
CASDSK_DEBUG_ERROR("Cannot allocate major number");
return -EINVAL;
}
CASDSK_DEBUG_PARAM("Allocated major number: %d", casdsk_module->disk_major);
casdsk_module->disk_cache =
kmem_cache_create("casdsk_disk", sizeof(struct casdsk_disk),
0, 0, NULL);
if (!casdsk_module->disk_cache) {
unregister_blkdev(casdsk_module->disk_major, "cas");
return -ENOMEM;
}
return 0;
}
void casdsk_deinit_disks(void)
{
CASDSK_DEBUG_TRACE();
kmem_cache_destroy(casdsk_module->disk_cache);
unregister_blkdev(casdsk_module->disk_major, "cas");
}
static int _casdsk_disk_init_kobject(struct casdsk_disk *dsk)
{
int result = 0;
kobject_init(&dsk->kobj, &casdsk_disk_ktype);
result = kobject_add(&dsk->kobj, &disk_to_dev(dsk->bd->bd_disk)->kobj,
"cas%d", dsk->id);
if (result)
CASDSK_DEBUG_DISK_ERROR(dsk, "Cannot register kobject");
return result;
}
struct casdsk_disk *casdsk_disk_open(const char *path, void *private)
{
struct casdsk_disk *dsk;
int result = 0;
BUG_ON(!path);
CASDSK_DEBUG_TRACE();
dsk = kmem_cache_zalloc(casdsk_module->disk_cache, GFP_KERNEL);
if (!dsk) {
CASDSK_DEBUG_ERROR("Cannot allocate memory");
result = -ENOMEM;
goto error_kmem;
}
mutex_init(&dsk->lock);
dsk->path = kstrdup(path, GFP_KERNEL);
if (!dsk->path) {
result = -ENOMEM;
goto error_kstrdup;
}
atomic_set(&dsk->mode, CASDSK_MODE_UNKNOWN);
dsk->bd = open_bdev_exclusive(path, CASDSK_DISK_OPEN_FMODE, dsk);
if (IS_ERR(dsk->bd)) {
CASDSK_DEBUG_ERROR("Cannot open exclusive");
result = PTR_ERR(dsk->bd);
goto error_open_bdev;
}
dsk->private = private;
mutex_lock(&casdsk_module->lock);
dsk->id = casdsk_module->next_disk_id++;
list_add(&dsk->list, &casdsk_module->disk_list);
mutex_unlock(&casdsk_module->lock);
result = _casdsk_disk_init_kobject(dsk);
if (result)
goto error_kobject;
CASDSK_DEBUG_DISK(dsk, "Created (%p)", dsk);
return dsk;
error_kobject:
mutex_lock(&casdsk_module->lock);
list_del(&dsk->list);
mutex_unlock(&casdsk_module->lock);
close_bdev_exclusive(dsk->bd, CASDSK_DISK_OPEN_FMODE);
error_open_bdev:
kfree(dsk->path);
error_kstrdup:
kmem_cache_free(casdsk_module->disk_cache, dsk);
error_kmem:
return ERR_PTR(result);
}
EXPORT_SYMBOL(casdsk_disk_open);
static void _casdsk_disk_claim(struct casdsk_disk *dsk, void *private)
{
dsk->private = private;
}
struct casdsk_disk *casdsk_disk_claim(const char *path, void *private)
{
struct list_head *item;
struct casdsk_disk *dsk = NULL;
BUG_ON(!path);
mutex_lock(&casdsk_module->lock);
list_for_each(item, &casdsk_module->disk_list) {
dsk = list_entry(item, struct casdsk_disk, list);
if (strncmp(path, dsk->path, PATH_MAX) == 0) {
_casdsk_disk_claim(dsk, private);
mutex_unlock(&casdsk_module->lock);
return dsk;
}
}
mutex_unlock(&casdsk_module->lock);
return NULL;
}
EXPORT_SYMBOL(casdsk_disk_claim);
static void __casdsk_disk_close(struct casdsk_disk *dsk)
{
close_bdev_exclusive(dsk->bd, CASDSK_DISK_OPEN_FMODE);
casdsk_exp_obj_free(dsk);
kobject_put(&dsk->kobj);
}
void casdsk_disk_close(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->bd);
CASDSK_DEBUG_DISK(dsk, "Destroying (%p)", dsk);
mutex_lock(&casdsk_module->lock);
list_del(&dsk->list);
mutex_unlock(&casdsk_module->lock);
__casdsk_disk_close(dsk);
}
EXPORT_SYMBOL(casdsk_disk_close);
void __exit casdsk_disk_shutdown_all(void)
{
struct list_head *item, *n;
struct casdsk_disk *dsk;
CASDSK_DEBUG_TRACE();
mutex_lock(&casdsk_module->lock);
list_for_each_safe(item, n, &casdsk_module->disk_list) {
dsk = list_entry(item, struct casdsk_disk, list);
list_del(item);
casdsk_disk_lock(dsk);
BUG_ON(!casdsk_disk_is_pt(dsk) && !casdsk_disk_is_unknown(dsk));
if (casdsk_disk_is_pt(dsk)) {
atomic_set(&dsk->mode, CASDSK_MODE_TRANS_TO_SHUTDOWN);
casdsk_exp_obj_prepare_shutdown(dsk);
}
atomic_set(&dsk->mode, CASDSK_MODE_SHUTDOWN);
if (dsk->exp_obj) {
casdsk_exp_obj_lock(dsk);
casdsk_exp_obj_destroy(dsk);
casdsk_exp_obj_unlock(dsk);
}
casdsk_disk_unlock(dsk);
__casdsk_disk_close(dsk);
}
mutex_unlock(&casdsk_module->lock);
}
struct block_device *casdsk_disk_get_blkdev(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
return dsk->bd;
}
EXPORT_SYMBOL(casdsk_disk_get_blkdev);
struct gendisk *casdsk_disk_get_gendisk(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->bd);
return dsk->bd->bd_disk;
}
EXPORT_SYMBOL(casdsk_disk_get_gendisk);
struct request_queue *casdsk_disk_get_queue(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->bd);
BUG_ON(!dsk->bd->bd_contains);
BUG_ON(!dsk->bd->bd_contains->bd_disk);
return dsk->bd->bd_contains->bd_disk->queue;
}
EXPORT_SYMBOL(casdsk_disk_get_queue);
int casdsk_disk_allocate_minors(int count)
{
int minor = -1;
mutex_lock(&casdsk_module->lock);
if (casdsk_module->next_minor + count <= (1 << MINORBITS)) {
minor = casdsk_module->next_minor;
casdsk_module->next_minor += count;
}
mutex_unlock(&casdsk_module->lock);
return minor;
}
static inline int __casdsk_disk_set_pt(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
atomic_set(&dsk->mode, CASDSK_MODE_TRANS_TO_PT);
casdsk_exp_obj_prepare_pt(dsk);
return 0;
}
int casdsk_disk_set_pt(struct casdsk_disk *dsk)
{
int result;
CASDSK_DEBUG_DISK_TRACE(dsk);
if (!dsk->exp_obj)
return 0;
casdsk_disk_lock(dsk);
result = __casdsk_disk_set_pt(dsk);
casdsk_disk_unlock(dsk);
return result;
}
EXPORT_SYMBOL(casdsk_disk_set_pt);
static inline int __casdsk_disk_set_attached(struct casdsk_disk *dsk)
{
atomic_set(&dsk->mode, CASDSK_MODE_TRANS_TO_ATTACHED);
casdsk_exp_obj_prepare_attached(dsk);
return 0;
}
int casdsk_disk_set_attached(struct casdsk_disk *dsk)
{
int result;
BUG_ON(!dsk);
CASDSK_DEBUG_DISK_TRACE(dsk);
if (!dsk->exp_obj)
return 0;
casdsk_disk_lock(dsk);
result = __casdsk_disk_set_attached(dsk);
casdsk_disk_unlock(dsk);
return result;
}
EXPORT_SYMBOL(casdsk_disk_set_attached);
static inline int __casdsk_disk_clear_pt(struct casdsk_disk *dsk)
{
BUG_ON(atomic_read(&dsk->mode) != CASDSK_MODE_TRANS_TO_PT);
atomic_set(&dsk->mode, CASDSK_MODE_ATTACHED);
return 0;
}
int casdsk_disk_clear_pt(struct casdsk_disk *dsk)
{
int result;
BUG_ON(!dsk);
CASDSK_DEBUG_DISK_TRACE(dsk);
if (!dsk->exp_obj)
return 0;
casdsk_disk_lock(dsk);
result = __casdsk_disk_clear_pt(dsk);
casdsk_disk_unlock(dsk);
return result;
}
EXPORT_SYMBOL(casdsk_disk_clear_pt);
static inline int __casdsk_disk_dettach(struct casdsk_disk *dsk)
{
int result;
BUG_ON(atomic_read(&dsk->mode) != CASDSK_MODE_TRANS_TO_PT);
atomic_set(&dsk->mode, CASDSK_MODE_PT);
result = casdsk_exp_obj_dettach(dsk);
if (result) {
atomic_set(&dsk->mode, CASDSK_MODE_ATTACHED);
return result;
}
return 0;
}
int casdsk_disk_dettach(struct casdsk_disk *dsk)
{
int result;
BUG_ON(!dsk);
CASDSK_DEBUG_DISK_TRACE(dsk);
if (!dsk->exp_obj)
return 0;
casdsk_disk_lock(dsk);
result = __casdsk_disk_dettach(dsk);
casdsk_disk_unlock(dsk);
return result;
}
EXPORT_SYMBOL(casdsk_disk_dettach);
static inline int __casdsk_disk_attach(struct casdsk_disk *dsk,
struct module *owner, struct casdsk_exp_obj_ops *ops)
{
int result;
BUG_ON(!ops);
BUG_ON(atomic_read(&dsk->mode) != CASDSK_MODE_TRANS_TO_ATTACHED);
result = casdsk_exp_obj_attach(dsk, owner, ops);
if (result) {
atomic_set(&dsk->mode, CASDSK_MODE_PT);
return result;
}
atomic_set(&dsk->mode, CASDSK_MODE_ATTACHED);
return 0;
}
int casdsk_disk_attach(struct casdsk_disk *dsk, struct module *owner,
struct casdsk_exp_obj_ops *ops)
{
int result;
CASDSK_DEBUG_DISK_TRACE(dsk);
if (!dsk->exp_obj)
return 0;
casdsk_disk_lock(dsk);
result = __casdsk_disk_attach(dsk, owner, ops);
casdsk_disk_unlock(dsk);
return result;
}
EXPORT_SYMBOL(casdsk_disk_attach);

96
modules/cas_disk/disk.h Normal file
View File

@@ -0,0 +1,96 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __
#define __CASDISK_DISK_H__
#include <linux/kobject.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/mutex.h>
struct casdsk_exp_obj;
#define CASDSK_MODE_UNKNOWN 0
#define CASDSK_MODE_PT (1 << 0)
#define CASDSK_MODE_ATTACHED (1 << 1)
#define CASDSK_MODE_SHUTDOWN (1 << 2)
#define CASDSK_MODE_TRANSITION (1 << 3)
#define CASDSK_MODE_TRANS_TO_ATTACHED (CASDSK_MODE_PT | CASDSK_MODE_TRANSITION)
#define CASDSK_MODE_TRANS_TO_PT (CASDSK_MODE_ATTACHED | \
CASDSK_MODE_TRANSITION)
#define CASDSK_MODE_TRANS_TO_SHUTDOWN (CASDSK_MODE_SHUTDOWN | \
CASDSK_MODE_TRANSITION)
struct casdsk_disk {
uint32_t id;
atomic_t mode;
char *path;
struct mutex lock;
struct block_device *bd;
int gd_flags;
int gd_minors;
struct casdsk_exp_obj *exp_obj;
struct kobject kobj;
struct list_head list;
void *private;
};
int __init casdsk_init_disks(void);
void casdsk_deinit_disks(void);
void __exit casdsk_disk_shutdown_all(void);
int casdsk_disk_allocate_minors(int count);
static inline void casdsk_disk_lock(struct casdsk_disk *dsk)
{
mutex_lock(&dsk->lock);
}
static inline void casdsk_disk_unlock(struct casdsk_disk *dsk)
{
mutex_unlock(&dsk->lock);
}
static inline struct casdsk_disk *casdsk_kobj_to_disk(struct kobject *kobj)
{
return container_of(kobj, struct casdsk_disk, kobj);
}
static inline bool casdsk_disk_in_transition(struct casdsk_disk *dsk)
{
return (atomic_read(&dsk->mode) & CASDSK_MODE_TRANSITION) ==
CASDSK_MODE_TRANSITION;
}
static inline bool casdsk_disk_is_attached(struct casdsk_disk *dsk)
{
return (atomic_read(&dsk->mode) & CASDSK_MODE_ATTACHED) ==
CASDSK_MODE_ATTACHED;
}
static inline bool casdsk_disk_is_pt(struct casdsk_disk *dsk)
{
return (atomic_read(&dsk->mode) & CASDSK_MODE_PT) == CASDSK_MODE_PT;
}
static inline bool casdsk_disk_is_shutdown(struct casdsk_disk *dsk)
{
return (atomic_read(&dsk->mode) & CASDSK_MODE_SHUTDOWN) ==
CASDSK_MODE_SHUTDOWN;
}
static inline bool casdsk_disk_is_unknown(struct casdsk_disk *dsk)
{
return atomic_read(&dsk->mode) == CASDSK_MODE_UNKNOWN;
}
#endif

842
modules/cas_disk/exp_obj.c Normal file
View File

@@ -0,0 +1,842 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/blkpg.h>
#include <linux/elevator.h>
#include "cas_disk_defs.h"
#include "cas_disk.h"
#include "disk.h"
#include "exp_obj.h"
#include "linux_kernel_version.h"
#define CASDSK_DEV_MINORS 16
#define KMEM_CACHE_MIN_SIZE sizeof(void *)
int __init casdsk_init_exp_objs(void)
{
int ncpus;
CASDSK_DEBUG_TRACE();
casdsk_module->exp_obj_cache = kmem_cache_create("casdsk_exp_obj",
sizeof(struct casdsk_exp_obj), 0, 0, NULL);
if (!casdsk_module->exp_obj_cache)
goto error_exp_obj_cache;
ncpus = num_online_cpus();
casdsk_module->pending_rqs_cache =
kmem_cache_create("casdsk_exp_obj_pending_rqs",
((sizeof(atomic_t) * ncpus) < KMEM_CACHE_MIN_SIZE) ?
KMEM_CACHE_MIN_SIZE : (sizeof(atomic_t) * ncpus),
0, 0, NULL);
if (!casdsk_module->pending_rqs_cache)
goto error_pending_rqs_cache;
casdsk_module->pt_io_ctx_cache =
kmem_cache_create("casdsk_exp_obj_pt_io_ctx",
sizeof(struct casdsk_exp_obj_pt_io_ctx),
0, 0, NULL);
if (!casdsk_module->pt_io_ctx_cache)
goto error_pt_io_ctx_cache;
return 0;
error_pt_io_ctx_cache:
kmem_cache_destroy(casdsk_module->pending_rqs_cache);
error_pending_rqs_cache:
kmem_cache_destroy(casdsk_module->exp_obj_cache);
error_exp_obj_cache:
return -ENOMEM;
}
void casdsk_deinit_exp_objs(void)
{
CASDSK_DEBUG_TRACE();
kmem_cache_destroy(casdsk_module->pt_io_ctx_cache);
kmem_cache_destroy(casdsk_module->pending_rqs_cache);
kmem_cache_destroy(casdsk_module->exp_obj_cache);
}
static int _casdsk_exp_obj_prep_rq_fn(struct request_queue *q, struct request *rq)
{
struct casdsk_disk *dsk;;
BUG_ON(!q);
BUG_ON(!q->queuedata);
dsk = q->queuedata;
BUG_ON(!dsk->exp_obj);
if (likely(dsk->exp_obj->ops && dsk->exp_obj->ops->prep_rq_fn))
return dsk->exp_obj->ops->prep_rq_fn(dsk, q, rq, dsk->private);
else
return BLKPREP_OK;
}
static void _casdsk_exp_obj_request_fn(struct request_queue *q)
{
struct casdsk_disk *dsk;
struct request *rq;
BUG_ON(!q);
BUG_ON(!q->queuedata);
dsk = q->queuedata;
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
if (likely(dsk->exp_obj->ops && dsk->exp_obj->ops->request_fn)) {
dsk->exp_obj->ops->request_fn(dsk, q, dsk->private);
} else {
/*
* request_fn() is required, as we can't do any default
* action in attached mode. In PT mode we handle all bios
* directly in make_request_fn(), so request_fn() will not
* be called.
*/
rq = blk_peek_request(q);
BUG_ON(rq);
}
}
static inline void _casdsk_exp_obj_handle_bio_att(struct casdsk_disk *dsk,
struct request_queue *q,
struct bio *bio)
{
int status = CASDSK_BIO_NOT_HANDLED;
if (likely(dsk->exp_obj->ops->make_request_fn))
status = dsk->exp_obj->ops->
make_request_fn(dsk, q, bio, dsk->private);
if (status == CASDSK_BIO_NOT_HANDLED)
dsk->exp_obj->mk_rq_fn(q, bio);
}
DECLARE_BLOCK_CALLBACK(_casdsk_exp_obj_bio_pt_io, struct bio *bio,
unsigned int bytes_done, int error)
{
struct casdsk_exp_obj_pt_io_ctx *io;
BUG_ON(!bio);
BLOCK_CALLBACK_INIT(bio);
io = bio->bi_private;
BUG_ON(!io);
BIO_ENDIO(io->bio, BIO_BISIZE(io->bio),
BLOCK_CALLBACK_ERROR(bio, error));
if (atomic_dec_return(&io->dsk->exp_obj->pt_ios) < 0)
BUG();
bio_put(bio);
kmem_cache_free(casdsk_module->pt_io_ctx_cache, io);
BLOCK_CALLBACK_RETURN();
}
static inline void _casdsk_exp_obj_handle_bio_pt(struct casdsk_disk *dsk,
struct request_queue *q,
struct bio *bio)
{
struct bio *cloned_bio;
struct casdsk_exp_obj_pt_io_ctx *io;
io = kmem_cache_zalloc(casdsk_module->pt_io_ctx_cache, GFP_ATOMIC);
if (!io) {
BIO_ENDIO(bio, BIO_BISIZE(bio), -ENOMEM);
return;
}
cloned_bio = cas_bio_clone(bio, GFP_ATOMIC);
if (!cloned_bio) {
kmem_cache_free(casdsk_module->pt_io_ctx_cache, io);
BIO_ENDIO(bio, BIO_BISIZE(bio), -ENOMEM);
return;
}
io->bio = bio;
io->dsk = dsk;
atomic_inc(&dsk->exp_obj->pt_ios);
CAS_BIO_SET_DEV(cloned_bio, casdsk_disk_get_blkdev(dsk));
cloned_bio->bi_private = io;
cloned_bio->bi_end_io = REFER_BLOCK_CALLBACK(_casdsk_exp_obj_bio_pt_io);
cas_submit_bio(BIO_OP_FLAGS(cloned_bio), cloned_bio);
}
static inline void _casdsk_exp_obj_handle_bio(struct casdsk_disk *dsk,
struct request_queue *q,
struct bio *bio)
{
if (likely(casdsk_disk_is_attached(dsk)))
_casdsk_exp_obj_handle_bio_att(dsk, q, bio);
else if (casdsk_disk_is_pt(dsk))
_casdsk_exp_obj_handle_bio_pt(dsk, q, bio);
else if (casdsk_disk_is_shutdown(dsk))
BIO_ENDIO(bio, BIO_BISIZE(bio), -EIO);
else
BUG();
}
static inline void _casdsk_exp_obj_end_rq(struct casdsk_disk *dsk, unsigned int cpu)
{
return atomic_dec(&dsk->exp_obj->pending_rqs[cpu]);
}
static inline unsigned int _casdsk_exp_obj_begin_rq(struct casdsk_disk *dsk)
{
unsigned int cpu;
BUG_ON(!dsk);
retry:
while (unlikely(casdsk_disk_in_transition(dsk)))
io_schedule();
cpu = smp_processor_id();
atomic_inc(&dsk->exp_obj->pending_rqs[cpu]);
if (unlikely(casdsk_disk_in_transition(dsk))) {
/*
* If we are in transition state, decrement pending rqs counter
* and retry bio processing
*/
_casdsk_exp_obj_end_rq(dsk, cpu);
goto retry;
}
return cpu;
}
static MAKE_RQ_RET_TYPE _casdsk_exp_obj_make_rq_fn(struct request_queue *q,
struct bio *bio)
{
struct casdsk_disk *dsk;
unsigned int cpu;
BUG_ON(!bio);
BUG_ON(!q);
BUG_ON(!q->queuedata);
dsk = q->queuedata;
cpu = _casdsk_exp_obj_begin_rq(dsk);
_casdsk_exp_obj_handle_bio(dsk, q, bio);
_casdsk_exp_obj_end_rq(dsk, cpu);
KRETURN(0);
}
static int _casdsk_get_next_part_no(struct block_device *bd)
{
int part_no = 0;
struct gendisk *disk = bd->bd_disk;
struct disk_part_iter piter;
struct hd_struct *part;
mutex_lock(&bd->bd_mutex);
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
while ((part = disk_part_iter_next(&piter))) {
part_no = part->partno;
break;
}
disk_part_iter_exit(&piter);
mutex_unlock(&bd->bd_mutex);
return part_no;
}
static int _casdsk_del_partitions(struct block_device *bd)
{
int result = 0;
int part_no;
struct blkpg_partition bpart;
struct blkpg_ioctl_arg barg;
memset(&bpart, 0, sizeof(struct blkpg_partition));
memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
barg.data = (void __force __user *) &bpart;
barg.op = BLKPG_DEL_PARTITION;
while ((part_no = _casdsk_get_next_part_no(bd))) {
bpart.pno = part_no;
result = ioctl_by_bdev(bd, BLKPG, (unsigned long) &barg);
if (result == 0) {
printk(CASDSK_KERN_INFO "Partition %d on %s hidden\n",
part_no, bd->bd_disk->disk_name);
} else {
printk(CASDSK_KERN_ERR "Error(%d) hiding the partition %d on %s\n",
result, part_no, bd->bd_disk->disk_name);
break;
}
}
return result;
}
#ifdef GENHD_FL_NO_PART_SCAN
static int _casdsk_flags = GENHD_FL_NO_PART_SCAN | GENHD_FL_EXT_DEVT;
#else
static int _casdsk_flags = GENHD_FL_EXT_DEVT;
#endif
static int _casdsk_exp_obj_hide_parts(struct casdsk_disk *dsk)
{
struct block_device *bd = casdsk_disk_get_blkdev(dsk);
struct gendisk *gdsk = casdsk_disk_get_gendisk(dsk);
if (bd != bd->bd_contains)
/* It is partition, no more job required */
return 0;
if (disk_max_parts(dsk->bd->bd_disk) > 1) {
if (_casdsk_del_partitions(bd)) {
printk(CASDSK_KERN_ERR "Error deleting a partition on thedevice %s\n",
gdsk->disk_name);
/* Try restore previous partitions by rescaning */
ioctl_by_bdev(bd, BLKRRPART, (unsigned long) NULL);
return -EINVAL;
}
}
/* Save original flags and minors */
dsk->gd_flags = gdsk->flags & _casdsk_flags;
dsk->gd_minors = gdsk->minors;
/* Setup disk of bottom device as not partitioned device */
gdsk->flags &= ~_casdsk_flags;
gdsk->minors = 1;
/* Rescan partitions */
ioctl_by_bdev(bd, BLKRRPART, (unsigned long) NULL);
return 0;
}
static int _casdsk_exp_obj_set_dev_t(struct casdsk_disk *dsk, struct gendisk *gd)
{
int flags;
int minors = disk_max_parts(casdsk_disk_get_gendisk(dsk));
struct block_device *bdev;
bdev = casdsk_disk_get_blkdev(dsk);
BUG_ON(!bdev);
if (bdev->bd_contains != bdev) {
minors = 1;
flags = 0;
} else {
if (_casdsk_exp_obj_hide_parts(dsk))
return -EINVAL;
flags = dsk->gd_flags;
}
gd->first_minor = casdsk_disk_allocate_minors(minors);
if (gd->first_minor < 0) {
CASDSK_DEBUG_DISK_ERROR(dsk, "Cannot allocate %d minors", minors);
return -EINVAL;
}
gd->minors = minors;
gd->major = casdsk_module->disk_major;
gd->flags |= flags;
return 0;
}
static void _casdsk_exp_obj_clear_dev_t(struct casdsk_disk *dsk)
{
struct block_device *bdev = casdsk_disk_get_blkdev(dsk);
struct gendisk *gdsk = casdsk_disk_get_gendisk(dsk);
if (bdev->bd_contains == bdev) {
/* Restore previous configuration of bottom disk */
gdsk->minors = dsk->gd_minors;
gdsk->flags |= dsk->gd_flags;
ioctl_by_bdev(bdev, BLKRRPART, (unsigned long) NULL);
}
}
static const struct block_device_operations _casdsk_exp_obj_ops = {
.owner = THIS_MODULE,
};
static int casdsk_exp_obj_alloc(struct casdsk_disk *dsk)
{
struct casdsk_exp_obj *exp_obj;
int result;
BUG_ON(!dsk);
BUG_ON(dsk->exp_obj);
CASDSK_DEBUG_DISK_TRACE(dsk);
exp_obj = kmem_cache_zalloc(casdsk_module->exp_obj_cache, GFP_KERNEL);
if (!exp_obj) {
CASDSK_DEBUG_ERROR("Cannot allocate memory");
result = -ENOMEM;
goto error_exp_obj_alloc;
}
exp_obj->pending_rqs = kmem_cache_zalloc(casdsk_module->pending_rqs_cache,
GFP_KERNEL);
if (!exp_obj->pending_rqs) {
result = -ENOMEM;
goto error_pending_rqs_alloc;
}
dsk->exp_obj = exp_obj;
return 0;
error_pending_rqs_alloc:
kmem_cache_free(casdsk_module->exp_obj_cache, exp_obj);
error_exp_obj_alloc:
return result;
}
void casdsk_exp_obj_free(struct casdsk_disk *dsk)
{
struct casdsk_exp_obj *exp_obj;
CASDSK_DEBUG_DISK_TRACE(dsk);
exp_obj = dsk->exp_obj;
if (!exp_obj)
return;
kobject_put(&exp_obj->kobj);
dsk->exp_obj = NULL;
}
static void __casdsk_exp_obj_release(struct casdsk_exp_obj *exp_obj)
{
kfree(exp_obj->dev_name);
kmem_cache_free(casdsk_module->pending_rqs_cache, exp_obj->pending_rqs);
kmem_cache_free(casdsk_module->exp_obj_cache, exp_obj);
}
static void _casdsk_exp_obj_release(struct kobject *kobj)
{
struct casdsk_exp_obj *exp_obj;
struct module *owner;
BUG_ON(!kobj);
exp_obj = casdsk_kobj_to_exp_obj(kobj);
BUG_ON(!exp_obj);
CASDSK_DEBUG_TRACE();
owner = exp_obj->owner;
__casdsk_exp_obj_release(exp_obj);
if (owner)
module_put(owner);
}
static struct kobj_type casdsk_exp_obj_ktype = {
.release = _casdsk_exp_obj_release
};
static int _casdsk_exp_obj_init_kobject(struct casdsk_disk *dsk)
{
int result = 0;
struct casdsk_exp_obj *exp_obj = dsk->exp_obj;
kobject_init(&exp_obj->kobj, &casdsk_exp_obj_ktype);
result = kobject_add(&exp_obj->kobj, &dsk->kobj,
"%s", exp_obj->dev_name);
if (result)
CASDSK_DEBUG_DISK_ERROR(dsk, "Cannot register kobject");
return result;
}
int casdsk_exp_obj_create(struct casdsk_disk *dsk, const char *dev_name,
struct module *owner, struct casdsk_exp_obj_ops *ops)
{
struct casdsk_exp_obj *exp_obj;
struct request_queue *queue;
struct gendisk *gd;
int result = 0;
BUG_ON(!owner);
BUG_ON(!dsk);
BUG_ON(!ops);
BUG_ON(dsk->exp_obj);
CASDSK_DEBUG_DISK_TRACE(dsk);
if (strlen(dev_name) >= DISK_NAME_LEN)
return -EINVAL;
result = casdsk_exp_obj_alloc(dsk);
if (result)
goto error_alloc;
exp_obj = dsk->exp_obj;
exp_obj->dev_name = kstrdup(dev_name, GFP_KERNEL);
if (!exp_obj->dev_name) {
__casdsk_exp_obj_release(exp_obj);
result = -ENOMEM;
goto error_strdup;
}
result = _casdsk_exp_obj_init_kobject(dsk);
if (result) {
__casdsk_exp_obj_release(exp_obj);
goto error_kobject;
}
if (!try_module_get(owner)) {
CASDSK_DEBUG_DISK_ERROR(dsk, "Cannot get reference to module");
result = -ENAVAIL;
goto error_module;
}
exp_obj->owner = owner;
exp_obj->ops = ops;
gd = alloc_disk(1);
if (!gd) {
result = -ENOMEM;
goto error_alloc_disk;
}
exp_obj->gd = gd;
result = _casdsk_exp_obj_set_dev_t(dsk, gd);
if (result)
goto error_dev_t;
spin_lock_init(&exp_obj->rq_lock);
queue = blk_init_queue(_casdsk_exp_obj_request_fn, &exp_obj->rq_lock);
if (!queue) {
result = -ENOMEM;
goto error_init_queue;
}
BUG_ON(queue->queuedata);
queue->queuedata = dsk;
exp_obj->queue = queue;
gd->fops = &_casdsk_exp_obj_ops;
gd->queue = queue;
gd->private_data = dsk;
strlcpy(gd->disk_name, exp_obj->dev_name, sizeof(gd->disk_name));
if (exp_obj->ops->prepare_queue) {
result = exp_obj->ops->prepare_queue(dsk, queue, dsk->private);
if (result)
goto error_prepare_queue;
}
blk_queue_prep_rq(queue, _casdsk_exp_obj_prep_rq_fn);
dsk->exp_obj->mk_rq_fn = queue->make_request_fn;
blk_queue_make_request(queue, _casdsk_exp_obj_make_rq_fn);
if (exp_obj->ops->set_geometry) {
result = exp_obj->ops->set_geometry(dsk, dsk->private);
if (result)
goto error_set_geometry;
}
return 0;
error_set_geometry:
if (exp_obj->ops->cleanup_queue)
exp_obj->ops->cleanup_queue(dsk, queue, dsk->private);
error_prepare_queue:
blk_cleanup_queue(queue);
error_init_queue:
_casdsk_exp_obj_clear_dev_t(dsk);
error_dev_t:
put_disk(gd);
error_alloc_disk:
module_put(owner);
dsk->exp_obj->owner = NULL;
error_module:
casdsk_exp_obj_free(dsk);
error_kobject:
error_strdup:
dsk->exp_obj = NULL;
error_alloc:
return result;
}
EXPORT_SYMBOL(casdsk_exp_obj_create);
struct request_queue *casdsk_exp_obj_get_queue(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
return dsk->exp_obj->queue;
}
EXPORT_SYMBOL(casdsk_exp_obj_get_queue);
struct gendisk *casdsk_exp_obj_get_gendisk(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
return dsk->exp_obj->gd;
}
EXPORT_SYMBOL(casdsk_exp_obj_get_gendisk);
static bool _casdsk_exp_obj_exists(const char *path)
{
struct file *exported;
exported = filp_open(path, O_RDONLY, 0);
if (!exported || IS_ERR(exported)) {
/*failed to open file - it is safe to assume,
* it does not exist
*/
return false;
}
filp_close(exported, NULL);
return true;
}
int casdsk_exp_obj_activate(struct casdsk_disk *dsk)
{
char *path;
int result;
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
BUG_ON(!dsk->exp_obj->gd);
BUG_ON(dsk->exp_obj->activated);
CASDSK_DEBUG_DISK_TRACE(dsk);
path = kmalloc(PATH_MAX, GFP_KERNEL);
if (!path)
return -ENOMEM;
snprintf(path, PATH_MAX, "/dev/%s", dsk->exp_obj->dev_name);
if (_casdsk_exp_obj_exists(path)) {
printk(CASDSK_KERN_ERR "Could not activate exported object, "
"because file %s exists.\n", path);
kfree(path);
return -EEXIST;
}
kfree(path);
dsk->exp_obj->activated = true;
atomic_set(&dsk->mode, CASDSK_MODE_ATTACHED);
add_disk(dsk->exp_obj->gd);
result = bd_claim_by_disk(dsk->bd, dsk, dsk->exp_obj->gd);
if (result)
goto error_bd_claim;
result = sysfs_create_link(&dsk->exp_obj->kobj,
&disk_to_dev(dsk->exp_obj->gd)->kobj,
"blockdev");
if (result)
goto error_sysfs_link;
CASDSK_DEBUG_DISK(dsk, "Activated exp object %s", dsk->exp_obj->dev_name);
return 0;
error_sysfs_link:
bd_release_from_disk(dsk->bd, dsk->exp_obj->gd);
error_bd_claim:
del_gendisk(dsk->exp_obj->gd);
dsk->exp_obj->activated = false;
return result;
}
EXPORT_SYMBOL(casdsk_exp_obj_activate);
bool casdsk_exp_obj_activated(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
return dsk->exp_obj->activated;
}
EXPORT_SYMBOL(casdsk_exp_obj_activated);
int casdsk_exp_obj_lock(struct casdsk_disk *dsk)
{
struct casdsk_exp_obj *exp_obj;
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
CASDSK_DEBUG_DISK_TRACE(dsk);
exp_obj = dsk->exp_obj;
exp_obj->locked_bd = bdget_disk(exp_obj->gd, 0);
if (!exp_obj->locked_bd)
return -ENAVAIL;
mutex_lock(&exp_obj->locked_bd->bd_mutex);
if (exp_obj->locked_bd->bd_openers) {
printk(CASDSK_KERN_DEBUG "Device %s in use (openers=%d). Refuse to stop\n",
exp_obj->locked_bd->bd_disk->disk_name,
exp_obj->locked_bd->bd_openers);
casdsk_exp_obj_unlock(dsk);
return -EBUSY;
}
return 0;
}
EXPORT_SYMBOL(casdsk_exp_obj_lock);
int casdsk_exp_obj_unlock(struct casdsk_disk *dsk)
{
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
BUG_ON(!dsk->exp_obj->locked_bd);
CASDSK_DEBUG_DISK_TRACE(dsk);
mutex_unlock(&dsk->exp_obj->locked_bd->bd_mutex);
bdput(dsk->exp_obj->locked_bd);
dsk->exp_obj->locked_bd = NULL;
return 0;
}
EXPORT_SYMBOL(casdsk_exp_obj_unlock);
int casdsk_exp_obj_destroy(struct casdsk_disk *dsk)
{
struct casdsk_exp_obj *exp_obj;
BUG_ON(!dsk);
BUG_ON(!dsk->exp_obj);
BUG_ON(!dsk->exp_obj->locked_bd);
CASDSK_DEBUG_DISK_TRACE(dsk);
exp_obj = dsk->exp_obj;
if (casdsk_exp_obj_activated(dsk)) {
sysfs_remove_link(&exp_obj->kobj, "blockdev");
bd_release_from_disk(dsk->bd, exp_obj->gd);
_casdsk_exp_obj_clear_dev_t(dsk);
del_gendisk(exp_obj->gd);
}
if (exp_obj->queue)
blk_cleanup_queue(exp_obj->queue);
atomic_set(&dsk->mode, CASDSK_MODE_UNKNOWN);
put_disk(exp_obj->gd);
return 0;
}
EXPORT_SYMBOL(casdsk_exp_obj_destroy);
int casdsk_exp_obj_dettach(struct casdsk_disk *dsk)
{
module_put(dsk->exp_obj->owner);
dsk->exp_obj->owner = NULL;
dsk->exp_obj->ops = NULL;
return 0;
}
int casdsk_exp_obj_attach(struct casdsk_disk *dsk, struct module *owner,
struct casdsk_exp_obj_ops *ops)
{
if (!try_module_get(owner)) {
CASDSK_DEBUG_DISK_ERROR(dsk, "Cannot get reference to module");
return -ENAVAIL;
}
dsk->exp_obj->owner = owner;
dsk->exp_obj->ops = ops;
return 0;
}
static void _casdsk_exp_obj_wait_for_pending_rqs(struct casdsk_disk *dsk)
{
int i, ncpus;
struct casdsk_exp_obj *exp_obj = dsk->exp_obj;
ncpus = num_online_cpus();
for (i = 0; i < ncpus; i++)
while (atomic_read(&exp_obj->pending_rqs[i]))
schedule();
}
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 3, 0)
static void _casdsk_exp_obj_drain_elevator(struct request_queue *q)
{
if (q->elevator && q->elevator->elevator_type)
while (q->elevator->elevator_type->ops.
elevator_dispatch_fn(q, 1))
;
}
#elif LINUX_VERSION_CODE <= KERNEL_VERSION(4, 10, 0)
static void _casdsk_exp_obj_drain_elevator(struct request_queue *q)
{
if (q->elevator && q->elevator->type)
while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
;
}
#else
static void _casdsk_exp_obj_drain_elevator(struct request_queue *q)
{
if (q->elevator && q->elevator->type)
while (q->elevator->type->ops.sq.elevator_dispatch_fn(q, 1))
;
}
#endif
static void _casdsk_exp_obj_flush_queue(struct casdsk_disk *dsk)
{
struct casdsk_exp_obj *exp_obj = dsk->exp_obj;
struct request_queue *q = exp_obj->queue;
spin_lock_irq(q->queue_lock);
_casdsk_exp_obj_drain_elevator(q);
spin_unlock_irq(q->queue_lock);
blk_run_queue(q);
blk_sync_queue(q);
}
void casdsk_exp_obj_prepare_pt(struct casdsk_disk *dsk)
{
_casdsk_exp_obj_wait_for_pending_rqs(dsk);
_casdsk_exp_obj_flush_queue(dsk);
}
void casdsk_exp_obj_prepare_attached(struct casdsk_disk *dsk)
{
_casdsk_exp_obj_wait_for_pending_rqs(dsk);
while (atomic_read(&dsk->exp_obj->pt_ios))
schedule_timeout(msecs_to_jiffies(200));
}
void casdsk_exp_obj_prepare_shutdown(struct casdsk_disk *dsk)
{
_casdsk_exp_obj_wait_for_pending_rqs(dsk);
while (atomic_read(&dsk->exp_obj->pt_ios))
schedule_timeout(msecs_to_jiffies(200));
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __CASDISK_EXP_OBJ_H__
#define __CASDISK_EXP_OBJ_H__
#include <linux/kobject.h>
#include <linux/fs.h>
struct casdsk_disk;
struct casdsk_exp_obj_pt_io_ctx {
struct casdsk_disk *dsk;
struct bio *bio;
};
struct casdsk_exp_obj {
struct gendisk *gd;
struct request_queue *queue;
spinlock_t rq_lock;
struct block_device *locked_bd;
struct module *owner;
bool activated;
struct casdsk_exp_obj_ops *ops;
make_request_fn *mk_rq_fn;
const char *dev_name;
struct kobject kobj;
atomic_t pt_ios;
atomic_t *pending_rqs;
};
int __init casdsk_init_exp_objs(void);
void casdsk_deinit_exp_objs(void);
void casdsk_exp_obj_free(struct casdsk_disk *dsk);
int casdsk_exp_obj_dettach(struct casdsk_disk *dsk);
int casdsk_exp_obj_attach(struct casdsk_disk *dsk, struct module *owner,
struct casdsk_exp_obj_ops *ops);
void casdsk_exp_obj_prepare_pt(struct casdsk_disk *dsk);
void casdsk_exp_obj_prepare_attached(struct casdsk_disk *dsk);
void casdsk_exp_obj_prepare_shutdown(struct casdsk_disk *dsk);
static inline struct casdsk_exp_obj *casdsk_kobj_to_exp_obj(struct kobject *kobj)
{
return container_of(kobj, struct casdsk_exp_obj, kobj);
}
#endif

View File

@@ -0,0 +1,2 @@
/home/robert/work/cas/ICAS_Linux/modules/cas_disk/exp_obj.o-.text-f20
/home/robert/work/cas/ICAS_Linux/modules/cas_disk/exp_obj.o-.text-f27

165
modules/cas_disk/main.c Normal file
View File

@@ -0,0 +1,165 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include "cas_disk_defs.h"
#include "cas_disk.h"
#include "disk.h"
#include "exp_obj.h"
#include "sysfs.h"
/* Layer information. */
MODULE_AUTHOR("Intel(R) Corporation");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(CAS_VERSION);
static int iface_version = CASDSK_IFACE_VERSION;
module_param(iface_version, int, (S_IRUSR | S_IRGRP));
static int upgrade_in_progress = 0;
module_param(upgrade_in_progress, int, (S_IRUSR | S_IRGRP));
struct casdsk_module *casdsk_module;
uint32_t casdsk_get_version(void)
{
return CASDSK_IFACE_VERSION;
}
EXPORT_SYMBOL(casdsk_get_version);
static void _casdsk_module_free_config(struct casdsk_module *mod)
{
int i;
if (mod->config.blobs) {
for (i = 0; i < mod->config.n_blobs; i++)
vfree(mod->config.blobs[i].buffer);
kfree(mod->config.blobs);
mod->config.blobs = NULL;
mod->config.n_blobs = 0;
}
}
void casdsk_store_config(size_t n_blobs, struct casdsk_props_conf *blobs)
{
upgrade_in_progress = 1;
_casdsk_module_free_config(casdsk_module);
casdsk_module->config.blobs = blobs;
casdsk_module->config.n_blobs = n_blobs;
}
EXPORT_SYMBOL(casdsk_store_config);
size_t casdsk_get_stored_config(struct casdsk_props_conf **blobs)
{
BUG_ON(!blobs);
*blobs = casdsk_module->config.blobs;
return casdsk_module->config.n_blobs;
}
EXPORT_SYMBOL(casdsk_get_stored_config);
void casdsk_free_stored_config(void)
{
CASDSK_DEBUG_TRACE();
_casdsk_module_free_config(casdsk_module);
upgrade_in_progress = 0;
}
EXPORT_SYMBOL(casdsk_free_stored_config);
static void _casdsk_module_release(struct kobject *kobj)
{
struct casdsk_module *mod;
CASDSK_DEBUG_TRACE();
BUG_ON(!kobj);
mod = container_of(kobj, struct casdsk_module, kobj);
BUG_ON(!mod);
_casdsk_module_free_config(mod);
kfree(mod);
}
static struct kobj_type _casdsk_module_ktype = {
.release = _casdsk_module_release,
};
static int __init casdsk_init_kobjects(void)
{
int result = 0;
CASDSK_DEBUG_TRACE();
kobject_init(&casdsk_module->kobj, &_casdsk_module_ktype);
result = kobject_add(&casdsk_module->kobj, kernel_kobj, "cas_disk");
if (result)
CASDSK_DEBUG_ERROR("Cannot register kobject");
return result;
}
static int __init casdsk_init_module(void)
{
int result = 0;
casdsk_module = kzalloc(sizeof(*casdsk_module), GFP_KERNEL);
if (!casdsk_module) {
result = -ENOMEM;
goto error_kmalloc;
}
mutex_init(&casdsk_module->lock);
mutex_lock(&casdsk_module->lock);
result = casdsk_init_exp_objs();
if (result)
goto error_init_exp_objs;
result = casdsk_init_disks();
if (result)
goto error_init_disks;
result = casdsk_init_kobjects();
if (result)
goto error_kobjects;
mutex_unlock(&casdsk_module->lock);
printk(CASDSK_KERN_INFO "%s Version %s (%s)::Module loaded successfully\n",
CASDSK_LOGO, CAS_VERSION, CAS_KERNEL);
return result;
error_kobjects:
casdsk_deinit_disks();
error_init_disks:
casdsk_deinit_exp_objs();
error_init_exp_objs:
mutex_unlock(&casdsk_module->lock);
kfree(casdsk_module);
error_kmalloc:
return result;
}
module_init(casdsk_init_module);
static void __exit casdsk_deinit_kobjects(void)
{
kobject_put(&casdsk_module->kobj);
}
static void __exit casdsk_exit_module(void)
{
casdsk_disk_shutdown_all();
casdsk_deinit_disks();
casdsk_deinit_exp_objs();
casdsk_deinit_kobjects();
}
module_exit(casdsk_exit_module);

35
modules/cas_disk/sysfs.c Normal file
View File

@@ -0,0 +1,35 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "cas_disk_defs.h"
#include "sysfs.h"
static ssize_t _casdsk_sysfs_show(struct kobject *kobj, struct attribute *attr,
char *page)
{
struct casdsk_attribute *casdsk_attr =
container_of(attr, struct casdsk_attribute, attr);
if (!casdsk_attr->show)
return -EIO;
return casdsk_attr->show(kobj, page);
}
static ssize_t _casdsk_sysfs_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t len)
{
struct casdsk_attribute *casdsk_attr =
container_of(attr, struct casdsk_attribute, attr);
if (!casdsk_attr->store)
return -EIO;
return casdsk_attr->store(kobj, buf, len);
}
const struct sysfs_ops casdsk_sysfs_ops = {
.show = _casdsk_sysfs_show,
.store = _casdsk_sysfs_store
};

21
modules/cas_disk/sysfs.h Normal file
View File

@@ -0,0 +1,21 @@
/*
* Copyright(c) 2012-2019 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __CASDISK_SYSFS_H__
#define __CASDISK_SYSFS_H__
#include <linux/kobject.h>
#include <linux/sysfs.h>
struct casdsk_disk;
struct casdsk_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, char *page);
ssize_t (*store)(struct kobject *kobj, const char *buf, size_t len);
};
extern const struct sysfs_ops casdsk_sysfs_ops;
#endif