blob: b8922e3b497b652fbfd2e4e7d3edbd992d0c7054 [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
Theodore Ts'o2f79e512005-09-10 21:51:20 -04008 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05009 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the
12 * GNU Lesser General Public License.
13 * %End-Header%
14 */
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <fcntl.h>
Theodore Ts'o3a538e42008-02-27 02:06:08 -050021#include <ctype.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050022#include <sys/types.h>
23#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#ifdef HAVE_SYS_MKDEV_H
27#include <sys/mkdev.h>
28#endif
Eric Sandeen18f73432008-10-08 13:34:09 -050029#ifdef __linux__
Theodore Ts'ocfc19312008-02-08 16:04:12 -050030#include <sys/utsname.h>
Eric Sandeen18f73432008-10-08 13:34:09 -050031#endif
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050032#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050035#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050036#include "uuid/uuid.h"
37#include "probe.h"
38
Steve Kondik9284fd22013-07-06 03:07:31 -070039extern int probe_exfat(struct blkid_probe *probe,
40 struct blkid_magic *id __BLKID_ATTR((unused)),
41 unsigned char *buf);
42
Theodore Ts'o45a3fa82005-09-10 15:46:57 -040043static int figure_label_len(const unsigned char *label, int len)
44{
45 const unsigned char *end = label + len - 1;
46
47 while ((*end == ' ' || *end == 0) && end >= label)
48 --end;
JP Abgralle0ed7402014-03-19 19:08:39 -070049 if (end >= label)
Theodore Ts'o45a3fa82005-09-10 15:46:57 -040050 return end - label + 1;
Theodore Ts'o45a3fa82005-09-10 15:46:57 -040051 return 0;
52}
53
Theodore Ts'oefc6f622008-08-27 23:07:54 -040054static unsigned char *get_buffer(struct blkid_probe *pr,
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -040055 blkid_loff_t off, size_t len)
Theodore Ts'oca749852005-09-10 21:07:23 -040056{
57 ssize_t ret_read;
58 unsigned char *newbuf;
59
60 if (off + len <= SB_BUFFER_SIZE) {
61 if (!pr->sbbuf) {
62 pr->sbbuf = malloc(SB_BUFFER_SIZE);
63 if (!pr->sbbuf)
64 return NULL;
65 if (lseek(pr->fd, 0, SEEK_SET) < 0)
66 return NULL;
67 ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
68 if (ret_read < 0)
69 ret_read = 0;
70 pr->sb_valid = ret_read;
71 }
72 if (off+len > pr->sb_valid)
73 return NULL;
74 return pr->sbbuf + off;
75 } else {
76 if (len > pr->buf_max) {
77 newbuf = realloc(pr->buf, len);
78 if (newbuf == NULL)
79 return NULL;
80 pr->buf = newbuf;
81 pr->buf_max = len;
82 }
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -040083 if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
Theodore Ts'oca749852005-09-10 21:07:23 -040084 return NULL;
85 ret_read = read(pr->fd, pr->buf, len);
86 if (ret_read != (ssize_t) len)
87 return NULL;
88 return pr->buf;
89 }
90}
91
Steve Kondik9284fd22013-07-06 03:07:31 -070092unsigned char *blkid_probe_get_buffer(struct blkid_probe *pr,
93 blkid_loff_t off, size_t len)
94{
95 return get_buffer(pr, off, len);
96}
Theodore Ts'oca749852005-09-10 21:07:23 -040097
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050098/*
Theodore Ts'o50b380b2003-02-12 23:51:21 -050099 * This is a special case code to check for an MDRAID device. We do
100 * this special since it requires checking for a superblock at the end
101 * of the device.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500102 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500103static int check_mdraid(int fd, unsigned char *ret_uuid)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500104{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500105 struct mdp_superblock_s *md;
106 blkid_loff_t offset;
107 char buf[4096];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500109 if (fd < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500110 return -BLKID_ERR_PARAM;
111
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500112 offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500113
114 if (blkid_llseek(fd, offset, 0) < 0 ||
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500115 read(fd, buf, 4096) != 4096)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500116 return -BLKID_ERR_IO;
117
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500118 /* Check for magic number */
Sergey Vlasov6b8be162007-04-10 11:59:46 -0400119 if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500120 return -BLKID_ERR_PARAM;
121
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500122 if (!ret_uuid)
123 return 0;
124 *ret_uuid = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500125
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500126 /* The MD UUID is not contiguous in the superblock, make it so */
127 md = (struct mdp_superblock_s *)buf;
128 if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
129 memcpy(ret_uuid, &md->set_uuid0, 4);
Sergey Vlasovc9105ff2007-04-10 12:00:38 -0400130 memcpy(ret_uuid + 4, &md->set_uuid1, 12);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500131 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500132 return 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500133}
134
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -0400135static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500136{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500137 char str[37];
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500138
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500139 if (!uuid_is_null(uuid)) {
140 uuid_unparse(uuid, str);
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500141 blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500142 }
143}
144
Theodore Ts'o29213322008-01-26 22:25:50 -0500145static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
146 unsigned char *buf)
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500147{
148 struct ext2_super_block *es = (struct ext2_super_block *) buf;
149 const char *label = 0;
150
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400151 DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500152 blkid_le32(es->s_feature_compat),
153 blkid_le32(es->s_feature_incompat),
154 blkid_le32(es->s_feature_ro_compat)));
155
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500156 if (strlen(es->s_volume_name))
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500157 label = es->s_volume_name;
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500158 blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500159
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500160 set_uuid(dev, es->s_uuid, 0);
Theodore Ts'o29213322008-01-26 22:25:50 -0500161
162 if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
163 !uuid_is_null(es->s_journal_uuid))
164 set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
165
166 if (strcmp(id->bim_type, "ext2") &&
167 ((blkid_le32(es->s_feature_incompat) &
168 EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
169 blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500170}
171
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500172/*
173 * Check to see if a filesystem is in /proc/filesystems.
174 * Returns 1 if found, 0 if not
175 */
Theodore Ts'o3a538e42008-02-27 02:06:08 -0500176static int fs_proc_check(const char *fs_name)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500177{
178 FILE *f;
179 char buf[80], *cp, *t;
180
181 f = fopen("/proc/filesystems", "r");
182 if (!f)
183 return (0);
184 while (!feof(f)) {
185 if (!fgets(buf, sizeof(buf), f))
186 break;
187 cp = buf;
188 if (!isspace(*cp)) {
189 while (*cp && !isspace(*cp))
190 cp++;
191 }
192 while (*cp && isspace(*cp))
193 cp++;
194 if ((t = strchr(cp, '\n')) != NULL)
195 *t = 0;
196 if ((t = strchr(cp, '\t')) != NULL)
197 *t = 0;
198 if ((t = strchr(cp, ' ')) != NULL)
199 *t = 0;
200 if (!strcmp(fs_name, cp)) {
201 fclose(f);
202 return (1);
203 }
204 }
205 fclose(f);
206 return (0);
207}
208
209/*
210 * Check to see if a filesystem is available as a module
211 * Returns 1 if found, 0 if not
212 */
Theodore Ts'o3a538e42008-02-27 02:06:08 -0500213static int check_for_modules(const char *fs_name)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500214{
Eric Sandeen18f73432008-10-08 13:34:09 -0500215#ifdef __linux__
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500216 struct utsname uts;
217 FILE *f;
Karel Zak249c9622009-08-23 21:13:56 +0200218 char buf[1024], *cp;
219 int namesz;
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500220
221 if (uname(&uts))
222 return (0);
223 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
224
225 f = fopen(buf, "r");
226 if (!f)
227 return (0);
Karel Zak249c9622009-08-23 21:13:56 +0200228
229 namesz = strlen(fs_name);
230
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500231 while (!feof(f)) {
232 if (!fgets(buf, sizeof(buf), f))
233 break;
234 if ((cp = strchr(buf, ':')) != NULL)
235 *cp = 0;
236 else
237 continue;
238 if ((cp = strrchr(buf, '/')) != NULL)
239 cp++;
Eric Sandeenf2fe5da2009-02-24 15:13:39 -0600240 else
241 cp = buf;
Karel Zak249c9622009-08-23 21:13:56 +0200242 if (!strncmp(cp, fs_name, namesz) &&
243 (!strcmp(cp + namesz, ".ko") ||
244 !strcmp(cp + namesz, ".ko.gz"))) {
Theodore Ts'o6964a172008-10-11 11:07:23 -0400245 fclose(f);
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500246 return (1);
Theodore Ts'o6964a172008-10-11 11:07:23 -0400247 }
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500248 }
249 fclose(f);
Eric Sandeen18f73432008-10-08 13:34:09 -0500250#endif
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500251 return (0);
252}
253
Theodore Ts'o13618212009-03-08 18:56:41 -0400254static int linux_version_code()
255{
256#ifdef __linux__
257 struct utsname ut;
JP Abgralle0ed7402014-03-19 19:08:39 -0700258 static int version_code = -1;
Theodore Ts'o13618212009-03-08 18:56:41 -0400259 int major, minor, rev;
260 char *endptr;
261 const char *cp;
262
263 if (version_code > 0)
264 return version_code;
265
266 if (uname(&ut))
267 return 0;
268 cp = ut.release;
269
270 major = strtol(cp, &endptr, 10);
271 if (cp == endptr || *endptr != '.')
272 return 0;
273 cp = endptr + 1;
274 minor = strtol(cp, &endptr, 10);
275 if (cp == endptr || *endptr != '.')
276 return 0;
277 cp = endptr + 1;
278 rev = strtol(cp, &endptr, 10);
279 if (cp == endptr)
280 return 0;
281 version_code = (((major * 256) + minor) * 256) + rev;
282 return version_code;
283#else
284 return 0;
285#endif
286}
287
288#define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29)
289
290static int system_supports_ext2(void)
291{
292 static time_t last_check = 0;
293 static int ret = -1;
294 time_t now = time(0);
295
296 if (ret != -1 || (now - last_check) < 5)
297 return ret;
298 last_check = now;
299 ret = (fs_proc_check("ext2") || check_for_modules("ext2"));
300 return ret;
301}
302
Theodore Ts'o3a538e42008-02-27 02:06:08 -0500303static int system_supports_ext4(void)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500304{
305 static time_t last_check = 0;
306 static int ret = -1;
307 time_t now = time(0);
308
Eric Sandeen18f73432008-10-08 13:34:09 -0500309 if (ret != -1 || (now - last_check) < 5)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500310 return ret;
311 last_check = now;
312 ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
313 return ret;
314}
315
Theodore Ts'o3a538e42008-02-27 02:06:08 -0500316static int system_supports_ext4dev(void)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500317{
318 static time_t last_check = 0;
319 static int ret = -1;
320 time_t now = time(0);
321
Eric Sandeen18f73432008-10-08 13:34:09 -0500322 if (ret != -1 || (now - last_check) < 5)
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500323 return ret;
324 last_check = now;
325 ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
326 return ret;
327}
328
Theodore Ts'o29213322008-01-26 22:25:50 -0500329static int probe_ext4dev(struct blkid_probe *probe,
330 struct blkid_magic *id,
331 unsigned char *buf)
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500332{
333 struct ext2_super_block *es;
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500334 es = (struct ext2_super_block *)buf;
335
Theodore Ts'o29213322008-01-26 22:25:50 -0500336 /* Distinguish from jbd */
337 if (blkid_le32(es->s_feature_incompat) &
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500338 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
339 return -BLKID_ERR_PARAM;
340
Theodore Ts'o13618212009-03-08 18:56:41 -0400341 /*
342 * If the filesystem does not have a journal and ext2 and ext4
343 * is not present, then force this to be detected as an
344 * ext4dev filesystem.
345 */
346 if (!(blkid_le32(es->s_feature_compat) &
347 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
348 !system_supports_ext2() && !system_supports_ext4() &&
349 system_supports_ext4dev() &&
350 linux_version_code() >= EXT4_SUPPORTS_EXT2)
351 goto force_ext4dev;
352
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500353 /*
354 * If the filesystem is marked as OK for use by in-development
355 * filesystem code, but ext4dev is not supported, and ext4 is,
356 * then don't call ourselves ext4dev, since we should be
357 * detected as ext4 in that case.
358 *
359 * If the filesystem is marked as in use by production
360 * filesystem, then it can only be used by ext4 and NOT by
361 * ext4dev, so always disclaim we are ext4dev in that case.
362 */
363 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
364 if (!system_supports_ext4dev() && system_supports_ext4())
365 return -BLKID_ERR_PARAM;
366 } else
367 return -BLKID_ERR_PARAM;
368
Theodore Ts'o13618212009-03-08 18:56:41 -0400369force_ext4dev:
Theodore Ts'o29213322008-01-26 22:25:50 -0500370 get_ext2_info(probe->dev, id, buf);
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500371 return 0;
372}
373
Theodore Ts'o29213322008-01-26 22:25:50 -0500374static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
375 unsigned char *buf)
376{
377 struct ext2_super_block *es;
378 es = (struct ext2_super_block *)buf;
379
Theodore Ts'o29213322008-01-26 22:25:50 -0500380 /* Distinguish from jbd */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400381 if (blkid_le32(es->s_feature_incompat) &
Theodore Ts'o29213322008-01-26 22:25:50 -0500382 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
383 return -BLKID_ERR_PARAM;
384
Theodore Ts'o13618212009-03-08 18:56:41 -0400385 /*
386 * If the filesystem does not have a journal and ext2 is not
387 * present, then force this to be detected as an ext2
388 * filesystem.
389 */
390 if (!(blkid_le32(es->s_feature_compat) &
391 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
392 !system_supports_ext2() && system_supports_ext4() &&
393 linux_version_code() >= EXT4_SUPPORTS_EXT2)
394 goto force_ext4;
395
Theodore Ts'o29213322008-01-26 22:25:50 -0500396 /* Ext4 has at least one feature which ext3 doesn't understand */
397 if (!(blkid_le32(es->s_feature_ro_compat) &
398 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
399 !(blkid_le32(es->s_feature_incompat) &
400 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
401 return -BLKID_ERR_PARAM;
402
Theodore Ts'o13618212009-03-08 18:56:41 -0400403force_ext4:
Theodore Ts'ocfc19312008-02-08 16:04:12 -0500404 /*
405 * If the filesystem is a OK for use by in-development
406 * filesystem code, and ext4dev is supported or ext4 is not
407 * supported, then don't call ourselves ext4, so we can redo
408 * the detection and mark the filesystem as ext4dev.
409 *
410 * If the filesystem is marked as in use by production
411 * filesystem, then it can only be used by ext4 and NOT by
412 * ext4dev.
413 */
414 if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
415 if (system_supports_ext4dev() || !system_supports_ext4())
416 return -BLKID_ERR_PARAM;
417 }
Theodore Ts'o29213322008-01-26 22:25:50 -0500418 get_ext2_info(probe->dev, id, buf);
419 return 0;
420}
421
422static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
423 unsigned char *buf)
424{
425 struct ext2_super_block *es;
426 es = (struct ext2_super_block *)buf;
427
Theodore Ts'o29213322008-01-26 22:25:50 -0500428 /* ext3 requires journal */
429 if (!(blkid_le32(es->s_feature_compat) &
430 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
431 return -BLKID_ERR_PARAM;
432
433 /* Any features which ext3 doesn't understand */
434 if ((blkid_le32(es->s_feature_ro_compat) &
435 EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
436 (blkid_le32(es->s_feature_incompat) &
437 EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
438 return -BLKID_ERR_PARAM;
439
440 get_ext2_info(probe->dev, id, buf);
441 return 0;
442}
443
444static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
Theodore Ts'o12b3c8e2005-05-07 14:38:10 -0400445 unsigned char *buf)
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500446{
447 struct ext2_super_block *es;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500448
449 es = (struct ext2_super_block *)buf;
450
Karel Zak05a6edf2005-09-06 06:26:45 -0400451 /* Distinguish between ext3 and ext2 */
452 if ((blkid_le32(es->s_feature_compat) &
453 EXT3_FEATURE_COMPAT_HAS_JOURNAL))
454 return -BLKID_ERR_PARAM;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500455
Theodore Ts'o29213322008-01-26 22:25:50 -0500456 /* Any features which ext2 doesn't understand */
457 if ((blkid_le32(es->s_feature_ro_compat) &
458 EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
459 (blkid_le32(es->s_feature_incompat) &
460 EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
461 return -BLKID_ERR_PARAM;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500462
Theodore Ts'o13618212009-03-08 18:56:41 -0400463 /*
464 * If ext2 is not present, but ext4 or ext4dev are, then
465 * disclaim we are ext2
466 */
467 if (!system_supports_ext2() &&
468 (system_supports_ext4() || system_supports_ext4dev()) &&
469 linux_version_code() >= EXT4_SUPPORTS_EXT2)
470 return -BLKID_ERR_PARAM;
471
Theodore Ts'o29213322008-01-26 22:25:50 -0500472 get_ext2_info(probe->dev, id, buf);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500473 return 0;
474}
475
Theodore Ts'o29213322008-01-26 22:25:50 -0500476static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
Theodore Ts'o54434922003-12-07 01:28:50 -0500477 unsigned char *buf)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500478{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500479 struct ext2_super_block *es = (struct ext2_super_block *) buf;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500480
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500481 if (!(blkid_le32(es->s_feature_incompat) &
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500482 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500483 return -BLKID_ERR_PARAM;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500484
Theodore Ts'o29213322008-01-26 22:25:50 -0500485 get_ext2_info(probe->dev, id, buf);
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -0500486
487 return 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500488}
489
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400490#define FAT_ATTR_VOLUME_ID 0x08
491#define FAT_ATTR_DIR 0x10
492#define FAT_ATTR_LONG_NAME 0x0f
493#define FAT_ATTR_MASK 0x3f
494#define FAT_ENTRY_FREE 0xe5
495
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -0400496static const char *no_name = "NO NAME ";
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400497
498static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
499{
Matthias Andree12a829d2006-05-30 01:48:51 +0200500 int i;
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400501
502 for (i = 0; i < count; i++) {
503 if (dir[i].name[0] == 0x00)
504 break;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400505
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400506 if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
507 (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
508 ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
509 continue;
510
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400511 if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400512 FAT_ATTR_VOLUME_ID) {
513 return dir[i].name;
514 }
515 }
516 return 0;
517}
518
519/* FAT label extraction from the root directory taken from Kay
520 * Sievers's volume_id library */
Theodore Ts'oca749852005-09-10 21:07:23 -0400521static int probe_fat(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400522 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500523 unsigned char *buf)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500524{
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400525 struct vfat_super_block *vs = (struct vfat_super_block *) buf;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500526 struct msdos_super_block *ms = (struct msdos_super_block *) buf;
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400527 struct vfat_dir_entry *dir;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500528 char serno[10];
Matthias Andree94fa1102006-05-30 15:47:12 +0200529 const unsigned char *label = 0, *vol_label = 0, *tmp;
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400530 unsigned char *vol_serno;
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400531 int label_len = 0, maxloop = 100;
532 __u16 sector_size, dir_entries, reserved;
533 __u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
534 __u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500535
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400536 /* sector size check */
Matthias Andreeaadac9b2006-05-30 16:29:49 +0200537 tmp = (unsigned char *)&ms->ms_sector_size;
538 sector_size = tmp[0] + (tmp[1] << 8);
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400539 if (sector_size != 0x200 && sector_size != 0x400 &&
540 sector_size != 0x800 && sector_size != 0x1000)
541 return 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500542
Matthias Andreeaadac9b2006-05-30 16:29:49 +0200543 tmp = (unsigned char *)&ms->ms_dir_entries;
544 dir_entries = tmp[0] + (tmp[1] << 8);
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400545 reserved = blkid_le16(ms->ms_reserved);
Matthias Andreeaadac9b2006-05-30 16:29:49 +0200546 tmp = (unsigned char *)&ms->ms_sectors;
547 sect_count = tmp[0] + (tmp[1] << 8);
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400548 if (sect_count == 0)
549 sect_count = blkid_le32(ms->ms_total_sect);
550
551 fat_length = blkid_le16(ms->ms_fat_length);
552 if (fat_length == 0)
553 fat_length = blkid_le32(vs->vs_fat32_length);
554
555 fat_size = fat_length * ms->ms_fats;
556 dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
557 (sector_size-1)) / sector_size;
558
559 cluster_count = sect_count - (reserved + fat_size + dir_size);
Theodore Ts'o20c10a72007-12-15 22:21:31 -0500560 if (ms->ms_cluster_size == 0)
561 return 1;
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400562 cluster_count /= ms->ms_cluster_size;
563
564 if (cluster_count > FAT32_MAX)
565 return 1;
566
567 if (ms->ms_fat_length) {
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400568 /* the label may be an attribute in the root directory */
569 root_start = (reserved + fat_size) * sector_size;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400570 root_dir_entries = vs->vs_dir_entries[0] +
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400571 (vs->vs_dir_entries[1] << 8);
572
573 buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400574 dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400575 buf_size);
576 if (dir)
577 vol_label = search_fat_label(dir, root_dir_entries);
578
579 if (!vol_label || !memcmp(vol_label, no_name, 11))
580 vol_label = ms->ms_label;
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400581 vol_serno = ms->ms_serno;
582
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400583 blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400584 sizeof("msdos"));
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400585 } else {
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400586 /* Search the FAT32 root dir for the label attribute */
587 buf_size = vs->vs_cluster_size * sector_size;
588 start_data_sect = reserved + fat_size;
589
590 next = blkid_le32(vs->vs_root_cluster);
591 while (next && --maxloop) {
592 __u32 next_sect_off;
593 __u64 next_off, fat_entry_off;
594 int count;
595
596 next_sect_off = (next - 2) * vs->vs_cluster_size;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400597 next_off = (start_data_sect + next_sect_off) *
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400598 sector_size;
599
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400600 dir = (struct vfat_dir_entry *)
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400601 get_buffer(probe, next_off, buf_size);
602 if (dir == NULL)
603 break;
604
605 count = buf_size / sizeof(struct vfat_dir_entry);
606
607 vol_label = search_fat_label(dir, count);
608 if (vol_label)
609 break;
610
611 /* get FAT entry */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400612 fat_entry_off = (reserved * sector_size) +
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400613 (next * sizeof(__u32));
614 buf = get_buffer(probe, fat_entry_off, buf_size);
615 if (buf == NULL)
616 break;
617
618 /* set next cluster */
619 next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
620 }
621
622 if (!vol_label || !memcmp(vol_label, no_name, 11))
623 vol_label = vs->vs_label;
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400624 vol_serno = vs->vs_serno;
625 }
626
Theodore Ts'o2f79e512005-09-10 21:51:20 -0400627 if (vol_label && memcmp(vol_label, no_name, 11)) {
Karel Zak633f4532006-09-17 21:23:46 -0400628 if ((label_len = figure_label_len(vol_label, 11)))
629 label = vol_label;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500630 }
631
632 /* We can't just print them as %04X, because they are unaligned */
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400633 sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
634 vol_serno[1], vol_serno[0]);
635
Theodore Ts'oca749852005-09-10 21:07:23 -0400636 blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
637 blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500638
639 return 0;
640}
641
Karel Zakc4c740f2005-09-06 08:13:35 -0400642/*
643 * The FAT filesystem could be without a magic string in superblock
644 * (e.g. old floppies). This heuristic for FAT detection is inspired
645 * by http://vrfy.org/projects/volume_id/ and Linux kernel.
646 * [7-Jul-2005, Karel Zak <kzak@redhat.com>]
647 */
Theodore Ts'oca749852005-09-10 21:07:23 -0400648static int probe_fat_nomagic(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400649 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o038d2be2005-09-07 12:14:53 -0400650 unsigned char *buf)
Karel Zakc4c740f2005-09-06 08:13:35 -0400651{
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400652 struct msdos_super_block *ms;
Karel Zakc4c740f2005-09-06 08:13:35 -0400653
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400654 ms = (struct msdos_super_block *)buf;
Karel Zakc4c740f2005-09-06 08:13:35 -0400655
Karel Zakc4c740f2005-09-06 08:13:35 -0400656 /* heads check */
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400657 if (ms->ms_heads == 0)
Karel Zakc4c740f2005-09-06 08:13:35 -0400658 return 1;
659
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400660 /* cluster size check*/
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400661 if (ms->ms_cluster_size == 0 ||
662 (ms->ms_cluster_size & (ms->ms_cluster_size-1)))
Karel Zakc4c740f2005-09-06 08:13:35 -0400663 return 1;
664
665 /* media check */
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400666 if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0)
Karel Zakc4c740f2005-09-06 08:13:35 -0400667 return 1;
668
669 /* fat counts(Linux kernel expects at least 1 FAT table) */
Theodore Ts'o78d89cd2008-08-23 22:11:01 -0400670 if (!ms->ms_fats)
671 return 1;
672
673 /*
674 * OS/2 and apparently DFSee will place a FAT12/16-like
675 * pseudo-superblock in the first 512 bytes of non-FAT
676 * filesystems --- at least JFS and HPFS, and possibly others.
677 * So we explicitly check for those filesystems at the
678 * FAT12/16 filesystem magic field identifier, and if they are
679 * present, we rule this out as a FAT filesystem, despite the
680 * FAT-like pseudo-header.
681 */
682 if ((memcmp(ms->ms_magic, "JFS ", 8) == 0) ||
683 (memcmp(ms->ms_magic, "HPFS ", 8) == 0))
Karel Zakc4c740f2005-09-06 08:13:35 -0400684 return 1;
685
Theodore Ts'oca749852005-09-10 21:07:23 -0400686 return probe_fat(probe, id, buf);
Karel Zakc4c740f2005-09-06 08:13:35 -0400687}
688
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400689static int probe_ntfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400690 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400691 unsigned char *buf)
692{
693 struct ntfs_super_block *ns;
694 struct master_file_table_record *mft;
695 struct file_attribute *attr;
696 char uuid_str[17], label_str[129], *cp;
697 int bytes_per_sector, sectors_per_cluster;
698 int mft_record_size, attr_off, attr_len;
699 unsigned int i, attr_type, val_len;
700 int val_off;
701 __u64 nr_clusters;
702 blkid_loff_t off;
703 unsigned char *buf_mft, *val;
704
705 ns = (struct ntfs_super_block *) buf;
706
707 bytes_per_sector = ns->bios_parameter_block[0] +
708 (ns->bios_parameter_block[1] << 8);
709 sectors_per_cluster = ns->bios_parameter_block[2];
710
Theodore Ts'o30dd2622007-06-24 17:17:24 -0400711 if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
712 return 1;
713
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400714 if (ns->cluster_per_mft_record < 0)
Theodore Ts'o30dd2622007-06-24 17:17:24 -0400715 mft_record_size = 1 << (0-ns->cluster_per_mft_record);
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400716 else
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400717 mft_record_size = ns->cluster_per_mft_record *
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400718 sectors_per_cluster * bytes_per_sector;
719 nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
720
721 if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
722 (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
723 return 1;
724
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400725 off = blkid_le64(ns->mft_mirror_cluster_location) *
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400726 bytes_per_sector * sectors_per_cluster;
727
728 buf_mft = get_buffer(probe, off, mft_record_size);
729 if (!buf_mft)
730 return 1;
731
732 if (memcmp(buf_mft, "FILE", 4))
733 return 1;
734
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400735 off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400736 sectors_per_cluster;
737
738 buf_mft = get_buffer(probe, off, mft_record_size);
739 if (!buf_mft)
740 return 1;
741
742 if (memcmp(buf_mft, "FILE", 4))
743 return 1;
744
745 off += MFT_RECORD_VOLUME * mft_record_size;
746
747 buf_mft = get_buffer(probe, off, mft_record_size);
748 if (!buf_mft)
749 return 1;
750
751 if (memcmp(buf_mft, "FILE", 4))
752 return 1;
753
754 mft = (struct master_file_table_record *) buf_mft;
755
756 attr_off = blkid_le16(mft->attrs_offset);
757 label_str[0] = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400758
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400759 while (1) {
760 attr = (struct file_attribute *) (buf_mft + attr_off);
761 attr_len = blkid_le16(attr->len);
762 attr_type = blkid_le32(attr->type);
763 val_off = blkid_le16(attr->value_offset);
764 val_len = blkid_le32(attr->value_len);
765
766 attr_off += attr_len;
767
768 if ((attr_off > mft_record_size) ||
769 (attr_len == 0))
770 break;
771
772 if (attr_type == MFT_RECORD_ATTR_END)
773 break;
774
775 if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
776 if (val_len > sizeof(label_str))
777 val_len = sizeof(label_str)-1;
778
779 for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
780 val = ((__u8 *) attr) + val_off + i;
781 *cp = val[0];
782 if (val[1])
783 *cp = '?';
784 }
785 *cp = 0;
786 }
787 }
788
Pixelc40ad362008-01-11 11:41:15 +0100789 sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -0400790 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
791 if (label_str[0])
792 blkid_set_tag(probe->dev, "LABEL", label_str, 0);
793 return 0;
794}
795
796
Theodore Ts'oca749852005-09-10 21:07:23 -0400797static int probe_xfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400798 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500799 unsigned char *buf)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500800{
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500801 struct xfs_super_block *xs;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400802 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500803
804 xs = (struct xfs_super_block *)buf;
805
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500806 if (strlen(xs->xs_fname))
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400807 label = xs->xs_fname;
Theodore Ts'oca749852005-09-10 21:07:23 -0400808 blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500809 set_uuid(probe->dev, xs->xs_uuid, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500810 return 0;
811}
812
Theodore Ts'oca749852005-09-10 21:07:23 -0400813static int probe_reiserfs(struct blkid_probe *probe,
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500814 struct blkid_magic *id, unsigned char *buf)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500815{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500816 struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500817 unsigned int blocksize;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400818 const char *label = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500819
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500820 blocksize = blkid_le16(rs->rs_blocksize);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500821
Theodore Ts'o4dc30da2008-01-27 00:14:01 -0500822 /* The blocksize must be at least 1k */
823 if ((blocksize >> 10) == 0)
824 return -BLKID_ERR_PARAM;
825
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500826 /* If the superblock is inside the journal, we have the wrong one */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500827 if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500828 return -BLKID_ERR_BIG;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500829
830 /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
Theodore Ts'oca749852005-09-10 21:07:23 -0400831 if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400832 if (strlen(rs->rs_label))
833 label = rs->rs_label;
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500834 set_uuid(probe->dev, rs->rs_uuid, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500835 }
Theodore Ts'oca749852005-09-10 21:07:23 -0400836 blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500837
838 return 0;
839}
840
Theodore Ts'oca749852005-09-10 21:07:23 -0400841static int probe_reiserfs4(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400842 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'oca749852005-09-10 21:07:23 -0400843 unsigned char *buf)
Theodore Ts'obb626bc2005-09-10 11:48:38 -0400844{
845 struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
Theodore Ts'oca749852005-09-10 21:07:23 -0400846 const unsigned char *label = 0;
Theodore Ts'obb626bc2005-09-10 11:48:38 -0400847
Theodore Ts'oca749852005-09-10 21:07:23 -0400848 if (strlen((char *) rs4->rs4_label))
Theodore Ts'obb626bc2005-09-10 11:48:38 -0400849 label = rs4->rs4_label;
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500850 set_uuid(probe->dev, rs4->rs4_uuid, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400851 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
Theodore Ts'oca749852005-09-10 21:07:23 -0400852 sizeof(rs4->rs4_label));
Theodore Ts'obb626bc2005-09-10 11:48:38 -0400853
854 return 0;
855}
856
Theodore Ts'oca749852005-09-10 21:07:23 -0400857static int probe_jfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400858 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500859 unsigned char *buf)
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500860{
861 struct jfs_super_block *js;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400862 const char *label = 0;
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500863
864 js = (struct jfs_super_block *)buf;
865
Theodore Ts'ob41fb002008-08-23 22:27:22 -0400866 if (blkid_le32(js->js_bsize) != (1 << blkid_le16(js->js_l2bsize)))
867 return 1;
868
869 if (blkid_le32(js->js_pbsize) != (1 << blkid_le16(js->js_l2pbsize)))
870 return 1;
871
872 if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) !=
873 blkid_le16(js->js_l2bfactor))
874 return 1;
875
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400876 if (strlen((char *) js->js_label))
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400877 label = (char *) js->js_label;
Theodore Ts'oca749852005-09-10 21:07:23 -0400878 blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500879 set_uuid(probe->dev, js->js_uuid, 0);
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500880 return 0;
881}
882
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +0100883static int probe_zfs(struct blkid_probe *probe, struct blkid_magic *id,
884 unsigned char *buf)
885{
Eric Sandeen79e62402008-07-06 18:36:56 -0400886#if 0
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +0100887 char *vdev_label;
888 const char *pool_name = 0;
889
890 /* read nvpair data for pool name, pool GUID (complex) */
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +0100891 blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name));
892 set_uuid(probe->dev, pool_guid, 0);
893#endif
894 return 0;
895}
896
Karsten Hoppe382a7e2007-06-21 13:43:33 -0400897static int probe_luks(struct blkid_probe *probe,
898 struct blkid_magic *id __BLKID_ATTR((unused)),
899 unsigned char *buf)
900{
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -0400901 char uuid[40];
902
Karsten Hoppe382a7e2007-06-21 13:43:33 -0400903 /* 168 is the offset to the 40 character uuid:
904 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -0400905 strncpy(uuid, (char *) buf+168, 40);
Karsten Hoppe382a7e2007-06-21 13:43:33 -0400906 blkid_set_tag(probe->dev, "UUID", uuid, sizeof(uuid));
907 return 0;
908}
909
Theodore Ts'oca749852005-09-10 21:07:23 -0400910static int probe_romfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400911 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500912 unsigned char *buf)
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500913{
914 struct romfs_super_block *ros;
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400915 const char *label = 0;
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500916
917 ros = (struct romfs_super_block *)buf;
918
Theodore Ts'oa30b9442003-07-20 11:22:34 -0400919 if (strlen((char *) ros->ros_volume))
920 label = (char *) ros->ros_volume;
Theodore Ts'oca749852005-09-10 21:07:23 -0400921 blkid_set_tag(probe->dev, "LABEL", label, 0);
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500922 return 0;
923}
924
Theodore Ts'oca749852005-09-10 21:07:23 -0400925static int probe_cramfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400926 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'oca749852005-09-10 21:07:23 -0400927 unsigned char *buf)
Theodore Ts'o4c4e3f72005-05-05 18:06:04 -0400928{
929 struct cramfs_super_block *csb;
930 const char *label = 0;
931
932 csb = (struct cramfs_super_block *)buf;
933
934 if (strlen((char *) csb->name))
935 label = (char *) csb->name;
Theodore Ts'oca749852005-09-10 21:07:23 -0400936 blkid_set_tag(probe->dev, "LABEL", label, 0);
Theodore Ts'o4c4e3f72005-05-05 18:06:04 -0400937 return 0;
938}
939
Theodore Ts'oca749852005-09-10 21:07:23 -0400940static int probe_swap0(struct blkid_probe *probe,
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500941 struct blkid_magic *id __BLKID_ATTR((unused)),
942 unsigned char *buf __BLKID_ATTR((unused)))
943{
Theodore Ts'oca749852005-09-10 21:07:23 -0400944 blkid_set_tag(probe->dev, "UUID", 0, 0);
945 blkid_set_tag(probe->dev, "LABEL", 0, 0);
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500946 return 0;
947}
948
Theodore Ts'oca749852005-09-10 21:07:23 -0400949static int probe_swap1(struct blkid_probe *probe,
Eric Sandeen6360d122008-04-18 10:28:44 -0500950 struct blkid_magic *id,
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500951 unsigned char *buf __BLKID_ATTR((unused)))
952{
953 struct swap_id_block *sws;
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500954
Theodore Ts'oca749852005-09-10 21:07:23 -0400955 probe_swap0(probe, id, buf);
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500956 /*
957 * Version 1 swap headers are always located at offset of 1024
958 * bytes, although the swap signature itself is located at the
959 * end of the page (which may vary depending on hardware
960 * pagesize).
961 */
Theodore Ts'oca749852005-09-10 21:07:23 -0400962 sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
963 if (!sws)
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500964 return 1;
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500965
Eric Sandeen6360d122008-04-18 10:28:44 -0500966 /* check for wrong version or zeroed pagecount, for sanity */
967 if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) &&
Eric Sandeen22269b82008-05-12 13:26:51 -0500968 (sws->sws_version != 1 || sws->sws_lastpage == 0))
Eric Sandeen6360d122008-04-18 10:28:44 -0500969 return 1;
970
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500971 /* arbitrary sanity check.. is there any garbage down there? */
972 if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) {
973 if (sws->sws_volume[0])
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400974 blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500975 sizeof(sws->sws_volume));
976 if (sws->sws_uuid[0])
Theodore Ts'oba5e3842006-03-10 18:11:35 -0500977 set_uuid(probe->dev, sws->sws_uuid, 0);
Theodore Ts'o7369f0c2005-01-10 23:58:11 -0500978 }
Theodore Ts'o09a2ef82003-04-03 00:04:28 -0500979 return 0;
980}
981
Theodore Ts'oca749852005-09-10 21:07:23 -0400982static int probe_iso9660(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400983 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o45a3fa82005-09-10 15:46:57 -0400984 unsigned char *buf)
985{
986 struct iso_volume_descriptor *iso;
987 const unsigned char *label;
988
989 iso = (struct iso_volume_descriptor *) buf;
Theodore Ts'o45a3fa82005-09-10 15:46:57 -0400990 label = iso->volume_id;
991
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400992 blkid_set_tag(probe->dev, "LABEL", (const char *) label,
Theodore Ts'oca749852005-09-10 21:07:23 -0400993 figure_label_len(label, 32));
Theodore Ts'o45a3fa82005-09-10 15:46:57 -0400994 return 0;
995}
996
997
Theodore Ts'o54434922003-12-07 01:28:50 -0500998static const char
Theodore Ts'o3de5bf62003-07-22 01:06:36 -0400999*udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
1000 "NSR03", "TEA01", 0 };
1001
Theodore Ts'oca749852005-09-10 21:07:23 -04001002static int probe_udf(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001003 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05001004 unsigned char *buf __BLKID_ATTR((unused)))
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001005{
1006 int j, bs;
Theodore Ts'oca749852005-09-10 21:07:23 -04001007 struct iso_volume_descriptor *isosb;
Theodore Ts'o54434922003-12-07 01:28:50 -05001008 const char ** m;
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001009
1010 /* determine the block size by scanning in 2K increments
1011 (block sizes larger than 2K will be null padded) */
1012 for (bs = 1; bs < 16; bs++) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001013 isosb = (struct iso_volume_descriptor *)
Theodore Ts'oca749852005-09-10 21:07:23 -04001014 get_buffer(probe, bs*2048+32768, sizeof(isosb));
1015 if (!isosb)
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001016 return 1;
Theodore Ts'oca749852005-09-10 21:07:23 -04001017 if (isosb->vd_id[0])
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001018 break;
1019 }
1020
1021 /* Scan up to another 64 blocks looking for additional VSD's */
1022 for (j = 1; j < 64; j++) {
1023 if (j > 1) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001024 isosb = (struct iso_volume_descriptor *)
1025 get_buffer(probe, j*bs*2048+32768,
Theodore Ts'oca749852005-09-10 21:07:23 -04001026 sizeof(isosb));
1027 if (!isosb)
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001028 return 1;
1029 }
1030 /* If we find NSR0x then call it udf:
1031 NSR01 for UDF 1.00
1032 NSR02 for UDF 1.50
1033 NSR03 for UDF 2.00 */
Theodore Ts'o49487b72006-05-14 17:29:00 -04001034 if (!memcmp(isosb->vd_id, "NSR0", 4))
Theodore Ts'oa4045c22008-02-09 23:12:56 -05001035 return probe_iso9660(probe, id, buf);
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001036 for (m = udf_magic; *m; m++)
Theodore Ts'o49487b72006-05-14 17:29:00 -04001037 if (!memcmp(*m, isosb->vd_id, 5))
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001038 break;
1039 if (*m == 0)
1040 return 1;
1041 }
1042 return 1;
1043}
1044
Theodore Ts'oca749852005-09-10 21:07:23 -04001045static int probe_ocfs(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001046 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o9387c282004-03-04 19:59:58 -05001047 unsigned char *buf)
1048{
1049 struct ocfs_volume_header ovh;
1050 struct ocfs_volume_label ovl;
Theodore Ts'o3838f7d2004-11-30 19:05:38 -05001051 __u32 major;
Theodore Ts'o9387c282004-03-04 19:59:58 -05001052
1053 memcpy(&ovh, buf, sizeof(ovh));
1054 memcpy(&ovl, buf+512, sizeof(ovl));
1055
1056 major = ocfsmajor(ovh);
1057 if (major == 1)
Theodore Ts'oca749852005-09-10 21:07:23 -04001058 blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
Theodore Ts'o9387c282004-03-04 19:59:58 -05001059 else if (major >= 9)
Theodore Ts'oca749852005-09-10 21:07:23 -04001060 blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001061
Theodore Ts'oca749852005-09-10 21:07:23 -04001062 blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
1063 blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001064 set_uuid(probe->dev, ovl.vol_id, 0);
Theodore Ts'o9387c282004-03-04 19:59:58 -05001065 return 0;
1066}
1067
Theodore Ts'oca749852005-09-10 21:07:23 -04001068static int probe_ocfs2(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001069 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o2c923752005-01-21 17:57:56 -05001070 unsigned char *buf)
Theodore Ts'o414846b2004-09-17 21:37:49 -04001071{
1072 struct ocfs2_super_block *osb;
1073
1074 osb = (struct ocfs2_super_block *)buf;
1075
Theodore Ts'oca749852005-09-10 21:07:23 -04001076 blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001077 set_uuid(probe->dev, osb->s_uuid, 0);
Theodore Ts'o414846b2004-09-17 21:37:49 -04001078 return 0;
1079}
1080
Theodore Ts'oca749852005-09-10 21:07:23 -04001081static int probe_oracleasm(struct blkid_probe *probe,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001082 struct blkid_magic *id __BLKID_ATTR((unused)),
Theodore Ts'o2c923752005-01-21 17:57:56 -05001083 unsigned char *buf)
1084{
1085 struct oracle_asm_disk_label *dl;
1086
1087 dl = (struct oracle_asm_disk_label *)buf;
1088
Theodore Ts'oca749852005-09-10 21:07:23 -04001089 blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
Theodore Ts'o2c923752005-01-21 17:57:56 -05001090 return 0;
1091}
1092
Karel Zakb5517ca2006-09-17 21:10:58 -04001093static int probe_gfs(struct blkid_probe *probe,
1094 struct blkid_magic *id __BLKID_ATTR((unused)),
1095 unsigned char *buf)
1096{
1097 struct gfs2_sb *sbd;
1098 const char *label = 0;
1099
1100 sbd = (struct gfs2_sb *)buf;
1101
1102 if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
1103 blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001104 {
Karel Zakb5517ca2006-09-17 21:10:58 -04001105 blkid_set_tag(probe->dev, "UUID", 0, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001106
Karel Zakb5517ca2006-09-17 21:10:58 -04001107 if (strlen(sbd->sb_locktable))
1108 label = sbd->sb_locktable;
1109 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1110 return 0;
1111 }
1112 return 1;
1113}
1114
1115static int probe_gfs2(struct blkid_probe *probe,
1116 struct blkid_magic *id __BLKID_ATTR((unused)),
1117 unsigned char *buf)
1118{
1119 struct gfs2_sb *sbd;
1120 const char *label = 0;
1121
1122 sbd = (struct gfs2_sb *)buf;
1123
1124 if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
1125 blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001126 {
Karel Zakb5517ca2006-09-17 21:10:58 -04001127 blkid_set_tag(probe->dev, "UUID", 0, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001128
Karel Zakb5517ca2006-09-17 21:10:58 -04001129 if (strlen(sbd->sb_locktable))
1130 label = sbd->sb_locktable;
1131 blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1132 return 0;
1133 }
1134 return 1;
1135}
1136
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001137static void unicode_16be_to_utf8(unsigned char *str, int out_len,
Theodore Ts'od56ccbd2008-08-24 23:34:13 -04001138 const unsigned char *buf, int in_len)
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001139{
1140 int i, j;
1141 unsigned int c;
1142
1143 for (i = j = 0; i + 2 <= in_len; i += 2) {
1144 c = (buf[i] << 8) | buf[i+1];
1145 if (c == 0) {
1146 str[j] = '\0';
1147 break;
1148 } else if (c < 0x80) {
1149 if (j+1 >= out_len)
1150 break;
1151 str[j++] = (unsigned char) c;
1152 } else if (c < 0x800) {
1153 if (j+2 >= out_len)
1154 break;
1155 str[j++] = (unsigned char) (0xc0 | (c >> 6));
1156 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1157 } else {
1158 if (j+3 >= out_len)
1159 break;
1160 str[j++] = (unsigned char) (0xe0 | (c >> 12));
1161 str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1162 str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1163 }
1164 }
1165 str[j] = '\0';
1166}
1167
1168static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
Theodore Ts'o69d74222008-01-27 00:21:07 -05001169 struct blkid_magic *id __BLKID_ATTR((unused)),
1170 unsigned char *buf)
1171{
JP Abgralle0ed7402014-03-19 19:08:39 -07001172 struct hfs_mdb *hfs = (struct hfs_mdb *)buf;
1173 unsigned long long *uuid_ptr;
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001174 char uuid_str[17];
1175 __u64 uuid;
1176
1177 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
1178 (memcmp(hfs->embed_sig, "HX", 2) == 0))
1179 return 1; /* Not hfs, but an embedded HFS+ */
1180
JP Abgralle0ed7402014-03-19 19:08:39 -07001181 uuid_ptr = (unsigned long long *)hfs->finder_info.id;
1182 uuid = blkid_le64(*uuid_ptr);
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001183 if (uuid) {
1184 sprintf(uuid_str, "%016llX", uuid);
1185 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1186 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001187 blkid_set_tag(probe->dev, "LABEL", (char *)hfs->label, hfs->label_len);
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001188 return 0;
1189}
1190
1191
1192static int probe_hfsplus(struct blkid_probe *probe,
1193 struct blkid_magic *id,
1194 unsigned char *buf)
1195{
1196 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1197 struct hfsplus_bnode_descriptor *descr;
1198 struct hfsplus_bheader_record *bnode;
1199 struct hfsplus_catalog_key *key;
1200 struct hfsplus_vol_header *hfsplus;
1201 struct hfs_mdb *sbd = (struct hfs_mdb *) buf;
1202 unsigned int alloc_block_size;
1203 unsigned int alloc_first_block;
1204 unsigned int embed_first_block;
1205 unsigned int off = 0;
1206 unsigned int blocksize;
1207 unsigned int cat_block;
1208 unsigned int ext_block_start;
1209 unsigned int ext_block_count;
1210 unsigned int record_count;
1211 unsigned int leaf_node_head;
1212 unsigned int leaf_node_count;
1213 unsigned int leaf_node_size;
1214 unsigned int leaf_block;
1215 unsigned int label_len;
JP Abgralle0ed7402014-03-19 19:08:39 -07001216 unsigned long long *uuid_ptr;
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001217 __u64 leaf_off, uuid;
1218 char uuid_str[17], label[512];
JP Abgralle0ed7402014-03-19 19:08:39 -07001219 int ext;
Theodore Ts'o69d74222008-01-27 00:21:07 -05001220
1221 /* Check for a HFS+ volume embedded in a HFS volume */
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001222 if (memcmp(sbd->signature, "BD", 2) == 0) {
1223 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
1224 (memcmp(sbd->embed_sig, "HX", 2) != 0))
1225 /* This must be an HFS volume, so fail */
1226 return 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001227
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001228 alloc_block_size = blkid_be32(sbd->al_blk_size);
1229 alloc_first_block = blkid_be16(sbd->al_bl_st);
1230 embed_first_block = blkid_be16(sbd->embed_startblock);
1231 off = (alloc_first_block * 512) +
1232 (embed_first_block * alloc_block_size);
1233 buf = get_buffer(probe, off + (id->bim_kboff * 1024),
1234 sizeof(sbd));
1235 if (!buf)
1236 return 1;
1237
1238 hfsplus = (struct hfsplus_vol_header *) buf;
1239 }
1240
1241 hfsplus = (struct hfsplus_vol_header *) buf;
1242
1243 if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
1244 (memcmp(hfsplus->signature, "HX", 2) != 0))
1245 return 1;
1246
JP Abgralle0ed7402014-03-19 19:08:39 -07001247 uuid_ptr = (unsigned long long *)hfsplus->finder_info.id;
1248 uuid = blkid_le64(*uuid_ptr);
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001249 if (uuid) {
1250 sprintf(uuid_str, "%016llX", uuid);
1251 blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1252 }
1253
1254 blocksize = blkid_be32(hfsplus->blocksize);
1255 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1256 cat_block = blkid_be32(extents[0].start_block);
1257
1258 buf = get_buffer(probe, off + (cat_block * blocksize), 0x2000);
1259 if (!buf)
Theodore Ts'o69d74222008-01-27 00:21:07 -05001260 return 0;
1261
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001262 bnode = (struct hfsplus_bheader_record *)
1263 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1264
1265 leaf_node_head = blkid_be32(bnode->leaf_head);
1266 leaf_node_size = blkid_be16(bnode->node_size);
1267 leaf_node_count = blkid_be32(bnode->leaf_count);
1268 if (leaf_node_count == 0)
1269 return 0;
1270
1271 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1272
1273 /* get physical location */
1274 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1275 ext_block_start = blkid_be32(extents[ext].start_block);
1276 ext_block_count = blkid_be32(extents[ext].block_count);
1277 if (ext_block_count == 0)
1278 return 0;
1279
1280 /* this is our extent */
1281 if (leaf_block < ext_block_count)
1282 break;
1283
1284 leaf_block -= ext_block_count;
1285 }
1286 if (ext == HFSPLUS_EXTENT_COUNT)
1287 return 0;
1288
1289 leaf_off = (ext_block_start + leaf_block) * blocksize;
1290
1291 buf = get_buffer(probe, off + leaf_off, leaf_node_size);
1292 if (!buf)
1293 return 0;
1294
1295 descr = (struct hfsplus_bnode_descriptor *) buf;
1296 record_count = blkid_be16(descr->num_recs);
1297 if (record_count == 0)
1298 return 0;
1299
1300 if (descr->type != HFS_NODE_LEAF)
1301 return 0;
1302
1303 key = (struct hfsplus_catalog_key *)
1304 &buf[sizeof(struct hfsplus_bnode_descriptor)];
1305
1306 if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID)
1307 return 0;
1308
1309 label_len = blkid_be16(key->unicode_len) * 2;
JP Abgralle0ed7402014-03-19 19:08:39 -07001310 unicode_16be_to_utf8((unsigned char *)label, sizeof(label),
1311 key->unicode, label_len);
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001312 blkid_set_tag(probe->dev, "LABEL", label, 0);
1313 return 0;
Theodore Ts'o69d74222008-01-27 00:21:07 -05001314}
1315
Eric Sandeena451d922008-01-30 17:25:03 -06001316#define LVM2_LABEL_SIZE 512
Theodore Ts'odd232042008-03-08 20:01:05 -05001317static unsigned int lvm2_calc_crc(const void *buf, unsigned int size)
Eric Sandeena451d922008-01-30 17:25:03 -06001318{
Theodore Ts'odd232042008-03-08 20:01:05 -05001319 static const unsigned int crctab[] = {
Eric Sandeena451d922008-01-30 17:25:03 -06001320 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1321 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1322 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1323 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1324 };
Theodore Ts'odd232042008-03-08 20:01:05 -05001325 unsigned int i, crc = 0xf597a6cf;
Eric Sandeena451d922008-01-30 17:25:03 -06001326 const __u8 *data = (const __u8 *) buf;
1327
1328 for (i = 0; i < size; i++) {
1329 crc ^= *data++;
1330 crc = (crc >> 4) ^ crctab[crc & 0xf];
1331 crc = (crc >> 4) ^ crctab[crc & 0xf];
1332 }
1333 return crc;
1334}
1335
1336static int probe_lvm2(struct blkid_probe *probe,
Theodore Ts'occ19b952008-06-17 13:03:47 -04001337 struct blkid_magic *id,
Eric Sandeena451d922008-01-30 17:25:03 -06001338 unsigned char *buf)
1339{
Theodore Ts'o8d45e212008-06-21 14:04:36 -04001340 int sector = (id->bim_kboff) << 1;
Theodore Ts'occ19b952008-06-17 13:03:47 -04001341 struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf;
Eric Sandeena451d922008-01-30 17:25:03 -06001342 char *p, *q, uuid[40];
1343 unsigned int i, b;
1344
1345 /* buf is at 0k or 1k offset; find label inside */
1346 if (memcmp(buf, "LABELONE", 8) == 0) {
1347 label = (struct lvm2_pv_label_header *)buf;
1348 } else if (memcmp(buf + 512, "LABELONE", 8) == 0) {
1349 label = (struct lvm2_pv_label_header *)(buf + 512);
1350 sector++;
1351 } else {
1352 return 1;
1353 }
1354
Theodore Ts'o3a538e42008-02-27 02:06:08 -05001355 if (blkid_le64(label->sector_xl) != (unsigned) sector) {
Eric Sandeena451d922008-01-30 17:25:03 -06001356 DBG(DEBUG_PROBE,
Theodore Ts'o3a538e42008-02-27 02:06:08 -05001357 printf("LVM2: label for sector %llu found at sector %d\n",
Eric Sandeena451d922008-01-30 17:25:03 -06001358 blkid_le64(label->sector_xl), sector));
1359 return 1;
1360 }
1361
1362 if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
Theodore Ts'o3a538e42008-02-27 02:06:08 -05001363 ((char *)&label->offset_xl - (char *)label)) !=
Eric Sandeena451d922008-01-30 17:25:03 -06001364 blkid_le32(label->crc_xl)) {
1365 DBG(DEBUG_PROBE,
1366 printf("LVM2: label checksum incorrect at sector %d\n",
1367 sector));
1368 return 1;
1369 }
1370
Theodore Ts'o3a538e42008-02-27 02:06:08 -05001371 for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i <= 32;
1372 i++, b <<= 1) {
Eric Sandeena451d922008-01-30 17:25:03 -06001373 if (b & 0x4444440)
1374 *p++ = '-';
1375 *p++ = *q++;
1376 }
1377
1378 blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6);
1379
1380 return 0;
1381}
Eric Sandeen801b0052009-01-08 15:08:16 -06001382
1383static int probe_btrfs(struct blkid_probe *probe,
1384 struct blkid_magic *id,
1385 unsigned char *buf)
1386{
1387 struct btrfs_super_block *bs;
1388 const char *label = 0;
1389
1390 bs = (struct btrfs_super_block *)buf;
1391
1392 if (strlen(bs->label))
1393 label = bs->label;
1394 blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label));
1395 set_uuid(probe->dev, bs->fsid, 0);
1396 return 0;
1397}
Jeff Sharkeyb2cc45f2015-05-23 20:36:50 -07001398
1399static int probe_f2fs(struct blkid_probe *probe,
1400 struct blkid_magic *id,
1401 unsigned char *buf)
1402{
1403 struct f2fs_super_block *bs;
1404
1405 bs = (struct f2fs_super_block *)buf;
1406 set_uuid(probe->dev, bs->uuid, 0);
1407 return 0;
1408}
1409
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001410/*
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001411 * Various filesystem magics that we can check for. Note that kboff and
1412 * sboff are in kilobytes and bytes respectively. All magics are in
1413 * byte strings so we don't worry about endian issues.
1414 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001415static struct blkid_magic type_array[] = {
1416/* type kboff sboff len magic probe */
Theodore Ts'o2c923752005-01-21 17:57:56 -05001417 { "oracleasm", 0, 32, 8, "ORCLDISK", probe_oracleasm },
Theodore Ts'ocb0c5d72007-06-19 03:29:47 -04001418 { "ntfs", 0, 3, 8, "NTFS ", probe_ntfs },
Steve Kondik9284fd22013-07-06 03:07:31 -07001419 { "exfat", 0, 3, 8, "EXFAT ", probe_exfat },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001420 { "jbd", 1, 0x38, 2, "\123\357", probe_jbd },
Theodore Ts'o29213322008-01-26 22:25:50 -05001421 { "ext4dev", 1, 0x38, 2, "\123\357", probe_ext4dev },
1422 { "ext4", 1, 0x38, 2, "\123\357", probe_ext4 },
Theodore Ts'o2e6a9fe2005-01-05 17:45:32 -05001423 { "ext3", 1, 0x38, 2, "\123\357", probe_ext3 },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001424 { "ext2", 1, 0x38, 2, "\123\357", probe_ext2 },
1425 { "reiserfs", 8, 0x34, 8, "ReIsErFs", probe_reiserfs },
1426 { "reiserfs", 64, 0x34, 9, "ReIsEr2Fs", probe_reiserfs },
1427 { "reiserfs", 64, 0x34, 9, "ReIsEr3Fs", probe_reiserfs },
1428 { "reiserfs", 64, 0x34, 8, "ReIsErFs", probe_reiserfs },
1429 { "reiserfs", 8, 20, 8, "ReIsErFs", probe_reiserfs },
Theodore Ts'obb626bc2005-09-10 11:48:38 -04001430 { "reiser4", 64, 0, 7, "ReIsEr4", probe_reiserfs4 },
Karel Zakb5517ca2006-09-17 21:10:58 -04001431 { "gfs2", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs2 },
1432 { "gfs", 64, 0, 4, "\x01\x16\x19\x70", probe_gfs },
Theodore Ts'o038d2be2005-09-07 12:14:53 -04001433 { "vfat", 0, 0x52, 5, "MSWIN", probe_fat },
1434 { "vfat", 0, 0x52, 8, "FAT32 ", probe_fat },
1435 { "vfat", 0, 0x36, 5, "MSDOS", probe_fat },
1436 { "vfat", 0, 0x36, 8, "FAT16 ", probe_fat },
1437 { "vfat", 0, 0x36, 8, "FAT12 ", probe_fat },
Theodore Ts'o846be6d2007-09-23 09:50:11 -04001438 { "vfat", 0, 0, 1, "\353", probe_fat_nomagic },
Theodore Ts'o038d2be2005-09-07 12:14:53 -04001439 { "vfat", 0, 0, 1, "\351", probe_fat_nomagic },
Theodore Ts'o846be6d2007-09-23 09:50:11 -04001440 { "vfat", 0, 0x1fe, 2, "\125\252", probe_fat_nomagic },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001441 { "minix", 1, 0x10, 2, "\177\023", 0 },
1442 { "minix", 1, 0x10, 2, "\217\023", 0 },
1443 { "minix", 1, 0x10, 2, "\150\044", 0 },
1444 { "minix", 1, 0x10, 2, "\170\044", 0 },
1445 { "vxfs", 1, 0, 4, "\365\374\001\245", 0 },
1446 { "xfs", 0, 0, 4, "XFSB", probe_xfs },
Theodore Ts'o09a2ef82003-04-03 00:04:28 -05001447 { "romfs", 0, 0, 8, "-rom1fs-", probe_romfs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001448 { "bfs", 0, 0, 4, "\316\372\173\033", 0 },
Theodore Ts'o4c4e3f72005-05-05 18:06:04 -04001449 { "cramfs", 0, 0, 4, "E=\315\050", probe_cramfs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001450 { "qnx4", 0, 4, 6, "QNX4FS", 0 },
Theodore Ts'o3de5bf62003-07-22 01:06:36 -04001451 { "udf", 32, 1, 5, "BEA01", probe_udf },
1452 { "udf", 32, 1, 5, "BOOT2", probe_udf },
1453 { "udf", 32, 1, 5, "CD001", probe_udf },
1454 { "udf", 32, 1, 5, "CDW02", probe_udf },
1455 { "udf", 32, 1, 5, "NSR02", probe_udf },
1456 { "udf", 32, 1, 5, "NSR03", probe_udf },
1457 { "udf", 32, 1, 5, "TEA01", probe_udf },
Theodore Ts'o45a3fa82005-09-10 15:46:57 -04001458 { "iso9660", 32, 1, 5, "CD001", probe_iso9660 },
1459 { "iso9660", 32, 9, 5, "CDROM", probe_iso9660 },
Theodore Ts'o09a2ef82003-04-03 00:04:28 -05001460 { "jfs", 32, 0, 4, "JFS1", probe_jfs },
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +01001461 { "zfs", 8, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs },
1462 { "zfs", 8, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs },
1463 { "zfs", 264, 0, 8, "\0\0\x02\xf5\xb0\x07\xb1\x0c", probe_zfs },
1464 { "zfs", 264, 0, 8, "\x0c\xb1\x07\xb0\xf5\x02\0\0", probe_zfs },
Theodore Ts'o69d74222008-01-27 00:21:07 -05001465 { "hfsplus", 1, 0, 2, "BD", probe_hfsplus },
Theodore Ts'o9c460ca2008-08-22 16:45:44 -04001466 { "hfsplus", 1, 0, 2, "H+", probe_hfsplus },
1467 { "hfsplus", 1, 0, 2, "HX", probe_hfsplus },
1468 { "hfs", 1, 0, 2, "BD", probe_hfs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001469 { "ufs", 8, 0x55c, 4, "T\031\001\000", 0 },
1470 { "hpfs", 8, 0, 4, "I\350\225\371", 0 },
1471 { "sysv", 0, 0x3f8, 4, "\020~\030\375", 0 },
Theodore Ts'o7369f0c2005-01-10 23:58:11 -05001472 { "swap", 0, 0xff6, 10, "SWAP-SPACE", probe_swap0 },
1473 { "swap", 0, 0xff6, 10, "SWAPSPACE2", probe_swap1 },
Karel Zakabaa1122005-09-06 06:42:49 -04001474 { "swsuspend", 0, 0xff6, 9, "S1SUSPEND", probe_swap1 },
1475 { "swsuspend", 0, 0xff6, 9, "S2SUSPEND", probe_swap1 },
Theodore Ts'o31e2a752009-01-20 12:07:31 -05001476 { "swsuspend", 0, 0xff6, 9, "ULSUSPEND", probe_swap1 },
Theodore Ts'o7369f0c2005-01-10 23:58:11 -05001477 { "swap", 0, 0x1ff6, 10, "SWAP-SPACE", probe_swap0 },
1478 { "swap", 0, 0x1ff6, 10, "SWAPSPACE2", probe_swap1 },
Karel Zakabaa1122005-09-06 06:42:49 -04001479 { "swsuspend", 0, 0x1ff6, 9, "S1SUSPEND", probe_swap1 },
1480 { "swsuspend", 0, 0x1ff6, 9, "S2SUSPEND", probe_swap1 },
Theodore Ts'o31e2a752009-01-20 12:07:31 -05001481 { "swsuspend", 0, 0x1ff6, 9, "ULSUSPEND", probe_swap1 },
Theodore Ts'o7369f0c2005-01-10 23:58:11 -05001482 { "swap", 0, 0x3ff6, 10, "SWAP-SPACE", probe_swap0 },
1483 { "swap", 0, 0x3ff6, 10, "SWAPSPACE2", probe_swap1 },
Karel Zakabaa1122005-09-06 06:42:49 -04001484 { "swsuspend", 0, 0x3ff6, 9, "S1SUSPEND", probe_swap1 },
1485 { "swsuspend", 0, 0x3ff6, 9, "S2SUSPEND", probe_swap1 },
Theodore Ts'o31e2a752009-01-20 12:07:31 -05001486 { "swsuspend", 0, 0x3ff6, 9, "ULSUSPEND", probe_swap1 },
Theodore Ts'o7369f0c2005-01-10 23:58:11 -05001487 { "swap", 0, 0x7ff6, 10, "SWAP-SPACE", probe_swap0 },
1488 { "swap", 0, 0x7ff6, 10, "SWAPSPACE2", probe_swap1 },
Karel Zakabaa1122005-09-06 06:42:49 -04001489 { "swsuspend", 0, 0x7ff6, 9, "S1SUSPEND", probe_swap1 },
1490 { "swsuspend", 0, 0x7ff6, 9, "S2SUSPEND", probe_swap1 },
Theodore Ts'o31e2a752009-01-20 12:07:31 -05001491 { "swsuspend", 0, 0x7ff6, 9, "ULSUSPEND", probe_swap1 },
Theodore Ts'o7369f0c2005-01-10 23:58:11 -05001492 { "swap", 0, 0xfff6, 10, "SWAP-SPACE", probe_swap0 },
1493 { "swap", 0, 0xfff6, 10, "SWAPSPACE2", probe_swap1 },
Karel Zakabaa1122005-09-06 06:42:49 -04001494 { "swsuspend", 0, 0xfff6, 9, "S1SUSPEND", probe_swap1 },
1495 { "swsuspend", 0, 0xfff6, 9, "S2SUSPEND", probe_swap1 },
Theodore Ts'o31e2a752009-01-20 12:07:31 -05001496 { "swsuspend", 0, 0xfff6, 9, "ULSUSPEND", probe_swap1 },
Theodore Ts'o2c923752005-01-21 17:57:56 -05001497 { "ocfs", 0, 8, 9, "OracleCFS", probe_ocfs },
1498 { "ocfs2", 1, 0, 6, "OCFSV2", probe_ocfs2 },
1499 { "ocfs2", 2, 0, 6, "OCFSV2", probe_ocfs2 },
1500 { "ocfs2", 4, 0, 6, "OCFSV2", probe_ocfs2 },
1501 { "ocfs2", 8, 0, 6, "OCFSV2", probe_ocfs2 },
Karsten Hoppe382a7e2007-06-21 13:43:33 -04001502 { "crypt_LUKS", 0, 0, 6, "LUKS\xba\xbe", probe_luks },
Eric Sandeenf493d4e2007-10-15 12:48:44 -05001503 { "squashfs", 0, 0, 4, "sqsh", 0 },
Eric Sandeen5845efd2007-09-25 12:02:50 -05001504 { "squashfs", 0, 0, 4, "hsqs", 0 },
Eric Sandeena451d922008-01-30 17:25:03 -06001505 { "lvm2pv", 0, 0x218, 8, "LVM2 001", probe_lvm2 },
1506 { "lvm2pv", 0, 0x018, 8, "LVM2 001", probe_lvm2 },
1507 { "lvm2pv", 1, 0x018, 8, "LVM2 001", probe_lvm2 },
1508 { "lvm2pv", 1, 0x218, 8, "LVM2 001", probe_lvm2 },
Eric Sandeen801b0052009-01-08 15:08:16 -06001509 { "btrfs", 64, 0x40, 8, "_BHRfS_M", probe_btrfs },
Jeff Sharkeyb2cc45f2015-05-23 20:36:50 -07001510 { "f2fs", 1, 0, 4, "\x10\x20\xf5\xf2", probe_f2fs },
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001511 { NULL, 0, 0, 0, NULL, NULL }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001512};
1513
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001514/*
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001515 * Verify that the data in dev is consistent with what is on the actual
1516 * block device (using the devname field only). Normally this will be
1517 * called when finding items in the cache, but for long running processes
1518 * is also desirable to revalidate an item before use.
1519 *
1520 * If we are unable to revalidate the data, we return the old data and
1521 * do not set the BLKID_BID_FL_VERIFIED flag on it.
1522 */
Theodore Ts'o18d12962005-01-27 19:51:47 -05001523blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001524{
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001525 struct blkid_magic *id;
Theodore Ts'oca749852005-09-10 21:07:23 -04001526 struct blkid_probe probe;
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001527 blkid_tag_iterate iter;
Theodore Ts'oca749852005-09-10 21:07:23 -04001528 unsigned char *buf;
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001529 const char *type, *value;
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001530 struct stat st;
Jeff Sharkey9f9a0502013-10-16 15:42:12 -07001531 time_t now;
1532 double diff;
Theodore Ts'oca749852005-09-10 21:07:23 -04001533 int idx;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001534
1535 if (!dev)
1536 return NULL;
1537
Theodore Ts'o7ce08062004-04-19 21:42:18 -04001538 now = time(0);
Jeff Sharkey9f9a0502013-10-16 15:42:12 -07001539 diff = difftime(now, dev->bid_time);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001540
Theodore Ts'o492ea652008-05-20 17:28:54 -04001541 if (stat(dev->bid_name, &st) < 0) {
1542 DBG(DEBUG_PROBE,
1543 printf("blkid_verify: error %s (%d) while "
1544 "trying to stat %s\n", strerror(errno), errno,
1545 dev->bid_name));
1546 open_err:
1547 if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
1548 /* We don't have read permission, just return cache data. */
1549 DBG(DEBUG_PROBE, printf("returning unverified data for %s\n",
1550 dev->bid_name));
1551 return dev;
1552 }
1553 blkid_free_dev(dev);
1554 return NULL;
1555 }
1556
1557 if ((now >= dev->bid_time) &&
1558 (st.st_mtime <= dev->bid_time) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001559 ((diff < BLKID_PROBE_MIN) ||
Theodore Ts'oe324b252006-03-12 23:25:15 -05001560 (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1561 diff < BLKID_PROBE_INTERVAL)))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001562 return dev;
1563
Theodore Ts'of0a22d02003-02-22 13:19:53 -05001564 DBG(DEBUG_PROBE,
Eric Sandeen79e62402008-07-06 18:36:56 -04001565 printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t"
Theodore Ts'o492ea652008-05-20 17:28:54 -04001566 "time since last check %lu)\n",
Eric Sandeen79e62402008-07-06 18:36:56 -04001567 dev->bid_name, (unsigned long)dev->bid_time,
1568 (unsigned long)st.st_mtime, (unsigned long)diff));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001569
Theodore Ts'o492ea652008-05-20 17:28:54 -04001570 if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) {
1571 DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while "
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001572 "opening %s\n", strerror(errno), errno,
Theodore Ts'o492ea652008-05-20 17:28:54 -04001573 dev->bid_name));
1574 goto open_err;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001575 }
1576
Theodore Ts'oca749852005-09-10 21:07:23 -04001577 probe.cache = cache;
1578 probe.dev = dev;
1579 probe.sbbuf = 0;
1580 probe.buf = 0;
1581 probe.buf_max = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001582
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001583 /*
1584 * Iterate over the type array. If we already know the type,
1585 * then try that first. If it doesn't work, then blow away
1586 * the type information, and try again.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001587 *
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001588 */
1589try_again:
1590 type = 0;
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001591 if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1592 uuid_t uuid;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001593
Theodore Ts'oca749852005-09-10 21:07:23 -04001594 if (check_mdraid(probe.fd, uuid) == 0) {
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001595 set_uuid(dev, uuid, 0);
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001596 type = "mdraid";
1597 goto found_type;
1598 }
1599 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001600 for (id = type_array; id->bim_type; id++) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001601 if (dev->bid_type &&
1602 strcmp(id->bim_type, dev->bid_type))
1603 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001604
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001605 idx = id->bim_kboff + (id->bim_sboff >> 10);
Theodore Ts'oca749852005-09-10 21:07:23 -04001606 buf = get_buffer(&probe, idx << 10, 1024);
1607 if (!buf)
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001608 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001609
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +01001610 if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff),
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001611 id->bim_len))
1612 continue;
1613
1614 if ((id->bim_probe == NULL) ||
Theodore Ts'oca749852005-09-10 21:07:23 -04001615 (id->bim_probe(&probe, id, buf) == 0)) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001616 type = id->bim_type;
1617 goto found_type;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001618 }
1619 }
1620
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001621 if (!id->bim_type && dev->bid_type) {
1622 /*
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001623 * Zap the device filesystem information and try again
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001624 */
Theodore Ts'o0225fac2007-07-08 20:17:31 -04001625 DBG(DEBUG_PROBE,
1626 printf("previous fs type %s not valid, "
1627 "trying full probe\n", dev->bid_type));
Theodore Ts'oba5e3842006-03-10 18:11:35 -05001628 iter = blkid_tag_iterate_begin(dev);
1629 while (blkid_tag_next(iter, &type, &value) == 0)
1630 blkid_set_tag(dev, type, 0, 0);
1631 blkid_tag_iterate_end(iter);
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001632 goto try_again;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001633 }
1634
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001635 if (!dev->bid_type) {
1636 blkid_free_dev(dev);
Theodore Ts'o257ace82007-03-06 19:56:18 -05001637 dev = 0;
1638 goto found_type;
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001639 }
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +01001640
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001641found_type:
1642 if (dev && type) {
1643 dev->bid_devno = st.st_rdev;
1644 dev->bid_time = time(0);
1645 dev->bid_flags |= BLKID_BID_FL_VERIFIED;
Theodore Ts'oce72b862003-02-14 01:31:45 -05001646 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001647
Theodore Ts'o79dd2342003-02-22 17:15:20 -05001648 blkid_set_tag(dev, "TYPE", type, 0);
Ricardo M. Correiac9bad3c2008-05-30 19:20:47 +01001649
Theodore Ts'o12b3c8e2005-05-07 14:38:10 -04001650 DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
Matthias Andree12a829d2006-05-30 01:48:51 +02001651 dev->bid_name, (long long)st.st_rdev, type));
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001652 }
1653
Jim Meyering45e338f2009-02-23 18:07:50 +01001654 free(probe.sbbuf);
1655 free(probe.buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001656 if (probe.fd >= 0)
Theodore Ts'o257ace82007-03-06 19:56:18 -05001657 close(probe.fd);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001658
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001659 return dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001660}
1661
Theodore Ts'o78e2edf2003-07-21 19:42:19 -04001662int blkid_known_fstype(const char *fstype)
1663{
1664 struct blkid_magic *id;
1665
1666 for (id = type_array; id->bim_type; id++) {
1667 if (strcmp(fstype, id->bim_type) == 0)
1668 return 1;
1669 }
1670 return 0;
1671}
1672
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001673#ifdef TEST_PROGRAM
1674int main(int argc, char **argv)
1675{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -05001676 blkid_dev dev;
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001677 blkid_cache cache;
Theodore Ts'o79dd2342003-02-22 17:15:20 -05001678 int ret;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001679
1680 if (argc != 2) {
1681 fprintf(stderr, "Usage: %s device\n"
1682 "Probe a single device to determine type\n", argv[0]);
1683 exit(1);
1684 }
Theodore Ts'o79dd2342003-02-22 17:15:20 -05001685 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1686 fprintf(stderr, "%s: error creating cache (%d)\n",
1687 argv[0], ret);
1688 exit(1);
1689 }
Theodore Ts'o98999c32003-02-16 00:47:07 -05001690 dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001691 if (!dev) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001692 printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001693 return (1);
1694 }
Theodore Ts'o1e5630a2005-09-10 15:55:14 -04001695 printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001696 if (dev->bid_label)
Theodore Ts'o1e5630a2005-09-10 15:55:14 -04001697 printf("LABEL='%s'\n", dev->bid_label);
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001698 if (dev->bid_uuid)
Theodore Ts'o1e5630a2005-09-10 15:55:14 -04001699 printf("UUID='%s'\n", dev->bid_uuid);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001700
Theodore Ts'o50b380b2003-02-12 23:51:21 -05001701 blkid_free_dev(dev);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001702 return (0);
1703}
1704#endif