]>
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"
42 #include "fileutils.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
;
56 if (!cache
|| !devname
)
60 list_for_each(p
, &cache
->bic_devs
) {
61 tmp
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
62 if (strcmp(tmp
->bid_name
, devname
) != 0)
68 /* try canonicalize the name */
69 if (!dev
&& (cn
= canonicalize_path(devname
))) {
70 if (strcmp(cn
, devname
) != 0) {
71 DBG(DEVNAME
, ul_debug("search canonical %s", cn
));
72 list_for_each(p
, &cache
->bic_devs
) {
73 tmp
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
74 if (strcmp(tmp
->bid_name
, cn
) != 0)
78 /* update name returned by blkid_dev_devname() */
80 dev
->bid_xname
= strdup(devname
);
89 if (!dev
&& (flags
& BLKID_DEV_CREATE
)) {
90 if (access(devname
, F_OK
) < 0)
92 dev
= blkid_new_dev();
95 dev
->bid_time
= (uintmax_t)1 << (sizeof(time_t) * 8 - 1);
98 dev
->bid_xname
= strdup(devname
);
99 cn
= NULL
; /* see free() below */
101 dev
->bid_name
= strdup(devname
);
103 dev
->bid_cache
= cache
;
104 list_add_tail(&dev
->bid_devs
, &cache
->bic_devs
);
105 cache
->bic_flags
|= BLKID_BIC_FL_CHANGED
;
108 if (flags
& BLKID_DEV_VERIFY
) {
109 dev
= blkid_verify(cache
, dev
);
110 if (!dev
|| !(dev
->bid_flags
& BLKID_BID_FL_VERIFIED
))
113 * If the device is verified, then search the blkid
114 * cache for any entries that match on the type, uuid,
115 * and label, and verify them; if a cache entry can
116 * not be verified, then it's stale and so we remove
119 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
120 blkid_dev dev2
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
121 if (dev2
->bid_flags
& BLKID_BID_FL_VERIFIED
)
123 if (!dev
->bid_type
|| !dev2
->bid_type
||
124 strcmp(dev
->bid_type
, dev2
->bid_type
) != 0)
126 if (dev
->bid_label
&& dev2
->bid_label
&&
127 strcmp(dev
->bid_label
, dev2
->bid_label
) != 0)
129 if (dev
->bid_uuid
&& dev2
->bid_uuid
&&
130 strcmp(dev
->bid_uuid
, dev2
->bid_uuid
) != 0)
132 if ((dev
->bid_label
&& !dev2
->bid_label
) ||
133 (!dev
->bid_label
&& dev2
->bid_label
) ||
134 (dev
->bid_uuid
&& !dev2
->bid_uuid
) ||
135 (!dev
->bid_uuid
&& dev2
->bid_uuid
))
137 dev2
= blkid_verify(cache
, dev2
);
138 if (dev2
&& !(dev2
->bid_flags
& BLKID_BID_FL_VERIFIED
))
139 blkid_free_dev(dev2
);
144 DBG(DEVNAME
, ul_debug("%s requested, found %s in cache", devname
, dev
->bid_name
));
149 /* Directories where we will try to search for device names */
150 static const char *dirlist
[] = { "/dev", "/devfs", "/devices", NULL
};
153 * Return 1 if the device is a device-mapper 'leaf' node
154 * not holding any other devices in its hierarchy.
156 static int is_dm_leaf(const char *devname
)
159 char path
[NAME_MAX
+ 18 + 1];
162 snprintf(path
, sizeof(path
), "/sys/block/%s/holders", devname
);
163 if ((dir
= opendir(path
)) == NULL
)
166 ret
= xreaddir(dir
) == NULL
? 1 : 0; /* 'leaf' has no entries */
173 * Probe a single block device to add to the device cache.
175 static void probe_one(blkid_cache cache
, const char *ptname
,
176 dev_t devno
, int pri
, int only_if_new
, int removable
)
178 blkid_dev dev
= NULL
;
179 struct list_head
*p
, *pnext
;
181 char *devname
= NULL
;
183 /* See if we already have this device number in the cache. */
184 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
185 blkid_dev tmp
= list_entry(p
, struct blkid_struct_dev
,
187 if (tmp
->bid_devno
== devno
) {
188 if (only_if_new
&& !access(tmp
->bid_name
, F_OK
))
190 dev
= blkid_verify(cache
, tmp
);
191 if (dev
&& (dev
->bid_flags
& BLKID_BID_FL_VERIFIED
))
196 if (dev
&& dev
->bid_devno
== devno
)
199 /* Try to translate private device-mapper dm-<N> names
200 * to standard /dev/mapper/<name>.
202 if (!strncmp(ptname
, "dm-", 3) && isdigit(ptname
[3])) {
203 devname
= canonicalize_dm_name(ptname
);
205 blkid__scan_dir("/dev/mapper", devno
, NULL
, &devname
);
211 * Take a quick look at /dev/ptname for the device number. We check
212 * all of the likely device directories. If we don't find it, or if
213 * the stat information doesn't check out, use blkid_devno_to_devname()
214 * to find it via an exhaustive search for the device major/minor.
216 for (dir
= dirlist
; *dir
; dir
++) {
220 snprintf(device
, sizeof(device
), "%s/%s", *dir
, ptname
);
221 if ((dev
= blkid_get_dev(cache
, device
, BLKID_DEV_FIND
)) &&
222 dev
->bid_devno
== devno
)
225 if (stat(device
, &st
) == 0 &&
226 (S_ISBLK(st
.st_mode
) ||
227 (S_ISCHR(st
.st_mode
) && !strncmp(ptname
, "ubi", 3))) &&
228 st
.st_rdev
== devno
) {
229 devname
= strdup(device
);
233 /* Do a short-cut scan of /dev/mapper first */
235 blkid__scan_dir("/dev/mapper", devno
, NULL
, &devname
);
237 devname
= blkid_devno_to_devname(devno
);
243 dev
= blkid_get_dev(cache
, devname
, BLKID_DEV_NORMAL
);
250 else if (!strncmp(dev
->bid_name
, "/dev/mapper/", 12)) {
251 dev
->bid_pri
= BLKID_PRI_DM
;
252 if (is_dm_leaf(ptname
))
254 } else if (!strncmp(ptname
, "md", 2))
255 dev
->bid_pri
= BLKID_PRI_MD
;
257 dev
->bid_flags
|= BLKID_BID_FL_REMOVABLE
;
261 #define PROC_PARTITIONS "/proc/partitions"
262 #define VG_DIR "/proc/lvm/VGs"
265 * This function initializes the UUID cache with devices from the LVM
266 * proc hierarchy. We currently depend on the names of the LVM
267 * hierarchy giving us the device structure in /dev. (XXX is this a
271 static dev_t
lvm_get_devno(const char *lvm_device
)
278 DBG(DEVNAME
, ul_debug("opening %s", lvm_device
));
279 if ((lvf
= fopen(lvm_device
, "r" UL_CLOEXECSTR
)) == NULL
) {
280 DBG(DEVNAME
, ul_debug("%s: (%d) %m", lvm_device
, errno
));
284 while (fgets(buf
, sizeof(buf
), lvf
)) {
285 if (sscanf(buf
, "device: %d:%d", &ma
, &mi
) == 2) {
286 ret
= makedev(ma
, mi
);
295 static void lvm_probe_all(blkid_cache cache
, int only_if_new
)
298 struct dirent
*vg_iter
;
299 int vg_len
= strlen(VG_DIR
);
302 if ((vg_list
= opendir(VG_DIR
)) == NULL
)
305 DBG(DEVNAME
, ul_debug("probing LVM devices under %s", VG_DIR
));
307 while ((vg_iter
= readdir(vg_list
)) != NULL
) {
311 struct dirent
*lv_iter
;
314 vg_name
= vg_iter
->d_name
;
315 if (!strcmp(vg_name
, ".") || !strcmp(vg_name
, ".."))
317 len
= vg_len
+ strlen(vg_name
) + 8;
318 vdirname
= malloc(len
);
321 snprintf(vdirname
, len
, "%s/%s/LVs", VG_DIR
, vg_name
);
323 lv_list
= opendir(vdirname
);
328 while ((lv_iter
= readdir(lv_list
)) != NULL
) {
329 char *lv_name
, *lvm_device
;
331 lv_name
= lv_iter
->d_name
;
332 if (!strcmp(lv_name
, ".") || !strcmp(lv_name
, ".."))
335 len
= vg_len
+ strlen(vg_name
) + strlen(lv_name
) + 8;
336 lvm_device
= malloc(len
);
341 snprintf(lvm_device
, len
, "%s/%s/LVs/%s", VG_DIR
, vg_name
,
343 dev
= lvm_get_devno(lvm_device
);
344 snprintf(lvm_device
, len
, "%s/%s", vg_name
, lv_name
);
345 DBG(DEVNAME
, ul_debug("Probe LVM dev %s: devno 0x%04X",
347 (unsigned int) dev
));
348 probe_one(cache
, lvm_device
, dev
, BLKID_PRI_LVM
,
360 ubi_probe_all(blkid_cache cache
, int only_if_new
)
362 const char **dirname
;
364 for (dirname
= dirlist
; *dirname
; dirname
++) {
368 DBG(DEVNAME
, ul_debug("probing UBI volumes under %s",
371 dir
= opendir(*dirname
);
375 while ((iter
= readdir(dir
)) != NULL
) {
381 #ifdef _DIRENT_HAVE_D_TYPE
382 if (iter
->d_type
!= DT_UNKNOWN
&&
383 iter
->d_type
!= DT_CHR
&& iter
->d_type
!= DT_LNK
)
386 if (!strcmp(name
, ".") || !strcmp(name
, "..") ||
387 !strstr(name
, "ubi"))
389 if (!strcmp(name
, "ubi_ctrl"))
391 if (fstatat(dirfd(dir
), name
, &st
, 0))
396 if (!S_ISCHR(st
.st_mode
) || !minor(dev
))
398 DBG(DEVNAME
, ul_debug("Probe UBI vol %s/%s: devno 0x%04X",
399 *dirname
, name
, (int) dev
));
400 probe_one(cache
, name
, dev
, BLKID_PRI_UBI
, only_if_new
, 0);
407 * This function uses /sys to read all block devices in way compatible with
408 * /proc/partitions (like the original libblkid implementation)
411 sysfs_probe_all(blkid_cache cache
, int only_if_new
, int only_removable
)
416 sysfs
= opendir(_PATH_SYS_BLOCK
);
418 return -BLKID_ERR_SYSFS
;
420 DBG(DEVNAME
, ul_debug(" probe /sys/block"));
422 /* scan /sys/block */
423 while ((dev
= xreaddir(sysfs
))) {
427 unsigned int maxparts
= 0, removable
= 0;
429 struct path_cxt
*pc
= NULL
;
432 DBG(DEVNAME
, ul_debug("checking %s", dev
->d_name
));
434 devno
= sysfs_devname_to_devno(dev
->d_name
);
437 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
441 if (ul_path_read_u64(pc
, &size
, "size") != 0)
443 if (ul_path_read_u32(pc
, &removable
, "removable") != 0)
446 /* ignore empty devices */
450 /* accept removable if only removable requested */
451 if (only_removable
) {
455 /* emulate /proc/partitions
456 * -- ignore empty devices and non-partitionable removable devices */
458 if (ul_path_read_u32(pc
, &maxparts
, "ext_range") != 0)
460 if (!maxparts
&& removable
)
464 DBG(DEVNAME
, ul_debug("read device name %s", dev
->d_name
));
466 dir
= ul_path_opendir(pc
, NULL
);
470 /* read /sys/block/<name>/ do get partitions */
471 while ((part
= xreaddir(dir
))) {
474 if (!sysfs_blkdev_is_partition_dirent(dir
, part
, dev
->d_name
))
477 /* ignore extended partitions
478 * -- recount size to blocks like /proc/partitions */
479 if (ul_path_readf_u64(pc
, &size
, "%s/size", part
->d_name
) == 0
482 partno
= __sysfs_devname_to_devno(NULL
, part
->d_name
, dev
->d_name
);
486 DBG(DEVNAME
, ul_debug(" Probe partition dev %s, devno 0x%04X",
487 part
->d_name
, (unsigned int) partno
));
489 probe_one(cache
, part
->d_name
, partno
, 0, only_if_new
, 0);
493 /* add non-partitioned whole disk to cache */
494 DBG(DEVNAME
, ul_debug(" Probe whole dev %s, devno 0x%04X",
495 dev
->d_name
, (unsigned int) devno
));
496 probe_one(cache
, dev
->d_name
, devno
, 0, only_if_new
, 0);
498 /* remove partitioned whole-disk from cache */
499 struct list_head
*p
, *pnext
;
501 list_for_each_safe(p
, pnext
, &cache
->bic_devs
) {
502 blkid_dev tmp
= list_entry(p
, struct blkid_struct_dev
,
504 if (tmp
->bid_devno
== devno
) {
505 DBG(DEVNAME
, ul_debug(" freeing %s", tmp
->bid_name
));
507 cache
->bic_flags
|= BLKID_BIC_FL_CHANGED
;
524 * Read the device data for all available block devices in the system.
526 static int probe_all(blkid_cache cache
, int only_if_new
, int update_interval
)
531 return -BLKID_ERR_PARAM
;
533 if (cache
->bic_flags
& BLKID_BIC_FL_PROBED
&&
534 time(NULL
) - cache
->bic_time
< BLKID_PROBE_INTERVAL
) {
535 DBG(PROBE
, ul_debug("don't re-probe [delay < %d]", BLKID_PROBE_INTERVAL
));
539 blkid_read_cache(cache
);
541 lvm_probe_all(cache
, only_if_new
);
543 ubi_probe_all(cache
, only_if_new
);
545 rc
= sysfs_probe_all(cache
, only_if_new
, 0);
547 /* Don't mark the change as "probed" if /sys not avalable */
548 if (update_interval
&& rc
== 0) {
549 cache
->bic_time
= time(NULL
);
550 cache
->bic_flags
|= BLKID_BIC_FL_PROBED
;
553 blkid_flush_cache(cache
);
559 * @cache: cache handler
561 * Probes all block devices.
563 * Returns: 0 on success, or number less than zero in case of error.
565 int blkid_probe_all(blkid_cache cache
)
569 DBG(PROBE
, ul_debug("Begin blkid_probe_all()"));
570 ret
= probe_all(cache
, 0, 1);
571 DBG(PROBE
, ul_debug("End blkid_probe_all() [rc=%d]", ret
));
576 * blkid_probe_all_new:
577 * @cache: cache handler
579 * Probes all new block devices.
581 * Returns: 0 on success, or number less than zero in case of error.
583 int blkid_probe_all_new(blkid_cache cache
)
587 DBG(PROBE
, ul_debug("Begin blkid_probe_all_new()"));
588 ret
= probe_all(cache
, 1, 0);
589 DBG(PROBE
, ul_debug("End blkid_probe_all_new() [rc=%d]", ret
));
594 * blkid_probe_all_removable:
595 * @cache: cache handler
597 * The libblkid probing is based on devices from /proc/partitions by default.
598 * This file usually does not contain removable devices (e.g. CDROMs) and this kind
599 * of devices are invisible for libblkid.
601 * This function adds removable block devices to @cache (probing is based on
602 * information from the /sys directory). Don't forget that removable devices
603 * (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call
604 * this function by default.
606 * Note that devices which were detected by this function won't be written to
607 * blkid.tab cache file.
609 * Returns: 0 on success, or number less than zero in case of error.
611 int blkid_probe_all_removable(blkid_cache cache
)
615 DBG(PROBE
, ul_debug("Begin blkid_probe_all_removable()"));
616 ret
= sysfs_probe_all(cache
, 0, 1);
617 DBG(PROBE
, ul_debug("End blkid_probe_all_removable() [rc=%d]", ret
));
622 int main(int argc
, char **argv
)
624 blkid_cache cache
= NULL
;
627 blkid_init_debug(BLKID_DEBUG_ALL
);
629 fprintf(stderr
, "Usage: %s\n"
630 "Probe all devices and exit\n", argv
[0]);
633 if ((ret
= blkid_get_cache(&cache
, "/dev/null")) != 0) {
634 fprintf(stderr
, "%s: error creating cache (%d)\n",
638 if (blkid_probe_all(cache
) < 0)
639 printf("%s: error probing devices\n", argv
[0]);
641 if (blkid_probe_all_removable(cache
) < 0)
642 printf("%s: error probing removable devices\n", argv
[0]);
644 blkid_put_cache(cache
);