chiark / gitweb /
volume_id: add OVFS Version 1
[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 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 <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28
29 #include "volume_id.h"
30 #include "logging.h"
31 #include "util.h"
32
33 #include "ext.h"
34 #include "reiserfs.h"
35 #include "fat.h"
36 #include "hfs.h"
37 #include "jfs.h"
38 #include "xfs.h"
39 #include "ufs.h"
40 #include "ntfs.h"
41 #include "iso9660.h"
42 #include "udf.h"
43 #include "highpoint.h"
44 #include "isw_raid.h"
45 #include "lsi_raid.h"
46 #include "via_raid.h"
47 #include "silicon_raid.h"
48 #include "nvidia_raid.h"
49 #include "promise_raid.h"
50 #include "luks.h"
51 #include "linux_swap.h"
52 #include "linux_raid.h"
53 #include "lvm.h"
54 #include "cramfs.h"
55 #include "hpfs.h"
56 #include "romfs.h"
57 #include "sysv.h"
58 #include "minix.h"
59 #include "mac.h"
60 #include "msdos.h"
61 #include "ocfs.h"
62
63 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
64 {
65         if (id == NULL)
66                 return -EINVAL;
67
68         /* probe for raid first, cause fs probes may be successful on raid members */
69         if (size) {
70                 if (volume_id_probe_linux_raid(id, off, size) == 0)
71                         goto exit;
72
73                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
74                         goto exit;
75
76                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
77                         goto exit;
78
79                 if (volume_id_probe_via_raid(id, off, size) == 0)
80                         goto exit;
81
82                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
83                         goto exit;
84
85                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
86                         goto exit;
87
88                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
89                         goto exit;
90
91                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
92                         goto exit;
93         }
94
95         if (volume_id_probe_lvm1(id, off) == 0)
96                 goto exit;
97
98         if (volume_id_probe_lvm2(id, off) == 0)
99                 goto exit;
100
101         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
102                 goto exit;
103
104         if (volume_id_probe_luks(id, off) == 0)
105                 goto exit;
106
107         /* signature in the first block, only small buffer needed */
108         if (volume_id_probe_vfat(id, off) == 0)
109                 goto exit;
110
111         if (volume_id_probe_xfs(id, off) == 0)
112                 goto exit;
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 exit;
119
120         if (volume_id_probe_ext(id, off) == 0)
121                 goto exit;
122
123         if (volume_id_probe_reiserfs(id, off) == 0)
124                 goto exit;
125
126         if (volume_id_probe_jfs(id, off) == 0)
127                 goto exit;
128
129         if (volume_id_probe_udf(id, off) == 0)
130                 goto exit;
131
132         if (volume_id_probe_iso9660(id, off) == 0)
133                 goto exit;
134
135         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
136                 goto exit;
137
138         if (volume_id_probe_ufs(id, off) == 0)
139                 goto exit;
140
141         if (volume_id_probe_ntfs(id, off)  == 0)
142                 goto exit;
143
144         if (volume_id_probe_cramfs(id, off) == 0)
145                 goto exit;
146
147         if (volume_id_probe_romfs(id, off) == 0)
148                 goto exit;
149
150         if (volume_id_probe_hpfs(id, off) == 0)
151                 goto exit;
152
153         if (volume_id_probe_sysv(id, off) == 0)
154                 goto exit;
155
156         if (volume_id_probe_minix(id, off) == 0)
157                 goto exit;
158
159         if (volume_id_probe_ocfs1(id, off) == 0)
160                 goto exit;
161
162         if (volume_id_probe_ocfs2(id, off) == 0)
163                 goto exit;
164
165         return -1;
166
167 exit:
168         /* If the filestystem in recognized, we free the allocated buffers,
169            otherwise they will stay in place for the possible next probe call */
170         volume_id_free_buffer(id);
171
172         return 0;
173 }
174
175 /* open volume by already open file descriptor */
176 struct volume_id *volume_id_open_fd(int fd)
177 {
178         struct volume_id *id;
179
180         id = malloc(sizeof(struct volume_id));
181         if (id == NULL)
182                 return NULL;
183         memset(id, 0x00, sizeof(struct volume_id));
184
185         id->fd = fd;
186
187         return id;
188 }
189
190 /* open volume by device node */
191 struct volume_id *volume_id_open_node(const char *path)
192 {
193         struct volume_id *id;
194         int fd;
195
196         fd = open(path, O_RDONLY);
197         if (fd < 0) {
198                 dbg("unable to open '%s'", path);
199                 return NULL;
200         }
201
202         id = volume_id_open_fd(fd);
203         if (id == NULL)
204                 return NULL;
205
206         /* close fd on device close */
207         id->fd_close = 1;
208
209         return id;
210 }
211
212 /* open volume by major/minor */
213 struct volume_id *volume_id_open_dev_t(dev_t devt)
214 {
215         struct volume_id *id;
216         char tmp_node[VOLUME_ID_PATH_MAX];
217
218         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
219                  "/dev/.volume_id-%u-%u-%u", getpid(), major(devt), minor(devt));
220         tmp_node[VOLUME_ID_PATH_MAX-1] = '\0';
221
222         /* create tempory node to open the block device */
223         unlink(tmp_node);
224         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
225                 return NULL;
226
227         id = volume_id_open_node(tmp_node);
228
229         unlink(tmp_node);
230
231         return id;
232 }
233
234 void volume_id_close(struct volume_id *id)
235 {
236         if (id == NULL)
237                 return;
238
239         if (id->fd_close != 0)
240                 close(id->fd);
241
242         volume_id_free_buffer(id);
243
244         if (id->partitions != NULL)
245                 free(id->partitions);
246
247         free(id);
248 }