]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/devname.c
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.
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
29 #ifdef HAVE_SYS_STAT_H
39 #include "canonicalize.h" /* $(top_srcdir)/include */
40 #include "pathnames.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
;
55 if (!cache
|| !devname
)
59 list_for_each(p
, &cache
->bic_devs
) {
60 tmp
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
61 if (strcmp(tmp
->bid_name
, devname
))
67 /* try canonicalize the name */
68 if (!dev
&& (cn
= canonicalize_path(devname
))) {
69 if (strcmp(cn
, devname
) != 0) {
70 DBG(DEVNAME
, ul_debug("search canonical %s", cn
));
71 list_for_each(p
, &cache
->bic_devs
) {
72 tmp
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
73 if (strcmp(tmp
->bid_name
, cn
))
77 /* update name returned by blkid_dev_devname() */
79 dev
->bid_xname
= strdup(devname
);
88 if (!dev
&& (flags
& BLKID_DEV_CREATE
)) {
89 if (access(devname
, F_OK
) < 0)
91 dev
= blkid_new_dev();
94 dev
->bid_time
= INT_MIN
;
97 dev
->bid_xname
= strdup(devname
);
98 cn
= NULL
; /* see free() below */
100 dev
->bid_name
= strdup(devname
);
102 dev
->bid_cache
= cache
;
103 list_add_tail(&dev
->bid_devs
, &cache
->bic_devs
);
104 cache
->bic_flags
|= BLKID_BIC_FL_CHANGED
;
107 if (flags
& BLKID_DEV_VERIFY
) {
108 dev
= blkid_verify(cache
, dev
);
109 if (!dev
|| !(dev
->bid_flags
& BLKID_BID_FL_VERIFIED
))
112 * If the device is verified, then search the blkid
113 * cache for any entries that match on the type, uuid,
114 * and label, and verify them; if a cache entry can
115 * not be verified, then it's stale and so we remove
118 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
119 blkid_dev dev2
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
120 if (dev2
->bid_flags
& BLKID_BID_FL_VERIFIED
)
122 if (!dev
->bid_type
|| !dev2
->bid_type
||
123 strcmp(dev
->bid_type
, dev2
->bid_type
))
125 if (dev
->bid_label
&& dev2
->bid_label
&&
126 strcmp(dev
->bid_label
, dev2
->bid_label
))
128 if (dev
->bid_uuid
&& dev2
->bid_uuid
&&
129 strcmp(dev
->bid_uuid
, dev2
->bid_uuid
))
131 if ((dev
->bid_label
&& !dev2
->bid_label
) ||
132 (!dev
->bid_label
&& dev2
->bid_label
) ||
133 (dev
->bid_uuid
&& !dev2
->bid_uuid
) ||
134 (!dev
->bid_uuid
&& dev2
->bid_uuid
))
136 dev2
= blkid_verify(cache
, dev2
);
137 if (dev2
&& !(dev2
->bid_flags
& BLKID_BID_FL_VERIFIED
))
138 blkid_free_dev(dev2
);
143 DBG(DEVNAME
, ul_debug("%s requested, found %s in cache", devname
, dev
->bid_name
));
148 /* Directories where we will try to search for device names */
149 static const char *dirlist
[] = { "/dev", "/devfs", "/devices", NULL
};
151 static int is_dm_leaf(const char *devname
)
153 struct dirent
*de
, *d_de
;
155 char path
[NAME_MAX
+ 18 + 1];
158 if ((dir
= opendir("/sys/block")) == NULL
)
160 while ((de
= readdir(dir
)) != NULL
) {
161 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, "..") ||
162 !strcmp(de
->d_name
, devname
) ||
163 strncmp(de
->d_name
, "dm-", 3) ||
164 strlen(de
->d_name
) > sizeof(path
)-32)
166 sprintf(path
, "/sys/block/%s/slaves", de
->d_name
);
167 if ((d_dir
= opendir(path
)) == NULL
)
169 while ((d_de
= readdir(d_dir
)) != NULL
) {
170 if (!strcmp(d_de
->d_name
, devname
)) {
184 * Probe a single block device to add to the device cache.
186 static void probe_one(blkid_cache cache
, const char *ptname
,
187 dev_t devno
, int pri
, int only_if_new
, int removable
)
189 blkid_dev dev
= NULL
;
190 struct list_head
*p
, *pnext
;
192 char *devname
= NULL
;
194 /* See if we already have this device number in the cache. */
195 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
196 blkid_dev tmp
= list_entry(p
, struct blkid_struct_dev
,
198 if (tmp
->bid_devno
== devno
) {
199 if (only_if_new
&& !access(tmp
->bid_name
, F_OK
))
201 dev
= blkid_verify(cache
, tmp
);
202 if (dev
&& (dev
->bid_flags
& BLKID_BID_FL_VERIFIED
))
207 if (dev
&& dev
->bid_devno
== devno
)
210 /* Try to translate private device-mapper dm-<N> names
211 * to standard /dev/mapper/<name>.
213 if (!strncmp(ptname
, "dm-", 3) && isdigit(ptname
[3])) {
214 devname
= canonicalize_dm_name(ptname
);
216 blkid__scan_dir("/dev/mapper", devno
, NULL
, &devname
);
222 * Take a quick look at /dev/ptname for the device number. We check
223 * all of the likely device directories. If we don't find it, or if
224 * the stat information doesn't check out, use blkid_devno_to_devname()
225 * to find it via an exhaustive search for the device major/minor.
227 for (dir
= dirlist
; *dir
; dir
++) {
231 snprintf(device
, sizeof(device
), "%s/%s", *dir
, ptname
);
232 if ((dev
= blkid_get_dev(cache
, device
, BLKID_DEV_FIND
)) &&
233 dev
->bid_devno
== devno
)
236 if (stat(device
, &st
) == 0 &&
237 (S_ISBLK(st
.st_mode
) ||
238 (S_ISCHR(st
.st_mode
) && !strncmp(ptname
, "ubi", 3))) &&
239 st
.st_rdev
== devno
) {
240 devname
= strdup(device
);
244 /* Do a short-cut scan of /dev/mapper first */
246 blkid__scan_dir("/dev/mapper", devno
, NULL
, &devname
);
248 devname
= blkid_devno_to_devname(devno
);
254 dev
= blkid_get_dev(cache
, devname
, BLKID_DEV_NORMAL
);
261 else if (!strncmp(dev
->bid_name
, "/dev/mapper/", 11)) {
262 dev
->bid_pri
= BLKID_PRI_DM
;
263 if (is_dm_leaf(ptname
))
265 } else if (!strncmp(ptname
, "md", 2))
266 dev
->bid_pri
= BLKID_PRI_MD
;
268 dev
->bid_flags
|= BLKID_BID_FL_REMOVABLE
;
273 #define PROC_PARTITIONS "/proc/partitions"
274 #define VG_DIR "/proc/lvm/VGs"
277 * This function initializes the UUID cache with devices from the LVM
278 * proc hierarchy. We currently depend on the names of the LVM
279 * hierarchy giving us the device structure in /dev. (XXX is this a
283 static dev_t
lvm_get_devno(const char *lvm_device
)
290 DBG(DEVNAME
, ul_debug("opening %s", lvm_device
));
291 if ((lvf
= fopen(lvm_device
, "r" UL_CLOEXECSTR
)) == NULL
) {
292 DBG(DEVNAME
, ul_debug("%s: (%d) %m", lvm_device
, errno
));
296 while (fgets(buf
, sizeof(buf
), lvf
)) {
297 if (sscanf(buf
, "device: %d:%d", &ma
, &mi
) == 2) {
298 ret
= makedev(ma
, mi
);
307 static void lvm_probe_all(blkid_cache cache
, int only_if_new
)
310 struct dirent
*vg_iter
;
311 int vg_len
= strlen(VG_DIR
);
314 if ((vg_list
= opendir(VG_DIR
)) == NULL
)
317 DBG(DEVNAME
, ul_debug("probing LVM devices under %s", VG_DIR
));
319 while ((vg_iter
= readdir(vg_list
)) != NULL
) {
323 struct dirent
*lv_iter
;
325 vg_name
= vg_iter
->d_name
;
326 if (!strcmp(vg_name
, ".") || !strcmp(vg_name
, ".."))
328 vdirname
= malloc(vg_len
+ strlen(vg_name
) + 8);
331 sprintf(vdirname
, "%s/%s/LVs", VG_DIR
, vg_name
);
333 lv_list
= opendir(vdirname
);
338 while ((lv_iter
= readdir(lv_list
)) != NULL
) {
339 char *lv_name
, *lvm_device
;
341 lv_name
= lv_iter
->d_name
;
342 if (!strcmp(lv_name
, ".") || !strcmp(lv_name
, ".."))
345 lvm_device
= malloc(vg_len
+ strlen(vg_name
) +
346 strlen(lv_name
) + 8);
351 sprintf(lvm_device
, "%s/%s/LVs/%s", VG_DIR
, vg_name
,
353 dev
= lvm_get_devno(lvm_device
);
354 sprintf(lvm_device
, "%s/%s", vg_name
, lv_name
);
355 DBG(DEVNAME
, ul_debug("LVM dev %s: devno 0x%04X",
357 (unsigned int) dev
));
358 probe_one(cache
, lvm_device
, dev
, BLKID_PRI_LVM
,
369 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
372 evms_probe_all(blkid_cache cache
, int only_if_new
)
375 int ma
, mi
, sz
, num
= 0;
379 procpt
= fopen(PROC_EVMS_VOLUMES
, "r" UL_CLOEXECSTR
);
382 while (fgets(line
, sizeof(line
), procpt
)) {
383 if (sscanf (line
, " %d %d %d %*s %*s %[^\n ]",
384 &ma
, &mi
, &sz
, device
) != 4)
387 DBG(DEVNAME
, ul_debug("Checking partition %s (%d, %d)",
390 probe_one(cache
, device
, makedev(ma
, mi
), BLKID_PRI_EVMS
,
399 ubi_probe_all(blkid_cache cache
, int only_if_new
)
401 const char **dirname
;
403 for (dirname
= dirlist
; *dirname
; dirname
++) {
407 DBG(DEVNAME
, ul_debug("probing UBI volumes under %s",
410 dir
= opendir(*dirname
);
414 while ((iter
= readdir(dir
)) != NULL
) {
420 #ifdef _DIRENT_HAVE_D_TYPE
421 if (iter
->d_type
!= DT_UNKNOWN
&&
422 iter
->d_type
!= DT_CHR
&& iter
->d_type
!= DT_LNK
)
425 if (!strcmp(name
, ".") || !strcmp(name
, "..") ||
426 !strstr(name
, "ubi"))
428 if (!strcmp(name
, "ubi_ctrl"))
430 if (fstatat(dirfd(dir
), name
, &st
, 0))
435 if (!S_ISCHR(st
.st_mode
) || !minor(dev
))
437 DBG(DEVNAME
, ul_debug("UBI vol %s/%s: devno 0x%04X",
438 *dirname
, name
, (int) dev
));
439 probe_one(cache
, name
, dev
, BLKID_PRI_UBI
, only_if_new
, 0);
446 * Read the device data for all available block devices in the system.
448 static int probe_all(blkid_cache cache
, int only_if_new
)
452 char ptname0
[128 + 1], ptname1
[128 + 1], *ptname
= NULL
;
454 dev_t devs
[2] = { 0, 0 };
455 int iswhole
[2] = { 0, 0 };
457 unsigned long long sz
;
458 int lens
[2] = { 0, 0 };
459 int which
= 0, last
= 0;
460 struct list_head
*p
, *pnext
;
462 ptnames
[0] = ptname0
;
463 ptnames
[1] = ptname1
;
466 return -BLKID_ERR_PARAM
;
468 if (cache
->bic_flags
& BLKID_BIC_FL_PROBED
&&
469 time(NULL
) - cache
->bic_time
< BLKID_PROBE_INTERVAL
)
472 blkid_read_cache(cache
);
473 evms_probe_all(cache
, only_if_new
);
475 lvm_probe_all(cache
, only_if_new
);
477 ubi_probe_all(cache
, only_if_new
);
479 proc
= fopen(PROC_PARTITIONS
, "r" UL_CLOEXECSTR
);
481 return -BLKID_ERR_PROC
;
483 while (fgets(line
, sizeof(line
), proc
)) {
486 ptname
= ptnames
[which
];
488 if (sscanf(line
, " %d %d %llu %128[^\n ]",
489 &ma
, &mi
, &sz
, ptname
) != 4)
491 devs
[which
] = makedev(ma
, mi
);
493 DBG(DEVNAME
, ul_debug("read device name %s", ptname
));
495 /* Skip whole disk devs unless they have no partitions.
496 * If base name of device has changed, also
497 * check previous dev to see if it didn't have a partn.
498 * heuristic: partition name ends in a digit, & partition
499 * names contain whole device name as substring.
501 * Skip extended partitions.
502 * heuristic: size is 1
505 lens
[which
] = strlen(ptname
);
506 iswhole
[which
] = sysfs_devno_is_wholedisk(devs
[which
]);
508 /* probably partition, so check */
509 if (!iswhole
[which
]) {
510 DBG(DEVNAME
, ul_debug(" partition dev %s, devno 0x%04X",
511 ptname
, (unsigned int) devs
[which
]));
514 probe_one(cache
, ptname
, devs
[which
], 0,
516 lens
[which
] = 0; /* mark as checked */
520 * If last was a whole disk and we just found a partition
521 * on it, remove the whole-disk dev from the cache if
524 if (lens
[last
] && iswhole
[last
]
525 && !strncmp(ptnames
[last
], ptname
, lens
[last
])) {
527 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
530 /* find blkid dev for the whole-disk devno */
531 tmp
= list_entry(p
, struct blkid_struct_dev
,
533 if (tmp
->bid_devno
== devs
[last
]) {
534 DBG(DEVNAME
, ul_debug(" freeing %s",
537 cache
->bic_flags
|= BLKID_BIC_FL_CHANGED
;
541 lens
[last
] = 0; /* mark as checked */
544 * If last was not checked because it looked like a whole-disk
545 * dev, and the device's base name has changed,
546 * check last as well.
548 if (lens
[last
] && strncmp(ptnames
[last
], ptname
, lens
[last
])) {
549 DBG(DEVNAME
, ul_debug(" whole dev %s, devno 0x%04X",
550 ptnames
[last
], (unsigned int) devs
[last
]));
551 probe_one(cache
, ptnames
[last
], devs
[last
], 0,
554 lens
[last
] = 0; /* mark as checked */
558 /* Handle the last device if it wasn't partitioned */
560 probe_one(cache
, ptname
, devs
[which
], 0, only_if_new
, 0);
563 blkid_flush_cache(cache
);
567 /* Don't use it by default -- it's pretty slow (because cdroms, floppy, ...)
569 static int probe_all_removable(blkid_cache cache
)
576 return -BLKID_ERR_PARAM
;
578 dir
= opendir(_PATH_SYS_BLOCK
);
580 return -BLKID_ERR_PROC
;
582 pc
= ul_new_path(NULL
);
584 while((d
= readdir(dir
))) {
588 #ifdef _DIRENT_HAVE_D_TYPE
589 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_LNK
)
592 if (d
->d_name
[0] == '.' &&
593 ((d
->d_name
[1] == 0) ||
594 ((d
->d_name
[1] == '.') && (d
->d_name
[2] == 0))))
597 devno
= sysfs_devname_to_devno(d
->d_name
);
601 if (sysfs_blkdev_init_path(pc
, devno
, NULL
) == 0
602 && ul_path_read_s32(pc
, &removable
, "removable") != 0)
606 probe_one(cache
, d
->d_name
, devno
, 0, 0, 1);
617 * @cache: cache handler
619 * Probes all block devices.
621 * Returns: 0 on success, or number less than zero in case of error.
623 int blkid_probe_all(blkid_cache cache
)
627 DBG(PROBE
, ul_debug("Begin blkid_probe_all()"));
628 ret
= probe_all(cache
, 0);
630 cache
->bic_time
= time(NULL
);
631 cache
->bic_flags
|= BLKID_BIC_FL_PROBED
;
633 DBG(PROBE
, ul_debug("End blkid_probe_all() [rc=%d]", ret
));
638 * blkid_probe_all_new:
639 * @cache: cache handler
641 * Probes all new block devices.
643 * Returns: 0 on success, or number less than zero in case of error.
645 int blkid_probe_all_new(blkid_cache cache
)
649 DBG(PROBE
, ul_debug("Begin blkid_probe_all_new()"));
650 ret
= probe_all(cache
, 1);
651 DBG(PROBE
, ul_debug("End blkid_probe_all_new() [rc=%d]", ret
));
656 * blkid_probe_all_removable:
657 * @cache: cache handler
659 * The libblkid probing is based on devices from /proc/partitions by default.
660 * This file usually does not contain removable devices (e.g. CDROMs) and this kind
661 * of devices are invisible for libblkid.
663 * This function adds removable block devices to @cache (probing is based on
664 * information from the /sys directory). Don't forget that removable devices
665 * (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call
666 * this function by default.
668 * Note that devices which were detected by this function won't be written to
669 * blkid.tab cache file.
671 * Returns: 0 on success, or number less than zero in case of error.
673 int blkid_probe_all_removable(blkid_cache cache
)
677 DBG(PROBE
, ul_debug("Begin blkid_probe_all_removable()"));
678 ret
= probe_all_removable(cache
);
679 DBG(PROBE
, ul_debug("End blkid_probe_all_removable() [rc=%d]", ret
));
684 int main(int argc
, char **argv
)
686 blkid_cache cache
= NULL
;
689 blkid_init_debug(BLKID_DEBUG_ALL
);
691 fprintf(stderr
, "Usage: %s\n"
692 "Probe all devices and exit\n", argv
[0]);
695 if ((ret
= blkid_get_cache(&cache
, "/dev/null")) != 0) {
696 fprintf(stderr
, "%s: error creating cache (%d)\n",
700 if (blkid_probe_all(cache
) < 0)
701 printf("%s: error probing devices\n", argv
[0]);
703 if (blkid_probe_all_removable(cache
) < 0)
704 printf("%s: error probing removable devices\n", argv
[0]);
706 blkid_put_cache(cache
);