⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block_dev.c

📁 自已看吧
💻 C
字号:
#include <errno.h>

#include <linux/fs.h>
#include <linux/kernel.h>
#include <asm/segment.h>

#define NR_BLK_DEV ((sizeof (rd_blk))/(sizeof (rd_blk[0])))

int block_write(int dev, long * pos, char * buf, int count)	//从buf写count个数据到指定dev文件中指定的地方
{
	int block = *pos / BLOCK_SIZE;		//计算块号
	int offset = *pos % BLOCK_SIZE;		//计算块内位移
	int chars;
	int written = 0;
	struct buffer_head * bh;
	register char * p;

	while (count>0) {
		bh = bread(dev,block);
		if (!bh)
			return written?written:-EIO;
		chars = (count<BLOCK_SIZE) ? count : BLOCK_SIZE;	//要写的字符是否少于1块
		p = offset + bh->b_data;					//指针指向要写数据的地方
		offset = 0;
		block++;
		*pos += chars;
		written += chars;
		count -= chars;
		while (chars-->0)
			*(p++) = get_fs_byte(buf++);		//实际写数据
		bh->b_dirt = 1;
		brelse(bh);
	}
	return written;
}

int block_read(int dev, unsigned long * pos, char * buf, int count)		//从dev文件中指定的pos位置读数据到buf中
{
	int block = *pos / BLOCK_SIZE;
	int offset = *pos % BLOCK_SIZE;
	int chars;
	int read = 0;
	struct buffer_head * bh;
	register char * p;

	while (count>0) {
		bh = bread(dev,block);
		if (!bh)
			return read?read:-EIO;
		chars = (count<BLOCK_SIZE) ? count : BLOCK_SIZE;
		p = offset + bh->b_data;
		offset = 0;
		block++;
		*pos += chars;
		read += chars;
		count -= chars;
		while (chars-->0)
			put_fs_byte(*(p++),buf++);
		bh->b_dirt = 1;
		brelse(bh);
	}
	return read;
}

extern void rw_hd(int rw, struct buffer_head * bh);

typedef void (*blk_fn)(int rw, struct buffer_head * bh);	//数值指针,在这里代表2个参数指针,一个指向rw,一个指向bh

static blk_fn rd_blk[]={
	NULL,		/* nodev */
	NULL,		/* dev mem */
	NULL,		/* dev fd */
	rw_hd,		/* dev hd */			//硬盘读写驱动程序,在hd.c中定义
	NULL,		/* dev ttyx */
	NULL,		/* dev tty */
	NULL};		/* dev lp */

void ll_rw_block(int rw, struct buffer_head * bh)	//在这里实现磁盘的真正的读写
{
	blk_fn blk_addr;
	unsigned int major;

	if ((major=MAJOR(bh->b_dev)) >= NR_BLK_DEV || !(blk_addr=rd_blk[major]))
		panic("Trying to read nonexistent block-device");
	blk_addr(rw, bh);			//在这里实际上是rw_hb(rw,bh)
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -