chiark / gitweb /
fd95e7d3b0c557848e43c35d8227250109122e6b
[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                 if (volume_id_probe_adaptec_raid(id, off, size) == 0)
74                         goto found;
75
76                 if (volume_id_probe_jmicron_raid(id, off, size) == 0)
77                         goto found;
78         }
79
80         if (volume_id_probe_lvm1(id, off) == 0)
81                 goto found;
82
83         if (volume_id_probe_lvm2(id, off) == 0)
84                 goto found;
85
86         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
87                 goto found;
88
89         return -1;
90
91 found:
92         /* If recognized, we free the allocated buffers */
93         volume_id_free_buffer(id);
94         return 0;
95 }
96
97 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
98 {
99         if (id == NULL)
100                 return -EINVAL;
101
102         info("probing at offset 0x%llx, size 0x%llx",
103             (unsigned long long) off, (unsigned long long) size);
104
105         if (volume_id_probe_luks(id, off) == 0)
106                 goto found;
107
108         if (volume_id_probe_vfat(id, off) == 0)
109                 goto found;
110
111         if (volume_id_probe_xfs(id, off) == 0)
112                 goto found;
113
114         /* fill buffer with maximum */
115         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
116
117         if (volume_id_probe_linux_swap(id, off) == 0)
118                 goto found;
119
120         if (volume_id_probe_ext(id, off) == 0)
121                 goto found;
122
123         if (volume_id_probe_reiserfs(id, off) == 0)
124                 goto found;
125
126         if (volume_id_probe_jfs(id, off) == 0)
127                 goto found;
128
129         if (volume_id_probe_udf(id, off) == 0)
130                 goto found;
131
132         if (volume_id_probe_iso9660(id, off) == 0)
133                 goto found;
134
135         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
136                 goto found;
137
138         if (volume_id_probe_ufs(id, off) == 0)
139                 goto found;
140
141         if (volume_id_probe_ntfs(id, off)  == 0)
142                 goto found;
143
144         if (volume_id_probe_cramfs(id, off) == 0)
145                 goto found;
146
147         if (volume_id_probe_romfs(id, off) == 0)
148                 goto found;
149
150         if (volume_id_probe_hpfs(id, off) == 0)
151                 goto found;
152
153         if (volume_id_probe_sysv(id, off) == 0)
154                 goto found;
155
156         if (volume_id_probe_minix(id, off) == 0)
157                 goto found;
158
159         if (volume_id_probe_ocfs1(id, off) == 0)
160                 goto found;
161
162         if (volume_id_probe_ocfs2(id, off) == 0)
163                 goto found;
164
165         if (volume_id_probe_vxfs(id, off) == 0)
166                 goto found;
167
168         if (volume_id_probe_squashfs(id, off) == 0)
169                 goto found;
170
171         if (volume_id_probe_netware(id, off) == 0)
172                 goto found;
173
174         return -1;
175
176 found:
177         /* If recognized, we free the allocated buffers */
178         volume_id_free_buffer(id);
179         return 0;
180 }
181
182 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
183 {
184         if (id == NULL)
185                 return -EINVAL;
186
187         if (volume_id_probe_raid(id, off, size) == 0)
188                 return 0;
189
190         if (volume_id_probe_filesystem(id, off, size) == 0)
191                 return 0;
192
193         return -1;
194 }
195
196 /* open volume by already open file descriptor */
197 struct volume_id *volume_id_open_fd(int fd)
198 {
199         struct volume_id *id;
200
201         id = malloc(sizeof(struct volume_id));
202         if (id == NULL)
203                 return NULL;
204         memset(id, 0x00, sizeof(struct volume_id));
205
206         id->fd = fd;
207
208         return id;
209 }
210
211 /* open volume by device node */
212 struct volume_id *volume_id_open_node(const char *path)
213 {
214         struct volume_id *id;
215         int fd;
216
217         fd = open(path, O_RDONLY);
218         if (fd < 0) {
219                 dbg("unable to open '%s'", path);
220                 return NULL;
221         }
222
223         id = volume_id_open_fd(fd);
224         if (id == NULL)
225                 return NULL;
226
227         /* close fd on device close */
228         id->fd_close = 1;
229
230         return id;
231 }
232
233 void volume_id_close(struct volume_id *id)
234 {
235         if (id == NULL)
236                 return;
237
238         if (id->fd_close != 0)
239                 close(id->fd);
240
241         volume_id_free_buffer(id);
242
243         free(id);
244 }