Linux 0.12 OS. block - blk.h

重点摘要

I. 设备支持

目前系统支持的设备为

0 - unused (无设备)

1 - /dev/mem (内存盘)

2 - /dev/fd (软盘)

3 - /dev/hd (硬盘)

4 - /dev/ttyx (串口)

5 - /dev/tty (控制台)

6 - /dev/lp (并口)

7 - unnamed pipes (无名管道)

II. 数据结构

- device command

  READ 0:读操作

  WRITE 1:写操作

  READA 2:预读操作,可以理解为尝试性读取,如果遇到资源忙的状态可以不操作

  WRITEA 3:预写操作,同理

- device number

  MAJOR:主设备号,为(dev >> 8),比如硬盘的设备号为0x301、0x305等,软盘的设备号0x208、0x21c等

  MINOR:次设备号,为(dev & 0xff)

- NR_REQUEST 32

  针对请求项的个数,Linus也给出了注释:

/*
 * NR_REQUEST is the number of entries in the request-queue.
 * NOTE that writes may use only the low 2/3 of these: reads
 * take precedence.
 *
 * 32 seems to be a reasonable number: enough to get some benefit
 * from the elevator(电梯)-mechanism, but not so much as to lock a lot of
 * buffers when they are in the queue. 64 seems to be too many (easily
 * long pauses in reading when heavy writing/syncing is going on)

 */

1. 关于2/3,从代码中仅仅能看出,读写操作开始遍历请求项的地方不一样,读操作是从最底部开始遍历的,而写操作是从the low 2/3的地方开始遍历的,也即读操作比写操作多出了1/3的请求项供使用,个人认为是读操作比写操作更加频繁而特地这样设定的结果。

2. 关于32,Linus也做了简单的解释,大概的意思就是说太多的请求会使某个操作进入长时间等待状态。

- IN_ORDER(s1,s2):如果为真,那么说明s1优先于s2。这里我自己理解的电梯算法 (假想,所有坐电梯的人都要到达0楼,那么这个问题就变成如何在电梯折返次数最少且所有人等待电梯时间最短的情况下把他们运送到0楼的方法)。通俗的算法机制就是从低扇区开始操作直到高扇区都完成后在去操作低的扇区,如此反复。

- device about

  DEVICE_NAME:设备名称

  DEVICE_REQUEST:设备请求函数

  DEVICE_NR:次设备,次设备号里面包含了该设备更具体的信息,比如软盘驱动器号,第一块硬盘还是第二块硬盘等

  DEVICE_ON:打开设备

  DEVICE_OFF:关闭设备

  DEVICE_INTR:设备中断

  DEVICE_TIMEOUT:设备超时时长

- end_request:当一个请求项完成时需要调用它,这个函数比较有意思,它在设备中断的上下文中。

- struct buffer_head {

  char * b_data; // pointer to data block (1024 bytes)

  unsigned long b_blocknr; // block number

  unsigned short b_dev; // 设备号

  unsigned char b_uptodate; // 指明该buffer的数据是否被磁盘初始化过

  unsigned char b_dirt; // 指明该buffer的数据是否被用户修改过

  unsigned char b_count; // 被引用的次数

  unsigned  char b_lock; // 锁标识

  struct task_struct * b_wait; // 等待队列头

  ...

};

code部分

#ifndef _BLK_H
#define _BLK_H

/*
 目前系统支持的设备为:
 0 - unused (nodev)
 1 - /dev/mem (内存盘)
 2 - /dev/fd   (软盘)
 3 - /dev/hd   (硬盘)
 4 - /dev/ttyx  (串口)
 5 - /dev/tty   (控制台)
 6 - /dev/lp    (并口)
 7 - unnamed pipes (无名管道)
*/
#define NR_BLK_DEV	7
/*
 * NR_REQUEST is the number of entries in the request-queue.
 * NOTE that writes may use only the low 2/3 of these: reads
 * take precedence.
 *
 * 32 seems to be a reasonable number: enough to get some benefit
 * from the elevator(电梯)-mechanism, but not so much as to lock a lot of
 * buffers when they are in the queue. 64 seems to be too many (easily
 * long pauses in reading when heavy writing/syncing is going on)
 */
#define NR_REQUEST	32

/*
 * Ok, this is an expanded form so that we can use the same
 * request for paging requests when that is implemented. In
 * paging, 'bh' is NULL, and 'waiting' is used to wait for
 * read/write completion.
 */
struct request {
	int dev;		/* -1 if no request */
	int cmd;		/* READ or WRITE */
	int errors;
	unsigned long sector;
	unsigned long nr_sectors;
	char * buffer;
	struct task_struct * waiting; /* 当一个request请求处理完后唤醒等待它的进程,
这里运用在page操作中,仅会有一个等待进程 */
	struct buffer_head * bh; /* 对应于块设备的一个block,需要同步来使用 */
	struct request * next;
};

/*
 * This is used in the elevator algorithm(电梯算法): Note that
 * reads always go before writes. This is natural: reads
 * are much more time-critical than writes.
 */
