| 1 |
/* |
| 2 |
* devname.c - get a dev by its device inode name |
| 3 |
* |
| 4 |
* Copyright (C) Andries Brouwer |
| 5 |
* Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o |
| 6 |
* Copyright (C) 2001 Andreas Dilger |
| 7 |
* |
| 8 |
* %Begin-Header% |
| 9 |
* This file may be redistributed under the terms of the |
| 10 |
* GNU Lesser General Public License. |
| 11 |
* %End-Header% |
| 12 |
*/ |
| 13 |
|
| 14 |
#define _GNU_SOURCE 1 |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
#include <limits.h> |
| 19 |
#ifdef HAVE_UNISTD_H |
| 20 |
#include <unistd.h> |
| 21 |
#endif |
| 22 |
#include <stdlib.h> |
| 23 |
#include <ctype.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#ifdef HAVE_SYS_TYPES_H |
| 26 |
#include <sys/types.h> |
| 27 |
#endif |
| 28 |
#include <dirent.h> |
| 29 |
#ifdef HAVE_SYS_STAT_H |
| 30 |
#include <sys/stat.h> |
| 31 |
#endif |
| 32 |
#ifdef HAVE_ERRNO_H |
| 33 |
#include <errno.h> |
| 34 |
#endif |
| 35 |
#include <time.h> |
| 36 |
|
| 37 |
#include "blkidP.h" |
| 38 |
|
| 39 |
#include "canonicalize.h" /* $(top_srcdir)/include */ |
| 40 |
#include "pathnames.h" |
| 41 |
#include "sysfs.h" |
| 42 |
#include "fileutils.h" |
| 43 |
|
| 44 |
/* |
| 45 |
* Find a dev struct in the cache by device name, if available. |
| 46 |
* |
| 47 |
* If there is no entry with the specified device name, and the create |
| 48 |
* flag is set, then create an empty device entry. |
| 49 |
*/ |
| 50 |
blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags) |
| 51 |
{ |
| 52 |
blkid_dev dev = NULL, tmp; |
| 53 |
struct list_head *p, *pnext; |
| 54 |
char *cn = NULL; |
| 55 |
|
| 56 |
if (!cache || !devname) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
/* search by name */ |
| 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) |
| 63 |
continue; |
| 64 |
dev = tmp; |
| 65 |
break; |
| 66 |
} |
| 67 |
|
| 68 |
/* try canonicalize the name */ |
| 69 |
if (!dev && (cn = ul_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) |
| 75 |
continue; |
| 76 |
dev = tmp; |
| 77 |
|
| 78 |
/* update name returned by blkid_dev_devname() */ |
| 79 |
free(dev->bid_xname); |
| 80 |
dev->bid_xname = strdup(devname); |
| 81 |
break; |
| 82 |
} |
| 83 |
} else { |
| 84 |
free(cn); |
| 85 |
cn = NULL; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (!dev && (flags & BLKID_DEV_CREATE)) { |
| 90 |
if (access(devname, F_OK) < 0) |
| 91 |
goto done; |
| 92 |
dev = blkid_new_dev(); |
| 93 |
if (!dev) |
| 94 |
goto done; |
| 95 |
dev->bid_time = (uintmax_t)1 << (sizeof(time_t) * 8 - 1); |
| 96 |
if (cn) { |
| 97 |
dev->bid_name = cn; |
| 98 |
dev->bid_xname = strdup(devname); |
| 99 |
cn = NULL; /* see free() below */ |
| 100 |
} else |
| 101 |
dev->bid_name = strdup(devname); |
| 102 |
|
| 103 |
dev->bid_cache = cache; |
| 104 |
list_add_tail(&dev->bid_devs, &cache->bic_devs); |
| 105 |
cache->bic_flags |= BLKID_BIC_FL_CHANGED; |
| 106 |
} |
| 107 |
|
| 108 |
if (flags & BLKID_DEV_VERIFY) { |
| 109 |
dev = blkid_verify(cache, dev); |
| 110 |
if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 111 |
goto done; |
| 112 |
/* |
| 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 |
| 117 |
* it. |
| 118 |
*/ |
| 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) |
| 122 |
continue; |
| 123 |
if (!dev->bid_type || !dev2->bid_type || |
| 124 |
strcmp(dev->bid_type, dev2->bid_type) != 0) |
| 125 |
continue; |
| 126 |
if (dev->bid_label && dev2->bid_label && |
| 127 |
strcmp(dev->bid_label, dev2->bid_label) != 0) |
| 128 |
continue; |
| 129 |
if (dev->bid_uuid && dev2->bid_uuid && |
| 130 |
strcmp(dev->bid_uuid, dev2->bid_uuid) != 0) |
| 131 |
continue; |
| 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)) |
| 136 |
continue; |
| 137 |
dev2 = blkid_verify(cache, dev2); |
| 138 |
if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 139 |
blkid_free_dev(dev2); |
| 140 |
} |
| 141 |
} |
| 142 |
done: |
| 143 |
if (dev) |
| 144 |
DBG(DEVNAME, ul_debug("%s requested, found %s in cache", devname, dev->bid_name)); |
| 145 |
free(cn); |
| 146 |
return dev; |
| 147 |
} |
| 148 |
|
| 149 |
/* Directories where we will try to search for device names */ |
| 150 |
static const char *const dirlist[] = { "/dev", "/devfs", "/devices", NULL }; |
| 151 |
|
| 152 |
/* |
| 153 |
* Return 1 if the device is a device-mapper 'leaf' node |
| 154 |
* not holding any other devices in its hierarchy. |
| 155 |
*/ |
| 156 |
static int is_dm_leaf(const char *devname) |
| 157 |
{ |
| 158 |
DIR *dir; |
| 159 |
char path[NAME_MAX + 18 + 1]; |
| 160 |
int ret; |
| 161 |
|
| 162 |
snprintf(path, sizeof(path), "/sys/block/%s/holders", devname); |
| 163 |
if ((dir = opendir(path)) == NULL) |
| 164 |
return 0; |
| 165 |
|
| 166 |
ret = xreaddir(dir) == NULL ? 1 : 0; /* 'leaf' has no entries */ |
| 167 |
|
| 168 |
closedir(dir); |
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Probe a single block device to add to the device cache. |
| 174 |
*/ |
| 175 |
static void probe_one(blkid_cache cache, const char *ptname, |
| 176 |
dev_t devno, int pri, int only_if_new, int removable) |
| 177 |
{ |
| 178 |
blkid_dev dev = NULL; |
| 179 |
struct list_head *p, *pnext; |
| 180 |
const char *const*dir; |
| 181 |
char *devname = NULL; |
| 182 |
|
| 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, |
| 186 |
bid_devs); |
| 187 |
if (tmp->bid_devno == devno) { |
| 188 |
if (only_if_new && !access(tmp->bid_name, F_OK)) |
| 189 |
return; |
| 190 |
dev = blkid_verify(cache, tmp); |
| 191 |
if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 192 |
break; |
| 193 |
dev = NULL; |
| 194 |
} |
| 195 |
} |
| 196 |
if (dev && dev->bid_devno == devno) |
| 197 |
goto set_pri; |
| 198 |
|
| 199 |
/* Try to translate private device-mapper dm-<N> names |
| 200 |
* to standard /dev/mapper/<name>. |
| 201 |
*/ |
| 202 |
if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) { |
| 203 |
devname = ul_canonicalize_dm_name(ptname); |
| 204 |
if (!devname) |
| 205 |
blkid__scan_dir("/dev/mapper", devno, NULL, &devname); |
| 206 |
if (devname) |
| 207 |
goto get_dev; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 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. |
| 215 |
*/ |
| 216 |
for (dir = dirlist; *dir; dir++) { |
| 217 |
struct stat st; |
| 218 |
char device[256]; |
| 219 |
|
| 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) |
| 223 |
goto set_pri; |
| 224 |
|
| 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); |
| 230 |
goto get_dev; |
| 231 |
} |
| 232 |
} |
| 233 |
/* Do a short-cut scan of /dev/mapper first */ |
| 234 |
if (!devname) |
| 235 |
blkid__scan_dir("/dev/mapper", devno, NULL, &devname); |
| 236 |
if (!devname) { |
| 237 |
devname = blkid_devno_to_devname(devno); |
| 238 |
if (!devname) |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
get_dev: |
| 243 |
dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL); |
| 244 |
free(devname); |
| 245 |
|
| 246 |
set_pri: |
| 247 |
if (dev) { |
| 248 |
if (pri) |
| 249 |
dev->bid_pri = pri; |
| 250 |
else if (!strncmp(dev->bid_name, "/dev/mapper/", 12)) { |
| 251 |
dev->bid_pri = BLKID_PRI_DM; |
| 252 |
if (is_dm_leaf(ptname)) |
| 253 |
dev->bid_pri += 5; |
| 254 |
} else if (!strncmp(ptname, "md", 2)) |
| 255 |
dev->bid_pri = BLKID_PRI_MD; |
| 256 |
if (removable) |
| 257 |
dev->bid_flags |= BLKID_BID_FL_REMOVABLE; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
#define PROC_PARTITIONS "/proc/partitions" |
| 262 |
#define VG_DIR "/proc/lvm/VGs" |
| 263 |
|
| 264 |
/* |
| 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 |
| 268 |
* safe thing to do?) |
| 269 |
*/ |
| 270 |
#ifdef VG_DIR |
| 271 |
static dev_t lvm_get_devno(const char *lvm_device) |
| 272 |
{ |
| 273 |
FILE *lvf; |
| 274 |
char buf[1024]; |
| 275 |
int ma, mi; |
| 276 |
dev_t ret = 0; |
| 277 |
|
| 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)); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
while (fgets(buf, sizeof(buf), lvf)) { |
| 285 |
if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) { |
| 286 |
ret = makedev(ma, mi); |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
fclose(lvf); |
| 291 |
|
| 292 |
return ret; |
| 293 |
} |
| 294 |
|
| 295 |
static void lvm_probe_all(blkid_cache cache, int only_if_new) |
| 296 |
{ |
| 297 |
DIR *vg_list; |
| 298 |
struct dirent *vg_iter; |
| 299 |
int vg_len = strlen(VG_DIR); |
| 300 |
dev_t dev; |
| 301 |
|
| 302 |
if ((vg_list = opendir(VG_DIR)) == NULL) |
| 303 |
return; |
| 304 |
|
| 305 |
DBG(DEVNAME, ul_debug("probing LVM devices under %s", VG_DIR)); |
| 306 |
|
| 307 |
while ((vg_iter = readdir(vg_list)) != NULL) { |
| 308 |
DIR *lv_list; |
| 309 |
char *vdirname; |
| 310 |
char *vg_name; |
| 311 |
struct dirent *lv_iter; |
| 312 |
size_t len; |
| 313 |
|
| 314 |
vg_name = vg_iter->d_name; |
| 315 |
if (!strcmp(vg_name, ".") || !strcmp(vg_name, "..")) |
| 316 |
continue; |
| 317 |
len = vg_len + strlen(vg_name) + 8; |
| 318 |
vdirname = malloc(len); |
| 319 |
if (!vdirname) |
| 320 |
goto exit; |
| 321 |
snprintf(vdirname, len, "%s/%s/LVs", VG_DIR, vg_name); |
| 322 |
|
| 323 |
lv_list = opendir(vdirname); |
| 324 |
free(vdirname); |
| 325 |
if (lv_list == NULL) |
| 326 |
continue; |
| 327 |
|
| 328 |
while ((lv_iter = readdir(lv_list)) != NULL) { |
| 329 |
char *lv_name, *lvm_device; |
| 330 |
|
| 331 |
lv_name = lv_iter->d_name; |
| 332 |
if (!strcmp(lv_name, ".") || !strcmp(lv_name, "..")) |
| 333 |
continue; |
| 334 |
|
| 335 |
len = vg_len + strlen(vg_name) + strlen(lv_name) + 8; |
| 336 |
lvm_device = malloc(len); |
| 337 |
if (!lvm_device) { |
| 338 |
closedir(lv_list); |
| 339 |
goto exit; |
| 340 |
} |
| 341 |
snprintf(lvm_device, len, "%s/%s/LVs/%s", VG_DIR, vg_name, |
| 342 |
lv_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", |
| 346 |
lvm_device, |
| 347 |
(unsigned int) dev)); |
| 348 |
probe_one(cache, lvm_device, dev, BLKID_PRI_LVM, |
| 349 |
only_if_new, 0); |
| 350 |
free(lvm_device); |
| 351 |
} |
| 352 |
closedir(lv_list); |
| 353 |
} |
| 354 |
exit: |
| 355 |
closedir(vg_list); |
| 356 |
} |
| 357 |
#endif |
| 358 |
|
| 359 |
static void |
| 360 |
ubi_probe_all(blkid_cache cache, int only_if_new) |
| 361 |
{ |
| 362 |
const char *const*dirname; |
| 363 |
|
| 364 |
for (dirname = dirlist; *dirname; dirname++) { |
| 365 |
DIR *dir; |
| 366 |
struct dirent *iter; |
| 367 |
|
| 368 |
DBG(DEVNAME, ul_debug("probing UBI volumes under %s", |
| 369 |
*dirname)); |
| 370 |
|
| 371 |
dir = opendir(*dirname); |
| 372 |
if (dir == NULL) |
| 373 |
continue ; |
| 374 |
|
| 375 |
while ((iter = readdir(dir)) != NULL) { |
| 376 |
char *name; |
| 377 |
struct stat st; |
| 378 |
dev_t dev; |
| 379 |
|
| 380 |
name = iter->d_name; |
| 381 |
#ifdef _DIRENT_HAVE_D_TYPE |
| 382 |
if (iter->d_type != DT_UNKNOWN && |
| 383 |
iter->d_type != DT_CHR && iter->d_type != DT_LNK) |
| 384 |
continue; |
| 385 |
#endif |
| 386 |
if (!strcmp(name, ".") || !strcmp(name, "..") || |
| 387 |
!strstr(name, "ubi")) |
| 388 |
continue; |
| 389 |
if (!strcmp(name, "ubi_ctrl")) |
| 390 |
continue; |
| 391 |
if (fstatat(dirfd(dir), name, &st, 0)) |
| 392 |
continue; |
| 393 |
|
| 394 |
dev = st.st_rdev; |
| 395 |
|
| 396 |
if (!S_ISCHR(st.st_mode) || !minor(dev)) |
| 397 |
continue; |
| 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); |
| 401 |
} |
| 402 |
closedir(dir); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
/* |
| 407 |
* This function uses /sys to read all block devices in way compatible with |
| 408 |
* /proc/partitions (like the original libblkid implementation) |
| 409 |
*/ |
| 410 |
static int |
| 411 |
sysfs_probe_all(blkid_cache cache, int only_if_new, int only_removable) |
| 412 |
{ |
| 413 |
DIR *sysfs; |
| 414 |
struct dirent *dev; |
| 415 |
|
| 416 |
sysfs = opendir(_PATH_SYS_BLOCK); |
| 417 |
if (!sysfs) |
| 418 |
return -BLKID_ERR_SYSFS; |
| 419 |
|
| 420 |
DBG(DEVNAME, ul_debug(" probe /sys/block")); |
| 421 |
|
| 422 |
/* scan /sys/block */ |
| 423 |
while ((dev = xreaddir(sysfs))) { |
| 424 |
DIR *dir = NULL; |
| 425 |
dev_t devno; |
| 426 |
size_t nparts = 0; |
| 427 |
unsigned int maxparts = 0, removable = 0; |
| 428 |
struct dirent *part; |
| 429 |
struct path_cxt *pc = NULL; |
| 430 |
uint64_t size = 0; |
| 431 |
|
| 432 |
DBG(DEVNAME, ul_debug("checking %s", dev->d_name)); |
| 433 |
|
| 434 |
devno = sysfs_devname_to_devno(dev->d_name); |
| 435 |
if (!devno) |
| 436 |
goto next; |
| 437 |
pc = ul_new_sysfs_path(devno, NULL, NULL); |
| 438 |
if (!pc) |
| 439 |
goto next; |
| 440 |
|
| 441 |
if (ul_path_read_u64(pc, &size, "size") != 0) |
| 442 |
size = 0; |
| 443 |
if (ul_path_read_u32(pc, &removable, "removable") != 0) |
| 444 |
removable = 0; |
| 445 |
|
| 446 |
/* ignore empty devices */ |
| 447 |
if (!size) |
| 448 |
goto next; |
| 449 |
|
| 450 |
/* accept removable if only removable requested */ |
| 451 |
if (only_removable) { |
| 452 |
if (!removable) |
| 453 |
goto next; |
| 454 |
|
| 455 |
/* emulate /proc/partitions |
| 456 |
* -- ignore empty devices and non-partitionable removable devices */ |
| 457 |
} else { |
| 458 |
if (ul_path_read_u32(pc, &maxparts, "ext_range") != 0) |
| 459 |
maxparts = 0; |
| 460 |
if (!maxparts && removable) |
| 461 |
goto next; |
| 462 |
} |
| 463 |
|
| 464 |
DBG(DEVNAME, ul_debug("read device name %s", dev->d_name)); |
| 465 |
|
| 466 |
dir = ul_path_opendir(pc, NULL); |
| 467 |
if (!dir) |
| 468 |
goto next; |
| 469 |
|
| 470 |
/* read /sys/block/<name>/ do get partitions */ |
| 471 |
while ((part = xreaddir(dir))) { |
| 472 |
dev_t partno; |
| 473 |
|
| 474 |
if (!sysfs_blkdev_is_partition_dirent(dir, part, dev->d_name)) |
| 475 |
continue; |
| 476 |
|
| 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 |
| 480 |
&& (size >> 1) == 1) |
| 481 |
continue; |
| 482 |
partno = __sysfs_devname_to_devno(NULL, part->d_name, dev->d_name); |
| 483 |
if (!partno) |
| 484 |
continue; |
| 485 |
|
| 486 |
DBG(DEVNAME, ul_debug(" Probe partition dev %s, devno 0x%04X", |
| 487 |
part->d_name, (unsigned int) partno)); |
| 488 |
nparts++; |
| 489 |
probe_one(cache, part->d_name, partno, 0, only_if_new, 0); |
| 490 |
} |
| 491 |
|
| 492 |
if (!nparts) { |
| 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); |
| 497 |
} else { |
| 498 |
/* remove partitioned whole-disk from cache */ |
| 499 |
struct list_head *p, *pnext; |
| 500 |
|
| 501 |
list_for_each_safe(p, pnext, &cache->bic_devs) { |
| 502 |
blkid_dev tmp = list_entry(p, struct blkid_struct_dev, |
| 503 |
bid_devs); |
| 504 |
if (tmp->bid_devno == devno) { |
| 505 |
DBG(DEVNAME, ul_debug(" freeing %s", tmp->bid_name)); |
| 506 |
blkid_free_dev(tmp); |
| 507 |
cache->bic_flags |= BLKID_BIC_FL_CHANGED; |
| 508 |
break; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
next: |
| 513 |
if (dir) |
| 514 |
closedir(dir); |
| 515 |
if (pc) |
| 516 |
ul_unref_path(pc); |
| 517 |
} |
| 518 |
|
| 519 |
closedir(sysfs); |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
/* |
| 524 |
* Read the device data for all available block devices in the system. |
| 525 |
*/ |
| 526 |
static int probe_all(blkid_cache cache, int only_if_new, int update_interval) |
| 527 |
{ |
| 528 |
int rc; |
| 529 |
|
| 530 |
if (!cache) |
| 531 |
return -BLKID_ERR_PARAM; |
| 532 |
|
| 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)); |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
blkid_read_cache(cache); |
| 540 |
#ifdef VG_DIR |
| 541 |
lvm_probe_all(cache, only_if_new); |
| 542 |
#endif |
| 543 |
ubi_probe_all(cache, only_if_new); |
| 544 |
|
| 545 |
rc = sysfs_probe_all(cache, only_if_new, 0); |
| 546 |
|
| 547 |
/* Don't mark the change as "probed" if /sys not available */ |
| 548 |
if (update_interval && rc == 0) { |
| 549 |
cache->bic_time = time(NULL); |
| 550 |
cache->bic_flags |= BLKID_BIC_FL_PROBED; |
| 551 |
} |
| 552 |
|
| 553 |
blkid_flush_cache(cache); |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* blkid_probe_all: |
| 559 |
* @cache: cache handler |
| 560 |
* |
| 561 |
* Probes all block devices. |
| 562 |
* |
| 563 |
* Returns: 0 on success, or number less than zero in case of error. |
| 564 |
*/ |
| 565 |
int blkid_probe_all(blkid_cache cache) |
| 566 |
{ |
| 567 |
int ret; |
| 568 |
|
| 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)); |
| 572 |
return ret; |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* blkid_probe_all_new: |
| 577 |
* @cache: cache handler |
| 578 |
* |
| 579 |
* Probes all new block devices. |
| 580 |
* |
| 581 |
* Returns: 0 on success, or number less than zero in case of error. |
| 582 |
*/ |
| 583 |
int blkid_probe_all_new(blkid_cache cache) |
| 584 |
{ |
| 585 |
int ret; |
| 586 |
|
| 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)); |
| 590 |
return ret; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* blkid_probe_all_removable: |
| 595 |
* @cache: cache handler |
| 596 |
* |
| 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. |
| 600 |
* |
| 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. |
| 605 |
* |
| 606 |
* Note that devices which were detected by this function won't be written to |
| 607 |
* blkid.tab cache file. |
| 608 |
* |
| 609 |
* Returns: 0 on success, or number less than zero in case of error. |
| 610 |
*/ |
| 611 |
int blkid_probe_all_removable(blkid_cache cache) |
| 612 |
{ |
| 613 |
int ret; |
| 614 |
|
| 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)); |
| 618 |
return ret; |
| 619 |
} |
| 620 |
|
| 621 |
#ifdef TEST_PROGRAM |
| 622 |
int main(int argc, char **argv) |
| 623 |
{ |
| 624 |
blkid_cache cache = NULL; |
| 625 |
int ret; |
| 626 |
|
| 627 |
blkid_init_debug(BLKID_DEBUG_ALL); |
| 628 |
if (argc != 1) { |
| 629 |
fprintf(stderr, "Usage: %s\n" |
| 630 |
"Probe all devices and exit\n", argv[0]); |
| 631 |
exit(1); |
| 632 |
} |
| 633 |
if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { |
| 634 |
fprintf(stderr, "%s: error creating cache (%d)\n", |
| 635 |
argv[0], ret); |
| 636 |
exit(1); |
| 637 |
} |
| 638 |
if (blkid_probe_all(cache) < 0) |
| 639 |
printf("%s: error probing devices\n", argv[0]); |
| 640 |
|
| 641 |
if (blkid_probe_all_removable(cache) < 0) |
| 642 |
printf("%s: error probing removable devices\n", argv[0]); |
| 643 |
|
| 644 |
blkid_put_cache(cache); |
| 645 |
return (0); |
| 646 |
} |
| 647 |
#endif |