免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 790 | 回复: 0
打印 上一主题 下一主题

Linux块设备(1) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-01 00:29 |只看该作者 |倒序浏览
块设备与字符设备比较
1大多数设备为字符设备,因为他们不需要缓冲,而且不以固定块大小进行操作
2块设备对于IO请求有对应的缓冲区,因此可以选择以什么样的顺序进行响应。
3字符设备只能被顺序读写,块设备可以随机访问。
struct block_device_operations
{
int (*open)(struct inode * inode, struct file * filp);
int (*release)(struct inode *inode , struct file *filp);
int (*ioctl)(struct inode *inode, struct file *filp,unsigned int cmd,unsigned long arg);
long (*unlocked_ioctl)(struct file,unsigned , unsigned long);
long (*compat_ioctl)(struct file *, unsigned , unsigned long);
int (*direct_access)(struct block_device *, sector_t, unsigned long *);
int (*media_changed)(struct gendisk *);检查驱动器介质是否已经改变,是非0,移动设备
int (*revalidate_disk)(struct gendisk *);响应一个介质改变
int (*getgeo)(struct block_device *,struct hd_geometry*));填充驱动器信息
struct module *owner;
};
struct gendisk
{
  int major;//主设备号
  int first_minor;//第一个次设备号
  int minors; //最大次设备数
  char disk_name[32];
  struct hd_struct **part;//分区信息
  struct block_device_operations *fops;
  struct request_queue *queue;
  void *private_data;
  sector_t capacity;扇区数  512字节
  
  int flags;
  char devfs_name[64];
  int number;
  struct device *driverfs_dev;
  struct kobject kobj;
  
   struct timer_rand_state *random;
   int policy;
    atomic_t sync_io;
   unsigned long stamp;
   int in_flight;
   #ifdef CONFIG_SMP
      struct disk_stats *dkstats;
   #else
struct disk_stats dkstats;
   #endif
};
分配gendisk
struct dendisk *alloc_disk(int minors);分区数
注册,发生在初始化完成,能响应磁盘请求之后
void add_disk(struct gendisk *gd);
释放
void del_gendisk(struct gendisk *gd);
引用计数
get_disk() put_disk()
设置容量
void set_capacity(struct gendisk *disk, sector_t size); 521倍数大小
======================================================================================
表征等待进行的IO请求--request
struct request
{
struct list_head queuelist;
unsigned long flags;
sector_t sector;//要传送的下一个扇区
unsigned long nr_sectors;//传送的扇区数目
unsigned int  current_nr_sectors;//当前要传送的扇区数目
sector_t hard_sector;//要完成的下一个扇区
unsigned long hard_nr_sectors;//要被完成的扇区数目
unsigned int hard_cur_sectors;//当前要被完成的扇区数目
struct bio *bio;请求的bio结构链表
struct bio *biotail;
void *elevator_private;
unsigned int short ioprio;
int rq_status;
struct gendisk *rq_disk;
int errors;
unsigned long start_time;
unsigned short nr_phys_segments;//请求在物理内存中占据的不连续的段数目
unsigned short nr_hw_segment;//考虑到系统IOMMU remap
int tag;
char * buffer; //传送的缓冲
int ref_count;//引用计数
...
}
int blk_rq_map_sg(request_queue_t *q, struct request *req, struct  scatterlist *sglist);
使用如下宏可以从request 获得数据传送的方向
rq_data_dir(struct request *req); 0 read 1write


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/69624/showart_1070260.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP