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