标签:des style blog http color os
1 /* 2 * Structure of the super block 3 */ 4 struct ext2_super_block { 5 __le32 s_inodes_count; /* Inodes count 索引节点总数*/ 6 __le32 s_blocks_count; /* Blocks count 块大小,即文件系统以块为单位的大小*/ 7 __le32 s_r_blocks_count; /* Reserved blocks count */ 8 __le32 s_free_blocks_count; /* Free blocks count */ 9 __le32 s_free_inodes_count; /* Free inodes count */ 10 __le32 s_first_data_block; /* First Data Block */ 11 __le32 s_log_block_size; /* Block size */ 12 __le32 s_log_frag_size; /* Fragment size */ 13 __le32 s_blocks_per_group; /* # Blocks per group 每个块组中的块数*/ 14 __le32 s_frags_per_group; /* # Fragments per group */ 15 __le32 s_inodes_per_group; /* # Inodes per group 每个块组中的索引节点个数*/
16 __le32 s_mtime; /* Mount time */ 17 __le32 s_wtime; /* Write time */ 18 __le16 s_mnt_count; /* Mount count */ 19 __le16 s_max_mnt_count; /* Maximal mount count */ 20 __le16 s_magic; /* Magic signature */ 21 __le16 s_state; /* File system state */ 22 __le16 s_errors; /* Behaviour when detecting errors */ 23 __le16 s_minor_rev_level; /* minor revision level */ 24 __le32 s_lastcheck; /* time of last check */ 25 __le32 s_checkinterval; /* max. time between checks */ 26 __le32 s_creator_os; /* OS */ 27 __le32 s_rev_level; /* Revision level */ 28 __le16 s_def_resuid; /* Default uid for reserved blocks */ 29 __le16 s_def_resgid; /* Default gid for reserved blocks */ 30 /* 31 * These fields are for EXT2_DYNAMIC_REV superblocks only. 32 * 33 * Note: the difference between the compatible feature set and 34 * the incompatible feature set is that if there is a bit set 35 * in the incompatible feature set that the kernel doesn‘t 36 * know about, it should refuse to mount the filesystem. 37 * 38 * e2fsck‘s requirements are more strict; if it doesn‘t know 39 * about a feature in either the compatible or incompatible 40 * feature set, it must abort and not try to meddle with 41 * things it doesn‘t understand... 42 */ 43 __le32 s_first_ino; /* First non-reserved inode */ 44 __le16 s_inode_size; /* size of inode structure */ 45 __le16 s_block_group_nr; /* block group # of this superblock */ 46 __le32 s_feature_compat; /* compatible feature set */ 47 __le32 s_feature_incompat; /* incompatible feature set */ 48 __le32 s_feature_ro_compat; /* readonly-compatible feature set */ 49 __u8 s_uuid[16]; /* 128-bit uuid for volume */ 50 char s_volume_name[16]; /* volume name */ 51 char s_last_mounted[64]; /* directory where last mounted */ 52 __le32 s_algorithm_usage_bitmap; /* For compression */ 53 /* 54 * Performance hints. Directory preallocation should only 55 * happen if the EXT2_COMPAT_PREALLOC flag is on. 56 */ 57 __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/ 58 __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */ 59 __u16 s_padding1; 60 /* 61 * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set. 62 */ 63 __u8 s_journal_uuid[16]; /* uuid of journal superblock */ 64 __u32 s_journal_inum; /* inode number of journal file */ 65 __u32 s_journal_dev; /* device number of journal file */ 66 __u32 s_last_orphan; /* start of list of inodes to delete */ 67 __u32 s_hash_seed[4]; /* HTREE hash seed */ 68 __u8 s_def_hash_version; /* Default hash version to use */ 69 __u8 s_reserved_char_pad; 70 __u16 s_reserved_word_pad; 71 __le32 s_default_mount_opts; 72 __le32 s_first_meta_bg; /* First metablock block group */ 73 __u32 s_reserved[190]; /* Padding to the end of the block */ 74 };
可以看到,ext2磁盘超级块结构中大部分是描述整个文件系统的信息,如文件系统中块组的数量,inode数量,磁盘块的数量等等,不一而足,基本上从代码的注释我们就能比较清楚各个成员的含义,而且在后续的文章中我们或多或少地也会遇到这些成员,另外ext2超级块的最后一部分成员是为了兼容ext3而设计的,可能是为了更方便地从ext2升级至ext3吧,当然这只是我的猜测而已。
/* * Structure of a blocks group descriptor */ struct ext2_group_desc { __le32 bg_block_bitmap; /* Blocks bitmap block */ __le32 bg_inode_bitmap; /* Inodes bitmap block */ __le32 bg_inode_table; /* Inodes table block */ __le16 bg_free_blocks_count; /* Free blocks count */ __le16 bg_free_inodes_count; /* Free inodes count */ __le16 bg_used_dirs_count; /* Directories count */ __le16 bg_pad; __le32 bg_reserved[3]; };
struct ext2_dir_entry_2 { __le32 inode; /* Inode number inode编号 */ __le16 rec_len; /* Directory entry length */ __u8 name_len; /* Name length */ __u8 file_type; char name[EXT2_NAME_LEN]; /* File name */ };
在该结构中,inode代表该文件inode编号,rec_len表示本文件目录项的大小,为什么需要这个rec_len呢,结构体定义好了整个长度不也就确认了嘛?非也,这是因为该结构体的最后一个成员name并不是固定长度的,其最大可以支持256字节,因此必须要有一个长度域来保存当前目录项长度,name_len指的是文件名长度,既然已经有了rec_len,为什么还需要文件名长呢,岂不多此一举?这是考虑到存在文件名填充的问题。从效率上来考虑,每个struct ext2_dir_entry_2最终都会被填充成4字节整数倍,对于目录项不是4字节整数倍的,需要在最后name文件名后面填充若干个0,因此name_len中记录的便是name[]域中有效文件名长度(即不包含0)。考虑下图所示事例:
1. “.”和“..”文件名后都填充了‘\0’以使文件目录项总长度为4的整数倍;
2. music和src文件/目录均也填充了‘\0’以使文件目录项总长度为4的整数倍;
3. test.txt因为其文件目录项已经是16个字节,无需填充。
/* * Structure of an inode on the disk */ struct ext2_inode { __le16 i_mode; /* File mode */ __le16 i_uid; /* Low 16 bits of Owner Uid */ __le32 i_size; /* Size in bytes */ __le32 i_atime; /* Access time */ __le32 i_ctime; /* Creation time */ __le32 i_mtime; /* Modification time */ __le32 i_dtime; /* Deletion Time */ __le16 i_gid; /* Low 16 bits of Group Id */ __le16 i_links_count; /* Links count */ __le32 i_blocks; /* Blocks count */ __le32 i_flags; /* File flags */ union { struct { __le32 l_i_reserved1; } linux1; struct { __le32 h_i_translator; } hurd1; struct { __le32 m_i_reserved1; } masix1; } osd1; /* OS dependent 1 */ __le32 i_block[EXT2_N_BLOCKS];/* Pointers to blocks */ __le32 i_generation; /* File version (for NFS) */ __le32 i_file_acl; /* File ACL */ __le32 i_dir_acl; /* Directory ACL */ __le32 i_faddr; /* Fragment address */ union { struct { __u8 l_i_frag; /* Fragment number */ __u8 l_i_fsize; /* Fragment size */ __u16 i_pad1; __le16 l_i_uid_high; /* these 2 fields */ __le16 l_i_gid_high; /* were reserved2[0] */ __u32 l_i_reserved2; } linux2; struct { __u8 h_i_frag; /* Fragment number */ __u8 h_i_fsize; /* Fragment size */ __le16 h_i_mode_high; __le16 h_i_uid_high; __le16 h_i_gid_high; __le32 h_i_author; } hurd2; struct { __u8 m_i_frag; /* Fragment number */ __u8 m_i_fsize; /* Fragment size */ __u16 m_pad1; __u32 m_i_reserved2[2]; } masix2; } osd2; /* OS dependent 2 */ };
标签:des style blog http color os
原文地址:http://www.cnblogs.com/wuchanming/p/3862210.html