blob: 03bbc749c2735ba9fc2121e39547b82fbfdd6f3a [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
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"
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000031#include "../uuid/uuid.h"
Mike Frysinger38a33f92005-05-09 22:13:22 +000032#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 */
39static 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];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000044
Mike Frysinger38a33f92005-05-09 22:13:22 +000045 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
71static 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
81static 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
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000086 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
Mike Frysinger38a33f92005-05-09 22:13:22 +000087 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
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000098static int probe_ext3(int fd __BLKID_ATTR((unused)),
99 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000100 blkid_dev dev,
Mike Frysinger9aa7f202006-03-18 04:17:59 +0000101 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger874af852006-03-08 07:03:27 +0000102 unsigned char *buf)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000103{
104 struct ext2_super_block *es;
105
106 es = (struct ext2_super_block *)buf;
107
108 /* Distinguish between jbd and ext2/3 fs */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000109 if (blkid_le32(es->s_feature_incompat) &
Mike Frysinger38a33f92005-05-09 22:13:22 +0000110 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
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000125static int probe_ext2(int fd __BLKID_ATTR((unused)),
126 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000127 blkid_dev dev,
Mike Frysinger9aa7f202006-03-18 04:17:59 +0000128 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger874af852006-03-08 07:03:27 +0000129 unsigned char *buf)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000130{
131 struct ext2_super_block *es;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000132
133 es = (struct ext2_super_block *)buf;
134
135 /* Distinguish between jbd and ext2/3 fs */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000136 if (blkid_le32(es->s_feature_incompat) &
Mike Frysinger38a33f92005-05-09 22:13:22 +0000137 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
138 return -BLKID_ERR_PARAM;
139
140 get_ext2_info(dev, buf);
141
142 return 0;
143}
144
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000145static int probe_jbd(int fd __BLKID_ATTR((unused)),
146 blkid_cache cache __BLKID_ATTR((unused)),
147 blkid_dev dev,
148 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000149 unsigned char *buf)
150{
151 struct ext2_super_block *es = (struct ext2_super_block *) buf;
152
153 if (!(blkid_le32(es->s_feature_incompat) &
154 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
155 return -BLKID_ERR_PARAM;
156
157 get_ext2_info(dev, buf);
158
159 return 0;
160}
161
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000162static int probe_vfat(int fd __BLKID_ATTR((unused)),
163 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000164 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000165 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000166 unsigned char *buf)
167{
168 struct vfat_super_block *vs;
169 char serno[10];
170 const char *label = 0;
171 int label_len = 0;
172
173 vs = (struct vfat_super_block *)buf;
174
175 if (strncmp(vs->vs_label, "NO NAME", 7)) {
176 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
177
178 while (*end == ' ' && end >= vs->vs_label)
179 --end;
180 if (end >= vs->vs_label) {
181 label = vs->vs_label;
182 label_len = end - vs->vs_label + 1;
183 }
184 }
185
186 /* We can't just print them as %04X, because they are unaligned */
187 sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
188 vs->vs_serno[1], vs->vs_serno[0]);
189 blkid_set_tag(dev, "LABEL", label, label_len);
190 blkid_set_tag(dev, "UUID", serno, sizeof(serno));
191
192 return 0;
193}
194
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000195static int probe_msdos(int fd __BLKID_ATTR((unused)),
196 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000197 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000198 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000199 unsigned char *buf)
200{
201 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
202 char serno[10];
203 const char *label = 0;
204 int label_len = 0;
205
206 if (strncmp(ms->ms_label, "NO NAME", 7)) {
207 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
208
209 while (*end == ' ' && end >= ms->ms_label)
210 --end;
211 if (end >= ms->ms_label) {
212 label = ms->ms_label;
213 label_len = end - ms->ms_label + 1;
214 }
215 }
216
217 /* We can't just print them as %04X, because they are unaligned */
218 sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
219 ms->ms_serno[1], ms->ms_serno[0]);
220 blkid_set_tag(dev, "UUID", serno, 0);
221 blkid_set_tag(dev, "LABEL", label, label_len);
222 blkid_set_tag(dev, "SEC_TYPE", "msdos", sizeof("msdos"));
223
224 return 0;
225}
226
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000227static int probe_xfs(int fd __BLKID_ATTR((unused)),
228 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000229 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000230 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000231 unsigned char *buf)
232{
233 struct xfs_super_block *xs;
234 const char *label = 0;
235
236 xs = (struct xfs_super_block *)buf;
237
238 if (strlen(xs->xs_fname))
239 label = xs->xs_fname;
240 blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
241 set_uuid(dev, xs->xs_uuid);
242 return 0;
243}
244
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000245static int probe_reiserfs(int fd __BLKID_ATTR((unused)),
246 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000247 blkid_dev dev,
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000248 const struct blkid_magic *id, unsigned char *buf)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000249{
250 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
251 unsigned int blocksize;
252 const char *label = 0;
253
254 blocksize = blkid_le16(rs->rs_blocksize);
255
256 /* If the superblock is inside the journal, we have the wrong one */
257 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
258 return -BLKID_ERR_BIG;
259
260 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
261 if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
262 !strcmp(id->bim_magic, "ReIsEr3Fs")) {
263 if (strlen(rs->rs_label))
264 label = rs->rs_label;
265 set_uuid(dev, rs->rs_uuid);
266 }
267 blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
268
269 return 0;
270}
271
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000272static int probe_jfs(int fd __BLKID_ATTR((unused)),
273 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000274 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000275 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000276 unsigned char *buf)
277{
278 struct jfs_super_block *js;
279 const char *label = 0;
280
281 js = (struct jfs_super_block *)buf;
282
283 if (strlen((char *) js->js_label))
284 label = (char *) js->js_label;
285 blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
286 set_uuid(dev, js->js_uuid);
287 return 0;
288}
289
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000290static int probe_romfs(int fd __BLKID_ATTR((unused)),
291 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000292 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000293 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000294 unsigned char *buf)
295{
296 struct romfs_super_block *ros;
297 const char *label = 0;
298
299 ros = (struct romfs_super_block *)buf;
300
301 if (strlen((char *) ros->ros_volume))
302 label = (char *) ros->ros_volume;
303 blkid_set_tag(dev, "LABEL", label, 0);
304 return 0;
305}
306
Mike Frysinger874af852006-03-08 07:03:27 +0000307static int probe_cramfs(int fd __BLKID_ATTR((unused)),
308 blkid_cache cache __BLKID_ATTR((unused)),
309 blkid_dev dev,
Mike Frysinger9aa7f202006-03-18 04:17:59 +0000310 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger874af852006-03-08 07:03:27 +0000311 unsigned char *buf)
312{
313 struct cramfs_super_block *csb;
314 const char *label = 0;
315
316 csb = (struct cramfs_super_block *)buf;
317
318 if (strlen((char *) csb->name))
319 label = (char *) csb->name;
320 blkid_set_tag(dev, "LABEL", label, 0);
321 return 0;
322}
323
Mike Frysinger38a33f92005-05-09 22:13:22 +0000324static int probe_swap0(int fd __BLKID_ATTR((unused)),
325 blkid_cache cache __BLKID_ATTR((unused)),
326 blkid_dev dev,
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000327 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000328 unsigned char *buf __BLKID_ATTR((unused)))
329{
330 blkid_set_tag(dev, "UUID", 0, 0);
331 blkid_set_tag(dev, "LABEL", 0, 0);
332 return 0;
333}
334
335static int probe_swap1(int fd,
336 blkid_cache cache __BLKID_ATTR((unused)),
337 blkid_dev dev,
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000338 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000339 unsigned char *buf __BLKID_ATTR((unused)))
340{
341 struct swap_id_block *sws;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000342
343 probe_swap0(fd, cache, dev, id, buf);
344 /*
345 * Version 1 swap headers are always located at offset of 1024
346 * bytes, although the swap signature itself is located at the
347 * end of the page (which may vary depending on hardware
348 * pagesize).
349 */
350 if (lseek(fd, 1024, SEEK_SET) < 0) return 1;
Mike Frysinger7fde8de2005-06-11 22:37:25 +0000351 sws = (struct swap_id_block *)xmalloc(1024);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000352 if (read(fd, sws, 1024) != 1024) {
353 free(sws);
354 return 1;
355 }
356
357 /* arbitrary sanity check.. is there any garbage down there? */
358 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
359 if (sws->sws_volume[0])
Eric Andersen3496fdc2006-01-30 23:09:20 +0000360 blkid_set_tag(dev, "LABEL", (const char*)sws->sws_volume,
Mike Frysinger38a33f92005-05-09 22:13:22 +0000361 sizeof(sws->sws_volume));
362 if (sws->sws_uuid[0])
363 set_uuid(dev, sws->sws_uuid);
364 }
365 free(sws);
366
367 return 0;
368}
369
370static const char
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000371* const udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
Mike Frysinger38a33f92005-05-09 22:13:22 +0000372 "NSR03", "TEA01", 0 };
373
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000374static int probe_udf(int fd, blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000375 blkid_dev dev __BLKID_ATTR((unused)),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000376 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000377 unsigned char *buf __BLKID_ATTR((unused)))
378{
379 int j, bs;
380 struct iso_volume_descriptor isosb;
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000381 const char * const * m;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000382
383 /* determine the block size by scanning in 2K increments
384 (block sizes larger than 2K will be null padded) */
385 for (bs = 1; bs < 16; bs++) {
386 lseek(fd, bs*2048+32768, SEEK_SET);
387 if (read(fd, (char *)&isosb, sizeof(isosb)) != sizeof(isosb))
388 return 1;
389 if (isosb.id[0])
390 break;
391 }
392
393 /* Scan up to another 64 blocks looking for additional VSD's */
394 for (j = 1; j < 64; j++) {
395 if (j > 1) {
396 lseek(fd, j*bs*2048+32768, SEEK_SET);
397 if (read(fd, (char *)&isosb, sizeof(isosb))
398 != sizeof(isosb))
399 return 1;
400 }
401 /* If we find NSR0x then call it udf:
402 NSR01 for UDF 1.00
403 NSR02 for UDF 1.50
404 NSR03 for UDF 2.00 */
405 if (!strncmp(isosb.id, "NSR0", 4))
406 return 0;
407 for (m = udf_magic; *m; m++)
408 if (!strncmp(*m, isosb.id, 5))
409 break;
410 if (*m == 0)
411 return 1;
412 }
413 return 1;
414}
415
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000416static int probe_ocfs(int fd __BLKID_ATTR((unused)),
417 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000418 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000419 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000420 unsigned char *buf)
421{
422 struct ocfs_volume_header ovh;
423 struct ocfs_volume_label ovl;
424 __u32 major;
425
426 memcpy(&ovh, buf, sizeof(ovh));
427 memcpy(&ovl, buf+512, sizeof(ovl));
428
429 major = ocfsmajor(ovh);
430 if (major == 1)
431 blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
432 else if (major >= 9)
433 blkid_set_tag(dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000434
Eric Andersen3496fdc2006-01-30 23:09:20 +0000435 blkid_set_tag(dev, "LABEL", (const char*)ovl.label, ocfslabellen(ovl));
436 blkid_set_tag(dev, "MOUNT", (const char*)ovh.mount, ocfsmountlen(ovh));
Mike Frysinger38a33f92005-05-09 22:13:22 +0000437 set_uuid(dev, ovl.vol_id);
438 return 0;
439}
440
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000441static int probe_ocfs2(int fd __BLKID_ATTR((unused)),
442 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000443 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000444 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000445 unsigned char *buf)
446{
447 struct ocfs2_super_block *osb;
448
449 osb = (struct ocfs2_super_block *)buf;
450
Eric Andersen3496fdc2006-01-30 23:09:20 +0000451 blkid_set_tag(dev, "LABEL", (const char*)osb->s_label, sizeof(osb->s_label));
Mike Frysinger38a33f92005-05-09 22:13:22 +0000452 set_uuid(dev, osb->s_uuid);
453 return 0;
454}
455
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000456static int probe_oracleasm(int fd __BLKID_ATTR((unused)),
457 blkid_cache cache __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000458 blkid_dev dev,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000459 const struct blkid_magic *id __BLKID_ATTR((unused)),
Mike Frysinger38a33f92005-05-09 22:13:22 +0000460 unsigned char *buf)
461{
462 struct oracle_asm_disk_label *dl;
463
464 dl = (struct oracle_asm_disk_label *)buf;
465
466 blkid_set_tag(dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
467 return 0;
468}
469
470/*
471 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
472 * in the type_array table below + bim_kbalign.
473 *
474 * When probing for a lot of magics, we handle everything in 1kB buffers so
475 * that we don't have to worry about reading each combination of block sizes.
476 */
477#define BLKID_BLK_OFFS 64 /* currently reiserfs */
478
479/*
480 * Various filesystem magics that we can check for. Note that kboff and
481 * sboff are in kilobytes and bytes respectively. All magics are in
482 * byte strings so we don't worry about endian issues.
483 */
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000484static const struct blkid_magic type_array[] = {
Mike Frysinger38a33f92005-05-09 22:13:22 +0000485/* type kboff sboff len magic probe */
486 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
487 { "ntfs", 0, 3, 8, "NTFS ", 0 },
488 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
489 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
490 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
491 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
492 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
493 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
494 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
495 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
496 { "vfat", 0, 0x52, 5, "MSWIN", probe_vfat },
497 { "vfat", 0, 0x52, 8, "FAT32 ", probe_vfat },
498 { "vfat", 0, 0x36, 5, "MSDOS", probe_msdos },
499 { "vfat", 0, 0x36, 8, "FAT16 ", probe_msdos },
500 { "vfat", 0, 0x36, 8, "FAT12 ", probe_msdos },
501 { "minix", 1, 0x10, 2, "\177\023", 0 },
502 { "minix", 1, 0x10, 2, "\217\023", 0 },
503 { "minix", 1, 0x10, 2, "\150\044", 0 },
504 { "minix", 1, 0x10, 2, "\170\044", 0 },
505 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
506 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
507 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
508 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
Mike Frysinger874af852006-03-08 07:03:27 +0000509 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
Mike Frysinger38a33f92005-05-09 22:13:22 +0000510 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
511 { "udf", 32, 1, 5, "BEA01", probe_udf },
512 { "udf", 32, 1, 5, "BOOT2", probe_udf },
513 { "udf", 32, 1, 5, "CD001", probe_udf },
514 { "udf", 32, 1, 5, "CDW02", probe_udf },
515 { "udf", 32, 1, 5, "NSR02", probe_udf },
516 { "udf", 32, 1, 5, "NSR03", probe_udf },
517 { "udf", 32, 1, 5, "TEA01", probe_udf },
518 { "iso9660", 32, 1, 5, "CD001", 0 },
519 { "iso9660", 32, 9, 5, "CDROM", 0 },
520 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
521 { "hfs", 1, 0, 2, "BD", 0 },
522 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
523 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
524 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
525 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
526 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
527 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
528 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
529 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
530 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
531 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
532 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
533 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
534 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
535 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
536 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
537 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
538 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
539 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
540 { NULL, 0, 0, 0, NULL, NULL }
541};
542
543/*
544 * Verify that the data in dev is consistent with what is on the actual
545 * block device (using the devname field only). Normally this will be
546 * called when finding items in the cache, but for long running processes
547 * is also desirable to revalidate an item before use.
548 *
549 * If we are unable to revalidate the data, we return the old data and
550 * do not set the BLKID_BID_FL_VERIFIED flag on it.
551 */
552blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
553{
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000554 const struct blkid_magic *id;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000555 unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
556 const char *type;
557 struct stat st;
558 time_t diff, now;
559 int fd, idx;
560
561 if (!dev)
562 return NULL;
563
564 now = time(0);
565 diff = now - dev->bid_time;
566
567 if ((now < dev->bid_time) ||
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000568 (diff < BLKID_PROBE_MIN) ||
Mike Frysinger38a33f92005-05-09 22:13:22 +0000569 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
570 diff < BLKID_PROBE_INTERVAL))
571 return dev;
572
573 DBG(DEBUG_PROBE,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000574 printf("need to revalidate %s (time since last check %lu)\n",
Mike Frysinger38a33f92005-05-09 22:13:22 +0000575 dev->bid_name, diff));
576
577 if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
578 (fstat(fd, &st) < 0)) {
579 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
580 blkid_free_dev(dev);
581 return NULL;
582 }
583 /* We don't have read permission, just return cache data. */
584 DBG(DEBUG_PROBE,
585 printf("returning unverified data for %s\n",
586 dev->bid_name));
587 return dev;
588 }
589
590 memset(bufs, 0, sizeof(bufs));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000591
Mike Frysinger38a33f92005-05-09 22:13:22 +0000592 /*
593 * Iterate over the type array. If we already know the type,
594 * then try that first. If it doesn't work, then blow away
595 * the type information, and try again.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000596 *
Mike Frysinger38a33f92005-05-09 22:13:22 +0000597 */
598try_again:
599 type = 0;
600 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
601 uuid_t uuid;
602
603 if (check_mdraid(fd, uuid) == 0) {
604 set_uuid(dev, uuid);
605 type = "mdraid";
606 goto found_type;
607 }
608 }
609 for (id = type_array; id->bim_type; id++) {
610 if (dev->bid_type &&
611 strcmp(id->bim_type, dev->bid_type))
612 continue;
613
614 idx = id->bim_kboff + (id->bim_sboff >> 10);
615 if (idx > BLKID_BLK_OFFS || idx < 0)
616 continue;
617 buf = bufs[idx];
618 if (!buf) {
619 if (lseek(fd, idx << 10, SEEK_SET) < 0)
620 continue;
621
Mike Frysinger7fde8de2005-06-11 22:37:25 +0000622 buf = (unsigned char *)xmalloc(1024);
623
Mike Frysinger38a33f92005-05-09 22:13:22 +0000624 if (read(fd, buf, 1024) != 1024) {
625 free(buf);
626 continue;
627 }
628 bufs[idx] = buf;
629 }
630
631 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
632 id->bim_len))
633 continue;
634
635 if ((id->bim_probe == NULL) ||
636 (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
637 type = id->bim_type;
638 goto found_type;
639 }
640 }
641
642 if (!id->bim_type && dev->bid_type) {
643 /*
644 * Zap the device filesystem type and try again
645 */
646 blkid_set_tag(dev, "TYPE", 0, 0);
647 blkid_set_tag(dev, "SEC_TYPE", 0, 0);
648 blkid_set_tag(dev, "LABEL", 0, 0);
649 blkid_set_tag(dev, "UUID", 0, 0);
650 goto try_again;
651 }
652
653 if (!dev->bid_type) {
654 blkid_free_dev(dev);
655 return NULL;
656 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000657
Mike Frysinger38a33f92005-05-09 22:13:22 +0000658found_type:
659 if (dev && type) {
660 dev->bid_devno = st.st_rdev;
661 dev->bid_time = time(0);
662 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
663 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
664
665 blkid_set_tag(dev, "TYPE", type, 0);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000666
Mike Frysinger874af852006-03-08 07:03:27 +0000667 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
Mike Frysinger38a33f92005-05-09 22:13:22 +0000668 dev->bid_name, st.st_rdev, type));
669 }
670
671 close(fd);
672
673 return dev;
674}
675
676int blkid_known_fstype(const char *fstype)
677{
"Vladimir N. Oleynik"c961a442005-10-15 14:41:08 +0000678 const struct blkid_magic *id;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000679
680 for (id = type_array; id->bim_type; id++) {
681 if (strcmp(fstype, id->bim_type) == 0)
682 return 1;
683 }
684 return 0;
685}
686
687#ifdef TEST_PROGRAM
688int main(int argc, char **argv)
689{
690 blkid_dev dev;
691 blkid_cache cache;
692 int ret;
693
694 blkid_debug_mask = DEBUG_ALL;
695 if (argc != 2) {
696 fprintf(stderr, "Usage: %s device\n"
697 "Probe a single device to determine type\n", argv[0]);
698 exit(1);
699 }
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000700 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
Mike Frysinger38a33f92005-05-09 22:13:22 +0000701 fprintf(stderr, "%s: error creating cache (%d)\n",
702 argv[0], ret);
703 exit(1);
704 }
705 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
706 if (!dev) {
707 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
708 return (1);
709 }
710 printf("%s is type %s\n", argv[1], dev->bid_type ?
711 dev->bid_type : "(null)");
712 if (dev->bid_label)
713 printf("\tlabel is '%s'\n", dev->bid_label);
714 if (dev->bid_uuid)
715 printf("\tuuid is %s\n", dev->bid_uuid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000716
Mike Frysinger38a33f92005-05-09 22:13:22 +0000717 blkid_free_dev(dev);
718 return (0);
719}
720#endif