為了加速對物理設備的訪問速度,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()函數不同: