chiark / gitweb /
forgot the ChangeLog for 074
[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 #include "vxfs.h"
63
64 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
65 {
66         if (id == NULL)
67                 return -EINVAL;
68
69         /* probe for raid first, cause fs probes may be successful on raid members */
70         if (size) {
71                 if (volume_id_probe_linux_raid(id, off, size) == 0)
72                         goto exit;
73
74                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
75                         goto exit;
76
77                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
78                         goto exit;
79
80                 if (volume_id_probe_via_raid(id, off, size) == 0)
81                         goto exit;
82
83                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
84                         goto exit;
85
86                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
87                         goto exit;
88
89                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
90                         goto exit;
91
92                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
93                         goto exit;
94         }
95
96         if (volume_id_probe_lvm1(id, off) == 0)
97                 goto exit;
98
99         if (volume_id_probe_lvm2(id, off) == 0)
100                 goto exit;
101
102         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
103                 goto exit;
104
105         if (volume_id_probe_luks(id, off) == 0)
106                 goto exit;
107
108         /* signature in the first block, only small buffer needed */
109         if (volume_id_probe_vfat(id, off) == 0)
110                 goto exit;
111
112         if (volume_id_probe_xfs(id, off) == 0)
113                 goto exit;
114
115         /* fill buffer with maximum */
116         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
117
118         if (volume_id_probe_linux_swap(id, off) == 0)
119                 goto exit;
120
121         if (volume_id_probe_ext(id, off) == 0)
122                 goto exit;
123
124         if (volume_id_probe_reiserfs(id, off) == 0)
125                 goto exit;
126
127         if (volume_id_probe_jfs(id, off) == 0)
128                 goto exit;
129
130         if (volume_id_probe_udf(id, off) == 0)
131                 goto exit;
132
133         if (volume_id_probe_iso9660(id, off) == 0)
134                 goto exit;
135
136         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
137                 goto exit;
138
139         if (volume_id_probe_ufs(id, off) == 0)
140                 goto exit;
141
142         if (volume_id_probe_ntfs(id, off)  == 0)
143                 goto exit;
144
145         if (volume_id_probe_cramfs(id, off) == 0)
146                 goto exit;
147
148         if (volume_id_probe_romfs(id, off) == 0)
149                 goto exit;
150
151         if (volume_id_probe_hpfs(id, off) == 0)
152                 goto exit;
153
154         if (volume_id_probe_sysv(id, off) == 0)
155                 goto exit;
156
157         if (volume_id_probe_minix(id, off) == 0)
158                 goto exit;
159
160         if (volume_id_probe_ocfs1(id, off) == 0)
161                 goto exit;
162
163         if (volume_id_probe_ocfs2(id, off) == 0)
164                 goto exit;
165
166         if (volume_id_probe_vxfs(id, off) == 0)
167                 goto exit;
168
169         return -1;
170
171 exit:
172         /* If the filestystem in recognized, we free the allocated buffers,
173            otherwise they will stay in place for the possible next probe call */
174         volume_id_free_buffer(id);
175
176         return 0;
177 }
178
179 /* open volume by already open file descriptor */
180 struct volume_id *volume_id_open_fd(int fd)
181 {
182         struct volume_id *id;
183
184         id = malloc(sizeof(struct volume_id));
185         if (id == NULL)
186                 return NULL;
187         memset(id, 0x00, sizeof(struct volume_id));
188
189         id->fd = fd;
190
191         return id;
192 }
193
194 /* open volume by device node */
195 struct volume_id *volume_id_open_node(const char *path)
196 {
197         struct volume_id *id;
198         int fd;
199
200         fd = open(path, O_RDONLY);
201         if (fd < 0) {
202                 dbg("unable to open '%s'", path);
203                 return NULL;
204         }
205
206         id = volume_id_open_fd(fd);
207         if (id == NULL)
208                 return NULL;
209
210         /* close fd on device close */
211         id->fd_close = 1;
212
213         return id;
214 }
215
216 /* open volume by major/minor */
217 struct volume_id *volume_id_open_dev_t(dev_t devt)
218 {
219         struct volume_id *id;
220         char tmp_node[VOLUME_ID_PATH_MAX];
221
222         snprintf(tmp_node, VOLUME_ID_PATH_MAX,
223                  "/dev/.volume_id-%u-%u-%u", getpid(), major(devt), minor(devt));
224         tmp_node[VOLUME_ID_PATH_MAX-1] = '\0';
225
226         /* create tempory node to open the block device */
227         unlink(tmp_node);
228         if (mknod(tmp_node, (S_IFBLK | 0600), devt) != 0)
229                 return NULL;
230
231         id = volume_id_open_node(tmp_node);
232
233         unlink(tmp_node);
234
235         return id;
236 }
237
238 void volume_id_close(struct volume_id *id)
239 {
240         if (id == NULL)
241                 return;
242
243         if (id->fd_close != 0)
244                 close(id->fd);
245
246         volume_id_free_buffer(id);
247
248         if (id->partitions != NULL)
249                 free(id->partitions);
250
251         free(id);
252 }