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