Super Block 的信息被破坏,可以说整个文件系统结构就被破坏了。超级块在每个块组的开头都有一份拷贝(第一个块组必须有,后面的块组可以没有)。为了保证文件系统在磁盘部分扇区出现物理问题的情况下还能正常工作,就必须保证文件系统的 super block 信息在这种情况下也能正常访问。所以一个文件系统的 super block 会在多个 block group 中进行备份,这些 super block 区域的数据保持一致。
下面是 Linux 内核源码中存储超级块的结构体:
/* * Structure of the super block */structext2_super_block {
__le32 s_inodes_count; /* Inodes count */
__le32 s_blocks_count; /* Blocks count */
__le32 s_r_blocks_count; /* Reserved blocks count */
__le32 s_free_blocks_count; /* Free blocks count */
__le32 s_free_inodes_count; /* Free inodes count */
__le32 s_first_data_block; /* First Data Block */
__le32 s_log_block_size; /* Block size */
__le32 s_log_frag_size; /* Fragment size */
__le32 s_blocks_per_group; /* # Blocks per group */
__le32 s_frags_per_group; /* # Fragments per group */
__le32 s_inodes_per_group; /* # Inodes per group */
__le32 s_mtime; /* Mount time */
__le32 s_wtime; /* Write time */
__le16 s_mnt_count; /* Mount count */
__le16 s_max_mnt_count; /* Maximal mount count */
__le16 s_magic; /* Magic signature */
__le16 s_state; /* File system state */
__le16 s_errors; /* Behaviour when detecting errors */
__le16 s_minor_rev_level; /* minor revision level */
__le32 s_lastcheck; /* time of last check */
__le32 s_checkinterval; /* max. time between checks */
__le32 s_creator_os; /* OS */
__le32 s_rev_level; /* Revision level */
__le16 s_def_resuid; /* Default uid for reserved blocks */
__le16 s_def_resgid; /* Default gid for reserved blocks */
__le32 s_first_ino; /* First non-reserved inode */
__le16 s_inode_size; /* size of inode structure */
__le16 s_block_group_nr; /* block group # of this superblock */
__le32 s_feature_compat; /* compatible feature set */
__le32 s_feature_incompat;/* incompatible feature set */
__le32 s_feature_ro_compat; /* readonly-compatible feature set */
__u8 s_uuid[16]; /* 128-bit uuid for volume */char s_volume_name[16]; /* volume name */char s_last_mounted[64]; /* directory where last mounted */
__le32 s_algorithm_usage_bitmap; /* For compression */
__u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
__u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
__u16 s_padding1; /* Padding to the end of the block */
__u8 s_journal_uuid[16]; /* uuid of journal superblock */
__u32 s_journal_inum; /* inode number of journal file */
__u32 s_journal_dev; /* device number of journal file */
__u32 s_last_orphan; /* start of list of inodes to delete */
__u32 s_hash_seed[4]; /* HTREE hash seed */
__u8 s_def_hash_version; /* Default hash version to use */
__u8 s_reserved_char_pad;
__u16 s_reserved_word_pad;
__le32 s_default_mount_opts;
__le32 s_first_meta_bg; /* First metablock block group */
__u32 s_reserved[190]; /* Padding to the end of the block */
};
Linux 的根目录及系统缺省目录构成了目录树的基础骨架,用户新建的目录则对其进行扩展,两者共同形成了完整的目录树;而路径是基于这个目录树,描述文件 / 目录位置的访问标识 —— 因此系统和用户共同构建了 Linux 路径结构的基础。
思考一下:在 Linux 的磁盘中真的存在目录树吗?也就是目录的树状结构。答案是不存在的,在磁盘中只有文件的属性和内容。那么在我们访问文件时,是不是就意味着每一次访问都需要从根目录开始进行路径解析呢?从原则上来说是这样的,但是这种方式效率太过低下,因此 OS 会缓存历史路径结构。
所以在 Linux 中目录的概念是由 OS 产生的,OS 自己在内存中进行路径维护。
Linux 中,在内核中维护树状路径结构的内核结构体叫做:struct dentry
structdentry {atomic_t d_count;
unsignedint d_flags; /* protected by d_lock */spinlock_t d_lock; /* per dentry lock */structinode *d_inode;/* Where the name belongs to - NULL is negative */structhlist_noded_hash;/* lookup hash list */structdentry *d_parent;/* parent directory */structqstrd_name;structlist_headd_lru;/* LRU list */union {structlist_headd_child;/* child of parent list */structrcu_headd_rcu;
} d_u;
structlist_headd_subdirs;/* our children */structlist_headd_alias;/* inode alias list */unsignedlong d_time; /* used by d_revalidate */structdentry_operations *d_op;structsuper_block *d_sb;/* The root of the dentry tree */void *d_fsdata; /* fs-specific data */#ifdef CONFIG_PROFILINGstructdcookie_struct *d_cookie;/* cookie, if any */#endifint d_mounted;
unsignedchar d_iname[DNAME_INLINE_LEN_MIN]; /* small names */
}