direct-io.hg

view tools/libfsimage/ext2fs/fsys_ext2fs.c @ 12511:60b60f75a221

[POWERPC][LIBFS] Fix build breakage in log2 assembly.
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author kfraser@localhost.localdomain
date Wed Nov 22 10:10:29 2006 +0000 (2006-11-22)
parents 825be74657c3
children 89ca591a2c21
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 __u32 s_reserved[235]; /* Padding to the end of the block */
81 };
83 struct ext2_group_desc
84 {
85 __u32 bg_block_bitmap; /* Blocks bitmap block */
86 __u32 bg_inode_bitmap; /* Inodes bitmap block */
87 __u32 bg_inode_table; /* Inodes table block */
88 __u16 bg_free_blocks_count; /* Free blocks count */
89 __u16 bg_free_inodes_count; /* Free inodes count */
90 __u16 bg_used_dirs_count; /* Directories count */
91 __u16 bg_pad;
92 __u32 bg_reserved[3];
93 };
95 struct ext2_inode
96 {
97 __u16 i_mode; /* File mode */
98 __u16 i_uid; /* Owner Uid */
99 __u32 i_size; /* 4: Size in bytes */
100 __u32 i_atime; /* Access time */
101 __u32 i_ctime; /* 12: Creation time */
102 __u32 i_mtime; /* Modification time */
103 __u32 i_dtime; /* 20: Deletion Time */
104 __u16 i_gid; /* Group Id */
105 __u16 i_links_count; /* 24: Links count */
106 __u32 i_blocks; /* Blocks count */
107 __u32 i_flags; /* 32: File flags */
108 union
109 {
110 struct
111 {
112 __u32 l_i_reserved1;
113 }
114 linux1;
115 struct
116 {
117 __u32 h_i_translator;
118 }
119 hurd1;
120 struct
121 {
122 __u32 m_i_reserved1;
123 }
124 masix1;
125 }
126 osd1; /* OS dependent 1 */
127 __u32 i_block[EXT2_N_BLOCKS]; /* 40: Pointers to blocks */
128 __u32 i_version; /* File version (for NFS) */
129 __u32 i_file_acl; /* File ACL */
130 __u32 i_dir_acl; /* Directory ACL */
131 __u32 i_faddr; /* Fragment address */
132 union
133 {
134 struct
135 {
136 __u8 l_i_frag; /* Fragment number */
137 __u8 l_i_fsize; /* Fragment size */
138 __u16 i_pad1;
139 __u32 l_i_reserved2[2];
140 }
141 linux2;
142 struct
143 {
144 __u8 h_i_frag; /* Fragment number */
145 __u8 h_i_fsize; /* Fragment size */
146 __u16 h_i_mode_high;
147 __u16 h_i_uid_high;
148 __u16 h_i_gid_high;
149 __u32 h_i_author;
150 }
151 hurd2;
152 struct
153 {
154 __u8 m_i_frag; /* Fragment number */
155 __u8 m_i_fsize; /* Fragment size */
156 __u16 m_pad1;
157 __u32 m_i_reserved2[2];
158 }
159 masix2;
160 }
161 osd2; /* OS dependent 2 */
162 };
164 /* linux/limits.h */
165 #define NAME_MAX 255 /* # chars in a file name */
167 /* linux/posix_type.h */
168 typedef long linux_off_t;
170 /* linux/ext2fs.h */
171 #define EXT2_NAME_LEN 255
172 struct ext2_dir_entry
173 {
174 __u32 inode; /* Inode number */
175 __u16 rec_len; /* Directory entry length */
176 __u8 name_len; /* Name length */
177 __u8 file_type;
178 char name[EXT2_NAME_LEN]; /* File name */
179 };
181 /* linux/ext2fs.h */
182 /*
183 * EXT2_DIR_PAD defines the directory entries boundaries
184 *
185 * NOTE: It must be a multiple of 4
186 */
187 #define EXT2_DIR_PAD 4
188 #define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
189 #define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
190 ~EXT2_DIR_ROUND)
193 /* ext2/super.c */
194 #define log2(n) ffz(~(n))
196 #define EXT2_SUPER_MAGIC 0xEF53 /* include/linux/ext2_fs.h */
197 #define EXT2_ROOT_INO 2 /* include/linux/ext2_fs.h */
198 #define PATH_MAX 1024 /* include/linux/limits.h */
199 #define MAX_LINK_COUNT 5 /* number of symbolic links to follow */
201 /* made up, these are pointers into FSYS_BUF */
202 /* read once, always stays there: */
203 #define SUPERBLOCK \
204 ((struct ext2_super_block *)(FSYS_BUF))
205 #define GROUP_DESC \
206 ((struct ext2_group_desc *) \
207 ((char *)SUPERBLOCK + sizeof(struct ext2_super_block)))
208 #define INODE \
209 ((struct ext2_inode *)((caddr_t)GROUP_DESC + EXT2_BLOCK_SIZE(SUPERBLOCK)))
210 #define DATABLOCK1 \
211 ((char *)((caddr_t)INODE + sizeof(struct ext2_inode)))
212 #define DATABLOCK2 \
213 ((char *)((caddr_t)DATABLOCK1 + EXT2_BLOCK_SIZE(SUPERBLOCK)))
215 /* linux/ext2_fs.h */
216 #define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (__u32))
217 #define EXT2_ADDR_PER_BLOCK_BITS(s) (log2(EXT2_ADDR_PER_BLOCK(s)))
219 /* linux/ext2_fs.h */
220 #define EXT2_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
221 /* kind of from ext2/super.c */
222 #define EXT2_BLOCK_SIZE(s) (1 << EXT2_BLOCK_SIZE_BITS(s))
223 /* linux/ext2fs.h */
224 #define EXT2_DESC_PER_BLOCK(s) \
225 (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_group_desc))
226 /* linux/stat.h */
227 #define S_IFMT 00170000
228 #define S_IFLNK 0120000
229 #define S_IFREG 0100000
230 #define S_IFDIR 0040000
231 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
232 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
233 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
235 #if defined(__i386__) || defined(__x86_64__)
236 /* include/asm-i386/bitops.h */
237 /*
238 * ffz = Find First Zero in word. Undefined if no zero exists,
239 * so code should check against ~0UL first..
240 */
241 #ifdef __amd64
242 #define BSF "bsfq"
243 #else
244 #define BSF "bsfl"
245 #endif
246 static __inline__ unsigned long
247 ffz (unsigned long word)
248 {
249 __asm__ (BSF " %1,%0"
250 : "=r" (word)
251 : "r" (~word));
252 return word;
253 }
255 #elif defined(__ia64__)
257 typedef unsigned long __u64;
259 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
260 # define ia64_popcnt(x) __builtin_popcountl(x)
261 #else
262 # define ia64_popcnt(x) \
263 ({ \
264 __u64 ia64_intri_res; \
265 asm ("popcnt %0=%1" : "=r" (ia64_intri_res) : "r" (x)); \
266 ia64_intri_res; \
267 })
268 #endif
270 static __inline__ unsigned long
271 ffz (unsigned long word)
272 {
273 unsigned long result;
275 result = ia64_popcnt(word & (~word - 1));
276 return result;
277 }
279 #elif defined(__powerpc__)
281 #ifdef __powerpc64__
282 #define PPC_CNTLZL "cntlzd"
283 #else
284 #define PPC_CNTLZL "cntlzw"
285 #endif
286 #define BITS_PER_LONG (sizeof(long) * 8)
288 static __inline__ int
289 __ilog2(unsigned long x)
290 {
291 int lz;
293 asm (PPC_CNTLZL " %0,%1" : "=r" (lz) : "r" (x));
294 return BITS_PER_LONG - 1 - lz;
295 }
297 static __inline__ unsigned long
298 ffz (unsigned long word)
299 {
300 if ((word = ~word) == 0)
301 return BITS_PER_LONG;
302 return __ilog2(word & -word);
303 }
305 #else /* Unoptimized */
307 static __inline__ unsigned long
308 ffz (unsigned long word)
309 {
310 unsigned long result;
312 result = 0;
313 while(word & 1)
314 {
315 result++;
316 word >>= 1;
317 }
318 return result;
319 }
320 #endif
322 /* check filesystem types and read superblock into memory buffer */
323 int
324 ext2fs_mount (fsi_file_t *ffi)
325 {
326 int retval = 1;
328 if (/*(((current_drive & 0x80) || (current_slice != 0))
329 && (current_slice != PC_SLICE_TYPE_EXT2FS)
330 && (current_slice != PC_SLICE_TYPE_LINUX_RAID)
331 && (! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_EXT2FS))
332 && (! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_OTHER)))
333 || part_length < (SBLOCK + (sizeof (struct ext2_super_block) / DEV_BSIZE))
334 || */ !devread (ffi, SBLOCK, 0, sizeof (struct ext2_super_block),
335 (char *) SUPERBLOCK)
336 || SUPERBLOCK->s_magic != EXT2_SUPER_MAGIC)
337 retval = 0;
339 return retval;
340 }
342 /* Takes a file system block number and reads it into BUFFER. */
343 static int
344 ext2_rdfsb (fsi_file_t *ffi, int fsblock, char *buffer)
345 {
346 #ifdef E2DEBUG
347 printf ("fsblock %d buffer %d\n", fsblock, buffer);
348 #endif /* E2DEBUG */
349 return devread (ffi, fsblock * (EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE), 0,
350 EXT2_BLOCK_SIZE (SUPERBLOCK), (char *) buffer);
351 }
353 /* from
354 ext2/inode.c:ext2_bmap()
355 */
356 /* Maps LOGICAL_BLOCK (the file offset divided by the blocksize) into
357 a physical block (the location in the file system) via an inode. */
358 static int
359 ext2fs_block_map (fsi_file_t *ffi, int logical_block)
360 {
362 #ifdef E2DEBUG
363 unsigned char *i;
364 for (i = (unsigned char *) INODE;
365 i < ((unsigned char *) INODE + sizeof (struct ext2_inode));
366 i++)
367 {
368 printf ("%c", "0123456789abcdef"[*i >> 4]);
369 printf ("%c", "0123456789abcdef"[*i % 16]);
370 if (!((i + 1 - (unsigned char *) INODE) % 16))
371 {
372 printf ("\n");
373 }
374 else
375 {
376 printf (" ");
377 }
378 }
379 printf ("logical block %d\n", logical_block);
380 #endif /* E2DEBUG */
382 /* if it is directly pointed to by the inode, return that physical addr */
383 if (logical_block < EXT2_NDIR_BLOCKS)
384 {
385 #ifdef E2DEBUG
386 printf ("returning %d\n", (unsigned char *) (INODE->i_block[logical_block]));
387 printf ("returning %d\n", INODE->i_block[logical_block]);
388 #endif /* E2DEBUG */
389 return INODE->i_block[logical_block];
390 }
391 /* else */
392 logical_block -= EXT2_NDIR_BLOCKS;
393 /* try the indirect block */
394 if (logical_block < EXT2_ADDR_PER_BLOCK (SUPERBLOCK))
395 {
396 if (mapblock1 != 1
397 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_IND_BLOCK], DATABLOCK1))
398 {
399 errnum = ERR_FSYS_CORRUPT;
400 return -1;
401 }
402 mapblock1 = 1;
403 return ((__u32 *) DATABLOCK1)[logical_block];
404 }
405 /* else */
406 logical_block -= EXT2_ADDR_PER_BLOCK (SUPERBLOCK);
407 /* now try the double indirect block */
408 if (logical_block < (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2)))
409 {
410 int bnum;
411 if (mapblock1 != 2
412 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_DIND_BLOCK], DATABLOCK1))
413 {
414 errnum = ERR_FSYS_CORRUPT;
415 return -1;
416 }
417 mapblock1 = 2;
418 if ((bnum = (((__u32 *) DATABLOCK1)
419 [logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)]))
420 != mapblock2
421 && !ext2_rdfsb (ffi, bnum, DATABLOCK2))
422 {
423 errnum = ERR_FSYS_CORRUPT;
424 return -1;
425 }
426 mapblock2 = bnum;
427 return ((__u32 *) DATABLOCK2)
428 [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
429 }
430 /* else */
431 mapblock2 = -1;
432 logical_block -= (1 << (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK) * 2));
433 if (mapblock1 != 3
434 && !ext2_rdfsb (ffi, INODE->i_block[EXT2_TIND_BLOCK], DATABLOCK1))
435 {
436 errnum = ERR_FSYS_CORRUPT;
437 return -1;
438 }
439 mapblock1 = 3;
440 if (!ext2_rdfsb (ffi, ((__u32 *) DATABLOCK1)
441 [logical_block >> (EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK)
442 * 2)],
443 DATABLOCK2))
444 {
445 errnum = ERR_FSYS_CORRUPT;
446 return -1;
447 }
448 if (!ext2_rdfsb (ffi, ((__u32 *) DATABLOCK2)
449 [(logical_block >> EXT2_ADDR_PER_BLOCK_BITS (SUPERBLOCK))
450 & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)],
451 DATABLOCK2))
452 {
453 errnum = ERR_FSYS_CORRUPT;
454 return -1;
455 }
456 return ((__u32 *) DATABLOCK2)
457 [logical_block & (EXT2_ADDR_PER_BLOCK (SUPERBLOCK) - 1)];
458 }
460 /* preconditions: all preconds of ext2fs_block_map */
461 int
462 ext2fs_read (fsi_file_t *ffi, char *buf, int len)
463 {
464 int logical_block;
465 int offset;
466 int map;
467 int ret = 0;
468 int size = 0;
470 #ifdef E2DEBUG
471 static char hexdigit[] = "0123456789abcdef";
472 unsigned char *i;
473 for (i = (unsigned char *) INODE;
474 i < ((unsigned char *) INODE + sizeof (struct ext2_inode));
475 i++)
476 {
477 printf ("%c", hexdigit[*i >> 4]);
478 printf ("%c", hexdigit[*i % 16]);
479 if (!((i + 1 - (unsigned char *) INODE) % 16))
480 {
481 printf ("\n");
482 }
483 else
484 {
485 printf (" ");
486 }
487 }
488 #endif /* E2DEBUG */
489 while (len > 0)
490 {
491 /* find the (logical) block component of our location */
492 logical_block = filepos >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
493 offset = filepos & (EXT2_BLOCK_SIZE (SUPERBLOCK) - 1);
494 map = ext2fs_block_map (ffi, logical_block);
495 #ifdef E2DEBUG
496 printf ("map=%d\n", map);
497 #endif /* E2DEBUG */
498 if (map < 0)
499 break;
501 size = EXT2_BLOCK_SIZE (SUPERBLOCK);
502 size -= offset;
503 if (size > len)
504 size = len;
506 if (map == 0) {
507 memset ((char *) buf, 0, size);
508 } else {
509 disk_read_func = disk_read_hook;
511 devread (ffi, map * (EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE),
512 offset, size, buf);
514 disk_read_func = NULL;
515 }
517 buf += size;
518 len -= size;
519 filepos += size;
520 ret += size;
521 }
523 if (errnum)
524 ret = 0;
526 return ret;
527 }
530 /* Based on:
531 def_blk_fops points to
532 blkdev_open, which calls (I think):
533 sys_open()
534 do_open()
535 open_namei()
536 dir_namei() which accesses current->fs->root
537 fs->root was set during original mount:
538 (something)... which calls (I think):
539 ext2_read_super()
540 iget()
541 __iget()
542 read_inode()
543 ext2_read_inode()
544 uses desc_per_block_bits, which is set in ext2_read_super()
545 also uses group descriptors loaded during ext2_read_super()
546 lookup()
547 ext2_lookup()
548 ext2_find_entry()
549 ext2_getblk()
551 */
553 static inline
554 int ext2_is_fast_symlink (fsi_file_t *ffi)
555 {
556 int ea_blocks;
557 ea_blocks = INODE->i_file_acl ? EXT2_BLOCK_SIZE (SUPERBLOCK) / DEV_BSIZE : 0;
558 return INODE->i_blocks == ea_blocks;
559 }
561 /* preconditions: ext2fs_mount already executed, therefore supblk in buffer
562 * known as SUPERBLOCK
563 * returns: 0 if error, nonzero iff we were able to find the file successfully
564 * postconditions: on a nonzero return, buffer known as INODE contains the
565 * inode of the file we were trying to look up
566 * side effects: messes up GROUP_DESC buffer area
567 */
568 int
569 ext2fs_dir (fsi_file_t *ffi, char *dirname)
570 {
571 int current_ino = EXT2_ROOT_INO; /* start at the root */
572 int updir_ino = current_ino; /* the parent of the current directory */
573 int group_id; /* which group the inode is in */
574 int group_desc; /* fs pointer to that group */
575 int desc; /* index within that group */
576 int ino_blk; /* fs pointer of the inode's information */
577 int str_chk = 0; /* used to hold the results of a string compare */
578 struct ext2_group_desc *gdp;
579 struct ext2_inode *raw_inode; /* inode info corresponding to current_ino */
581 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
582 int link_count = 0;
584 char *rest;
585 char ch; /* temp char holder */
587 int off; /* offset within block of directory entry (off mod blocksize) */
588 int loc; /* location within a directory */
589 int blk; /* which data blk within dir entry (off div blocksize) */
590 long map; /* fs pointer of a particular block from dir entry */
591 struct ext2_dir_entry *dp; /* pointer to directory entry */
592 #ifdef E2DEBUG
593 unsigned char *i;
594 #endif /* E2DEBUG */
596 /* loop invariants:
597 current_ino = inode to lookup
598 dirname = pointer to filename component we are cur looking up within
599 the directory known pointed to by current_ino (if any)
600 */
602 while (1)
603 {
604 #ifdef E2DEBUG
605 printf ("inode %d\n", current_ino);
606 printf ("dirname=%s\n", dirname);
607 #endif /* E2DEBUG */
609 /* look up an inode */
610 group_id = (current_ino - 1) / (SUPERBLOCK->s_inodes_per_group);
611 group_desc = group_id >> log2 (EXT2_DESC_PER_BLOCK (SUPERBLOCK));
612 desc = group_id & (EXT2_DESC_PER_BLOCK (SUPERBLOCK) - 1);
613 #ifdef E2DEBUG
614 printf ("ipg=%d, dpb=%d\n", SUPERBLOCK->s_inodes_per_group,
615 EXT2_DESC_PER_BLOCK (SUPERBLOCK));
616 printf ("group_id=%d group_desc=%d desc=%d\n", group_id, group_desc, desc);
617 #endif /* E2DEBUG */
618 if (!ext2_rdfsb (ffi,
619 (WHICH_SUPER + group_desc + SUPERBLOCK->s_first_data_block),
620 (char *)GROUP_DESC))
621 {
622 return 0;
623 }
624 gdp = GROUP_DESC;
625 ino_blk = gdp[desc].bg_inode_table +
626 (((current_ino - 1) % (SUPERBLOCK->s_inodes_per_group))
627 >> log2 (EXT2_BLOCK_SIZE (SUPERBLOCK) / sizeof (struct ext2_inode)));
628 #ifdef E2DEBUG
629 printf ("inode table fsblock=%d\n", ino_blk);
630 #endif /* E2DEBUG */
631 if (!ext2_rdfsb (ffi, ino_blk, (char *)INODE))
632 {
633 return 0;
634 }
636 /* reset indirect blocks! */
637 mapblock2 = mapblock1 = -1;
639 raw_inode = INODE +
640 ((current_ino - 1)
641 & (EXT2_BLOCK_SIZE (SUPERBLOCK) / sizeof (struct ext2_inode) - 1));
642 #ifdef E2DEBUG
643 printf ("ipb=%d, sizeof(inode)=%d\n",
644 (EXT2_BLOCK_SIZE (SUPERBLOCK) / sizeof (struct ext2_inode)),
645 sizeof (struct ext2_inode));
646 printf ("inode=%x, raw_inode=%x\n", INODE, raw_inode);
647 printf ("offset into inode table block=%d\n", (int) raw_inode - (int) INODE);
648 for (i = (unsigned char *) INODE; i <= (unsigned char *) raw_inode;
649 i++)
650 {
651 printf ("%c", "0123456789abcdef"[*i >> 4]);
652 printf ("%c", "0123456789abcdef"[*i % 16]);
653 if (!((i + 1 - (unsigned char *) INODE) % 16))
654 {
655 printf ("\n");
656 }
657 else
658 {
659 printf (" ");
660 }
661 }
662 printf ("first word=%x\n", *((int *) raw_inode));
663 #endif /* E2DEBUG */
665 /* copy inode to fixed location */
666 memmove ((void *) INODE, (void *) raw_inode, sizeof (struct ext2_inode));
668 #ifdef E2DEBUG
669 printf ("first word=%x\n", *((int *) INODE));
670 #endif /* E2DEBUG */
672 /* If we've got a symbolic link, then chase it. */
673 if (S_ISLNK (INODE->i_mode))
674 {
675 int len;
676 if (++link_count > MAX_LINK_COUNT)
677 {
678 errnum = ERR_SYMLINK_LOOP;
679 return 0;
680 }
682 /* Find out how long our remaining name is. */
683 len = 0;
684 while (dirname[len] && !isspace (dirname[len]))
685 len++;
687 /* Get the symlink size. */
688 filemax = (INODE->i_size);
689 if (filemax + len > sizeof (linkbuf) - 2)
690 {
691 errnum = ERR_FILELENGTH;
692 return 0;
693 }
695 if (len)
696 {
697 /* Copy the remaining name to the end of the symlink data.
698 Note that DIRNAME and LINKBUF may overlap! */
699 memmove (linkbuf + filemax, dirname, len);
700 }
701 linkbuf[filemax + len] = '\0';
703 /* Read the symlink data. */
704 if (! ext2_is_fast_symlink (ffi))
705 {
706 /* Read the necessary blocks, and reset the file pointer. */
707 len = ext2fs_read (ffi, linkbuf, filemax);
708 filepos = 0;
709 if (!len)
710 return 0;
711 }
712 else
713 {
714 /* Copy the data directly from the inode. */
715 len = filemax;
716 memmove (linkbuf, (char *) INODE->i_block, len);
717 }
719 #ifdef E2DEBUG
720 printf ("symlink=%s\n", linkbuf);
721 #endif
723 dirname = linkbuf;
724 if (*dirname == '/')
725 {
726 /* It's an absolute link, so look it up in root. */
727 current_ino = EXT2_ROOT_INO;
728 updir_ino = current_ino;
729 }
730 else
731 {
732 /* Relative, so look it up in our parent directory. */
733 current_ino = updir_ino;
734 }
736 /* Try again using the new name. */
737 continue;
738 }
740 /* if end of filename, INODE points to the file's inode */
741 if (!*dirname || isspace (*dirname))
742 {
743 if (!S_ISREG (INODE->i_mode))
744 {
745 errnum = ERR_BAD_FILETYPE;
746 return 0;
747 }
749 filemax = (INODE->i_size);
750 return 1;
751 }
753 /* else we have to traverse a directory */
754 updir_ino = current_ino;
756 /* skip over slashes */
757 while (*dirname == '/')
758 dirname++;
760 /* if this isn't a directory of sufficient size to hold our file, abort */
761 if (!(INODE->i_size) || !S_ISDIR (INODE->i_mode))
762 {
763 errnum = ERR_BAD_FILETYPE;
764 return 0;
765 }
767 /* skip to next slash or end of filename (space) */
768 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/';
769 rest++);
771 /* look through this directory and find the next filename component */
772 /* invariant: rest points to slash after the next filename component */
773 *rest = 0;
774 loc = 0;
776 do
777 {
779 #ifdef E2DEBUG
780 printf ("dirname=%s, rest=%s, loc=%d\n", dirname, rest, loc);
781 #endif /* E2DEBUG */
783 /* if our location/byte offset into the directory exceeds the size,
784 give up */
785 if (loc >= INODE->i_size)
786 {
787 if (print_possibilities < 0)
788 {
789 # if 0
790 putchar ('\n');
791 # endif
792 }
793 else
794 {
795 errnum = ERR_FILE_NOT_FOUND;
796 *rest = ch;
797 }
798 return (print_possibilities < 0);
799 }
801 /* else, find the (logical) block component of our location */
802 blk = loc >> EXT2_BLOCK_SIZE_BITS (SUPERBLOCK);
804 /* we know which logical block of the directory entry we are looking
805 for, now we have to translate that to the physical (fs) block on
806 the disk */
807 map = ext2fs_block_map (ffi, blk);
808 #ifdef E2DEBUG
809 printf ("fs block=%d\n", map);
810 #endif /* E2DEBUG */
811 mapblock2 = -1;
812 if ((map < 0) || !ext2_rdfsb (ffi, map, DATABLOCK2))
813 {
814 errnum = ERR_FSYS_CORRUPT;
815 *rest = ch;
816 return 0;
817 }
818 off = loc & (EXT2_BLOCK_SIZE (SUPERBLOCK) - 1);
819 dp = (struct ext2_dir_entry *) (DATABLOCK2 + off);
820 /* advance loc prematurely to next on-disk directory entry */
821 loc += dp->rec_len;
823 /* NOTE: ext2fs filenames are NOT null-terminated */
825 #ifdef E2DEBUG
826 printf ("directory entry ino=%d\n", dp->inode);
827 if (dp->inode)
828 printf ("entry=%s\n", dp->name);
829 #endif /* E2DEBUG */
831 if (dp->inode)
832 {
833 int saved_c = dp->name[dp->name_len];
835 dp->name[dp->name_len] = 0;
836 str_chk = substring (dirname, dp->name);
838 # ifndef STAGE1_5
839 if (print_possibilities && ch != '/'
840 && (!*dirname || str_chk <= 0))
841 {
842 if (print_possibilities > 0)
843 print_possibilities = -print_possibilities;
844 print_a_completion (dp->name);
845 }
846 # endif
848 dp->name[dp->name_len] = saved_c;
849 }
851 }
852 while (!dp->inode || (str_chk || (print_possibilities && ch != '/')));
854 current_ino = dp->inode;
855 *(dirname = rest) = ch;
856 }
857 /* never get here */
858 }
860 fsi_plugin_ops_t *
861 fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
862 {
863 static fsig_plugin_ops_t ops = {
864 FSIMAGE_PLUGIN_VERSION,
865 .fpo_mount = ext2fs_mount,
866 .fpo_dir = ext2fs_dir,
867 .fpo_read = ext2fs_read
868 };
870 *name = "ext2fs";
871 return (fsig_init(fp, &ops));
872 }