chiark / gitweb /
472ea246112117a4fd3ebcf70b548ad4fcb5abca
[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         if (volume_id_probe_netware(id, off) == 0)
167                 goto found;
168
169         return -1;
170
171 found:
172         /* If recognized, we free the allocated buffers */
173         volume_id_free_buffer(id);
174         return 0;
175 }
176
177 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
178 {
179         if (id == NULL)
180                 return -EINVAL;
181
182         if (volume_id_probe_raid(id, off, size) == 0)
183                 return 0;
184
185         if (volume_id_probe_filesystem(id, off, size) == 0)
186                 return 0;
187
188         return -1;
189 }
190
191 /* open volume by already open file descriptor */
192 struct volume_id *volume_id_open_fd(int fd)
193 {
194         struct volume_id *id;
195
196         id = malloc(sizeof(struct volume_id));
197         if (id == NULL)
198                 return NULL;
199         memset(id, 0x00, sizeof(struct volume_id));
200
201         id->fd = fd;
202
203         return id;
204 }
205
206 /* open volume by device node */
207 struct volume_id *volume_id_open_node(const char *path)
208 {
209         struct volume_id *id;
210         int fd;
211
212         fd = open(path, O_RDONLY);
213         if (fd < 0) {
214                 dbg("unable to open '%s'", path);
215                 return NULL;
216         }
217
218         id = volume_id_open_fd(fd);
219         if (id == NULL)
220                 return NULL;
221
222         /* close fd on device close */
223         id->fd_close = 1;
224
225         return id;
226 }
227
228 void volume_id_close(struct volume_id *id)
229 {
230         if (id == NULL)
231                 return;
232
233         if (id->fd_close != 0)
234                 close(id->fd);
235
236         volume_id_free_buffer(id);
237
238         free(id);
239 }