probe.c 19 KB

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