1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <xenfsimage_grub.h>
20
21 #define mapblock1 (*fsig_int1(ffi))
22 #define mapblock2 (*fsig_int2(ffi))
23
24 /* sizes are always in bytes, BLOCK values are always in DEV_BSIZE (sectors) */
25 #define DEV_BSIZE 512
26
27 /* include/linux/fs.h */
28 #define BLOCK_SIZE 1024 /* initial block size for superblock read */
29 /* made up, defaults to 1 but can be passed via mount_opts */
30 #define WHICH_SUPER 1
31 /* kind of from fs/ext2/super.c */
32 #define SBLOCK (WHICH_SUPER * BLOCK_SIZE / DEV_BSIZE) /* = 2 */
33
34 /* include/asm-i386/types.h */
35 typedef __signed__ char __s8;
36 typedef unsigned char __u8;
37 typedef __signed__ short __s16;
38 typedef unsigned short __u16;
39 typedef __signed__ int __s32;
40 typedef unsigned int __u32;
41
42 /*
43 * Constants relative to the data blocks, from ext2_fs.h
44 */
45 #define EXT2_NDIR_BLOCKS 12
46 #define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
47 #define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
48 #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
49 #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
50
51 /* Inode flags */
52 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
53
54 /* include/linux/ext2_fs.h */
55 struct ext2_super_block
56 {
57 __u32 s_inodes_count; /* Inodes count */
58 __u32 s_blocks_count; /* Blocks count */
59 __u32 s_r_blocks_count; /* Reserved blocks count */
60 __u32 s_free_blocks_count; /* Free blocks count */
61 __u32 s_free_inodes_count; /* Free inodes count */
62 __u32 s_first_data_block; /* First Data Block */
63 __u32 s_log_block_size; /* Block size */
64 __s32 s_log_frag_size; /* Fragment size */
65 __u32 s_blocks_per_group; /* # Blocks per group */
66 __u32 s_frags_per_group; /* # Fragments per group */
67 __u32 s_inodes_per_group; /* # Inodes per group */
68 __u32 s_mtime; /* Mount time */
69 __u32 s_wtime; /* Write time */
70 __u16 s_mnt_count; /* Mount count */
71 __s16 s_max_mnt_count; /* Maximal mount count */
72 __u16 s_magic; /* Magic signature */
73 __u16 s_state; /* File system state */
74 __u16 s_errors; /* Behaviour when detecting errors */
75 __u16 s_pad;
76 __u32 s_lastcheck; /* time of last check */
77 __u32 s_checkinterval; /* max. time between checks */
78 __u32 s_creator_os; /* OS */
79 __u32 s_rev_level; /* Revision level */
80 __u16 s_def_resuid; /* Default uid for reserved blocks */
81 __u16 s_def_resgid; /* Default gid for reserved blocks */
82 /*
83 * These fields are for EXT2_DYNAMIC_REV superblocks only.
84 *
85 * Note: the difference between the compatible feature set and
86 * the incompatible feature set is that if there is a bit set
87 * in the incompatible feature set that the kernel doesn't
88 * know about, it should refuse to mount the filesystem.
89 *
90 * e2fsck's requirements are more strict; if it doesn't know
91 * about a feature in either the compatible or incompatible
92 * feature set, it must abort and not try to meddle with
93 * things it doesn't understand...
94 */
95 __u32 s_first_ino; /* First non-reserved inode */
96 __u16 s_inode_size; /* size of inode structure */
97 __u16 s_block_group_nr; /* block group # of this superblock */
98 __u32 s_feature_compat; /* compatible feature set */
99 __u32 s_feature_incompat; /* incompatible feature set */
100 __u32 s_feature_ro_compat; /* readonly-compatible feature set */
101 __u8 s_uuid[16]; /* 128-bit uuid for volume */
102 char s_volume_name[16]; /* volume name */
103 char s_last_mounted[64]; /* directory where last mounted */
104 __u32 s_algorithm_usage_bitmap; /* For compression */
105 /*
106 * Performance hints. Directory preallocation should only
107 * happen if the EXT2_FEATURE_COMPAT_DIR_PREALLOC flag is on.
108 */
109 __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
110 __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
111 __u16 s_reserved_gdt_blocks;/* Per group table for online growth */
112 /*
113 * Journaling support valid if EXT2_FEATURE_COMPAT_HAS_JOURNAL set.
114 */
115 __u8 s_journal_uuid[16]; /* uuid of journal superblock */
116 __u32 s_journal_inum; /* inode number of journal file */
117 __u32 s_journal_dev; /* device number of journal file */
118 __u32 s_last_orphan; /* start of list of inodes to delete */
119 __u32 s_hash_seed[4]; /* HTREE hash seed */
120 __u8 s_def_hash_version; /* Default hash version to use */
121 __u8 s_jnl_backup_type; /* Default type of journal backup */
122 __u16 s_reserved_word_pad;
123 __u32 s_default_mount_opts;
124 __u32 s_first_meta_bg; /* First metablock group */
125 __u32 s_mkfs_time; /* When the filesystem was created */
126 __u32 s_jnl_blocks[17]; /* Backup of the journal inode */
127 __u32 s_reserved[172]; /* Padding to the end of the block */
128 };
129
130 struct ext2_group_desc
131 {
132 __u32 bg_block_bitmap; /* Blocks bitmap block */
133 __u32 bg_inode_bitmap; /* Inodes bitmap block */
134 __u32 bg_inode_table; /* Inodes table block */
135 __u16 bg_free_blocks_count; /* Free blocks count */
136 __u16 bg_free_inodes_count; /* Free inodes count */
137 __u16 bg_used_dirs_count; /* Directories count */
138 __u16 bg_pad;
139 __u32 bg_reserved[3];
140 };
141
142 struct ext2_inode
143 {
144 __u16 i_mode; /* File mode */
145 __u16 i_uid; /* Owner Uid */
146 __u32 i_size; /* 4: Size in bytes */
147 __u32 i_atime; /* Access time */
148 __u32 i_ctime; /* 12: Creation time */
149 __u32 i_mtime; /* Modification time */
150 __u32 i_dtime; /* 20: Deletion Time */
151 __u16 i_gid; /* Group Id */
152 __u16 i_links_count; /* 24: Links count */
153 __u32 i_blocks; /* Blocks count */
154 __u32 i_flags; /* 32: File flags */
155 union
156 {
157 struct
158 {
159 __u32 l_i_reserved1;
160 }
161 linux1;
162 struct
163 {
164 __u32 h_i_translator;
165 }
166 hurd1;
167 struct
168 {
169 __u32 m_i_reserved1;
170 }
171 masix1;
172 }
173 osd1; /* OS dependent 1 */
174 __u32 i_block[EXT2_N_BLOCKS]; /* 40: Pointers to blocks */
175 __u32 i_version; /* File version (for NFS) */
176 __u32 i_file_acl; /* File ACL */
177 __u32 i_dir_acl; /* Directory ACL */
178 __u32 i_faddr; /* Fragment address */
179 union
180 {
181 struct
182 {
183 __u8 l_i_frag; /* Fragment number */
184 __u8 l_i_fsize; /* Fragment size */
185 __u16 i_pad1;
186 __u32 l_i_reserved2[2];
187 }
188 linux2;
189 struct
190 {
191 __u8 h_i_frag; /* Fragment number */
192 __u8 h_i_fsize; /* Fragment size */
193 __u16 h_i_mode_high;
194 __u16 h_i_uid_high;
195 __u16 h_i_gid_high;
196 __u32 h_i_author;
197 }
198 hurd2;
199 struct
200 {
201 __u8 m_i_frag; /* Fragment number */
202 __u8 m_i_fsize; /* Fragment size */
203 __u16 m_pad1;
204 __u32 m_i_reserved2[2];
205 }
206 masix2;
207 }
208 osd2; /* OS dependent 2 */
209 };
210
211 /* linux/limits.h */
212 #define NAME_MAX 255 /* # chars in a file name */
213
214 /* linux/posix_type.h */
215 typedef long linux_off_t;
216
217 /* linux/ext2fs.h */
218 #define EXT2_NAME_LEN 255
219 struct ext2_dir_entry
220 {
221 __u32 inode; /* Inode number */
222 __u16 rec_len; /* Directory entry length */
223 __u8 name_len; /* Name length */
224 __u8 file_type;
225 char name[EXT2_NAME_LEN]; /* File name */
226 };
227
228 /* linux/ext2fs.h */
229 /*
230 * EXT2_DIR_PAD defines the directory entries boundaries
231 *
232 * NOTE: It must be a multiple of 4
233 */
234 #define EXT2_DIR_PAD 4
235 #define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
236 #define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
237 ~EXT2_DIR_ROUND)
238
239 /* linux/ext4_fs_extents.h */
240 /*
241 * This is the extent on-disk structure.
242 * It's used at the bottom of the tree.
243 */
244 struct ext4_extent {
245 __u32 ee_block; /* first logical block extent covers */
246 __u16 ee_len; /* number of blocks covered by extent */
247 __u16 ee_start_hi; /* high 16 bits of physical block */
248 __u32 ee_start; /* low 32 bits of physical block */
249 };
250
251 /*
252 * This is index on-disk structure.
253 * It's used at all the levels except the bottom.
254 */
255 struct ext4_extent_idx {
256 __u32 ei_block; /* index covers logical blocks from 'block' */
257 __u32 ei_leaf; /* pointer to the physical block of the next *
258 * level. leaf or next index could be there */
259 __u16 ei_leaf_hi; /* high 16 bits of physical block */
260 __u16 ei_unused;
261 };
262
263 /*
264 * Each block (leaves and indexes), even inode-stored has header.
265 */
266 struct ext4_extent_header {
267 __u16 eh_magic; /* probably will support different formats */
268 __u16 eh_entries; /* number of valid entries */
269 __u16 eh_max; /* capacity of store in entries */
270 __u16 eh_depth; /* has tree real underlying blocks? */
271 __u32 eh_generation; /* generation of the tree */
272 };
273
274 #define EXT4_EXT_MAGIC 0xf30a
275
276 /* ext2/super.c */
277 #define log2(n) grub_log2(n)
278
279 #define EXT2_SUPER_MAGIC 0xEF53 /* include/linux/ext2_fs.h */
280 #define EXT2_ROOT_INO 2 /* include/linux/ext2_fs.h */
281 #ifndef PATH_MAX
282 #define PATH_MAX 1024 /* include/linux/limits.h */
283 #endif
284 #define MAX_LINK_COUNT 5 /* number of symbolic links to follow */
285
286 /* made up, these are pointers into FSYS_BUF */
287 /* read once, always stays there: */
288 #define SUPERBLOCK \
289 ((struct ext2_super_block *)(FSYS_BUF))
290 #define GROUP_DESC \
291 ((struct ext2_group_desc *) \
292 ((char *)SUPERBLOCK + sizeof(struct ext2_super_block)))
293 #define INODE \
294 ((struct ext2_inode *)((caddr_t)GROUP_DESC + EXT2_BLOCK_SIZE(SUPERBLOCK)))
295 #define DATABLOCK1 \
296 ((char *)((caddr_t)INODE + sizeof(struct ext2_inode)))
297 #define DATABLOCK2 \
298 ((char *)((caddr_t)DATABLOCK1 + EXT2_BLOCK_SIZE(SUPERBLOCK)))
299
300 /* linux/ext2_fs.h */
301 #define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (__u32))
302 #define EXT2_ADDR_PER_BLOCK_BITS(s) (log2(EXT2_ADDR_PER_BLOCK(s)))
303
304 #define EXT2_INODE_SIZE(s) (SUPERBLOCK->s_inode_size)
305 #define EXT2_INODES_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s)/EXT2_INODE_SIZE(s))
306
307 /* linux/ext2_fs.h */
308 #define EXT2_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
309 /* kind of from ext2/super.c */
310 #define EXT2_BLOCK_SIZE(s) (1 << EXT2_BLOCK_SIZE_BITS(s))
311 /* linux/ext2fs.h */
312 #define EXT2_DESC_PER_BLOCK(s) \
313 (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
314 /* linux/stat.h */
315 #define S_IFMT 00170000
316 #define S_IFLNK 0120000
317 #define S_IFREG 0100000
318 #define S_IFDIR 0040000
319 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
320 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
321 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
322
323 /* check filesystem types and read superblock into memory buffer */
324 static int
ext2fs_mount(fsi_file_t * ffi,const char * options)325 ext2fs_mount (fsi_file_t *ffi, const char *options)
326 {
327 int retval = 1;
328
329 if (/*(((current_drive & 0x80) || (current_slice != 0))
330 && (current_slice != PC_SLICE_TYPE_EXT2FS)
331 && (current_slice != PC_SLICE_TYPE_LINUX_RAID)
332 && (! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_EXT2FS))
333 && (! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_OTHER)))
334 || part_length < (SBLOCK + (sizeof (struct ext2_super_block) / DEV_BSIZE))
335 || */ !devread (ffi, SBLOCK, 0, sizeof (struct ext2_super_block),
336 (char *) SUPERBLOCK)
337 || SUPERBLOCK->s_magic != EXT2_SUPER_MAGIC)
338 retval = 0;
339
340 return retval;
341 }
342
343 /* Takes a file system block number and reads it into BUFFER. */
344 static int
ext2_rdfsb(fsi_file_t * ffi,int fsblock,char * buffer)345 ext2_rdfsb (fsi_file_t *ffi, int fsblock, char *buffer)
346 {
347 #ifdef E2DEBUG
348 printf ("fsblock %d buffer %d\n", fsblock, buffer);
349 #endif /* E2DEBUG */
350 return devread (ffi, fsblock * (EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE), 0,
351 EXT2_BLOCK_SIZE (SUPERBLOCK), (char *) buffer);
352 }
353
354 /* Walk through extents index tree to find the good leaf */
355 static struct ext4_extent_header *
ext4_recurse_extent_index(fsi_file_t * ffi,struct ext4_extent_header * extent_block,int logical_block)356 ext4_recurse_extent_index(fsi_file_t *ffi, struct ext4_extent_header *extent_block, int logical_block)
357 {
358 int i;
359 struct ext4_extent_idx *index = (struct ext4_extent_idx *) (extent_block + 1);
360 if (extent_block->eh_magic != EXT4_EXT_MAGIC)
361 return NULL;
362 if (extent_block->eh_depth == 0)
363 return extent_block;
364 for (i = 0; i < extent_block->eh_entries; i++)
365 {
366 if (logical_block < index[i].ei_block)
367 break;
368 }
369 if (i == 0 || !ext2_rdfsb(ffi, index[i-1].ei_leaf, DATABLOCK1))
370 return NULL;
371 return (ext4_recurse_extent_index(ffi, (struct ext4_extent_header *) DATABLOCK1, logical_block));
372 }
373
374
375 /* from
376 ext2/inode.c:ext2_bmap()
377 */
378 /* Maps LOGICAL_BLOCK (the file offset divided by the blocksize) into
379 a physical block (the location in the file system) via an inode. */
380 static int
ext2fs_block_map(fsi_file_t * ffi,int logical_block)381 ext2fs_block_map (fsi_file_t *ffi, int logical_block)
382 {
383 #ifdef E2DEBUG
384 unsigned char *i;
385 for (i = (unsigned char *) INODE;
386 i < ((unsigned char *) INODE + sizeof (struct ext2_inode));
387 i++)
388 {
389 printf ("%c", "0123456789abcdef"[*i >> 4]);
390 printf ("%c", "0123456789abcdef"[*i % 16]);
391 if (!((i + 1 - (unsigned char *) INODE) % 16))
392 {
393 printf ("\n");
394 }
395 else
396 {
397 printf (" ");
398 }
399 }
400 printf ("logical block %d\n", logical_block);
401 #endif /* E2DEBUG */
402
403 if (!(INODE->i_flags & EXT4_EXTENTS_FL))
404 {
405 /* if it is directly pointed to by the inode, return that physical addr */
406 if (logical_block < EXT2_NDIR_BLOCKS)
407 {
408 #ifdef E2DEBUG
409 printf ("returning %d\n", (unsigned char *) (INODE->i_block[logical_block]));
410 printf ("returning %d\n", INODE->i_block[logical_block]);
411 #endif /* E2DEBUG */
412 return INODE->i_block[logical_block];
413 }
414 /* else */
415 logical_block -= EXT2_NDIR_BLOCKS;
416 /* try the indirect block */
417 if (logical_block < EXT2_ADDR_PER_BLOCK (SUPERBLOCK))
418 {
419 if (mapblock1 != 1 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_IND_BLOCK], DATABLOCK1))
420 {
421 errnum = ERR_FSYS_CORRUPT;
422 return -1;
423 }
424 mapblock1 = 1;
425 return ((__u32 *) DATABLOCK1)[logical_block];
426 }
427 /* else */
428 logical_block -= EXT2_ADDR_PER_BLOCK (SUPERBLOCK);
429 /* now try the double indirect block */
430 if (logical_block < (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2)))
431 {
432 int bnum;
433 if (mapblock1 != 2 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_DIND_BLOCK], DATABLOCK1))
434 {
435 errnum = ERR_FSYS_CORRUPT;
436 return -1;
437 }
438 mapblock1 = 2;
439 if ((bnum = (((__u32 *) DATABLOCK1)
440 [logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)]))
441 != mapblock2
442 && !ext2_rdfsb (ffi, bnum, DATABLOCK2))
443 {
444 errnum = ERR_FSYS_CORRUPT;
445 return -1;
446 }
447 mapblock2 = bnum;
448 return ((__u32 *) DATABLOCK2)
449 [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
450 }
451 /* else */
452 mapblock2 = -1;
453 logical_block -= (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2));
454 if (mapblock1 != 3
455 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_TIND_BLOCK], DATABLOCK1))
456 {
457 errnum = ERR_FSYS_CORRUPT;
458 return -1;
459 }
460 mapblock1 = 3;
461 if (!ext2_rdfsb (ffi, ((__u32 *) DATABLOCK1)
462 [logical_block >> (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)
463 * 2)],
464 DATABLOCK2))
465 {
466 errnum = ERR_FSYS_CORRUPT;
467 return -1;
468 }
469 if (!ext2_rdfsb (ffi, ((__u32 *) DATABLOCK2)
470 [(logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK))
471 & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)],
472 DATABLOCK2))
473 {
474 errnum = ERR_FSYS_CORRUPT;
475 return -1;
476 }
477
478 return ((__u32 *) DATABLOCK2)
479 [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
480 }
481 /* inode is in extents format */
482 else
483 {
484 int i;
485 struct ext4_extent_header *extent_hdr =
486 ext4_recurse_extent_index(ffi, (struct ext4_extent_header *) INODE->i_block, logical_block);
487 struct ext4_extent *extent = (struct ext4_extent *) (extent_hdr + 1);
488 if ( extent_hdr == NULL || extent_hdr->eh_magic != EXT4_EXT_MAGIC)
489 {
490 errnum = ERR_FSYS_CORRUPT;
491 return -1;
492 }
493 for (i = 0; i<extent_hdr->eh_entries; i++)
494 {
495 if (extent[i].ee_block <= logical_block && logical_block < extent[i].ee_block + extent[i].ee_len && !(extent[i].ee_len>>15))
496 return (logical_block - extent[i].ee_block + extent[i].ee_start);
497 }
498 /* We should not arrive here */
499
500 errnum = ERR_FSYS_CORRUPT;
501 return -1;
502 }
503 }
504
505 /* preconditions: all preconds of ext2fs_block_map */
506 static int
ext2fs_read(fsi_file_t * ffi,char * buf,int len)507 ext2fs_read (fsi_file_t *ffi, char *buf, int len)
508 {
509 int logical_block;
510 int offset;
511 int map;
512 int ret = 0;
513 int size = 0;
514
515 #ifdef E2DEBUG
516 static char hexdigit[] = "0123456789abcdef";
517 unsigned char *i;
518 for (i = (unsigned char *) INODE;
519 i < ((unsigned char *) INODE + sizeof (struct ext2_inode));
520 i++)
521 {
522 printf ("%c", hexdigit[*i >> 4]);
523 printf ("%c", hexdigit[*i % 16]);
524 if (!((i + 1 - (unsigned char *) INODE) % 16))
525 {
526 printf ("\n");
527 }
528 else
529 {
530 printf (" ");
531 }
532 }
533 #endif /* E2DEBUG */
534 while (len > 0)
535 {
536 /* find the (logical) block component of our location */
537 logical_block = filepos >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
538 offset = filepos & (EXT2_BLOCK_SIZE (SUPERBLOCK) - 1);
539 map = ext2fs_block_map (ffi, logical_block);
540 #ifdef E2DEBUG
541 printf ("map=%d\n", map);
542 #endif /* E2DEBUG */
543 if (map < 0)
544 break;
545
546 size = EXT2_BLOCK_SIZE (SUPERBLOCK);
547 size -= offset;
548 if (size > len)
549 size = len;
550
551 if (map == 0) {
552 memset ((char *) buf, 0, size);
553 } else {
554 disk_read_func = disk_read_hook;
555
556 devread (ffi, map * (EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE),
557 offset, size, buf);
558
559 disk_read_func = NULL;
560 }
561
562 buf += size;
563 len -= size;
564 filepos += size;
565 ret += size;
566 }
567
568 if (errnum)
569 ret = 0;
570
571 return ret;
572 }
573
574
575 /* Based on:
576 def_blk_fops points to
577 blkdev_open, which calls (I think):
578 sys_open()
579 do_open()
580 open_namei()
581 dir_namei() which accesses current->fs->root
582 fs->root was set during original mount:
583 (something)... which calls (I think):
584 ext2_read_super()
585 iget()
586 __iget()
587 read_inode()
588 ext2_read_inode()
589 uses desc_per_block_bits, which is set in ext2_read_super()
590 also uses group descriptors loaded during ext2_read_super()
591 lookup()
592 ext2_lookup()
593 ext2_find_entry()
594 ext2_getblk()
595
596 */
597
598 static inline
ext2_is_fast_symlink(fsi_file_t * ffi)599 int ext2_is_fast_symlink (fsi_file_t *ffi)
600 {
601 int ea_blocks;
602 ea_blocks = INODE->i_file_acl ? EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE : 0;
603 return INODE->i_blocks == ea_blocks;
604 }
605
606 /* preconditions: ext2fs_mount already executed, therefore supblk in buffer
607 * known as SUPERBLOCK
608 * returns: 0 if error, nonzero iff we were able to find the file successfully
609 * postconditions: on a nonzero return, buffer known as INODE contains the
610 * inode of the file we were trying to look up
611 * side effects: messes up GROUP_DESC buffer area
612 */
613 static int
ext2fs_dir(fsi_file_t * ffi,char * dirname)614 ext2fs_dir (fsi_file_t *ffi, char *dirname)
615 {
616 int current_ino = EXT2_ROOT_INO; /* start at the root */
617 int updir_ino = current_ino; /* the parent of the current directory */
618 int group_id; /* which group the inode is in */
619 int group_desc; /* fs pointer to that group */
620 int desc; /* index within that group */
621 int ino_blk; /* fs pointer of the inode's information */
622 int str_chk = 0; /* used to hold the results of a string compare */
623 struct ext2_group_desc *gdp;
624 struct ext2_inode *raw_inode; /* inode info corresponding to current_ino */
625
626 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
627 int link_count = 0;
628
629 char *rest;
630 char ch; /* temp char holder */
631
632 int off; /* offset within block of directory entry (off mod blocksize) */
633 int loc; /* location within a directory */
634 int blk; /* which data blk within dir entry (off div blocksize) */
635 int inodes_per_block; /* number of inodes in each block */
636 int inode_offset; /* inode offset in block */
637 long map; /* fs pointer of a particular block from dir entry */
638 struct ext2_dir_entry *dp; /* pointer to directory entry */
639 #ifdef E2DEBUG
640 unsigned char *i;
641 #endif /* E2DEBUG */
642
643 /* loop invariants:
644 current_ino = inode to lookup
645 dirname = pointer to filename component we are cur looking up within
646 the directory known pointed to by current_ino (if any)
647 */
648
649 while (1)
650 {
651 #ifdef E2DEBUG
652 printf ("inode %d\n", current_ino);
653 printf ("dirname=%s\n", dirname);
654 #endif /* E2DEBUG */
655
656 /* look up an inode */
657 group_id = (current_ino - 1) / (SUPERBLOCK->s_inodes_per_group);
658 group_desc = group_id >> log2 (EXT2_DESC_PER_BLOCK (SUPERBLOCK));
659 desc = group_id & (EXT2_DESC_PER_BLOCK (SUPERBLOCK) - 1);
660 #ifdef E2DEBUG
661 printf ("ipg=%d, dpb=%d\n", SUPERBLOCK->s_inodes_per_group,
662 EXT2_DESC_PER_BLOCK (SUPERBLOCK));
663 printf ("group_id=%d group_desc=%d desc=%d\n", group_id, group_desc, desc);
664 #endif /* E2DEBUG */
665 if (!ext2_rdfsb (ffi,
666 (WHICH_SUPER + group_desc + SUPERBLOCK->s_first_data_block),
667 (char *)GROUP_DESC))
668 {
669 return 0;
670 }
671 gdp = GROUP_DESC;
672 inodes_per_block = EXT2_BLOCK_SIZE (SUPERBLOCK) / EXT2_INODE_SIZE(SUPERBLOCK);
673 inode_offset = ((current_ino - 1) % (SUPERBLOCK->s_inodes_per_group));
674 ino_blk = gdp[desc].bg_inode_table + (inode_offset / inodes_per_block);
675 #ifdef E2DEBUG
676 printf ("inode table fsblock=%d\n", ino_blk);
677 #endif /* E2DEBUG */
678 if (!ext2_rdfsb (ffi, ino_blk, (char *)INODE))
679 {
680 return 0;
681 }
682
683 /* reset indirect blocks! */
684 mapblock2 = mapblock1 = -1;
685
686 raw_inode = (struct ext2_inode *)((char *)INODE +
687 ((current_ino - 1) & (EXT2_INODES_PER_BLOCK (SUPERBLOCK) - 1)) *
688 EXT2_INODE_SIZE (SUPERBLOCK));
689 #ifdef E2DEBUG
690 printf ("ipb=%d, sizeof(inode)=%d\n",
691 EXT2_INODES_PER_BLOCK (SUPERBLOCK), EXT2_INODE_SIZE (SUPERBLOCK));
692 printf ("inode=%x, raw_inode=%x\n", INODE, raw_inode);
693 printf ("offset into inode table block=%d\n", (int) raw_inode - (int) INODE);
694 for (i = (unsigned char *) INODE; i <= (unsigned char *) raw_inode;
695 i++)
696 {
697 printf ("%c", "0123456789abcdef"[*i >> 4]);
698 printf ("%c", "0123456789abcdef"[*i % 16]);
699 if (!((i + 1 - (unsigned char *) INODE) % 16))
700 {
701 printf ("\n");
702 }
703 else
704 {
705 printf (" ");
706 }
707 }
708 printf ("first word=%x\n", *((int *) raw_inode));
709 #endif /* E2DEBUG */
710
711 /* copy inode to fixed location */
712 memmove ((void *) INODE, (void *) raw_inode, sizeof (struct ext2_inode));
713
714 #ifdef E2DEBUG
715 printf ("first word=%x\n", *((int *) INODE));
716 #endif /* E2DEBUG */
717
718 /* If we've got a symbolic link, then chase it. */
719 if (S_ISLNK (INODE->i_mode))
720 {
721 int len;
722 if (++link_count > MAX_LINK_COUNT)
723 {
724 errnum = ERR_SYMLINK_LOOP;
725 return 0;
726 }
727
728 /* Find out how long our remaining name is. */
729 len = 0;
730 while (dirname[len] && !isspace ((uint8_t)dirname[len]))
731 len++;
732
733 /* Get the symlink size. */
734 filemax = (INODE->i_size);
735 if (filemax + len > sizeof (linkbuf) - 2)
736 {
737 errnum = ERR_FILELENGTH;
738 return 0;
739 }
740
741 if (len)
742 {
743 /* Copy the remaining name to the end of the symlink data.
744 Note that DIRNAME and LINKBUF may overlap! */
745 memmove (linkbuf + filemax, dirname, len);
746 }
747 linkbuf[filemax + len] = '\0';
748
749 /* Read the symlink data. */
750 if (! ext2_is_fast_symlink (ffi))
751 {
752 /* Read the necessary blocks, and reset the file pointer. */
753 len = ext2fs_read (ffi, linkbuf, filemax);
754 filepos = 0;
755 if (!len)
756 return 0;
757 }
758 else
759 {
760 /* Copy the data directly from the inode. */
761 len = filemax;
762 memmove (linkbuf, (char *) INODE->i_block, len);
763 }
764
765 #ifdef E2DEBUG
766 printf ("symlink=%s\n", linkbuf);
767 #endif
768
769 dirname = linkbuf;
770 if (*dirname == '/')
771 {
772 /* It's an absolute link, so look it up in root. */
773 current_ino = EXT2_ROOT_INO;
774 updir_ino = current_ino;
775 }
776 else
777 {
778 /* Relative, so look it up in our parent directory. */
779 current_ino = updir_ino;
780 }
781
782 /* Try again using the new name. */
783 continue;
784 }
785
786 /* if end of filename, INODE points to the file's inode */
787 if (!*dirname || isspace ((uint8_t)*dirname))
788 {
789 if (!S_ISREG (INODE->i_mode))
790 {
791 errnum = ERR_BAD_FILETYPE;
792 return 0;
793 }
794
795 filemax = (INODE->i_size);
796 return 1;
797 }
798
799 /* else we have to traverse a directory */
800 updir_ino = current_ino;
801
802 /* skip over slashes */
803 while (*dirname == '/')
804 dirname++;
805
806 /* if this isn't a directory of sufficient size to hold our file, abort */
807 if (!(INODE->i_size) || !S_ISDIR (INODE->i_mode))
808 {
809 errnum = ERR_BAD_FILETYPE;
810 return 0;
811 }
812
813 /* skip to next slash or end of filename (space) */
814 for (rest = dirname; (ch = *rest) && !isspace ((uint8_t)ch) && ch != '/';
815 rest++);
816
817 /* look through this directory and find the next filename component */
818 /* invariant: rest points to slash after the next filename component */
819 *rest = 0;
820 loc = 0;
821
822 do
823 {
824
825 #ifdef E2DEBUG
826 printf ("dirname=%s, rest=%s, loc=%d\n", dirname, rest, loc);
827 #endif /* E2DEBUG */
828
829 /* if our location/byte offset into the directory exceeds the size,
830 give up */
831 if (loc >= INODE->i_size)
832 {
833 if (print_possibilities < 0)
834 {
835 # if 0
836 putchar ('\n');
837 # endif
838 }
839 else
840 {
841 errnum = ERR_FILE_NOT_FOUND;
842 *rest = ch;
843 }
844 return (print_possibilities < 0);
845 }
846
847 /* else, find the (logical) block component of our location */
848 blk = loc >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
849
850 /* we know which logical block of the directory entry we are looking
851 for, now we have to translate that to the physical (fs) block on
852 the disk */
853 map = ext2fs_block_map (ffi, blk);
854 #ifdef E2DEBUG
855 printf ("fs block=%d\n", map);
856 #endif /* E2DEBUG */
857 mapblock2 = -1;
858 if ((map < 0) || !ext2_rdfsb (ffi, map, DATABLOCK2))
859 {
860 errnum = ERR_FSYS_CORRUPT;
861 *rest = ch;
862 return 0;
863 }
864 off = loc & (EXT2_BLOCK_SIZE (SUPERBLOCK) - 1);
865 dp = (struct ext2_dir_entry *) (DATABLOCK2 + off);
866 /* advance loc prematurely to next on-disk directory entry */
867 loc += dp->rec_len;
868
869 /* NOTE: ext2fs filenames are NOT null-terminated */
870
871 #ifdef E2DEBUG
872 printf ("directory entry ino=%d\n", dp->inode);
873 if (dp->inode)
874 printf ("entry=%s\n", dp->name);
875 #endif /* E2DEBUG */
876
877 if (dp->inode)
878 {
879 int saved_c = dp->name[dp->name_len];
880
881 dp->name[dp->name_len] = 0;
882 str_chk = substring (dirname, dp->name);
883
884 # ifndef STAGE1_5
885 if (print_possibilities && ch != '/'
886 && (!*dirname || str_chk <= 0))
887 {
888 if (print_possibilities > 0)
889 print_possibilities = -print_possibilities;
890 print_a_completion (dp->name);
891 }
892 # endif
893
894 dp->name[dp->name_len] = saved_c;
895 }
896
897 }
898 while (!dp->inode || (str_chk || (print_possibilities && ch != '/')));
899
900 current_ino = dp->inode;
901 *(dirname = rest) = ch;
902 }
903 /* never get here */
904 }
905
906 fsi_plugin_ops_t *
fsi_init_plugin(int version,fsi_plugin_t * fp,const char ** name)907 fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
908 {
909 static fsig_plugin_ops_t ops = {
910 FSIMAGE_PLUGIN_VERSION,
911 .fpo_mount = ext2fs_mount,
912 .fpo_dir = ext2fs_dir,
913 .fpo_read = ext2fs_read
914 };
915
916 *name = "ext2fs";
917 return (fsig_init(fp, &ops));
918 }
919