资源描述:
《Yaffs通过VFS层的读写》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Yaffs通过VFS层的读写Mournjust(sev7ncolor@gmail.com)在看过dreamice的《yaffs文件系统分析》之后,总是感到意犹未尽,还不够深入,所以先结合自己了解的一些知识继续写下去。Yaffs文件系统最终是通过VFS层接口被调用的,所以yaffs必须给VFS层提供相应的接口。VFS提供的标准接口结构是inode和dentry结构,也就是说不管最终yaffs是如何实现的,提供给VFS层的必须是inode和dentry结构。在yaffs中,函数yaffs_fill_inode_fro
2、m_obj用于根据yaffs的结构体yaffs_obj来填充inode结构体,以便于VFS层使用。staticvoidyaffs_fill_inode_from_obj(structinode*inode,structyaffs_obj*obj){....switch(obj->yst_mode&S_IFMT){default:/*fifo,deviceorsocket*/init_special_inode(inode,obj->yst_mode,old_decode_dev(obj->yst_rdev));b
3、reak;caseS_IFREG:/*file*/inode->i_op=&yaffs_file_inode_operations;inode->i_fop=&yaffs_file_operations;inode->i_mapping->a_ops=&yaffs_file_address_operations;break;caseS_IFDIR:/*directory*/inode->i_op=&yaffs_dir_inode_operations;inode->i_fop=&yaffs_dir_operati
4、ons;break;caseS_IFLNK:/*symlink*/inode->i_op=&yaffs_symlink_inode_operations;break;}....}其中obj->yst_mode用于表示yaffs_obj指代的object是具体文件、目录、symlink,以及hardlink等等。对于不同类型的object,显然处理的方法是不一样。首先需要关注的是关于VFS的缓冲页的操作结构体:staticstructaddress_space_operationsyaffs_file_addres
5、s_operations={.readpage=yaffs_readpage,.writepage=yaffs_writepage,#if(YAFFS_USE_WRITE_BEGIN_END>0).write_begin=yaffs_write_begin,.write_end=yaffs_write_end,#else.prepare_write=yaffs_prepare_write,.commit_write=yaffs_commit_write,#endif};因为内核版本的更新,原版本的prepare_
6、write和commit_write函数被write_begin和write_end函数所代替,这儿为了保持yaffs的可移植性,采用了一个条件编译。其中write_begin函数主要调用grab_cache_page_write_begin在radix树里面查找要被写的page,如果不存在则创建一个。如果被写入的设备是一个块设备的话,调用__block_prepare_write为这个page准备一组buffer_head结构,用于描述组成这个page的数据块。write_end主要用于将被写入的page标记为
7、脏,后台进程pdflush会寻找这些脏页,并将数据写入设备中去。如果被写入的设备是块设备的话,还需要将相应的buffer-head标记为脏。staticintyaffs_write_begin(structfile*filp,structaddress_space*mapping,loff_tpos,unsignedlen,unsignedflags,structpage**pagep,void**fsdata){structpage*pg=NULL;pgoff_tindex=pos>>PAGE_CACHE_SH
8、IFT;intret=0;intspace_held=0;/*Getapage*/pg=grab_cache_page_write_begin(mapping,index,flags);首先根据文件内部偏移量算出所处的page位置index,然后根据index从文件的structaddress_space中获得page。当然如果不存在该page的话,内核也会为它分配一个p