chiark / gitweb /
8e944be0b12cf9116c97fc19275d810bc6da6931
[elogind.git] / extras / volume_id / volume_id / volume_id.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #include <asm/types.h>
39
40 #include "volume_id.h"
41 #include "logging.h"
42 #include "util.h"
43
44 #include "ext.h"
45 #include "reiserfs.h"
46 #include "fat.h"
47 #include "hfs.h"
48 #include "jfs.h"
49 #include "xfs.h"
50 #include "ufs.h"
51 #include "ntfs.h"
52 #include "iso9660.h"
53 #include "udf.h"
54 #include "highpoint.h"
55 #include "isw_raid.h"
56 #include "lsi_raid.h"
57 #include "via_raid.h"
58 #include "silicon_raid.h"
59 #include "nvidia_raid.h"
60 #include "promise_raid.h"
61 #include "luks.h"
62 #include "linux_swap.h"
63 #include "linux_raid.h"
64 #include "lvm.h"
65 #include "cramfs.h"
66 #include "hpfs.h"
67 #include "romfs.h"
68 #include "sysv.h"
69 #include "minix.h"
70 #include "mac.h"
71 #include "msdos.h"
72 #include "ocfs2.h"
73
74 int volume_id_probe_all(struct volume_id *id, unsigned long long off, unsigned long long size)
75 {
76         if (id == NULL)
77                 return -EINVAL;
78
79         /* probe for raid first, cause fs probes may be successful on raid members */
80         if (size) {
81                 if (volume_id_probe_linux_raid(id, off, size) == 0)
82                         goto exit;
83
84                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
85                         goto exit;
86
87                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
88                         goto exit;
89
90                 if (volume_id_probe_via_raid(id, off, size) == 0)
91                         goto exit;
92
93                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
94                         goto exit;
95
96                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
97                         goto exit;
98
99                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
100                         goto exit;
101
102                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
103                         goto exit;
104         }
105
106         if (volume_id_probe_lvm1(id, off) == 0)
107                 goto exit;
108
109         if (volume_id_probe_lvm2(id, off) == 0)
110                 goto exit;
111
112         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
113                 goto exit;
114
115         if (volume_id_probe_luks(id, off) == 0)
116                 goto exit;
117
118         /* signature in the first block, only small buffer needed */
119         if (volume_id_probe_vfat(id, off) == 0)
120                 goto exit;
121
122         if (volume_id_probe_mac_partition_map(id, off) == 0)
123                 goto exit;
124
125         if (volume_id_probe_xfs(id, off) == 0)
126                 goto exit;
127
128         /* fill buffer with maximum */
129         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
130
131         if (volume_id_probe_linux_swap(id, off) == 0)
132                 goto exit;
133
134         if (volume_id_probe_ext(id, off) == 0)
135                 goto exit;
136
137         if (volume_id_probe_reiserfs(id, off) == 0)
138                 goto exit;
139
140         if (volume_id_probe_jfs(id, off) == 0)
141                 goto exit;
142
143         if (volume_id_probe_udf(id, off) == 0)
144                 goto exit;
145
146         if (volume_id_probe_iso9660(id, off) == 0)
147                 goto exit;
148
149         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
150                 goto exit;
151
152         if (volume_id_probe_ufs(id, off) == 0)
153                 goto exit;
154
155         if (volume_id_probe_ntfs(id, off)  == 0)
156                 goto exit;
157
158         if (volume_id_probe_cramfs(id, off) == 0)
159                 goto exit;
160
161         if (volume_id_probe_romfs(id, off) == 0)
162                 goto exit;
163
164         if (volume_id_probe_hpfs(id, off) == 0)
165                 goto exit;
166
167         if (volume_id_probe_sysv(id, off) == 0)
168                 goto exit;
169
170         if (volume_id_probe_minix(id, off) == 0)
171                 goto exit;
172
173         if (volume_id_probe_ocfs2(id, off) == 0)
174                 goto exit;
175
176         return -1;
177
178 exit:
179         /* If the filestystem in recognized, we free the allocated buffers,
180            otherwise they will stay in place for the possible next probe call */
181         volume_id_free_buffer(id);
182
183         return 0;
184 }
185
186 /* open volume by already open file descriptor */
187 struct volume_id *volume_id_open_fd(int fd)
188 {
189         struct volume_id *id;
190
191         id = malloc(sizeof(struct volume_id));
192         if (id == NULL)
193                 return NULL;
194         memset(id, 0x00, sizeof(struct volume_id));
195
196         id->fd = fd;
197
198         return id;
199 }
200
201 /* open volume by device node */
202 struct volume_id *volume_id_open_node(const char *path)
203 {
204         struct volume_id *id;
205         int fd;
206
207         fd = open(path, O_RDONLY);
208         if (fd < 0) {
209                 dbg("unable to open '%s'", path);
210                 return NULL;
211         }
212
213         id = volume_id_open_fd(fd);
214         if (id == NULL)
215                 return NULL;
216
217         /* close fd on device close */
218         id->fd_close = 1;
219
220         return id;
221 }
222
223 /* open volume by major/minor */
224 struct volume_id *volume_id_open_dev_t(dev_t devt)
225 {
226         struct volume_id *id;
227         __u8 tmp_node[VOLUME_ID_PATH_MAX];
228
229         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
230                  "/dev/.volume_id-%u-%u-%u", getpid(), major(devt), minor(devt));
231         tmp_node[VOLUME_ID_PATH_MAX] = '\0';
232
233         /* create tempory node to open the block device */
234         unlink(tmp_node);
235         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
236                 return NULL;
237
238         id = volume_id_open_node(tmp_node);
239
240         unlink(tmp_node);
241
242         return id;
243 }
244
245 void volume_id_close(struct volume_id *id)
246 {
247         if (id == NULL)
248                 return;
249
250         if (id->fd_close != 0)
251                 close(id->fd);
252
253         volume_id_free_buffer(id);
254
255         if (id->partitions != NULL)
256                 free(id->partitions);
257
258         free(id);
259 }