chiark / gitweb /
33fd890dfd61fdedba1369d2c46625a2e59419dd
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume label and uuid
3  *
4  * Copyright (C) 2005 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 /* the user can overwrite this log function */
32 static void default_log(int priority, const char *file, int line, const char *format, ...)
33 {
34         return;
35 }
36
37 volume_id_log_fn_t volume_id_log_fn = default_log;
38
39 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
40 {
41         if (id == NULL)
42                 return -EINVAL;
43
44         info("probing at offset 0x%llx, size 0x%llx",
45             (unsigned long long) off, (unsigned long long) size);
46
47         /* probe for raid first, because fs probes may be successful on raid members */
48         if (size) {
49                 if (volume_id_probe_linux_raid(id, off, size) == 0)
50                         goto found;
51
52                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
53                         goto found;
54
55                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
56                         goto found;
57
58                 if (volume_id_probe_via_raid(id, off, size) == 0)
59                         goto found;
60
61                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
62                         goto found;
63
64                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
65                         goto found;
66
67                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
68                         goto found;
69
70                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
71                         goto found;
72         }
73
74         if (volume_id_probe_lvm1(id, off) == 0)
75                 goto found;
76
77         if (volume_id_probe_lvm2(id, off) == 0)
78                 goto found;
79
80         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
81                 goto found;
82
83         return -1;
84
85 found:
86         /* If recognized, we free the allocated buffers */
87         volume_id_free_buffer(id);
88         return 0;
89 }
90
91 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
92 {
93         if (id == NULL)
94                 return -EINVAL;
95
96         info("probing at offset 0x%llx, size 0x%llx",
97             (unsigned long long) off, (unsigned long long) size);
98
99         if (volume_id_probe_luks(id, off) == 0)
100                 goto found;
101
102         /* signature in the first block, only small buffer needed */
103         if (volume_id_probe_vfat(id, off) == 0)
104                 goto found;
105
106         if (volume_id_probe_xfs(id, off) == 0)
107                 goto found;
108
109         /* fill buffer with maximum */
110         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
111
112         if (volume_id_probe_linux_swap(id, off) == 0)
113                 goto found;
114
115         if (volume_id_probe_ext(id, off) == 0)
116                 goto found;
117
118         if (volume_id_probe_reiserfs(id, off) == 0)
119                 goto found;
120
121         if (volume_id_probe_jfs(id, off) == 0)
122                 goto found;
123
124         if (volume_id_probe_udf(id, off) == 0)
125                 goto found;
126
127         if (volume_id_probe_iso9660(id, off) == 0)
128                 goto found;
129
130         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
131                 goto found;
132
133         if (volume_id_probe_ufs(id, off) == 0)
134                 goto found;
135
136         if (volume_id_probe_ntfs(id, off)  == 0)
137                 goto found;
138
139         if (volume_id_probe_cramfs(id, off) == 0)
140                 goto found;
141
142         if (volume_id_probe_romfs(id, off) == 0)
143                 goto found;
144
145         if (volume_id_probe_hpfs(id, off) == 0)
146                 goto found;
147
148         if (volume_id_probe_sysv(id, off) == 0)
149                 goto found;
150
151         if (volume_id_probe_minix(id, off) == 0)
152                 goto found;
153
154         if (volume_id_probe_ocfs1(id, off) == 0)
155                 goto found;
156
157         if (volume_id_probe_ocfs2(id, off) == 0)
158                 goto found;
159
160         if (volume_id_probe_vxfs(id, off) == 0)
161                 goto found;
162
163         if (volume_id_probe_squashfs(id, off) == 0)
164                 goto found;
165
166         return -1;
167
168 found:
169         /* If recognized, we free the allocated buffers */
170         volume_id_free_buffer(id);
171         return 0;
172 }
173
174 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
175 {
176         if (id == NULL)
177                 return -EINVAL;
178
179         if (volume_id_probe_raid(id, off, size) == 0)
180                 return 0;
181
182         if (volume_id_probe_filesystem(id, off, size) == 0)
183                 return 0;
184
185         return -1;
186 }
187
188 /* open volume by already open file descriptor */
189 struct volume_id *volume_id_open_fd(int fd)
190 {
191         struct volume_id *id;
192
193         id = malloc(sizeof(struct volume_id));
194         if (id == NULL)
195                 return NULL;
196         memset(id, 0x00, sizeof(struct volume_id));
197
198         id->fd = fd;
199
200         return id;
201 }
202
203 /* open volume by device node */
204 struct volume_id *volume_id_open_node(const char *path)
205 {
206         struct volume_id *id;
207         int fd;
208
209         fd = open(path, O_RDONLY);
210         if (fd < 0) {
211                 dbg("unable to open '%s'", path);
212                 return NULL;
213         }
214
215         id = volume_id_open_fd(fd);
216         if (id == NULL)
217                 return NULL;
218
219         /* close fd on device close */
220         id->fd_close = 1;
221
222         return id;
223 }
224
225 void volume_id_close(struct volume_id *id)
226 {
227         if (id == NULL)
228                 return;
229
230         if (id->fd_close != 0)
231                 close(id->fd);
232
233         volume_id_free_buffer(id);
234
235         free(id);
236 }