1 /*
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 #define _GNU_SOURCE 1
15
16 #include "config.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <limits.h>
20 #if HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #if HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #include <dirent.h>
30 #if HAVE_SYS_STAT_H
31 #include <sys/stat.h>
32 #endif
33 #if HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #if HAVE_SYS_MKDEV_H
37 #include <sys/mkdev.h>
38 #endif
39 #ifdef HAVE_SYS_SYSMACROS_H
40 #include <sys/sysmacros.h>
41 #endif
42 #include <time.h>
43
44 #include "blkidP.h"
45
46 /*
47 * Find a dev struct in the cache by device name, if available.
48 *
49 * If there is no entry with the specified device name, and the create
50 * flag is set, then create an empty device entry.
51 */
blkid_get_dev(blkid_cache cache,const char * devname,int flags)52 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
53 {
54 blkid_dev dev = NULL, tmp;
55 struct list_head *p, *pnext;
56
57 if (!cache || !devname)
58 return NULL;
59
60 list_for_each(p, &cache->bic_devs) {
61 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
62 if (strcmp(tmp->bid_name, devname))
63 continue;
64
65 DBG(DEBUG_DEVNAME,
66 printf("found devname %s in cache\n", tmp->bid_name));
67 dev = tmp;
68 break;
69 }
70
71 if (!dev && (flags & BLKID_DEV_CREATE)) {
72 if (access(devname, F_OK) < 0)
73 return NULL;
74 dev = blkid_new_dev();
75 if (!dev)
76 return NULL;
77 dev->bid_time = INT_MIN;
78 dev->bid_name = blkid_strdup(devname);
79 dev->bid_cache = cache;
80 list_add_tail(&dev->bid_devs, &cache->bic_devs);
81 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
82 }
83
84 if (flags & BLKID_DEV_VERIFY) {
85 dev = blkid_verify(cache, dev);
86 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
87 return dev;
88 /*
89 * If the device is verified, then search the blkid
90 * cache for any entries that match on the type, uuid,
91 * and label, and verify them; if a cache entry can
92 * not be verified, then it's stale and so we remove
93 * it.
94 */
95 list_for_each_safe(p, pnext, &cache->bic_devs) {
96 blkid_dev dev2;
97 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
98 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
99 continue;
100 if (!dev->bid_type || !dev2->bid_type ||
101 strcmp(dev->bid_type, dev2->bid_type))
102 continue;
103 if (dev->bid_label && dev2->bid_label &&
104 strcmp(dev->bid_label, dev2->bid_label))
105 continue;
106 if (dev->bid_uuid && dev2->bid_uuid &&
107 strcmp(dev->bid_uuid, dev2->bid_uuid))
108 continue;
109 if ((dev->bid_label && !dev2->bid_label) ||
110 (!dev->bid_label && dev2->bid_label) ||
111 (dev->bid_uuid && !dev2->bid_uuid) ||
112 (!dev->bid_uuid && dev2->bid_uuid))
113 continue;
114 dev2 = blkid_verify(cache, dev2);
115 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
116 blkid_free_dev(dev2);
117 }
118 }
119 return dev;
120 }
121
122 /* Directories where we will try to search for device names */
123 static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
124
is_dm_leaf(const char * devname)125 static int is_dm_leaf(const char *devname)
126 {
127 struct dirent *de, *d_de;
128 DIR *dir, *d_dir;
129 char path[300];
130 int ret = 1;
131
132 if ((dir = opendir("/sys/block")) == NULL)
133 return 0;
134 while ((de = readdir(dir)) != NULL) {
135 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
136 !strcmp(de->d_name, devname) ||
137 strncmp(de->d_name, "dm-", 3) ||
138 strlen(de->d_name) > sizeof(path)-32)
139 continue;
140 sprintf(path, "/sys/block/%s/slaves", de->d_name);
141 if ((d_dir = opendir(path)) == NULL)
142 continue;
143 while ((d_de = readdir(d_dir)) != NULL) {
144 if (!strcmp(d_de->d_name, devname)) {
145 ret = 0;
146 break;
147 }
148 }
149 closedir(d_dir);
150 if (!ret)
151 break;
152 }
153 closedir(dir);
154 return ret;
155 }
156
157 /*
158 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
159 * provides the real DM device names in /sys/block/<ptname>/dm/name
160 */
get_dm_name(const char * ptname)161 static char *get_dm_name(const char *ptname)
162 {
163 FILE *f;
164 size_t sz;
165 char path[300], name[256], *res = NULL;
166
167 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
168 if ((f = fopen(path, "r")) == NULL)
169 return NULL;
170
171 /* read "<name>\n" from sysfs */
172 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
173 name[sz - 1] = '\0';
174 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
175 res = blkid_strdup(path);
176 }
177 fclose(f);
178 return res;
179 }
180
181 /*
182 * Probe a single block device to add to the device cache.
183 */
probe_one(blkid_cache cache,const char * ptname,dev_t devno,int pri,int only_if_new)184 static void probe_one(blkid_cache cache, const char *ptname,
185 dev_t devno, int pri, int only_if_new)
186 {
187 blkid_dev dev = NULL;
188 struct list_head *p, *pnext;
189 const char **dir;
190 char *devname = NULL;
191
192 /* See if we already have this device number in the cache. */
193 list_for_each_safe(p, pnext, &cache->bic_devs) {
194 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
195 bid_devs);
196 if (tmp->bid_devno == devno) {
197 if (only_if_new && !access(tmp->bid_name, F_OK))
198 return;
199 dev = blkid_verify(cache, tmp);
200 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
201 break;
202 dev = 0;
203 }
204 }
205 if (dev && dev->bid_devno == devno)
206 goto set_pri;
207
208 /* Try to translate private device-mapper dm-<N> names
209 * to standard /dev/mapper/<name>.
210 */
211 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
212 devname = get_dm_name(ptname);
213 if (!devname)
214 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
215 if (devname)
216 goto get_dev;
217 }
218
219 /*
220 * Take a quick look at /dev/ptname for the device number. We check
221 * all of the likely device directories. If we don't find it, or if
222 * the stat information doesn't check out, use blkid_devno_to_devname()
223 * to find it via an exhaustive search for the device major/minor.
224 */
225 for (dir = dirlist; *dir; dir++) {
226 struct stat st;
227 char device[256];
228
229 sprintf(device, "%s/%s", *dir, ptname);
230 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
231 dev->bid_devno == devno)
232 goto set_pri;
233
234 if (stat(device, &st) == 0 &&
235 blkidP_is_disk_device(st.st_mode) &&
236 st.st_rdev == devno) {
237 devname = blkid_strdup(device);
238 goto get_dev;
239 }
240 }
241 /* Do a short-cut scan of /dev/mapper first */
242 if (!devname)
243 devname = get_dm_name(ptname);
244 if (!devname)
245 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
246 if (!devname) {
247 devname = blkid_devno_to_devname(devno);
248 if (!devname)
249 return;
250 }
251 get_dev:
252 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
253 free(devname);
254 set_pri:
255 if (dev) {
256 if (pri)
257 dev->bid_pri = pri;
258 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
259 dev->bid_pri = BLKID_PRI_DM;
260 if (is_dm_leaf(ptname))
261 dev->bid_pri += 5;
262 } else if (!strncmp(ptname, "md", 2))
263 dev->bid_pri = BLKID_PRI_MD;
264 }
265 return;
266 }
267
268 #define PROC_PARTITIONS "/proc/partitions"
269 #define VG_DIR "/proc/lvm/VGs"
270
271 /*
272 * This function initializes the UUID cache with devices from the LVM
273 * proc hierarchy. We currently depend on the names of the LVM
274 * hierarchy giving us the device structure in /dev. (XXX is this a
275 * safe thing to do?)
276 */
277 #ifdef VG_DIR
lvm_get_devno(const char * lvm_device)278 static dev_t lvm_get_devno(const char *lvm_device)
279 {
280 FILE *lvf;
281 char buf[1024];
282 int ma, mi;
283 dev_t ret = 0;
284
285 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
286 if ((lvf = fopen(lvm_device, "r")) == NULL) {
287 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
288 strerror(errno)));
289 return 0;
290 }
291
292 while (fgets(buf, sizeof(buf), lvf)) {
293 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
294 ret = makedev(ma, mi);
295 break;
296 }
297 }
298 fclose(lvf);
299
300 return ret;
301 }
302
lvm_probe_all(blkid_cache cache,int only_if_new)303 static void lvm_probe_all(blkid_cache cache, int only_if_new)
304 {
305 DIR *vg_list;
306 struct dirent *vg_iter;
307 int vg_len = strlen(VG_DIR);
308 dev_t dev;
309
310 if ((vg_list = opendir(VG_DIR)) == NULL)
311 return;
312
313 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
314
315 while ((vg_iter = readdir(vg_list)) != NULL) {
316 DIR *lv_list;
317 char *vdirname;
318 char *vg_name;
319 struct dirent *lv_iter;
320
321 vg_name = vg_iter->d_name;
322 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
323 continue;
324 vdirname = malloc(vg_len + strlen(vg_name) + 8);
325 if (!vdirname)
326 goto exit;
327 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
328
329 lv_list = opendir(vdirname);
330 free(vdirname);
331 if (lv_list == NULL)
332 continue;
333
334 while ((lv_iter = readdir(lv_list)) != NULL) {
335 char *lv_name, *lvm_device;
336
337 lv_name = lv_iter->d_name;
338 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
339 continue;
340
341 lvm_device = malloc(vg_len + strlen(vg_name) +
342 strlen(lv_name) + 8);
343 if (!lvm_device) {
344 closedir(lv_list);
345 goto exit;
346 }
347 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
348 lv_name);
349 dev = lvm_get_devno(lvm_device);
350 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
351 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
352 lvm_device,
353 (unsigned int) dev));
354 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
355 only_if_new);
356 free(lvm_device);
357 }
358 closedir(lv_list);
359 }
360 exit:
361 closedir(vg_list);
362 }
363 #endif
364
365 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
366
367 static int
evms_probe_all(blkid_cache cache,int only_if_new)368 evms_probe_all(blkid_cache cache, int only_if_new)
369 {
370 char line[100];
371 int ma, mi, sz, num = 0;
372 FILE *procpt;
373 char device[110];
374
375 procpt = fopen(PROC_EVMS_VOLUMES, "r");
376 if (!procpt)
377 return 0;
378 while (fgets(line, sizeof(line), procpt)) {
379 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
380 &ma, &mi, &sz, device) != 4)
381 continue;
382
383 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
384 device, ma, mi));
385
386 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
387 only_if_new);
388 num++;
389 }
390 fclose(procpt);
391 return num;
392 }
393
394 /*
395 * Read the device data for all available block devices in the system.
396 */
probe_all(blkid_cache cache,int only_if_new)397 static int probe_all(blkid_cache cache, int only_if_new)
398 {
399 FILE *proc;
400 char line[1024];
401 char ptname0[129], ptname1[129], *ptname = 0;
402 char *ptnames[2];
403 dev_t devs[2];
404 int ma, mi;
405 unsigned long long sz;
406 int lens[2] = { 0, 0 };
407 int which = 0, last = 0;
408 struct list_head *p, *pnext;
409
410 ptnames[0] = ptname0;
411 ptnames[1] = ptname1;
412
413 if (!cache)
414 return -BLKID_ERR_PARAM;
415
416 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
417 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
418 return 0;
419
420 blkid_read_cache(cache);
421 evms_probe_all(cache, only_if_new);
422 #ifdef VG_DIR
423 lvm_probe_all(cache, only_if_new);
424 #endif
425
426 proc = fopen(PROC_PARTITIONS, "r");
427 if (!proc)
428 return -BLKID_ERR_PROC;
429
430 while (fgets(line, sizeof(line), proc)) {
431 last = which;
432 which ^= 1;
433 ptname = ptnames[which];
434
435 if (sscanf(line, " %d %d %llu %128[^\n ]",
436 &ma, &mi, &sz, ptname) != 4)
437 continue;
438 devs[which] = makedev(ma, mi);
439
440 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
441
442 /* Skip whole disk devs unless they have no partitions.
443 * If base name of device has changed, also
444 * check previous dev to see if it didn't have a partn.
445 * heuristic: partition name ends in a digit, & partition
446 * names contain whole device name as substring.
447 *
448 * Skip extended partitions.
449 * heuristic: size is 1
450 *
451 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
452 */
453
454 lens[which] = strlen(ptname);
455
456 /* ends in a digit, clearly a partition, so check */
457 if (isdigit(ptname[lens[which] - 1])) {
458 DBG(DEBUG_DEVNAME,
459 printf("partition dev %s, devno 0x%04X\n",
460 ptname, (unsigned int) devs[which]));
461
462 if (sz > 1)
463 probe_one(cache, ptname, devs[which], 0,
464 only_if_new);
465 lens[which] = 0; /* mark as checked */
466 }
467
468 /*
469 * If last was a whole disk and we just found a partition
470 * on it, remove the whole-disk dev from the cache if
471 * it exists.
472 */
473 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
474 list_for_each_safe(p, pnext, &cache->bic_devs) {
475 blkid_dev tmp;
476
477 /* find blkid dev for the whole-disk devno */
478 tmp = list_entry(p, struct blkid_struct_dev,
479 bid_devs);
480 if (tmp->bid_devno == devs[last]) {
481 DBG(DEBUG_DEVNAME,
482 printf("freeing %s\n",
483 tmp->bid_name));
484 blkid_free_dev(tmp);
485 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
486 break;
487 }
488 }
489 lens[last] = 0;
490 }
491 /*
492 * If last was not checked because it looked like a whole-disk
493 * dev, and the device's base name has changed,
494 * check last as well.
495 */
496 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
497 DBG(DEBUG_DEVNAME,
498 printf("whole dev %s, devno 0x%04X\n",
499 ptnames[last], (unsigned int) devs[last]));
500 probe_one(cache, ptnames[last], devs[last], 0,
501 only_if_new);
502 lens[last] = 0;
503 }
504 }
505
506 /* Handle the last device if it wasn't partitioned */
507 if (lens[which])
508 probe_one(cache, ptname, devs[which], 0, only_if_new);
509
510 fclose(proc);
511 blkid_flush_cache(cache);
512 return 0;
513 }
514
blkid_probe_all(blkid_cache cache)515 int blkid_probe_all(blkid_cache cache)
516 {
517 int ret;
518
519 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
520 ret = probe_all(cache, 0);
521 cache->bic_time = time(0);
522 cache->bic_flags |= BLKID_BIC_FL_PROBED;
523 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
524 return ret;
525 }
526
blkid_probe_all_new(blkid_cache cache)527 int blkid_probe_all_new(blkid_cache cache)
528 {
529 int ret;
530
531 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
532 ret = probe_all(cache, 1);
533 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
534 return ret;
535 }
536
537
538 #ifdef TEST_PROGRAM
main(int argc,char ** argv)539 int main(int argc, char **argv)
540 {
541 blkid_cache cache = NULL;
542 int ret;
543
544 blkid_debug_mask = DEBUG_ALL;
545 if (argc != 1) {
546 fprintf(stderr, "Usage: %s\n"
547 "Probe all devices and exit\n", argv[0]);
548 exit(1);
549 }
550 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
551 fprintf(stderr, "%s: error creating cache (%d)\n",
552 argv[0], ret);
553 exit(1);
554 }
555 if (blkid_probe_all(cache) < 0)
556 printf("%s: error probing devices\n", argv[0]);
557
558 blkid_put_cache(cache);
559 return (0);
560 }
561 #endif
562