2 * devname.c - get a dev by its device inode name
4 * Copyright (C) Andries Brouwer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
26 #include <sys/types.h>
35 #include <sys/mkdev.h>
42 #include <libdevmapper.h>
46 * Find a dev struct in the cache by device name, if available.
48 * If there is no entry with the specified device name, and the create
49 * flag is set, then create an empty device entry.
51 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
53 blkid_dev dev = NULL, tmp;
54 struct list_head *p, *pnext;
56 if (!cache || !devname)
59 list_for_each(p, &cache->bic_devs) {
60 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
61 if (strcmp(tmp->bid_name, devname))
65 printf("found devname %s in cache\n", tmp->bid_name));
70 if (!dev && (flags & BLKID_DEV_CREATE)) {
71 dev = blkid_new_dev();
74 dev->bid_time = INT_MIN;
75 dev->bid_name = blkid_strdup(devname);
76 dev->bid_cache = cache;
77 list_add_tail(&dev->bid_devs, &cache->bic_devs);
78 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
81 if (flags & BLKID_DEV_VERIFY) {
82 dev = blkid_verify(cache, dev);
83 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
86 * If the device is verified, then search the blkid
87 * cache for any entries that match on the type, uuid,
88 * and label, and verify them; if a cache entry can
89 * not be verified, then it's stale and so we remove
92 list_for_each_safe(p, pnext, &cache->bic_devs) {
96 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
97 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
99 if (strcmp(dev->bid_type, dev2->bid_type))
101 if (dev->bid_label && dev2->bid_label &&
102 strcmp(dev->bid_label, dev2->bid_label))
104 if (dev->bid_uuid && dev2->bid_uuid &&
105 strcmp(dev->bid_uuid, dev2->bid_uuid))
107 if ((dev->bid_label && !dev2->bid_label) ||
108 (!dev->bid_label && dev2->bid_label) ||
109 (dev->bid_uuid && !dev2->bid_uuid) ||
110 (!dev->bid_uuid && dev2->bid_uuid))
112 dev2 = blkid_verify(cache, dev2);
113 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
114 blkid_free_dev(dev2);
120 #ifdef HAVE_DEVMAPPER
121 static int dm_device_is_leaf(const dev_t dev);
125 * Probe a single block device to add to the device cache.
127 static void probe_one(blkid_cache cache, const char *ptname,
128 dev_t devno, int pri, int only_if_new)
130 blkid_dev dev = NULL;
133 char *devname = NULL;
135 /* See if we already have this device number in the cache. */
136 list_for_each(p, &cache->bic_devs) {
137 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
139 #ifdef HAVE_DEVMAPPER
140 if (!dm_device_is_leaf(devno))
143 if (tmp->bid_devno == devno) {
146 dev = blkid_verify(cache, tmp);
150 if (dev && dev->bid_devno == devno)
154 * Take a quick look at /dev/ptname for the device number. We check
155 * all of the likely device directories. If we don't find it, or if
156 * the stat information doesn't check out, use blkid_devno_to_devname()
157 * to find it via an exhaustive search for the device major/minor.
159 for (dir = blkid_devdirs; *dir; dir++) {
163 sprintf(device, "%s/%s", *dir, ptname);
164 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
165 dev->bid_devno == devno)
168 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
169 st.st_rdev == devno) {
170 devname = blkid_strdup(device);
175 devname = blkid_devno_to_devname(devno);
179 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
183 if (!pri && !strncmp(ptname, "md", 2))
190 #ifdef HAVE_DEVMAPPER
191 static void dm_quiet_log(int level __BLKID_ATTR((unused)),
192 const char *file __BLKID_ATTR((unused)),
193 int line __BLKID_ATTR((unused)),
194 const char *f __BLKID_ATTR((unused)), ...)
200 * device-mapper support
202 static int dm_device_has_dep(const dev_t dev, const char *name)
204 struct dm_task *task;
205 struct dm_deps *deps;
210 task = dm_task_create(DM_DEVICE_DEPS);
214 if (!dm_task_set_name(task, name))
217 if (!dm_task_run(task))
220 if (!dm_task_get_info(task, &info))
226 deps = dm_task_get_deps(task);
227 if (!deps || deps->count == 0)
230 for (i = 0; i < deps->count; i++) {
231 dev_t dep_dev = deps->device[i];
233 if (dev == dep_dev) {
241 dm_task_destroy(task);
246 static int dm_device_is_leaf(const dev_t dev)
248 struct dm_task *task;
249 struct dm_names *names;
250 unsigned int next = 0;
253 dm_log_init(dm_quiet_log);
254 task = dm_task_create(DM_DEVICE_LIST);
260 if (!dm_task_run(task))
263 names = dm_task_get_names(task);
264 if (!names || !names->dev)
269 names = (struct dm_names *) ((char *)names + next);
271 if (dm_device_has_dep(dev, names->name))
279 dm_task_destroy(task);
284 static dev_t dm_get_devno(const char *name)
286 struct dm_task *task;
290 task = dm_task_create(DM_DEVICE_INFO);
294 if (!dm_task_set_name(task, name))
297 if (!dm_task_run(task))
300 if (!dm_task_get_info(task, &info))
306 ret = makedev(info.major, info.minor);
310 dm_task_destroy(task);
315 static void dm_probe_all(blkid_cache cache, int only_if_new)
317 struct dm_task *task;
318 struct dm_names *names;
319 unsigned int next = 0;
322 dm_log_init(dm_quiet_log);
323 task = dm_task_create(DM_DEVICE_LIST);
328 if (!dm_task_run(task))
331 names = dm_task_get_names(task);
332 if (!names || !names->dev)
341 names = (struct dm_names *) ((char *)names + next);
343 rc = asprintf(&device, "mapper/%s", names->name);
347 dev = dm_get_devno(names->name);
351 if (!dm_device_is_leaf(dev))
354 probe_one(cache, device, dev, BLKID_PRI_DM, only_if_new);
363 dm_task_destroy(task);
365 #endif /* HAVE_DEVMAPPER */
367 #define PROC_PARTITIONS "/proc/partitions"
368 #define VG_DIR "/proc/lvm/VGs"
371 * This function initializes the UUID cache with devices from the LVM
372 * proc hierarchy. We currently depend on the names of the LVM
373 * hierarchy giving us the device structure in /dev. (XXX is this a
378 static dev_t lvm_get_devno(const char *lvm_device)
385 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
386 if ((lvf = fopen(lvm_device, "r")) == NULL) {
387 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
392 while (fgets(buf, sizeof(buf), lvf)) {
393 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
394 ret = makedev(ma, mi);
403 static void lvm_probe_all(blkid_cache cache, int only_if_new)
406 struct dirent *vg_iter;
407 int vg_len = strlen(VG_DIR);
410 if ((vg_list = opendir(VG_DIR)) == NULL)
413 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
415 while ((vg_iter = readdir(vg_list)) != NULL) {
419 struct dirent *lv_iter;
421 vg_name = vg_iter->d_name;
422 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
424 vdirname = malloc(vg_len + strlen(vg_name) + 8);
427 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
429 lv_list = opendir(vdirname);
434 while ((lv_iter = readdir(lv_list)) != NULL) {
435 char *lv_name, *lvm_device;
437 lv_name = lv_iter->d_name;
438 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
441 lvm_device = malloc(vg_len + strlen(vg_name) +
442 strlen(lv_name) + 8);
447 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
449 dev = lvm_get_devno(lvm_device);
450 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
451 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
453 (unsigned int) dev));
454 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
465 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
468 evms_probe_all(blkid_cache cache, int only_if_new)
471 int ma, mi, sz, num = 0;
475 procpt = fopen(PROC_EVMS_VOLUMES, "r");
478 while (fgets(line, sizeof(line), procpt)) {
479 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
480 &ma, &mi, &sz, device) != 4)
483 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
486 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
495 * Read the device data for all available block devices in the system.
497 static int probe_all(blkid_cache cache, int only_if_new)
501 char ptname0[128], ptname1[128], *ptname = 0;
505 unsigned long long sz;
506 int lens[2] = { 0, 0 };
507 int which = 0, last = 0;
509 ptnames[0] = ptname0;
510 ptnames[1] = ptname1;
513 return -BLKID_ERR_PARAM;
515 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
516 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
519 blkid_read_cache(cache);
520 #ifdef HAVE_DEVMAPPER
521 dm_probe_all(cache, only_if_new);
523 evms_probe_all(cache, only_if_new);
525 lvm_probe_all(cache, only_if_new);
528 proc = fopen(PROC_PARTITIONS, "r");
530 return -BLKID_ERR_PROC;
532 while (fgets(line, sizeof(line), proc)) {
535 ptname = ptnames[which];
537 if (sscanf(line, " %d %d %llu %128[^\n ]",
538 &ma, &mi, &sz, ptname) != 4)
540 devs[which] = makedev(ma, mi);
542 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
544 /* Skip whole disk devs unless they have no partitions.
545 * If base name of device has changed, also
546 * check previous dev to see if it didn't have a partn.
547 * heuristic: partition name ends in a digit, & partition
548 * names contain whole device name as substring.
550 * Skip extended partitions.
551 * heuristic: size is 1
553 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
556 lens[which] = strlen(ptname);
558 /* ends in a digit, clearly a partition, so check */
559 if (isdigit(ptname[lens[which] - 1])) {
561 printf("partition dev %s, devno 0x%04X\n",
562 ptname, (unsigned int) devs[which]));
565 probe_one(cache, ptname, devs[which], 0,
567 lens[which] = 0; /* mark as checked */
571 * If last was not checked because it looked like a whole-disk
572 * dev, and the device's base name has changed,
573 * check last as well.
575 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
577 printf("whole dev %s, devno 0x%04X\n",
578 ptnames[last], (unsigned int) devs[last]));
579 probe_one(cache, ptnames[last], devs[last], 0,
585 /* Handle the last device if it wasn't partitioned */
587 probe_one(cache, ptname, devs[which], 0, only_if_new);
590 blkid_flush_cache(cache);
594 int blkid_probe_all(blkid_cache cache)
598 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
599 ret = probe_all(cache, 0);
600 cache->bic_time = time(0);
601 cache->bic_flags |= BLKID_BIC_FL_PROBED;
602 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
606 int blkid_probe_all_new(blkid_cache cache)
610 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
611 ret = probe_all(cache, 1);
612 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
618 int main(int argc, char **argv)
620 blkid_cache cache = NULL;
623 blkid_debug_mask = DEBUG_ALL;
625 fprintf(stderr, "Usage: %s\n"
626 "Probe all devices and exit\n", argv[0]);
629 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
630 fprintf(stderr, "%s: error creating cache (%d)\n",
634 if (blkid_probe_all(cache) < 0)
635 printf("%s: error probing devices\n", argv[0]);
637 blkid_put_cache(cache);