blob: 87d5cbef393ab5768bf6ce57923e4b235f4ac5fb [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
2 * devname.c - get a dev by its device inode name
3 *
4 * Copyright (C) Andries Brouwer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
14#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22#if HAVE_SYS_TYPES_H
23#include <sys/types.h>
24#endif
25#if HAVE_SYS_STAT_H
26#include <sys/stat.h>
27#endif
28#if HAVE_ERRNO_H
29#include <errno.h>
30#endif
31#if HAVE_SYS_MKDEV_H
32#include <sys/mkdev.h>
33#endif
34#include <time.h>
35
36#include "blkidP.h"
37
38/*
39 * Find a dev struct in the cache by device name, if available.
40 *
41 * If there is no entry with the specified device name, and the create
42 * flag is set, then create an empty device entry.
43 */
44blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
45{
46 blkid_dev dev = NULL, tmp;
47 struct list_head *p;
48
49 if (!cache || !devname)
50 return NULL;
51
52 list_for_each(p, &cache->bic_devs) {
53 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
54 if (strcmp(tmp->bid_name, devname))
55 continue;
56
57 DBG(DEBUG_DEVNAME,
58 printf("found devname %s in cache\n", tmp->bid_name));
59 dev = tmp;
60 break;
61 }
62
63 if (!dev && (flags & BLKID_DEV_CREATE)) {
64 dev = blkid_new_dev();
65 if (!dev)
66 return NULL;
67 dev->bid_name = blkid_strdup(devname);
68 dev->bid_cache = cache;
69 list_add_tail(&dev->bid_devs, &cache->bic_devs);
70 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
71 }
72
73 if (flags & BLKID_DEV_VERIFY)
74 dev = blkid_verify(cache, dev);
75 return dev;
76}
77
78/*
79 * Probe a single block device to add to the device cache.
80 */
81static void probe_one(blkid_cache cache, const char *ptname,
82 dev_t devno, int pri)
83{
84 blkid_dev dev = NULL;
85 struct list_head *p;
86 const char **dir;
87 char *devname = NULL;
88
89 /* See if we already have this device number in the cache. */
90 list_for_each(p, &cache->bic_devs) {
91 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
92 bid_devs);
93 if (tmp->bid_devno == devno) {
94 dev = blkid_verify(cache, tmp);
95 break;
96 }
97 }
98 if (dev && dev->bid_devno == devno)
99 goto set_pri;
100
101 /*
102 * Take a quick look at /dev/ptname for the device number. We check
103 * all of the likely device directories. If we don't find it, or if
104 * the stat information doesn't check out, use blkid_devno_to_devname()
105 * to find it via an exhaustive search for the device major/minor.
106 */
107 for (dir = blkid_devdirs; *dir; dir++) {
108 struct stat st;
109 char device[256];
110
111 sprintf(device, "%s/%s", *dir, ptname);
112 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
113 dev->bid_devno == devno)
114 goto set_pri;
115
116 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
117 st.st_rdev == devno) {
118 devname = blkid_strdup(device);
119 break;
120 }
121 }
122 if (!devname) {
123 devname = blkid_devno_to_devname(devno);
124 if (!devname)
125 return;
126 }
127 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
128 free(devname);
129
130set_pri:
131 if (!pri && !strncmp(ptname, "md", 2))
132 pri = BLKID_PRI_MD;
133 if (dev)
134 dev->bid_pri = pri;
135 return;
136}
137
138#define PROC_PARTITIONS "/proc/partitions"
139#define VG_DIR "/proc/lvm/VGs"
140
141/*
142 * This function initializes the UUID cache with devices from the LVM
143 * proc hierarchy. We currently depend on the names of the LVM
144 * hierarchy giving us the device structure in /dev. (XXX is this a
145 * safe thing to do?)
146 */
147#ifdef VG_DIR
148#include <dirent.h>
149static dev_t lvm_get_devno(const char *lvm_device)
150{
151 FILE *lvf;
152 char buf[1024];
153 int ma, mi;
154 dev_t ret = 0;
155
156 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
157 if ((lvf = fopen(lvm_device, "r")) == NULL) {
158 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
159 strerror(errno)));
160 return 0;
161 }
162
163 while (fgets(buf, sizeof(buf), lvf)) {
164 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
165 ret = makedev(ma, mi);
166 break;
167 }
168 }
169 fclose(lvf);
170
171 return ret;
172}
173
174static void lvm_probe_all(blkid_cache cache)
175{
176 DIR *vg_list;
177 struct dirent *vg_iter;
178 int vg_len = strlen(VG_DIR);
179 dev_t dev;
180
181 if ((vg_list = opendir(VG_DIR)) == NULL)
182 return;
183
184 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
185
186 while ((vg_iter = readdir(vg_list)) != NULL) {
187 DIR *lv_list;
188 char *vdirname;
189 char *vg_name;
190 struct dirent *lv_iter;
191
192 vg_name = vg_iter->d_name;
193 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
194 continue;
195 vdirname = malloc(vg_len + strlen(vg_name) + 8);
196 if (!vdirname)
197 goto exit;
198 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
199
200 lv_list = opendir(vdirname);
201 free(vdirname);
202 if (lv_list == NULL)
203 continue;
204
205 while ((lv_iter = readdir(lv_list)) != NULL) {
206 char *lv_name, *lvm_device;
207
208 lv_name = lv_iter->d_name;
209 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
210 continue;
211
212 lvm_device = malloc(vg_len + strlen(vg_name) +
213 strlen(lv_name) + 8);
214 if (!lvm_device) {
215 closedir(lv_list);
216 goto exit;
217 }
218 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
219 lv_name);
220 dev = lvm_get_devno(lvm_device);
221 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
222 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
223 lvm_device,
224 (unsigned int) dev));
225 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM);
226 free(lvm_device);
227 }
228 closedir(lv_list);
229 }
230exit:
231 closedir(vg_list);
232}
233#endif
234
235#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
236
237static int
238evms_probe_all(blkid_cache cache)
239{
240 char line[100];
241 int ma, mi, sz, num = 0;
242 FILE *procpt;
243 char device[110];
244
245 procpt = fopen(PROC_EVMS_VOLUMES, "r");
246 if (!procpt)
247 return 0;
248 while (fgets(line, sizeof(line), procpt)) {
249 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
250 &ma, &mi, &sz, device) != 4)
251 continue;
252
253 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
254 device, ma, mi));
255
256 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS);
257 num++;
258 }
259 fclose(procpt);
260 return num;
261}
262
263/*
264 * Read the device data for all available block devices in the system.
265 */
266int blkid_probe_all(blkid_cache cache)
267{
268 FILE *proc;
269 char line[1024];
270 char ptname0[128], ptname1[128], *ptname = 0;
271 char *ptnames[2];
272 dev_t devs[2];
273 int ma, mi;
274 unsigned long long sz;
275 int lens[2] = { 0, 0 };
276 int which = 0, last = 0;
277
278 ptnames[0] = ptname0;
279 ptnames[1] = ptname1;
280
281 if (!cache)
282 return -BLKID_ERR_PARAM;
283
284 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
285 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
286 return 0;
287
288 blkid_read_cache(cache);
289 evms_probe_all(cache);
290#ifdef VG_DIR
291 lvm_probe_all(cache);
292#endif
293
294 proc = fopen(PROC_PARTITIONS, "r");
295 if (!proc)
296 return -BLKID_ERR_PROC;
297
298 while (fgets(line, sizeof(line), proc)) {
299 last = which;
300 which ^= 1;
301 ptname = ptnames[which];
302
303 if (sscanf(line, " %d %d %llu %128[^\n ]",
304 &ma, &mi, &sz, ptname) != 4)
305 continue;
306 devs[which] = makedev(ma, mi);
307
308 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
309
310 /* Skip whole disk devs unless they have no partitions
311 * If we don't have a partition on this dev, also
312 * check previous dev to see if it didn't have a partn.
313 * heuristic: partition name ends in a digit.
314 *
315 * Skip extended partitions.
316 * heuristic: size is 1
317 *
318 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
319 */
320
321 lens[which] = strlen(ptname);
322 if (isdigit(ptname[lens[which] - 1])) {
323 DBG(DEBUG_DEVNAME,
324 printf("partition dev %s, devno 0x%04X\n",
325 ptname, (unsigned int) devs[which]));
326
327 if (sz > 1)
328 probe_one(cache, ptname, devs[which], 0);
329 lens[which] = 0;
330 lens[last] = 0;
331 } else if (lens[last] && strncmp(ptnames[last], ptname,
332 lens[last])) {
333 DBG(DEBUG_DEVNAME,
334 printf("whole dev %s, devno 0x%04X\n",
335 ptnames[last], (unsigned int) devs[last]));
336 probe_one(cache, ptnames[last], devs[last], 0);
337 lens[last] = 0;
338 }
339 }
340
341 /* Handle the last device if it wasn't partitioned */
342 if (lens[which])
343 probe_one(cache, ptname, devs[which], 0);
344
345 fclose(proc);
346
347 cache->bic_time = time(0);
348 cache->bic_flags |= BLKID_BIC_FL_PROBED;
349 blkid_flush_cache(cache);
350 return 0;
351}
352
353#ifdef TEST_PROGRAM
354int main(int argc, char **argv)
355{
356 blkid_cache cache = NULL;
357 int ret;
358
359 blkid_debug_mask = DEBUG_ALL;
360 if (argc != 1) {
361 fprintf(stderr, "Usage: %s\n"
362 "Probe all devices and exit\n", argv[0]);
363 exit(1);
364 }
365 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
366 fprintf(stderr, "%s: error creating cache (%d)\n",
367 argv[0], ret);
368 exit(1);
369 }
370 if (blkid_probe_all(cache) < 0)
371 printf("%s: error probing devices\n", argv[0]);
372
373 blkid_put_cache(cache);
374 return (0);
375}
376#endif