ext2 super block

This commit is contained in:
w-mj committed 2019-06-15 19:02:36 +08:00
1 parent b6e050a09a
commit 3298177804
6 files changed
+127 -10

No files matched your search

+2 -1
View File
@@ -1,2 +1,3 @@
.vscode/
fs/ext2/fs
fs/ext2/fs
**/*.out
+1 -1
View File
@@ -6,7 +6,7 @@
0. 文件系统中存储的最小单位是块(Block),一个块的大小在格式化时确定,可以为1k、2k、4k等。
1. 第一个块为启动块,固定大小为1k,忽略不用。
2. 超级块:描述了整个文件系统的基本信息,在每个块组前都有一份拷贝。
2. 超级块:描述了整个文件系统的基本信息,在每个块组前都有一份拷贝。(注:ext2的第一个修订版本在每个块组前拷贝超级块,但是之后的版本为了减少冗余,只在特定的块组前保存超级块。特定的块组为0、1和3、5、7的乘方。)
3. 块组描述符表:由很多块组描述符组成,文件系统中有多少块组就有多少块组描述符,块组描述符表在每个块组前也有一份拷贝。
4. 块位图:用于描述整个块组中哪些块已用,它本身占一个块,每个bit表示一个块,1为已用,0为空闲。
5. inode位图:描述哪些inode可用。
-8
View File
@@ -1,8 +0,0 @@
#ifndef _INODE_H_
#define _INODE_H_
struct ext2_super_block {
};
#endif
+7
View File
@@ -0,0 +1,7 @@
#include <iostream>
#include "ext2_fs.h"
using namespace std;
int main(void) {
cout << sizeof(ext2_super_block) << endl;
}
+57
View File
@@ -0,0 +1,57 @@
#ifndef _INODE_H_
#define _INODE_H_
#include "types.h"
constexpr _u16 EXT2_VALID_FS = 0x01; // 文件系统状态定义,没有正常卸载
constexpr _u16 EXT2_ERROR_FS = 0x02; // 文件系统状态定义,内核代码检测到错误
/**
* ext2 文件系统的超级块定义
*/
struct ext2_super_block {
_u32 s_inodes_count; // 文件系统中Inode数量
_u32 s_blocks_count; // 文件系统中总块数
_u32 s_r_blocks_count; // 文件系统中保留块数目
_u32 s_free_blocks_count; // 文件系统中空闲块数目
_u32 s_free_inodes_count; // 文件系统中空心Inode数量
_u32 s_first_data_block; // 第一个数据块的位置,通常当数据块大小为1k时为1,否则为0
_u32 s_log_block_size; // 逻辑块数量
_i32 s_log_frag_size; // 逻辑碎片大小
_u32 s_blocks_per_group; // 每组块数
_u32 s_frags_per_group; // 每组Fragment数?
_u32 s_inodes_per_group; // 每组Inode数
_u32 s_mtime; // 上次挂载时间,为1970-1-1到现在的秒数
_u32 s_wtime; // 上次写入超级块的时间
_u16 s_mnt_count; // 未进行检查的挂载次数
_i16 s_max_mnt_count; // 必须执行检查前的最大挂载次数(可为-1)
_u16 s_magic; // Magic signature 用于表示文件系统的版本,一般为0xEF53
_u16 s_state; // 文件系统的状态,包括EXT2_VALID_FS和EXT2_ERROR_FS的或值。
_u16 s_errors; // 表明出现错误时要执行的操作
_u16 s_pad; // 未使用
_u32 s_lastcheck; // 上一次执行检查的时间
_u32 s_checkinterval; // 两次执行检查的间隔时间
_u32 s_creator_os;
_u32 s_rev_level;
_u16 s_def_resuid;
_u16 s_def_resgid;
_u32 s_first_ino; /* First non-reserved inode */
_u16 s_inode_size; /* size of inode structure */
_u16 s_block_group_nr; /* block group # of this superblock */
_u32 s_feature_compat; /* compatible feature set */
_u32 s_feature_incompat; /* incompatible feature set */
_u32 s_feature_ro_compat; /* readonly-compatible feature set */
_u8 s_uuid[16]; /* 128-bit uuid for volume */
_u8 s_volume_name[16]; /* volume name */
_u8 s_last_mounted[64]; /* directory where last mounted */
_u32 algorithm_used;
_u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
_u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
_u16 s_padding1;
_u32 s_reserved[204]; /* Padding to the end of the block */
};
#endif
+60
View File
@@ -0,0 +1,60 @@
#ifndef _TYPES_H_
#define _TYPES_H_
typedef signed char _i8;
typedef unsigned char _u8;
typedef signed short _i16;
typedef unsigned short _u16;
typedef signed int _i32;
typedef unsigned int _u32;
typedef signed long _i64;
typedef unsigned long _u64;
typedef _u32 _I;
typedef _u8 _c;
typedef _c* _s;
typedef void* _p;
#ifdef __CHECKER__
#define __bitwise __attribute__((bitwise))
#else
#define __bitwise
#endif
typedef _u16 __bitwise _le16;
typedef _u16 __bitwise _be16;
typedef _u32 __bitwise _le32;
typedef _u32 __bitwise _be32;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
typedef _u64 __bitwise _le64;
typedef _u64 __bitwise _be64;
#endif
#define _getva_T_(P, params, _T_) do {P = (_T_)va_arg(params, _T_); }while(0)
#define _getva_S(s, params) _getva_T_(s, params, _s)
#define _getva_I(s, params) _getva_T_(s, params, _I)
#define _getva_P(s, params) _getva_T_(s, params, _p)
#define _getva_double(s, params) _getva_T_(s, params, double)
#define _getva_I32(s, params) _getva_T_(s, params, _i32)
#define _BITS_SIZE(n) (1 << (n))
#define _BITS_MASK(n) (_BITS_SIZE(n) - 1)
#define _BITSET(n) _BITS_SIZE(n)
#define _get_bit(d, bit) ((d) & _BITSET(bit))
#define _set_bit(d, bit) ((d) |= _BITSET(bit))
#define _clr_bit(d, bit) ((d) &= (~(_BITSET(bit))))
#define _chk_bit(d, bit) _get_bit(d, bit)
#define _rshf(a, n) ((a) << (n))
#define _lshf(a, n) ((a) >> (n))
#define _Lclr(a, n) ((a) & (~(_BITS_MASK(n))))
#define _Hclr(a, n) _rshf(_lshf(a, n), n)
#define _LNclr(a, n) ((a & (_BITS_MASK(n))))
#define _Inext(pI, i) ((pI) + (i))->_next
#define nullptr NULL
#define null NULL
#endif