����9-1 linux/kernel/blk_drv/blk.h


  1 #ifndef _BLK_H

  2 #define _BLK_H

  3

  4 #define NR_BLK_DEV      7          // ���豸����������

  5 /*

  6  * NR_REQUEST is the number of entries in the request-queue.

  7  * NOTE that writes may use only the low 2/3 of these: reads

  8  * take precedence.

  9  *

 10  * 32 seems to be a reasonable number: enough to get some benefit

 11  * from the elevator-mechanism, but not so much as to lock a lot of

 12  * buffers when they are in the queue. 64 seems to be too many (easily

 13  * long pauses in reading when heavy writing/syncing is going on)

 14  */

    /*

     * ���涨���NR_REQUEST�������������������������

     * ע�⣬д������ʹ����Щ���еͶ˵�2/3����������ȴ�����

     *

     * 32�������һ�����������֣������Ѿ��㹻�ӵ����㷨�л�úô���

     * �����������ڶ����ж���סʱ�ֲ��Ե��Ǻܴ������64�Ϳ���ȥ̫

     * ���ˣ���������д/ͬ����������ʱ����������ʱ�����ͣ����

     */

 15 #define NR_REQUEST      32

 16

 17 /*

 18  * Ok, this is an expanded form so that we can use the same

 19  * request for paging requests when that is implemented. In

 20  * paging, 'bh' is NULL, and 'waiting' is used to wait for

 21  * read/write completion.

 22  */

    /*

     * OK��������request�ṹ��һ����չ��ʽ�������ʵ���Ժ�����

     * �Ϳ����ڷ�ҳ������ʹ��ͬ���� request �ṹ�� �ڷ�ҳ�����У�

     * 'bh'��NULL����'waiting'�����ڵȴ���/д����ɡ�

     */

    // �����������������Ľṹ����������ֶ�dev = -1�����ʾ�����и���û�б�ʹ�á�

    // �ֶ�cmd��ȡ���� READ��0���� WRITE��1����������include/linux/fs.h�У���

    // ���У��ں˲�û���õ�waitingָ�룬�����֮���ں�ʹ���˻����ĵȴ����С���Ϊ

    // �ȴ�һ���������ȴ�����������ǶԵȵġ�

 23 struct request {

 24         int dev;                       /* -1 if no request */  // ��������豸�š�

 25         int cmd;                       /* READ or WRITE */  // READ��WRITE���

 26         int errors;                     //����ʱ�����Ĵ��������

 27         unsigned long sector;           // ��ʼ������(1��=2����)

 28         unsigned long nr_sectors;       // ��/д��������

 29         char * buffer;                  // ���ݻ�������

 30         struct task_struct * waiting;   // ����ȴ�������ɲ����ĵط������У���

 31         struct buffer_head * bh;        // ������ͷָ��(include/linux/fs.h,68)��

 32         struct request * next;          // ָ����һ�����

 33 };

 34

 35 /*

 36  * This is used in the elevator algorithm: Note that

 37  * reads always go before writes. This is natural: reads

 38  * are much more time-critical than writes.

 39  */

    /*

     * ����Ķ������ڵ����㷨��ע�������������д����֮ǰ���С�

     * ���Ǻ���Ȼ�ģ���������ʱ���Ҫ��Ҫ��д�����ϸ�öࡣ

     */

    // ������в���s1��s2��ȡֵ�����涨�������ṹrequest��ָ�롣�ú궨�����ڸ�����������

    // ָ����������ṹ�е���Ϣ������cmd��READ��WRITE�����豸��dev�Լ���������������sector��

    // ���жϳ�����������ṹ��ǰ������˳�����˳���������ʿ��豸ʱ��������ִ��˳��

    // �������ڳ���blk_drv/ll_rw_blk.c�к���add_request()�б����ã���96�У����ú겿��

    // ��ʵ����I/O���ȹ��ܣ���ʵ���˶�������������ܣ���һ����������ϲ����ܣ���

 40 #define IN_ORDER(s1,s2) \

 41 ((s1)->cmd<(s2)->cmd || (s1)->cmd==(s2)->cmd && \

 42 ((s1)->dev < (s2)->dev || ((s1)->dev == (s2)->dev && \

 43 (s1)->sector < (s2)->sector)))

 44

    // ���豸�����ṹ��

 45 struct blk_dev_struct {

 46         void (*request_fn)(void);                      // ����������ָ�롣

 47         struct request * current_request;              // ��ǰ����������ṹ��

 48 };

 49

    // ���豸�������飩��ÿ�ֿ��豸ռ��һ���7�

 50 extern struct blk_dev_struct blk_dev[NR_BLK_DEV];

    // ����������飬��32�

 51 extern struct request request[NR_REQUEST];

    // �ȴ�����������Ľ��̶���ͷָ�롣

 52 extern struct task_struct * wait_for_request;

 53

    // �豸���ݿ�����ָ�����顣ÿ��ָ����ָ��ָ�����豸�ŵ��ܿ�������hd_sizes[]������

    // ��������ÿһ���Ӧ���豸��ȷ����һ�����豸����ӵ�е����ݿ�������1���С = 1KB����

 54 extern int * blk_size[NR_BLK_DEV];

 55

    // �ڿ��豸����������hd.c��������ͷ�ļ�ʱ�������ȶ��������������豸�����豸�š�

    // ������������63����90�о���Ϊ�������ļ����������������ȷ�ĺ궨�塣

 56 #ifdef MAJOR_NR                    // ���豸�š�

 57

 58 /*

 59  * Add entries as needed. Currently the only block devices

 60  * supported are hard-disks and floppies.

 61  */

    /*

     * ��Ҫʱ������Ŀ��Ŀǰ���豸��֧��Ӳ�̺����̣����������̣���

     */

 62

    // ���������MAJOR_NR = 1��RAM�����豸�ţ������������·��ų����ͺꡣ

 63 #if (MAJOR_NR == 1)

 64 /* ram disk */

 65 #define DEVICE_NAME "ramdisk"                // �豸���ƣ����ڴ������̡�����

 66 #define DEVICE_REQUEST do_rd_request         // �豸�������������

 67 #define DEVICE_NR(device) ((device) & 7)     // �豸�ţ�0 �C 7����

 68 #define DEVICE_ON(device)                    // �����豸�����������뿪���͹رգ���

 69 #define DEVICE_OFF(device)                   // �ر��豸��

 70

    // �������������MAJOR_NR = 2���������豸�ţ������������·��ų����ͺꡣ

 71 #elif (MAJOR_NR == 2)

 72 /* floppy */

 73 #define DEVICE_NAME "floppy"                 // �豸���ƣ�������������������

 74 #define DEVICE_INTR do_floppy                // �豸�жϴ���������

 75 #define DEVICE_REQUEST do_fd_request         // �豸�������������

 76 #define DEVICE_NR(device) ((device) & 3)     // �豸�ţ�0 �C 3����

 77 #define DEVICE_ON(device) floppy_on(DEVICE_NR(device))    // �����豸�ꡣ

 78 #define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))  // �ر��豸�ꡣ

 79

    // �������������MAJOR_NR = 3��Ӳ�����豸�ţ������������·��ų����ͺꡣ

 80 #elif (MAJOR_NR == 3)

 81 /* harddisk */

 82 #define DEVICE_NAME "harddisk"               // �豸���ƣ���Ӳ�̡�����

 83 #define DEVICE_INTR do_hd                    // �豸�жϴ���������

 84 #define DEVICE_TIMEOUT hd_timeout            // �豸��ʱֵ��

 85 #define DEVICE_REQUEST do_hd_request         // �豸�������������

 86 #define DEVICE_NR(device) (MINOR(device)/5)  // �豸�š�

 87 #define DEVICE_ON(device)                    // �����豸��

 88 #define DEVICE_OFF(device)                   // �ر��豸��

 89

    // �����ڱ���Ԥ�����׶���ʾ������Ϣ����δ֪���豸����

 90 #elif

 91 /* unknown blk device */

 92 #error "unknown blk device"

 93

 94 #endif

 95

    // Ϊ�˱��ڱ�̱�ʾ�����ﶨ���������꣺CURRENT��ָ��ס�豸�ŵĵ�ǰ����ṹ��ָ�룬

    // CURRENT_DEV �ǵ�ǰ������CURRENT���豸�š�

 96 #define CURRENT (blk_dev[MAJOR_NR].current_request)

 97 #define CURRENT_DEV DEVICE_NR(CURRENT->dev)

 98

    // ����������豸�жϴ������ų��������������Ϊһ������ָ�룬��Ĭ��ΪNULL��

 99 #ifdef DEVICE_INTR

