为了加速对物理设备的访问速度,linux将块缓冲区放在Cache内,块缓冲区是由buffer_head连成的链表结构。buffer_head的数据结构如下: /*includelinuxfs.h*/ struct buffer_head { unsigned long b_blocknr; /* block number */ kdev_t b_dev; /* device (B_FREE = free) */ kdev_t b_rdev; /* Real device */ unsigned long b_rsector; /* Real buffer location on disk */ struct buffer_head * b_next; /* Hash queue list */ struct buffer_head * b_this_page; /* circular list of buffers in one page * / unsigned long b_state; /* buffer state bitmap (see above) */ struct buffer_head * b_next_free; unsigned int b_count; /* users using this block */ unsigned long b_size; /* block size */ char * b_data; /* pointer to data block (1024 bytes) */ unsigned int b_list; /* List that this buffer appears */ unsigned long b_flushtime; /* Time when this (dirty) buffer should be written */ unsigned long b_lru_time; /* Time when this buffer was last used. */ struct wait_queue * b_wait; struct buffer_head * b_PRev; /* doubly linked list of hash-queue */ struct buffer_head * b_prev_free; /* doubly linked list of buffers */ struct buffer_head * b_reqnext; /* request queue */ };
2. 确定编写需要的file_operations中的操作函数: static int my_open(struct inode * inode,struct file * file) static void my_release(struct inode * inode,struct file * file) static int my_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg) 由于使用了高速缓存,块设备驱动程序就不需要包含自己的read()、write()和fsync()函数,但必须使用自己的open()、 release()和 ioctl()函数,这些函数的作用和字符设备的相应函数类似。
3. 确定编写需要的输入/输出函数: static int my _read(void) //正确处理时返回值为1,错误时返回值为0 static int my _write(void) //正确处理时返回值为1,错误时返回值为0 值得注重的是这两个函数和字符设备中的my read()、mywrite()函数不同: