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.
27 #include <sys/types.h>
37 #include <sys/mkdev.h>
39 #ifdef HAVE_SYS_SYSMACROS_H
40 #include <sys/sysmacros.h>
47 * Find a dev struct in the cache by device name, if available.
49 * If there is no entry with the specified device name, and the create
50 * flag is set, then create an empty device entry.
52 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
54 blkid_dev dev = NULL, tmp;
55 struct list_head *p, *pnext;
57 if (!cache || !devname)
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))
66 printf("found devname %s in cache\n", tmp->bid_name));
71 if (!dev && (flags & BLKID_DEV_CREATE)) {
72 if (access(devname, F_OK) < 0)
74 dev = blkid_new_dev();
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;
84 if (flags & BLKID_DEV_VERIFY) {
85 dev = blkid_verify(cache, dev);
86 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
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
95 list_for_each_safe(p, pnext, &cache->bic_devs) {
97 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
98 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
100 if (!dev->bid_type || !dev2->bid_type ||
101 strcmp(dev->bid_type, dev2->bid_type))
103 if (dev->bid_label && dev2->bid_label &&
104 strcmp(dev->bid_label, dev2->bid_label))
106 if (dev->bid_uuid && dev2->bid_uuid &&
107 strcmp(dev->bid_uuid, dev2->bid_uuid))
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))
114 dev2 = blkid_verify(cache, dev2);
115 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
116 blkid_free_dev(dev2);
122 /* Directories where we will try to search for device names */
123 static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
125 static int is_dm_leaf(const char *devname)
127 struct dirent *de, *d_de;
132 if ((dir = opendir("/sys/block")) == NULL)
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)
140 sprintf(path, "/sys/block/%s/slaves", de->d_name);
141 if ((d_dir = opendir(path)) == NULL)
143 while ((d_de = readdir(d_dir)) != NULL) {
144 if (!strcmp(d_de->d_name, devname)) {
158 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
159 * provides the real DM device names in /sys/block/<ptname>/dm/name
161 static char *get_dm_name(const char *ptname)
165 char path[300], name[256], *res = NULL;
167 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
168 if ((f = fopen(path, "r")) == NULL)
171 /* read "<name>\n" from sysfs */
172 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
174 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
175 res = blkid_strdup(path);
182 * Probe a single block device to add to the device cache.
184 static void probe_one(blkid_cache cache, const char *ptname,
185 dev_t devno, int pri, int only_if_new)
187 blkid_dev dev = NULL;
188 struct list_head *p, *pnext;
190 char *devname = NULL;
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,
196 if (tmp->bid_devno == devno) {
197 if (only_if_new && !access(tmp->bid_name, F_OK))
199 dev = blkid_verify(cache, tmp);
200 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
205 if (dev && dev->bid_devno == devno)
208 /* Try to translate private device-mapper dm-<N> names
209 * to standard /dev/mapper/<name>.
211 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
212 devname = get_dm_name(ptname);
214 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
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.
225 for (dir = dirlist; *dir; dir++) {
229 sprintf(device, "%s/%s", *dir, ptname);
230 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
231 dev->bid_devno == devno)
234 if (stat(device, &st) == 0 &&
235 blkidP_is_disk_device(st.st_mode) &&
236 st.st_rdev == devno) {
237 devname = blkid_strdup(device);
241 /* Do a short-cut scan of /dev/mapper first */
243 devname = get_dm_name(ptname);
245 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
247 devname = blkid_devno_to_devname(devno);
252 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
258 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
259 dev->bid_pri = BLKID_PRI_DM;
260 if (is_dm_leaf(ptname))
262 } else if (!strncmp(ptname, "md", 2))
263 dev->bid_pri = BLKID_PRI_MD;
268 #define PROC_PARTITIONS "/proc/partitions"
269 #define VG_DIR "/proc/lvm/VGs"
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
278 static dev_t lvm_get_devno(const char *lvm_device)
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,
292 while (fgets(buf, sizeof(buf), lvf)) {
293 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
294 ret = makedev(ma, mi);
303 static void lvm_probe_all(blkid_cache cache, int only_if_new)
306 struct dirent *vg_iter;
307 int vg_len = strlen(VG_DIR);
310 if ((vg_list = opendir(VG_DIR)) == NULL)
313 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
315 while ((vg_iter = readdir(vg_list)) != NULL) {
319 struct dirent *lv_iter;
321 vg_name = vg_iter->d_name;
322 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
324 vdirname = malloc(vg_len + strlen(vg_name) + 8);
327 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
329 lv_list = opendir(vdirname);
334 while ((lv_iter = readdir(lv_list)) != NULL) {
335 char *lv_name, *lvm_device;
337 lv_name = lv_iter->d_name;
338 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
341 lvm_device = malloc(vg_len + strlen(vg_name) +
342 strlen(lv_name) + 8);
347 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_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",
353 (unsigned int) dev));
354 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
365 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
368 evms_probe_all(blkid_cache cache, int only_if_new)
371 int ma, mi, sz, num = 0;
375 procpt = fopen(PROC_EVMS_VOLUMES, "r");
378 while (fgets(line, sizeof(line), procpt)) {
379 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
380 &ma, &mi, &sz, device) != 4)
383 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
386 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
395 * Read the device data for all available block devices in the system.
397 static int probe_all(blkid_cache cache, int only_if_new)
401 char ptname0[129], ptname1[129], *ptname = 0;
405 unsigned long long sz;
406 int lens[2] = { 0, 0 };
407 int which = 0, last = 0;
408 struct list_head *p, *pnext;
410 ptnames[0] = ptname0;
411 ptnames[1] = ptname1;
414 return -BLKID_ERR_PARAM;
416 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
417 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
420 blkid_read_cache(cache);
421 evms_probe_all(cache, only_if_new);
423 lvm_probe_all(cache, only_if_new);
426 proc = fopen(PROC_PARTITIONS, "r");
428 return -BLKID_ERR_PROC;
430 while (fgets(line, sizeof(line), proc)) {
433 ptname = ptnames[which];
435 if (sscanf(line, " %d %d %llu %128[^\n ]",
436 &ma, &mi, &sz, ptname) != 4)
438 devs[which] = makedev(ma, mi);
440 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
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.
448 * Skip extended partitions.
449 * heuristic: size is 1
451 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
454 lens[which] = strlen(ptname);
456 /* ends in a digit, clearly a partition, so check */
457 if (isdigit(ptname[lens[which] - 1])) {
459 printf("partition dev %s, devno 0x%04X\n",
460 ptname, (unsigned int) devs[which]));
463 probe_one(cache, ptname, devs[which], 0,
465 lens[which] = 0; /* mark as checked */
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
473 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
474 list_for_each_safe(p, pnext, &cache->bic_devs) {
477 /* find blkid dev for the whole-disk devno */
478 tmp = list_entry(p, struct blkid_struct_dev,
480 if (tmp->bid_devno == devs[last]) {
482 printf("freeing %s\n",
485 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
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.
496 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
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,
506 /* Handle the last device if it wasn't partitioned */
508 probe_one(cache, ptname, devs[which], 0, only_if_new);
511 blkid_flush_cache(cache);
515 int blkid_probe_all(blkid_cache cache)
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"));
527 int blkid_probe_all_new(blkid_cache cache)
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"));
539 int main(int argc, char **argv)
541 blkid_cache cache = NULL;
544 blkid_debug_mask = DEBUG_ALL;
546 fprintf(stderr, "Usage: %s\n"
547 "Probe all devices and exit\n", argv[0]);
550 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
551 fprintf(stderr, "%s: error creating cache (%d)\n",
555 if (blkid_probe_all(cache) < 0)
556 printf("%s: error probing devices\n", argv[0]);
558 blkid_put_cache(cache);