args changelog should mention this too
[grub2/phcoder.git] / fs / ext2.c
blobab50db010ad96c9009b08c0b4e4f8caeefd2bbb0
1 /* ext2.c - Second Extended filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 /* Magic value used to identify an ext2 filesystem. */
21 #define EXT2_MAGIC 0xEF53
22 /* Amount of indirect blocks in an inode. */
23 #define INDIRECT_BLOCKS 12
24 /* Maximum length of a pathname. */
25 #define EXT2_PATH_MAX 4096
26 /* Maximum nesting of symlinks, used to prevent a loop. */
27 #define EXT2_MAX_SYMLINKCNT 8
29 /* The good old revision and the default inode size. */
30 #define EXT2_GOOD_OLD_REVISION 0
31 #define EXT2_GOOD_OLD_INODE_SIZE 128
33 /* Filetype used in directory entry. */
34 #define FILETYPE_UNKNOWN 0
35 #define FILETYPE_REG 1
36 #define FILETYPE_DIRECTORY 2
37 #define FILETYPE_SYMLINK 7
39 /* Filetype information as used in inodes. */
40 #define FILETYPE_INO_MASK 0170000
41 #define FILETYPE_INO_REG 0100000
42 #define FILETYPE_INO_DIRECTORY 0040000
43 #define FILETYPE_INO_SYMLINK 0120000
45 #include <grub/err.h>
46 #include <grub/file.h>
47 #include <grub/mm.h>
48 #include <grub/misc.h>
49 #include <grub/disk.h>
50 #include <grub/dl.h>
51 #include <grub/types.h>
52 #include <grub/fshelp.h>
54 /* Log2 size of ext2 block in 512 blocks. */
55 #define LOG2_EXT2_BLOCK_SIZE(data) \
56 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 1)
58 /* Log2 size of ext2 block in bytes. */
59 #define LOG2_BLOCK_SIZE(data) \
60 (grub_le_to_cpu32 (data->sblock.log2_block_size) + 10)
62 /* The size of an ext2 block in bytes. */
63 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data))
65 /* The revision level. */
66 #define EXT2_REVISION(data) grub_le_to_cpu32 (data->sblock.revision_level)
68 /* The inode size. */
69 #define EXT2_INODE_SIZE(data) \
70 (EXT2_REVISION (data) == EXT2_GOOD_OLD_REVISION \
71 ? EXT2_GOOD_OLD_INODE_SIZE \
72 : grub_le_to_cpu16 (data->sblock.inode_size))
74 /* Superblock filesystem feature flags (RW compatible)
75 * A filesystem with any of these enabled can be read and written by a driver
76 * that does not understand them without causing metadata/data corruption. */
77 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001
78 #define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002
79 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
80 #define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008
81 #define EXT2_FEATURE_COMPAT_RESIZE_INODE 0x0010
82 #define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020
83 /* Superblock filesystem feature flags (RO compatible)
84 * A filesystem with any of these enabled can be safely read by a driver that
85 * does not understand them, but should not be written to, usually because
86 * additional metadata is required. */
87 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
88 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
89 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
90 #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
91 #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020
92 #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040
93 /* Superblock filesystem feature flags (back-incompatible)
94 * A filesystem with any of these enabled should not be attempted to be read
95 * by a driver that does not understand them, since they usually indicate
96 * metadata format changes that might confuse the reader. */
97 #define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001
98 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
99 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
100 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Volume is journal device */
101 #define EXT2_FEATURE_INCOMPAT_META_BG 0x0010
102 #define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* Extents used */
103 #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
104 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
106 /* The set of back-incompatible features this driver DOES support. Add (OR)
107 * flags here as the related features are implemented into the driver. */
108 #define EXT2_DRIVER_SUPPORTED_INCOMPAT ( EXT2_FEATURE_INCOMPAT_FILETYPE \
109 | EXT4_FEATURE_INCOMPAT_EXTENTS \
110 | EXT4_FEATURE_INCOMPAT_FLEX_BG )
111 /* List of rationales for the ignored "incompatible" features:
112 * needs_recovery: Not really back-incompatible - was added as such to forbid
113 * ext2 drivers from mounting an ext3 volume with a dirty
114 * journal because they will ignore the journal, but the next
115 * ext3 driver to mount the volume will find the journal and
116 * replay it, potentially corrupting the metadata written by
117 * the ext2 drivers. Safe to ignore for this RO driver. */
118 #define EXT2_DRIVER_IGNORED_INCOMPAT ( EXT3_FEATURE_INCOMPAT_RECOVER )
121 #define EXT3_JOURNAL_MAGIC_NUMBER 0xc03b3998U
123 #define EXT3_JOURNAL_DESCRIPTOR_BLOCK 1
124 #define EXT3_JOURNAL_COMMIT_BLOCK 2
125 #define EXT3_JOURNAL_SUPERBLOCK_V1 3
126 #define EXT3_JOURNAL_SUPERBLOCK_V2 4
127 #define EXT3_JOURNAL_REVOKE_BLOCK 5
129 #define EXT3_JOURNAL_FLAG_ESCAPE 1
130 #define EXT3_JOURNAL_FLAG_SAME_UUID 2
131 #define EXT3_JOURNAL_FLAG_DELETED 4
132 #define EXT3_JOURNAL_FLAG_LAST_TAG 8
134 #define EXT4_EXTENTS_FLAG 0x80000
136 /* The ext2 superblock. */
137 struct grub_ext2_sblock
139 grub_uint32_t total_inodes;
140 grub_uint32_t total_blocks;
141 grub_uint32_t reserved_blocks;
142 grub_uint32_t free_blocks;
143 grub_uint32_t free_inodes;
144 grub_uint32_t first_data_block;
145 grub_uint32_t log2_block_size;
146 grub_uint32_t log2_fragment_size;
147 grub_uint32_t blocks_per_group;
148 grub_uint32_t fragments_per_group;
149 grub_uint32_t inodes_per_group;
150 grub_uint32_t mtime;
151 grub_uint32_t utime;
152 grub_uint16_t mnt_count;
153 grub_uint16_t max_mnt_count;
154 grub_uint16_t magic;
155 grub_uint16_t fs_state;
156 grub_uint16_t error_handling;
157 grub_uint16_t minor_revision_level;
158 grub_uint32_t lastcheck;
159 grub_uint32_t checkinterval;
160 grub_uint32_t creator_os;
161 grub_uint32_t revision_level;
162 grub_uint16_t uid_reserved;
163 grub_uint16_t gid_reserved;
164 grub_uint32_t first_inode;
165 grub_uint16_t inode_size;
166 grub_uint16_t block_group_number;
167 grub_uint32_t feature_compatibility;
168 grub_uint32_t feature_incompat;
169 grub_uint32_t feature_ro_compat;
170 grub_uint16_t uuid[8];
171 char volume_name[16];
172 char last_mounted_on[64];
173 grub_uint32_t compression_info;
174 grub_uint8_t prealloc_blocks;
175 grub_uint8_t prealloc_dir_blocks;
176 grub_uint16_t reserved_gdt_blocks;
177 grub_uint8_t journal_uuid[16];
178 grub_uint32_t journal_inum;
179 grub_uint32_t journal_dev;
180 grub_uint32_t last_orphan;
181 grub_uint32_t hash_seed[4];
182 grub_uint8_t def_hash_version;
183 grub_uint8_t jnl_backup_type;
184 grub_uint16_t reserved_word_pad;
185 grub_uint32_t default_mount_opts;
186 grub_uint32_t first_meta_bg;
187 grub_uint32_t mkfs_time;
188 grub_uint32_t jnl_blocks[17];
191 /* The ext2 blockgroup. */
192 struct grub_ext2_block_group
194 grub_uint32_t block_id;
195 grub_uint32_t inode_id;
196 grub_uint32_t inode_table_id;
197 grub_uint16_t free_blocks;
198 grub_uint16_t free_inodes;
199 grub_uint16_t used_dirs;
200 grub_uint16_t pad;
201 grub_uint32_t reserved[3];
204 /* The ext2 inode. */
205 struct grub_ext2_inode
207 grub_uint16_t mode;
208 grub_uint16_t uid;
209 grub_uint32_t size;
210 grub_uint32_t atime;
211 grub_uint32_t ctime;
212 grub_uint32_t mtime;
213 grub_uint32_t dtime;
214 grub_uint16_t gid;
215 grub_uint16_t nlinks;
216 grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */
217 grub_uint32_t flags;
218 grub_uint32_t osd1;
219 union
221 struct datablocks
223 grub_uint32_t dir_blocks[INDIRECT_BLOCKS];
224 grub_uint32_t indir_block;
225 grub_uint32_t double_indir_block;
226 grub_uint32_t triple_indir_block;
227 } blocks;
228 char symlink[60];
230 grub_uint32_t version;
231 grub_uint32_t acl;
232 grub_uint32_t dir_acl;
233 grub_uint32_t fragment_addr;
234 grub_uint32_t osd2[3];
237 /* The header of an ext2 directory entry. */
238 struct ext2_dirent
240 grub_uint32_t inode;
241 grub_uint16_t direntlen;
242 grub_uint8_t namelen;
243 grub_uint8_t filetype;
246 struct grub_ext3_journal_header
248 grub_uint32_t magic;
249 grub_uint32_t block_type;
250 grub_uint32_t sequence;
253 struct grub_ext3_journal_revoke_header
255 struct grub_ext3_journal_header header;
256 grub_uint32_t count;
257 grub_uint32_t data[0];
260 struct grub_ext3_journal_block_tag
262 grub_uint32_t block;
263 grub_uint32_t flags;
266 struct grub_ext3_journal_sblock
268 struct grub_ext3_journal_header header;
269 grub_uint32_t block_size;
270 grub_uint32_t maxlen;
271 grub_uint32_t first;
272 grub_uint32_t sequence;
273 grub_uint32_t start;
276 #define EXT4_EXT_MAGIC 0xf30a
278 struct grub_ext4_extent_header
280 grub_uint16_t magic;
281 grub_uint16_t entries;
282 grub_uint16_t max;
283 grub_uint16_t depth;
284 grub_uint32_t generation;
287 struct grub_ext4_extent
289 grub_uint32_t block;
290 grub_uint16_t len;
291 grub_uint16_t start_hi;
292 grub_uint32_t start;
295 struct grub_ext4_extent_idx
297 grub_uint32_t block;
298 grub_uint32_t leaf;
299 grub_uint16_t leaf_hi;
300 grub_uint16_t unused;
303 struct grub_fshelp_node
305 struct grub_ext2_data *data;
306 struct grub_ext2_inode inode;
307 int ino;
308 int inode_read;
311 /* Information about a "mounted" ext2 filesystem. */
312 struct grub_ext2_data
314 struct grub_ext2_sblock sblock;
315 grub_disk_t disk;
316 struct grub_ext2_inode *inode;
317 struct grub_fshelp_node diropen;
320 #ifndef GRUB_UTIL
321 static grub_dl_t my_mod;
322 #endif
326 /* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of
327 the mounted filesystem DATA. */
328 inline static grub_err_t
329 grub_ext2_blockgroup (struct grub_ext2_data *data, int group,
330 struct grub_ext2_block_group *blkgrp)
332 return grub_disk_read (data->disk,
333 ((grub_le_to_cpu32 (data->sblock.first_data_block) + 1)
334 << LOG2_EXT2_BLOCK_SIZE (data)),
335 group * sizeof (struct grub_ext2_block_group),
336 sizeof (struct grub_ext2_block_group), (char *) blkgrp);
339 static struct grub_ext4_extent_header *
340 grub_ext4_find_leaf (struct grub_ext2_data *data, char *buf,
341 struct grub_ext4_extent_header *ext_block,
342 grub_uint32_t fileblock)
344 struct grub_ext4_extent_idx *index;
346 while (1)
348 int i;
349 grub_disk_addr_t block;
351 index = (struct grub_ext4_extent_idx *) (ext_block + 1);
353 if (grub_le_to_cpu16(ext_block->magic) != EXT4_EXT_MAGIC)
354 return 0;
356 if (ext_block->depth == 0)
357 return ext_block;
359 for (i = 0; i < grub_le_to_cpu16 (ext_block->entries); i++)
361 if (fileblock < grub_le_to_cpu32(index[i].block))
362 break;
365 if (--i < 0)
366 return 0;
368 block = grub_le_to_cpu16 (index[i].leaf_hi);
369 block = (block << 32) + grub_le_to_cpu32 (index[i].leaf);
370 if (grub_disk_read (data->disk,
371 block << LOG2_EXT2_BLOCK_SIZE (data),
372 0, EXT2_BLOCK_SIZE(data), buf))
373 return 0;
375 ext_block = (struct grub_ext4_extent_header *) buf;
379 static grub_disk_addr_t
380 grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
382 struct grub_ext2_data *data = node->data;
383 struct grub_ext2_inode *inode = &node->inode;
384 int blknr = -1;
385 unsigned int blksz = EXT2_BLOCK_SIZE (data);
386 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
388 if (grub_le_to_cpu32(inode->flags) & EXT4_EXTENTS_FLAG)
390 char buf[EXT2_BLOCK_SIZE(data)];
391 struct grub_ext4_extent_header *leaf;
392 struct grub_ext4_extent *ext;
393 int i;
395 leaf = grub_ext4_find_leaf (data, buf,
396 (struct grub_ext4_extent_header *) inode->blocks.dir_blocks,
397 fileblock);
398 if (! leaf)
400 grub_error (GRUB_ERR_BAD_FS, "invalid extent");
401 return -1;
404 ext = (struct grub_ext4_extent *) (leaf + 1);
405 for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++)
407 if (fileblock < grub_le_to_cpu32 (ext[i].block))
408 break;
411 if (--i >= 0)
413 fileblock -= grub_le_to_cpu32 (ext[i].block);
414 if (fileblock >= grub_le_to_cpu16 (ext[i].len))
415 return 0;
416 else
418 grub_disk_addr_t start;
420 start = grub_le_to_cpu16 (ext[i].start_hi);
421 start = (start << 32) + grub_le_to_cpu32 (ext[i].start);
423 return fileblock + start;
426 else
428 grub_error (GRUB_ERR_BAD_FS, "something wrong with extent");
429 return -1;
432 /* Direct blocks. */
433 if (fileblock < INDIRECT_BLOCKS)
434 blknr = grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
435 /* Indirect. */
436 else if (fileblock < INDIRECT_BLOCKS + blksz / 4)
438 grub_uint32_t indir[blksz / 4];
440 if (grub_disk_read (data->disk,
441 grub_le_to_cpu32 (inode->blocks.indir_block)
442 << log2_blksz,
443 0, blksz, (char *) indir))
444 return grub_errno;
446 blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
448 /* Double indirect. */
449 else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1))
451 unsigned int perblock = blksz / 4;
452 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
453 + blksz / 4);
454 grub_uint32_t indir[blksz / 4];
456 if (grub_disk_read (data->disk,
457 grub_le_to_cpu32 (inode->blocks.double_indir_block)
458 << log2_blksz,
459 0, blksz, (char *) indir))
460 return grub_errno;
462 if (grub_disk_read (data->disk,
463 grub_le_to_cpu32 (indir[rblock / perblock])
464 << log2_blksz,
465 0, blksz, (char *) indir))
466 return grub_errno;
469 blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
471 /* triple indirect. */
472 else
474 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
475 "ext2fs doesn't support triple indirect blocks");
478 return blknr;
481 /* Read LEN bytes from the file described by DATA starting with byte
482 POS. Return the amount of read bytes in READ. */
483 static grub_ssize_t
484 grub_ext2_read_file (grub_fshelp_node_t node,
485 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
486 unsigned offset, unsigned length),
487 int pos, grub_size_t len, char *buf)
489 return grub_fshelp_read_file (node->data->disk, node, read_hook,
490 pos, len, buf, grub_ext2_read_block,
491 node->inode.size,
492 LOG2_EXT2_BLOCK_SIZE (node->data));
497 /* Read the inode INO for the file described by DATA into INODE. */
498 static grub_err_t
499 grub_ext2_read_inode (struct grub_ext2_data *data,
500 int ino, struct grub_ext2_inode *inode)
502 struct grub_ext2_block_group blkgrp;
503 struct grub_ext2_sblock *sblock = &data->sblock;
504 int inodes_per_block;
505 unsigned int blkno;
506 unsigned int blkoff;
508 /* It is easier to calculate if the first inode is 0. */
509 ino--;
511 grub_ext2_blockgroup (data,
512 ino / grub_le_to_cpu32 (sblock->inodes_per_group),
513 &blkgrp);
514 if (grub_errno)
515 return grub_errno;
517 inodes_per_block = EXT2_BLOCK_SIZE (data) / EXT2_INODE_SIZE (data);
518 blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
519 / inodes_per_block;
520 blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
521 % inodes_per_block;
523 /* Read the inode. */
524 if (grub_disk_read (data->disk,
525 ((grub_le_to_cpu32 (blkgrp.inode_table_id) + blkno)
526 << LOG2_EXT2_BLOCK_SIZE (data)),
527 EXT2_INODE_SIZE (data) * blkoff,
528 sizeof (struct grub_ext2_inode), (char *) inode))
529 return grub_errno;
531 return 0;
534 static struct grub_ext2_data *
535 grub_ext2_mount (grub_disk_t disk)
537 struct grub_ext2_data *data;
539 data = grub_malloc (sizeof (struct grub_ext2_data));
540 if (!data)
541 return 0;
543 /* Read the superblock. */
544 grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock),
545 (char *) &data->sblock);
546 if (grub_errno)
547 goto fail;
549 /* Make sure this is an ext2 filesystem. */
550 if (grub_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC)
552 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
553 goto fail;
556 /* Check the FS doesn't have feature bits enabled that we don't support. */
557 if (grub_le_to_cpu32 (data->sblock.feature_incompat)
558 & ~(EXT2_DRIVER_SUPPORTED_INCOMPAT | EXT2_DRIVER_IGNORED_INCOMPAT))
560 grub_error (GRUB_ERR_BAD_FS, "filesystem has unsupported incompatible features");
561 goto fail;
565 data->disk = disk;
567 data->diropen.data = data;
568 data->diropen.ino = 2;
569 data->diropen.inode_read = 1;
571 data->inode = &data->diropen.inode;
573 grub_ext2_read_inode (data, 2, data->inode);
574 if (grub_errno)
575 goto fail;
577 return data;
579 fail:
580 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
581 grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
583 grub_free (data);
584 return 0;
587 static char *
588 grub_ext2_read_symlink (grub_fshelp_node_t node)
590 char *symlink;
591 struct grub_fshelp_node *diro = node;
593 if (! diro->inode_read)
595 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
596 if (grub_errno)
597 return 0;
600 symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1);
601 if (! symlink)
602 return 0;
604 /* If the filesize of the symlink is bigger than
605 60 the symlink is stored in a separate block,
606 otherwise it is stored in the inode. */
607 if (grub_le_to_cpu32 (diro->inode.size) <= 60)
608 grub_strncpy (symlink,
609 diro->inode.symlink,
610 grub_le_to_cpu32 (diro->inode.size));
611 else
613 grub_ext2_read_file (diro, 0, 0,
614 grub_le_to_cpu32 (diro->inode.size),
615 symlink);
616 if (grub_errno)
618 grub_free (symlink);
619 return 0;
623 symlink[grub_le_to_cpu32 (diro->inode.size)] = '\0';
624 return symlink;
627 static int
628 grub_ext2_iterate_dir (grub_fshelp_node_t dir,
629 int NESTED_FUNC_ATTR
630 (*hook) (const char *filename,
631 enum grub_fshelp_filetype filetype,
632 grub_fshelp_node_t node))
634 unsigned int fpos = 0;
635 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
637 if (! diro->inode_read)
639 grub_ext2_read_inode (diro->data, diro->ino, &diro->inode);
640 if (grub_errno)
641 return 0;
644 /* Search the file. */
645 while (fpos < grub_le_to_cpu32 (diro->inode.size))
647 struct ext2_dirent dirent;
649 grub_ext2_read_file (diro, 0, fpos, sizeof (struct ext2_dirent),
650 (char *) &dirent);
651 if (grub_errno)
652 return 0;
654 if (dirent.namelen != 0)
656 char filename[dirent.namelen + 1];
657 struct grub_fshelp_node *fdiro;
658 enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
660 grub_ext2_read_file (diro, 0, fpos + sizeof (struct ext2_dirent),
661 dirent.namelen, filename);
662 if (grub_errno)
663 return 0;
665 fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
666 if (! fdiro)
667 return 0;
669 fdiro->data = diro->data;
670 fdiro->ino = grub_le_to_cpu32 (dirent.inode);
672 filename[dirent.namelen] = '\0';
674 if (dirent.filetype != FILETYPE_UNKNOWN)
676 fdiro->inode_read = 0;
678 if (dirent.filetype == FILETYPE_DIRECTORY)
679 type = GRUB_FSHELP_DIR;
680 else if (dirent.filetype == FILETYPE_SYMLINK)
681 type = GRUB_FSHELP_SYMLINK;
682 else if (dirent.filetype == FILETYPE_REG)
683 type = GRUB_FSHELP_REG;
685 else
687 /* The filetype can not be read from the dirent, read
688 the inode to get more information. */
689 grub_ext2_read_inode (diro->data,
690 grub_le_to_cpu32 (dirent.inode),
691 &fdiro->inode);
692 if (grub_errno)
694 grub_free (fdiro);
695 return 0;
698 fdiro->inode_read = 1;
700 if ((grub_le_to_cpu16 (fdiro->inode.mode)
701 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
702 type = GRUB_FSHELP_DIR;
703 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
704 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
705 type = GRUB_FSHELP_SYMLINK;
706 else if ((grub_le_to_cpu16 (fdiro->inode.mode)
707 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
708 type = GRUB_FSHELP_REG;
711 if (hook (filename, type, fdiro))
712 return 1;
715 fpos += grub_le_to_cpu16 (dirent.direntlen);
718 return 0;
721 /* Open a file named NAME and initialize FILE. */
722 static grub_err_t
723 grub_ext2_open (struct grub_file *file, const char *name)
725 struct grub_ext2_data *data;
726 struct grub_fshelp_node *fdiro = 0;
728 #ifndef GRUB_UTIL
729 grub_dl_ref (my_mod);
730 #endif
732 data = grub_ext2_mount (file->device->disk);
733 if (! data)
734 goto fail;
736 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_ext2_iterate_dir,
737 grub_ext2_read_symlink, GRUB_FSHELP_REG);
738 if (grub_errno)
739 goto fail;
741 if (! fdiro->inode_read)
743 grub_ext2_read_inode (data, fdiro->ino, &fdiro->inode);
744 if (grub_errno)
745 goto fail;
748 grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_ext2_inode));
749 grub_free (fdiro);
751 file->size = grub_le_to_cpu32 (data->inode->size);
752 file->data = data;
753 file->offset = 0;
755 return 0;
757 fail:
758 if (fdiro != &data->diropen)
759 grub_free (fdiro);
760 grub_free (data);
762 #ifndef GRUB_UTIL
763 grub_dl_unref (my_mod);
764 #endif
766 return grub_errno;
769 static grub_err_t
770 grub_ext2_close (grub_file_t file)
772 grub_free (file->data);
774 #ifndef GRUB_UTIL
775 grub_dl_unref (my_mod);
776 #endif
778 return GRUB_ERR_NONE;
781 /* Read LEN bytes data from FILE into BUF. */
782 static grub_ssize_t
783 grub_ext2_read (grub_file_t file, char *buf, grub_size_t len)
785 struct grub_ext2_data *data = (struct grub_ext2_data *) file->data;
787 return grub_ext2_read_file (&data->diropen, file->read_hook,
788 file->offset, len, buf);
792 static grub_err_t
793 grub_ext2_dir (grub_device_t device, const char *path,
794 int (*hook) (const char *filename,
795 const struct grub_dirhook_info *info))
797 struct grub_ext2_data *data = 0;;
798 struct grub_fshelp_node *fdiro = 0;
800 auto int NESTED_FUNC_ATTR iterate (const char *filename,
801 enum grub_fshelp_filetype filetype,
802 grub_fshelp_node_t node);
804 int NESTED_FUNC_ATTR iterate (const char *filename,
805 enum grub_fshelp_filetype filetype,
806 grub_fshelp_node_t node)
808 struct grub_dirhook_info info;
809 grub_memset (&info, 0, sizeof (info));
810 if (! node->inode_read)
812 grub_ext2_read_inode (data, node->ino, &node->inode);
813 if (!grub_errno)
814 node->inode_read = 1;
815 grub_errno = GRUB_ERR_NONE;
817 if (node->inode_read)
819 info.mtimeset = 1;
820 info.mtime = grub_le_to_cpu32 (node->inode.mtime);
823 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
824 grub_free (node);
825 return hook (filename, &info);
828 #ifndef GRUB_UTIL
829 grub_dl_ref (my_mod);
830 #endif
832 data = grub_ext2_mount (device->disk);
833 if (! data)
834 goto fail;
836 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_ext2_iterate_dir,
837 grub_ext2_read_symlink, GRUB_FSHELP_DIR);
838 if (grub_errno)
839 goto fail;
841 grub_ext2_iterate_dir (fdiro, iterate);
843 fail:
844 if (fdiro != &data->diropen)
845 grub_free (fdiro);
846 grub_free (data);
848 #ifndef GRUB_UTIL
849 grub_dl_unref (my_mod);
850 #endif
852 return grub_errno;
855 static grub_err_t
856 grub_ext2_label (grub_device_t device, char **label)
858 struct grub_ext2_data *data;
859 grub_disk_t disk = device->disk;
861 #ifndef GRUB_UTIL
862 grub_dl_ref (my_mod);
863 #endif
865 data = grub_ext2_mount (disk);
866 if (data)
867 *label = grub_strndup (data->sblock.volume_name, 14);
868 else
869 *label = NULL;
871 #ifndef GRUB_UTIL
872 grub_dl_unref (my_mod);
873 #endif
875 grub_free (data);
877 return grub_errno;
880 static grub_err_t
881 grub_ext2_uuid (grub_device_t device, char **uuid)
883 struct grub_ext2_data *data;
884 grub_disk_t disk = device->disk;
886 #ifndef GRUB_UTIL
887 grub_dl_ref (my_mod);
888 #endif
890 data = grub_ext2_mount (disk);
891 if (data)
893 *uuid = grub_malloc (40 + sizeof ('\0'));
894 grub_sprintf (*uuid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
895 grub_be_to_cpu16 (data->sblock.uuid[0]), grub_be_to_cpu16 (data->sblock.uuid[1]),
896 grub_be_to_cpu16 (data->sblock.uuid[2]), grub_be_to_cpu16 (data->sblock.uuid[3]),
897 grub_be_to_cpu16 (data->sblock.uuid[4]), grub_be_to_cpu16 (data->sblock.uuid[5]),
898 grub_be_to_cpu16 (data->sblock.uuid[6]), grub_be_to_cpu16 (data->sblock.uuid[7]));
900 else
901 *uuid = NULL;
903 #ifndef GRUB_UTIL
904 grub_dl_unref (my_mod);
905 #endif
907 grub_free (data);
909 return grub_errno;
912 /* Get mtime. */
913 static grub_err_t
914 grub_ext2_mtime (grub_device_t device, grub_int32_t *tm)
916 struct grub_ext2_data *data;
917 grub_disk_t disk = device->disk;
919 #ifndef GRUB_UTIL
920 grub_dl_ref (my_mod);
921 #endif
923 data = grub_ext2_mount (disk);
924 if (!data)
925 *tm = 0;
926 else
927 *tm = grub_le_to_cpu32 (data->sblock.utime);
929 #ifndef GRUB_UTIL
930 grub_dl_unref (my_mod);
931 #endif
933 grub_free (data);
935 return grub_errno;
941 static struct grub_fs grub_ext2_fs =
943 .name = "ext2",
944 .dir = grub_ext2_dir,
945 .open = grub_ext2_open,
946 .read = grub_ext2_read,
947 .close = grub_ext2_close,
948 .label = grub_ext2_label,
949 .uuid = grub_ext2_uuid,
950 .mtime = grub_ext2_mtime,
951 .next = 0
954 GRUB_MOD_INIT(ext2)
956 grub_fs_register (&grub_ext2_fs);
957 #ifndef GRUB_UTIL
958 my_mod = mod;
959 #endif
962 GRUB_MOD_FINI(ext2)
964 grub_fs_unregister (&grub_ext2_fs);