inode.c ( File view )

  • By 2010-08-31
  • View(s):24
  • Download(s):2
  • Point(s): 1
			/*
* linux/fs/inode.c
*
* (C) 1991 Linus Torvalds
*/

#include 		// ַͷļҪһЩйַǶ뺯
#include 		// ļ״̬ͷļļļϵͳ״̬ṹstat{

}ͳ

#include 	// ȳͷļṹtask_structʼ0 ݣ
// һЩйúͻȡǶʽຯ䡣
#include 	// ںͷļһЩں˳úԭζ塣
#include 		// ڴͷļҳСһЩҳͷźԭ͡
#include 		// ϵͳͷļû޸/жŵȵǶʽꡣ

struct m_inode inode_table[NR_INODE] = {
 {
0,
}, 
};	// ڴi ڵNR_INODE=32 

static void read_inode (struct m_inode *inode);
static void write_inode (struct m_inode *inode);

//// ȴָi ڵá
// i ڵѱ򽫵ǰΪжϵĵȴ״ֱ̬i ڵ
static inline void
wait_on_inode (struct m_inode *inode)
{

  cli ();
  while (inode->i_lock)
    sleep_on (&inode->i_wait);
  sti ();

}

//// ָi ڵָi ڵ㣩
// i ڵѱ򽫵ǰΪжϵĵȴ״ֱ̬i ڵȻ
static inline void
lock_inode (struct m_inode *inode)
{

  cli ();
  while (inode->i_lock)
    sleep_on (&inode->i_wait);
  inode->i_lock = 1;		// ־
  sti ();

}

//// ָi ڵ
// λi ڵ־ȷػѵȴi ڵĽ̡
static inline void
unlock_inode (struct m_inode *inode)
{

  inode->i_lock = 0;
  wake_up (&inode->i_wait);

}

//// ͷڴ豸dev i ڵ㡣
// ɨڴеi ڵ飬ָ豸ʹõi ڵͷ֮
void
invalidate_inodes (int dev)
{

  int i;
  struct m_inode *inode;

  inode = 0 + inode_table;	// ָָi ڵָ
  for (i = 0; i < NR_INODE; i++, inode++)
    {
				// ɨi ڵָеi ڵ㡣
      wait_on_inode (inode);	// ȴi ڵã
      if (inode->i_dev == dev)
	{
			// ָ豸i ڵ㣬
	  if (inode->i_count)	// Ϊ0ʾ棻
	    printk ("inode in use on removed disk\n\r");
	  inode->i_dev = inode->i_dirt = 0;	// ͷŸi ڵ(豸Ϊ0 )
	
}
    
}

}

//// ͬi ڵ㡣
// ͬڴ豸ϵi ڵϢ
void
sync_inodes (void)
{

  int i;
  struct m_inode *inode;

  inode = 0 + inode_table;	// ָָi ڵָ
  for (i = 0; i < NR_INODE; i++, inode++)
    {
				// ɨi ڵָ顣
      wait_on_inode (inode);	// ȴi ڵã
      if (inode->i_dirt && !inode->i_pipe)	// i ڵ޸Ҳǹܵڵ㣬
	write_inode (inode);	// д̡
    
}

}

