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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
| #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/stat.h> #include <linux/init.h> #include <linux/device.h> #include <linux/blk_types.h> #include <linux/blkdev.h> #include <linux/blk-mq.h> #include <uapi/linux/hdreg.h> #include <uapi/linux/cdrom.h>
#ifndef SUCCESS #define SUCCESS 0 #endif
#ifndef SECTOR_SHIFT #define SECTOR_SHIFT 9 #endif #ifndef SECTOR_SIZE #define SECTOR_SIZE (1 << SECTOR_SHIFT) #endif
static const char* _hwhdev_name = "hwhdev"; static const size_t _hwhdev_buffer_size = 32 *1024 * PAGE_SIZE;
typedef struct hwhdev_cmd_s { } hwhdev_cmd_t;
typedef struct hwhdev_device_s { sector_t capacity; u8* data; atomic_t open_counter;
struct blk_mq_tag_set tag_set; struct request_queue *queue;
struct gendisk *disk; } hwhdev_device_t;
static int _hwhdev_major = 0; static hwhdev_device_t* _hwhdev_device = NULL;
static int hwhdev_allocate_buffer(hwhdev_device_t* dev) { dev->capacity = _hwhdev_buffer_size >> SECTOR_SHIFT; dev->data = vmalloc(dev->capacity << SECTOR_SHIFT); if (dev->data == NULL) { printk(KERN_WARNING "hwhdev: vmalloc failure.\n"); return -ENOMEM; }
return SUCCESS; }
static void hwhdev_free_buffer(hwhdev_device_t* dev) { if (dev->data) { vfree(dev->data);
dev->data = NULL; dev->capacity = 0; } }
static void hwhdev_remove_device(void) { hwhdev_device_t* dev = _hwhdev_device; if (dev == NULL) return;
if (dev->disk) del_gendisk(dev->disk);
if (dev->queue) { blk_cleanup_queue(dev->queue); dev->queue = NULL; }
if (dev->tag_set.tags) blk_mq_free_tag_set(&dev->tag_set);
if (dev->disk) { put_disk(dev->disk); dev->disk = NULL; }
hwhdev_free_buffer(dev);
vfree(dev); _hwhdev_device = NULL;
printk(KERN_WARNING "hwhdev: simple block device was removed\n"); }
static int do_simple_request(struct request *rq, unsigned int *nr_bytes) { int ret = SUCCESS; struct bio_vec bvec; struct req_iterator iter; hwhdev_device_t *dev = rq->q->queuedata; loff_t pos = blk_rq_pos(rq) << SECTOR_SHIFT; loff_t dev_size = (loff_t)(dev->capacity << SECTOR_SHIFT);
printk(KERN_WARNING "hwhdev: request start from sector %lld \n", blk_rq_pos(rq));
rq_for_each_segment(bvec, rq, iter) { unsigned long b_len = bvec.bv_len;
void* b_buf = page_address(bvec.bv_page) + bvec.bv_offset;
if ((pos + b_len) > dev_size) b_len = (unsigned long)(dev_size - pos);
if (rq_data_dir(rq)) memcpy(dev->data + pos, b_buf, b_len); else memcpy(b_buf, dev->data + pos, b_len);
pos += b_len; *nr_bytes += b_len; }
return ret; }
static blk_status_t _queue_rq(struct blk_mq_hw_ctx *hctx, const struct blk_mq_queue_data* bd) { unsigned int nr_bytes = 0; blk_status_t status = BLK_STS_OK; struct request *rq = bd->rq;
blk_mq_start_request(rq);
if (do_simple_request(rq, &nr_bytes) != SUCCESS) status = BLK_STS_IOERR;
printk(KERN_WARNING "hwhdev: request process %d bytes\n", nr_bytes);
#if 0 blk_mq_end_request(rq, status); #else if (blk_update_request(rq, status, nr_bytes)) BUG(); __blk_mq_end_request(rq, status); #endif
return BLK_STS_OK; }
static struct blk_mq_ops _mq_ops = { .queue_rq = _queue_rq, };
static int _open(struct block_device *bdev, fmode_t mode) { hwhdev_device_t* dev = bdev->bd_disk->private_data; if (dev == NULL) { printk(KERN_WARNING "hwhdev: invalid disk private_data\n"); return -ENXIO; }
atomic_inc(&dev->open_counter);
printk(KERN_WARNING "hwhdev: device was opened\n");
return SUCCESS; } static void _release(struct gendisk *disk, fmode_t mode) { hwhdev_device_t* dev = disk->private_data; if (dev) { atomic_dec(&dev->open_counter);
printk(KERN_WARNING "hwhdev: device was closed\n"); } else printk(KERN_WARNING "hwhdev: invalid disk private_data\n"); }
static int _getgeo(hwhdev_device_t* dev, struct hd_geometry* geo) { sector_t quotient;
geo->start = 0; if (dev->capacity > 63) {
geo->sectors = 63; quotient = (dev->capacity + (63 - 1)) / 63;
if (quotient > 255) { geo->heads = 255; geo->cylinders = (unsigned short)((quotient + (255 - 1)) / 255); } else { geo->heads = (unsigned char)quotient; geo->cylinders = 1; } } else { geo->sectors = (unsigned char)dev->capacity; geo->cylinders = 1; geo->heads = 1; } return SUCCESS; }
static int _ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { int ret = -ENOTTY; hwhdev_device_t* dev = bdev->bd_disk->private_data;
printk(KERN_WARNING "hwhdev: ioctl %x received\n", cmd);
switch (cmd) { case HDIO_GETGEO: { struct hd_geometry geo;
ret = _getgeo(dev, &geo ); if (copy_to_user((void *)arg, &geo, sizeof(struct hd_geometry))) ret = -EFAULT; else ret = SUCCESS; break; } case CDROM_GET_CAPABILITY: { struct gendisk *disk = bdev->bd_disk;
if (bdev->bd_disk && (disk->flags & GENHD_FL_CD)) ret = SUCCESS; else ret = -EINVAL; break; } }
return ret; } #ifdef CONFIG_COMPAT static int _compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { return -ENOTTY; } #endif
static const struct block_device_operations _fops = { .owner = THIS_MODULE, .open = _open, .release = _release, .ioctl = _ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = _compat_ioctl, #endif };
static int hwhdev_add_device(void) { int ret = SUCCESS;
hwhdev_device_t* dev = kzalloc(sizeof(hwhdev_device_t), GFP_KERNEL); if (dev == NULL) { printk(KERN_WARNING "hwhdev: unable to allocate %ld bytes\n", sizeof(hwhdev_device_t)); return -ENOMEM; } _hwhdev_device = dev;
do{ ret = hwhdev_allocate_buffer(dev); if(ret) break;
#if 0 { struct request_queue *queue;
dev->tag_set.cmd_size = sizeof(hwhdev_cmd_t); dev->tag_set.driver_data = dev;
queue = blk_mq_init_sq_queue(&dev->tag_set, &_mq_ops, 128, BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE); if (IS_ERR(queue)) { ret = PTR_ERR(queue); printk(KERN_WARNING "hwhdev: unable to allocate and initialize tag set\n"); break; } dev->queue = queue; } #else { dev->tag_set.ops = &_mq_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 = sizeof(hwhdev_cmd_t); dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE ; dev->tag_set.driver_data = dev;
ret = blk_mq_alloc_tag_set(&dev->tag_set); if (ret) { printk(KERN_WARNING "hwhdev: unable to allocate tag set\n"); break; } }
{ struct request_queue *queue = blk_mq_init_queue(&dev->tag_set); if (IS_ERR(queue)) { ret = PTR_ERR(queue); printk(KERN_WARNING "hwhdev: Failed to allocate queue\n"); break; } dev->queue = queue; } #endif dev->queue->queuedata = dev;
{ struct gendisk *disk = alloc_disk(1); if (disk == NULL) { printk(KERN_WARNING "hwhdev: Failed to allocate disk\n"); ret = -ENOMEM; break; }
disk->flags |= GENHD_FL_NO_PART_SCAN; disk->flags |= GENHD_FL_REMOVABLE;
disk->major = _hwhdev_major; disk->first_minor = 0; disk->fops = &_fops; disk->private_data = dev; disk->queue = dev->queue; sprintf(disk->disk_name, "hwhdev%d", 0); set_capacity(disk, dev->capacity);
dev->disk = disk; add_disk(disk); }
printk(KERN_WARNING "hwhdev: simple block device was created\n"); }while(false);
if (ret){ hwhdev_remove_device(); printk(KERN_WARNING "hwhdev: Failed add block device\n"); }
return ret; }
static int __init hwhdev_init(void) { int ret = SUCCESS;
_hwhdev_major = register_blkdev(_hwhdev_major, _hwhdev_name); if (_hwhdev_major <= 0){ printk(KERN_WARNING "hwhdev: unable to get major number\n"); return -EBUSY; }
ret = hwhdev_add_device(); if (ret) unregister_blkdev(_hwhdev_major, _hwhdev_name);
return ret; }
static void __exit hwhdev_exit(void) { hwhdev_remove_device();
if (_hwhdev_major > 0) unregister_blkdev(_hwhdev_major, _hwhdev_name); }
module_init(hwhdev_init); module_exit(hwhdev_exit);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Code Imp");
|