Whamcloud - gitweb
Avoid using uint and uchar types when probing for ocfs2 filesystems
[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 int probe_ext2(int fd __BLKID_ATTR((unused)), 
82                       blkid_cache cache __BLKID_ATTR((unused)), 
83                       blkid_dev dev,
84                       struct blkid_magic *id, unsigned char *buf)
85 {
86         struct ext2_super_block *es;
87         const char *sec_type = 0, *label = 0;
88
89         es = (struct ext2_super_block *)buf;
90
91         DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", 
92                    blkid_le32(es->s_feature_compat),
93                    blkid_le32(es->s_feature_incompat),
94                    blkid_le32(es->s_feature_ro_compat)));
95
96         /* Distinguish between jbd and ext2/3 fs */
97         if (id && (blkid_le32(es->s_feature_incompat) &
98                    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
99                 return -BLKID_ERR_PARAM;
100
101         if (strlen(es->s_volume_name))
102                 label = es->s_volume_name;
103         blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
104
105         set_uuid(dev, es->s_uuid);
106
107         if (blkid_le32(es->s_feature_compat) &
108             EXT3_FEATURE_COMPAT_HAS_JOURNAL)
109                 sec_type = "ext3";
110         
111         blkid_set_tag(dev, "SEC_TYPE", sec_type, 0);
112
113         return 0;
114 }
115
116 static int probe_jbd(int fd __BLKID_ATTR((unused)), 
117                      blkid_cache cache __BLKID_ATTR((unused)), 
118                      blkid_dev dev, 
119                      struct blkid_magic *id __BLKID_ATTR((unused)), 
120                      unsigned char *buf)
121 {
122         struct ext2_super_block *es = (struct ext2_super_block *) buf;
123
124         if (!(blkid_le32(es->s_feature_incompat) &
125               EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
126                 return -BLKID_ERR_PARAM;
127
128         return (probe_ext2(fd, cache, dev, 0, buf));
129 }
130
131 static int probe_vfat(int fd __BLKID_ATTR((unused)), 
132                       blkid_cache cache __BLKID_ATTR((unused)), 
133                       blkid_dev dev,
134                       struct blkid_magic *id __BLKID_ATTR((unused)), 
135                       unsigned char *buf)
136 {
137         struct vfat_super_block *vs;
138         char serno[10];
139         const char *label = 0;
140
141         vs = (struct vfat_super_block *)buf;
142
143         if (strncmp(vs->vs_label, "NO NAME", 7)) {
144                 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
145
146                 while (*end == ' ' && end >= vs->vs_label)
147                         --end;
148                 if (end >= vs->vs_label)
149                         label = vs->vs_label;
150                 blkid_set_tag(dev, "LABEL", label, end - vs->vs_label + 1);
151         }
152
153         /* We can't just print them as %04X, because they are unaligned */
154         sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
155                 vs->vs_serno[1], vs->vs_serno[0]);
156         blkid_set_tag(dev, "UUID", serno, sizeof(serno));
157
158         return 0;
159 }
160
161 static int probe_msdos(int fd __BLKID_ATTR((unused)), 
162                        blkid_cache cache __BLKID_ATTR((unused)), 
163                        blkid_dev dev,
164                        struct blkid_magic *id __BLKID_ATTR((unused)), 
165                        unsigned char *buf)
166 {
167         struct msdos_super_block *ms = (struct msdos_super_block *) buf;
168         char serno[10];
169         const char *label = 0;
170
171         if (strncmp(ms->ms_label, "NO NAME", 7)) {
172                 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
173
174                 while (*end == ' ' && end >= ms->ms_label)
175                         --end;
176                 if (end >= ms->ms_label)
177                         label = ms->ms_label;
178                 blkid_set_tag(dev, "LABEL", label, end - ms->ms_label + 1);
179         }
180
181         /* We can't just print them as %04X, because they are unaligned */
182         sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
183                 ms->ms_serno[1], ms->ms_serno[0]);
184         blkid_set_tag(dev, "UUID", serno, 0);
185
186         return 0;
187 }
188
189 static int probe_xfs(int fd __BLKID_ATTR((unused)), 
190                      blkid_cache cache __BLKID_ATTR((unused)), 
191                      blkid_dev dev,
192                      struct blkid_magic *id __BLKID_ATTR((unused)), 
193                      unsigned char *buf)
194 {
195         struct xfs_super_block *xs;
196         const char *label = 0;
197
198         xs = (struct xfs_super_block *)buf;
199
200         if (strlen(xs->xs_fname))
201                 label = xs->xs_fname;
202         blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
203         set_uuid(dev, xs->xs_uuid);
204         return 0;
205 }
206
207 static int probe_reiserfs(int fd __BLKID_ATTR((unused)), 
208                           blkid_cache cache __BLKID_ATTR((unused)), 
209                           blkid_dev dev,
210                           struct blkid_magic *id, unsigned char *buf)
211 {
212         struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
213         unsigned int blocksize;
214         const char *label = 0;
215
216         blocksize = blkid_le16(rs->rs_blocksize);
217
218         /* If the superblock is inside the journal, we have the wrong one */
219         if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
220                 return -BLKID_ERR_BIG;
221
222         /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
223         if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
224             !strcmp(id->bim_magic, "ReIsEr3Fs")) {
225                 if (strlen(rs->rs_label))
226                         label = rs->rs_label;
227                 blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
228                 set_uuid(dev, rs->rs_uuid);
229         }
230
231         return 0;
232 }
233
234 static int probe_jfs(int fd __BLKID_ATTR((unused)), 
235                      blkid_cache cache __BLKID_ATTR((unused)), 
236                      blkid_dev dev,
237                      struct blkid_magic *id __BLKID_ATTR((unused)), 
238                      unsigned char *buf)
239 {
240         struct jfs_super_block *js;
241         const char *label = 0;
242
243         js = (struct jfs_super_block *)buf;
244
245         if (strlen((char *) js->js_label))
246                 label = (char *) js->js_label;
247         blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
248         set_uuid(dev, js->js_uuid);
249         return 0;
250 }
251
252 static int probe_romfs(int fd __BLKID_ATTR((unused)), 
253                        blkid_cache cache __BLKID_ATTR((unused)), 
254                        blkid_dev dev,
255                        struct blkid_magic *id __BLKID_ATTR((unused)), 
256                        unsigned char *buf)
257 {
258         struct romfs_super_block *ros;
259         const char *label = 0;
260
261         ros = (struct romfs_super_block *)buf;
262
263         if (strlen((char *) ros->ros_volume))
264                 label = (char *) ros->ros_volume;
265         blkid_set_tag(dev, "LABEL", label, strlen(label));
266         return 0;
267 }
268
269 static const char
270 *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
271                  "NSR03", "TEA01", 0 };
272
273 static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)), 
274                      blkid_dev dev __BLKID_ATTR((unused)),
275                        struct blkid_magic *id __BLKID_ATTR((unused)), 
276                      unsigned char *buf __BLKID_ATTR((unused)))
277 {
278         int j, bs;
279         struct iso_volume_descriptor isosb;
280         const char ** m;
281
282         /* determine the block size by scanning in 2K increments
283            (block sizes larger than 2K will be null padded) */
284         for (bs = 1; bs < 16; bs++) {
285                 lseek(fd, bs*2048+32768, SEEK_SET);
286                 if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
287                         return 1;
288                 if (isosb.id[0])
289                         break;
290         }
291
292         /* Scan up to another 64 blocks looking for additional VSD's */
293         for (j = 1; j < 64; j++) {
294                 if (j > 1) {
295                         lseek(fd, j*bs*2048+32768, SEEK_SET);
296                         if (read(fd, (char *)&isosb, sizeof(isosb))
297                             != sizeof(isosb))
298                                 return 1;
299                 }
300                 /* If we find NSR0x then call it udf:
301                    NSR01 for UDF 1.00
302                    NSR02 for UDF 1.50
303                    NSR03 for UDF 2.00 */
304                 if (!strncmp(isosb.id, "NSR0", 4))
305                         return 0;
306                 for (m = udf_magic; *m; m++)
307                         if (!strncmp(*m, isosb.id, 5))
308                                 break;
309                 if (*m == 0)
310                         return 1;
311         }
312         return 1;
313 }
314
315 static int probe_ocfs(int fd __BLKID_ATTR((unused)), 
316                       blkid_cache cache __BLKID_ATTR((unused)), 
317                       blkid_dev dev,
318                       struct blkid_magic *id __BLKID_ATTR((unused)), 
319                       unsigned char *buf)
320 {
321         struct ocfs_volume_header ovh;
322         struct ocfs_volume_label ovl;
323         __u32 major;
324
325         memcpy(&ovh, buf, sizeof(ovh));
326         memcpy(&ovl, buf+512, sizeof(ovl));
327
328         major = ocfsmajor(ovh);
329         if (major == 1)
330                 blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
331         else if (major >= 9)
332                 blkid_set_tag(dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
333         
334         blkid_set_tag(dev, "LABEL", ovl.label, ocfslabellen(ovl));
335         blkid_set_tag(dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
336         set_uuid(dev, ovl.vol_id);
337         return 0;
338 }
339
340 static int probe_ocfs2(int fd __BLKID_ATTR((unused)), 
341                       blkid_cache cache __BLKID_ATTR((unused)), 
342                       blkid_dev dev,
343                       struct blkid_magic *id __BLKID_ATTR((unused)), 
344                       unsigned char *buf)
345 {
346         struct ocfs2_super_block *osb;
347
348         osb = (struct ocfs2_super_block *)buf;
349
350         blkid_set_tag(dev, "LABEL", osb->s_label, sizeof(osb->s_label));
351         set_uuid(dev, osb->s_uuid);
352         return 0;
353 }
354
355 /*
356  * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
357  * in the type_array table below + bim_kbalign.
358  *
359  * When probing for a lot of magics, we handle everything in 1kB buffers so
360  * that we don't have to worry about reading each combination of block sizes.
361  */
362 #define BLKID_BLK_OFFS  64      /* currently reiserfs */
363
364 /*
365  * Various filesystem magics that we can check for.  Note that kboff and
366  * sboff are in kilobytes and bytes respectively.  All magics are in
367  * byte strings so we don't worry about endian issues.
368  */
369 static struct blkid_magic type_array[] = {
370 /*  type     kboff   sboff len  magic                   probe */
371   { "jbd",       1,   0x38,  2, "\123\357",             probe_jbd },
372   { "ext2",      1,   0x38,  2, "\123\357",             probe_ext2 },
373   { "reiserfs",  8,   0x34,  8, "ReIsErFs",             probe_reiserfs },
374   { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",            probe_reiserfs },
375   { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",            probe_reiserfs },
376   { "reiserfs", 64,   0x34,  8, "ReIsErFs",             probe_reiserfs },
377   { "reiserfs",  8,     20,  8, "ReIsErFs",             probe_reiserfs },
378   { "ntfs",      0,      3,  8, "NTFS    ",             0 },
379   { "vfat",      0,   0x52,  5, "MSWIN",                probe_vfat },
380   { "vfat",      0,   0x52,  8, "FAT32   ",             probe_vfat },
381   { "msdos",     0,   0x36,  5, "MSDOS",                probe_msdos },
382   { "msdos",     0,   0x36,  8, "FAT16   ",             probe_msdos },
383   { "msdos",     0,   0x36,  8, "FAT12   ",             probe_msdos },
384   { "minix",     1,   0x10,  2, "\177\023",             0 },
385   { "minix",     1,   0x10,  2, "\217\023",             0 },
386   { "minix",     1,   0x10,  2, "\150\044",             0 },
387   { "minix",     1,   0x10,  2, "\170\044",             0 },
388   { "vxfs",      1,      0,  4, "\365\374\001\245",     0 },
389   { "xfs",       0,      0,  4, "XFSB",                 probe_xfs },
390   { "romfs",     0,      0,  8, "-rom1fs-",             probe_romfs },
391   { "bfs",       0,      0,  4, "\316\372\173\033",     0 },
392   { "cramfs",    0,      0,  4, "E=\315\034",           0 },
393   { "qnx4",      0,      4,  6, "QNX4FS",               0 },
394   { "udf",      32,      1,  5, "BEA01",                probe_udf },
395   { "udf",      32,      1,  5, "BOOT2",                probe_udf },
396   { "udf",      32,      1,  5, "CD001",                probe_udf },
397   { "udf",      32,      1,  5, "CDW02",                probe_udf },
398   { "udf",      32,      1,  5, "NSR02",                probe_udf },
399   { "udf",      32,      1,  5, "NSR03",                probe_udf },
400   { "udf",      32,      1,  5, "TEA01",                probe_udf },
401   { "iso9660",  32,      1,  5, "CD001",                0 },
402   { "iso9660",  32,      9,  5, "CDROM",                0 },
403   { "jfs",      32,      0,  4, "JFS1",                 probe_jfs },
404   { "hfs",       1,      0,  2, "BD",                   0 },
405   { "ufs",       8,  0x55c,  4, "T\031\001\000",        0 },
406   { "hpfs",      8,      0,  4, "I\350\225\371",        0 },
407   { "sysv",      0,  0x3f8,  4, "\020~\030\375",        0 },
408   { "swap",      0,  0xff6, 10, "SWAP-SPACE",           0 },
409   { "swap",      0,  0xff6, 10, "SWAPSPACE2",           0 },
410   { "swap",      0, 0x1ff6, 10, "SWAP-SPACE",           0 },
411   { "swap",      0, 0x1ff6, 10, "SWAPSPACE2",           0 },
412   { "swap",      0, 0x3ff6, 10, "SWAP-SPACE",           0 },
413   { "swap",      0, 0x3ff6, 10, "SWAPSPACE2",           0 },
414   { "ocfs",      0,      8,  9,  "OracleCFS",           probe_ocfs },
415   { "ocfs2",     1,      0,  6,  "OCFSV2",              probe_ocfs2 },
416   { "ocfs2",     2,      0,  6,  "OCFSV2",              probe_ocfs2 },
417   { "ocfs2",     4,      0,  6,  "OCFSV2",              probe_ocfs2 },
418   { "ocfs2",     8,      0,  6,  "OCFSV2",              probe_ocfs2 },
419   {   NULL,      0,      0,  0, NULL,                   NULL }
420 };
421
422 /*
423  * Verify that the data in dev is consistent with what is on the actual
424  * block device (using the devname field only).  Normally this will be
425  * called when finding items in the cache, but for long running processes
426  * is also desirable to revalidate an item before use.
427  *
428  * If we are unable to revalidate the data, we return the old data and
429  * do not set the BLKID_BID_FL_VERIFIED flag on it.
430  */
431 blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
432 {
433         struct blkid_magic *id;
434         unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
435         const char *type;
436         struct stat st;
437         time_t diff, now;
438         int fd, idx;
439
440         if (!dev)
441                 return NULL;
442
443         now = time(0);
444         diff = now - dev->bid_time;
445
446         if ((now < dev->bid_time) ||
447             (diff < BLKID_PROBE_MIN) || 
448             (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
449              diff < BLKID_PROBE_INTERVAL))
450                 return dev;
451
452         DBG(DEBUG_PROBE,
453             printf("need to revalidate %s (time since last check %lu)\n", 
454                    dev->bid_name, diff));
455
456         if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
457             (fstat(fd, &st) < 0) || !S_ISBLK(st.st_mode)) {
458                 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
459                         blkid_free_dev(dev);
460                         return NULL;
461                 }
462                 /* We don't have read permission, just return cache data. */
463                 DBG(DEBUG_PROBE,
464                     printf("returning unverified data for %s\n",
465                            dev->bid_name));
466                 return dev;
467         }
468
469         memset(bufs, 0, sizeof(bufs));
470         
471         /*
472          * Iterate over the type array.  If we already know the type,
473          * then try that first.  If it doesn't work, then blow away
474          * the type information, and try again.
475          * 
476          */
477 try_again:
478         type = 0;
479         if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
480                 uuid_t  uuid;
481
482                 if (check_mdraid(fd, uuid) == 0) {
483                         set_uuid(dev, uuid);
484                         type = "mdraid";
485                         goto found_type;
486                 }
487         }
488         for (id = type_array; id->bim_type; id++) {
489                 if (dev->bid_type &&
490                     strcmp(id->bim_type, dev->bid_type))
491                         continue;
492
493                 idx = id->bim_kboff + (id->bim_sboff >> 10);
494                 if (idx > BLKID_BLK_OFFS || idx < 0)
495                         continue;
496                 buf = bufs[idx];
497                 if (!buf) {
498                         if (lseek(fd, idx << 10, SEEK_SET) < 0)
499                                 continue;
500
501                         if (!(buf = (unsigned char *)malloc(1024)))
502                                 continue;
503                         
504                         if (read(fd, buf, 1024) != 1024) {
505                                 free(buf);
506                                 continue;
507                         }
508                         bufs[idx] = buf;
509                 }
510
511                 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
512                            id->bim_len))
513                         continue;
514
515                 if ((id->bim_probe == NULL) ||
516                     (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
517                         type = id->bim_type;
518                         goto found_type;
519                 }
520         }
521
522         if (!id->bim_type && dev->bid_type) {
523                 /*
524                  * Zap the device filesystem type and try again
525                  */
526                 blkid_set_tag(dev, "TYPE", 0, 0);
527                 blkid_set_tag(dev, "SEC_TYPE", 0, 0);
528                 blkid_set_tag(dev, "LABEL", 0, 0);
529                 blkid_set_tag(dev, "UUID", 0, 0);
530                 goto try_again;
531         }
532
533         if (!dev->bid_type) {
534                 blkid_free_dev(dev);
535                 return NULL;
536         }
537                 
538 found_type:
539         if (dev && type) {
540                 dev->bid_devno = st.st_rdev;
541                 dev->bid_time = time(0);
542                 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
543                 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
544
545                 blkid_set_tag(dev, "TYPE", type, 0);
546                                 
547                 DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
548                            dev->bid_name, st.st_rdev, type));
549         }
550
551         close(fd);
552
553         return dev;
554 }
555
556 int blkid_known_fstype(const char *fstype)
557 {
558         struct blkid_magic *id;
559
560         for (id = type_array; id->bim_type; id++) {
561                 if (strcmp(fstype, id->bim_type) == 0)
562                         return 1;
563         }
564         return 0;
565 }
566
567 #ifdef TEST_PROGRAM
568 int main(int argc, char **argv)
569 {
570         blkid_dev dev;
571         blkid_cache cache;
572         int ret;
573
574         blkid_debug_mask = DEBUG_ALL;
575         if (argc != 2) {
576                 fprintf(stderr, "Usage: %s device\n"
577                         "Probe a single device to determine type\n", argv[0]);
578                 exit(1);
579         }
580         if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
581                 fprintf(stderr, "%s: error creating cache (%d)\n",
582                         argv[0], ret);
583                 exit(1);
584         }
585         dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
586         if (!dev) {
587                 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
588                 return (1);
589         }
590         printf("%s is type %s\n", argv[1], dev->bid_type ?
591                 dev->bid_type : "(null)");
592         if (dev->bid_label)
593                 printf("\tlabel is '%s'\n", dev->bid_label);
594         if (dev->bid_uuid)
595                 printf("\tuuid is %s\n", dev->bid_uuid);
596         
597         blkid_free_dev(dev);
598         return (0);
599 }
600 #endif