ext2_dir_entry_2{
__u32 inode; // 4 byte for inode number
__u16 rec_len; // 2 byte for this record length
__u8 name_len; // 1 byte for name length
__u8 file_type; // 1 byte for file type
char name[EXT2_NAME_LEN]; // file name
}
"/"
) is inode 2 (inode number starts from 1)struct ext2_inode {
__u16 i_mode; // 0-1. file type, access mode.
__u16 i_uid ; // 2-3. owner identifier
__u32 i_size ; // 4-7. file size in bytes
.................
__u16 i_links_count; // hard links counter
__u32 i_blocks; // file size in blocks
__u32 i_block[EXT2_N_BLOCKS]; // block location array
}
i_block[0]
to i_block[11]
tells the location of corresponding blocki_block[12]
is an indirect address:
i_block[12]==100
, block 100 has the location of next 1024 blocksi_block[13]
is a double indirect address:
if i_block[13]==200
, block 200 has the location of 1024 indirect addr blocki_block[14]
is a triple indirect addressThe use/free information of each inode. With 4K-byte block, 1 block can show the usage info of 4*1024*8 = 32768 inodes.
The usage info of 32768 blocks.
The location of inode bit map,data block bit map, inode table, etc. for each block group.
Overall information about this file system such as total number of inodes, total number of blocks, block size, etc.
data structures:
typedef struct // super block
{
u32 m_inodes_count; // 0-3
u32 m_blocks_count; // 4-7
u32 m_r_blocks_count; // 8-B
u32 m_free_blocks_count; // C-F
u32 m_free_inodes_count; // 10-13
u32 m_first_data_block; // 14-17
// block location of superblock.
u32 m_log_block_size; // 18-1B. block size=1024*(2^(m_log_block_size))
u32 m_log_frag_size; // 1C-1F
u32 m_blocks_per_group; // 20-23
u32 m_frrags_per_group; // 24-27
u32 m_inodes_per_group; // 28-2B
u32 m_mtime; // 2C-2F
u32 m_wtime; // 30-33
u16 m_mnt_count; // 34-35
u16 m_max_mnt_count; // 36-37
u16 m_magic; // 38-39
u16 m_state; // 3A-3B
u16 m_errors; // 3C-3D
u16 m_minor_rev_level; // 3E-3F
u32 m_lastcheck; // 40-43
u32 m_checkinterval; // 44-47
u32 m_creator_os; // 48-4b
u32 m_rev_level; // 4c-4f
u16 m_def_resuid; // 50-51
u16 m_def_resgid; // 52-53
u32 m_first_ino; // 54-57
u16 m_inode_size; //58-59
u16 m_block_group_nr; //5a-5b
u32 m_feature_compat; //5c-5f
u32 m_feature_incompat; //60-63
u32 m_feature_ro_compat; //64-67
u08 m_uuid[16]; //68-77
char m_volume_name[16]; //78-87
char m_last_mounted[64]; //88-c7
u32 m_algorithm_usage_bitmap; //c8-cb
u08 m_prealloc_blocks; //cc
u08 m_prealloc_dir_blocks; //cd
u16 m_padding; // ce-cf
u08 m_journal_uuid[16]; // d0-df
u32 m_journal_inum; // e0-e3
u32 m_journal_dev; // e4-e7
u32 m_last_orphan; // e8-eb
u32 m_hash_seed[4]; // ec-fb
} SuperBlock;
typedef struct // group descriptor
{
u32 m_block_bitmap; // block location of DBM
u32 m_inode_bitmap; // block location of IBM
u32 m_inode_table; // block location of inode table
u16 m_free_blocks_count;
u16 m_free_inodes_count;
u16 m_used_dir_count;
u16 m_padding;
u32 m_reserved[3];
} GroupDescriptor;
typedef struct // inode
{
u16 m_mode; // 0-1
u16 m_uid; // 2-3
u32 m_size; // 4-7
u32 m_atime; // 8-B
u32 m_ctime; // C-F
u32 m_mtime; // 10-13
u32 m_dtime; // 14-17
u16 m_gid; // 18-19
u16 m_links_count; // 1A-1B
u32 m_blocks; // 1C-1F. shows num of data blocks for this file in units of 512 bytes
u32 m_flags; // 20-23
u32 m_reserved1; // 24-27
u32 m_block[15]; // block location of this file
u32 m_generation;
u32 m_file_acl;
u32 m_dir_acl;
u32 m_faddr;
u32 m_reserved2[3];
} Inode;
typedef struct // directory
{
u32 m_inode;
u16 m_rec_len;
u08 m_name_len;
u08 m_file_type;
char m_name[255];
} DirectoryEntry;
/d1/f1
/
”)i_block[0]
of inode 2 (block 32)i_block[0]
of inode 12 (block 54)/d1/f1
is written in inode 23$ mkfs –t ext2 /dev/fd0
# or
$ mke2fs /dev/fd0
creates an ext2 file system in a floppy disk.
/f1
, and write “korea” in it
/f1
f1
in an empty directory entryi_block[0]
of inode 3$ dd bs=1024 count=1440 if=/dev/zero of=myfd
will make a virtual floppy disk of size 1.44MB with name “myfd”.
dd
is a command to write data into disk.bs
is block-size. bs=1024 means 1 block is 1024 byte (this “block” is not the file system block, it is just the unit of data transfer in dd
command).count
is the number of blocks to write. count=1440 means write 1440 blocks, which is 1440*1024=1440KB=1.44 MB.if
is input file to read data from./dev/zero
is a special file that gives out zeros when being read.of
is the output file.bs
is NOT file system block size. You can make same disk with bs=1
as below:
$ dd bs=1 count=1474560 if=/dev/zero of=myfd
$ xxd myfd > x
$ vi x
/400
$ mkfs -t ext2 myfd
will format myfd with ext2 file system.
$ xxd myfd > x
$ vi x
/400
vi myfd
will not show the content. Why is that? How can you see the content of myfd?f1
and f2
) in myfd disk. Explain why it is not working.$ echo hello > f1
$ echo hello2 > f2
$ cp f1 myfd
$ cp f2 myfd
$ xxd myfd
To store a file in myfd, you have to connect the file tree in myfd to the system file tree via mounting. Regenerate myfd and do followings.
$ mkdir temp
We need an empty directory to mount myfd. If temp is already there, skip this step.
$ mount -o loop myfd temp
will connect myfd to temp directory, which is called mounting. Since myfd is not a physical disk but a file that contains a disk image, we need to use –o loop option. This command will connect myfd disk to /temp directory and you can access “myfd” disk via /temp.
$ cd temp --- go to myfd
$ ls --- check myfd is empty
$ echo korea > f1 --- make f1 in it
$ ls --- check f1
$ cd ..
$ umount temp
We need umount temp
to write the change in the myfd disk.
$ xxd -g1 myfd > x
-g1
option will display each byte of myfd separately.
$ vi x
(if it doesn’t start at line 0, type :1
to go to line 1)
f2
in myfd with vi
. What changes can you observe in DBM and IBM? Guess what is the inode number for f2
and what is the block number assigned to it.각각의 inode는 inode table에서 0x80(=128)bytes를 차지한다.
0x2800에 첫번째 inode가 위치하고, 0x2880에 두번째 inode가 위치한다.
root directory file은 inode 2번이므로 0x2880~0x28ff에 기록되어있다.
typedef struct // inode
{
... ......
u16 m_uid; // 2-3
u32 m_size; // 4-7
... ......
u32 m_block[15]; // 28-2C block location of this file
... ......
} Inode;
inode struct에 따르면 파일의 block 위치를 나타내는 m_block
은 [28-2C]에 위치한다.
해당 위치에 0x21이 기록된 것을 확인할 수 있다. 따라서 root directory의 file 위치는 0x21 * 0x400 = 0x8400이다.
또한, m_size
를 통해 byte size가 0x400 = 1024 bytes이고, m_blcoks
를 통해 block size가 0x2 (512 bytes) = 1024 bytes임을 알 수 있다.
이 파일의 소유자는 uid field에 기록되며 0이다. uid 0은 root 사용자를 의미한다.
root directory file의 시작 위치는 0x8400이다.
typedef struct // directory
{
u32 m_inode;
u16 m_rec_len;
u08 m_name_len;
u08 m_file_type;
char m_name[255];
} DirectoryEntry;
u32 m_inode;
: inode는 00 00 00 02(=2)로 root directory file은 2번 inode에 속해있다.u16 m_rec_len;
: record length는 00 0c(=12)이다.u08 m_name_len;
: 01(=1)u08 m_file_type;
: 02(=2 : directory file)char m_name[255];
: 00 00 00 2e(=”.”)u32 m_inode;
: inode는 00 00 00 02(=2)로 2번 inode를 의미한다.u16 m_rec_len;
: record length는 00 0c(=12)이다.u08 m_name_len;
: 02(=2)u08 m_file_type;
: 02(=2 : directory file)char m_name[255];
: 00 00 2e 2e(=”..”)u32 m_inode;
: inode는 00 00 00 0b(=11)로 11번 inode를 의미한다.u16 m_rec_len;
: record length는 00 14(=20)이다.u08 m_name_len;
: 0a(=10)u08 m_file_type;
: 02(=2 : directory file)char m_name[255];
: 00 00 6c 6f 73 74 2b 66 6f 75 6e 64(=”lost+found”)u32 m_inode;
: inode는 00 00 00 02(=2)로 2번 inode를 의미한다.u16 m_rec_len;
: record length는 03 d4(=980)이다.u08 m_name_len;
: 0a(=10)u08 m_file_type;
: 01(=1 : regular file)char m_name[255];
: 00 00 66 31(=”f1”)총 4개의 파일이 존재한다.
lost+found의 block location은 0x22 * 0x400 = 0x8800 이다.
f1의 block location은 0x2f * 0x400 = 0xbc00 이다.
lost+found의 directory file 내부에는 .
과 ..
파일이 있다. .
파일은 inode 번호가 11인 자신을 나타내고 ..
파일은 2번 root directory file을 의미한다.
f1은 file type 번호가 1인 regular file로 f1의 data, ‘korea’가 기록되어있다.
ls -a
. Confirm you can see all files you found in the file system with this command.$ ls -a
숨겨진 파일(.
, ..
)을 포함한 현재 디렉터리의 모든 파일이 출력된다.
ls -i
. Confirm the inode numbers of all files.$ ls -ai
각 파일명 왼쪽에 inode 번호가 같이 출력된다.
rm
command). What happens to the file system? How can you recover this file?$ mkdir temp
$ mount -o loop myfd temp # virtual disk(myfd)와 temp 디렉토리 연결
$ echo hello > f3
$ ls
f1 f3 lost+found
$ cd ..
$ umount temp # 연결 해제
$ xxd -g1 myfd > hw14 # 내용 추출
$ vi hw14
d7
) in the root directory (of myfd) with mkdir
command. Show the disk block content of the root directory file and find out the inode number of d7
.d7
. What is the block location of d7
? Show the block content of d7
. What files do you have in d7
?mv f1 d7/f2
and show the changes in the root directory file, d7
file, and inode table.$ dd bs=1024 count=8000 if=/dev/sda3 of=myhd
$ xxd –g1 myhd > x
$ vi x
struct superblock{
int total_inode_num;
int total_block_num;
.........
};
int x; char buf[1024]; struct superblock *sb;
x=open("myfd", O_RDONLY, 00777); //open a virtual disk
lseek(x, 1024, SEEK_SET); // move the file pointer to offset 1024 where the
// superblock starts
read(x, buf, 1024); // read the superblock into buf
sb=(struct superblock *)buf; // interpret the data in buf as "struct superblock"
printf("total inode num:%x, total_block_num:%x, ...",
sb->total_inode_num, sb->total_block_num, ....);