Whamcloud - gitweb
Improve blkid's VFAT/FAT filesystem probe code
[tools/e2fsprogs.git] / lib / blkid / probe.c
1 /*
2  * probe.c - identify a block device by its contents, and return a dev
3  *           struct with the details
4  *
5  * Copyright (C) 1999 by Andries Brouwer
6  * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
7  * Copyright (C) 2001 by Andreas Dilger
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the
11  * GNU Lesser General Public License.
12  * %End-Header%
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #ifdef HAVE_SYS_MKDEV_H
25 #include <sys/mkdev.h>
26 #endif
27 #ifdef HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30 #include "blkidP.h"
31 #include "uuid/uuid.h"
32 #include "probe.h"
33
34 /*
35  * This is a special case code to check for an MDRAID device.  We do
36  * this special since it requires checking for a superblock at the end
37  * of the device.
38  */
39 static int check_mdraid(int fd, unsigned char *ret_uuid)
40 {
41         struct mdp_superblock_s *md;
42         blkid_loff_t            offset;
43         char                    buf[4096];
44         
45         if (fd < 0)
46                 return -BLKID_ERR_PARAM;
47
48         offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
49
50         if (blkid_llseek(fd, offset, 0) < 0 ||
51             read(fd, buf, 4096) != 4096)
52                 return -BLKID_ERR_IO;
53
54         /* Check for magic number */
55         if (memcmp("\251+N\374", buf, 4))
56                 return -BLKID_ERR_PARAM;
57
58         if (!ret_uuid)
59                 return 0;
60         *ret_uuid = 0;
61
62         /* The MD UUID is not contiguous in the superblock, make it so */
63         md = (struct mdp_superblock_s *)buf;
64         if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
65                 memcpy(ret_uuid, &md->set_uuid0, 4);
66                 memcpy(ret_uuid, &md->set_uuid1, 12);
67         }
68         return 0;
69 }
70
71 static void set_uuid(blkid_dev dev, uuid_t uuid)
72 {
73         char    str[37];
74
75         if (!uuid_is_null(uuid)) {
76                 uuid_unparse(uuid, str);
77                 blkid_set_tag(dev, "UUID", str, sizeof(str));
78         }
79 }
80
81 static void get_ext2_info(blkid_dev dev, unsigned char *buf)
82 {
83         struct ext2_super_block *es = (struct ext2_super_block *) buf;
84         const char *label = 0;
85
86         DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", 
87                    blkid_le32(es->s_feature_compat),
88                    blkid_le32(es->s_feature_incompat),
89                    blkid_le32(es->s_feature_ro_compat)));
90
91         if (strlen(es->s_volume_name))
92                 label = es->s_volume_name;
93         blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
94
95         set_uuid(dev, es->s_uuid);
96 }
97
98 static int probe_ext3(int fd __BLKID_ATTR((unused)), 
99                       blkid_cache cache __BLKID_ATTR((unused)), 
100                       blkid_dev dev,
101                       struct blkid_magic *id __BLKID_ATTR((unused)), 
102                       unsigned char *buf)
103 {
104         struct ext2_super_block *es;
105
106         es = (struct ext2_super_block *)buf;
107
108         /* Distinguish between jbd and ext2/3 fs */
109         if (blkid_le32(es->s_feature_incompat) & 
110             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
111                 return -BLKID_ERR_PARAM;
112
113         /* Distinguish between ext3 and ext2 */
114         if (!(blkid_le32(es->s_feature_compat) &
115               EXT3_FEATURE_COMPAT_HAS_JOURNAL))
116                 return -BLKID_ERR_PARAM;
117
118         get_ext2_info(dev, buf);
119
120         blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
121
122         return 0;
123 }
124
125 static int probe_ext2(int fd __BLKID_ATTR((unused)), 
126                       blkid_cache cache __BLKID_ATTR((unused)), 
127                       blkid_dev dev,
128                       struct blkid_magic *id __BLKID_ATTR((unused)), 
129                       unsigned char *buf)
130 {
131         struct ext2_super_block *es;
132
133         es = (struct ext2_super_block *)buf;
134
135         /* Distinguish between jbd and ext2/3 fs */
136         if (blkid_le32(es->s_feature_incompat) & 
137             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
138                 return -BLKID_ERR_PARAM;
139         
140         /* Distinguish between ext3 and ext2 */
141         if ((blkid_le32(es->s_feature_compat) &
142               EXT3_FEATURE_COMPAT_HAS_JOURNAL))
143                 return -BLKID_ERR_PARAM;
144
145         get_ext2_info(dev, buf);
146
147         return 0;
148 }
149
150 static int probe_jbd(int fd __BLKID_ATTR((unused)), 
151                      blkid_cache cache __BLKID_ATTR((unused)), 
152                      blkid_dev dev, 
153                      struct blkid_magic *id __BLKID_ATTR((unused)), 
154                      unsigned char *buf)
155 {
156         struct ext2_super_block *es = (struct ext2_super_block *) buf;
157
158         if (!(blkid_le32(es->s_feature_incompat) &
159               EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
160                 return -BLKID_ERR_PARAM;
161
162         get_ext2_info(dev, buf);
163
164         return 0;
165 }
166
167 static int probe_fat(int fd __BLKID_ATTR((unused)), 
168                       blkid_cache cache __BLKID_ATTR((unused)), 
169                       blkid_dev dev,
170                       struct blkid_magic *id __BLKID_ATTR((unused)), 
171                       unsigned char *buf)
172 {
173         struct vfat_super_block *vs = (struct vfat_super_block *) buf;
174         struct msdos_super_block *ms = (struct msdos_super_block *) buf;
175         char serno[10];
176         const char *label = 0, *vol_label = 0;
177         unsigned char   *vol_serno;
178         int label_len = 0;
179         __u16 sector_size;
180         __u16 dir_entries;
181         __u32 sect_count;
182         __u16 reserved;
183         __u32 fat_size;
184         __u32 dir_size;
185         __u32 cluster_count;
186         __u32 fat_length;
187
188         /* sector size check */
189         sector_size = blkid_le16(*((__u16 *) &ms->ms_sector_size));
190         if (sector_size != 0x200 && sector_size != 0x400 &&
191             sector_size != 0x800 && sector_size != 0x1000)
192                 return 1;
193
194         dir_entries = blkid_le16(*((__u16 *) &ms->ms_dir_entries));
195         reserved =  blkid_le16(ms->ms_reserved);
196         sect_count = blkid_le16(*((__u16 *) &ms->ms_sectors));
197         if (sect_count == 0)
198                 sect_count = blkid_le32(ms->ms_total_sect);
199
200         fat_length = blkid_le16(ms->ms_fat_length);
201         if (fat_length == 0)
202                 fat_length = blkid_le32(vs->vs_fat32_length);
203
204         fat_size = fat_length * ms->ms_fats;
205         dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
206                         (sector_size-1)) / sector_size;
207
208         cluster_count = sect_count - (reserved + fat_size + dir_size);
209         cluster_count /= ms->ms_cluster_size;
210
211         if (cluster_count > FAT32_MAX)
212                 return 1;
213
214         if (ms->ms_fat_length) {
215                 vol_label = ms->ms_label;
216                 vol_serno = ms->ms_serno;
217
218                 blkid_set_tag(dev, "SEC_TYPE", "msdos", sizeof("msdos"));
219         } else {
220                 vol_label = vs->vs_label;
221                 vol_serno = vs->vs_serno;
222         }
223
224         if (vol_label && memcmp(vol_label, "NO NAME    ", 11)) {
225                 const char *end = vol_label + 10;
226
227                 while (*end == ' ' && end >= vol_label)
228                         --end;
229                 if (end >= vol_label) {
230                         label = vol_label;
231                         label_len = end - vol_label + 1;
232                 }
233         }
234
235         /* We can't just print them as %04X, because they are unaligned */
236         sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
237                 vol_serno[1], vol_serno[0]);
238
239         blkid_set_tag(dev, "LABEL", label, label_len);
240         blkid_set_tag(dev, "UUID", serno, sizeof(serno)-1);
241
242         return 0;
243 }
244
245 /*
246  * The FAT filesystem could be without a magic string in superblock
247  * (e.g. old floppies).  This heuristic for FAT detection is inspired
248  * by http://vrfy.org/projects/volume_id/ and Linux kernel.
249  * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
250  */
251 static int probe_fat_nomagic(int fd __BLKID_ATTR((unused)), 
252                              blkid_cache cache __BLKID_ATTR((unused)), 
253                              blkid_dev dev,
254                              struct blkid_magic *id __BLKID_ATTR((unused)), 
255                              unsigned char *buf)
256 {
257         struct vfat_super_block *vs;
258
259         vs = (struct vfat_super_block *)buf;
260
261         /* heads check */
262         if (vs->vs_heads == 0)
263                 return 1;
264
265         /* cluster size check*/ 
266         if (vs->vs_cluster_size == 0 ||
267             (vs->vs_cluster_size & (vs->vs_cluster_size-1)))
268                 return 1;
269
270         /* media check */
271         if (vs->vs_media < 0xf8 && vs->vs_media != 0xf0)
272                 return 1;
273
274         /* fat counts(Linux kernel expects at least 1 FAT table) */
275         if (!vs->vs_fats)
276                 return 1;
277
278         return probe_fat(fd, cache, dev, id, buf);
279 }
280
281 static int probe_xfs(int fd __BLKID_ATTR((unused)), 
282                      blkid_cache cache __BLKID_ATTR((unused)), 
283                      blkid_dev dev,
284                      struct blkid_magic *id __BLKID_ATTR((unused)), 
285                      unsigned char *buf)
286 {
287         struct xfs_super_block *xs;
288         const char *label = 0;
289
290         xs = (struct xfs_super_block *)buf;
291
292         if (strlen(xs->xs_fname))
293                 label = xs->xs_fname;
294         blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
295         set_uuid(dev, xs->xs_uuid);
296         return 0;
297 }
298
299 static int probe_reiserfs(int fd __BLKID_ATTR((unused)), 
300                           blkid_cache cache __BLKID_ATTR((unused)), 
301                           blkid_dev dev,
302                           struct blkid_magic *id, unsigned char *buf)
303 {
304         struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
305         unsigned int blocksize;
306         const char *label = 0;
307
308         blocksize = blkid_le16(rs->rs_blocksize);
309
310         /* If the superblock is inside the journal, we have the wrong one */
311         if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
312                 return -BLKID_ERR_BIG;
313
314         /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
315         if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
316             !strcmp(id->bim_magic, "ReIsEr3Fs")) {
317                 if (strlen(rs->rs_label))
318                         label = rs->rs_label;
319                 set_uuid(dev, rs->rs_uuid);
320         }
321         blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
322
323         return 0;
324 }
325
326 static int probe_jfs(int fd __BLKID_ATTR((unused)), 
327                      blkid_cache cache __BLKID_ATTR((unused)), 
328                      blkid_dev dev,
329                      struct blkid_magic *id __BLKID_ATTR((unused)), 
330                      unsigned char *buf)
331 {
332         struct jfs_super_block *js;
333         const char *label = 0;
334
335         js = (struct jfs_super_block *)buf;
336
337         if (strlen((char *) js->js_label))
338                 label = (char *) js->js_label;
339         blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
340         set_uuid(dev, js->js_uuid);
341         return 0;
342 }
343
344 static int probe_romfs(int fd __BLKID_ATTR((unused)), 
345                        blkid_cache cache __BLKID_ATTR((unused)), 
346                        blkid_dev dev,
347                        struct blkid_magic *id __BLKID_ATTR((unused)), 
348                        unsigned char *buf)
349 {
350         struct romfs_super_block *ros;
351         const char *label = 0;
352
353         ros = (struct romfs_super_block *)buf;
354
355         if (strlen((char *) ros->ros_volume))
356                 label = (char *) ros->ros_volume;
357         blkid_set_tag(dev, "LABEL", label, 0);
358         return 0;
359 }
360
361 static int probe_cramfs(int fd __BLKID_ATTR((unused)), 
362                        blkid_cache cache __BLKID_ATTR((unused)), 
363                        blkid_dev dev,
364                        struct blkid_magic *id __BLKID_ATTR((unused)), 
365                        unsigned char *buf)
366 {
367         struct cramfs_super_block *csb;
368         const char *label = 0;
369
370         csb = (struct cramfs_super_block *)buf;
371
372         if (strlen((char *) csb->name))
373                 label = (char *) csb->name;
374         blkid_set_tag(dev, "LABEL", label, 0);
375         return 0;
376 }
377
378 static int probe_swap0(int fd __BLKID_ATTR((unused)),
379                        blkid_cache cache __BLKID_ATTR((unused)),
380                        blkid_dev dev,
381                        struct blkid_magic *id __BLKID_ATTR((unused)),
382                        unsigned char *buf __BLKID_ATTR((unused)))
383 {
384         blkid_set_tag(dev, "UUID", 0, 0);
385         blkid_set_tag(dev, "LABEL", 0, 0);
386         return 0;
387 }
388
389 static int probe_swap1(int fd,
390                        blkid_cache cache __BLKID_ATTR((unused)),
391                        blkid_dev dev,
392                        struct blkid_magic *id __BLKID_ATTR((unused)),
393                        unsigned char *buf __BLKID_ATTR((unused)))
394 {
395         struct swap_id_block *sws;
396
397         probe_swap0(fd, cache, dev, id, buf);
398         /*
399          * Version 1 swap headers are always located at offset of 1024
400          * bytes, although the swap signature itself is located at the
401          * end of the page (which may vary depending on hardware
402          * pagesize).
403          */
404         if (lseek(fd, 1024, SEEK_SET) < 0) return 1;
405         if (!(sws = (struct swap_id_block *)malloc(1024))) return 1;
406         if (read(fd, sws, 1024) != 1024) {
407                 free(sws);
408                 return 1;
409         }
410
411         /* arbitrary sanity check.. is there any garbage down there? */
412         if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0)  {
413                 if (sws->sws_volume[0])
414                         blkid_set_tag(dev, "LABEL", sws->sws_volume, 
415                                       sizeof(sws->sws_volume));
416                 if (sws->sws_uuid[0])
417                         set_uuid(dev, sws->sws_uuid);
418         }
419         free(sws);
420
421         return 0;
422 }
423
424 static const char
425 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
426                  "NSR03", "TEA01", 0 };
427
428 static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)), 
429                      blkid_dev dev __BLKID_ATTR((unused)),
430                        struct blkid_magic *id __BLKID_ATTR((unused)), 
431                      unsigned char *buf __BLKID_ATTR((unused)))
432 {
433         int j, bs;
434         struct iso_volume_descriptor isosb;
435         const char ** m;
436
437         /* determine the block size by scanning in 2K increments
438            (block sizes larger than 2K will be null padded) */
439         for (bs = 1; bs < 16; bs++) {
440                 lseek(fd, bs*2048+32768, SEEK_SET);
441                 if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
442                         return 1;
443                 if (isosb.id[0])
444                         break;
445         }
446
447         /* Scan up to another 64 blocks looking for additional VSD's */
448         for (j = 1; j < 64; j++) {
449                 if (j > 1) {
450                         lseek(fd, j*bs*2048+32768, SEEK_SET);
451                         if (read(fd, (char *)&isosb, sizeof(isosb))
452                             != sizeof(isosb))
453                                 return 1;
454                 }
455                 /* If we find NSR0x then call it udf:
456                    NSR01 for UDF 1.00
457                    NSR02 for UDF 1.50
458                    NSR03 for UDF 2.00 */
459                 if (!strncmp(isosb.id, "NSR0", 4))
460                         return 0;
461                 for (m = udf_magic; *m; m++)
462                         if (!strncmp(*m, isosb.id, 5))
463                                 break;
464                 if (*m == 0)
465                         return 1;
466         }
467         return 1;
468 }
469
470 static int probe_ocfs(int fd __BLKID_ATTR((unused)), 
471                       blkid_cache cache __BLKID_ATTR((unused)), 
472                       blkid_dev dev,
473                       struct blkid_magic *id __BLKID_ATTR((unused)), 
474                       unsigned char *buf)
475 {
476         struct ocfs_volume_header ovh;
477         struct ocfs_volume_label ovl;
478         __u32 major;
479
480         memcpy(&ovh, buf, sizeof(ovh));
481         memcpy(&ovl, buf+512, sizeof(ovl));
482
483         major = ocfsmajor(ovh);
484         if (major == 1)
485                 blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
486         else if (major >= 9)
487                 blkid_set_tag(dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
488         
489         blkid_set_tag(dev, "LABEL", ovl.label, ocfslabellen(ovl));
490         blkid_set_tag(dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
491         set_uuid(dev, ovl.vol_id);
492         return 0;
493 }
494
495 static int probe_ocfs2(int fd __BLKID_ATTR((unused)), 
496                        blkid_cache cache __BLKID_ATTR((unused)), 
497                        blkid_dev dev,
498                        struct blkid_magic *id __BLKID_ATTR((unused)), 
499                        unsigned char *buf)
500 {
501         struct ocfs2_super_block *osb;
502
503         osb = (struct ocfs2_super_block *)buf;
504
505         blkid_set_tag(dev, "LABEL", osb->s_label, sizeof(osb->s_label));
506         set_uuid(dev, osb->s_uuid);
507         return 0;
508 }
509
510 static int probe_oracleasm(int fd __BLKID_ATTR((unused)), 
511                            blkid_cache cache __BLKID_ATTR((unused)), 
512                            blkid_dev dev,
513                            struct blkid_magic *id __BLKID_ATTR((unused)), 
514                            unsigned char *buf)
515 {
516         struct oracle_asm_disk_label *dl;
517
518         dl = (struct oracle_asm_disk_label *)buf;
519
520         blkid_set_tag(dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
521         return 0;
522 }
523
524 /*
525  * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
526  * in the type_array table below + bim_kbalign.
527  *
528  * When probing for a lot of magics, we handle everything in 1kB buffers so
529  * that we don't have to worry about reading each combination of block sizes.
530  */
531 #define BLKID_BLK_OFFS  64      /* currently reiserfs */
532
533 /*
534  * Various filesystem magics that we can check for.  Note that kboff and
535  * sboff are in kilobytes and bytes respectively.  All magics are in
536  * byte strings so we don't worry about endian issues.
537  */
538 static struct blkid_magic type_array[] = {
539 /*  type     kboff   sboff len  magic                   probe */
540   { "oracleasm", 0,     32,  8, "ORCLDISK",             probe_oracleasm },
541   { "ntfs",      0,      3,  8, "NTFS    ",             0 },
542   { "jbd",       1,   0x38,  2, "\123\357",             probe_jbd },
543   { "ext3",      1,   0x38,  2, "\123\357",             probe_ext3 },
544   { "ext2",      1,   0x38,  2, "\123\357",             probe_ext2 },
545   { "reiserfs",  8,   0x34,  8, "ReIsErFs",             probe_reiserfs },
546   { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",            probe_reiserfs },
547   { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",            probe_reiserfs },
548   { "reiserfs", 64,   0x34,  8, "ReIsErFs",             probe_reiserfs },
549   { "reiserfs",  8,     20,  8, "ReIsErFs",             probe_reiserfs },
550   { "vfat",      0,   0x52,  5, "MSWIN",                probe_fat },
551   { "vfat",      0,   0x52,  8, "FAT32   ",             probe_fat },
552   { "vfat",      0,   0x36,  5, "MSDOS",                probe_fat },
553   { "vfat",      0,   0x36,  8, "FAT16   ",             probe_fat },
554   { "vfat",      0,   0x36,  8, "FAT12   ",             probe_fat },
555   { "vfat",      0,      0,  2, "\353\220",             probe_fat_nomagic },
556   { "vfat",      0,      0,  1, "\351",                 probe_fat_nomagic },
557   { "minix",     1,   0x10,  2, "\177\023",             0 },
558   { "minix",     1,   0x10,  2, "\217\023",             0 },
559   { "minix",     1,   0x10,  2, "\150\044",             0 },
560   { "minix",     1,   0x10,  2, "\170\044",             0 },
561   { "vxfs",      1,      0,  4, "\365\374\001\245",     0 },
562   { "xfs",       0,      0,  4, "XFSB",                 probe_xfs },
563   { "romfs",     0,      0,  8, "-rom1fs-",             probe_romfs },
564   { "bfs",       0,      0,  4, "\316\372\173\033",     0 },
565   { "cramfs",    0,      0,  4, "E=\315\050",           probe_cramfs },
566   { "qnx4",      0,      4,  6, "QNX4FS",               0 },
567   { "udf",      32,      1,  5, "BEA01",                probe_udf },
568   { "udf",      32,      1,  5, "BOOT2",                probe_udf },
569   { "udf",      32,      1,  5, "CD001",                probe_udf },
570   { "udf",      32,      1,  5, "CDW02",                probe_udf },
571   { "udf",      32,      1,  5, "NSR02",                probe_udf },
572   { "udf",      32,      1,  5, "NSR03",                probe_udf },
573   { "udf",      32,      1,  5, "TEA01",                probe_udf },
574   { "iso9660",  32,      1,  5, "CD001",                0 },
575   { "iso9660",  32,      9,  5, "CDROM",                0 },
576   { "jfs",      32,      0,  4, "JFS1",                 probe_jfs },
577   { "hfs",       1,      0,  2, "BD",                   0 },
578   { "ufs",       8,  0x55c,  4, "T\031\001\000",        0 },
579   { "hpfs",      8,      0,  4, "I\350\225\371",        0 },
580   { "sysv",      0,  0x3f8,  4, "\020~\030\375",        0 },
581   { "swap",      0,  0xff6, 10, "SWAP-SPACE",           probe_swap0 },
582   { "swap",      0,  0xff6, 10, "SWAPSPACE2",           probe_swap1 },
583   { "swsuspend", 0,  0xff6,  9, "S1SUSPEND",            probe_swap1 },
584   { "swsuspend", 0,  0xff6,  9, "S2SUSPEND",            probe_swap1 },
585   { "swap",      0, 0x1ff6, 10, "SWAP-SPACE",           probe_swap0 },
586   { "swap",      0, 0x1ff6, 10, "SWAPSPACE2",           probe_swap1 },
587   { "swsuspend", 0, 0x1ff6,  9, "S1SUSPEND",            probe_swap1 },
588   { "swsuspend", 0, 0x1ff6,  9, "S2SUSPEND",            probe_swap1 },
589   { "swap",      0, 0x3ff6, 10, "SWAP-SPACE",           probe_swap0 },
590   { "swap",      0, 0x3ff6, 10, "SWAPSPACE2",           probe_swap1 },
591   { "swsuspend", 0, 0x3ff6,  9, "S1SUSPEND",            probe_swap1 },
592   { "swsuspend", 0, 0x3ff6,  9, "S2SUSPEND",            probe_swap1 },
593   { "swap",      0, 0x7ff6, 10, "SWAP-SPACE",           probe_swap0 },
594   { "swap",      0, 0x7ff6, 10, "SWAPSPACE2",           probe_swap1 },
595   { "swsuspend", 0, 0x7ff6,  9, "S1SUSPEND",            probe_swap1 },
596   { "swsuspend", 0, 0x7ff6,  9, "S2SUSPEND",            probe_swap1 },
597   { "swap",      0, 0xfff6, 10, "SWAP-SPACE",           probe_swap0 },
598   { "swap",      0, 0xfff6, 10, "SWAPSPACE2",           probe_swap1 },
599   { "swsuspend", 0, 0xfff6,  9, "S1SUSPEND",            probe_swap1 },
600   { "swsuspend", 0, 0xfff6,  9, "S2SUSPEND",            probe_swap1 },
601   { "ocfs",      0,      8,  9, "OracleCFS",            probe_ocfs },
602   { "ocfs2",     1,      0,  6, "OCFSV2",               probe_ocfs2 },
603   { "ocfs2",     2,      0,  6, "OCFSV2",               probe_ocfs2 },
604   { "ocfs2",     4,      0,  6, "OCFSV2",               probe_ocfs2 },
605   { "ocfs2",     8,      0,  6, "OCFSV2",               probe_ocfs2 },
606   {   NULL,      0,      0,  0, NULL,                   NULL }
607 };
608
609 /*
610  * Verify that the data in dev is consistent with what is on the actual
611  * block device (using the devname field only).  Normally this will be
612  * called when finding items in the cache, but for long running processes
613  * is also desirable to revalidate an item before use.
614  *
615  * If we are unable to revalidate the data, we return the old data and
616  * do not set the BLKID_BID_FL_VERIFIED flag on it.
617  */
618 blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
619 {
620         struct blkid_magic *id;
621         unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
622         const char *type;
623         struct stat st;
624         time_t diff, now;
625         int fd, idx;
626
627         if (!dev)
628                 return NULL;
629
630         now = time(0);
631         diff = now - dev->bid_time;
632
633         if ((now < dev->bid_time) ||
634             (diff < BLKID_PROBE_MIN) || 
635             (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
636              diff < BLKID_PROBE_INTERVAL))
637                 return dev;
638
639         DBG(DEBUG_PROBE,
640             printf("need to revalidate %s (time since last check %lu)\n", 
641                    dev->bid_name, diff));
642
643         if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
644             (fstat(fd, &st) < 0)) {
645                 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
646                         blkid_free_dev(dev);
647                         return NULL;
648                 }
649                 /* We don't have read permission, just return cache data. */
650                 DBG(DEBUG_PROBE,
651                     printf("returning unverified data for %s\n",
652                            dev->bid_name));
653                 return dev;
654         }
655
656         memset(bufs, 0, sizeof(bufs));
657         
658         /*
659          * Iterate over the type array.  If we already know the type,
660          * then try that first.  If it doesn't work, then blow away
661          * the type information, and try again.
662          * 
663          */
664 try_again:
665         type = 0;
666         if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
667                 uuid_t  uuid;
668
669                 if (check_mdraid(fd, uuid) == 0) {
670                         set_uuid(dev, uuid);
671                         type = "mdraid";
672                         goto found_type;
673                 }
674         }
675         for (id = type_array; id->bim_type; id++) {
676                 if (dev->bid_type &&
677                     strcmp(id->bim_type, dev->bid_type))
678                         continue;
679
680                 idx = id->bim_kboff + (id->bim_sboff >> 10);
681                 if (idx > BLKID_BLK_OFFS || idx < 0)
682                         continue;
683                 buf = bufs[idx];
684                 if (!buf) {
685                         if (lseek(fd, idx << 10, SEEK_SET) < 0)
686                                 continue;
687
688                         if (!(buf = (unsigned char *)malloc(1024)))
689                                 continue;
690                         
691                         if (read(fd, buf, 1024) != 1024) {
692                                 free(buf);
693                                 continue;
694                         }
695                         bufs[idx] = buf;
696                 }
697
698                 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
699                            id->bim_len))
700                         continue;
701
702                 if ((id->bim_probe == NULL) ||
703                     (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
704                         type = id->bim_type;
705                         goto found_type;
706                 }
707         }
708
709         if (!id->bim_type && dev->bid_type) {
710                 /*
711                  * Zap the device filesystem type and try again
712                  */
713                 blkid_set_tag(dev, "TYPE", 0, 0);
714                 blkid_set_tag(dev, "SEC_TYPE", 0, 0);
715                 blkid_set_tag(dev, "LABEL", 0, 0);
716                 blkid_set_tag(dev, "UUID", 0, 0);
717                 goto try_again;
718         }
719
720         if (!dev->bid_type) {
721                 blkid_free_dev(dev);
722                 return NULL;
723         }
724                 
725 found_type:
726         if (dev && type) {
727                 dev->bid_devno = st.st_rdev;
728                 dev->bid_time = time(0);
729                 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
730                 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
731
732                 blkid_set_tag(dev, "TYPE", type, 0);
733                                 
734                 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
735                            dev->bid_name, st.st_rdev, type));
736         }
737
738         close(fd);
739
740         return dev;
741 }
742
743 int blkid_known_fstype(const char *fstype)
744 {
745         struct blkid_magic *id;
746
747         for (id = type_array; id->bim_type; id++) {
748                 if (strcmp(fstype, id->bim_type) == 0)
749                         return 1;
750         }
751         return 0;
752 }
753
754 #ifdef TEST_PROGRAM
755 int main(int argc, char **argv)
756 {
757         blkid_dev dev;
758         blkid_cache cache;
759         int ret;
760
761         blkid_debug_mask = DEBUG_ALL;
762         if (argc != 2) {
763                 fprintf(stderr, "Usage: %s device\n"
764                         "Probe a single device to determine type\n", argv[0]);
765                 exit(1);
766         }
767         if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
768                 fprintf(stderr, "%s: error creating cache (%d)\n",
769                         argv[0], ret);
770                 exit(1);
771         }
772         dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
773         if (!dev) {
774                 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
775                 return (1);
776         }
777         printf("%s is type %s\n", argv[1], dev->bid_type ?
778                 dev->bid_type : "(null)");
779         if (dev->bid_label)
780                 printf("\tlabel is '%s'\n", dev->bid_label);
781         if (dev->bid_uuid)
782                 printf("\tuuid is %s\n", dev->bid_uuid);
783         
784         blkid_free_dev(dev);
785         return (0);
786 }
787 #endif