1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h>
#include <linux/genhd.h> #include <linux/fs.h> #include <linux/blkdev.h> #include <linux/blk_types.h> #include <linux/blkdev.h> #include <linux/blk-mq.h> #include <linux/bio.h> #include <linux/vmalloc.h>
MODULE_DESCRIPTION("Simple RAM Disk"); MODULE_AUTHOR("SO2"); MODULE_LICENSE("GPL");
#define KERN_LOG_LEVEL KERN_ALERT
#define MY_BLOCK_MAJOR 240 #define MY_BLKDEV_NAME "mybdev" #define MY_BLOCK_MINORS 1 #define NR_SECTORS 128
#define KERNEL_SECTOR_SIZE 512
#define USE_BIO_TRANSFER 0
static struct my_block_dev { struct blk_mq_tag_set tag_set; struct request_queue *queue; struct gendisk *gd; u8 *data; size_t size; } g_dev;
static int my_block_open(struct block_device *bdev, fmode_t mode) { return 0; }
static void my_block_release(struct gendisk *gd, fmode_t mode) { }
static const struct block_device_operations my_block_ops = { .owner = THIS_MODULE, .open = my_block_open, .release = my_block_release };
static void my_block_transfer(struct my_block_dev *dev, sector_t sector, unsigned long len, char *buffer, int dir) { unsigned long offset = sector * KERNEL_SECTOR_SIZE;
if ((offset + len) > dev->size) return;
if(dir == 1){ memcpy(dev->data + offset,buffer,len); } else{ memcpy(buffer,dev->data + offset,len); } }
#if USE_BIO_TRANSFER == 1 static void my_xfer_request(struct my_block_dev *dev, struct request *req) { struct bio_vec bvec; struct req_iterator iter; rq_for_each_segment(bvec,req,iter){ sector_t sector = iter.iter.bi_sector; unsigned long offset = bvec.bv_offset; size_t len = bvec.bv_len; int dir = bio_data_dir(iter.bio); char *buffer = kmap_atomic(bvec.bv_page); printk(KERN_LOG_LEVEL "%s: buf %8p offset %lu len %u dir %d\n", __func__, buffer, offset, len, dir);
my_block_transfer(dev, sector, len, buffer + offset, dir); kunmap_atomic(buffer); } } #endif
static blk_status_t my_block_request(struct blk_mq_hw_ctx *hctx, const struct blk_mq_queue_data *bd) { struct request *rq; struct my_block_dev *dev = hctx->queue->queuedata;
rq = bd->rq; blk_mq_start_request(rq); if(blk_rq_is_passthrough(rq)){ printk (KERN_NOTICE "Skip non-fs request\n"); blk_mq_end_request(rq, BLK_STS_IOERR); goto out; } printk(KERN_LOG_LEVEL "request received: pos=%llu bytes=%u " "cur_bytes=%u dir=%c\n", (unsigned long long) blk_rq_pos(rq), blk_rq_bytes(rq), blk_rq_cur_bytes(rq), rq_data_dir(rq) ? 'W' : 'R');
#if USE_BIO_TRANSFER == 1 my_xfer_request(dev,rq) #else my_block_transfer(dev,blk_rq_pos(rq),blk_rq_bytes(rq),bio_data(rq->bio),rq_data_dir(rq)); #endif
blk_mq_end_request(rq, BLK_STS_OK); out: return BLK_STS_OK; }
static struct blk_mq_ops my_queue_ops = { .queue_rq = my_block_request, };
static int create_block_device(struct my_block_dev *dev) { int err;
dev->size = NR_SECTORS * KERNEL_SECTOR_SIZE; dev->data = vmalloc(dev->size); if (dev->data == NULL) { printk(KERN_ERR "vmalloc: out of memory\n"); err = -ENOMEM; goto out_vmalloc; }
dev->tag_set.ops = &my_queue_ops; dev->tag_set.nr_hw_queues = 1; dev->tag_set.queue_depth = 128; dev->tag_set.numa_node = NUMA_NO_NODE; dev->tag_set.cmd_size = 0; dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; err = blk_mq_alloc_tag_set(&dev->tag_set); if (err) { printk(KERN_ERR "blk_mq_alloc_tag_set: can't allocate tag set\n"); goto out_alloc_tag_set; }
dev->queue = blk_mq_init_queue(&dev->tag_set); if (IS_ERR(dev->queue)) { printk(KERN_ERR "blk_mq_init_queue: out of memory\n"); err = -ENOMEM; goto out_blk_init; } blk_queue_logical_block_size(dev->queue, KERNEL_SECTOR_SIZE); dev->queue->queuedata = dev;
dev->gd = alloc_disk(MY_BLOCK_MINORS); if (!dev->gd) { printk(KERN_ERR "alloc_disk: failure\n"); err = -ENOMEM; goto out_alloc_disk; }
dev->gd->major = MY_BLOCK_MAJOR; dev->gd->first_minor = 0; dev->gd->fops = &my_block_ops; dev->gd->queue = dev->queue; dev->gd->private_data = dev; snprintf(dev->gd->disk_name, DISK_NAME_LEN, "myblock"); set_capacity(dev->gd, NR_SECTORS);
add_disk(dev->gd);
return 0;
out_alloc_disk: blk_cleanup_queue(dev->queue); out_blk_init: blk_mq_free_tag_set(&dev->tag_set); out_alloc_tag_set: vfree(dev->data); out_vmalloc: return err; }
static int __init my_block_init(void) { int err = 0;
int status = register_blkdev(MY_BLOCK_MAJOR,MY_BLKDEV_NAME); if(status < 0){ printk(KERN_ERR "unable to register mybdev block device\n"); return -EBUSY; } err = create_block_device(&g_dev); return 0;
out: unregister_blkdev(MY_BLOCK_MAJOR, MY_BLKDEV_NAME); return err; }
static void delete_block_device(struct my_block_dev *dev) { if (dev->gd) { del_gendisk(dev->gd); put_disk(dev->gd); }
if (dev->queue) blk_cleanup_queue(dev->queue); if (dev->tag_set.tags) blk_mq_free_tag_set(&dev->tag_set); if (dev->data) vfree(dev->data); }
static void __exit my_block_exit(void) { delete_block_device(&g_dev); unregister_blkdev(MY_BLOCK_MAJOR, MY_BLKDEV_NAME); }
module_init(my_block_init); module_exit(my_block_exit);
|