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.
25 #include <sys/types.h>
34 #include <sys/mkdev.h>
41 #include <libdevmapper.h>
45 * Find a dev struct in the cache by device name, if available.
47 * If there is no entry with the specified device name, and the create
48 * flag is set, then create an empty device entry.
50 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
52 blkid_dev dev = NULL, tmp;
55 if (!cache || !devname)
58 list_for_each(p, &cache->bic_devs) {
59 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
60 if (strcmp(tmp->bid_name, devname))
64 printf("found devname %s in cache\n", tmp->bid_name));
69 if (!dev && (flags & BLKID_DEV_CREATE)) {
70 dev = blkid_new_dev();
73 dev->bid_name = blkid_strdup(devname);
74 dev->bid_cache = cache;
75 list_add_tail(&dev->bid_devs, &cache->bic_devs);
76 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
79 if (flags & BLKID_DEV_VERIFY)
80 dev = blkid_verify(cache, dev);
85 static int dm_device_is_leaf(const dev_t dev);
89 * Probe a single block device to add to the device cache.
91 static void probe_one(blkid_cache cache, const char *ptname,
92 dev_t devno, int pri, int only_if_new)
99 /* See if we already have this device number in the cache. */
100 list_for_each(p, &cache->bic_devs) {
101 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
103 #ifdef HAVE_DEVMAPPER
104 if (!dm_device_is_leaf(devno))
107 if (tmp->bid_devno == devno) {
110 dev = blkid_verify(cache, tmp);
114 if (dev && dev->bid_devno == devno)
118 * Take a quick look at /dev/ptname for the device number. We check
119 * all of the likely device directories. If we don't find it, or if
120 * the stat information doesn't check out, use blkid_devno_to_devname()
121 * to find it via an exhaustive search for the device major/minor.
123 for (dir = blkid_devdirs; *dir; dir++) {
127 sprintf(device, "%s/%s", *dir, ptname);
128 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
129 dev->bid_devno == devno)
132 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
133 st.st_rdev == devno) {
134 devname = blkid_strdup(device);
139 devname = blkid_devno_to_devname(devno);
143 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
147 if (!pri && !strncmp(ptname, "md", 2))
154 #ifdef HAVE_DEVMAPPER
156 * device-mapper support
158 static int dm_device_has_dep(const dev_t dev, const char *name)
160 struct dm_task *task;
161 struct dm_deps *deps;
165 task = dm_task_create(DM_DEVICE_DEPS);
169 dm_task_set_name(task, name);
171 dm_task_get_info(task, &info);
174 dm_task_destroy(task);
178 deps = dm_task_get_deps(task);
179 if (!deps || deps->count == 0) {
180 dm_task_destroy(task);
184 for (i = 0; i < deps->count; i++) {
185 dev_t dep_dev = deps->device[i];
187 if (dev == dep_dev) {
188 dm_task_destroy(task);
193 dm_task_destroy(task);
197 static int dm_device_is_leaf(const dev_t dev)
199 struct dm_task *task;
200 struct dm_names *names;
201 unsigned int next = 0;
204 task = dm_task_create(DM_DEVICE_LIST);
209 names = dm_task_get_names(task);
210 if (!names || !names->dev) {
211 dm_task_destroy(task);
217 names = (void *)names + next;
219 if (dm_device_has_dep(dev, names->name))
225 dm_task_destroy(task);
230 static dev_t dm_get_devno(const char *name)
232 struct dm_task *task;
236 task = dm_task_create(DM_DEVICE_INFO);
240 dm_task_set_name(task, name);
242 dm_task_get_info(task, &info);
245 dm_task_destroy(task);
249 ret = makedev(info.major, info.minor);
251 dm_task_destroy(task);
256 static void dm_probe_all(blkid_cache cache, int only_if_new)
258 struct dm_task *task;
259 struct dm_names *names;
260 unsigned int next = 0;
263 task = dm_task_create(DM_DEVICE_LIST);
268 names = dm_task_get_names(task);
269 if (!names || !names->dev) {
270 dm_task_destroy(task);
280 names = (void *)names + next;
282 rc = asprintf(&device, "/dev/mapper/%s", names->name);
286 dev = dm_get_devno(names->name);
290 if (!dm_device_is_leaf(dev))
293 probe_one(cache, device, dev, BLKID_PRI_DM, only_if_new);
299 dm_task_destroy(task);
301 #endif /* HAVE_DEVMAPPER */
303 #define PROC_PARTITIONS "/proc/partitions"
304 #define VG_DIR "/proc/lvm/VGs"
307 * This function initializes the UUID cache with devices from the LVM
308 * proc hierarchy. We currently depend on the names of the LVM
309 * hierarchy giving us the device structure in /dev. (XXX is this a
314 static dev_t lvm_get_devno(const char *lvm_device)
321 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
322 if ((lvf = fopen(lvm_device, "r")) == NULL) {
323 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
328 while (fgets(buf, sizeof(buf), lvf)) {
329 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
330 ret = makedev(ma, mi);
339 static void lvm_probe_all(blkid_cache cache, int only_if_new)
342 struct dirent *vg_iter;
343 int vg_len = strlen(VG_DIR);
346 if ((vg_list = opendir(VG_DIR)) == NULL)
349 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
351 while ((vg_iter = readdir(vg_list)) != NULL) {
355 struct dirent *lv_iter;
357 vg_name = vg_iter->d_name;
358 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
360 vdirname = malloc(vg_len + strlen(vg_name) + 8);
363 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
365 lv_list = opendir(vdirname);
370 while ((lv_iter = readdir(lv_list)) != NULL) {
371 char *lv_name, *lvm_device;
373 lv_name = lv_iter->d_name;
374 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
377 lvm_device = malloc(vg_len + strlen(vg_name) +
378 strlen(lv_name) + 8);
383 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
385 dev = lvm_get_devno(lvm_device);
386 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
387 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
389 (unsigned int) dev));
390 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
401 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
404 evms_probe_all(blkid_cache cache, int only_if_new)
407 int ma, mi, sz, num = 0;
411 procpt = fopen(PROC_EVMS_VOLUMES, "r");
414 while (fgets(line, sizeof(line), procpt)) {
415 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
416 &ma, &mi, &sz, device) != 4)
419 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
422 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
431 * Read the device data for all available block devices in the system.
433 static int probe_all(blkid_cache cache, int only_if_new)
437 char ptname0[128], ptname1[128], *ptname = 0;
441 unsigned long long sz;
442 int lens[2] = { 0, 0 };
443 int which = 0, last = 0;
445 ptnames[0] = ptname0;
446 ptnames[1] = ptname1;
449 return -BLKID_ERR_PARAM;
451 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
452 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
455 blkid_read_cache(cache);
456 #ifdef HAVE_DEVMAPPER
457 dm_probe_all(cache, only_if_new);
459 evms_probe_all(cache, only_if_new);
461 lvm_probe_all(cache, only_if_new);
464 proc = fopen(PROC_PARTITIONS, "r");
466 return -BLKID_ERR_PROC;
468 while (fgets(line, sizeof(line), proc)) {
471 ptname = ptnames[which];
473 if (sscanf(line, " %d %d %llu %128[^\n ]",
474 &ma, &mi, &sz, ptname) != 4)
476 devs[which] = makedev(ma, mi);
478 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
480 /* Skip whole disk devs unless they have no partitions
481 * If we don't have a partition on this dev, also
482 * check previous dev to see if it didn't have a partn.
483 * heuristic: partition name ends in a digit.
485 * Skip extended partitions.
486 * heuristic: size is 1
488 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
491 lens[which] = strlen(ptname);
492 if (isdigit(ptname[lens[which] - 1])) {
494 printf("partition dev %s, devno 0x%04X\n",
495 ptname, (unsigned int) devs[which]));
498 probe_one(cache, ptname, devs[which], 0,
502 } else if (lens[last] && strncmp(ptnames[last], ptname,
505 printf("whole dev %s, devno 0x%04X\n",
506 ptnames[last], (unsigned int) devs[last]));
507 probe_one(cache, ptnames[last], devs[last], 0,
513 /* Handle the last device if it wasn't partitioned */
515 probe_one(cache, ptname, devs[which], 0, only_if_new);
518 blkid_flush_cache(cache);
522 int blkid_probe_all(blkid_cache cache)
526 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
527 ret = probe_all(cache, 0);
528 cache->bic_time = time(0);
529 cache->bic_flags |= BLKID_BIC_FL_PROBED;
530 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
534 int blkid_probe_all_new(blkid_cache cache)
538 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
539 ret = probe_all(cache, 1);
540 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
546 int main(int argc, char **argv)
548 blkid_cache cache = NULL;
551 blkid_debug_mask = DEBUG_ALL;
553 fprintf(stderr, "Usage: %s\n"
554 "Probe all devices and exit\n", argv[0]);
557 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
558 fprintf(stderr, "%s: error creating cache (%d)\n",
562 if (blkid_probe_all(cache) < 0)
563 printf("%s: error probing devices\n", argv[0]);
565 blkid_put_cache(cache);