#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /*printk*/
#include <linux/init.h> /*__init __exit*/
#include <linux/fs.h> /* file_operation*/
#include <linux/blkdev.h> /*block dev*/
#include <linux/uaccess.h> /* copy_to_user, copy_from_user*/
#include <linux/major.h>
#include <linux/genhd.h>
static void simp_blkdev_do_request(struct request_queue *q);
#define SIMP_BLOCK_MAJOR COMPAQ_SMART2_MAJOR //占用系统已经分配的major号
#define SIMP_BLKDEV_BYTES (16*1024*1024)
struct gendisk *simp_blkdev_disk; <linux/gehhd.h>
struct request_queue *simp_blkdev_queue; <linux/blkdev.h>
static int simp_blkdev_data[SIMP_BLKDEV_BYTES];
struct block_device_operations blkops = {
.owner = THIS_MODULE
};<linux/fs.h>
static int __init simp_blkdev_init(void)
{
simp_blkdev_queue = blk_init_queue(simp_blkdev_do_request,NULL); <linux/blkdev.h>
if(!simp_blkdev_queue)
return 1;
strcpy(simp_blkdev_dis->name,"myDev"); //这句话还是非常必须的 ,否则 这个导致每次加载都死机且键盘指示灯还不停的闪~~~
simp_blkdev_disk = alloc_disk(1); <linux/genhd.h>
simp_blkdev_disk->major = SIMP_BLOCK_MAJOR;
simp_blkdev_disk->first_minor = 0;
simp_blkdev_disk->fops = &blkops;
simp_blkdev_disk->queue = simp_blkdev_queue;
set_capacity(simp_blkdev_disk, SIMP_BLKDEV_BYTES>>9); <linux/genhd.h>
add_disk(simp_blkdev_disk);<linux/genhd.h>
return 0;
}
static void __exit simp_blkdev_exit(void)
{
del_gendisk(simp_blkdev_disk);
put_disk(simp_blkdev_disk);
blk_cleanup_queue(simp_blkdev_queue);
}
module_init(simp_blkdev_init);
module_exit(simp_blkdev_exit);
static void simp_blkdev_do_request(struct request_queue *q)
{
struct request *req;
while((req = elv_next_request(q)) != NULL){
if(((req->sector+req->current_nr_sectors)<<9) > SIMP_BLKDEV_BYTES)
{
end_request(req,0);
continue;
}
switch(rq_data_dir(req)) {
case READ:
memcpy(req->buffer, simp_blkdev_data+(req->sector<<9),req->current_nr_sectors<<9);
end_request(req,1);
break;
case WRITE:
memcpy(simp_blkdev_data+(req->sector<<9), req->buffer,req->current_nr_sectors<<9); //这儿写掉<<9而没挂上!!
end_request(req,1);
default:
break;
}
}
}
生成文件系统: mkfs.ext3 /dev/myDev
挂载: mount /dev/myDev /mnt
卸载: umount /mnt
查看文件系统信息: fdisk -l /dev/myDev