?? blk.h
字號(hào):
#ifndef _BLK_H#define _BLK_H#define NR_BLK_DEV 7 // 塊設(shè)備的數(shù)量。/** 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)*//** 下面定義的NR_REQUEST 是請(qǐng)求隊(duì)列中所包含的項(xiàng)數(shù)。* 注意,讀操作僅使用這些項(xiàng)低端的2/3;讀操作優(yōu)先處理。** 32 項(xiàng)好象是一個(gè)合理的數(shù)字:已經(jīng)足夠從電梯算法中獲得好處,* 但當(dāng)緩沖區(qū)在隊(duì)列中而鎖住時(shí)又不顯得是很大的數(shù)。64 就看上* 去太大了(當(dāng)大量的寫/同步操作運(yùn)行時(shí)很容易引起長時(shí)間的暫停)。*/#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.*//** OK,下面是request 結(jié)構(gòu)的一個(gè)擴(kuò)展形式,因而當(dāng)實(shí)現(xiàn)以后,我們就可以在分頁請(qǐng)求中* 使用同樣的request 結(jié)構(gòu)。在分頁處理中,'bh'是NULL,而'waiting'則用于等待讀/寫的完成。*/// 下面是請(qǐng)求隊(duì)列中項(xiàng)的結(jié)構(gòu)。其中如果dev=-1,則表示該項(xiàng)沒有被使用。struct request{ int dev; /* -1 if no request */// 使用的設(shè)備號(hào)。 int cmd; /* READ or WRITE */// 命令(READ 或WRITE)。 int errors; //操作時(shí)產(chǎn)生的錯(cuò)誤次數(shù)。 unsigned long sector; // 起始扇區(qū)。(1 塊=2 扇區(qū)) unsigned long nr_sectors; // 讀/寫扇區(qū)數(shù)。 char *buffer; // 數(shù)據(jù)緩沖區(qū)。 struct task_struct *waiting; // 任務(wù)等待操作執(zhí)行完成的地方。 struct buffer_head *bh; // 緩沖區(qū)頭指針(include/linux/fs.h,68)。 struct request *next; // 指向下一請(qǐng)求項(xiàng)。};/** 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.*//** 下面的定義用于電梯算法:注意讀操作總是在寫操作之前進(jìn)行。* 這是很自然的:讀操作對(duì)時(shí)間的要求要比寫嚴(yán)格得多。*/#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)))// 塊設(shè)備結(jié)構(gòu)。struct blk_dev_struct{ void (*request_fn) (void); // 請(qǐng)求操作的函數(shù)指針。 struct request *current_request; // 請(qǐng)求信息結(jié)構(gòu)。};extern struct blk_dev_struct blk_dev[NR_BLK_DEV]; // 塊設(shè)備數(shù)組,每種塊設(shè)備占用一項(xiàng)。extern struct request request[NR_REQUEST]; // 請(qǐng)求隊(duì)列數(shù)組。extern struct task_struct *wait_for_request; // 等待請(qǐng)求的任務(wù)結(jié)構(gòu)。#ifdef MAJOR_NR // 主設(shè)備號(hào)。/** Add entries as needed. Currently the only block devices* supported are hard-disks and floppies.*//** 需要時(shí)加入條目。目前塊設(shè)備僅支持硬盤和軟盤(還有虛擬盤)。*/#if (MAJOR_NR == 1) // RAM 盤的主設(shè)備號(hào)是1。根據(jù)這里的定義可以推理內(nèi)存塊主設(shè)備號(hào)也為1。/* ram disk *//* RAM 盤(內(nèi)存虛擬盤) */#define DEVICE_NAME "ramdisk" // 設(shè)備名稱ramdisk。#define DEVICE_REQUEST do_rd_request // 設(shè)備請(qǐng)求函數(shù)do_rd_request()。#define DEVICE_NR(device) ((device) & 7) // 設(shè)備號(hào)(0--7)。#define DEVICE_ON(device) // 開啟設(shè)備。虛擬盤無須開啟和關(guān)閉。#define DEVICE_OFF(device) // 關(guān)閉設(shè)備。#elif (MAJOR_NR == 2) // 軟驅(qū)的主設(shè)備號(hào)是2。/* floppy */#define DEVICE_NAME "floppy" // 設(shè)備名稱floppy。#define DEVICE_INTR do_floppy // 設(shè)備中斷處理程序do_floppy()。#define DEVICE_REQUEST do_fd_request // 設(shè)備請(qǐng)求函數(shù)do_fd_request()。#define DEVICE_NR(device) ((device) & 3) // 設(shè)備號(hào)(0--3)。#define DEVICE_ON(device) floppy_on(DEVICE_NR(device)) // 開啟設(shè)備函數(shù)floppyon()。#define DEVICE_OFF(device) floppy_off(DEVICE_NR(device)) // 關(guān)閉設(shè)備函數(shù)floppyoff()。#elif (MAJOR_NR == 3) // 硬盤主設(shè)備號(hào)是3。/* harddisk */#define DEVICE_NAME "harddisk" // 硬盤名稱harddisk。#define DEVICE_INTR do_hd // 設(shè)備中斷處理程序do_hd()。#define DEVICE_REQUEST do_hd_request // 設(shè)備請(qǐng)求函數(shù)do_hd_request()。#define DEVICE_NR(device) (MINOR(device)/5) // 設(shè)備號(hào)(0--1)。每個(gè)硬盤可以有4 個(gè)分區(qū)。#define DEVICE_ON(device) // 硬盤一直在工作,無須開啟和關(guān)閉。#define DEVICE_OFF(device)#elif/* unknown blk device *//* 未知塊設(shè)備 */#error "unknown blk device"#endif#define CURRENT (blk_dev[MAJOR_NR].current_request) // CURRENT 為指定主設(shè)備號(hào)的當(dāng)前請(qǐng)求結(jié)構(gòu)。#define CURRENT_DEV DEVICE_NR(CURRENT->dev) // CURRENT_DEV 為CURRENT 的設(shè)備號(hào)。#ifdef DEVICE_INTRvoid (*DEVICE_INTR) (void) = NULL;#endifstatic void (DEVICE_REQUEST) (void);// 釋放鎖定的緩沖區(qū)。extern inline voidunlock_buffer (struct buffer_head *bh){ if (!bh->b_lock) // 如果指定的緩沖區(qū)bh 并沒有被上鎖,則顯示警告信息。 printk (DEVICE_NAME ": free buffer being unlocked\n"); bh->b_lock = 0; // 否則將該緩沖區(qū)解鎖。 wake_up (&bh->b_wait); // 喚醒等待該緩沖區(qū)的進(jìn)程。}// 結(jié)束請(qǐng)求。extern inline voidend_request (int uptodate){ DEVICE_OFF (CURRENT->dev); // 關(guān)閉設(shè)備。 if (CURRENT->bh) { // CURRENT 為指定主設(shè)備號(hào)的當(dāng)前請(qǐng)求結(jié)構(gòu)。 CURRENT->bh->b_uptodate = uptodate; // 置更新標(biāo)志。 unlock_buffer (CURRENT->bh); // 解鎖緩沖區(qū)。 } if (!uptodate) { // 如果更新標(biāo)志為0 則顯示設(shè)備錯(cuò)誤信息。 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); // 喚醒等待該請(qǐng)求項(xiàng)的進(jìn)程。 wake_up (&wait_for_request); // 喚醒等待請(qǐng)求的進(jìn)程。 CURRENT->dev = -1; // 釋放該請(qǐng)求項(xiàng)。 CURRENT = CURRENT->next; // 從請(qǐng)求鏈表中刪除該請(qǐng)求項(xiàng)。}// 定義初始化請(qǐng)求宏。#define INIT_REQUEST \repeat: \if (!CURRENT) \ // 如果當(dāng)前請(qǐng)求結(jié)構(gòu)指針為null 則返回。return;if (MAJOR (CURRENT->dev) != MAJOR_NR) \ // 如果當(dāng)前設(shè)備的主設(shè)備號(hào)不對(duì)則死機(jī)。 panic (DEVICE_NAME ": request list destroyed");if (CURRENT->bh) { if (!CURRENT->bh->b_lock) \ // 如果在進(jìn)行請(qǐng)求操作時(shí)緩沖區(qū)沒鎖定則死機(jī)。 panic (DEVICE_NAME ": block not locked"); }#endif#endif
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -