2
* devno.c - find a particular device by its device number (major/minor)
4
* Copyright (C) 2000, 2001, 2003 Theodore Ts'o
5
* Copyright (C) 2001 Andreas Dilger
8
* This file may be redistributed under the terms of the
9
* GNU Lesser General Public License.
20
#include <sys/types.h>
30
#include <sys/mkdev.h>
36
#include "pathnames.h"
40
char *blkid_strndup(const char *s, int length)
50
ret = malloc(length + 1);
52
strncpy(ret, s, length);
58
char *blkid_strdup(const char *s)
60
return blkid_strndup(s, 0);
63
char *blkid_strconcat(const char *a, const char *b, const char *c)
66
size_t len, al, bl, cl;
68
al = a ? strlen(a) : 0;
69
bl = b ? strlen(b) : 0;
70
cl = c ? strlen(c) : 0;
75
p = res = malloc(len + 1);
95
* This function adds an entry to the directory list
97
static void add_to_dirlist(const char *dir, const char *subdir,
98
struct dir_list **list)
102
dp = malloc(sizeof(struct dir_list));
105
dp->name = subdir ? blkid_strconcat(dir, "/", subdir) :
116
* This function frees a directory list
118
static void free_dirlist(struct dir_list **list)
120
struct dir_list *dp, *next;
122
for (dp = *list; dp; dp = next) {
130
void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
137
if ((dir = opendir(dirname)) == NULL)
140
while ((dp = readdir(dir)) != 0) {
141
#ifdef _DIRENT_HAVE_D_TYPE
142
if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_BLK &&
143
dp->d_type != DT_LNK && dp->d_type != DT_DIR)
146
if (dp->d_name[0] == '.' &&
147
((dp->d_name[1] == 0) ||
148
((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
151
if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 0))
154
if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
155
*devname = blkid_strconcat(dirname, "/", dp->d_name);
157
printf("found 0x%llx at %s\n", (long long)devno,
162
if (!list || !S_ISDIR(st.st_mode))
165
/* add subdirectory (but not symlink) to the list */
166
#ifdef _DIRENT_HAVE_D_TYPE
167
if (dp->d_type == DT_LNK)
169
if (dp->d_type == DT_UNKNOWN)
172
if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 1) ||
173
!S_ISDIR(st.st_mode))
174
continue; /* symlink or lstat() failed */
177
if (*dp->d_name == '.' || (
178
#ifdef _DIRENT_HAVE_D_TYPE
179
dp->d_type == DT_DIR &&
181
strcmp(dp->d_name, "shm") == 0))
182
/* ignore /dev/.{udev,mount,mdadm} and /dev/shm */
185
add_to_dirlist(dirname, dp->d_name, list);
191
/* Directories where we will try to search for device numbers */
192
static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
196
* @title: Miscellaneous utils
197
* @short_description: mix of various utils for low-level and high-level API
200
/* returns basename and keeps dirname in the @path */
201
static char *stripoff_last_component(char *path)
203
char *p = strrchr(path, '/');
211
static char *scandev_devno_to_devpath(dev_t devno)
213
struct dir_list *list = NULL, *new_list = NULL;
214
char *devname = NULL;
218
* Add the starting directories to search in reverse order of
219
* importance, since we are using a stack...
221
for (dir = devdirs; *dir; dir++)
222
add_to_dirlist(*dir, NULL, &list);
225
struct dir_list *current = list;
228
DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
229
blkid__scan_dir(current->name, devno, &new_list, &devname);
235
* If we're done checking at this level, descend to
236
* the next level of subdirectories. (breadth-first)
244
free_dirlist(&new_list);
250
* blkid_devno_to_devname:
251
* @devno: device number
253
* This function finds the pathname to a block device with a given
256
* Returns: a pointer to allocated memory to the pathname on success,
257
* and NULL on failure.
259
char *blkid_devno_to_devname(dev_t devno)
264
path = sysfs_devno_to_devpath(devno, buf, sizeof(buf));
268
path = scandev_devno_to_devpath(devno);
272
printf("blkid: couldn't find devno 0x%04lx\n",
273
(unsigned long) devno));
276
printf("found devno 0x%04llx as %s\n", (long long)devno, path));
282
static int get_dm_wholedisk(struct sysfs_cxt *cxt, char *diskname,
283
size_t len, dev_t *diskdevno)
288
/* Note, sysfs_get_slave() returns the first slave only,
289
* if there is more slaves, then return NULL
291
name = sysfs_get_slave(cxt);
295
if (diskname && len) {
296
strncpy(diskname, name, len);
297
diskname[len - 1] = '\0';
301
*diskdevno = sysfs_devname_to_devno(name, NULL);
311
* blkid_devno_to_wholedisk:
312
* @dev: device number
313
* @diskname: buffer to return diskname (or NULL)
314
* @len: diskname buffer size (or 0)
315
* @diskdevno: pointer to returns devno of entire disk (or NULL)
317
* This function uses sysfs to convert the @devno device number to the *name*
318
* of the whole disk. The function DOES NOT return full device name. The @dev
319
* argument could be partition or whole disk -- both is converted.
321
* For example: sda1, 0x0801 --> sda, 0x0800
323
* For conversion to the full disk *path* use blkid_devno_to_devname(), for
329
* dev_t dev = 0x0801, disk; // sda1 = 8:1
330
* char *diskpath, diskname[32];
332
* blkid_devno_to_wholedisk(dev, diskname, sizeof(diskname), &disk);
333
* diskpath = blkid_devno_to_devname(disk);
335
* // print "0x0801: sda, /dev/sda, 8:0
336
* printf("0x%x: %s, %s, %d:%d\n",
337
* dev, diskname, diskpath, major(disk), minor(disk));
344
* Returns: 0 on success or -1 in case of error.
346
int blkid_devno_to_wholedisk(dev_t dev, char *diskname,
347
size_t len, dev_t *diskdevno)
349
struct sysfs_cxt cxt;
352
if (!dev || sysfs_init(&cxt, dev, NULL) != 0)
355
is_part = sysfs_has_attribute(&cxt, "partition");
358
* Extra case for partitions mapped by device-mapper.
360
* All regualar partitions (added by BLKPG ioctl or kernel PT
361
* parser) have the /sys/.../partition file. The partitions
362
* mapped by DM don't have such file, but they have "part"
365
char *uuid = sysfs_strdup(&cxt, "dm/uuid");
367
char *prefix = uuid ? strsep(&tmp, "-") : NULL;
369
if (prefix && strncasecmp(prefix, "part", 4) == 0)
374
get_dm_wholedisk(&cxt, diskname, len, diskdevno) == 0)
376
* partitioned device, mapped by DM
385
* unpartitioned device
387
if (diskname && len) {
388
if (!sysfs_get_devname(&cxt, diskname, len))
397
* - readlink /sys/dev/block/8:1 = ../../block/sda/sda1
398
* - dirname ../../block/sda/sda1 = ../../block/sda
399
* - basename ../../block/sda = sda
401
char linkpath[PATH_MAX];
405
linklen = sysfs_readlink(&cxt, NULL,
406
linkpath, sizeof(linkpath) - 1);
409
linkpath[linklen] = '\0';
411
stripoff_last_component(linkpath); /* dirname */
412
name = stripoff_last_component(linkpath); /* basename */
416
if (diskname && len) {
417
strncpy(diskname, name, len);
418
diskname[len - 1] = '\0';
422
*diskdevno = sysfs_devname_to_devno(name, NULL);
432
printf("found entire diskname for devno 0x%04llx %s\n",
433
(long long) dev, diskname ? diskname : ""));
439
printf("failed to convert 0x%04llx to wholedisk name, errno=%d\n",
440
(long long) dev, errno));
445
* Returns 1 if the @major number is associated with @drvname.
447
int blkid_driver_has_major(const char *drvname, int major)
453
f = fopen(_PATH_PROC_DEVICES, "r");
457
while (fgets(buf, sizeof(buf), f)) { /* skip to block dev section */
458
if (strncmp("Block devices:\n", buf, sizeof(buf)) == 0)
462
while (fgets(buf, sizeof(buf), f)) {
466
if (sscanf(buf, "%d %64[^\n ]", &maj, name) != 2)
469
if (maj == major && strcmp(name, drvname) == 0) {
477
DBG(DEBUG_DEVNO, printf("major %d %s associated with '%s' driver\n",
478
major, match ? "is" : "is NOT", drvname));
484
int main(int argc, char** argv)
487
char diskname[PATH_MAX];
489
dev_t devno, disk_devno;
490
const char *errmsg = "Couldn't parse %s: %s\n";
492
blkid_init_debug(DEBUG_ALL);
493
if ((argc != 2) && (argc != 3)) {
494
fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
495
"Resolve a device number to a device name\n",
500
devno = strtoul(argv[1], &tmp, 0);
502
fprintf(stderr, errmsg, "device number", argv[1]);
506
major = strtoul(argv[1], &tmp, 0);
508
fprintf(stderr, errmsg, "major number", argv[1]);
511
minor = strtoul(argv[2], &tmp, 0);
513
fprintf(stderr, errmsg, "minor number", argv[2]);
516
devno = makedev(major, minor);
518
printf("Looking for device 0x%04llx\n", (long long)devno);
519
devname = blkid_devno_to_devname(devno);
522
printf("Looking for whole-device for 0x%04llx\n", (long long)devno);
523
blkid_devno_to_wholedisk(devno, diskname, sizeof(diskname), &disk_devno);