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>
44 * Find a dev struct in the cache by device name, if available.
46 * If there is no entry with the specified device name, and the create
47 * flag is set, then create an empty device entry.
49 blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
51 blkid_dev dev = NULL, tmp;
52 struct list_head *p, *pnext;
54 if (!cache || !devname)
57 list_for_each(p, &cache->bic_devs) {
58 tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
59 if (strcmp(tmp->bid_name, devname))
63 printf("found devname %s in cache\n", tmp->bid_name));
68 if (!dev && (flags & BLKID_DEV_CREATE)) {
69 if (access(devname, F_OK) < 0)
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 (!dev->bid_type || !dev2->bid_type ||
100 strcmp(dev->bid_type, dev2->bid_type))
102 if (dev->bid_label && dev2->bid_label &&
103 strcmp(dev->bid_label, dev2->bid_label))
105 if (dev->bid_uuid && dev2->bid_uuid &&
106 strcmp(dev->bid_uuid, dev2->bid_uuid))
108 if ((dev->bid_label && !dev2->bid_label) ||
109 (!dev->bid_label && dev2->bid_label) ||
110 (dev->bid_uuid && !dev2->bid_uuid) ||
111 (!dev->bid_uuid && dev2->bid_uuid))
113 dev2 = blkid_verify(cache, dev2);
114 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
115 blkid_free_dev(dev2);
121 /* Directories where we will try to search for device names */
122 static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
124 static int is_dm_leaf(const char *devname)
126 struct dirent *de, *d_de;
131 if ((dir = opendir("/sys/block")) == NULL)
133 while ((de = readdir(dir)) != NULL) {
134 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
135 !strcmp(de->d_name, devname) ||
136 strncmp(de->d_name, "dm-", 3) ||
137 strlen(de->d_name) > sizeof(path)-32)
139 sprintf(path, "/sys/block/%s/slaves", de->d_name);
140 if ((d_dir = opendir(path)) == NULL)
142 while ((d_de = readdir(d_dir)) != NULL) {
143 if (!strcmp(d_de->d_name, devname)) {
157 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
158 * provides the real DM device names in /sys/block/<ptname>/dm/name
160 static char *get_dm_name(const char *ptname)
164 char path[256], name[256], *res = NULL;
166 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
167 if ((f = fopen(path, "r")) == NULL)
170 /* read "<name>\n" from sysfs */
171 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
173 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
174 res = blkid_strdup(path);
181 * Probe a single block device to add to the device cache.
183 static void probe_one(blkid_cache cache, const char *ptname,
184 dev_t devno, int pri, int only_if_new)
186 blkid_dev dev = NULL;
187 struct list_head *p, *pnext;
189 char *devname = NULL;
191 /* See if we already have this device number in the cache. */
192 list_for_each_safe(p, pnext, &cache->bic_devs) {
193 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
195 if (tmp->bid_devno == devno) {
196 if (only_if_new && !access(tmp->bid_name, F_OK))
198 dev = blkid_verify(cache, tmp);
199 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
204 if (dev && dev->bid_devno == devno)
207 /* Try to translate private device-mapper dm-<N> names
208 * to standard /dev/mapper/<name>.
210 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
211 devname = get_dm_name(ptname);
213 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
219 * Take a quick look at /dev/ptname for the device number. We check
220 * all of the likely device directories. If we don't find it, or if
221 * the stat information doesn't check out, use blkid_devno_to_devname()
222 * to find it via an exhaustive search for the device major/minor.
224 for (dir = dirlist; *dir; dir++) {
228 sprintf(device, "%s/%s", *dir, ptname);
229 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
230 dev->bid_devno == devno)
233 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
234 st.st_rdev == devno) {
235 devname = blkid_strdup(device);
239 /* Do a short-cut scan of /dev/mapper first */
241 devname = get_dm_name(ptname);
243 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
245 devname = blkid_devno_to_devname(devno);
250 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
256 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
257 dev->bid_pri = BLKID_PRI_DM;
258 if (is_dm_leaf(ptname))
260 } else if (!strncmp(ptname, "md", 2))
261 dev->bid_pri = BLKID_PRI_MD;
266 #define PROC_PARTITIONS "/proc/partitions"
267 #define VG_DIR "/proc/lvm/VGs"
270 * This function initializes the UUID cache with devices from the LVM
271 * proc hierarchy. We currently depend on the names of the LVM
272 * hierarchy giving us the device structure in /dev. (XXX is this a
276 static dev_t lvm_get_devno(const char *lvm_device)
283 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
284 if ((lvf = fopen(lvm_device, "r")) == NULL) {
285 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
290 while (fgets(buf, sizeof(buf), lvf)) {
291 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
292 ret = makedev(ma, mi);
301 static void lvm_probe_all(blkid_cache cache, int only_if_new)
304 struct dirent *vg_iter;
305 int vg_len = strlen(VG_DIR);
308 if ((vg_list = opendir(VG_DIR)) == NULL)
311 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
313 while ((vg_iter = readdir(vg_list)) != NULL) {
317 struct dirent *lv_iter;
319 vg_name = vg_iter->d_name;
320 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
322 vdirname = malloc(vg_len + strlen(vg_name) + 8);
325 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
327 lv_list = opendir(vdirname);
332 while ((lv_iter = readdir(lv_list)) != NULL) {
333 char *lv_name, *lvm_device;
335 lv_name = lv_iter->d_name;
336 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
339 lvm_device = malloc(vg_len + strlen(vg_name) +
340 strlen(lv_name) + 8);
345 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
347 dev = lvm_get_devno(lvm_device);
348 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
349 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
351 (unsigned int) dev));
352 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
363 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
366 evms_probe_all(blkid_cache cache, int only_if_new)
369 int ma, mi, sz, num = 0;
373 procpt = fopen(PROC_EVMS_VOLUMES, "r");
376 while (fgets(line, sizeof(line), procpt)) {
377 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
378 &ma, &mi, &sz, device) != 4)
381 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
384 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
393 * Read the device data for all available block devices in the system.
395 static int probe_all(blkid_cache cache, int only_if_new)
399 char ptname0[128], ptname1[128], *ptname = 0;
403 unsigned long long sz;
404 int lens[2] = { 0, 0 };
405 int which = 0, last = 0;
406 struct list_head *p, *pnext;
408 ptnames[0] = ptname0;
409 ptnames[1] = ptname1;
412 return -BLKID_ERR_PARAM;
414 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
415 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
418 blkid_read_cache(cache);
419 evms_probe_all(cache, only_if_new);
421 lvm_probe_all(cache, only_if_new);
424 proc = fopen(PROC_PARTITIONS, "r");
426 return -BLKID_ERR_PROC;
428 while (fgets(line, sizeof(line), proc)) {
431 ptname = ptnames[which];
433 if (sscanf(line, " %d %d %llu %128[^\n ]",
434 &ma, &mi, &sz, ptname) != 4)
436 devs[which] = makedev(ma, mi);
438 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
440 /* Skip whole disk devs unless they have no partitions.
441 * If base name of device has changed, also
442 * check previous dev to see if it didn't have a partn.
443 * heuristic: partition name ends in a digit, & partition
444 * names contain whole device name as substring.
446 * Skip extended partitions.
447 * heuristic: size is 1
449 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
452 lens[which] = strlen(ptname);
454 /* ends in a digit, clearly a partition, so check */
455 if (isdigit(ptname[lens[which] - 1])) {
457 printf("partition dev %s, devno 0x%04X\n",
458 ptname, (unsigned int) devs[which]));
461 probe_one(cache, ptname, devs[which], 0,
463 lens[which] = 0; /* mark as checked */
467 * If last was a whole disk and we just found a partition
468 * on it, remove the whole-disk dev from the cache if
471 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
472 list_for_each_safe(p, pnext, &cache->bic_devs) {
475 /* find blkid dev for the whole-disk devno */
476 tmp = list_entry(p, struct blkid_struct_dev,
478 if (tmp->bid_devno == devs[last]) {
480 printf("freeing %s\n",
483 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
490 * If last was not checked because it looked like a whole-disk
491 * dev, and the device's base name has changed,
492 * check last as well.
494 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
496 printf("whole dev %s, devno 0x%04X\n",
497 ptnames[last], (unsigned int) devs[last]));
498 probe_one(cache, ptnames[last], devs[last], 0,
504 /* Handle the last device if it wasn't partitioned */
506 probe_one(cache, ptname, devs[which], 0, only_if_new);
509 blkid_flush_cache(cache);
513 int blkid_probe_all(blkid_cache cache)
517 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
518 ret = probe_all(cache, 0);
519 cache->bic_time = time(0);
520 cache->bic_flags |= BLKID_BIC_FL_PROBED;
521 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
525 int blkid_probe_all_new(blkid_cache cache)
529 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
530 ret = probe_all(cache, 1);
531 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
537 int main(int argc, char **argv)
539 blkid_cache cache = NULL;
542 blkid_debug_mask = DEBUG_ALL;
544 fprintf(stderr, "Usage: %s\n"
545 "Probe all devices and exit\n", argv[0]);
548 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
549 fprintf(stderr, "%s: error creating cache (%d)\n",
553 if (blkid_probe_all(cache) < 0)
554 printf("%s: error probing devices\n", argv[0]);
556 blkid_put_cache(cache);