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