LinuxÏÂext2ÎļþϵͳµÄÎïÀí½á¹¹Ïê½â
LinuxÏÂext2ÎļþϵͳµÄÎïÀí½á¹¹Ïê½â
ÔÚLinuxϵͳÖУ¬ext2ÊÇÒ»ÖÖ³£ÓõÄÎļþϵͳÀàÐÍ£¬ËüÊÇÒ»ÖÖ½ÏΪ¼òÆÓ¶øÓÖ¸ßЧµÄÎļþϵͳ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÉîÈë̽ÌÖext2ÎļþϵͳµÄÎïÀí½á¹¹£¬°üÀ¨³¬µÈ¿é¡¢×éÐÎò·û¡¢inode±í¡¢Êý¾Ý¿éµÈ£¬Í¬Ê±»áÌṩÏêϸµÄ´úÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£
1. ³¬µÈ¿é£¨Super Block£©
³¬µÈ¿éÊÇext2ÎļþϵͳÖÐ×îÖ÷ÒªµÄÊý¾Ý½á¹¹Ö®Ò»£¬Ëü´æ´¢×ÅÎļþϵͳµÄÕûÌåÐÅÏ¢£¬Èçinode×ÜÊý¡¢Êý¾Ý¿é×ÜÊý¡¢Ã¿×éµÄinodeÊýÄ¿¡¢Êý¾Ý¿éÊýÄ¿µÈ¡£ÒÔÏÂÊdz¬µÈ¿éµÄ½á¹¹Ê¾Àý£º
struct ext2_super_block { __le32 s_inodes_count; // inode×ÜÊý __le32 s_blocks_count; // Êý¾Ý¿é×ÜÊý __le32 s_inodes_per_group; // ÿ×éµÄinodeÊýÄ¿ __le32 s_blocks_per_group; // ÿ×éµÄÊý¾Ý¿éÊýÄ¿ // ÆäËû×Ö¶ÎÊ¡ÂÔ };
µÇ¼ºó¸´ÖÆ
2. ×éÐÎò·û£¨Group Descriptor£©
×éÐÎò·û´æ´¢×Åÿ¸ö×飨group£©µÄÔªÊý¾ÝÐÅÏ¢£¬°üÀ¨inodeλͼ¡¢Êý¾Ý¿éλͼ¡¢inode±íÆðʼ¿éºÅ¡¢Êý¾Ý¿éÆðʼ¿éºÅµÈ¡£ÒÔÏÂÊÇ×éÐÎò·ûµÄ½á¹¹Ê¾Àý£º
struct ext2_group_desc { __le32 bg_inode_bitmap; // inodeλͼ¿éºÅ __le32 bg_block_bitmap; // Êý¾Ý¿éλͼ¿éºÅ __le32 bg_inode_table; // inode±íµÄÆðʼ¿éºÅ __le16 bg_free_blocks_count; // ¿ÕÏÐÊý¾Ý¿éÊýÄ¿ __le16 bg_free_inodes_count; // ¿ÕÏÐinodeÊýÄ¿ // ÆäËû×Ö¶ÎÊ¡ÂÔ };
µÇ¼ºó¸´ÖÆ
3. inode±í£¨Inode Table£©
inode±í´æ´¢×ÅÎļþ»òĿ¼µÄÔªÊý¾ÝÐÅÏ¢£¬ÈçÎļþ¾Þϸ¡¢È¨ÏÞ¡¢ËùÓÐÕß¡¢Ê±¼ä´ÁµÈ¡£Ã¿¸öÎļþ»òĿ¼¶¼¶ÔÓ¦Ò»¸öinode±íÖеÄË÷Òý½Úµã£¨inode£©¡£ÒÔÏÂÊÇinode±íÖÐÿ¸öinodeµÄ½á¹¹Ê¾Àý£º
struct ext2_inode { __le16 i_mode; // ÎļþÀàÐͺÍȨÏÞ __le32 i_size; // Îļþ¾Þϸ __le32 i_blocks; // Êý¾Ý¿éÊýÄ¿ __le32 i_block[15]; // Êý¾Ý¿éºÅÊý×é // ÆäËû×Ö¶ÎÊ¡ÂÔ };
µÇ¼ºó¸´ÖÆ
4. Êý¾Ý¿é£¨Data Block£©
Êý¾Ý¿éÊÇ´æ´¢ÏÖʵÎļþÄÚÈݵĵط½£¬ext2Îļþϵͳ½ÓÄɼä½ÓÑ°Ö·µÄ·½·¨À´ÖÎÀíÊý¾Ý¿é¡£Êý¾Ý¿éÓÉÈô¸É¸öÉÈÇø×é³É£¬ÉÈÇøÊÇÎļþϵͳÖÐ×îСµÄ´æ´¢µ¥Î»¡£ÒÔÏÂÊÇÊý¾Ý¿éµÄ½á¹¹Ê¾Àý£º
struct ext2_data_block { char data[1024]; // Êý¾Ý¿é¾ÞϸΪ1KB };
µÇ¼ºó¸´ÖÆ
5. ´úÂëʾÀý
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý³ÌÐò£¬ÓÃÓÚ¶ÁÈ¡ext2ÎļþϵͳÖеij¬µÈ¿éÐÅÏ¢£º
#include <stdio.h> #include <fcntl.h> #include <ext2fs/ext2_fs.h> int main() { int fd = open("/dev/sda1", O_RDONLY); if (fd == -1) { perror("open"); return 1; } struct ext2_super_block sb; lseek(fd, 1024, SEEK_SET); // ³¬µÈ¿éλÓÚÆ«ÒÆ1024×Ö½Ú´¦ read(fd, &sb, sizeof(sb)); printf("Inode×ÜÊý£º%u ", sb.s_inodes_count); printf("Êý¾Ý¿é×ÜÊý£º%u ", sb.s_blocks_count); // Êä³öÆäËû³¬µÈ¿éÐÅÏ¢ close(fd); return 0; }
µÇ¼ºó¸´ÖÆ
½áÓï
±¾ÎĶÔLinuxÏÂext2ÎļþϵͳµÄÎïÀí½á¹¹¾ÙÐÐÁËÏêϸÆÊÎö£¬°üÀ¨³¬µÈ¿é¡¢×éÐÎò·û¡¢inode±íºÍÊý¾Ý¿éµÈÖ÷Òª²¿·Ö£¬²¢ÌṩÁËÏà¹Ø´úÂëʾÀý×ÊÖú¶ÁÕßÔ½·¢ÉîÈëµØÏàʶext2ÎļþϵͳµÄÄÚ²¿ÊµÏÖ¡£Ï£Íû±¾ÎÄÄܹ»¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇLinuxÏÂext2ÎļþϵͳµÄÎïÀí½á¹¹Ïê½âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