blob: 48edb9560ace44ceb6ea3371901f02e49ad003ae [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
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
Theodore Ts'o50b380b2003-02-12 23:51:21 -05006 * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05007 * 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
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050030#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050031#include "uuid/uuid.h"
32#include "probe.h"
33
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050034/*
Theodore Ts'o50b380b2003-02-12 23:51:21 -050035 * 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.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050038 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -050039static int check_mdraid(int fd, unsigned char *ret_uuid)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050040{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050041 struct mdp_superblock_s *md;
42 blkid_loff_t offset;
43 char buf[4096];
44
45 if (fd < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050046 return -BLKID_ERR_PARAM;
47
Theodore Ts'o50b380b2003-02-12 23:51:21 -050048 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050049
50 if (blkid_llseek(fd, offset, 0) < 0 ||
Theodore Ts'o50b380b2003-02-12 23:51:21 -050051 read(fd, buf, 4096) != 4096)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050052 return -BLKID_ERR_IO;
53
Theodore Ts'o50b380b2003-02-12 23:51:21 -050054 /* Check for magic number */
55 if (memcmp("\251+N\374", buf, 4))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050056 return -BLKID_ERR_PARAM;
57
Theodore Ts'o50b380b2003-02-12 23:51:21 -050058 if (!ret_uuid)
59 return 0;
60 *ret_uuid = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050061
Theodore Ts'o50b380b2003-02-12 23:51:21 -050062 /* 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);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050067 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050068 return 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050069}
70
Theodore Ts'o50b380b2003-02-12 23:51:21 -050071static void set_uuid(blkid_dev dev, uuid_t uuid)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050072{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050073 char str[37];
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050074
Theodore Ts'o50b380b2003-02-12 23:51:21 -050075 if (!uuid_is_null(uuid)) {
76 uuid_unparse(uuid, str);
Theodore Ts'o79dd2342003-02-22 17:15:20 -050077 blkid_set_tag(dev, "UUID", str, sizeof(str));
Theodore Ts'o50b380b2003-02-12 23:51:21 -050078 }
79}
80
Theodore Ts'o54434922003-12-07 01:28:50 -050081static int probe_ext2(int fd __BLKID_ATTR((unused)),
82 blkid_cache cache __BLKID_ATTR((unused)),
83 blkid_dev dev,
Theodore Ts'o79dd2342003-02-22 17:15:20 -050084 struct blkid_magic *id, unsigned char *buf)
Theodore Ts'o50b380b2003-02-12 23:51:21 -050085{
86 struct ext2_super_block *es;
Theodore Ts'oa30b9442003-07-20 11:22:34 -040087 const char *sec_type = 0, *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050088
89 es = (struct ext2_super_block *)buf;
90
Theodore Ts'of0a22d02003-02-22 13:19:53 -050091 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050092 blkid_le32(es->s_feature_compat),
93 blkid_le32(es->s_feature_incompat),
94 blkid_le32(es->s_feature_ro_compat)));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050095
Theodore Ts'o79dd2342003-02-22 17:15:20 -050096 /* Distinguish between jbd and ext2/3 fs */
97 if (id && (blkid_le32(es->s_feature_incompat) &
98 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050099 return -BLKID_ERR_PARAM;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500100
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500101 if (strlen(es->s_volume_name))
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400102 label = es->s_volume_name;
103 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500104
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500105 set_uuid(dev, es->s_uuid);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500106
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500107 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
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500113 return 0;
114}
115
Theodore Ts'o54434922003-12-07 01:28:50 -0500116static 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)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500121{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500122 struct ext2_super_block *es = (struct ext2_super_block *) buf;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500123
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500124 if (!(blkid_le32(es->s_feature_incompat) &
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500125 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500126 return -BLKID_ERR_PARAM;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500127
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500128 return (probe_ext2(fd, cache, dev, 0, buf));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500129}
130
Theodore Ts'o54434922003-12-07 01:28:50 -0500131static 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)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500136{
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500137 struct vfat_super_block *vs;
138 char serno[10];
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400139 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500140
141 vs = (struct vfat_super_block *)buf;
142
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500143 if (strncmp(vs->vs_label, "NO NAME", 7)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500144 char *end = vs->vs_label + sizeof(vs->vs_label) - 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500145
146 while (*end == ' ' && end >= vs->vs_label)
147 --end;
148 if (end >= vs->vs_label)
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400149 label = vs->vs_label;
150 blkid_set_tag(dev, "LABEL", label, end - vs->vs_label + 1);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500151 }
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]);
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500156 blkid_set_tag(dev, "UUID", serno, sizeof(serno));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500157
158 return 0;
159}
160
Theodore Ts'o54434922003-12-07 01:28:50 -0500161static 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)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500166{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500167 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500168 char serno[10];
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400169 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500170
171 if (strncmp(ms->ms_label, "NO NAME", 7)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500172 char *end = ms->ms_label + sizeof(ms->ms_label) - 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500173
174 while (*end == ' ' && end >= ms->ms_label)
175 --end;
176 if (end >= ms->ms_label)
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400177 label = ms->ms_label;
178 blkid_set_tag(dev, "LABEL", label, end - ms->ms_label + 1);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500179 }
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]);
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500184 blkid_set_tag(dev, "UUID", serno, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500185
186 return 0;
187}
188
Theodore Ts'o54434922003-12-07 01:28:50 -0500189static 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)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500194{
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500195 struct xfs_super_block *xs;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400196 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500197
198 xs = (struct xfs_super_block *)buf;
199
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500200 if (strlen(xs->xs_fname))
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400201 label = xs->xs_fname;
202 blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname));
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500203 set_uuid(dev, xs->xs_uuid);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500204 return 0;
205}
206
Theodore Ts'o54434922003-12-07 01:28:50 -0500207static int probe_reiserfs(int fd __BLKID_ATTR((unused)),
208 blkid_cache cache __BLKID_ATTR((unused)),
209 blkid_dev dev,
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500210 struct blkid_magic *id, unsigned char *buf)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500211{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500212 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500213 unsigned int blocksize;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400214 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500215
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500216 blocksize = blkid_le16(rs->rs_blocksize);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500217
218 /* If the superblock is inside the journal, we have the wrong one */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500219 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500220 return -BLKID_ERR_BIG;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500221
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")) {
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400225 if (strlen(rs->rs_label))
226 label = rs->rs_label;
227 blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label));
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500228 set_uuid(dev, rs->rs_uuid);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500229 }
230
231 return 0;
232}
233
Theodore Ts'o54434922003-12-07 01:28:50 -0500234static 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)
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500239{
240 struct jfs_super_block *js;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400241 const char *label = 0;
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500242
243 js = (struct jfs_super_block *)buf;
244
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400245 if (strlen((char *) js->js_label))
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400246 label = (char *) js->js_label;
247 blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label));
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500248 set_uuid(dev, js->js_uuid);
249 return 0;
250}
251
Theodore Ts'o54434922003-12-07 01:28:50 -0500252static 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)
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500257{
258 struct romfs_super_block *ros;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400259 const char *label = 0;
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500260
261 ros = (struct romfs_super_block *)buf;
262
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400263 if (strlen((char *) ros->ros_volume))
264 label = (char *) ros->ros_volume;
265 blkid_set_tag(dev, "LABEL", label, strlen(label));
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500266 return 0;
267}
268
Theodore Ts'o54434922003-12-07 01:28:50 -0500269static const char
Theodore Ts'o3de5bf62003-07-22 01:06:36 -0400270*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
271 "NSR03", "TEA01", 0 };
272
Theodore Ts'o54434922003-12-07 01:28:50 -0500273static 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)))
Theodore Ts'o3de5bf62003-07-22 01:06:36 -0400277{
278 int j, bs;
279 struct iso_volume_descriptor isosb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500280 const char ** m;
Theodore Ts'o3de5bf62003-07-22 01:06:36 -0400281
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
Theodore Ts'o9387c282004-03-04 19:59:58 -0500315static 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;
Theodore Ts'o3838f7d2004-11-30 19:05:38 -0500323 __u32 major;
Theodore Ts'o9387c282004-03-04 19:59:58 -0500324
325 memcpy(&ovh, buf, sizeof(ovh));
326 memcpy(&ovl, buf+512, sizeof(ovl));
327
328 major = ocfsmajor(ovh);
329 if (major == 1)
Theodore Ts'o4b8f81a2004-11-19 14:28:01 -0500330 blkid_set_tag(dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
Theodore Ts'o9387c282004-03-04 19:59:58 -0500331 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
Theodore Ts'o414846b2004-09-17 21:37:49 -0400340static 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
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500355/*
356 * BLKID_BLK_OFFS is at least as large as the highest bim_kboff defined
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500357 * 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.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500361 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500362#define BLKID_BLK_OFFS 64 /* currently reiserfs */
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500363
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 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500369static struct blkid_magic type_array[] = {
370/* type kboff sboff len magic probe */
371 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500372 { "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 },
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500390 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500391 { "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 },
Theodore Ts'o3de5bf62003-07-22 01:06:36 -0400394 { "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 },
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500401 { "iso9660", 32, 1, 5, "CD001", 0 },
402 { "iso9660", 32, 9, 5, "CDROM", 0 },
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500403 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500404 { "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 },
Theodore Ts'o414846b2004-09-17 21:37:49 -0400414 { "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 },
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500419 { NULL, 0, 0, 0, NULL, NULL }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500420};
421
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500422/*
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500423 * 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 */
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500431blkid_dev blkid_verify_devname(blkid_cache cache, blkid_dev dev)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500432{
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500433 struct blkid_magic *id;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500434 unsigned char *bufs[BLKID_BLK_OFFS + 1], *buf;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500435 const char *type;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500436 struct stat st;
Theodore Ts'o7ce08062004-04-19 21:42:18 -0400437 time_t diff, now;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500438 int fd, idx;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500439
440 if (!dev)
441 return NULL;
442
Theodore Ts'o7ce08062004-04-19 21:42:18 -0400443 now = time(0);
444 diff = now - dev->bid_time;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500445
Theodore Ts'o7ce08062004-04-19 21:42:18 -0400446 if ((now < dev->bid_time) ||
447 (diff < BLKID_PROBE_MIN) ||
448 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
449 diff < BLKID_PROBE_INTERVAL))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500450 return dev;
451
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500452 DBG(DEBUG_PROBE,
453 printf("need to revalidate %s (time since last check %lu)\n",
Theodore Ts'oce72b862003-02-14 01:31:45 -0500454 dev->bid_name, diff));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500455
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500456 if (((fd = open(dev->bid_name, O_RDONLY)) < 0) ||
457 (fstat(fd, &st) < 0) || !S_ISBLK(st.st_mode)) {
Theodore Ts'oce72b862003-02-14 01:31:45 -0500458 if (errno == ENXIO || errno == ENODEV || errno == ENOENT) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500459 blkid_free_dev(dev);
460 return NULL;
461 }
462 /* We don't have read permission, just return cache data. */
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500463 DBG(DEBUG_PROBE,
464 printf("returning unverified data for %s\n",
465 dev->bid_name));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500466 return dev;
467 }
468
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500469 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 */
477try_again:
478 type = 0;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500479 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
480 uuid_t uuid;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500481
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500482 if (check_mdraid(fd, uuid) == 0) {
483 set_uuid(dev, uuid);
484 type = "mdraid";
485 goto found_type;
486 }
487 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500488 for (id = type_array; id->bim_type; id++) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500489 if (dev->bid_type &&
490 strcmp(id->bim_type, dev->bid_type))
491 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500492
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500493 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;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500507 }
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500508 bufs[idx] = buf;
509 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500510
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500511 if (memcmp(id->bim_magic, buf + (id->bim_sboff&0x3ff),
512 id->bim_len))
513 continue;
514
515 if ((id->bim_probe == NULL) ||
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500516 (id->bim_probe(fd, cache, dev, id, buf) == 0)) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500517 type = id->bim_type;
518 goto found_type;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500519 }
520 }
521
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500522 if (!id->bim_type && dev->bid_type) {
523 /*
524 * Zap the device filesystem type and try again
525 */
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500526 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);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500530 goto try_again;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500531 }
532
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500533 if (!dev->bid_type) {
534 blkid_free_dev(dev);
535 return NULL;
536 }
537
538found_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;
Theodore Ts'oce72b862003-02-14 01:31:45 -0500543 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500544
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500545 blkid_set_tag(dev, "TYPE", type, 0);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500546
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500547 DBG(DEBUG_PROBE, printf("%s: devno 0x%04Lx, type %s\n",
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500548 dev->bid_name, st.st_rdev, type));
549 }
550
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500551 close(fd);
552
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500553 return dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500554}
555
Theodore Ts'o78e2edf2003-07-21 19:42:19 -0400556int 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
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500567#ifdef TEST_PROGRAM
568int main(int argc, char **argv)
569{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500570 blkid_dev dev;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500571 blkid_cache cache;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500572 int ret;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500573
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500574 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500575 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 }
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500580 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 }
Theodore Ts'o98999c32003-02-16 00:47:07 -0500585 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500586 if (!dev) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500587 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500588 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);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500598 return (0);
599}
600#endif