1 --- a/stage2/fsys_ext2fs.c
2 +++ b/stage2/fsys_ext2fs.c
3 @@ -51,6 +51,9 @@ typedef unsigned int __u32;
4 #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
5 #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
8 +#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
10 /* include/linux/ext2_fs.h */
11 struct ext2_super_block
13 @@ -191,6 +194,42 @@ struct ext2_dir_entry
14 #define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
17 +/* linux/ext4_fs_extents.h */
19 + * This is the extent on-disk structure.
20 + * It's used at the bottom of the tree.
23 + __u32 ee_block; /* first logical block extent covers */
24 + __u16 ee_len; /* number of blocks covered by extent */
25 + __u16 ee_start_hi; /* high 16 bits of physical block */
26 + __u32 ee_start; /* low 32 bits of physical block */
30 + * This is index on-disk structure.
31 + * It's used at all the levels except the bottom.
33 +struct ext4_extent_idx {
34 + __u32 ei_block; /* index covers logical blocks from 'block' */
35 + __u32 ei_leaf; /* pointer to the physical block of the next *
36 + * level. leaf or next index could be there */
37 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
42 + * Each block (leaves and indexes), even inode-stored has header.
44 +struct ext4_extent_header {
45 + __u16 eh_magic; /* probably will support different formats */
46 + __u16 eh_entries; /* number of valid entries */
47 + __u16 eh_max; /* capacity of store in entries */
48 + __u16 eh_depth; /* has tree real underlying blocks? */
49 + __u32 eh_generation; /* generation of the tree */
52 +#define EXT4_EXT_MAGIC 0xf30a
55 #define log2(n) ffz(~(n))
56 @@ -279,6 +318,27 @@ ext2_rdfsb (int fsblock, int buffer)
57 EXT2_BLOCK_SIZE (SUPERBLOCK), (char *) buffer);
60 +/* Walk through extents index tree to find the good leaf */
61 +static struct ext4_extent_header *
62 +ext4_recurse_extent_index(struct ext4_extent_header *extent_block, int logical_block)
65 + struct ext4_extent_idx *index = (struct ext4_extent_idx *) (extent_block + 1);
66 + if (extent_block->eh_magic != EXT4_EXT_MAGIC)
68 + if (extent_block->eh_depth == 0)
69 + return extent_block;
70 + for (i = 0; i < extent_block->eh_entries; i++)
72 + if (logical_block < index[i].ei_block)
75 + if (i == 0 || !ext2_rdfsb(index[i-1].ei_leaf, DATABLOCK1))
77 + return (ext4_recurse_extent_index((struct ext4_extent_header *) DATABLOCK1, logical_block));
82 ext2/inode.c:ext2_bmap()
84 @@ -287,7 +347,6 @@ ext2_rdfsb (int fsblock, int buffer)
86 ext2fs_block_map (int logical_block)
91 for (i = (unsigned char *) INODE;
92 @@ -308,82 +367,106 @@ ext2fs_block_map (int logical_block)
93 printf ("logical block %d\n", logical_block);
96 - /* if it is directly pointed to by the inode, return that physical addr */
97 - if (logical_block < EXT2_NDIR_BLOCKS)
98 + if (!(INODE->i_flags & EXT4_EXTENTS_FL))
101 - printf ("returning %d\n", (unsigned char *) (INODE->i_block[logical_block]));
102 - printf ("returning %d\n", INODE->i_block[logical_block]);
103 -#endif /* E2DEBUG */
104 - return INODE->i_block[logical_block];
107 - logical_block -= EXT2_NDIR_BLOCKS;
108 - /* try the indirect block */
109 - if (logical_block < EXT2_ADDR_PER_BLOCK (SUPERBLOCK))
112 - && !ext2_rdfsb (INODE->i_block[EXT2_IND_BLOCK], DATABLOCK1))
114 - errnum = ERR_FSYS_CORRUPT;
118 - return ((__u32 *) DATABLOCK1)[logical_block];
121 - logical_block -= EXT2_ADDR_PER_BLOCK (SUPERBLOCK);
122 - /* now try the double indirect block */
123 - if (logical_block < (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2)))
127 - && !ext2_rdfsb (INODE->i_block[EXT2_DIND_BLOCK], DATABLOCK1))
129 - errnum = ERR_FSYS_CORRUPT;
133 - if ((bnum = (((__u32 *) DATABLOCK1)
134 - [logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)]))
136 - && !ext2_rdfsb (bnum, DATABLOCK2))
138 - errnum = ERR_FSYS_CORRUPT;
142 + /* if it is directly pointed to by the inode, return that physical addr */
143 + if (logical_block < EXT2_NDIR_BLOCKS)
146 + printf ("returning %d\n", (unsigned char *) (INODE->i_block[logical_block]));
147 + printf ("returning %d\n", INODE->i_block[logical_block]);
148 +#endif /* E2DEBUG */
149 + return INODE->i_block[logical_block];
152 + logical_block -= EXT2_NDIR_BLOCKS;
153 + /* try the indirect block */
154 + if (logical_block < EXT2_ADDR_PER_BLOCK (SUPERBLOCK))
156 + if (mapblock1 != 1 && !ext2_rdfsb (INODE->i_block[EXT2_IND_BLOCK], DATABLOCK1))
158 + errnum = ERR_FSYS_CORRUPT;
162 + return ((__u32 *) DATABLOCK1)[logical_block];
165 + logical_block -= EXT2_ADDR_PER_BLOCK (SUPERBLOCK);
166 + /* now try the double indirect block */
167 + if (logical_block < (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2)))
170 + if (mapblock1 != 2 && !ext2_rdfsb (INODE->i_block[EXT2_DIND_BLOCK], DATABLOCK1))
172 + errnum = ERR_FSYS_CORRUPT;
176 + if ((bnum = (((__u32 *) DATABLOCK1)
177 + [logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)]))
179 + && !ext2_rdfsb (bnum, DATABLOCK2))
181 + errnum = ERR_FSYS_CORRUPT;
185 + return ((__u32 *) DATABLOCK2)
186 + [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
190 + logical_block -= (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2));
192 + && !ext2_rdfsb (INODE->i_block[EXT2_TIND_BLOCK], DATABLOCK1))
194 + errnum = ERR_FSYS_CORRUPT;
198 + if (!ext2_rdfsb (((__u32 *) DATABLOCK1)
199 + [logical_block >> (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)
203 + errnum = ERR_FSYS_CORRUPT;
206 + if (!ext2_rdfsb (((__u32 *) DATABLOCK2)
207 + [(logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK))
208 + & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)],
211 + errnum = ERR_FSYS_CORRUPT;
215 return ((__u32 *) DATABLOCK2)
216 - [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
217 + [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
221 - logical_block -= (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2));
223 - && !ext2_rdfsb (INODE->i_block[EXT2_TIND_BLOCK], DATABLOCK1))
225 - errnum = ERR_FSYS_CORRUPT;
229 - if (!ext2_rdfsb (((__u32 *) DATABLOCK1)
230 - [logical_block >> (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)
234 - errnum = ERR_FSYS_CORRUPT;
237 - if (!ext2_rdfsb (((__u32 *) DATABLOCK2)
238 - [(logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK))
239 - & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)],
241 + /* inode is in extents format */
245 + struct ext4_extent_header *extent_hdr =
246 + ext4_recurse_extent_index((struct ext4_extent_header *) INODE->i_block, logical_block);
247 + struct ext4_extent *extent = (struct ext4_extent *) (extent_hdr + 1);
248 + if ( extent_hdr == NULL || extent_hdr->eh_magic != EXT4_EXT_MAGIC)
250 + errnum = ERR_FSYS_CORRUPT;
253 + for (i = 0; i<extent_hdr->eh_entries; i++)
255 + if (extent[i].ee_block <= logical_block && logical_block < extent[i].ee_block + extent[i].ee_len && !(extent[i].ee_len>>15))
256 + return (logical_block - extent[i].ee_block + extent[i].ee_start);
258 + /* We should not arrive here */
260 errnum = ERR_FSYS_CORRUPT;
263 - return ((__u32 *) DATABLOCK2)
264 - [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
267 /* preconditions: all preconds of ext2fs_block_map */