chiark / gitweb /
delete vol_id and require util-linux-ng's blkid
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume label and uuid
3  *
4  * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
35
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
37
38 struct prober {
39         volume_id_probe_fn_t prober;
40         const char *name[4];
41 };
42
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", } },
58 };
59
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", } },
87 };
88
89 /* the user can overwrite this log function */
90 static void default_log(int priority, const char *file, int line, const char *format, ...)
91 {
92         return;
93 }
94
95 volume_id_log_fn_t volume_id_log_fn = default_log;
96
97 /**
98  * volume_id_get_prober_by_type:
99  * @type: Type string.
100  *
101  * Lookup the probing function for a specific type.
102  *
103  * Returns: The probing function for the given type, #NULL otherwise.
104  **/
105 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
106 {
107         unsigned int p, n;
108
109         if (type == NULL)
110                 return NULL;
111
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;
120         return NULL;
121 }
122
123 /**
124  * volume_id_get_label:
125  * @id: Probing context.
126  * @label: Label string. Must not be freed by the caller.
127  *
128  * Get the label string after a successful probe. Unicode
129  * is translated to UTF-8.
130  *
131  * Returns: 1 if the value was set, 0 otherwise.
132  **/
133 int volume_id_get_label(struct volume_id *id, const char **label)
134 {
135         if (id == NULL)
136                 return 0;
137         if (label == NULL)
138                 return 0;
139         if (id->usage_id == VOLUME_ID_UNUSED)
140                 return 0;
141
142         *label = id->label;
143         return 1;
144 }
145
146 /**
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.
151  *
152  * Get the raw label byte array after a successful probe. It may
153  * contain undecoded multibyte character streams.
154  *
155  * Returns: 1 if the value was set, 0 otherwise.
156  **/
157 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
158 {
159         if (id == NULL)
160                 return 0;
161         if (label == NULL)
162                 return 0;
163         if (len == NULL)
164                 return 0;
165         if (id->usage_id == VOLUME_ID_UNUSED)
166                 return 0;
167
168         *label = id->label_raw;
169         *len = id->label_raw_len;
170         return 1;
171 }
172
173 /**
174  * volume_id_get_uuid:
175  * @id: Probing context.
176  * @uuid: UUID string. Must not be freed by the caller.
177  *
178  * Get the raw UUID string after a successful probe.
179  *
180  * Returns: 1 if the value was set, 0 otherwise.
181  **/
182 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
183 {
184         if (id == NULL)
185                 return 0;
186         if (uuid == NULL)
187                 return 0;
188         if (id->usage_id == VOLUME_ID_UNUSED)
189                 return 0;
190
191         *uuid = id->uuid;
192         return 1;
193 }
194
195 /**
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.
200  *
201  * Get the raw UUID byte array after a successful probe. It may
202  * contain unconverted endianes values.
203  *
204  * Returns: 1 if the value was set, 0 otherwise.
205  **/
206 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
207 {
208         if (id == NULL)
209                 return 0;
210         if (uuid == NULL)
211                 return 0;
212         if (len == NULL)
213                 return 0;
214         if (id->usage_id == VOLUME_ID_UNUSED)
215                 return 0;
216
217         *uuid = id->uuid_raw;
218         *len = id->uuid_raw_len;
219         return 1;
220 }
221
222 int volume_id_get_uuid_sub(struct volume_id *id, const char **uuid)
223 {
224         if (id == NULL)
225                 return 0;
226         if (uuid == NULL)
227                 return 0;
228         if (id->usage_id == VOLUME_ID_UNUSED)
229                 return 0;
230
231         *uuid = id->uuid_sub;
232         return 1;
233 }
234
235 /**
236  * volume_id_get_usage:
237  * @id: Probing context.
238  * @usage: Usage string. Must not be freed by the caller.
239  *
240  * Get the usage string after a successful probe.
241  *
242  * Returns: 1 if the value was set, 0 otherwise.
243  **/
244 int volume_id_get_usage(struct volume_id *id, const char **usage)
245 {
246         if (id == NULL)
247                 return 0;
248         if (usage == NULL)
249                 return 0;
250         if (id->usage_id == VOLUME_ID_UNUSED)
251                 return 0;
252
253         *usage = id->usage;
254         return 1;
255 }
256
257 /**
258  * volume_id_get_type:
259  * @id: Probing context
260  * @type: Type string. Must not be freed by the caller.
261  *
262  * Get the type string after a successful probe.
263  *
264  * Returns: 1 if the value was set, 0 otherwise.
265  **/
266 int volume_id_get_type(struct volume_id *id, const char **type)
267 {
268         if (id == NULL)
269                 return 0;
270         if (type == NULL)
271                 return 0;
272         if (id->usage_id == VOLUME_ID_UNUSED)
273                 return 0;
274
275         *type = id->type;
276         return 1;
277 }
278
279 /**
280  * volume_id_get_type_version:
281  * @id: Probing context.
282  * @type_version: Type version string. Must not be freed by the caller.
283  *
284  * Get the Type version string after a successful probe.
285  *
286  * Returns: 1 if the value was set, 0 otherwise.
287  **/
288 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
289 {
290         if (id == NULL)
291                 return 0;
292         if (type_version == NULL)
293                 return 0;
294         if (id->usage_id == VOLUME_ID_UNUSED)
295                 return 0;
296
297         *type_version = id->type_version;
298         return 1;
299 }
300
301 static int needs_encoding(const char c)
302 {
303         if ((c >= '0' && c <= '9') ||
304             (c >= 'A' && c <= 'Z') ||
305             (c >= 'a' && c <= 'z') ||
306             strchr(ALLOWED_CHARS, c))
307                 return 0;
308         return 1;
309 }
310
311 /**
312  * volume_id_encode_string:
313  * @str: Input string to be encoded.
314  * @str_enc: Target string to store the encoded input.
315  * @len: Location to store the encoded string. The target string,
316  * which may be four times as long as the input string.
317  *
318  * Encode all potentially unsafe characters of a string to the
319  * corresponding hex value prefixed by '\x'.
320  *
321  * Returns: 1 if the entire string was copied, 0 otherwise.
322  **/
323 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
324 {
325         size_t i, j;
326
327         if (str == NULL || str_enc == NULL || len == 0)
328                 return 0;
329
330         str_enc[0] = '\0';
331         for (i = 0, j = 0; str[i] != '\0'; i++) {
332                 int seqlen;
333
334                 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
335                 if (seqlen > 1) {
336                         memcpy(&str_enc[j], &str[i], seqlen);
337                         j += seqlen;
338                         i += (seqlen-1);
339                 } else if (str[i] == '\\' || needs_encoding(str[i])) {
340                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
341                         j += 4;
342                 } else {
343                         str_enc[j] = str[i];
344                         j++;
345                 }
346                 if (j+3 >= len)
347                         goto err;
348         }
349         str_enc[j] = '\0';
350         return 1;
351 err:
352         return 0;
353 }
354
355 /* run only once into a timeout for unreadable devices */
356 static int device_is_readable(struct volume_id *id, uint64_t off)
357 {
358         if (volume_id_get_buffer(id, off, 0x200) != NULL)
359                 return 1;
360         return 0;
361 }
362
363 /**
364  * volume_id_probe_raid:
365  * @id: Probing context.
366  * @off: Probing offset relative to the start of the device.
367  * @size: Total size of the device.
368  *
369  * Probe device for all known raid signatures.
370  *
371  * Returns: 0 on successful probe, otherwise negative value.
372  **/
373 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
374 {
375         unsigned int i;
376
377         if (id == NULL)
378                 return -EINVAL;
379
380         if (!device_is_readable(id, off))
381                 return -1;
382
383         info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
384
385         for (i = 0; i < ARRAY_SIZE(prober_raid); i++) {
386                 if (prober_raid[i].prober(id, off, size) == 0) {
387                         info("signature '%s' detected\n", id->type);
388                         goto found;
389                 }
390         }
391         return -1;
392
393 found:
394         /* If recognized, we free the allocated buffers */
395         volume_id_free_buffer(id);
396         return 0;
397 }
398
399 static void volume_id_reset_result(struct volume_id *id)
400 {
401         id->label_raw_len = 0;
402         id->label[0] = '\0';
403         id->uuid_raw_len = 0;
404         id->uuid[0] = '\0';
405         id->usage_id = VOLUME_ID_UNUSED;
406         id->usage = NULL;
407         id->type = NULL;
408         id->type_version[0] = '\0';
409 }
410
411 /**
412  * volume_id_probe_filesystem:
413  * @id: Probing context.
414  * @off: Probing offset relative to the start of the device.
415  * @size: Total size of the device.
416  *
417  * Probe device for all known filesystem signatures.
418  *
419  * Returns: 0 on successful probe, otherwise negative value.
420  **/
421 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
422 {
423         unsigned int i;
424
425         if (id == NULL)
426                 return -EINVAL;
427
428         if (!device_is_readable(id, off))
429                 return -1;
430
431         info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
432
433         /*
434          * We probe for all known filesystems to find conflicting signatures. If
435          * we find multiple matching signatures and one of the detected filesystem
436          * types claims that it can not co-exist with any other filesystem type,
437          * we do not return a probing result.
438          *
439          * We can not afford to mount a volume with the wrong filesystem code and
440          * possibly corrupt it. Linux sytems have the problem of dozens of possible
441          * filesystem types, and volumes with left-over signatures from former
442          * filesystem types. Invalid signatures need to be removed from the volume
443          * to make the filesystem detection successful.
444          *
445          * We do not want to read that many bytes from probed floppies, skip volumes
446          * smaller than a usual floppy disk.
447          */
448         if (size > 1440 * 1024) {
449                 int found = 0;
450                 int force_unique_result = 0;
451                 int first_match = -1;
452
453                 volume_id_reset_result(id);
454                 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
455                         int match;
456
457                         match = (prober_filesystem[i].prober(id, off, size) == 0);
458                         if (match) {
459                                 info("signature '%s' %i detected\n", id->type, i);
460                                 if (id->force_unique_result)
461                                         force_unique_result = 1;
462                                 if (found > 0 && force_unique_result) {
463                                         info("conflicting signatures found, skip results\n");
464                                         return -1;
465                                 }
466                                 found++;
467                                 if (first_match < 0)
468                                         first_match = i;
469                         }
470                 }
471                 if (found < 1)
472                         return -1;
473                 if (found == 1)
474                         goto found;
475                 if (found > 1) {
476                         volume_id_reset_result(id);
477                         info("re-read first match metadata %i\n", first_match);
478                         if (prober_filesystem[first_match].prober(id, off, size) == 0)
479                                 goto found;
480                         return -1;
481                 }
482         }
483
484         /* return the first match */
485         volume_id_reset_result(id);
486         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
487                 if (prober_filesystem[i].prober(id, off, size) == 0) {
488                         info("signature '%s' detected\n", id->type);
489                         goto found;
490                 }
491         }
492         return -1;
493 found:
494         /* If recognized, we free the allocated buffers */
495         volume_id_free_buffer(id);
496         return 0;
497 }
498
499 /**
500  * volume_id_probe_all:
501  * @id: Probing context.
502  * @off: Probing offset relative to the start of the device.
503  * @size: Total size of the device.
504  *
505  * Probe device for all known raid and filesystem signatures.
506  *
507  * Returns: 0 on successful probe, otherwise negative value.
508  **/
509 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
510 {
511         if (id == NULL)
512                 return -EINVAL;
513
514         if (!device_is_readable(id, off))
515                 return -1;
516
517         /* probe for raid first, because fs probes may be successful on raid members */
518         if (volume_id_probe_raid(id, off, size) == 0)
519                 return 0;
520
521         if (volume_id_probe_filesystem(id, off, size) == 0)
522                 return 0;
523
524         return -1;
525 }
526
527 /**
528  * volume_id_probe_raid:
529  * @all_probers_fn: prober function to called for all known probing routines.
530  * @id: Context passed to prober function.
531  * @off: Offset value passed to prober function.
532  * @size: Size value passed to prober function.
533  * @data: Arbitrary data passed to the prober function.
534  *
535  * Run a custom function for all known probing routines.
536  **/
537 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
538                            struct volume_id *id, uint64_t off, uint64_t size,
539                            void *data)
540 {
541         unsigned int i;
542
543         if (all_probers_fn == NULL)
544                 return;
545
546         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
547                 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
548                         goto out;
549         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
550                 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
551                         goto out;
552 out:
553         return;
554 }
555
556 /**
557  * volume_id_open_fd:
558  * @id: Probing context.
559  * @fd: Open file descriptor of device to read from.
560  *
561  * Create the context for probing.
562  *
563  * Returns: Probing context, or #NULL on failure.
564  **/
565 struct volume_id *volume_id_open_fd(int fd)
566 {
567         struct volume_id *id;
568
569         id = calloc(1, sizeof(struct volume_id));
570         if (id == NULL)
571                 return NULL;
572
573         id->fd = fd;
574
575         return id;
576 }
577
578 /**
579  * volume_id_close:
580  * @id: Probing context.
581  *
582  * Release probing context and free all associated data.
583  */
584 void volume_id_close(struct volume_id *id)
585 {
586         if (id == NULL)
587                 return;
588
589         volume_id_free_buffer(id);
590
591         free(id);
592 }