blk.h ( File view )

  • By 2010-08-31
  • View(s):24
  • Download(s):2
  • Point(s): 1
			#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)
*/
/*
* 下面定义的NR_REQUEST 是请求队列中所包含的项数。
* 注意,读操作仅使用这些项低端的2/3;读操作优先处理。
*
* 32 项好象是一个合理的数字:已经足够从电梯算法中获得好处,
* 但当缓冲区在队列中而锁住时又不显得是很大的数。64 就看上
* 去太大了(当大量的写/同步操作运行时很容易引起长时间的暂停)。
*/
#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 结构的一个扩展形式,因而当实现以后,我们就可以在分页请求中
* 使用同样的request 结构。在分页处理中,'bh'是NULL,而'waiting'则用于等待读/写的完成。
*/
// 下面是请求队列中项的结构。其中如果dev=-1,则表示该项没有被使用。
struct request
{

  int dev;			/* -1 if no request */// 使用的设备号。
  int cmd;			/* READ or WRITE */// 命令(READ 或WRITE)。
  int errors;			//操作时产生的错误次数。
  unsigned long sector;		// 起始扇区。(1 块=2 扇区)
  unsigned long nr_sectors;	// 读/写扇区数。
  char *buffer;			// 数据缓冲区。
  struct task_struct *waiting;	// 任务等待操作执行完成的地方。
  struct buffer_head *bh;	// 缓冲区头指针(include/linux/fs.h,68)。
  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;	// 请求信息结构。

};

extern struct blk_dev_struct blk_dev[NR_BLK_DEV];	// 块设备数组,每种块设备占用一项。
extern struct request request[NR_REQUEST];	// 请求队列数组。
extern struct task_struct *wait_for_request;	// 等待请求的任务结构。

#ifdef MAJOR_NR			// 主设备号。

/*
* Add entries as needed. Currently the only block devices
* supported are hard-disks and floppies.
*/
/*
* 需要时加入条目。目前块设备仅支持硬盘和软盘(还有虚拟盘)。
*/

#if (MAJOR_NR == 1)		// RAM 盘的主设备号是1。根据这里的定义可以推理内存块主设备号也为1。
/* ram disk *//* RAM 盘(内存虚拟盘) */
#define DEVICE_NAME "ramdisk"	// 设备名称ramdisk。
#define DEVICE_REQUEST do_rd_request	// 设备请求函数do_rd_request()。
#define DEVICE_NR(device) ((device) & 7)	// 设备号(0--7)。
#define DEVICE_ON(device)	// 开启设备。虚拟盘无须开启和关闭。
#define DEVICE_OFF(device)	// 关闭设备。

#elif (MAJOR_NR == 2)		// 软驱的主设备号是2。
/* floppy */
#define DEVICE_NAME "floppy"	// 设备名称floppy。
#define DEVICE_INTR do_floppy	// 设备中断处理程序do_floppy()。
#define DEVICE_REQUEST do_fd_request	// 设备请求函数do_fd_request()。
#define DEVICE_NR(device) ((device) & 3)	// 设备号(0--3)。
#define DEVICE_ON(device) floppy_on(DEVICE_NR(device))	// 开启设备函数floppyon()。
#define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))	// 关闭设备函数floppyoff()。

#elif (MAJOR_NR == 3)		// 硬盘主设备号是3。
/* harddisk */
#define DEVICE_NAME "harddisk"	// 硬盘名称harddisk。
#define DEVICE_INTR do_hd	// 设备中断处理程序do_hd()。
#define DEVICE_REQUEST do_hd_request	// 设备请求函数do_hd_request()。
#define DEVICE_NR(device) (MINOR(device)/5)	// 设备号(0--1)。每个硬盘可以有4 个分区。
#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)	// CURRENT 为指定主设备号的当前请求结构。
#define CURRENT_DEV DEVICE_NR(CURRENT->dev)	// CURRENT_DEV 为CURRENT 的设备号。

#ifdef DEVICE_INTR
void (*DEVICE_INTR) (void) = NULL;
#endif
static void (DEVICE_REQUEST) (void);

// 释放锁定的缓冲区。
extern inline void
unlock_buffer (struct buffer_head *bh)
{

  if (!bh->b_lock)		// 如果指定的缓冲区bh 并没有被上锁,则显示警告信息。
    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 为指定主设备号的当前请求结构。
      CURRENT->bh->b_uptodate = uptodate;	// 置更新标志。
      unlock_buffer (CURRENT->bh);	// 解锁缓冲区。
    
}
  if (!uptodate)
    {
				// 如果更新标志为0 则显示设备错误信息。
      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;	// 从请求链表中删除该请求项。

}

