演示ext4 文件系统如何找到一个文件的绝对磁盘扇区位置

#if 0

//演示ext4 文件系统如何找到一个文件的绝对磁盘扇区位置


//前提
在根目录下创建一个文本文件  /aaaaa.txt 文件内容如下:
12345ssdlh
67890
abcde
hahhahahaahah!!!!

from dir get filename = aaaaa.txt
fdiro->inode_read end = 0

root@local:/home/dai#hexdump -C /dev/mmcblk0p2 -n 128 -s 0x1000
00001000  01 04 00 00 11 04 00 00  21 04 00 00 91 52 38 04  |........!....R8.|
00001010  0c 03 04 00 00 00 00 00  00 00 00 00 36 04 ce 92  |............6...|
00001020  02 04 00 00 12 04 00 00  21 06 00 00 f8 00 00 20  |........!...... |
00001030  00 00 05 00 00 00 00 00  00 00 00 00 00 20 60 a2  |............. `.|
00001040  03 04 00 00 13 04 00 00  21 08 00 00 d2 09 00 20  |........!...... |
00001050  00 00 05 00 00 00 00 00  00 00 00 00 00 20 88 16  |............. ..|
00001060  04 04 00 00 14 04 00 00  21 0a 00 00 fb 38 00 20  |........!....8. |
00001070  00 00 05 00 00 00 00 00  00 00 00 00 00 20 dd 5d  |............. .]|
00001080


//第一步: 根据文件的ino i节点号 取得超级块组描述符  stat aaaaa.txt
//超级块组描述符 在磁盘分区0x1000的位置 前面的4k是操作系统保留的 文件系统不得使用


i节点号的取得 从代码看是 从根目录的i节点指向的内容中取得的
目录文件的内容就是文件名 和文件的i节点号

00000000  ca 1b 00 00 58 0e 09 01                           |....X...|
0-3 字节文件的i节点号
4-5 此条目在目录文件中占多少字节
6   文件名的长度 最大255字节
7   文件类型 01 普通文件 02 目录文件 07 链接文件

ret scan = 0x2421
from dir get filename = aaaaa.txt
fdiro->inode_read end = 0


root@local:/home/dai#stat /aaaaa.txt 
  文件:'/aaaaa.txt'
  大小:41              块:8          IO 块:4096   普通文件
设备:b302h/45826d      Inode:7114        硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2019-12-04 17:06:32.287060428 +0800
最近更改:2019-12-04 17:06:15.410607416 +0800
最近改动:2019-12-04 17:06:15.430602180 +0800
创建时间:-


Read the inode. base = 0x421.  超级块组描述符的 off = 0x08


ino = 7114 = 0x1bca

首先减了1
ino--
blkno = (ino % grub_le_to_cpu32(sblock->inodes_per_group)) / inodes_per_block;
         (7113 % 8192)/16 = 444 = 0x1bc  
blkoff = (ino % grub_le_to_cpu32(sblock->inodes_per_group)) % inodes_per_block;
         (7113%8192)%16=9
base = grub_le_to_cpu32(blkgrp.inode_table_id);
0x421

    

((base + blkno) << LOG2_EXT2_BLOCK_SIZE (data)) = 0x2ee8.
    (0x421+0x1bc)<<3 = 0x2ee8
//块组
    
EXT2_INODE_SIZE(data) * blkoff = 2304.
256*9=2304
//偏移  一个i节点占256字节

0x5dd900 = 0x2ee8*0x200 + 0x900(2304)


取得文件的i节点内容
root@local:/home/dai#hexdump -C /dev/mmcblk0p2 -n 128 -s 0x5dd900
005dd900  a4 81 00 00 29 00 00 00  18 77 e7 5d 07 77 e7 5d  |....)....w.].w.]|
005dd910  07 77 e7 5d 00 00 00 00  00 00 01 00 08 00 00 00  |.w.]............|
005dd920  00 00 08 00 01 00 00 00  0a f3 01 00 04 00 00 00  |................|
005dd930  00 00 00 00 00 00 00 00  01 00 00 00 03 b6 46 00  |..............F.|
005dd940  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
005dd960  00 00 00 00 a8 99 b5 0e  00 00 00 00 00 00 00 00  |................|
005dd970  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
005dd980


sizeof (struct grub_ext2_inode) = 128.
//第二步: 取得文件的i节点  off = 0x38


in grub_ext2_read


//ret scan = 0x46b603
//由文件i节点获取到的值  代表块号 每个块4k字节 0x1000
//in grub_ext2_read

//第三步: 获得测试文件的数据块内容
root@local:/home/dai#hexdump -C /dev/mmcblk0p2 -n 128 -s 0x46b603000
46b603000  31 32 33 34 35 73 73 64  6c 68 0a 36 37 38 39 30  |12345ssdlh.67890|
46b603010  0a 61 62 63 64 65 0a 68  61 68 68 61 68 61 68 61  |.abcde.hahhahaha|
46b603020  61 68 61 68 21 21 21 21  0a 00 00 00 00 00 00 00  |ahah!!!!........|
46b603030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
46b603080
#endif
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值