100 void (*DEVICE_INTR)(void) = NULL;

101 #endif

    // ����������豸��ʱ���ų�����������ֵ����0��������SET_INTR()�ꡣ����ֻ����ꡣ

102 #ifdef DEVICE_TIMEOUT

103 int DEVICE_TIMEOUT = 0;

104 #define SET_INTR(x) (DEVICE_INTR = (x),DEVICE_TIMEOUT = 200)

105 #else

106 #define SET_INTR(x) (DEVICE_INTR = (x))

107 #endif

    // �����豸������ų���DEVICE_REGUEST��һ�������������޷��صľ�̬����ָ�롣

108 static void (DEVICE_REQUEST)(void);

109

    // ����ָ���Ļ���顣

    // ���ָ�������bh��û�б�����������ʾ������Ϣ�����򽫸û��������������ѵȴ�

    // �û����Ľ��̡���Ϊ��Ƕ�����������ǻ����ͷָ�롣

110 extern inline void unlock_buffer(struct buffer_head * bh)

111 {

112         if (!bh->b_lock)

113                 printk(DEVICE_NAME ": free buffer being unlocked\n");

114         bh->b_lock=0;

115         wake_up(&bh->b_wait);

116 }

117

    // ������������

    // ����uptodate�Ǹ��±�־��

    // ���ȹر�ָ�����豸��Ȼ����˴ζ�д�������Ƿ���Ч�������Ч����ݲ���ֵ���û���

    // �����ݸ��±�־���������û������� ������±�־����ֵ��0����ʾ�˴�������IJ�����ʧ

    // �ܣ������ʾ��ؿ��豸IO������Ϣ�� ��󣬻��ѵȴ���������Ľ����Լ��ȴ���������

    // ����ֵĽ��̣��ͷŲ�������������ɾ������������ѵ�ǰ������ָ��ָ����һ�����

