chiark / gitweb /
vol_id: use libvolume_id prober list for --probe-all
[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 it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "util.h"
30
31 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
32
33 struct prober {
34         const char *type;
35         volume_id_probe_fn_t prober;
36 };
37
38 static const struct prober prober_raid[] = {
39         {"linux_raid_member", volume_id_probe_linux_raid },
40         {"ddf_raid_member", volume_id_probe_ddf_raid },
41         {"isw_raid_member", volume_id_probe_intel_software_raid },
42         {"lsi_mega_raid_member", volume_id_probe_lsi_mega_raid },
43         {"via_raid_member", volume_id_probe_via_raid },
44         {"silicon_medley_raid_member", volume_id_probe_silicon_medley_raid },
45         {"nvidia_raid_member", volume_id_probe_nvidia_raid },
46         {"promise_fasttrack_raid_member", volume_id_probe_promise_fasttrack_raid },
47         {"highpoint_raid_member", volume_id_probe_highpoint_45x_raid },
48         {"adaptec_raid_member", volume_id_probe_adaptec_raid },
49         {"jmicron_raid_member", volume_id_probe_jmicron_raid },
50         {"LVM1_member", volume_id_probe_lvm1 },
51         {"LVM2_member", volume_id_probe_lvm2 },
52         {"highpoint_raid_member", volume_id_probe_highpoint_37x_raid },
53 };
54
55 static const struct prober prober_filesystem[] = {
56         { "vfat", volume_id_probe_vfat },
57         { "linux_swap", volume_id_probe_linux_swap },
58         { "luks", volume_id_probe_luks },
59         { "xfs", volume_id_probe_xfs },
60         { "ext", volume_id_probe_ext },
61         { "reiserfs", volume_id_probe_reiserfs },
62         { "jfs", volume_id_probe_jfs },
63         { "udf", volume_id_probe_udf },
64         { "iso9660", volume_id_probe_iso9660 },
65         { "hfs", volume_id_probe_hfs_hfsplus },
66         { "ufs", volume_id_probe_ufs },
67         { "ntfs", volume_id_probe_ntfs },
68         { "cramfs", volume_id_probe_cramfs },
69         { "romfs", volume_id_probe_romfs },
70         { "hpfs", volume_id_probe_hpfs },
71         { "sysv", volume_id_probe_sysv },
72         { "minix", volume_id_probe_minix },
73         { "ocfs1", volume_id_probe_ocfs1 },
74         { "ocfs2", volume_id_probe_ocfs2 },
75         { "vxfs", volume_id_probe_vxfs },
76         { "squashfs", volume_id_probe_squashfs },
77         { "netware", volume_id_probe_netware },
78         { "gfs", volume_id_probe_gfs },
79         { "gfs2", volume_id_probe_gfs2 },
80 };
81
82 /* the user can overwrite this log function */
83 static void default_log(int priority, const char *file, int line, const char *format, ...)
84 {
85         return;
86 }
87
88 volume_id_log_fn_t volume_id_log_fn = default_log;
89
90 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
91 {
92         unsigned int i;
93
94         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
95                 if (strcmp(type, prober_raid[i].type) == 0)
96                         return &prober_raid[i].prober;
97         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
98                 if (strcmp(type, prober_filesystem[i].type) == 0)
99                         return &prober_filesystem[i].prober;
100         return NULL;
101 }
102
103 int volume_id_get_label(struct volume_id *id, const char **label)
104 {
105         if (id == NULL)
106                 return -EINVAL;
107         if (label == NULL)
108                 return -EINVAL;
109         if (id->usage_id == VOLUME_ID_UNUSED)
110                 return 0;
111
112         *label = id->label;
113         return 1;
114 }
115
116 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
117 {
118         if (id == NULL)
119                 return -EINVAL;
120         if (label == NULL)
121                 return -EINVAL;
122         if (len == NULL)
123                 return -EINVAL;
124         if (id->usage_id == VOLUME_ID_UNUSED)
125                 return 0;
126
127         *label = id->label_raw;
128         *len = id->label_raw_len;
129         return 1;
130 }
131
132 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
133 {
134         if (id == NULL)
135                 return -EINVAL;
136         if (uuid == NULL)
137                 return -EINVAL;
138         if (id->usage_id == VOLUME_ID_UNUSED)
139                 return 0;
140
141         *uuid = id->uuid;
142         return 1;
143 }
144
145 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
146 {
147         if (id == NULL)
148                 return -EINVAL;
149         if (uuid == NULL)
150                 return -EINVAL;
151         if (len == NULL)
152                 return -EINVAL;
153         if (id->usage_id == VOLUME_ID_UNUSED)
154                 return 0;
155
156         *uuid = id->uuid_raw;
157         *len = id->uuid_raw_len;
158         return 1;
159 }
160
161 int volume_id_get_usage(struct volume_id *id, const char **usage)
162 {
163         if (id == NULL)
164                 return -EINVAL;
165         if (usage == NULL)
166                 return -EINVAL;
167         if (id->usage_id == VOLUME_ID_UNUSED)
168                 return 0;
169
170         *usage = id->usage;
171         return 1;
172 }
173
174 int volume_id_get_type(struct volume_id *id, const char **type)
175 {
176         if (id == NULL)
177                 return -EINVAL;
178         if (type == NULL)
179                 return -EINVAL;
180         if (id->usage_id == VOLUME_ID_UNUSED)
181                 return 0;
182
183         *type = id->type;
184         return 1;
185 }
186
187 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
188 {
189         if (id == NULL)
190                 return -EINVAL;
191         if (type_version == NULL)
192                 return -EINVAL;
193         if (id->usage_id == VOLUME_ID_UNUSED)
194                 return 0;
195
196         *type_version = id->type_version;
197         return 1;
198 }
199
200 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
201 {
202         unsigned int i;
203
204         if (id == NULL)
205                 return -EINVAL;
206
207         info("probing at offset 0x%llx, size 0x%llx",
208             (unsigned long long) off, (unsigned long long) size);
209
210         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
211                 if (prober_raid[i].prober(id, off, size) == 0)
212                         goto found;
213         return -1;
214
215 found:
216         /* If recognized, we free the allocated buffers */
217         volume_id_free_buffer(id);
218         return 0;
219 }
220
221 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
222 {
223         unsigned int i;
224
225         if (id == NULL)
226                 return -EINVAL;
227
228         info("probing at offset 0x%llx, size 0x%llx",
229             (unsigned long long) off, (unsigned long long) size);
230
231         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
232                 if (prober_filesystem[i].prober(id, off, size) == 0)
233                         goto found;
234         return -1;
235
236 found:
237         /* If recognized, we free the allocated buffers */
238         volume_id_free_buffer(id);
239         return 0;
240 }
241
242 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
243 {
244         if (id == NULL)
245                 return -EINVAL;
246
247         /* probe for raid first, because fs probes may be successful on raid members */
248         if (volume_id_probe_raid(id, off, size) == 0)
249                 return 0;
250
251         if (volume_id_probe_filesystem(id, off, size) == 0)
252                 return 0;
253
254         return -1;
255 }
256
257 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
258                            struct volume_id *id, uint64_t off, uint64_t size,
259                            void *data)
260 {
261         unsigned int i;
262
263         if (all_probers_fn == NULL)
264                 return;
265
266         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
267                 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
268                         goto out;
269         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
270                 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
271                         goto out;
272 out:
273         return;
274 }
275
276 /* open volume by already open file descriptor */
277 struct volume_id *volume_id_open_fd(int fd)
278 {
279         struct volume_id *id;
280
281         id = malloc(sizeof(struct volume_id));
282         if (id == NULL)
283                 return NULL;
284         memset(id, 0x00, sizeof(struct volume_id));
285
286         id->fd = fd;
287
288         return id;
289 }
290
291 /* open volume by device node */
292 struct volume_id *volume_id_open_node(const char *path)
293 {
294         struct volume_id *id;
295         int fd;
296
297         fd = open(path, O_RDONLY);
298         if (fd < 0) {
299                 dbg("unable to open '%s'", path);
300                 return NULL;
301         }
302
303         id = volume_id_open_fd(fd);
304         if (id == NULL)
305                 return NULL;
306
307         /* close fd on device close */
308         id->fd_close = 1;
309
310         return id;
311 }
312
313 void volume_id_close(struct volume_id *id)
314 {
315         if (id == NULL)
316                 return;
317
318         if (id->fd_close != 0)
319                 close(id->fd);
320
321         volume_id_free_buffer(id);
322
323         free(id);
324 }