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

📄 inode.c

📁 linux的文件系统的部分代码的详细注释
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *  linux/fs/ext2/inode.c * * Copyright (C) 1992, 1993, 1994, 1995 * Remy Card (card@masi.ibp.fr) * Laboratoire MASI - Institut Blaise Pascal * Universite Pierre et Marie Curie (Paris VI) * *  from * *  linux/fs/minix/inode.c * *  Copyright (C) 1991, 1992  Linus Torvalds * *  Goal-directed block allocation by Stephen Tweedie * 	(sct@dcs.ed.ac.uk), 1993, 1998 *  Big-endian to little-endian byte-swapping/bitmaps by *        David S. Miller (davem@caip.rutgers.edu), 1995 *  64-bit file support on 64-bit platforms by Jakub Jelinek * 	(jj@sunsite.ms.mff.cuni.cz) * *  Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000 */#include <linux/fs.h>#include <linux/ext2_fs.h>#include <linux/locks.h>#include <linux/smp_lock.h>#include <linux/sched.h>#include <linux/highuid.h>#include <linux/quotaops.h>#include <linux/module.h>MODULE_AUTHOR("Remy Card and others");MODULE_DESCRIPTION("Second Extended Filesystem");MODULE_LICENSE("GPL");/*更新索引节点*/static int ext2_update_inode(struct inode * inode, int do_sync);/* * Called at each iput() */ /*释放一个inode的预分配块*/void ext2_put_inode (struct inode * inode){	ext2_discard_prealloc (inode);/*预分配块失效,	                                                    *重新设置设备块组描述子和块组数据块位图	                                                    */}/* * Called at the last iput() if i_nlink is zero. */ /*删除一个inode对象*/void ext2_delete_inode (struct inode * inode){	lock_kernel();/*给内核上锁*/       /*如果是受损inode,或是索引节点号是EXT2_ACL_IDX_INO或EXT2_ACL_DATA_INO,        *那么,不删除该inode*/	if (is_bad_inode(inode) ||	    inode->i_ino == EXT2_ACL_IDX_INO ||	    inode->i_ino == EXT2_ACL_DATA_INO)		goto no_delete;	inode->u.ext2_i.i_dtime	= CURRENT_TIME;/*设置被删除时间为当前时间*/	mark_inode_dirty(inode);/*标志inode脏*/	ext2_update_inode(inode, IS_SYNC(inode));/*更新inode*/	inode->i_size = 0;/*置文件的大小(以字节为单位)为0*/	if (inode->i_blocks)/*该文件所占块数不为0*/		ext2_truncate (inode);/*截短清空inode*/	ext2_free_inode (inode);/*释放inode*/	unlock_kernel();/*给内核解锁*/	return;no_delete:	unlock_kernel();/*给内核解锁*/	clear_inode(inode);	/* We must guarantee clearing of inode... */	/*清除一个inode对象中的内容*/}/*预分配块失效,重新设置设备块组描述子和块组数据块位图*/void ext2_discard_prealloc (struct inode * inode){#ifdef EXT2_PREALLOCATE	lock_kernel();/*给内核上锁*/	/* Writer: ->i_prealloc* */	if (inode->u.ext2_i.i_prealloc_count) {/*有预分配的数据块*/		unsigned short total = inode->u.ext2_i.i_prealloc_count;/*total=预分配的数据块的块号码的数量*/		unsigned long block = inode->u.ext2_i.i_prealloc_block;/*block=预分配的数据块的块号码*/		inode->u.ext2_i.i_prealloc_count = 0;/*置预分配的数据块的块号码的数量为0*/		inode->u.ext2_i.i_prealloc_block = 0;/*置预分配的数据块的块号码为0*/		/* Writer: end */		ext2_free_blocks (inode, block, total);/*释放块,更新限额和i_blocks域*/		/* Free given blocks, update quota and i_blocks field */	}	unlock_kernel();/*给内核解锁*/#endif}/*分配块函数*//*如果分配块正好是预分配号,就不用新分配, *如果不是,就调用ext2_new_block()新分配一个物理块 */static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err){#ifdef EXT2FS_DEBUG	static unsigned long alloc_hits = 0, alloc_attempts = 0;#endif	unsigned long result;#ifdef EXT2_PREALLOCATE	/* Writer: ->i_prealloc* */       /*判断是否是预分配块*/	if (inode->u.ext2_i.i_prealloc_count &&	    (goal == inode->u.ext2_i.i_prealloc_block ||	     goal + 1 == inode->u.ext2_i.i_prealloc_block))	{	/* 如果预分配的块存在,并且将要分配的块正好起始于预分配的块,	        *则使用预分配块	        */			result = inode->u.ext2_i.i_prealloc_block++;		inode->u.ext2_i.i_prealloc_count--;		/* Writer: end */		ext2_debug ("preallocation hit (%lu/%lu).\n",			    ++alloc_hits, ++alloc_attempts);	} else {/*不是预分配块,则重新分配块*/	       /*删除节点的预分配块,清除块位图相应的bit位*/		ext2_discard_prealloc (inode);/*预分配块失效,	                                                           *重新设置设备块组描述子和块组数据块位图	                                                           */		ext2_debug ("preallocation miss (%lu/%lu).\n",			    alloc_hits, ++alloc_attempts);		if (S_ISREG(inode->i_mode))			/*分配新块,从新的分配点开始分配*/			result = ext2_new_block (inode, goal, 				 &inode->u.ext2_i.i_prealloc_count,				 &inode->u.ext2_i.i_prealloc_block, err);		else/*对于特殊文件,不进行预分配*/			result = ext2_new_block (inode, goal, 0, 0, err);	}#else	result = ext2_new_block (inode, goal, 0, 0, err);#endif	return result;}typedef struct {	u32	*p;/*索引项的地址*/	u32	key;/*索引项的值*/	struct buffer_head *bh;/*索引块所在的缓冲区*/} Indirect;/*描述索引块中的索引项*//*将描述索引块中的索引项的地址和值放入chain数组中*/static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v){	p->key = *(p->p = v);	p->bh = bh;}/*读写校验描述索引块中的索引项的地址和值*/static inline int verify_chain(Indirect *from, Indirect *to){	while (from <= to && from->key == *from->p)		from++;	return (from > to);}/** *	ext2_block_to_path - parse the block number into array of offsets *	@inode: inode in question (we are only interested in its superblock) *	@i_block: block number to be parsed *	@offsets: array to store the offsets in * *	To store the locations of file's data ext2 uses a data structure common *	for UNIX filesystems - tree of pointers anchored in the inode, with *	data blocks at leaves and indirect blocks in intermediate nodes. *	This function translates the block number into path in that tree - *	return value is the path length and @offsets[n] is the offset of *	pointer to (n+1)th node in the nth one. If @block is out of range *	(negative or too large) warning is printed and zero returned. * *	Note: function doesn't find node addresses, so no IO is needed. All *	we need to know is the capacity of indirect blocks (taken from the *	inode->i_sb). *//* * Portability note: the last comparison (check that we fit into triple * indirect block) is spelled differently, because otherwise on an * architecture with 32-bit longs and 8Kb pages we might get into trouble * if our filesystem had 8Kb blocks. We might use long long, but that would * kill us on x86. Oh, well, at least the sign propagation does not matter - * i_block would have to be negative in the very beginning, so we would not * get there at all. *//*以逻辑块号为索引查找物理块*/static int ext2_block_to_path(struct inode *inode, long i_block, int offsets[4]){       /*每块地址数=块大小/每个指针大小即32位,一般为1kbyte/32bit=256*/	int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);/*每个索引块的索引字数目*/	int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);/*每块索引字数目的二进制指数*/       /*直接索引表上的索引字数目,即直接块数=12*/	const long direct_blocks = EXT2_NDIR_BLOCKS,              /*每个间接索引块所索引块的数目,即一次间址块数=256*/              indirect_blocks = ptrs,              /*每个双重索引块所索引块的数目,即二次间址块数=256*256*/		double_blocks = (1 << (ptrs_bits * 2));	int n = 0;	if (i_block < 0) {		ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");	} else if (i_block < direct_blocks) {	   /*如果寻址的块号小于直接索引字数量,则索引深度为1*/		offsets[n++] = i_block;/*直接块*/	} else if ( (i_block -= direct_blocks) < indirect_blocks) {	   /*如果剩余的块号小于间接索引块所索引块的数目,则索引深度为2*/		offsets[n++] = EXT2_IND_BLOCK;/*一次间址块*/		offsets[n++] = i_block;	} else if ((i_block -= indirect_blocks) < double_blocks) {	   /*如果剩余的块号小于二重索引块所索引的块数目,则索引深度为3*/		offsets[n++] = EXT2_DIND_BLOCK;/*二次间址块*/		offsets[n++] = i_block >> ptrs_bits;/*一次间址块*/		offsets[n++] = i_block & (ptrs - 1);/*直接块*/	} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {	   /*如果剩余的块号小于三级索引块所索引的块数目,则索引深度为4*/		offsets[n++] = EXT2_TIND_BLOCK;/*三次间址块*/		offsets[n++] = i_block >> (ptrs_bits * 2);/*二次间址块*/		offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);/*一次间址块*/		offsets[n++] = i_block & (ptrs - 1);/*直接块*/	} else {		ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");	}	return n;}/** *	ext2_get_branch - read the chain of indirect blocks leading to data *	@inode: inode in question *	@depth: depth of the chain (1 - direct pointer, etc.) *	@offsets: offsets of pointers in inode/indirect blocks *	@chain: place to store the result *	@err: here we store the error value * *	Function fills the array of triples <key, p, bh> and returns %NULL *	if everything went OK or the pointer to the last filled triple *	(incomplete one) otherwise. Upon the return chain[i].key contains *	the number of (i+1)-th block in the chain (as it is stored in memory, *	i.e. little-endian 32-bit), chain[i].p contains the address of that *	number (it points into struct inode for i==0 and into the bh->b_data *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect *	block for i>0 and NULL for i==0. In other words, it holds the block *	numbers of the chain, addresses they were taken from (and where we can *	verify that chain did not change) and buffer_heads hosting these *	numbers. * *	Function stops when it stumbles upon zero pointer (absent block) *		(pointer to last triple returned, *@err == 0) *	or when it gets an IO error reading an indirect block *		(ditto, *@err == -EIO) *	or when it notices that chain had been changed while it was reading *		(ditto, *@err == -EAGAIN) *	or when it reads all @depth-1 indirect blocks successfully and finds *	the whole chain, all way to the data (returns %NULL, *err == 0). */ /*从物理块中读取数据到chain的buffer中,功能是填充Indirect结构的数组,  *如果运行正常,则返回NULL  */static Indirect *ext2_get_branch(struct inode *inode,/*操作的节点*/				 int depth,/*间接块深度,1-一次间接块指针*/				 int *offsets,/*间接物理块的指针数组*/				 Indirect chain[4],/*存储读取物理块的数据*/				 int *err)/*存储错误标志*/{	struct super_block *sb = inode->i_sb;	Indirect *p = chain;	struct buffer_head *bh;	*err = 0;	/* i_data is not going away, no lock needed */	/*初始化chain*/	add_chain (chain, NULL, inode->u.ext2_i.i_data + *offsets);	if (!p->key)		goto no_block;	/*depth为间接块深度,三次间接块深度为4*/	while (--depth) {		bh = sb_bread(sb, le32_to_cpu(p->key));		if (!bh)			goto failure;		/* Reader: pointers */		/*测试根索引项chain->key到当前索引项p->key的值有没有发生变化*/		if (!verify_chain(chain, p))			goto changed;		/*读出物理块数据到chain中,p为chain数组的指针*/		add_chain(++p, bh, (u32*)bh->b_data + *++offsets);		/* Reader: end */		if (!p->key)			goto no_block;	}	return NULL;changed:	*err = -EAGAIN;	goto no_block;failure:	*err = -EIO;no_block:	return p;}/** *	ext2_find_near - find a place for allocation with sufficient locality *	@inode: owner *	@ind: descriptor of indirect block. * *	This function returns the prefered place for block allocation. *	It is used when heuristic for sequential allocation fails. *	Rules are: *	  + if there is a block to the left of our position - allocate near it. *	  + if pointer will live in indirect block - allocate near that block. *	  + if pointer will live in inode - allocate in the same cylinder group. *	Caller must make sure that @ind is valid and will stay that way. *//*从附近找一个物理块号*/static inline unsigned long ext2_find_near(struct inode *inode, Indirect *ind){	u32 *start = ind->bh ? (u32*) ind->bh->b_data : inode->u.ext2_i.i_data;	u32 *p;	/* Try to find previous block */	/*试着查找前面的块*/	for (p = ind->p - 1; p >= start; p--)		if (*p)			return le32_to_cpu(*p);/*返回文件末尾的块*/

⌨️ 快捷键说明

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