Magellan Linux

Contents of /trunk/grub/patches/grub-0.97-ext4-2.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 898 - (show annotations) (download)
Fri Oct 16 15:39:54 2009 UTC (14 years, 6 months ago) by niro
File size: 15802 byte(s)
-re-diffed for magellan

1 diff -Naur grub-0.97/stage2/fsys_ext2fs.c grub-0.97-magellan/stage2/fsys_ext2fs.c
2 --- grub-0.97/stage2/fsys_ext2fs.c 2004-08-08 20:19:18.000000000 +0200
3 +++ grub-0.97-magellan/stage2/fsys_ext2fs.c 2009-10-16 17:26:03.000000000 +0200
4 @@ -41,6 +41,7 @@
5 typedef unsigned short __u16;
6 typedef __signed__ int __s32;
7 typedef unsigned int __u32;
8 +typedef unsigned long long __u64;
9
10 /*
11 * Constants relative to the data blocks, from ext2_fs.h
12 @@ -51,7 +52,7 @@
13 #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
14 #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
15
16 -/* include/linux/ext2_fs.h */
17 +/* lib/ext2fs/ext2_fs.h from e2fsprogs */
18 struct ext2_super_block
19 {
20 __u32 s_inodes_count; /* Inodes count */
21 @@ -61,9 +62,9 @@
22 __u32 s_free_inodes_count; /* Free inodes count */
23 __u32 s_first_data_block; /* First Data Block */
24 __u32 s_log_block_size; /* Block size */
25 - __s32 s_log_frag_size; /* Fragment size */
26 + __s32 s_obso_log_frag_size; /* Obsoleted Fragment size */
27 __u32 s_blocks_per_group; /* # Blocks per group */
28 - __u32 s_frags_per_group; /* # Fragments per group */
29 + __u32 s_obso_frags_per_group; /* Obsoleted Fragments per group */
30 __u32 s_inodes_per_group; /* # Inodes per group */
31 __u32 s_mtime; /* Mount time */
32 __u32 s_wtime; /* Write time */
33 @@ -72,17 +73,32 @@
34 __u16 s_magic; /* Magic signature */
35 __u16 s_state; /* File system state */
36 __u16 s_errors; /* Behaviour when detecting errors */
37 - __u16 s_pad;
38 + __u16 s_minor_rev_level; /* minor revision level */
39 + __u16 s_desc_size; /* size of group descriptor */
40 __u32 s_lastcheck; /* time of last check */
41 __u32 s_checkinterval; /* max. time between checks */
42 __u32 s_creator_os; /* OS */
43 __u32 s_rev_level; /* Revision level */
44 __u16 s_def_resuid; /* Default uid for reserved blocks */
45 __u16 s_def_resgid; /* Default gid for reserved blocks */
46 - __u32 s_reserved[235]; /* Padding to the end of the block */
47 - };
48 + /* 64bit desc support valid if EXT4_FEATURE_INCOMPAT_64BIT */
49 + __u32 s_blocks_count_hi; /* Blocks count */
50 + __u32 s_r_blocks_count_hi; /* Reserved blocks count */
51 + __u32 s_free_blocks_count_hi; /* Free blocks count */
52 + __u16 s_min_extra_isize; /* All inodes have at least # bytes */
53 + __u16 s_max_extra_isize; /* New inodes should reverve # bytes */
54 + __u32 s_flags; /* Miscellaneous flags */
55 + __u16 s_raid_stride; /* Raid stride */
56 + __u16 s_mmp_interval; /* # seconds to wait MMP checking */
57 + __u64 s_mmp_block; /* Block for multi-mount protection */
58 + __u32 s_raid_stripe_width; /* Blocks on all data disks (N*stride)*/
59 + __u8 s_log_groups_per_flex;/* FLEX_BG group size*/
60 + __u8 s_reserved_char_pad;
61 + __u16 s_reserved_pad;
62 + __u32 s_reserved[162]; /* Padding to the end of the block */
63 +};
64
65 -struct ext2_group_desc
66 +struct ext4_group_desc
67 {
68 __u32 bg_block_bitmap; /* Blocks bitmap block */
69 __u32 bg_inode_bitmap; /* Inodes bitmap block */
70 @@ -90,8 +106,18 @@
71 __u16 bg_free_blocks_count; /* Free blocks count */
72 __u16 bg_free_inodes_count; /* Free inodes count */
73 __u16 bg_used_dirs_count; /* Directories count */
74 - __u16 bg_pad;
75 - __u32 bg_reserved[3];
76 + __u16 bg_flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */
77 + __u32 bg_reserved[2]; /* Likely block/inode bitmap checksum */
78 + __u16 bg_itable_unused; /* Unused inodes count */
79 + __u16 bg_checksum; /* crc16(sb_uuid+group+desc) */
80 + __u32 bg_block_bitmap_hi; /* Blocks bitmap block MSB */
81 + __u32 bg_inode_bitmap_hi; /* Inodes bitmap block MSB */
82 + __u32 bg_inode_table_hi; /* Inodes table block MSB */
83 + __u16 bg_free_blocks_count_hi;/* Free blocks count MSB */
84 + __u16 bg_free_inodes_count_hi;/* Free inodes count MSB */
85 + __u16 bg_used_dirs_count_hi; /* Directories count MSB */
86 + __u16 bg_itable_unused_hi; /* Unused inodes count MSB */
87 + __u32 bg_reserved2[3];
88 };
89
90 struct ext2_inode
91 @@ -129,22 +155,22 @@
92 __u32 i_block[EXT2_N_BLOCKS]; /* 40: Pointers to blocks */
93 __u32 i_version; /* File version (for NFS) */
94 __u32 i_file_acl; /* File ACL */
95 - __u32 i_dir_acl; /* Directory ACL */
96 - __u32 i_faddr; /* Fragment address */
97 + __u32 i_size_high;
98 + __u32 i_obso_faddr; /* Obsoleted fragment address */
99 union
100 {
101 struct
102 {
103 - __u8 l_i_frag; /* Fragment number */
104 - __u8 l_i_fsize; /* Fragment size */
105 - __u16 i_pad1;
106 - __u32 l_i_reserved2[2];
107 + __u16 l_i_blocks_high; /* were l_i_reserved1 */
108 + __u16 l_i_file_acl_high;
109 + __u16 l_i_uid_high; /* these 2 fields */
110 + __u16 l_i_gid_high; /* were reserved2[0] */
111 + __u32 l_i_reserved2;
112 }
113 linux2;
114 struct
115 {
116 - __u8 h_i_frag; /* Fragment number */
117 - __u8 h_i_fsize; /* Fragment size */
118 + __u16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
119 __u16 h_i_mode_high;
120 __u16 h_i_uid_high;
121 __u16 h_i_gid_high;
122 @@ -153,16 +179,36 @@
123 hurd2;
124 struct
125 {
126 - __u8 m_i_frag; /* Fragment number */
127 - __u8 m_i_fsize; /* Fragment size */
128 - __u16 m_pad1;
129 + __u16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
130 + __u16 m_i_file_acl_high;
131 __u32 m_i_reserved2[2];
132 }
133 masix2;
134 }
135 osd2; /* OS dependent 2 */
136 + __u16 i_extra_isize;
137 + __u16 i_pad1;
138 + __u32 i_ctime_extra; /* extra Change time (nsec << 2 | epoch) */
139 + __u32 i_mtime_extra; /* extra Modification time(nsec << 2 | epoch) */
140 + __u32 i_atime_extra; /* extra Access time (nsec << 2 | epoch) */
141 + __u32 i_crtime; /* File Creation time */
142 + __u32 i_crtime_extra; /* extra FileCreationtime (nsec << 2 | epoch) */
143 + __u32 i_version_hi; /* high 32 bits for 64-bit version */
144 };
145
146 +#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
147 +#define EXT4_FEATURE_INCOMPAT_64BIT 0x0080 /* grub not supported*/
148 +#define EXT4_FEATURE_INCOMPAT_MMP 0x0100
149 +#define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
150 +
151 +#define EXT4_HAS_INCOMPAT_FEATURE(sb,mask) \
152 + ( sb->s_feature_incompat & mask )
153 +
154 +#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
155 +#define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
156 +
157 +#define EXT4_MIN_DESC_SIZE 32
158 +
159 /* linux/limits.h */
160 #define NAME_MAX 255 /* # chars in a file name */
161
162 @@ -180,6 +226,56 @@
163 char name[EXT2_NAME_LEN]; /* File name */
164 };
165
166 +/* linux/ext4_fs_extents.h */
167 +/* This is the extent on-disk structure.
168 + * It's used at the bottom of the tree.
169 + */
170 +struct ext4_extent
171 + {
172 + __u32 ee_block; /* first logical block extent covers */
173 + __u16 ee_len; /* number of blocks covered by extent */
174 + __u16 ee_start_hi; /* high 16 bits of physical block */
175 + __u32 ee_start_lo; /* low 32 bits of physical block */
176 + };
177 +
178 +/*
179 + * This is index on-disk structure.
180 + * It's used at all the levels except the bottom.
181 + */
182 +struct ext4_extent_idx
183 + {
184 + __u32 ei_block; /* index covers logical blocks from 'block' */
185 + __u32 ei_leaf_lo; /* pointer to the physical block of the next *
186 + * level. leaf or next index could be there */
187 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
188 + __u16 ei_unused;
189 + };
190 +
191 +/*
192 + * Each block (leaves and indexes), even inode-stored has header.
193 + */
194 +struct ext4_extent_header
195 + {
196 + __u16 eh_magic; /* probably will support different formats */
197 + __u16 eh_entries; /* number of valid entries */
198 + __u16 eh_max; /* capacity of store in entries */
199 + __u16 eh_depth; /* has tree real underlying blocks? */
200 + __u32 eh_generation; /* generation of the tree */
201 + };
202 +
203 +#define EXT4_EXT_MAGIC (0xf30a)
204 +#define EXT_FIRST_EXTENT(__hdr__) \
205 + ((struct ext4_extent *) (((char *) (__hdr__)) + \
206 + sizeof(struct ext4_extent_header)))
207 +#define EXT_FIRST_INDEX(__hdr__) \
208 + ((struct ext4_extent_idx *) (((char *) (__hdr__)) + \
209 + sizeof(struct ext4_extent_header)))
210 +#define EXT_LAST_EXTENT(__hdr__) \
211 + (EXT_FIRST_EXTENT((__hdr__)) + (__u16)((__hdr__)->eh_entries) - 1)
212 +#define EXT_LAST_INDEX(__hdr__) \
213 + (EXT_FIRST_INDEX((__hdr__)) + (__u16)((__hdr__)->eh_entries) - 1)
214 +
215 +
216 /* linux/ext2fs.h */
217 /*
218 * EXT2_DIR_PAD defines the directory entries boundaries
219 @@ -223,8 +319,17 @@
220 /* kind of from ext2/super.c */
221 #define EXT2_BLOCK_SIZE(s) (1 << EXT2_BLOCK_SIZE_BITS(s))
222 /* linux/ext2fs.h */
223 +/* sizeof(struct ext2_group_desc) is changed in ext4
224 + * in kernel code, ext2/3 uses sizeof(struct ext2_group_desc) to calculate
225 + * number of desc per block, while ext4 uses superblock->s_desc_size in stead
226 + * superblock->s_desc_size is not available in ext2/3
227 + * */
228 +#define EXT2_DESC_SIZE(s) \
229 + (EXT4_HAS_INCOMPAT_FEATURE(s,EXT4_FEATURE_INCOMPAT_64BIT)? \
230 + s->s_desc_size : EXT4_MIN_DESC_SIZE)
231 #define EXT2_DESC_PER_BLOCK(s) \
232 - (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
233 + (EXT2_BLOCK_SIZE(s) / EXT2_DESC_SIZE(s))
234 +
235 /* linux/stat.h */
236 #define S_IFMT 00170000
237 #define S_IFLNK 0120000
238 @@ -386,6 +491,122 @@
239 [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
240 }
241
242 +/* extent binary search index
243 + * find closest index in the current level extent tree
244 + * kind of from ext4_ext_binsearch_idx in ext4/extents.c
245 + */
246 +static struct ext4_extent_idx*
247 +ext4_ext_binsearch_idx(struct ext4_extent_header* eh, int logical_block)
248 +{
249 + struct ext4_extent_idx *r, *l, *m;
250 + l = EXT_FIRST_INDEX(eh) + 1;
251 + r = EXT_LAST_INDEX(eh);
252 + while (l <= r)
253 + {
254 + m = l + (r - l) / 2;
255 + if (logical_block < m->ei_block)
256 + r = m - 1;
257 + else
258 + l = m + 1;
259 + }
260 + return (struct ext4_extent_idx*)(l - 1);
261 +}
262 +
263 +/* extent binary search
264 + * find closest extent in the leaf level
265 + * kind of from ext4_ext_binsearch in ext4/extents.c
266 + */
267 +static struct ext4_extent*
268 +ext4_ext_binsearch(struct ext4_extent_header* eh, int logical_block)
269 +{
270 + struct ext4_extent *r, *l, *m;
271 + l = EXT_FIRST_EXTENT(eh) + 1;
272 + r = EXT_LAST_EXTENT(eh);
273 + while (l <= r)
274 + {
275 + m = l + (r - l) / 2;
276 + if (logical_block < m->ee_block)
277 + r = m - 1;
278 + else
279 + l = m + 1;
280 + }
281 + return (struct ext4_extent*)(l - 1);
282 +}
283 +
284 +/* Maps extents enabled logical block into physical block via an inode.
285 + * EXT4_HUGE_FILE_FL should be checked before calling this.
286 + */
287 +static int
288 +ext4fs_block_map (int logical_block)
289 +{
290 + struct ext4_extent_header *eh;
291 + struct ext4_extent *ex, *extent;
292 + struct ext4_extent_idx *ei, *index;
293 + int depth;
294 + int i;
295 +
296 +#ifdef E2DEBUG
297 + unsigned char *i;
298 + for (i = (unsigned char *) INODE;
299 + i < ((unsigned char *) INODE + sizeof (struct ext2_inode));
300 + i++)
301 + {
302 + printf ("%c", "0123456789abcdef"[*i >> 4]);
303 + printf ("%c", "0123456789abcdef"[*i % 16]);
304 + if (!((i + 1 - (unsigned char *) INODE) % 16))
305 + {
306 + printf ("\n");
307 + }
308 + else
309 + {
310 + printf (" ");
311 + }
312 + }
313 + printf ("logical block %d\n", logical_block);
314 +#endif /* E2DEBUG */
315 + eh = (struct ext4_extent_header*)INODE->i_block;
316 + if (eh->eh_magic != EXT4_EXT_MAGIC)
317 + {
318 + errnum = ERR_FSYS_CORRUPT;
319 + return -1;
320 + }
321 + while((depth = eh->eh_depth) != 0)
322 + { /* extent index */
323 + if (eh->eh_magic != EXT4_EXT_MAGIC)
324 + {
325 + errnum = ERR_FSYS_CORRUPT;
326 + return -1;
327 + }
328 + ei = ext4_ext_binsearch_idx(eh, logical_block);
329 + if (ei->ei_leaf_hi)
330 + {/* 64bit physical block number not supported */
331 + errnum = ERR_FILELENGTH;
332 + return -1;
333 + }
334 + if (!ext2_rdfsb(ei->ei_leaf_lo, DATABLOCK1))
335 + {
336 + errnum = ERR_FSYS_CORRUPT;
337 + return -1;
338 + }
339 + eh = (struct ext4_extent_header*)DATABLOCK1;
340 + }
341 +
342 + /* depth==0, we come to the leaf */
343 + ex = ext4_ext_binsearch(eh, logical_block);
344 + if (ex->ee_start_hi)
345 + {/* 64bit physical block number not supported */
346 + errnum = ERR_FILELENGTH;
347 + return -1;
348 + }
349 + if ((ex->ee_block + ex->ee_len) < logical_block)
350 + {
351 + errnum = ERR_FSYS_CORRUPT;
352 + return -1;
353 + }
354 + return ex->ee_start_lo + logical_block - ex->ee_block;
355 +
356 +}
357 +
358 /* preconditions: all preconds of ext2fs_block_map */
359 int
360 ext2fs_read (char *buf, int len)
361 @@ -420,6 +641,11 @@
362 /* find the (logical) block component of our location */
363 logical_block = filepos >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
364 offset = filepos & (EXT2_BLOCK_SIZE (SUPERBLOCK) - 1);
365 + /* map extents enabled logical block number to physical fs on-disk block number */
366 + if (EXT4_HAS_INCOMPAT_FEATURE(SUPERBLOCK,EXT4_FEATURE_INCOMPAT_EXTENTS)
367 + && INODE->i_flags & EXT4_EXTENTS_FL)
368 + map = ext4fs_block_map (logical_block);
369 + else
370 map = ext2fs_block_map (logical_block);
371 #ifdef E2DEBUG
372 printf ("map=%d\n", map);
373 @@ -504,7 +730,7 @@
374 int desc; /* index within that group */
375 int ino_blk; /* fs pointer of the inode's information */
376 int str_chk = 0; /* used to hold the results of a string compare */
377 - struct ext2_group_desc *gdp;
378 + struct ext4_group_desc *ext4_gdp;
379 struct ext2_inode *raw_inode; /* inode info corresponding to current_ino */
380
381 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
382 @@ -550,8 +776,15 @@
383 {
384 return 0;
385 }
386 - gdp = GROUP_DESC;
387 - ino_blk = gdp[desc].bg_inode_table +
388 + ext4_gdp = (struct ext4_group_desc *)( (__u8*)GROUP_DESC +
389 + desc * EXT2_DESC_SIZE(SUPERBLOCK));
390 + if (EXT4_HAS_INCOMPAT_FEATURE(SUPERBLOCK, EXT4_FEATURE_INCOMPAT_64BIT)
391 + && (! ext4_gdp->bg_inode_table_hi))
392 + {/* 64bit itable not supported */
393 + errnum = ERR_FILELENGTH;
394 + return -1;
395 + }
396 + ino_blk = ext4_gdp->bg_inode_table +
397 (((current_ino - 1) % (SUPERBLOCK->s_inodes_per_group))
398 >> log2 (EXT2_BLOCK_SIZE (SUPERBLOCK) / sizeof (struct ext2_inode)));
399 #ifdef E2DEBUG
400 @@ -629,7 +862,10 @@
401 }
402 linkbuf[filemax + len] = '\0';
403
404 - /* Read the symlink data. */
405 + /* Read the symlink data.
406 + * Slow symlink is extents enabled
407 + * But since grub_read invokes ext2fs_read, nothing to change here
408 + */
409 if (! ext2_is_fast_symlink ())
410 {
411 /* Read the necessary blocks, and reset the file pointer. */
412 @@ -640,7 +876,9 @@
413 }
414 else
415 {
416 - /* Copy the data directly from the inode. */
417 + /* Copy the data directly from the inode.
418 + * Fast symlink is not extents enabled
419 + */
420 len = filemax;
421 memmove (linkbuf, (char *) INODE->i_block, len);
422 }
423 @@ -674,7 +912,13 @@
424 errnum = ERR_BAD_FILETYPE;
425 return 0;
426 }
427 -
428 + /* if file is too large, just stop and report an error*/
429 + if ( (INODE->i_flags & EXT4_HUGE_FILE_FL) && !(INODE->i_size_high))
430 + {
431 + /* file too large, stop reading */
432 + errnum = ERR_FILELENGTH;
433 + return 0;
434 + }
435 filemax = (INODE->i_size);
436 return 1;
437 }
438 @@ -728,17 +972,28 @@
439 }
440
441 /* else, find the (logical) block component of our location */
442 + /* ext4 logical block number the same as ext2/3 */
443 blk = loc >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
444
445 /* we know which logical block of the directory entry we are looking
446 for, now we have to translate that to the physical (fs) block on
447 the disk */
448 + /* map extents enabled logical block number to physical fs on-disk block number */
449 + if (EXT4_HAS_INCOMPAT_FEATURE(SUPERBLOCK,EXT4_FEATURE_INCOMPAT_EXTENTS)
450 + && INODE->i_flags & EXT4_EXTENTS_FL)
451 + map = ext4fs_block_map (blk);
452 + else
453 map = ext2fs_block_map (blk);
454 #ifdef E2DEBUG
455 printf ("fs block=%d\n", map);
456 #endif /* E2DEBUG */
457 mapblock2 = -1;
458 - if ((map < 0) || !ext2_rdfsb (map, DATABLOCK2))
459 + if (map < 0)
460 + {
461 + *rest = ch;
462 + return 0;
463 + }
464 + if (!ext2_rdfsb (map, DATABLOCK2))
465 {
466 errnum = ERR_FSYS_CORRUPT;
467 *rest = ch;