点击(此处)折叠或打开
-
/* 参考:
-
* drivers\block\xd.c
-
* drivers\block\z2ram.c
-
*/
-
-
#include <linux/module.h>
-
#include <linux/errno.h>
-
#include <linux/interrupt.h>
-
#include <linux/mm.h>
-
#include <linux/fs.h>
-
#include <linux/kernel.h>
-
#include <linux/timer.h>
-
#include <linux/genhd.h>
-
#include <linux/hdreg.h>
-
#include <linux/ioport.h>
-
#include <linux/init.h>
-
#include <linux/wait.h>
-
#include <linux/blkdev.h>
-
#include <linux/blkpg.h>
-
#include <linux/delay.h>
-
#include <linux/io.h>
-
-
#include <asm/system.h>
-
#include <asm/uaccess.h>
-
#include <asm/dma.h>
-
-
static struct gendisk *ramblock_disk;
-
static struct request_queue *ramblock_queue;
-
-
static int major;
-
-
static DEFINE_SPINLOCK(ramblock_lock);//自旋锁
-
-
#define RAMBLOCK_SIZE (1024*1024)
-
static unsigned char *ramblock_buf;
-
//获得几何信息函数,把几何信息保存在geo结构体中。
-
//为了测试的时候可以使用分区工具fdisk,假装内存也具有一些几何信息
-
static int ramblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
-
{
-
/* 块设备容量(字节为单位)=heads*cylinders*sectors*512 ,其中sector为一环里面的扇区数*/
-
geo->heads = 2; //磁头个数,或面数
-
geo->cylinders = 32; //柱面数或环数
-
geo->sectors = RAMBLOCK_SIZE/2/32/512;//一环里面的扇区数,
-
//根据:块设备容量(字节为单位)=heads*cylinders*sectors*512,
-
//模拟磁盘才可以这样算,实际的磁盘这些信息都是固定的。
-
return 0;
-
}
-
-
-
static struct block_device_operations ramblock_fops = {
-
.owner = THIS_MODULE,
-
.getgeo = ramblock_getgeo,
-
};
-
-
//这里要处理的是保存在request_queue *q 队列里面的一系列读写请求,不是某一次某一个,而是多
-
//个在这里统一优化,处理。这是块设备特殊的地方,不立即读写,合并为一大块读写等等以提高效率。
-
static void do_ramblock_request(struct request_queue * q)
-
{
-
struct request *req;
-
-
req = blk_fetch_request(q);
-
while (req) {
-
unsigned long start = blk_rq_pos(req) << 9;
-
unsigned long len = blk_rq_cur_bytes(req);
-
int err = 0;
-
-
if (start + len > RAMBLOCK_SIZE) {
-
pr_err("ramblock" ": bad access: block=%llu, "
-
"count=%u\n",
-
(unsigned long long)blk_rq_pos(req),
-
blk_rq_cur_sectors(req));
-
err = -EIO;
-
goto done;
-
}
-
while (len) {
-
//unsigned long addr = start & Z2RAM_CHUNKMASK;
-
//unsigned long size = Z2RAM_CHUNKSIZE - addr;
-
//if (len < size)
-
//size = len;
-
//addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
-
if (rq_data_dir(req) == READ)
-
memcpy(req->buffer, ramblock_buf+start, len);
-
else
-
memcpy(ramblock_buf+start, req->buffer, len);
-
//start += size;
-
//len -= size;
-
len -= len;
-
}
-
done:
-
if (!__blk_end_request_cur(req, err))
-
req = blk_fetch_request(q);
-
}
-
-
-
#if 0
-
static int r_cnt = 0;
-
static int w_cnt = 0;
-
struct request *req;
-
-
//printk("do_ramblock_request %d\n", ++cnt);
-
-
//利用elv_next_request(q)函数取出优化后的一个个request结构体,解析,进行实际读写
-
while ((req = blk_fetch_request(q)) != NULL) {
-
-
/* 数据传输三要素: 源,目的,长度 */
-
-
/* 源/目的: */
-
//unsigned long offset = req->__sector * 512;
-
-
unsigned long offset =blk_rq_pos(req);
-
//req->sector的含义是:当前这个req要从第sector个扇区开始读/写数据。
-
//这些东西内核优化队列的时候会设置好,我们拿来用就行
-
-
/* 目的/源: */
-
// req->buffer
-
-
/* 长度: */
-
//unsigned long len = req->current_nr_sectors * 512; //字节为单位
-
-
unsigned long len = blk_rq_bytes (req);
-
-
//利用rq_data_dir(req)函数获得这个req的读写操作标志,分别处理读和写操作。
-
if (rq_data_dir(req) == READ)
-
{
-
//printk("do_ramblock_request read %d\n", ++r_cnt);
-
memcpy(req->buffer, ramblock_buf+offset, len);
-
//数据从ramblock_buf+offset读到req->buffer
-
}
-
else
-
{
-
//printk("do_ramblock_request write %d\n", ++w_cnt);
-
memcpy(ramblock_buf+offset, req->buffer, len);
-
//数据从req->buffer写到ramblock_buf+offset
-
}
-
-
__blk_end_request_all(req, 1);//通知完成当前请求成功,end_request()会将当前请求从请求
-
//队列中剥离
-
}
-
#endif
-
}
-
-
static int ramblock_init(void)
-
{
-
/* 1. 分配一个gendisk结构体 */
-
ramblock_disk = alloc_disk(16); /* 次设备号个数: 分区个数+1 */
-
-
/* 2. 设置 */
-
/* 2.1 分配/设置队列 */
-
ramblock_queue = blk_init_queue(do_ramblock_request, &ramblock_lock);
-
ramblock_disk->queue = ramblock_queue;
-
-
/* 2.2 设置其他属性: 比如容量 */
-
major = register_blkdev(0, "ramblock"); /* cat /proc/devices */
-
ramblock_disk->major = major;
-
ramblock_disk->first_minor = 0;
-
sprintf(ramblock_disk->disk_name, "ramblock");
-
ramblock_disk->fops = &ramblock_fops;
-
set_capacity(ramblock_disk, RAMBLOCK_SIZE / 512);
-
-
/* 3. 硬件相关操作 */
-
ramblock_buf = kzalloc(RAMBLOCK_SIZE, GFP_KERNEL);
-
//ramblock_buf指向块设备起始地址
-
-
/* 4. 注册 */
-
add_disk(ramblock_disk);
-
-
return 0;
-
}
-
-
static void ramblock_exit(void)
-
{
-
unregister_blkdev(major, "ramblock");
-
del_gendisk(ramblock_disk);
-
put_disk(ramblock_disk);
-
blk_cleanup_queue(ramblock_queue);
-
-
kfree(ramblock_buf);
-
}
-
-
module_init(ramblock_init);
-
module_exit(ramblock_exit);
-
- MODULE_LICENSE("GPL");