ia64/xen-unstable

view tools/libfsimage/ext2fs/fsys_ext2fs.c @ 18059:4b882c41c9b9

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