#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int ramdisk_major = 0;
module_param(ramdisk_major, int, 0);
static int hardsect_size = 512;
module_param(hardsect_size, int, 0);
static int nsectors = 1024;
module_param(nsectors, int, 0);
#define RAMDISK_MINORS 16
struct ramdisk_dev {
int size;
int users;
u8 *data;
spinlock_t lock;
struct gendisk *gd;
struct request_queue *queue;
};
static struct ramdisk_dev *Devices = NULL;
static void ramdisk_transfer(struct ramdisk_dev *dev, unsigned long sector,
unsigned long nsect, char *buffer, int write)
{
unsigned long offset = sector * hardsect_size;
unsigned long nbytes = nsect * hardsect_size;
if ((offset + nbytes) > dev->size) {
printk (KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes);
return;
}
if (write)
memcpy(dev->data + offset, buffer, nbytes);
else
memcpy(buffer, dev->data + offset, nbytes);
}
static int ramdisk_xfer_bio(struct ramdisk_dev *dev, struct bio *bio)
{
int index;
struct bio_vec *bvec;
sector_t sector = bio->bi_sector; //bio的起始扇区号
bio_for_each_segment(bvec, bio, index) { //遍历bio里面的每一个bvec
char *buffer = __bio_kmap_atomic(bio, index);//bvec bv_page成员转内核态虚拟地址
unsigned len = bvec->bv_len >> 9;//当前bvec的扇区数
ramdisk_transfer(dev, sector, len, buffer, bio_data_dir(bio) == WRITE);
sector += len; //扇区数累加
__bio_kunmap_atomic(bio); //解除映射
}
return 0;
}
static int ramdisk_make_request(struct request_queue *q, struct bio *bio)
{
struct ramdisk_dev *dev = q->queuedata;
int status = ramdisk_xfer_bio(dev, bio);
bio_endio(bio, status);
}
int ramdisk_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)
{
printk("ramdisk_ioctl \n");
return 0;
}
static int ramdisk_open(struct block_device *bdev, fmode_t mode)
{
struct ramdisk_dev *dev = bdev->bd_disk->private_data;
spin_lock(&dev->lock);
if (!dev->users)
check_disk_change(bdev);
++dev->users;
printk("ramdisk_open, dev->users = %d\n", dev->users);
spin_unlock(&dev->lock);
return 0;
}
static void ramdisk_release(struct gendisk *disk, fmode_t mode)
{
struct ramdisk_dev *dev = disk->private_data;
spin_lock(&dev->lock);
--dev->users;
printk("ramdisk_release, dev->users = %d\n", dev->users);
spin_unlock(&dev->lock);
}
static struct block_device_operations ramdisk_ops = {
.owner = THIS_MODULE,
.ioctl = ramdisk_ioctl,
.open = ramdisk_open,
.release = ramdisk_release,
};
static void setup_device(struct ramdisk_dev *dev)
{
memset (dev, 0, sizeof (struct ramdisk_dev));
dev->size = nsectors * hardsect_size;
dev->data = vmalloc(dev->size);
if (dev->data == NULL) {
printk (KERN_NOTICE "vmalloc failure.\n");
return;
}
spin_lock_init(&dev->lock);
dev->queue = blk_alloc_queue(GFP_KERNEL);
if (dev->queue == NULL)
goto out_vfree;
blk_queue_make_request(dev->queue, ramdisk_make_request);
dev->queue->queuedata = dev;
dev->gd = alloc_disk(RAMDISK_MINORS);
if (!dev->gd) {
printk (KERN_NOTICE "alloc_disk failure\n");
goto out_alloc_disk;
}
dev->gd->major = ramdisk_major;
dev->gd->first_minor = RAMDISK_MINORS;
dev->gd->fops = &ramdisk_ops;
dev->gd->queue = dev->queue;
dev->gd->private_data = dev;
snprintf (dev->gd->disk_name, 32, "ramdisk%c", 'a');
set_capacity(dev->gd, nsectors);
add_disk(dev->gd);
return;
out_alloc_disk:
blk_cleanup_queue(dev->queue);
dev->queue = NULL;
out_vfree:
if (dev->data)
vfree(dev->data);
}
static int __init ramdisk_init(void)
{
ramdisk_major = register_blkdev(ramdisk_major, "ramdisk");
if (ramdisk_major <= 0) {
printk(KERN_WARNING "ramdisk: unable to get major number\n");
return -EBUSY;
}
Devices = kmalloc(sizeof(struct ramdisk_dev), GFP_KERNEL);
if(Devices == NULL)
goto out_unregister;
setup_device(Devices);
return 0;
out_unregister:
unregister_blkdev(ramdisk_major, "ramdisk");
return -ENOMEM;
}
static void __exit ramdisk_exit(void)
{
if(Devices->gd)
del_gendisk(Devices->gd);
if(Devices->queue)
blk_cleanup_queue(Devices->queue);
if(Devices->data)
vfree(Devices->data);
kfree(Devices);
unregister_blkdev(ramdisk_major, "ramdisk");
}
module_init(ramdisk_init);
module_exit(ramdisk_exit);
Makefile:
# SPDX-License-Identifier: GPL-2.0
obj-m += ram.o
ram-objs := ramdisk.o
all:
make -C /usr/src/linux-headers-$(shell uname -r) M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
代码在linux3.13版本下可以编译通过,装载驱动以后,可以用fio工具进行读写测试。