Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/kinit/fstype/fstype.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1122 - (hide annotations) (download)
Wed Aug 18 21:11:40 2010 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 13462 byte(s)
-updated to klibc-1.5.19
1 niro 532 /*
2     * by rmk
3     *
4     * Detect filesystem type (on stdin) and output strings for two
5     * environment variables:
6     * FSTYPE - filesystem type
7     * FSSIZE - filesystem size (if known)
8     *
9     * We currently detect the filesystems listed below in the struct
10     * "imagetype images" (in the order they are listed).
11     */
12    
13     #include <sys/types.h>
14     #include <stdio.h>
15 niro 815 #include <ctype.h>
16 niro 532 #include <string.h>
17     #include <unistd.h>
18     #include <fcntl.h>
19     #include <endian.h>
20     #include <netinet/in.h>
21 niro 815 #include <sys/utsname.h>
22 niro 532 #include <sys/vfs.h>
23    
24     #define cpu_to_be32(x) __cpu_to_be32(x) /* Needed by romfs_fs.h */
25    
26 niro 1122 #include "btrfs.h"
27 niro 532 #include "cramfs_fs.h"
28     #include "ext2_fs.h"
29     #include "ext3_fs.h"
30 niro 1122 #include "gfs2_fs.h"
31     #include "iso9660_sb.h"
32 niro 532 #include "luks_fs.h"
33     #include "lvm2_sb.h"
34 niro 1122 #include "minix_fs.h"
35     #include "nilfs_fs.h"
36     #include "ocfs2_fs.h"
37     #include "romfs_fs.h"
38 niro 815 #include "squashfs_fs.h"
39 niro 1122 #include "xfs_sb.h"
40 niro 532
41     /*
42     * Slightly cleaned up version of jfs_superblock to
43     * avoid pulling in other kernel header files.
44     */
45     #include "jfs_superblock.h"
46    
47     /*
48     * reiserfs_fs.h is too sick to include directly.
49     * Use a cleaned up version.
50     */
51     #include "reiserfs_fs.h"
52 niro 815 #include "reiser4_fs.h"
53 niro 532
54     #include "fstype.h"
55    
56     #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
57    
58     #define BLOCK_SIZE 1024
59    
60     /* Swap needs the definition of block size */
61     #include "swap_fs.h"
62    
63     static int gzip_image(const void *buf, unsigned long long *bytes)
64     {
65     const unsigned char *p = buf;
66    
67     if (p[0] == 037 && (p[1] == 0213 || p[1] == 0236)) {
68     /* The length of a gzip stream can only be determined
69     by processing the whole stream */
70     *bytes = 0ULL;
71     return 1;
72     }
73     return 0;
74     }
75    
76     static int cramfs_image(const void *buf, unsigned long long *bytes)
77     {
78     const struct cramfs_super *sb = (const struct cramfs_super *)buf;
79    
80     if (sb->magic == CRAMFS_MAGIC) {
81     if (sb->flags & CRAMFS_FLAG_FSID_VERSION_2)
82     *bytes = (unsigned long long)sb->fsid.blocks << 10;
83     else
84     *bytes = 0;
85     return 1;
86     }
87     return 0;
88     }
89    
90     static int romfs_image(const void *buf, unsigned long long *bytes)
91     {
92     const struct romfs_super_block *sb =
93     (const struct romfs_super_block *)buf;
94    
95     if (sb->word0 == ROMSB_WORD0 && sb->word1 == ROMSB_WORD1) {
96     *bytes = __be32_to_cpu(sb->size);
97     return 1;
98     }
99     return 0;
100     }
101    
102     static int minix_image(const void *buf, unsigned long long *bytes)
103     {
104     const struct minix_super_block *sb =
105     (const struct minix_super_block *)buf;
106    
107     if (sb->s_magic == MINIX_SUPER_MAGIC ||
108     sb->s_magic == MINIX_SUPER_MAGIC2) {
109     *bytes = (unsigned long long)sb->s_nzones
110     << (sb->s_log_zone_size + 10);
111     return 1;
112     }
113     return 0;
114     }
115    
116 niro 815 /*
117     * Check to see if a filesystem is in /proc/filesystems.
118     * Returns 1 if found, 0 if not
119     */
120     static int fs_proc_check(const char *fs_name)
121     {
122     FILE *f;
123     char buf[80], *cp, *t;
124    
125     f = fopen("/proc/filesystems", "r");
126     if (!f)
127     return (0);
128     while (fgets(buf, sizeof(buf), f)) {
129     cp = buf;
130     if (!isspace(*cp)) {
131     while (*cp && !isspace(*cp))
132     cp++;
133     }
134     while (*cp && isspace(*cp))
135     cp++;
136     if ((t = strchr(cp, '\n')) != NULL)
137     *t = 0;
138     if ((t = strchr(cp, '\t')) != NULL)
139     *t = 0;
140     if ((t = strchr(cp, ' ')) != NULL)
141     *t = 0;
142     if (!strcmp(fs_name, cp)) {
143     fclose(f);
144     return (1);
145     }
146     }
147     fclose(f);
148     return (0);
149     }
150    
151     /*
152     * Check to see if a filesystem is available as a module
153     * Returns 1 if found, 0 if not
154     */
155     static int check_for_modules(const char *fs_name)
156     {
157     struct utsname uts;
158     FILE *f;
159     char buf[1024], *cp, *t;
160     int i;
161    
162     if (uname(&uts))
163     return (0);
164     snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
165    
166     f = fopen(buf, "r");
167     if (!f)
168     return (0);
169     while (fgets(buf, sizeof(buf), f)) {
170     if ((cp = strchr(buf, ':')) != NULL)
171     *cp = 0;
172     else
173     continue;
174     if ((cp = strrchr(buf, '/')) != NULL)
175     cp++;
176     i = strlen(cp);
177     if (i > 3) {
178     t = cp + i - 3;
179     if (!strcmp(t, ".ko"))
180     *t = 0;
181     }
182     if (!strcmp(cp, fs_name)) {
183     fclose(f);
184     return (1);
185     }
186     }
187     fclose(f);
188     return (0);
189     }
190    
191     static int base_ext4_image(const void *buf, unsigned long long *bytes,
192     int *test_fs)
193     {
194     const struct ext3_super_block *sb =
195     (const struct ext3_super_block *)buf;
196    
197     if (sb->s_magic != __cpu_to_le16(EXT2_SUPER_MAGIC))
198     return 0;
199    
200     /* There is at least one feature not supported by ext3 */
201     if ((sb->s_feature_incompat
202     & __cpu_to_le32(EXT3_FEATURE_INCOMPAT_UNSUPPORTED)) ||
203     (sb->s_feature_ro_compat
204     & __cpu_to_le32(EXT3_FEATURE_RO_COMPAT_UNSUPPORTED))) {
205     *bytes = (unsigned long long)__le32_to_cpu(sb->s_blocks_count)
206     << (10 + __le32_to_cpu(sb->s_log_block_size));
207     *test_fs = (sb->s_flags &
208     __cpu_to_le32(EXT2_FLAGS_TEST_FILESYS)) != 0;
209     return 1;
210     }
211     return 0;
212     }
213    
214     static int ext4_image(const void *buf, unsigned long long *bytes)
215     {
216     int ret, test_fs, ext4dev_present, ext4_present;
217    
218     ret = base_ext4_image(buf, bytes, &test_fs);
219     if (ret == 0)
220     return 0;
221     ext4dev_present = (fs_proc_check("ext4dev") ||
222     check_for_modules("ext4dev"));
223     ext4_present = (fs_proc_check("ext4") || check_for_modules("ext4"));
224     if ((test_fs || !ext4_present) && ext4dev_present)
225     return 0;
226     return 1;
227     }
228    
229     static int ext4dev_image(const void *buf, unsigned long long *bytes)
230     {
231     int ret, test_fs, ext4dev_present, ext4_present;
232    
233     ret = base_ext4_image(buf, bytes, &test_fs);
234     if (ret == 0)
235     return 0;
236     ext4dev_present = (fs_proc_check("ext4dev") ||
237     check_for_modules("ext4dev"));
238     ext4_present = (fs_proc_check("ext4") || check_for_modules("ext4"));
239     if ((!test_fs || !ext4dev_present) && ext4_present)
240     return 0;
241     return 1;
242     }
243    
244 niro 532 static int ext3_image(const void *buf, unsigned long long *bytes)
245     {
246     const struct ext3_super_block *sb =
247     (const struct ext3_super_block *)buf;
248    
249     if (sb->s_magic == __cpu_to_le16(EXT2_SUPER_MAGIC) &&
250     sb->
251     s_feature_compat & __cpu_to_le32(EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
252     *bytes = (unsigned long long)__le32_to_cpu(sb->s_blocks_count)
253     << (10 + __le32_to_cpu(sb->s_log_block_size));
254     return 1;
255     }
256     return 0;
257     }
258    
259     static int ext2_image(const void *buf, unsigned long long *bytes)
260     {
261     const struct ext2_super_block *sb =
262     (const struct ext2_super_block *)buf;
263    
264     if (sb->s_magic == __cpu_to_le16(EXT2_SUPER_MAGIC)) {
265     *bytes = (unsigned long long)__le32_to_cpu(sb->s_blocks_count)
266     << (10 + __le32_to_cpu(sb->s_log_block_size));
267     return 1;
268     }
269     return 0;
270     }
271    
272     static int reiserfs_image(const void *buf, unsigned long long *bytes)
273     {
274     const struct reiserfs_super_block *sb =
275     (const struct reiserfs_super_block *)buf;
276    
277     if (memcmp(REISERFS_MAGIC(sb), REISERFS_SUPER_MAGIC_STRING,
278     sizeof(REISERFS_SUPER_MAGIC_STRING) - 1) == 0 ||
279     memcmp(REISERFS_MAGIC(sb), REISER2FS_SUPER_MAGIC_STRING,
280     sizeof(REISER2FS_SUPER_MAGIC_STRING) - 1) == 0 ||
281     memcmp(REISERFS_MAGIC(sb), REISER2FS_JR_SUPER_MAGIC_STRING,
282     sizeof(REISER2FS_JR_SUPER_MAGIC_STRING) - 1) == 0) {
283     *bytes = (unsigned long long)REISERFS_BLOCK_COUNT(sb) *
284     REISERFS_BLOCKSIZE(sb);
285     return 1;
286     }
287     return 0;
288     }
289    
290 niro 815 static int reiser4_image(const void *buf, unsigned long long *bytes)
291     {
292     const struct reiser4_master_sb *sb =
293     (const struct reiser4_master_sb *)buf;
294    
295     if (memcmp(sb->ms_magic, REISER4_SUPER_MAGIC_STRING,
296     sizeof(REISER4_SUPER_MAGIC_STRING) - 1) == 0) {
297     *bytes = (unsigned long long) __le32_to_cpu(sb->ms_format) *
298     __le32_to_cpu(sb->ms_blksize);
299     return 1;
300     }
301     return 0;
302     }
303    
304 niro 532 static int xfs_image(const void *buf, unsigned long long *bytes)
305     {
306     const struct xfs_sb *sb = (const struct xfs_sb *)buf;
307    
308     if (__be32_to_cpu(sb->sb_magicnum) == XFS_SB_MAGIC) {
309     *bytes = __be64_to_cpu(sb->sb_dblocks) *
310     __be32_to_cpu(sb->sb_blocksize);
311     return 1;
312     }
313     return 0;
314     }
315    
316     static int jfs_image(const void *buf, unsigned long long *bytes)
317     {
318     const struct jfs_superblock *sb = (const struct jfs_superblock *)buf;
319    
320     if (!memcmp(sb->s_magic, JFS_MAGIC, 4)) {
321 niro 815 *bytes = __le64_to_cpu(sb->s_size) << __le16_to_cpu(sb->s_l2pbsize);
322 niro 532 return 1;
323     }
324     return 0;
325     }
326    
327     static int luks_image(const void *buf, unsigned long long *blocks)
328     {
329     const struct luks_partition_header *lph =
330     (const struct luks_partition_header *)buf;
331    
332     if (!memcmp(lph->magic, LUKS_MAGIC, LUKS_MAGIC_L)) {
333     /* FSSIZE is dictated by the underlying fs, not by LUKS */
334     *blocks = 0;
335     return 1;
336     }
337     return 0;
338     }
339    
340     static int swap_image(const void *buf, unsigned long long *blocks)
341     {
342     const struct swap_super_block *ssb =
343     (const struct swap_super_block *)buf;
344    
345     if (!memcmp(ssb->magic, SWAP_MAGIC_1, SWAP_MAGIC_L) ||
346     !memcmp(ssb->magic, SWAP_MAGIC_2, SWAP_MAGIC_L)) {
347     *blocks = 0;
348     return 1;
349     }
350     return 0;
351     }
352    
353     static int suspend_image(const void *buf, unsigned long long *blocks)
354     {
355     const struct swap_super_block *ssb =
356     (const struct swap_super_block *)buf;
357    
358     if (!memcmp(ssb->magic, SUSP_MAGIC_1, SUSP_MAGIC_L) ||
359     !memcmp(ssb->magic, SUSP_MAGIC_2, SUSP_MAGIC_L) ||
360     !memcmp(ssb->magic, SUSP_MAGIC_U, SUSP_MAGIC_L)) {
361     *blocks = 0;
362     return 1;
363     }
364     return 0;
365     }
366    
367     static int lvm2_image(const void *buf, unsigned long long *blocks)
368     {
369     const struct lvm2_super_block *lsb;
370     int i;
371    
372     /* We must check every 512 byte sector */
373     for (i = 0; i < BLOCK_SIZE; i += 0x200) {
374     lsb = (const struct lvm2_super_block *)(buf + i);
375    
376     if (!memcmp(lsb->magic, LVM2_MAGIC, LVM2_MAGIC_L) &&
377     !memcmp(lsb->type, LVM2_TYPE, LVM2_TYPE_L)) {
378     /* This is just one of possibly many PV's */
379     *blocks = 0;
380     return 1;
381     }
382     }
383    
384     return 0;
385     }
386    
387     static int iso_image(const void *buf, unsigned long long *blocks)
388     {
389     const struct iso_volume_descriptor *isovd =
390     (const struct iso_volume_descriptor *)buf;
391     const struct iso_hs_volume_descriptor *isohsvd =
392     (const struct iso_hs_volume_descriptor *)buf;
393    
394     if (!memcmp(isovd->id, ISO_MAGIC, ISO_MAGIC_L) ||
395     !memcmp(isohsvd->id, ISO_HS_MAGIC, ISO_HS_MAGIC_L)) {
396     *blocks = 0;
397     return 1;
398     }
399     return 0;
400     }
401    
402 niro 815 static int squashfs_image(const void *buf, unsigned long long *blocks)
403     {
404     const struct squashfs_super_block *sb =
405     (const struct squashfs_super_block *)buf;
406    
407     if (sb->s_magic == SQUASHFS_MAGIC
408     || sb->s_magic == SQUASHFS_MAGIC_SWAP
409     || sb->s_magic == SQUASHFS_MAGIC_LZMA
410     || sb->s_magic == SQUASHFS_MAGIC_LZMA_SWAP) {
411     *blocks = (unsigned long long) sb->bytes_used;
412     return 1;
413     }
414     return 0;
415     }
416    
417     static int gfs2_image(const void *buf, unsigned long long *bytes)
418     {
419     const struct gfs2_sb *sb =
420     (const struct gfs2_sb *)buf;
421    
422     if (__be32_to_cpu(sb->sb_header.mh_magic) == GFS2_MAGIC
423     && (__be32_to_cpu(sb->sb_fs_format) == GFS2_FORMAT_FS
424     || __be32_to_cpu(sb->sb_fs_format) == GFS2_FORMAT_MULTI)) {
425     *bytes = 0; /* cpu_to_be32(sb->sb_bsize) * ?; */
426     return 1;
427     }
428     return 0;
429     }
430    
431     static int ocfs2_image(const void *buf, unsigned long long *bytes)
432     {
433     const struct ocfs2_dinode *sb =
434     (const struct ocfs2_dinode *)buf;
435    
436     if (!memcmp(sb->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
437     sizeof(OCFS2_SUPER_BLOCK_SIGNATURE) - 1)) {
438     *bytes = 0;
439     return 1;
440     }
441     return 0;
442     }
443    
444     static int nilfs2_image(const void *buf, unsigned long long *bytes)
445     {
446     const struct nilfs_super_block *sb =
447     (const struct nilfs_super_block *)buf;
448    
449     if (sb->s_magic == __cpu_to_le16(NILFS_SUPER_MAGIC) &&
450     sb->s_rev_level == __cpu_to_le32(2)) {
451     *bytes = (unsigned long long)__le64_to_cpu(sb->s_dev_size);
452     return 1;
453     }
454     return 0;
455     }
456    
457 niro 1122 static int btrfs_image(const void *buf, unsigned long long *bytes)
458     {
459     const struct btrfs_super_block *sb =
460     (const struct btrfs_super_block *)buf;
461    
462     if (!memcmp(sb->magic, BTRFS_MAGIC, BTRFS_MAGIC_L)) {
463     *bytes = (unsigned long long)__le64_to_cpu(sb->total_bytes);
464     return 1;
465     }
466     return 0;
467     }
468    
469 niro 532 struct imagetype {
470     off_t block;
471     const char name[12];
472     int (*identify) (const void *, unsigned long long *);
473     };
474    
475     /*
476     * Note:
477     *
478     * Minix test needs to come after ext3/ext2, since it's possible for
479     * ext3/ext2 to look like minix by pure random chance.
480     *
481     * LVM comes after all other filesystems since it's possible
482     * that an old lvm signature is left on the disk if pvremove
483     * is not used before creating the new fs.
484     *
485     * The same goes for LUKS as for LVM.
486     */
487     static struct imagetype images[] = {
488     {0, "gzip", gzip_image},
489     {0, "cramfs", cramfs_image},
490     {0, "romfs", romfs_image},
491     {0, "xfs", xfs_image},
492 niro 815 {0, "squashfs", squashfs_image},
493     {1, "ext4dev", ext4dev_image},
494     {1, "ext4", ext4_image},
495 niro 532 {1, "ext3", ext3_image},
496     {1, "ext2", ext2_image},
497     {1, "minix", minix_image},
498 niro 815 {1, "nilfs2", nilfs2_image},
499     {2, "ocfs2", ocfs2_image},
500 niro 532 {8, "reiserfs", reiserfs_image},
501     {64, "reiserfs", reiserfs_image},
502 niro 815 {64, "reiser4", reiser4_image},
503     {64, "gfs2", gfs2_image},
504 niro 1122 {64, "btrfs", btrfs_image},
505 niro 532 {32, "jfs", jfs_image},
506     {32, "iso9660", iso_image},
507     {0, "luks", luks_image},
508     {0, "lvm2", lvm2_image},
509     {1, "lvm2", lvm2_image},
510     {-1, "swap", swap_image},
511     {-1, "suspend", suspend_image},
512     {0, "", NULL}
513     };
514    
515     int identify_fs(int fd, const char **fstype,
516     unsigned long long *bytes, off_t offset)
517     {
518     uint64_t buf[BLOCK_SIZE >> 3]; /* 64-bit worst case alignment */
519     off_t cur_block = (off_t) - 1;
520     struct imagetype *ip;
521     int ret;
522     unsigned long long dummy;
523    
524     if (!bytes)
525     bytes = &dummy;
526    
527     *fstype = NULL;
528     *bytes = 0;
529    
530     for (ip = images; ip->identify; ip++) {
531     /* Hack for swap, which apparently is dependent on page size */
532     if (ip->block == -1)
533     ip->block = SWAP_OFFSET();
534    
535     if (cur_block != ip->block) {
536     /*
537     * Read block.
538     */
539     cur_block = ip->block;
540     ret = pread(fd, buf, BLOCK_SIZE,
541     offset + cur_block * BLOCK_SIZE);
542     if (ret != BLOCK_SIZE)
543     return -1; /* error */
544     }
545    
546     if (ip->identify(buf, bytes)) {
547     *fstype = ip->name;
548     return 0;
549     }
550     }
551    
552     return 1; /* Unknown filesystem */
553     }