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>
36 #include <sys/mkdev.h>
43 * Find a dev struct in the cache by device name, if available.
45 * If there is no entry with the specified device name, and the create
46 * flag is set, then create an empty device entry.
48 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
50 blkid_dev dev = NULL, tmp;
51 struct list_head *p, *pnext;
53 if (!cache || !devname)
56 list_for_each(p, &cache->bic_devs) {
57 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
58 if (strcmp(tmp->bid_name, devname))
62 printf("found devname %s in cache\n", tmp->bid_name));
67 if (!dev && (flags & BLKID_DEV_CREATE)) {
68 if (access(devname, F_OK) < 0)
70 dev = blkid_new_dev();
73 dev->bid_time = INT_MIN;
74 dev->bid_name = blkid_strdup(devname);
75 dev->bid_cache = cache;
76 list_add_tail(&dev->bid_devs, &cache->bic_devs);
77 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
80 if (flags & BLKID_DEV_VERIFY) {
81 dev = blkid_verify(cache, dev);
82 if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
85 * If the device is verified, then search the blkid
86 * cache for any entries that match on the type, uuid,
87 * and label, and verify them; if a cache entry can
88 * not be verified, then it's stale and so we remove
91 list_for_each_safe(p, pnext, &cache->bic_devs) {
95 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
96 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
98 if (!dev->bid_type || !dev2->bid_type ||
99 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 /* Directories where we will try to search for device names */
121 static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
123 static int is_dm_leaf(const char *devname)
125 struct dirent *de, *d_de;
130 if ((dir = opendir("/sys/block")) == NULL)
132 while ((de = readdir(dir)) != NULL) {
133 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
134 !strcmp(de->d_name, devname) ||
135 strncmp(de->d_name, "dm-", 3) ||
136 strlen(de->d_name) > sizeof(path)-32)
138 sprintf(path, "/sys/block/%s/slaves", de->d_name);
139 if ((d_dir = opendir(path)) == NULL)
141 while ((d_de = readdir(d_dir)) != NULL) {
142 if (!strcmp(d_de->d_name, devname)) {
156 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
157 * provides the real DM device names in /sys/block/<ptname>/dm/name
159 static char *get_dm_name(const char *ptname)
163 char path[256], name[256], *res = NULL;
165 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
166 if ((f = fopen(path, "r")) == NULL)
169 /* read "<name>\n" from sysfs */
170 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
172 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
173 res = blkid_strdup(path);
180 * Probe a single block device to add to the device cache.
182 static void probe_one(blkid_cache cache, const char *ptname,
183 dev_t devno, int pri, int only_if_new)
185 blkid_dev dev = NULL;
186 struct list_head *p, *pnext;
188 char *devname = NULL;
190 /* See if we already have this device number in the cache. */
191 list_for_each_safe(p, pnext, &cache->bic_devs) {
192 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
194 if (tmp->bid_devno == devno) {
195 if (only_if_new && !access(tmp->bid_name, F_OK))
197 dev = blkid_verify(cache, tmp);
198 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
203 if (dev && dev->bid_devno == devno)
206 /* Try to translate private device-mapper dm-<N> names
207 * to standard /dev/mapper/<name>.
209 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
210 devname = get_dm_name(ptname);
212 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
218 * Take a quick look at /dev/ptname for the device number. We check
219 * all of the likely device directories. If we don't find it, or if
220 * the stat information doesn't check out, use blkid_devno_to_devname()
221 * to find it via an exhaustive search for the device major/minor.
223 for (dir = dirlist; *dir; dir++) {
227 sprintf(device, "%s/%s", *dir, ptname);
228 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
229 dev->bid_devno == devno)
232 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
233 st.st_rdev == devno) {
234 devname = blkid_strdup(device);
238 /* Do a short-cut scan of /dev/mapper first */
240 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
242 devname = blkid_devno_to_devname(devno);
248 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
255 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
256 dev->bid_pri = BLKID_PRI_DM;
257 if (is_dm_leaf(ptname))
259 } else if (!strncmp(ptname, "md", 2))
260 dev->bid_pri = BLKID_PRI_MD;
265 #define PROC_PARTITIONS "/proc/partitions"
266 #define VG_DIR "/proc/lvm/VGs"
269 * This function initializes the UUID cache with devices from the LVM
270 * proc hierarchy. We currently depend on the names of the LVM
271 * hierarchy giving us the device structure in /dev. (XXX is this a
275 static dev_t lvm_get_devno(const char *lvm_device)
282 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
283 if ((lvf = fopen(lvm_device, "r")) == NULL) {
284 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
289 while (fgets(buf, sizeof(buf), lvf)) {
290 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
291 ret = makedev(ma, mi);
300 static void lvm_probe_all(blkid_cache cache, int only_if_new)
303 struct dirent *vg_iter;
304 int vg_len = strlen(VG_DIR);
307 if ((vg_list = opendir(VG_DIR)) == NULL)
310 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
312 while ((vg_iter = readdir(vg_list)) != NULL) {
316 struct dirent *lv_iter;
318 vg_name = vg_iter->d_name;
319 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
321 vdirname = malloc(vg_len + strlen(vg_name) + 8);
324 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
326 lv_list = opendir(vdirname);
331 while ((lv_iter = readdir(lv_list)) != NULL) {
332 char *lv_name, *lvm_device;
334 lv_name = lv_iter->d_name;
335 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
338 lvm_device = malloc(vg_len + strlen(vg_name) +
339 strlen(lv_name) + 8);
344 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
346 dev = lvm_get_devno(lvm_device);
347 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
348 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
350 (unsigned int) dev));
351 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
362 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
365 evms_probe_all(blkid_cache cache, int only_if_new)
368 int ma, mi, sz, num = 0;
372 procpt = fopen(PROC_EVMS_VOLUMES, "r");
375 while (fgets(line, sizeof(line), procpt)) {
376 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
377 &ma, &mi, &sz, device) != 4)
380 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
383 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
392 * Read the device data for all available block devices in the system.
394 static int probe_all(blkid_cache cache, int only_if_new)
398 char ptname0[128], ptname1[128], *ptname = 0;
402 unsigned long long sz;
403 int lens[2] = { 0, 0 };
404 int which = 0, last = 0;
405 struct list_head *p, *pnext;
407 ptnames[0] = ptname0;
408 ptnames[1] = ptname1;
411 return -BLKID_ERR_PARAM;
413 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
414 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
417 blkid_read_cache(cache);
418 evms_probe_all(cache, only_if_new);
420 lvm_probe_all(cache, only_if_new);
423 proc = fopen(PROC_PARTITIONS, "r");
425 return -BLKID_ERR_PROC;
427 while (fgets(line, sizeof(line), proc)) {
430 ptname = ptnames[which];
432 if (sscanf(line, " %d %d %llu %128[^\n ]",
433 &ma, &mi, &sz, ptname) != 4)
435 devs[which] = makedev(ma, mi);
437 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
439 /* Skip whole disk devs unless they have no partitions.
440 * If base name of device has changed, also
441 * check previous dev to see if it didn't have a partn.
442 * heuristic: partition name ends in a digit, & partition
443 * names contain whole device name as substring.
445 * Skip extended partitions.
446 * heuristic: size is 1
448 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
451 lens[which] = strlen(ptname);
453 /* ends in a digit, clearly a partition, so check */
454 if (isdigit(ptname[lens[which] - 1])) {
456 printf("partition dev %s, devno 0x%04X\n",
457 ptname, (unsigned int) devs[which]));
460 probe_one(cache, ptname, devs[which], 0,
462 lens[which] = 0; /* mark as checked */
466 * If last was a whole disk and we just found a partition
467 * on it, remove the whole-disk dev from the cache if
470 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
471 list_for_each_safe(p, pnext, &cache->bic_devs) {
474 /* find blkid dev for the whole-disk devno */
475 tmp = list_entry(p, struct blkid_struct_dev,
477 if (tmp->bid_devno == devs[last]) {
479 printf("freeing %s\n",
482 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
489 * If last was not checked because it looked like a whole-disk
490 * dev, and the device's base name has changed,
491 * check last as well.
493 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
495 printf("whole dev %s, devno 0x%04X\n",
496 ptnames[last], (unsigned int) devs[last]));
497 probe_one(cache, ptnames[last], devs[last], 0,
503 /* Handle the last device if it wasn't partitioned */
505 probe_one(cache, ptname, devs[which], 0, only_if_new);
508 blkid_flush_cache(cache);
512 int blkid_probe_all(blkid_cache cache)
516 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
517 ret = probe_all(cache, 0);
518 cache->bic_time = time(0);
519 cache->bic_flags |= BLKID_BIC_FL_PROBED;
520 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
524 int blkid_probe_all_new(blkid_cache cache)
528 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
529 ret = probe_all(cache, 1);
530 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
536 int main(int argc, char **argv)
538 blkid_cache cache = NULL;
541 blkid_init_debug(DEBUG_ALL);
543 fprintf(stderr, "Usage: %s\n"
544 "Probe all devices and exit\n", argv[0]);
547 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
548 fprintf(stderr, "%s: error creating cache (%d)\n",
552 if (blkid_probe_all(cache) < 0)
553 printf("%s: error probing devices\n", argv[0]);
555 blkid_put_cache(cache);