//// ļݿӳ䵽̿Ĵ(block λͼbmap - block map)
// inode C ļi ڵ㣻block C ļеݿţcreate - ־
// ־λڶӦ߼鲻ʱ´̿顣
// block ݿӦ豸ϵ߼ţ̿ţ
static int
_bmap (struct m_inode *inode, int block, int create)
{

  struct buffer_head *bh;
  int i;

// С0
  if (block < 0)
    panic ("_bmap: block<0");
// Ŵֱӿ + ӿ + μӿļϵͳʾΧ
  if (block >= 7 + 512 + 512 * 512)
    panic ("_bmap: block>big");
// ÿС7ʹֱӿʾ
  if (block < 7)
    {

// ־λi ڵжӦÿ߼飨ΣֶΪ0Ӧ豸һ
// 飨߼飬飩߼ţ̿ţ߼ֶСȻi ڵ޸ʱ䣬
// i ڵ޸ı־󷵻߼š
      if (create && !inode->i_zone[block])
	if (inode->i_zone[block] = new_block (inode->i_dev))
	  {

	    inode->i_ctime = CURRENT_TIME;
	    inode->i_dirt = 1;
	  
}
      return inode->i_zone[block];
    
}
// ÿ>=7С7+512˵һμӿ顣һμӿд
  block -= 7;
  if (block < 512)
    {

// ǴҸi ڵжӦӿֶΪ0ļ״ʹüӿ飬
// һ̿ڴżӿϢʵʴ̿ӿֶСȻi ڵ
// ޸ı־޸ʱ䡣
      if (create && !inode->i_zone[7])
	if (inode->i_zone[7] = new_block (inode->i_dev))
	  {

	    inode->i_dirt = 1;
	    inode->i_ctime = CURRENT_TIME;
	  
}
// ʱi ڵӿֶΪ0̿ʧܣ0 ˳
      if (!inode->i_zone[7])
	return 0;
// ȡ豸ϵһμӿ顣
      if (!(bh = bread (inode->i_dev, inode->i_zone[7])))
	return 0;
// ȡüӿϵblock е߼ţ̿ţ
      i = ((unsigned short *) (bh->b_data))[block];
// ǴҼӿĵblock е߼Ϊ0 Ļһ̿飨߼飩
// ӿеĵblock ڸ߼šȻλӿ޸ı־
      if (create && !i)
	if (i = new_block (inode->i_dev))
	  {

	    ((unsigned short *) (bh->b_data))[block] = i;
	    bh->b_dirt = 1;
	  
}
// ͷŸüӿ飬شĶӦblock ߼Ŀš
      brelse (bh);
      return i;
    
}
// еˣݿǶμӿ飬һμӿơǶԶμӿĴ
// block ټȥӿɵĿ(512)
  block -= 512;
// ´i ڵĶμӿֶΪ0һ̿ڴŶμӿһ
// Ϣʵʴ̿μӿֶС֮i ڵ޸ıƺ޸ʱ䡣
  if (create && !inode->i_zone[8])
    if (inode->i_zone[8] = new_block (inode->i_dev))
      {

	inode->i_dirt = 1;
	inode->i_ctime = CURRENT_TIME;
      
}
// ʱi ڵμӿֶΪ0̿ʧܣ0 ˳
  if (!inode->i_zone[8])
    return 0;
// ȡöμӿһ顣
  if (!(bh = bread (inode->i_dev, inode->i_zone[8])))
    return 0;
// ȡöμӿһϵ(block/512)е߼š
  i = ((unsigned short *) bh->b_data)[block >> 9];
// ǴҶμӿһϵ(block/512)е߼Ϊ0 Ļһ
// 飨߼飩ΪμӿĶ飬öμӿһе(block/512)ڸö
// ĿšȻλμӿһ޸ı־ͷŶμӿһ顣
  if (create && !i)
    if (i = new_block (inode->i_dev))
      {

	((unsigned short *) (bh->b_data))[block >> 9] = i;
	bh->b_dirt = 1;
      
}
  brelse (bh);
// μӿĶΪ0ʾ̿ʧܣ0 ˳
  if (!i)
    return 0;
// ȡμӿĶ顣
  if (!(bh = bread (inode->i_dev, i)))
    return 0;
// ȡöϵblock е߼š(511 Ϊ޶block ֵ511)
  i = ((unsigned short *) bh->b_data)[block & 511];
// ǴҶĵblock е߼Ϊ0 Ļһ̿飨߼飩Ϊ
// մϢĿ顣öеĵblock ڸ߼(i)Ȼλ
// ޸ı־
  if (create && !i)
    if (i = new_block (inode->i_dev))
      {

	((unsigned short *) (bh->b_data))[block & 511] = i;
	bh->b_dirt = 1;
      
}
// ͷŸöμӿĶ飬شĶӦblock ߼Ŀš
  brelse (bh);
  return i;

}

