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) {
94 dev2 = list_entry(p, struct blkid_struct_dev, bid_devs);
95 if (dev2->bid_flags & BLKID_BID_FL_VERIFIED)
97 if (!dev->bid_type || !dev2->bid_type ||
98 strcmp(dev->bid_type, dev2->bid_type))
100 if (dev->bid_label && dev2->bid_label &&
101 strcmp(dev->bid_label, dev2->bid_label))
103 if (dev->bid_uuid && dev2->bid_uuid &&
104 strcmp(dev->bid_uuid, dev2->bid_uuid))
106 if ((dev->bid_label && !dev2->bid_label) ||
107 (!dev->bid_label && dev2->bid_label) ||
108 (dev->bid_uuid && !dev2->bid_uuid) ||
109 (!dev->bid_uuid && dev2->bid_uuid))
111 dev2 = blkid_verify(cache, dev2);
112 if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED))
113 blkid_free_dev(dev2);
119 /* Directories where we will try to search for device names */
120 static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL };
122 static int is_dm_leaf(const char *devname)
124 struct dirent *de, *d_de;
129 if ((dir = opendir("/sys/block")) == NULL)
131 while ((de = readdir(dir)) != NULL) {
132 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
133 !strcmp(de->d_name, devname) ||
134 strncmp(de->d_name, "dm-", 3) ||
135 strlen(de->d_name) > sizeof(path)-32)
137 sprintf(path, "/sys/block/%s/slaves", de->d_name);
138 if ((d_dir = opendir(path)) == NULL)
140 while ((d_de = readdir(d_dir)) != NULL) {
141 if (!strcmp(d_de->d_name, devname)) {
155 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
156 * provides the real DM device names in /sys/block/<ptname>/dm/name
158 static char *get_dm_name(const char *ptname)
162 char path[256], name[256], *res = NULL;
164 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
165 if ((f = fopen(path, "r")) == NULL)
168 /* read "<name>\n" from sysfs */
169 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
171 snprintf(path, sizeof(path), "/dev/mapper/%s", name);
172 res = blkid_strdup(path);
179 * Probe a single block device to add to the device cache.
181 static void probe_one(blkid_cache cache, const char *ptname,
182 dev_t devno, int pri, int only_if_new)
184 blkid_dev dev = NULL;
185 struct list_head *p, *pnext;
187 char *devname = NULL;
189 /* See if we already have this device number in the cache. */
190 list_for_each_safe(p, pnext, &cache->bic_devs) {
191 blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
193 if (tmp->bid_devno == devno) {
194 if (only_if_new && !access(tmp->bid_name, F_OK))
196 dev = blkid_verify(cache, tmp);
197 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
202 if (dev && dev->bid_devno == devno)
205 /* Try to translate private device-mapper dm-<N> names
206 * to standard /dev/mapper/<name>.
208 if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
209 devname = get_dm_name(ptname);
211 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
217 * Take a quick look at /dev/ptname for the device number. We check
218 * all of the likely device directories. If we don't find it, or if
219 * the stat information doesn't check out, use blkid_devno_to_devname()
220 * to find it via an exhaustive search for the device major/minor.
222 for (dir = dirlist; *dir; dir++) {
226 sprintf(device, "%s/%s", *dir, ptname);
227 if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
228 dev->bid_devno == devno)
231 if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
232 st.st_rdev == devno) {
233 devname = blkid_strdup(device);
237 /* Do a short-cut scan of /dev/mapper first */
239 devname = get_dm_name(ptname);
241 blkid__scan_dir("/dev/mapper", devno, 0, &devname);
243 devname = blkid_devno_to_devname(devno);
248 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
254 else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
255 dev->bid_pri = BLKID_PRI_DM;
256 if (is_dm_leaf(ptname))
258 } else if (!strncmp(ptname, "md", 2))
259 dev->bid_pri = BLKID_PRI_MD;
264 #define PROC_PARTITIONS "/proc/partitions"
265 #define VG_DIR "/proc/lvm/VGs"
268 * This function initializes the UUID cache with devices from the LVM
269 * proc hierarchy. We currently depend on the names of the LVM
270 * hierarchy giving us the device structure in /dev. (XXX is this a
274 static dev_t lvm_get_devno(const char *lvm_device)
281 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
282 if ((lvf = fopen(lvm_device, "r")) == NULL) {
283 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
288 while (fgets(buf, sizeof(buf), lvf)) {
289 if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
290 ret = makedev(ma, mi);
299 static void lvm_probe_all(blkid_cache cache, int only_if_new)
302 struct dirent *vg_iter;
303 int vg_len = strlen(VG_DIR);
306 if ((vg_list = opendir(VG_DIR)) == NULL)
309 DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
311 while ((vg_iter = readdir(vg_list)) != NULL) {
315 struct dirent *lv_iter;
317 vg_name = vg_iter->d_name;
318 if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
320 vdirname = malloc(vg_len + strlen(vg_name) + 8);
323 sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
325 lv_list = opendir(vdirname);
330 while ((lv_iter = readdir(lv_list)) != NULL) {
331 char *lv_name, *lvm_device;
333 lv_name = lv_iter->d_name;
334 if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
337 lvm_device = malloc(vg_len + strlen(vg_name) +
338 strlen(lv_name) + 8);
343 sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
345 dev = lvm_get_devno(lvm_device);
346 sprintf(lvm_device, "%s/%s", vg_name, lv_name);
347 DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
349 (unsigned int) dev));
350 probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
361 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
364 evms_probe_all(blkid_cache cache, int only_if_new)
367 int ma, mi, sz, num = 0;
371 procpt = fopen(PROC_EVMS_VOLUMES, "r");
374 while (fgets(line, sizeof(line), procpt)) {
375 if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
376 &ma, &mi, &sz, device) != 4)
379 DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
382 probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
391 * Read the device data for all available block devices in the system.
393 static int probe_all(blkid_cache cache, int only_if_new)
397 char ptname0[128], ptname1[128], *ptname = 0;
401 unsigned long long sz;
402 int lens[2] = { 0, 0 };
403 int which = 0, last = 0;
404 struct list_head *p, *pnext;
406 ptnames[0] = ptname0;
407 ptnames[1] = ptname1;
410 return -BLKID_ERR_PARAM;
412 if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
413 time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
416 blkid_read_cache(cache);
417 evms_probe_all(cache, only_if_new);
419 lvm_probe_all(cache, only_if_new);
422 proc = fopen(PROC_PARTITIONS, "r");
424 return -BLKID_ERR_PROC;
426 while (fgets(line, sizeof(line), proc)) {
429 ptname = ptnames[which];
431 if (sscanf(line, " %d %d %llu %128[^\n ]",
432 &ma, &mi, &sz, ptname) != 4)
434 devs[which] = makedev(ma, mi);
436 DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
438 /* Skip whole disk devs unless they have no partitions.
439 * If base name of device has changed, also
440 * check previous dev to see if it didn't have a partn.
441 * heuristic: partition name ends in a digit, & partition
442 * names contain whole device name as substring.
444 * Skip extended partitions.
445 * heuristic: size is 1
447 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
450 lens[which] = strlen(ptname);
452 /* ends in a digit, clearly a partition, so check */
453 if (isdigit(ptname[lens[which] - 1])) {
455 printf("partition dev %s, devno 0x%04X\n",
456 ptname, (unsigned int) devs[which]));
459 probe_one(cache, ptname, devs[which], 0,
461 lens[which] = 0; /* mark as checked */
465 * If last was a whole disk and we just found a partition
466 * on it, remove the whole-disk dev from the cache if
469 if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
470 list_for_each_safe(p, pnext, &cache->bic_devs) {
473 /* find blkid dev for the whole-disk devno */
474 tmp = list_entry(p, struct blkid_struct_dev,
476 if (tmp->bid_devno == devs[last]) {
478 printf("freeing %s\n",
481 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
488 * If last was not checked because it looked like a whole-disk
489 * dev, and the device's base name has changed,
490 * check last as well.
492 if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
494 printf("whole dev %s, devno 0x%04X\n",
495 ptnames[last], (unsigned int) devs[last]));
496 probe_one(cache, ptnames[last], devs[last], 0,
502 /* Handle the last device if it wasn't partitioned */
504 probe_one(cache, ptname, devs[which], 0, only_if_new);
507 blkid_flush_cache(cache);
511 int blkid_probe_all(blkid_cache cache)
515 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
516 ret = probe_all(cache, 0);
517 cache->bic_time = time(0);
518 cache->bic_flags |= BLKID_BIC_FL_PROBED;
519 DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
523 int blkid_probe_all_new(blkid_cache cache)
527 DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
528 ret = probe_all(cache, 1);
529 DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
535 int main(int argc, char **argv)
537 blkid_cache cache = NULL;
540 blkid_debug_mask = DEBUG_ALL;
542 fprintf(stderr, "Usage: %s\n"
543 "Probe all devices and exit\n", argv[0]);
546 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
547 fprintf(stderr, "%s: error creating cache (%d)\n",
551 if (blkid_probe_all(cache) < 0)
552 printf("%s: error probing devices\n", argv[0]);
554 blkid_put_cache(cache);