linux 0.11 ÄÚºËѧϰ file_dev.c
/*
* linux/fs/file_dev.c
*
* (C) 1991 Linus Torvalds
*/
#include <errno.h>
#include <fcntl.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
/* ¸ù¾Ýi ½ÚµãºÍÎļþ½á¹¹£¬¶ÁÉ豸Êý¾Ý£¬ÎªÉϲãµÄreadº¯ÊýÌṩº¯Êý */
int file_read(struct m_inode * inode, struct file * filp, char * buf, int count)
{
// ÓÉi ½Úµã¿ÉÒÔÖªµÀÉ豸ºÅ£¬ÓÉfilp ½á¹¹¿ÉÒÔÖªµÀÎļþÖе±Ç°¶ÁдָÕëλÖá£buf Ö¸¶¨Óû§Ì¬ÖÐ
// »º³åÇøµÄλÖã¬count ΪÐèÒª¶ÁÈ¡µÄ×Ö½ÚÊý¡£·µ»ØÖµÊÇʵ¼Ê¶ÁÈ¡µÄ×Ö½ÚÊý£¬»ò³ö´íºÅ(СÓÚ0)
int left,chars,nr;
struct buffer_head * bh;
// ÈôÐèÒª¶ÁÈ¡µÄ×Ö½Ú¼ÆÊýֵСÓÚµÈÓÚÁ㣬Ôò·µ»Ø
if ((left=count)<=0)
return 0;
while (left)
{
// ¸ù¾Ýi ½ÚµãºÍÎļþ±í½á¹¹ÐÅÏ¢£¬È¡Êý¾Ý¿éÎļþµ±Ç°¶ÁдλÖÃÔÚÉ豸É϶ÔÓ¦µÄÂß¼¿éºÅnr¡£Èônr ²»
// Ϊ0£¬Ôò´Ói ½ÚµãÖ¸¶¨µÄÉ豸É϶ÁÈ¡¸ÃÂß¼¿é
if (nr = bmap(inode,(filp->f_pos)/BLOCK_SIZE)) // µÃµ½Âß¼¿éºÅ
{
if (!(bh=bread(inode->i_dev,nr)))
break;
} else
bh = NULL;
// ¼ÆËãÎļþ¶ÁдָÕëÔÚÊý¾Ý¿éÖÐµÄÆ«ÒÆÖµnr
nr = filp->f_pos % BLOCK_SIZE;
// È»ºóÓ뻹Ðè¶ÁÈ¡µÄ×Ö½ÚÊýleft ×÷±È½Ï£¬ÆäÖÐСֵ¼´Îª±¾´ÎÐè¶ÁµÄ×Ö½ÚÊýchars
chars = MIN( BLOCK_SIZE-nr , left );
// µ÷Õû¶ÁдÎļþÖ¸Õë
filp->f_pos += chars;
// Ê£Óà×Ö½Ú¼ÆÊýÏàÓ¦¼õÈ¥chars
left -= chars;
if (bh) // Èç¹û¶Á³öÁËÊý¾Ý
{
char * p = nr + bh->b_data; // ½«p Ö¸Ïò¶Á³öÊý¾Ý¿é»º³åÇøÖпªÊ¼¶ÁÈ¡µÄλÖÃ
// ¸´ÖÆchars×Ö½Úµ½Óû§µÄ»º³åÇø
while (chars-->0)
put_fs_byte(*(p++),buf++);
brelse(bh);
}
// ·ñÔò½«Óû§µÄ»º³åÇøÈ«²¿ÉèÖóÉ0
else
{
/*************************************/
while (chars-->0)
put_fs_byte(0,buf++);
/**************************************/
}
}
// Ð޸ĸÃi ½ÚµãµÄ·ÃÎÊʱ¼äΪµ±Ç°Ê±¼ä¡£·µ»Ø¶ÁÈ¡µÄ×Ö½ÚÊý
inode->i_atime = CURRENT_TIME;
return (count-left)?(count-left):-ERROR;
/*
* ¸Ãº¯ÊýʵÏÖµÄÊǸ
Ïà¹ØÎĵµ£º
1. LinusºÍBill
ËÆºõÔÚÒ»Ò¹Ö®¼ä£¬Õâ¸öÃû×ÖͻȻ±äµÃͬÏó±È¶û¡£¸Ç´ÄÒ»ÑùµÄ¶úÊìÄÜÏê¡£µ«ÊDZȶû¡£¸Ç´Ä£¬ÄÄÅÂÉí¼ÛÓÐÊý²»Çå±¶µÄ°ÙÍòÃÀ½ð£¬Ò²ÓÀÔ¶²»»á±ä³ÉLinux Torvalds¡£Õâλ29ËêµÄ·ÒÀ¼ÈË£¬¼òµ¥¶øÇ¿´óµÄLinux²Ù×÷ϵͳµÄ´´ÔìÕߣ¬³¬Ô½Á˸ǴĵÄÉñ»°¡£Ò²Óд«ÎÅ˵¸Ç´ÄÊÇÒ»¸ö׿ԽµÄ³ÌÐòÔ±£¬µ«LinusÊÇ»õÕæ¼ÛʵµÄ¸ßÊ ......
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH&nbs ......
°æÈ¨ÉùÃ÷£ºÔ´´×÷Æ·£¬ÔÊÐí×ªÔØ£¬×ªÔØÊ±ÇëÎñ±ØÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕ Ôʼ³ö´¦ ¡¢×÷ÕßÐÅÏ¢ºÍ±¾ÉùÃ÷¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£http://blog.csdn.net/Neverland2012
ÎÒÃǾ³£»áÈ¥ÏÂÔØ±ð ......
×ªÔØÊ±Çë×¢Ã÷³ö´¦ºÍ×÷ÕßÁªÏµ·½Ê½
ÎÄÕ³ö´¦£ºhttp://www.limodev.cn/blog
×÷ÕßÁªÏµ·½Ê½£ºÀîÏȾ² <xianjimli at hotmail dot com>
Ëæ×ÅXPµÄÁ÷ÐУ¬ÈËÃÇÔ½À´Ô½×¢ÖØÈí¼þµÄǰÆÚÉè¼Æ¡¢ºóÆÚµÄʵÏÖ£¬ÒÔ¼°¹á´©ÓÚÆäÖеIJâÊÔ¹¤×÷£¬¾¹ýÕâ¸ö¹ý³Ì³öÀ´µÄ×ÔÈ»ÊǸßÖÊÁ¿µÄÈí¼þ¡£ÉõÖÁÓÐÈËÉù³ÆXP»áÌÔ̵÷ÊÔÆ÷£¡Õ⵱ȻÊÇÓÐÒ»¶¨µÀÀíµÄ£¬È ......
/*
* ¸ÃÎļþʵÏÖϵͳµ÷ÓÃread£¬writeºÍlseek¡£
*/
/*
* linux/fs/read_write.c
*
* (C) 1991 Linus Torvalds
*/
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/kernel.h>
#include < ......