linux-blk.h

#ifndef _BLK_H
#define _BLK_H
#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;        //--任务等待请求完成操作的地方
    struct buffer_head * bh;            //--缓冲区头指针
    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,s2为request指针
((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;
};
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);
    }
    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);
    wake_up(&wait_for_request);
    CURRENT->dev = -1;
    CURRENT = CURRENT->next;
}
#ifdef DEVICE_TIMEOUT
#define CLEAR_DEVICE_TIMEOUT DEVICE_TIMEOUT = 0;
#else
#define CLEAR_DEVICE_TIMEOUT
#endif
#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
#endif
|xGv00|fcc8d4de8197f69fde70263fb4d52380
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值