118 extern inline void end_request(int uptodate)

119 {

120         DEVICE_OFF(CURRENT->dev);                   // �ر��豸��

121         if (CURRENT->bh) {                          // CURRENTΪ��ǰ����ṹ��ָ�롣

122                 CURRENT->bh->b_uptodate = uptodate; // �ø��±�־��

123                 unlock_buffer(CURRENT->bh);         // ������������

124         }

125         if (!uptodate) {                            // �����±�־Ϊ0����ʾ������Ϣ��

126                 printk(DEVICE_NAME " I/O error\n\r");

127                 printk("dev %04x, block %d\n\r",CURRENT->dev,

128                         CURRENT->bh->b_blocknr);

129         }

130         wake_up(&CURRENT->waiting);                 // ���ѵȴ���������Ľ��̡�

131         wake_up(&wait_for_request);                 // ���ѵȴ�����������Ľ��̡�

132         CURRENT->dev = -1;                          // �ͷŸ������

133         CURRENT = CURRENT->next;                    // ָ����һ�����

134 }

135

    // ����������豸��ʱ���ų���DEVICE_TIMEOUT������CLEAR_DEVICE_TIMEOUT���ų���

    // Ϊ��DEVICE_TIMEOUT = 0����������CLEAR_DEVICE_TIMEOUTΪ�ա�

136 #ifdef DEVICE_TIMEOUT

137 #define CLEAR_DEVICE_TIMEOUT DEVICE_TIMEOUT = 0;

138 #else

139 #define CLEAR_DEVICE_TIMEOUT

140 #endif

141

    // ����������豸�жϷ��ų���DEVICE_INTR������CLEAR_DEVICE_INTR���ų���Ϊ

    // ��DEVICE_INTR = 0������������Ϊ�ա�

142 #ifdef DEVICE_INTR

143 #define CLEAR_DEVICE_INTR DEVICE_INTR = 0;

144 #else

145 #define CLEAR_DEVICE_INTR

146 #endif

147

    // �����ʼ��������ꡣ

    // ���ڼ������豸��������ʼ����������ij�ʼ���������ƣ��������Ϊ���Ƕ�����һ��

    // ͳһ�ij�ʼ���ꡣ�ú����ڶԵ�ǰ���������һЩ��Ч���жϡ������������£�

    // ����豸��ǰ������Ϊ�գ�NULL������ʾ���豸Ŀǰ������Ҫ�������������������ɨβ

    // �������˳���Ӧ���������������ǰ���������豸�����豸�Ų��������������������

    // ���ţ�˵������������ҵ��ˣ������ں���ʾ������Ϣ��ͣ�������������������õĻ����

    // û�б�������Ҳ˵���ں˳���������⣬������ʾ������Ϣ��ͣ����

148 #define INIT_REQUEST \

149 repeat: \

150         if (!CURRENT) {\                         // �����ǰ������ָ��ΪNULL�򷵻ء�

151                 CLEAR_DEVICE_INTR \

152                 CLEAR_DEVICE_TIMEOUT \

153                 return; \

154         } \

155         if (MAJOR(CURRENT->dev) != MAJOR_NR) \   // �����ǰ�豸���豸�Ų�����ͣ����

156                 panic(DEVICE_NAME ": request list destroyed"); \

157         if (CURRENT->bh) { \

158                 if (!CURRENT->bh->b_lock) \      // ���������Ļ�����û������ͣ����

159                         panic(DEVICE_NAME ": block not locked"); \

160         }

161

162 #endif

163

164 #endif

165