上一节介绍了块设备子系统常用的数据结构和常用的接口api函数
内核驱动里面使用ramdisk,即系统的内存来构造一个虚拟的设备,在这个块设备里面做一些读写的操作,和对request做一些简单的操作,
ramdisk-driver.c
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/blkdev.h>
#include <linux/genhd.h>
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/version.h>
#define MY_DEVICE_NAME "myramdisk"
static int mybdrv_ma_no, diskmb = 256, disk_size;
static char *ramdisk;
static struct gendisk *my_gd;
static spinlock_t lock;
static unsigned short sector_size = 512;
static struct request_queue *my_request_queue;
module_param_named(size, diskmb, int, 0);
static void my_request(struct request_queue *q)
{
struct request *rq;
int size, res = 0;
char *ptr;
unsigned nr_sectors, sector;
pr_info("start handle request\n");
rq = blk_fetch_request(q);
while (rq) {
nr_sectors = blk_rq_cur_sectors(rq);
sector = blk_rq_pos(rq);
ptr = ramdisk + sector * sector_size;
size = nr_sectors * sector_size;
if ((ptr + size) > (ramdisk + disk_size)) {
pr_err("end of device\n");
goto done;
}
if (rq_data_dir(rq)) {
pr_info("writing at sector %d, %u sectors\n",
sector, nr_sectors);
memcpy(ptr, bio_data(rq->bio), size);
} else {
pr_info("reading at sector %d, %u sectors\n",
sector, nr_sectors);
memcpy(bio_data(rq->bio), ptr, size);
}
done:
if (!__blk_end_request_cur(rq, res))
rq = blk_fetch_request(q);
}
pr_info("handle request done\n");
}
static int my_ioctl(struct block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg)
{
long size;
struct hd_geometry geo;
pr_info("cmd=%d\n", cmd);
switch (cmd) {
case HDIO_GETGEO:
pr_info("HIT HDIO_GETGEO\n");
size = disk_size;
size &= ~0x3f;
geo.cylinders = size>>6;
geo.heads = 2;
geo.sectors = 16;
geo.start = 4;
if (copy_to_user((void __user *)arg, &geo, sizeof(geo)))
return -EFAULT;
return 0;
}
pr_warn("return -ENOTTY\n");
return -ENOTTY;
}
static const struct block_device_operations mybdrv_fops = {
.owner = THIS_MODULE,
.ioctl = my_ioctl,
};
static int __init my_init(void)
{
disk_size = diskmb * 1024 * 1024;
spin_lock_init(&lock);
ramdisk = vmalloc(disk_size);
if (!ramdisk)
return -ENOMEM;
my_request_queue = blk_init_queue(my_request, &lock);
if (!my_request_queue) {
vfree(ramdisk);
return -ENOMEM;
}
blk_queue_logical_block_size(my_request_queue, sector_size);
mybdrv_ma_no = register_blkdev(0, MY_DEVICE_NAME);
if (mybdrv_ma_no < 0) {
pr_err("Failed registering mybdrv, returned %d\n",
mybdrv_ma_no);
vfree(ramdisk);
return mybdrv_ma_no;
}
my_gd = alloc_disk(16);
if (!my_gd) {
unregister_blkdev(mybdrv_ma_no, MY_DEVICE_NAME);
vfree(ramdisk);
return -ENOMEM;
}
my_gd->major = mybdrv_ma_no;
my_gd->first_minor = 0;
my_gd->fops = &mybdrv_fops;
strcpy(my_gd->disk_name, MY_DEVICE_NAME);
my_gd->queue = my_request_queue;
set_capacity(my_gd, disk_size / sector_size);
add_disk(my_gd);
pr_info("device successfully registered, Major No. = %d\n",
mybdrv_ma_no);
pr_info("Capacity of ram disk is: %d MB\n", diskmb);
return 0;
}
static void __exit my_exit(void)
{
del_gendisk(my_gd);
put_disk(my_gd);
unregister_blkdev(mybdrv_ma_no, MY_DEVICE_NAME);
pr_info("module successfully unloaded, Major No. = %d\n", mybdrv_ma_no);
blk_cleanup_queue(my_request_queue);
vfree(ramdisk);
}
module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("Benshushu");
MODULE_LICENSE("GPL v2");
Makefile
BASEINCLUDE ?= /lib/modules/$(shell uname -r)/build
oops-objs := ramdisk-driver.o
KBUILD_CFLAGS +=-g -O0
obj-m := ramdisk_driver.o
all :
$(MAKE) -C $(BASEINCLUDE) SUBDIRS=$(PWD) modules;
install:
$(MAKE) -C $(BASEINCLUDE) SUBDIRS=$(PWD) modules_install;
clean:
$(MAKE) -C $(BASEINCLUDE) SUBDIRS=$(PWD) clean;
rm -f *.ko;
编译并插入内核模块,输入dmesg命令查看
可以看到它的主设备号是252,ram disk的容量是256MB
可以查看/dev目录下的设备
这时候可以把ramdisk给格式化
sudo mkfs.ext4 /dev/myramdisk
查看dmesg
可以看到dmesg里面打印了很多信息
每一次request处理的时候,入口会打印一个start handle request
writing at sector 409600,8 sectors 这里409600是一个sector在整个ramdisk的一个其实的指针,相当于文件的一个指针,8是这次操作操作了8个sector
格式化完之后我们可以对它进行mount操作
sudo mount /dev/myramdisk /mnt/
进入到/mnt目录下
可以使用df命令来查看系统挂载的分区
可以看到 /dev/myramdisk挂载到/mnt里面
创建一个文件夹mytest,再看一下dmesg的信息
这些dmesg信息就是创建mytest目录的时候它发起的一些块设备的IO操作,是块设备驱动里面处理这些来自文件系统的请求所做的一些最底层的操作了
|