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

📄 inode.c

📁 一个完整的SHELL实现源代码
💻 C
字号:
#include "fs.h"

inode g_inode[NINODE]={0};

/* copy inode_o src into inode dst as the ino'th inode.
*/
static s_int inode_o2m(inode *dst,inode_o *src,s_int ino)
{
	s_int k;
	dst->flag=ILOCK | IUSE;
	dst->count=1; /* use count */
	dst->dev=0;  /* not used */
	dst->number=ino;
	dst->mode=src->mode;
	dst->nlink=src->nlink;
	dst->uid=src->uid;
	dst->gid=src->gid;
	dst->size0=src->size0;
	dst->size1=src->size1;
	for(k=0;k<8;k++)
		dst->adr[k]=src->addr[k]; /* i known it is not good when linux and unix v6 do not use more var i */
	dst->lastr=src->mtime[0];
	return 0;
}

/* copy inode src into inode_o dst.
*/
static s_int inode_m2o(inode_o *dst,inode *src)
{
	s_int k;
	dst->mode=src->mode;
	dst->nlink=src->nlink;
	dst->uid=src->uid;
	dst->gid=src->gid;
	dst->size0=src->size0;
	dst->size1=src->size1;
	for(k=0;k<8;k++)
		dst->addr[k]=src->adr[k];
	dst->mtime[0]=src->lastr;
	return 0;
}

/* iget(ino)
   =========
   get the No.'ino'th inode from hdd to mem
*/
inode *iget(s_int ino)
{
	inode_o *pbuf;
	inode *ip;
	/* find inode in memory */
	for(ip=&g_inode[0];ip<&g_inode[NINODE];ip++)
	{
		/*if((ip->mode & IALLOC) == 0)continue;*/
		if((ip->flag & IUSE) == 0)
		{
			continue;
		}
		if(ino==ip->number)
		{
			if(ip->flag&ILOCK)
			{
				/*p->flag|=IWAIT;*/
			}
			else
			{
				ip->flag|=ILOCK;
			}
			ip->count++;
			return ip; /* found the No.ino inode */
		}
	}
	/* not found in memory, find a free inode record */
	for(ip=&g_inode[0];ip<&g_inode[NINODE];ip++)
	{
		if((ip->flag&ILOCK)==0) /* a free inode record, not being used */
			break;
	}
	if(ip == &g_inode[NINODE])
	{
		panic("iget():g_inode empty.");
		return NULL;
	}
	/* swap old inode out, save it to hdd 
	     if it has been used before
	*/
	if(ip->flag & IUSE) 
	{
		pbuf=(inode_o *)bread((ip->number>>4)+2);
		if(!pbuf)
		{
			#ifdef DEBUG_MODE
			printf("iget: bread return NULL.\n");
			#endif
			return NULL; /* failed to load it */
		}
		inode_m2o(&pbuf[ip->number & 0x0f],ip);
	}

	/* Not in g_inode[], or say not in memory. load it */
	pbuf=(inode_o *)bread((ino>>4)+2);
	if(!pbuf)
	{
		#ifdef DEBUG_MODE
		printf("iget: bread return NULL.\n");
		#endif
		return NULL; /* failed to load it */
	}
	inode_o2m(ip,&pbuf[ino&0x0f],ino);
	return ip;
}


/* iput(ino)
   =========
   let *ip inode free from mem, and ready to swap out
*/
s_int iput(inode *ip)
{
       ip->count--;
       if(ip->count<=0)           /* if it is not used by others */
       	ip->flag&=~ILOCK; /* allow it swap out */
	return 0;
}

/*分配i结点函数*/
inode *ialloc()
{
	s_int ino;
	s_int *bp;
	s_int i,j;
	inode *ip;
	inode_o *op;
	loop:
	if(g_sblock.ninode>0)
	{
		ino=g_sblock.inode[--g_sblock.ninode];
		g_sblock.fmod=1;/* super block modified */
		ip=iget(ino);
		if(ip==NULL)return NULL;
		if(ip->mode==0)
		{
			for(bp=&ip->mode;bp<&ip->adr[8];)*bp++=0;
			return ip;
		}
		/* Inode was allocated after all.
		 * look some more.
		 */
		iput(ip);
		goto loop;
	}
	for(i=g_sblock.isize+1;i>=2;i--)
	{
		op=(inode_o *)bread(i);
		for(j=0;j<512/sizeof(inode_o);j++)
		{
			if((op[j].mode & IALLOC) == 0)
			{
				g_sblock.inode[g_sblock.ninode++]=((i-2)<<4)+j;
				g_sblock.fmod=1;/* super block modified */
				if(g_sblock.ninode==100)goto loop;
			}
		}
	}
	if(g_sblock.ninode>0)goto loop;
	return NULL;
}

/*释放i结点函数*/
s_int ifree(s_int ino)
{
	inode *ip;
	ip=iget(ino);
	if(ip==NULL)return 1;
	ip->mode=0;
	iput(ip);
	if(g_sblock.ninode<99)
	{
		g_sblock.inode[g_sblock.ninode++]=ino;
		g_sblock.fmod = 1; /* super block modified */
	}
	/*else
	{
		hd_write(bno,&g_sblock);
		g_sblock.nfree=1;
		g_sblock.free[0]=bno;
	}*/
	return 0;	
}

s_int iwrite_back()
{
	inode_o *pbuf;
	inode *ip;
	for(ip=&g_inode[0];ip<&g_inode[NINODE];ip++)
	{
		/* all used inode should be saved to hdd */
		if((ip->flag & IUSE) ==0)continue;
		pbuf=(inode_o *)bread((ip->number>>4)+2);
		if(!pbuf)
		{
			#ifdef DEBUG_MODE
			printf("iwrite_back: bread return NULL.\n");
			#endif
			return NULL; /* failed to load it */
		}
		inode_m2o(&pbuf[ip->number & 0x0f],ip);
	}
	return 0;
}

s_int iname()
{
	return 0;
}

⌨️ 快捷键说明

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