/*
 读比写优先级更高
 读写操作按顺序进行,低扇区比高扇区的优先级更高
*/
#define IN_ORDER(s1,s2) \
((s1)->cmd<(s2)->cmd || ((s1)->cmd==(s2)->cmd && \
((s1)->dev < (s2)->dev || ((s1)->dev == (s2)->dev && \
(s1)->sector < (s2)->sector))))

/*
 块设备结构体
 请求函数
 当前请求项
*/
struct blk_dev_struct {
	void (*request_fn)(void);
	struct request * current_request;
};

// 它们都定义在ll_rw_blk.c中
extern struct blk_dev_struct blk_dev[NR_BLK_DEV];
extern struct request request[NR_REQUEST];
extern struct task_struct * wait_for_request;

extern int * blk_size[NR_BLK_DEV];

// 下面对应了不同的块设备
#ifdef MAJOR_NR

/*
 * Add entries as needed. Currently the only block devices
 * supported are hard-disks and floppies.
 */

#if (MAJOR_NR == 1)
/* ram disk */
#define DEVICE_NAME "ramdisk"
#define DEVICE_REQUEST do_rd_request
#define DEVICE_NR(device) ((device) & 7)
#define DEVICE_ON(device) 
#define DEVICE_OFF(device)

#elif (MAJOR_NR == 2)
/* floppy */
#define DEVICE_NAME "floppy"
#define DEVICE_INTR do_floppy
#define DEVICE_REQUEST do_fd_request
#define DEVICE_NR(device) ((device) & 3)
#define DEVICE_ON(device) floppy_on(DEVICE_NR(device))
#define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))

#elif (MAJOR_NR == 3)
/* harddisk */
#define DEVICE_NAME "harddisk"
#define DEVICE_INTR do_hd
#define DEVICE_TIMEOUT hd_timeout
#define DEVICE_REQUEST do_hd_request
#define DEVICE_NR(device) (MINOR(device)/5)
#define DEVICE_ON(device)
#define DEVICE_OFF(device)

#elif
/* unknown blk device */
#error "unknown blk device"

#endif

#define CURRENT (blk_dev[MAJOR_NR].current_request)
#define CURRENT_DEV DEVICE_NR(CURRENT->dev)

// 定义设备中断执行函数
#ifdef DEVICE_INTR
void (*DEVICE_INTR)(void) = NULL;
#endif

// 设定设备中断执行函数
#ifdef DEVICE_TIMEOUT
int DEVICE_TIMEOUT = 0;
#define SET_INTR(x) (DEVICE_INTR = (x),DEVICE_TIMEOUT = 200)
#else
#define SET_INTR(x) (DEVICE_INTR = (x))
#endif

// 定义请求函数
static void (DEVICE_REQUEST)(void);

extern inline void unlock_buffer(struct buffer_head * bh)
{
	if (!bh->b_lock)
		printk(DEVICE_NAME ": free buffer being unlocked\n");
	bh->b_lock=0;
	wake_up(&bh->b_wait);
}

// 当前请求项执行完成
extern inline void end_request(int uptodate)
{
	DEVICE_OFF(CURRENT->dev); // 关闭设备
	if (CURRENT->bh) {
		CURRENT->bh->b_uptodate = uptodate;
		unlock_buffer(CURRENT->bh); // 唤醒等待这个buffer的队列头
	}
	if (!uptodate) { // 没有更新
		printk(DEVICE_NAME " I/O error\n\r");
		printk("dev %04x, block %d\n\r",CURRENT->dev,
			CURRENT->bh->b_blocknr);
	}
	wake_up(&CURRENT->waiting); // 唤醒等待该page的进程
	wake_up(&wait_for_request); // 唤醒等待请求项的队列头
	CURRENT->dev = -1; // 置请求项为空
	CURRENT = CURRENT->next; // 继续下一个请求项
}

// 设备超时变量清0
#ifdef DEVICE_TIMEOUT
#define CLEAR_DEVICE_TIMEOUT DEVICE_TIMEOUT = 0;
#else
#define CLEAR_DEVICE_TIMEOUT
#endif

// 设备中断执行函数清0
#ifdef DEVICE_INTR
#define CLEAR_DEVICE_INTR DEVICE_INTR = 0;
#else
#define CLEAR_DEVICE_INTR
#endif

// 请求开始时检查
#define INIT_REQUEST \
repeat: \
	if (!CURRENT) {\
		CLEAR_DEVICE_INTR \
		CLEAR_DEVICE_TIMEOUT \
		return; \
	} \
	if (MAJOR(CURRENT->dev) != MAJOR_NR) \
		panic(DEVICE_NAME ": request list destroyed"); \
	if (CURRENT->bh) { \
		if (!CURRENT->bh->b_lock) \
			panic(DEVICE_NAME ": block not locked"); \
	}

#endif /* MAJOR_NR */

#endif


你可能感兴趣的:(Linux 0.12 OS. block - blk.h)