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

📄 buffer.c

📁 一份精简的linux内核源代码
💻 C
字号:
/* *  linux/fs/buffer.c * *  (C) 1991  Linus Torvalds *//* *  'buffer.c' implements the buffer-cache functions. Race-conditions have * been avoided by NEVER letting a interrupt change a buffer (except for the * data, of course), but instead letting the caller do it. NOTE! As interrupts * can wake up a caller, some cli-sti sequences are needed to check for * sleep-on-calls. These should be extremely quick, though (I hope). *//* * NOTE! There is one discordant note here: checking floppies for * disk change. This is where it fits best, I think, as it should * invalidate changed floppy-disk-caches. */#include <stdarg.h> #include <linux/config.h>#include <linux/sched.h>#include <linux/kernel.h>#include <asm/system.h>#include <asm/io.h>extern int end; /*内核代码末端地址,由链接程序生成*/struct buffer_head * start_buffer = (struct buffer_head *) &end;/*缓冲区开始位置*/struct buffer_head * hash_table[NR_HASH];/*NR_HASH=307*/static struct buffer_head * free_list;/*空闲缓冲链表头指针*/static struct task_struct * buffer_wait = NULL;/*等待空闲缓冲块而睡眠的任务队列*/int NR_BUFFERS = 0;/*系统含有缓冲块个数,371*/static inline void wait_on_buffer(struct buffer_head * bh)/*若指定的缓冲块上锁,则当前进程挂起(在b_wait队列中等待)*/{	cli();	while (bh->b_lock)		sleep_on(&bh->b_wait);	sti();}int sys_sync(void)/*同步块设备和内存缓冲区中的数据*/{	int i;	struct buffer_head * bh;	sync_inodes();		/* 将所有修改过的i节点结构写入缓冲区,59 */	bh = start_buffer;	for (i=0 ; i<NR_BUFFERS ; i++,bh++) {/*将缓冲区中的所有被修改过的数据写入设备中*/		wait_on_buffer(bh);/*??某个进程修改已写盘的缓冲块怎么办*/		if (bh->b_dirt)			ll_rw_block(WRITE,bh);	}	return 0;}int sync_dev(int dev)/*对指定设备进行高速缓冲区与设备上数据的同步*/{	int i;	struct buffer_head * bh;	bh = start_buffer;	for (i=0 ; i<NR_BUFFERS ; i++,bh++) {/*对缓冲区中指定设备的缓冲块(修改过的)进行写盘*/		if (bh->b_dev != dev)			continue;		wait_on_buffer(bh);		if (bh->b_dev == dev && bh->b_dirt)			ll_rw_block(WRITE,bh);	}	sync_inodes(); /* 将所有修改过的i节点结构写入缓冲区,59*/	bh = start_buffer;	for (i=0 ; i<NR_BUFFERS ; i++,bh++) {{/*对缓冲区中指定设备的缓冲块(修改过的)进行写盘*/		if (bh->b_dev != dev)			continue;		wait_on_buffer(bh);		if (bh->b_dev == dev && bh->b_dirt)			ll_rw_block(WRITE,bh);	}	return 0;}void inline invalidate_buffers(int dev){/*使指定设备在高速缓冲区中的数据无效*/{	int i;	struct buffer_head * bh;	bh = start_buffer;	for (i=0 ; i<NR_BUFFERS ; i++,bh++) {		if (bh->b_dev != dev)			continue;		wait_on_buffer(bh);		if (bh->b_dev == dev)			bh->b_uptodate = bh->b_dirt = 0;{/*缓冲块中的数据无效*/	}}/* * This routine checks whether a floppy has been changed, and * invalidates all buffer-cache-entries in that case. This * is a relatively slow routine, so we have to try to minimize using * it. Thus it is called only upon a 'mount' or 'open'. This * is the best way of combining speed and utility, I think. * People changing diskettes in the middle of an operation deserve * to loose :-) * * NOTE! Although currently this is only for floppies, the idea is * that any additional removable block-device will use this routine, * and that mount/open needn't know that floppies/whatever are * special. */void check_disk_change(int dev){/*flopy disk 不看*/{	int i;	if (MAJOR(dev) != 2)		return;	if (!floppy_change(dev & 0x03))		return;	for (i=0 ; i<NR_SUPER ; i++)		if (super_block[i].s_dev == dev)			put_super(super_block[i].s_dev);	invalidate_inodes(dev);	invalidate_buffers(dev);}#define _hashfn(dev,block) (((unsigned)(dev^block))%NR_HASH){/*dev异或block再除以NR_HASH*/#define hash(dev,block) hash_table[_hashfn(dev,block)]static inline void remove_from_queues(struct buffer_head * bh){/*从hash队列和空闲缓冲队列中移走缓冲块*/{/* remove from hash-queue */	if (bh->b_next)		bh->b_next->b_prev = bh->b_prev;	if (bh->b_prev)		bh->b_prev->b_next = bh->b_next;	if (hash(bh->b_dev,bh->b_blocknr) == bh)		hash(bh->b_dev,bh->b_blocknr) = bh->b_next;/* remove from free list */	if (!(bh->b_prev_free) || !(bh->b_next_free))		panic("Free block list corrupted");	bh->b_prev_free->b_next_free = bh->b_next_free;	bh->b_next_free->b_prev_free = bh->b_prev_free;	if (free_list == bh)		free_list = bh->b_next_free;}static inline void insert_into_queues(struct buffer_head * bh){/*将缓冲块移入hash队列中和空闲缓冲队列尾*/{/* put at end of free list */	bh->b_next_free = free_list;	bh->b_prev_free = free_list->b_prev_free;	free_list->b_prev_free->b_next_free = bh;	free_list->b_prev_free = bh;/* put the buffer in new hash-queue if it has a device */	bh->b_prev = NULL;	bh->b_next = NULL;	if (!bh->b_dev)		return;	bh->b_next = hash(bh->b_dev,bh->b_blocknr);	hash(bh->b_dev,bh->b_blocknr) = bh;	bh->b_next->b_prev = bh;}static struct buffer_head * find_buffer(int dev, int block)/*利用hash表在高速缓冲中寻找给定设备和指定块号的缓冲块*/{			struct buffer_head * tmp;	for (tmp = hash(dev,block) ; tmp != NULL ; tmp = tmp->b_next)		if (tmp->b_dev==dev && tmp->b_blocknr==block)			return tmp;	return NULL;}/* * Why like this, I hear you say... The reason is race-conditions. * As we don't lock buffers (unless we are readint them, that is), * something might happen to it while we sleep (ie a read-error * will force it bad). This shouldn't really happen currently, but * the code is ready. */struct buffer_head * get_hash_table(int dev, int block)/*利用hash表在高速缓冲中寻找给定设备和指定块号的缓冲块,并将其上锁*/{	struct buffer_head * bh;	for (;;) {		if (!(bh=find_buffer(dev,block)))			return NULL;		bh->b_count++;		wait_on_buffer(bh);		if (bh->b_dev == dev && bh->b_blocknr == block)/*防止在进程睡眠时,该缓冲块的dev或block发生变化*/			return bh;		bh->b_count--;	}}/* * Ok, this is getblk, and it isn't very clear, again to hinder * race-conditions. Most of the code is seldom used, (ie repeating), * so it should be much more efficient than it looks. * * The algoritm is changed: hopefully better, and an elusive bug removed. */#define BADNESS(bh) (((bh)->b_dirt<<1)+(bh)->b_lock)/*设某一缓冲块的权重*/struct buffer_head * getblk(int dev,int block)/*取高速缓冲区中指定的缓冲块,若直接找到则返回,否则在缓冲区中设置一个新项*/{	struct buffer_head * tmp, * bh;repeat:	if (bh = get_hash_table(dev,block))		return bh;	tmp = free_list;	do {		if (tmp->b_count)/*先除去已被别的进程引用的缓冲块*//*!一个未被引用的缓冲块不一定就是干净的或没有上锁的*/			continue;		if (!bh || BADNESS(tmp)<BADNESS(bh)) {/*在缓冲队列中找出权值最小的缓冲块*/			bh = tmp;			if (!BADNESS(tmp))				break;		}/* and repeat until we find something good */	} while ((tmp = tmp->b_next_free) != free_list);	if (!bh) {/*无空闲缓冲块(b_count非0)则挂起当前进程*/		sleep_on(&buffer_wait);		goto repeat;	}	wait_on_buffer(bh);/*有空闲缓冲块,但该块未解锁*/	if (bh->b_count)		goto repeat;	while (bh->b_dirt) {/*写盘(可能刚写入某一块,但该块立刻又被更改,但这不影响)*/		sync_dev(bh->b_dev);		wait_on_buffer(bh);		if (bh->b_count)			goto repeat;	}/* NOTE!! While we slept waiting for this block, somebody else might *//* already have added "this" block to the cache. check it */	if (find_buffer(dev,block))		goto repeat;/* OK, FINALLY we know that this buffer is the only one of it's kind, *//* and that it's unused (b_count=0), unlocked (b_lock=0), and clean */	bh->b_count=1;/*设缓冲头结构*/	bh->b_dirt=0;	bh->b_uptodate=0;	remove_from_queues(bh);	bh->b_dev=dev;	bh->b_blocknr=block;	insert_into_queues(bh);	return bh;}void brelse(struct buffer_head * buf)/*释放缓冲块(b_count-1,唤醒等待该缓冲块的进程)*/{	if (!buf)		return;	wait_on_buffer(buf);	if (!(buf->b_count--))		panic("Trying to free free buffer");	wake_up(&buffer_wait);}/* * bread() reads a specified block and returns the buffer that contains * it. It returns NULL if the block was unreadable. */struct buffer_head * bread(int dev,int block)/*从设备上读取设备块*/{	struct buffer_head * bh;	if (!(bh=getblk(dev,block)))/*此处返回的块一定是未上锁的*/		panic("bread: getblk returned NULL\n");	if (bh->b_uptodate)/*若缓冲区中已有则直接返回*/		return bh;	ll_rw_block(READ,bh);/*底层读设备操作*/	wait_on_buffer(bh);	if (bh->b_uptodate)		return bh;	brelse(bh);	return NULL;}#define COPYBLK(from,to) \__asm__("cld\n\t" \	"rep\n\t" \	"movsl\n\t" \	::"c" (BLOCK_SIZE/4),"S" (from),"D" (to) \	:"cx","di","si")/* * bread_page reads four buffers into memory at the desired address. It's * a function of its own, as there is some speed to be got by reading them * all at the same time, not waiting for one to be read, and then another * etc. */void bread_page(unsigned long address,int dev,int b[4])/*读入4个缓冲块并将其存入指定的地址处(该函数仅用于memory.c中的do_no_page中)*/{	struct buffer_head * bh[4];	int i;	for (i=0 ; i<4 ; i++)		if (b[i]) {			if (bh[i] = getblk(dev,b[i]))				if (!bh[i]->b_uptodate)					ll_rw_block(READ,bh[i]);		} else			bh[i] = NULL;	for (i=0 ; i<4 ; i++,address += BLOCK_SIZE)		if (bh[i]) {			wait_on_buffer(bh[i]);			if (bh[i]->b_uptodate)				COPYBLK((unsigned long) bh[i]->b_data,address);			brelse(bh[i]);		}}/* * Ok, breada can be used as bread, but additionally to mark other * blocks for reading as well. End the argument list with a negative * number. */struct buffer_head * breada(int dev,int first, ...)/*预读入一些块*/{	va_list args;/*函数可变参数*/	struct buffer_head * bh, *tmp;	va_start(args,first);/*读第一个块*/	if (!(bh=getblk(dev,first)))		panic("bread: getblk returned NULL\n");	if (!bh->b_uptodate)		ll_rw_block(READ,bh);	while ((first=va_arg(args,int))>=0) {/*预读几个块*/		tmp=getblk(dev,first);		if (tmp) {			if (!tmp->b_uptodate)				ll_rw_block(READA,bh);			tmp->b_count--;/*因为是预读,所以仅仅是把该块读入缓冲区,但引用计数仍为0*/		}	}	va_end(args);	wait_on_buffer(bh);	if (bh->b_uptodate)		return bh;	brelse(bh);	return (NULL);}void buffer_init(long buffer_end)/*缓冲区初始化*/{	struct buffer_head * h = start_buffer;	void * b;	int i;	if (buffer_end == 1<<20)        /*去除显存*/		b = (void *) (640*1024);	else		b = (void *) buffer_end;	while ( (b -= BLOCK_SIZE) >= ((void *) (h+1)) ) { /*若剩余内存仍够分配一个缓冲块(1kb),则分配*/		h->b_dev = 0; /*使用该缓冲块的设备号*/		h->b_dirt = 0; /*缓冲块修改标志*/		h->b_count = 0; /*缓冲块应用计数*/		h->b_lock = 0; /*缓冲块锁定标志*/		h->b_uptodate = 0; /*缓冲块更新标志*/		h->b_wait = NULL;/*指向等待该缓冲块解锁的进程*/		h->b_next = NULL;/*指向具有相同hash值的下一个缓冲头*/		h->b_prev = NULL;/*指向具有相同hash值的上一个缓冲头*/		h->b_data = (char *) b;/*指向对应缓冲块数据块*/		h->b_prev_free = h-1;/*指向链表中前一项*/		h->b_next_free = h+1;/*指向链表中下一项*/		h++;                  /*指向下一个新缓冲头位置*/		NR_BUFFERS++;         /*缓冲区块数累加*/		if (b == (void *) 0x100000)/*跳过显存*/			b = (void *) 0xA0000;	}	h--;	free_list = start_buffer;/*形成一个环*/	free_list->b_prev_free = h;	h->b_next_free = free_list;	for (i=0;i<NR_HASH;i++)		hash_table[i]=NULL;}	

⌨️ 快捷键说明

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