// 定义初始化请求宏。
#define INIT_REQUEST \
repeat: \
if (!CURRENT) \			// 如果当前请求结构指针为null 则返回。
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
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
linux0.00 B0% 04-10-07
<boot>0.00 B04-10-07 15:01
bootsect.s12.49 kB08-01-04 21:38
head.s13.22 kB08-01-04 21:38
setup.s12.42 kB08-01-04 21:38
<fs>0.00 B04-10-07 15:01
bitmap.c8.46 kB02-09-04 13:12
block_dev.c3.95 kB02-09-04 13:12
buffer.c17.71 kB02-09-04 13:12
char_dev.c4.09 kB02-09-04 13:12
exec.c18.90 kB02-09-04 13:12
fcntl.c3.33 kB02-09-04 13:12
file_dev.c4.82 kB02-09-04 13:12
file_table.c209.00 B02-09-04 13:12
inode.c14.92 kB02-09-04 13:12
ioctl.c1.94 kB02-09-04 13:12
Makefile6.80 kB08-01-04 21:45
namei.c37.20 kB02-09-04 13:12
open.c10.00 kB02-09-04 13:12
pipe.c5.42 kB02-09-04 13:12
read_write.c5.86 kB02-09-04 13:12
stat.c2.69 kB02-09-04 13:12
super.c13.57 kB02-09-04 13:12
truncate.c2.46 kB02-09-04 13:12
<include>0.00 B04-10-07 15:01
a.out.h8.22 kB02-09-04 13:14
<asm>0.00 B04-10-07 15:01
io.h772.00 B02-09-04 13:14
memory.h1.03 kB08-01-04 22:13
segment.h2.50 kB02-09-04 13:14
system.h4.08 kB02-09-04 13:14
const.h589.00 B02-09-04 13:14
ctype.h1.68 kB02-09-04 13:14
errno.h2.30 kB02-09-04 13:14
fcntl.h3.30 kB02-09-04 13:14
<linux>0.00 B04-10-07 15:01
config.h2.16 kB02-09-04 13:14
fs.h9.72 kB02-09-04 13:14
hdreg.h2.94 kB02-09-04 13:14
head.h760.00 B02-09-04 13:14
kernel.h1.44 kB02-09-04 13:14
mm.h473.00 B02-09-04 13:14
sched.h13.25 kB02-09-04 13:14
sys.h5.41 kB02-09-04 13:14
tty.h3.93 kB02-09-04 13:14
signal.h4.01 kB02-09-04 13:14
stdarg.h1.77 kB02-09-04 13:14
stddef.h378.00 B02-09-04 13:14
string.h21.75 kB02-09-04 13:14
<sys>0.00 B04-10-07 15:01
stat.h2.37 kB02-09-04 13:14
times.h377.00 B02-09-04 13:14
types.h1.10 kB02-09-04 13:14
utsname.h423.00 B02-09-04 13:14
wait.h1.48 kB02-09-04 13:14
termios.h13.58 kB02-09-04 13:14
time.h1.81 kB02-09-04 13:14
unistd.h9.21 kB02-09-04 13:14
utime.h392.00 B02-09-04 13:14
<init>0.00 B04-10-07 15:01
main.c12.52 kB02-09-04 13:12
<kernel>0.00 B04-10-07 15:01
asm.s5.10 kB08-01-04 22:48
<blk_drv>0.00 B04-10-07 15:01
blk.h5.69 kB02-09-04 13:14
floppy.c23.49 kB02-09-04 13:12
hd.c17.14 kB02-09-04 13:12
ll_rw_blk.c7.59 kB02-09-04 13:12
Makefile4.25 kB08-01-04 22:53
ramdisk.c6.13 kB02-09-04 13:12
<chr_drv>0.00 B04-10-07 15:01
console.c30.91 kB02-09-04 13:12
keyboard.S21.04 kB08-01-04 22:59
Makefile4.81 kB08-01-04 22:59
rs_io.s5.75 kB08-01-04 22:59
serial.c2.91 kB02-09-04 13:12
tty_io.c18.40 kB02-09-04 13:12
tty_ioctl.c10.70 kB02-09-04 13:12
exit.c8.08 kB02-09-04 13:12
fork.c6.78 kB15-07-07 22:10
<math>0.00 B04-10-07 15:01
Makefile3.21 kB08-01-04 23:01
math_emulate.c2.08 kB02-09-04 13:12
mktime.c2.72 kB02-09-04 13:12
panic.c952.00 B02-09-04 13:12
printk.c1.75 kB02-09-04 13:12
sched.c18.57 kB16-07-07 21:23
signal.c5.61 kB02-09-04 13:12
sys.c7.62 kB02-09-04 13:12
system_call.s12.53 kB08-01-04 22:48
traps.c8.29 kB04-10-07 14:57
vsprintf.c9.96 kB02-09-04 13:12
<lib>0.00 B04-10-07 15:01
close.c397.00 B02-09-04 13:12
ctype.c1.72 kB02-09-04 13:12
dup.c401.00 B02-09-04 13:12
errno.c66.00 B02-09-04 13:12
execve.c607.00 B02-09-04 13:12
Makefile4.83 kB08-01-04 22:30
malloc.c13.50 kB02-09-04 13:12
open.c1.22 kB02-09-04 13:12
setsid.c382.00 B02-09-04 13:12
string.c199.00 B02-09-04 13:12
wait.c774.00 B02-09-04 13:12
write.c545.00 B02-09-04 13:12
_exit.c616.00 B02-09-04 13:12
Makefile8.58 kB08-01-04 22:21
<mm>0.00 B04-10-07 15:01
Makefile2.91 kB08-01-04 22:34
memory.c25.10 kB02-09-04 13:12
page.s842.00 B08-01-04 22:34
<tools>0.00 B04-10-07 15:01
build.c8.12 kB02-09-04 13:12
...
Sponsored links

blk.h (286.77 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D