//// i ڵϢȡļݿblock 豸϶Ӧ߼š
int
bmap (struct m_inode *inode, int block)
{

  return _bmap (inode, block, 0);

}

//// ļݿblock 豸϶Ӧ߼飬豸϶Ӧ߼š
int
create_block (struct m_inode *inode, int block)
{

  return _bmap (inode, block, 1);

}

//// ͷһi ڵ(д豸)
void
iput (struct m_inode *inode)
{

  if (!inode)
    return;
  wait_on_inode (inode);	// ȴinode ڵ(Ļ)
  if (!inode->i_count)
    panic ("iput: trying to free free inode");
// ǹܵi ڵ㣬ѵȴùܵḶ̌ô1򷵻ءͷ
// ܵռõڴҳ棬λýڵüֵ޸ı־͹ܵ־ء
// pipe ڵ㣬inode->i_size ڴҳַμget_pipe_inode()228234 С
  if (inode->i_pipe)
    {

      wake_up (&inode->i_wait);
      if (--inode->i_count)
	return;
      free_page (inode->i_size);
      inode->i_count = 0;
      inode->i_dirt = 0;
      inode->i_pipe = 0;
      return;
    
}
// i ڵӦ豸=0򽫴˽ڵüݼ1ء
  if (!inode->i_dev)
    {

      inode->i_count--;
      return;
    
}
// ǿ豸ļi ڵ㣬ʱ߼ֶ0 豸ţˢ¸豸ȴi ڵ
  if (S_ISBLK (inode->i_mode))
    {

      sync_dev (inode->i_zone[0]);
      wait_on_inode (inode);
    
}
repeat:
// i ڵü1ݼ1
  if (inode->i_count > 1)
    {

      inode->i_count--;
      return;
    
}
// i ڵΪ0ͷŸi ڵ߼飬ͷŸi ڵ㡣
  if (!inode->i_nlinks)
    {

      truncate (inode);
      free_inode (inode);
      return;
    
}
// i ڵ޸ģ¸i ڵ㣬ȴi ڵ
  if (inode->i_dirt)
    {

      write_inode (inode);	/* we can sleep - so do again */
      wait_on_inode (inode);
      goto repeat;
    
}
// i ڵüݼ1
  inode->i_count--;
  return;

}

//// i ڵ(inode_table)лȡһi ڵ
// Ѱücount Ϊ0 i ڵ㣬д̺㣬ָ롣
struct m_inode *
get_empty_inode (void)
{

  struct m_inode *inode;
  static struct m_inode *last_inode = inode_table;	// last_inode ָi ڵһ
  int i;

  do
    {

// ɨi ڵ
      inode = NULL;
      for (i = NR_INODE; i; i--)
	{

// last_inode Ѿָi ڵ1 ָ֮i ڵʼ
	  if (++last_inode >= inode_table + NR_INODE)
	    last_inode = inode_table;
// last_inode ָi ڵļֵΪ0˵ҵi ڵinode ָ
// i ڵ㡣i ڵ޸ı־־Ϊ0ǿʹøi ڵ㣬˳ѭ
	  if (!last_inode->i_count)
	    {

	      inode = last_inode;
	      if (!inode->i_dirt && !inode->i_lock)
		break;
	    
}
	
}
// ûҵi ڵ(inode=NULL)i ڵӡʹã
      if (!inode)
	{

	  for (i = 0; i < NR_INODE; i++)
	    printk ("%04x: %6d\t", inode_table[i].i_dev,
		    inode_table[i].i_num);
	  panic ("No free inodes in mem");
	
}
// ȴi ڵֱĻ
      wait_on_inode (inode);
// i ڵ޸ı־λĻ򽫸i ڵˢ£ȴi ڵ
      while (inode->i_dirt)
	{

	  write_inode (inode);
	  wait_on_inode (inode);
	
}
    
}
  while (inode->i_count);	// i ڵֱռõĻѰҿi ڵ㡣
// ҵi ڵ򽫸i ڵ㣬
...
...
(Not finished, please download and read the complete file)
			
...
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

inode.c (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