2 * volume_id - reads volume label and uuid
4 * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
39 volume_id_probe_fn_t prober;
43 static const struct prober prober_raid[] = {
44 { volume_id_probe_linux_raid, { "linux_raid", } },
45 { volume_id_probe_ddf_raid, { "ddf_raid", } },
46 { volume_id_probe_intel_software_raid, { "isw_raid", } },
47 { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
48 { volume_id_probe_via_raid, { "via_raid", } },
49 { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
50 { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
51 { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
52 { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
53 { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
54 { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
55 { volume_id_probe_lvm1, { "lvm1", } },
56 { volume_id_probe_lvm2, { "lvm2", } },
57 { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
60 static const struct prober prober_filesystem[] = {
61 { volume_id_probe_vfat, { "vfat", } },
62 { volume_id_probe_linux_swap, { "swap", } },
63 { volume_id_probe_luks, { "luks", } },
64 { volume_id_probe_xfs, { "xfs", } },
65 { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
66 { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
67 { volume_id_probe_jfs, { "jfs", } },
68 { volume_id_probe_udf, { "udf", } },
69 { volume_id_probe_iso9660, { "iso9660", } },
70 { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
71 { volume_id_probe_ufs, { "ufs", } },
72 { volume_id_probe_ntfs, { "ntfs", } },
73 { volume_id_probe_cramfs, { "cramfs", } },
74 { volume_id_probe_romfs, { "romfs", } },
75 { volume_id_probe_hpfs, { "hpfs", } },
76 { volume_id_probe_sysv, { "sysv", "xenix", } },
77 { volume_id_probe_minix, { "minix", } },
78 { volume_id_probe_gfs, { "gfs", } },
79 { volume_id_probe_gfs2, { "gfs2", } },
80 { volume_id_probe_ocfs1, { "ocfs1", } },
81 { volume_id_probe_ocfs2, { "ocfs2", } },
82 { volume_id_probe_vxfs, { "vxfs", } },
83 { volume_id_probe_squashfs, { "squashfs", } },
84 { volume_id_probe_netware, { "netware", } },
85 { volume_id_probe_oracleasm, { "oracleasm", } },
86 { volume_id_probe_btrfs, { "btrfs", } },
89 /* the user can overwrite this log function */
90 static void default_log(int priority, const char *file, int line, const char *format, ...)
95 volume_id_log_fn_t volume_id_log_fn = default_log;
98 * volume_id_get_prober_by_type:
101 * Lookup the probing function for a specific type.
103 * Returns: The probing function for the given type, #NULL otherwise.
105 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
112 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
113 for (n = 0; prober_raid[p].name[n] != NULL; n++)
114 if (strcmp(type, prober_raid[p].name[n]) == 0)
115 return &prober_raid[p].prober;
116 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
117 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
118 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
119 return &prober_filesystem[p].prober;
124 * volume_id_get_label:
125 * @id: Probing context.
126 * @label: Label string. Must not be freed by the caller.
128 * Get the label string after a successful probe. Unicode
129 * is translated to UTF-8.
131 * Returns: 1 if the value was set, 0 otherwise.
133 int volume_id_get_label(struct volume_id *id, const char **label)
139 if (id->usage_id == VOLUME_ID_UNUSED)
147 * volume_id_get_label_raw:
148 * @id: Probing context.
149 * @label: Label byte array. Must not be freed by the caller.
150 * @len: Length of raw label byte array.
152 * Get the raw label byte array after a successful probe. It may
153 * contain undecoded multibyte character streams.
155 * Returns: 1 if the value was set, 0 otherwise.
157 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
165 if (id->usage_id == VOLUME_ID_UNUSED)
168 *label = id->label_raw;
169 *len = id->label_raw_len;
174 * volume_id_get_uuid:
175 * @id: Probing context.
176 * @uuid: UUID string. Must not be freed by the caller.
178 * Get the raw UUID string after a successful probe.
180 * Returns: 1 if the value was set, 0 otherwise.
182 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
188 if (id->usage_id == VOLUME_ID_UNUSED)
196 * volume_id_get_uuid_raw:
197 * @id: Probing context.
198 * @uuid: UUID byte array. Must not be freed by the caller.
199 * @len: Length of raw UUID byte array.
201 * Get the raw UUID byte array after a successful probe. It may
202 * contain unconverted endianes values.
204 * Returns: 1 if the value was set, 0 otherwise.
206 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
214 if (id->usage_id == VOLUME_ID_UNUSED)
217 *uuid = id->uuid_raw;
218 *len = id->uuid_raw_len;
223 * volume_id_get_usage:
224 * @id: Probing context.
225 * @usage: Usage string. Must not be freed by the caller.
227 * Get the usage string after a successful probe.
229 * Returns: 1 if the value was set, 0 otherwise.
231 int volume_id_get_usage(struct volume_id *id, const char **usage)
237 if (id->usage_id == VOLUME_ID_UNUSED)
245 * volume_id_get_type:
246 * @id: Probing context
247 * @type: Type string. Must not be freed by the caller.
249 * Get the type string after a successful probe.
251 * Returns: 1 if the value was set, 0 otherwise.
253 int volume_id_get_type(struct volume_id *id, const char **type)
259 if (id->usage_id == VOLUME_ID_UNUSED)
267 * volume_id_get_type_version:
268 * @id: Probing context.
269 * @type_version: Type version string. Must not be freed by the caller.
271 * Get the Type version string after a successful probe.
273 * Returns: 1 if the value was set, 0 otherwise.
275 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
279 if (type_version == NULL)
281 if (id->usage_id == VOLUME_ID_UNUSED)
284 *type_version = id->type_version;
288 static int needs_encoding(const char c)
290 if ((c >= '0' && c <= '9') ||
291 (c >= 'A' && c <= 'Z') ||
292 (c >= 'a' && c <= 'z') ||
293 strchr(ALLOWED_CHARS, c))
299 * volume_id_encode_string:
300 * @str: Input string to be encoded.
301 * @str_enc: Target string to store the encoded input.
302 * @len: Location to store the encoded string. The target string,
303 * which may be four times as long as the input string.
305 * Encode all potentially unsafe characters of a string to the
306 * corresponding hex value prefixed by '\x'.
308 * Returns: 1 if the entire string was copied, 0 otherwise.
310 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
314 if (str == NULL || str_enc == NULL || len == 0)
318 for (i = 0, j = 0; str[i] != '\0'; i++) {
321 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
323 memcpy(&str_enc[j], &str[i], seqlen);
326 } else if (str[i] == '\\' || needs_encoding(str[i])) {
327 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
342 /* run only once into a timeout for unreadable devices */
343 static int device_is_readable(struct volume_id *id, uint64_t off)
345 if (volume_id_get_buffer(id, off, 0x200) != NULL)
351 * volume_id_probe_raid:
352 * @id: Probing context.
353 * @off: Probing offset relative to the start of the device.
354 * @size: Total size of the device.
356 * Probe device for all known raid signatures.
358 * Returns: 0 on successful probe, otherwise negative value.
360 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
367 if (!device_is_readable(id, off))
370 info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
372 for (i = 0; i < ARRAY_SIZE(prober_raid); i++) {
373 if (prober_raid[i].prober(id, off, size) == 0) {
374 info("signature '%s' detected\n", id->type);
381 /* If recognized, we free the allocated buffers */
382 volume_id_free_buffer(id);
387 * volume_id_probe_filesystem:
388 * @id: Probing context.
389 * @off: Probing offset relative to the start of the device.
390 * @size: Total size of the device.
392 * Probe device for all known filesystem signatures.
394 * Returns: 0 on successful probe, otherwise negative value.
396 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
403 if (!device_is_readable(id, off))
406 info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
409 * We probe for all known filesystems to find conflicting signatures. If
410 * we find multiple matching signatures and one of the detected filesystem
411 * types claims that it can not co-exist with any other filesystem type,
412 * we do not return a probing result.
414 * We can not afford to mount a volume with the wrong filesystem code and
415 * possibly corrupt it. Linux sytems have the problem of dozens of possible
416 * filesystem types, and volumes with left-over signatures from former
417 * filesystem types. Invalid signatures need to be removed from the volume
418 * to make the filesystem detection successful.
420 * We do not want to read that many bytes from probed floppies, skip volumes
421 * smaller than a usual floppy disk.
423 if (size > 1440 * 1024) {
425 int force_unique_result = 0;
427 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
430 match = (prober_filesystem[i].prober(id, off, size) == 0);
432 info("signature '%s' detected\n", id->type);
433 if (id->force_unique_result)
434 force_unique_result = 1;
435 if (found && force_unique_result) {
436 info("conflicting signatures found, skip results\n");
444 /* return the first match */
445 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
446 if (prober_filesystem[i].prober(id, off, size) == 0) {
447 info("signature '%s' detected\n", id->type);
453 /* If recognized, we free the allocated buffers */
454 volume_id_free_buffer(id);
459 * volume_id_probe_all:
460 * @id: Probing context.
461 * @off: Probing offset relative to the start of the device.
462 * @size: Total size of the device.
464 * Probe device for all known raid and filesystem signatures.
466 * Returns: 0 on successful probe, otherwise negative value.
468 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
473 if (!device_is_readable(id, off))
476 /* probe for raid first, because fs probes may be successful on raid members */
477 if (volume_id_probe_raid(id, off, size) == 0)
480 if (volume_id_probe_filesystem(id, off, size) == 0)
487 * volume_id_probe_raid:
488 * @all_probers_fn: prober function to called for all known probing routines.
489 * @id: Context passed to prober function.
490 * @off: Offset value passed to prober function.
491 * @size: Size value passed to prober function.
492 * @data: Arbitrary data passed to the prober function.
494 * Run a custom function for all known probing routines.
496 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
497 struct volume_id *id, uint64_t off, uint64_t size,
502 if (all_probers_fn == NULL)
505 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
506 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
508 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
509 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
517 * @id: Probing context.
518 * @fd: Open file descriptor of device to read from.
520 * Create the context for probing.
522 * Returns: Probing context, or #NULL on failure.
524 struct volume_id *volume_id_open_fd(int fd)
526 struct volume_id *id;
528 id = calloc(1, sizeof(struct volume_id));
539 * @id: Probing context.
541 * Release probing context and free all associated data.
543 void volume_id_close(struct volume_id *id)
548 volume_id_free_buffer(id);