#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/slab.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/timer.h> #include <linux/types.h> /* size_t */ #include <linux/fcntl.h> /* O_ACCMODE */ #include <linux/hdreg.h> /* HDIO_GETGEO */ #include <linux/kdev_t.h> #include <linux/vmalloc.h> #include <linux/genhd.h> #include <linux/blkdev.h> #include <linux/buffer_head.h> /* invalidate_bdev */ #include <linux/bio.h> static int sbull_major = 0;/* 块设备号,0就自动分配*/ module_param(sbull_major, int, 0);//模参数,这个主设备号可以在加载模块时指定一个值 static int hardsect_size = 512; /* 硬件扇区大小 */ module_param(hardsect_size, int, 0); /* 同上 */ static int nsectors = 1024;/* 硬件扇区数目 */ module_param(nsectors, int, 0); /* 同上 */ static int ndevices = 4; module_param(ndevices,int,0); /* The different "request modes " we can use. */ enum { RM_SIMPLE = 0, RM_FULL = 1, RM_NOQUEUE = 2, }; static int request_mode = RM_SIMPLE; module_param(request_mode, int, 0); #define SBULL_MINORS 16 #define KERNEL_SECTOR_SIZE 512 #define INVALIDATE_DELAY 30*HZ /*sbull 设备结构*/ struct sbull_dev{ int size; /* 以扇区为单位, 设备的大小 */ u8 *data; /* 数据数组 */ short users; /* 用户数目 */ short media_change; /* 介质改变标志 */ spinlock_t lock; /* 用于互斥 */ struct request_queue *queue; /* 设备请求队列 */ struct gendisk *gd; /* gendisk结构 */ struct timer_list timer; /* 用来模拟介质改变 */ }; static struct sbull_dev *Devices = NULL; /* * Handle an I/O request. 处理 I/O 拷贝数据的 函数 */ static void sbull_transfer(struct sbull_dev *dev, unsigned long sector, unsigned long nsect, char *buffer, int write) { unsigned long offset = sector * KERNEL_SECTOR_SIZE; unsigned long nbytes = nsect * KERNEL_SECTOR_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); } /* * The simple form of the request function. */ static void sbull_request(struct request_queue *q) { struct request *req; //定义请求结构体 req = blk_fetch_request(q); while(req != NULL){ struct sbull_dev *dev = req->rq_disk->private_data;//获得队列中第一个未完成请求 if(req->cmd_type != REQ_TYPE_FS){ //判断是否为文件系统请求 printk(KERN_NOTICE "Skip non-fs request\n"); blk_end_request_all(req, -EIO); //通知请求处理失败,EIO为i/o error. continue; } sbull_transfer(dev, blk_rq_pos(req), //扇区光标所在的位置 blk_rq_cur_sectors(req), //需要传输的扇区数目 req->buffer, //要传输或者接受的数据缓存区 rq_data_dir(req)); //获取传输方向,0表示读,1表示写 if(!__blk_end_request_cur(req, 0)){ //下一个请求 req = NULL; } } } /* * Transfer a single BIO. bio处理函数 */ static int sbull_xfer_bio(struct sbull_dev *dev, struct bio *bio) { int i; struct bio_vec *bvec; //定义实际的 vec 列表 sector_t sector = bio->bi_sector; //定义要传输的第一个扇区 bio_for_each_segment(bvec,bio,i){ //下面的宏遍历bio的每一段,获得一个内核虚拟地址来存取缓冲 char *buffer = __bio_kmap_atomic(bio,i,KM_USER0);//通过kmap_atomic()函数获得返回bio的第i个缓冲区的虚拟地址 sbull_transfer(dev, sector,// 开始扇区的索引号 bio_cur_bytes(bio)/KERNEL_SECTOR_SIZE, // 需要传输的扇区数 buffer, // 传输数据的缓冲区指针 bio_data_dir(bio) == WRITE); // 传输方向,0表述从设备读,非0从设备写 sector += bio_cur_bytes(bio)/KERNEL_SECTOR_SIZE;//返回扇区数 __bio_kunmap_atomic(bio,KM_USER0);//返回由 __bio_kmap_atomic()获得的内核虚拟地址 } return 0; } /* * Transfer a full request.请求处理函数 */ static int sbull_xfer_request(struct sbull_dev *dev, struct request *req) { struct bio *bio; int nsect = 0; __rq_for_each_bio(bio, req) { //此宏遍历请求中的每个bio,传递用于sbull_xfer_bio()传输的指针 sbull_xfer_bio(dev, bio); //调用 bio 处理函数 nsect += bio->bi_size / KERNEL_SECTOR_SIZE; //传递的字节数/扇区大小等于扇区数 } return nsect; } /* * Smarter request function that "handles clustering". */ static void sbull_full_request(struct request_queue *q) { struct request *req; struct sbull_dev *dev = q->queuedata; req = blk_fetch_request(q); /* 遍历每个请求 */ while ( req != NULL ) { if(req->cmd_type != REQ_TYPE_FS) { //判断请求是否是个文件系统请求 printk(KERN_NOTICE "Skip non-fs request\n"); blk_end_request_all(req, -EIO); //该请求是一个I/O error continue; } sbull_xfer_request(dev, req); //调用请求处理函数 if (!blk_end_request_cur(req, 0)) { //req指向下一个请求 req = NULL; } } } /* * The direct make request version. */ static int sbull_make_request(struct request_queue *q, struct bio *bio) { struct sbull_dev *dev = q->queuedata; int status; status = sbull_xfer_bio(dev,bio); bio_endio(bio, status); // bio_endio()函数通知处理结束 return 0; } /* * open()函数 */ static int sbull_open(struct block_device *bd, fmode_t mode) { struct sbull_dev *dev = bd->bd_disk->private_data; del_timer_sync(&dev->timer); /* 介质移除定时器: 销毁定时器 */ spin_lock(&dev->lock); /* lock */ if(!dev->users) check_disk_change(bd); dev->users++; spin_unlock(&dev->lock); /* unlock */ return 0; } static int sbull_release(struct gendisk *gd, fmode_t mode) { struct sbull_dev *dev = gd->private_data; spin_lock(&dev->lock); dev->users--; if(!dev->users){ dev->timer.expires = jiffies + INVALIDATE_DELAY; add_timer(&dev->timer); /* 介质移除定时器: 加载定时器 ,30s */ } spin_unlock(&dev->lock); return 0; } /* * Look for a (simulated) media change. */ int sbull_media_change(struct gendisk *gd) { struct sbull_dev *dev = gd->private_data; return dev->media_change; } /* * Revalidate.we do not take the lock here,for fear of deadlocking with open. * That needs to be reevaluated. * 调用此函数内核将试着重新读取分区表,在这里这个函数这是简单的重置 media_change 的标志位,并 * 清除内存空间以模拟插入一张磁盘 */ int sbull_revalidate(struct gendisk *gd) { struct sbull_dev *dev = gd->private_data; if(dev->media_change){ dev->media_change = 0; memset(dev->data, 0, dev->size); } return 0; } /* * The "invalidte"function runs out of the device timer;it sets a flag to * simulate the removal of the media. */ void sbull_invalidate(unsigned long ldev) { struct sbull_dev *dev = (struct sbull_dev *)ldev; spin_lock(&dev->lock); if(dev->users || !dev->data) printk (KERN_WARNING "sbull: timer sanity check failed\n"); else dev->media_change = 1; spin_unlock(&dev->lock); } /* *ioctl: *暂时处理一个命令: 对设备的物理信息的查询请求 */ int sbull_ioctl(struct block_device *bd, fmode_t mode, unsigned cmd, unsigned long arg) { long size; struct hd_geometry geo; struct sbull_dev *dev = bd->bd_disk->private_data; switch (cmd) { case HDIO_GETGEO: size = dev->size*(hardsect_size / KERNEL_SECTOR_SIZE); geo.cylinders = (size & ~0x3f) >> 6; geo.sectors = 16; geo.heads = 4; geo.start = 4; if(copy_to_user((void __user*)arg, &geo, sizeof(geo))) return -EFAULT; return 0; } return -ENOTTY; } /* 获取几何信息 */ static int sbull_getgeo(struct block_device *bd, struct hd_geometry *geo) { long size; struct sbull_dev *dev = bd->bd_disk->private_data; size = dev->size *(hardsect_size / KERNEL_SECTOR_SIZE); geo->cylinders = (size & ~0x3f) >> 6; geo->heads = 4; geo->sectors = 16; geo->start = 4; return 0; } /* * The device operations structure. */ static struct block_device_operations sbull_ops = { .owner = THIS_MODULE, .open = sbull_open, .release = sbull_release, .media_changed = sbull_media_change, /* 用户检查介质是否被改变(移除) */ .revalidate_disk = sbull_revalidate, .ioctl = sbull_ioctl, .getgeo = sbull_getgeo }; /*初始化 sbull_dev 数据结构的具体实现*/ static void sbull_setup_device(struct sbull_dev *dev) { printk(KERN_INFO "sbull: step into into setup_device\n"); memset(dev, 0, sizeof(struct sbull_dev)); dev->size = nsectors * hardsect_size; //整个块设备大小1024(扇区个数) * 512(扇区大小) dev->data = vmalloc(dev->size); /* 开辟虚拟存储空间 */ if(dev->data == NULL){ printk(KERN_NOTICE "vmalloc failure\n"); return; } spin_lock_init(&dev->lock); /* 初始化自旋锁 */ init_timer(&dev->timer); /* 初始化定时器 */ dev->timer.data = (unsigned long)dev; dev->timer.function = sbull_invalidate; /* 超时处理函数 */ /* * The I/O queue, depending on whether we are using our own * make_request function or not. */ switch (request_mode) { case RM_NOQUEUE: dev->queue = blk_alloc_queue(GFP_KERNEL); /* 分配“请求队列” */ if(dev->queue == NULL) goto out_vfree; blk_queue_make_request(dev->queue, sbull_make_request);/*绑定"制造请求"函数 */ break; case RM_FULL: dev->queue = blk_init_queue(sbull_full_request, &dev->lock); /*请求队列初始化*/ if(dev->queue == NULL) goto out_vfree; break; case RM_SIMPLE: dev->queue = blk_init_queue(sbull_request, &dev->lock);/*请求队列初始化*/ if(dev->queue == NULL) goto out_vfree; break; default: printk(KERN_NOTICE "Bad request mode %d, using simple\n", request_mode); } blk_queue_logical_block_size(dev->queue, hardsect_size);/* 硬件扇区尺寸设置 */ dev->queue->queuedata = dev; dev->gd = alloc_disk(SBULL_MINORS); if(!dev->gd){ /* 动态分配 gendisk 结构体*/ printk(KERN_NOTICE "alloc_disk failure\n"); goto out_vfree; } dev->gd->major = sbull_major; /* 主设备号 */ dev->gd->first_minor = SBULL_MINORS; /* 次设备号 */ dev->gd->fops = &sbull_ops; /* 块设备操作结构体 */ dev->gd->queue = dev->queue; /* 请求队列 */ dev->gd->private_data = dev; /* 私有数据 */ snprintf(dev->gd->disk_name, 32, "sbull"); /* 次设备的名字 */ /* 每个请求的大小都是扇区大小的整数倍,内核总是认为扇区大小是512字节,因此必须进行转换 */ set_capacity(dev->gd, nsectors * (hardsect_size / KERNEL_SECTOR_SIZE)); add_disk(dev->gd);/* 完成以上初始化后,调用 add_disk 函数来注册这个磁盘设备 */ return; out_vfree: if(dev->data) vfree(dev->data); } static int __init sbull_init(void) { int i; printk(KERN_WARNING "sbull: start init\n"); // 注册块设备,第一个参数是设备号,0为动态分配,第二个参数是设备名 sbull_major = register_blkdev(sbull_major, "sbull"); if (sbull_major <= 0) { printk(KERN_WARNING "sbull: unable to get major number\n"); return -EBUSY; } printk(KERN_INFO "sbull: start kmalloc\n"); Devices = kmalloc(ndevices * sizeof(struct sbull_dev), GFP_KERNEL);/* 为块核心数据结构 sbull_dev 分配空间 */ if(Devices == NULL) goto out_unregister; printk(KERN_WARNING "sbull: start setup_device\n"); sbull_setup_device(Devices); /* 初始化 sbull_dev 核心数据结构 ,并add_disk*/ return 0; out_unregister: unregister_blkdev(sbull_major, "sbull"); return -ENOMEM; } static void sbull_exit(void) { struct sbull_dev *dev = Devices; del_timer_sync(&dev->timer); if (dev->gd) { del_gendisk(dev->gd); put_disk(dev->gd); } if (dev->queue) { if (request_mode == RM_NOQUEUE) kobject_put(&(dev->queue)->kobj); else blk_cleanup_queue(dev->queue); } if (dev->data) vfree(dev->data); unregister_blkdev(sbull_major, "sbull"); kfree(Devices); } MODULE_LICENSE("Dual BSD/GPL"); module_init(sbull_init); module_exit(sbull_exit);