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>
39
#include "canonicalize.h" /* $(top_srcdir)/include */
40
#include "pathnames.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;
53
struct list_head *p, *pnext;
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
if (access(devname, F_OK) < 0)
72
dev = blkid_new_dev();
75
dev->bid_time = INT_MIN;
76
dev->bid_name = blkid_strdup(devname);
77
dev->bid_cache = cache;
78
list_add_tail(&dev->bid_devs, &cache->bic_devs);
79
cache->bic_flags |= BLKID_BIC_FL_CHANGED;
82
if (flags & BLKID_DEV_VERIFY) {
83
dev = blkid_verify(cache, dev);
84
if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
87
* If the device is verified, then search the blkid
88
* cache for any entries that match on the type, uuid,
89
* and label, and verify them; if a cache entry can
90
* not be verified, then it's stale and so we remove
93
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
* Probe a single block device to add to the device cache.
160
static void probe_one(blkid_cache cache, const char *ptname,
161
dev_t devno, int pri, int only_if_new, int removable)
163
blkid_dev dev = NULL;
164
struct list_head *p, *pnext;
166
char *devname = NULL;
168
/* See if we already have this device number in the cache. */
169
list_for_each_safe(p, pnext, &cache->bic_devs) {
170
blkid_dev tmp = list_entry(p, struct blkid_struct_dev,
172
if (tmp->bid_devno == devno) {
173
if (only_if_new && !access(tmp->bid_name, F_OK))
175
dev = blkid_verify(cache, tmp);
176
if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
181
if (dev && dev->bid_devno == devno)
184
/* Try to translate private device-mapper dm-<N> names
185
* to standard /dev/mapper/<name>.
187
if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) {
188
devname = canonicalize_dm_name(ptname);
190
blkid__scan_dir("/dev/mapper", devno, 0, &devname);
196
* Take a quick look at /dev/ptname for the device number. We check
197
* all of the likely device directories. If we don't find it, or if
198
* the stat information doesn't check out, use blkid_devno_to_devname()
199
* to find it via an exhaustive search for the device major/minor.
201
for (dir = dirlist; *dir; dir++) {
205
sprintf(device, "%s/%s", *dir, ptname);
206
if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
207
dev->bid_devno == devno)
210
if (stat(device, &st) == 0 &&
211
(S_ISBLK(st.st_mode) ||
212
(S_ISCHR(st.st_mode) && !strncmp(ptname, "ubi", 3))) &&
213
st.st_rdev == devno) {
214
devname = blkid_strdup(device);
218
/* Do a short-cut scan of /dev/mapper first */
220
blkid__scan_dir("/dev/mapper", devno, 0, &devname);
222
devname = blkid_devno_to_devname(devno);
228
dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
235
else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) {
236
dev->bid_pri = BLKID_PRI_DM;
237
if (is_dm_leaf(ptname))
239
} else if (!strncmp(ptname, "md", 2))
240
dev->bid_pri = BLKID_PRI_MD;
242
dev->bid_flags |= BLKID_BID_FL_REMOVABLE;
247
#define PROC_PARTITIONS "/proc/partitions"
248
#define VG_DIR "/proc/lvm/VGs"
251
* This function initializes the UUID cache with devices from the LVM
252
* proc hierarchy. We currently depend on the names of the LVM
253
* hierarchy giving us the device structure in /dev. (XXX is this a
257
static dev_t lvm_get_devno(const char *lvm_device)
264
DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
265
if ((lvf = fopen(lvm_device, "r")) == NULL) {
266
DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
271
while (fgets(buf, sizeof(buf), lvf)) {
272
if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) {
273
ret = makedev(ma, mi);
282
static void lvm_probe_all(blkid_cache cache, int only_if_new)
285
struct dirent *vg_iter;
286
int vg_len = strlen(VG_DIR);
289
if ((vg_list = opendir(VG_DIR)) == NULL)
292
DBG(DEBUG_DEVNAME, printf("probing LVM devices under %s\n", VG_DIR));
294
while ((vg_iter = readdir(vg_list)) != NULL) {
298
struct dirent *lv_iter;
300
vg_name = vg_iter->d_name;
301
if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
303
vdirname = malloc(vg_len + strlen(vg_name) + 8);
306
sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
308
lv_list = opendir(vdirname);
313
while ((lv_iter = readdir(lv_list)) != NULL) {
314
char *lv_name, *lvm_device;
316
lv_name = lv_iter->d_name;
317
if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
320
lvm_device = malloc(vg_len + strlen(vg_name) +
321
strlen(lv_name) + 8);
326
sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
328
dev = lvm_get_devno(lvm_device);
329
sprintf(lvm_device, "%s/%s", vg_name, lv_name);
330
DBG(DEBUG_DEVNAME, printf("LVM dev %s: devno 0x%04X\n",
332
(unsigned int) dev));
333
probe_one(cache, lvm_device, dev, BLKID_PRI_LVM,
344
#define PROC_EVMS_VOLUMES "/proc/evms/volumes"
347
evms_probe_all(blkid_cache cache, int only_if_new)
350
int ma, mi, sz, num = 0;
354
procpt = fopen(PROC_EVMS_VOLUMES, "r");
357
while (fgets(line, sizeof(line), procpt)) {
358
if (sscanf (line, " %d %d %d %*s %*s %[^\n ]",
359
&ma, &mi, &sz, device) != 4)
362
DBG(DEBUG_DEVNAME, printf("Checking partition %s (%d, %d)\n",
365
probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS,
374
ubi_probe_all(blkid_cache cache, int only_if_new)
376
const char **dirname;
378
for (dirname = dirlist; *dirname; dirname++) {
379
DBG(DEBUG_DEVNAME, printf("probing UBI volumes under %s\n",
385
dir = opendir(*dirname);
389
while ((iter = readdir(dir)) != NULL) {
395
#ifdef _DIRENT_HAVE_D_TYPE
396
if (iter->d_type != DT_UNKNOWN &&
397
iter->d_type != DT_CHR && iter->d_type != DT_LNK)
400
if (!strcmp(name, ".") || !strcmp(name, "..") ||
401
!strstr(name, "ubi"))
403
if (!strcmp(name, "ubi_ctrl"))
405
if (fstat_at(dirfd(dir), *dirname, name, &st, 0))
410
if (!S_ISCHR(st.st_mode) || !minor(dev))
412
DBG(DEBUG_DEVNAME, printf("UBI vol %s/%s: devno 0x%04X\n",
413
*dirname, name, (int) dev));
414
probe_one(cache, name, dev, BLKID_PRI_UBI, only_if_new, 0);
421
* Read the device data for all available block devices in the system.
423
static int probe_all(blkid_cache cache, int only_if_new)
427
char ptname0[128], ptname1[128], *ptname = 0;
431
unsigned long long sz;
432
int lens[2] = { 0, 0 };
433
int which = 0, last = 0;
434
struct list_head *p, *pnext;
436
ptnames[0] = ptname0;
437
ptnames[1] = ptname1;
440
return -BLKID_ERR_PARAM;
442
if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
443
time(0) - cache->bic_time < BLKID_PROBE_INTERVAL)
446
blkid_read_cache(cache);
447
evms_probe_all(cache, only_if_new);
449
lvm_probe_all(cache, only_if_new);
451
ubi_probe_all(cache, only_if_new);
453
proc = fopen(PROC_PARTITIONS, "r");
455
return -BLKID_ERR_PROC;
457
while (fgets(line, sizeof(line), proc)) {
460
ptname = ptnames[which];
462
if (sscanf(line, " %d %d %llu %128[^\n ]",
463
&ma, &mi, &sz, ptname) != 4)
465
devs[which] = makedev(ma, mi);
467
DBG(DEBUG_DEVNAME, printf("read partition name %s\n", ptname));
469
/* Skip whole disk devs unless they have no partitions.
470
* If base name of device has changed, also
471
* check previous dev to see if it didn't have a partn.
472
* heuristic: partition name ends in a digit, & partition
473
* names contain whole device name as substring.
475
* Skip extended partitions.
476
* heuristic: size is 1
478
* FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
481
lens[which] = strlen(ptname);
483
/* ends in a digit, clearly a partition, so check */
484
if (isdigit(ptname[lens[which] - 1])) {
486
printf("partition dev %s, devno 0x%04X\n",
487
ptname, (unsigned int) devs[which]));
490
probe_one(cache, ptname, devs[which], 0,
492
lens[which] = 0; /* mark as checked */
496
* If last was a whole disk and we just found a partition
497
* on it, remove the whole-disk dev from the cache if
500
if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) {
501
list_for_each_safe(p, pnext, &cache->bic_devs) {
504
/* find blkid dev for the whole-disk devno */
505
tmp = list_entry(p, struct blkid_struct_dev,
507
if (tmp->bid_devno == devs[last]) {
509
printf("freeing %s\n",
512
cache->bic_flags |= BLKID_BIC_FL_CHANGED;
519
* If last was not checked because it looked like a whole-disk
520
* dev, and the device's base name has changed,
521
* check last as well.
523
if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) {
525
printf("whole dev %s, devno 0x%04X\n",
526
ptnames[last], (unsigned int) devs[last]));
527
probe_one(cache, ptnames[last], devs[last], 0,
533
/* Handle the last device if it wasn't partitioned */
535
probe_one(cache, ptname, devs[which], 0, only_if_new, 0);
538
blkid_flush_cache(cache);
542
/* Don't use it by default -- it's pretty slow (because cdroms, floppy, ...)
544
static int probe_all_removable(blkid_cache cache)
550
return -BLKID_ERR_PARAM;
552
dir = opendir(_PATH_SYS_BLOCK);
554
return -BLKID_ERR_PROC;
556
while((d = readdir(dir))) {
557
struct sysfs_cxt sysfs;
561
#ifdef _DIRENT_HAVE_D_TYPE
562
if (d->d_type != DT_UNKNOWN && d->d_type != DT_LNK)
565
if (d->d_name[0] == '.' &&
566
((d->d_name[1] == 0) ||
567
((d->d_name[1] == '.') && (d->d_name[2] == 0))))
570
devno = sysfs_devname_to_devno(d->d_name, NULL);
574
if (sysfs_init(&sysfs, devno, NULL) == 0) {
575
sysfs_read_int(&sysfs, "removable", &removable);
576
sysfs_deinit(&sysfs);
580
probe_one(cache, d->d_name, devno, 0, 0, 1);
590
* @cache: cache handler
592
* Probes all block devices.
594
* Returns: 0 on success, or number less than zero in case of error.
596
int blkid_probe_all(blkid_cache cache)
600
DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
601
ret = probe_all(cache, 0);
602
cache->bic_time = time(0);
603
cache->bic_flags |= BLKID_BIC_FL_PROBED;
604
DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
609
* blkid_probe_all_new:
610
* @cache: cache handler
612
* Probes all new block devices.
614
* Returns: 0 on success, or number less than zero in case of error.
616
int blkid_probe_all_new(blkid_cache cache)
620
DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
621
ret = probe_all(cache, 1);
622
DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
627
* blkid_probe_all_removable:
628
* @cache: cache handler
630
* The libblkid probing is based on devices from /proc/partitions by default.
631
* This file usually does not contain removable devices (e.g. CDROMs) and this kind
632
* of devices are invisible for libblkid.
634
* This function adds removable block devices to @cache (probing is based on
635
* information from the /sys directory). Don't forget that removable devices
636
* (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call
637
* this function by default.
639
* Note that devices which were detected by this function won't be written to
640
* blkid.tab cache file.
642
* Returns: 0 on success, or number less than zero in case of error.
644
int blkid_probe_all_removable(blkid_cache cache)
648
DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_removable()\n"));
649
ret = probe_all_removable(cache);
650
DBG(DEBUG_PROBE, printf("End blkid_probe_all_removable()\n"));
655
int main(int argc, char **argv)
657
blkid_cache cache = NULL;
660
blkid_init_debug(DEBUG_ALL);
662
fprintf(stderr, "Usage: %s\n"
663
"Probe all devices and exit\n", argv[0]);
666
if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
667
fprintf(stderr, "%s: error creating cache (%d)\n",
671
if (blkid_probe_all(cache) < 0)
672
printf("%s: error probing devices\n", argv[0]);
674
if (blkid_probe_all_removable(cache) < 0)
675
printf("%s: error probing removable devices\n", argv[0]);
677
blkid_put_cache(cache);