2
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5
* This file is part of LVM2.
7
* This copyrighted material is made available to anyone wishing to use,
8
* modify, copy, or redistribute it subject to the terms and conditions
9
* of the GNU Lesser General Public License v.2.1.
11
* You should have received a copy of the GNU Lesser General Public License
12
* along with this program; if not, write to the Free Software Foundation,
13
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
#include "dev-cache.h"
18
#include "lvm-types.h"
21
#include "filter-persistent.h"
22
#include "toolcontext.h"
25
#include <sys/param.h>
29
struct btree_iter *current;
30
struct dev_filter *filter;
40
struct dm_hash_table *names;
41
struct btree *devices;
42
struct dm_regex *preferred_names_matcher;
51
#define _alloc(x) dm_pool_zalloc(_cache.mem, (x))
52
#define _free(x) dm_pool_free(_cache.mem, (x))
53
#define _strdup(x) dm_pool_strdup(_cache.mem, (x))
55
static int _insert(const char *path, int rec, int check_with_udev_db);
57
struct device *dev_create_file(const char *filename, struct device *dev,
58
struct str_list *alias, int use_malloc)
64
if (!(dev = dm_malloc(sizeof(*dev)))) {
65
log_error("struct device allocation failed");
68
if (!(alias = dm_malloc(sizeof(*alias)))) {
69
log_error("struct str_list allocation failed");
73
if (!(alias->str = dm_strdup(filename))) {
74
log_error("filename strdup failed");
79
dev->flags = DEV_ALLOCED;
81
if (!(dev = _alloc(sizeof(*dev)))) {
82
log_error("struct device allocation failed");
85
if (!(alias = _alloc(sizeof(*alias)))) {
86
log_error("struct str_list allocation failed");
90
if (!(alias->str = _strdup(filename))) {
91
log_error("filename strdup failed");
95
} else if (!(alias->str = dm_strdup(filename))) {
96
log_error("filename strdup failed");
100
dev->flags |= DEV_REGULAR;
101
dm_list_init(&dev->aliases);
102
dm_list_add(&dev->aliases, &alias->list);
103
dev->end = UINT64_C(0);
107
dev->error_count = 0;
108
dev->max_error_count = NO_DEV_ERROR_COUNT_LIMIT;
109
dev->block_size = -1;
110
dev->read_ahead = -1;
111
memset(dev->pvid, 0, sizeof(dev->pvid));
112
dm_list_init(&dev->open_list);
117
static struct device *_dev_create(dev_t d)
121
if (!(dev = _alloc(sizeof(*dev)))) {
122
log_error("struct device allocation failed");
126
dm_list_init(&dev->aliases);
130
dev->max_error_count = dev_disable_after_error_count();
131
dev->block_size = -1;
132
dev->read_ahead = -1;
133
dev->end = UINT64_C(0);
134
memset(dev->pvid, 0, sizeof(dev->pvid));
135
dm_list_init(&dev->open_list);
140
void dev_set_preferred_name(struct str_list *sl, struct device *dev)
143
* Don't interfere with ordering specified in config file.
145
if (_cache.preferred_names_matcher)
148
log_debug("%s: New preferred name", sl->str);
149
dm_list_del(&sl->list);
150
dm_list_add_h(&dev->aliases, &sl->list);
154
* Check whether path0 or path1 contains the subpath. The path that
155
* *does not* contain the subpath wins (return 0 or 1). If both paths
156
* contain the subpath, return -1. If none of them contains the subpath,
159
static int _builtin_preference(const char *path0, const char *path1,
160
size_t skip_prefix_count, const char *subpath)
165
subpath_len = strlen(subpath);
167
r0 = !strncmp(path0 + skip_prefix_count, subpath, subpath_len);
168
r1 = !strncmp(path1 + skip_prefix_count, subpath, subpath_len);
171
/* path0 does not have the subpath - it wins */
174
/* path1 does not have the subpath - it wins */
177
/* both of them have the subpath */
180
/* no path has the subpath */
184
static int _apply_builtin_path_preference_rules(const char *path0, const char *path1)
189
devdir_len = strlen(_cache.dev_dir);
191
if (!strncmp(path0, _cache.dev_dir, devdir_len) &&
192
!strncmp(path1, _cache.dev_dir, devdir_len)) {
194
* We're trying to achieve the ordering:
195
* /dev/block/ < /dev/dm-* < /dev/disk/ < /dev/mapper/ < anything else
198
/* Prefer any other path over /dev/block/ path. */
199
if ((r = _builtin_preference(path0, path1, devdir_len, "block/")) >= -1)
202
/* Prefer any other path over /dev/dm-* path. */
203
if ((r = _builtin_preference(path0, path1, devdir_len, "dm-")) >= -1)
206
/* Prefer any other path over /dev/disk/ path. */
207
if ((r = _builtin_preference(path0, path1, devdir_len, "disk/")) >= -1)
210
/* Prefer any other path over /dev/mapper/ path. */
211
if ((r = _builtin_preference(path0, path1, 0, dm_dir())) >= -1)
218
/* Return 1 if we prefer path1 else return 0 */
219
static int _compare_paths(const char *path0, const char *path1)
221
int slash0 = 0, slash1 = 0;
224
char p0[PATH_MAX], p1[PATH_MAX];
226
struct stat stat0, stat1;
230
* FIXME Better to compare patterns one-at-a-time against all names.
232
if (_cache.preferred_names_matcher) {
233
m0 = dm_regex_match(_cache.preferred_names_matcher, path0);
234
m1 = dm_regex_match(_cache.preferred_names_matcher, path1);
248
/* Apply built-in preference rules first. */
249
if ((r = _apply_builtin_path_preference_rules(path0, path1)) >= 0)
252
/* Return the path with fewer slashes */
253
for (p = path0; p++; p = (const char *) strchr(p, '/'))
256
for (p = path1; p++; p = (const char *) strchr(p, '/'))
264
strncpy(p0, path0, PATH_MAX);
265
strncpy(p1, path1, PATH_MAX);
269
/* We prefer symlinks - they exist for a reason!
270
* So we prefer a shorter path before the first symlink in the name.
271
* FIXME Configuration option to invert this? */
273
s0 = strchr(s0, '/');
274
s1 = strchr(s1, '/');
279
if (lstat(p0, &stat0)) {
280
log_sys_very_verbose("lstat", p0);
283
if (lstat(p1, &stat1)) {
284
log_sys_very_verbose("lstat", p1);
287
if (S_ISLNK(stat0.st_mode) && !S_ISLNK(stat1.st_mode))
289
if (!S_ISLNK(stat0.st_mode) && S_ISLNK(stat1.st_mode))
297
/* ASCII comparison */
298
if (strcmp(path0, path1) < 0)
304
static int _add_alias(struct device *dev, const char *path)
306
struct str_list *sl = _alloc(sizeof(*sl));
307
struct str_list *strl;
314
/* Is name already there? */
315
dm_list_iterate_items(strl, &dev->aliases) {
316
if (!strcmp(strl->str, path)) {
317
log_debug("%s: Already in device cache", path);
324
if (!dm_list_empty(&dev->aliases)) {
325
oldpath = dm_list_item(dev->aliases.n, struct str_list)->str;
326
prefer_old = _compare_paths(path, oldpath);
327
log_debug("%s: Aliased to %s in device cache%s",
328
path, oldpath, prefer_old ? "" : " (preferred name)");
331
log_debug("%s: Added to device cache", path);
334
dm_list_add(&dev->aliases, &sl->list);
336
dm_list_add_h(&dev->aliases, &sl->list);
342
* Either creates a new dev, or adds an alias to
345
static int _insert_dev(const char *path, dev_t d)
348
static dev_t loopfile_count = 0;
352
/* Generate pretend device numbers for loopfiles */
354
if (dm_hash_lookup(_cache.names, path))
356
d = ++loopfile_count;
360
/* is this device already registered ? */
361
if (!(dev = (struct device *) btree_lookup(_cache.devices,
363
/* create new device */
365
if (!(dev = dev_create_file(path, NULL, NULL, 0)))
367
} else if (!(dev = _dev_create(d)))
370
if (!(btree_insert(_cache.devices, (uint32_t) d, dev))) {
371
log_error("Couldn't insert device into binary tree.");
377
if (!(path_copy = dm_pool_strdup(_cache.mem, path))) {
378
log_error("Failed to duplicate path string.");
382
if (!loopfile && !_add_alias(dev, path_copy)) {
383
log_error("Couldn't add alias to dev cache.");
387
if (!dm_hash_insert(_cache.names, path_copy, dev)) {
388
log_error("Couldn't add name to hash in dev cache.");
395
static char *_join(const char *dir, const char *name)
397
size_t len = strlen(dir) + strlen(name) + 2;
398
char *r = dm_malloc(len);
400
snprintf(r, len, "%s/%s", dir, name);
406
* Get rid of extra slashes in the path string.
408
static void _collapse_slashes(char *str)
413
for (ptr = str; *ptr; ptr++) {
427
static int _insert_dir(const char *dir)
429
int n, dirent_count, r = 1;
430
struct dirent **dirent;
433
dirent_count = scandir(dir, &dirent, NULL, alphasort);
434
if (dirent_count > 0) {
435
for (n = 0; n < dirent_count; n++) {
436
if (dirent[n]->d_name[0] == '.') {
441
if (!(path = _join(dir, dirent[n]->d_name)))
444
_collapse_slashes(path);
445
r &= _insert(path, 1, 0);
456
static int _insert_file(const char *path)
460
if (stat(path, &info) < 0) {
461
log_sys_very_verbose("stat", path);
465
if (!S_ISREG(info.st_mode)) {
466
log_debug("%s: Not a regular file", path);
470
if (!_insert_dev(path, 0))
476
#ifdef UDEV_SYNC_SUPPORT
478
static int _device_in_udev_db(const dev_t d)
481
struct udev_device *udev_device;
483
if (!(udev = udev_get_library_context()))
486
if ((udev_device = udev_device_new_from_devnum(udev, 'b', d))) {
487
udev_device_unref(udev_device);
494
static int _insert_udev_dir(struct udev *udev, const char *dir)
496
struct udev_enumerate *udev_enum = NULL;
497
struct udev_list_entry *device_entry, *symlink_entry;
498
const char *node_name, *symlink_name;
499
struct udev_device *device;
502
if (!(udev_enum = udev_enumerate_new(udev)))
505
if (udev_enumerate_add_match_subsystem(udev_enum, "block") ||
506
udev_enumerate_scan_devices(udev_enum))
509
udev_list_entry_foreach(device_entry, udev_enumerate_get_list_entry(udev_enum)) {
510
device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(device_entry));
512
node_name = udev_device_get_devnode(device);
513
r &= _insert(node_name, 0, 0);
515
udev_list_entry_foreach(symlink_entry, udev_device_get_devlinks_list_entry(device)) {
516
symlink_name = udev_list_entry_get_name(symlink_entry);
517
r &= _insert(symlink_name, 0, 0);
520
udev_device_unref(device);
523
udev_enumerate_unref(udev_enum);
527
log_error("Failed to enumerate udev device list.");
528
udev_enumerate_unref(udev_enum);
532
static void _insert_dirs(struct dm_list *dirs)
538
with_udev = obtain_device_list_from_udev() &&
539
(udev = udev_get_library_context());
541
dm_list_iterate_items(dl, &_cache.dirs) {
543
if (!_insert_udev_dir(udev, dl->dir))
544
log_debug("%s: Failed to insert devices from "
545
"udev-managed directory to device "
546
"cache fully", dl->dir);
548
else if (!_insert_dir(dl->dir))
549
log_debug("%s: Failed to insert devices to "
550
"device cache fully", dl->dir);
554
#else /* UDEV_SYNC_SUPPORT */
556
static int _device_in_udev_db(const dev_t d)
561
static void _insert_dirs(struct dm_list *dirs)
565
dm_list_iterate_items(dl, &_cache.dirs)
566
_insert_dir(dl->dir);
569
#endif /* UDEV_SYNC_SUPPORT */
571
static int _insert(const char *path, int rec, int check_with_udev_db)
576
if (stat(path, &info) < 0) {
577
log_sys_very_verbose("stat", path);
581
if (check_with_udev_db && !_device_in_udev_db(info.st_rdev)) {
582
log_very_verbose("%s: Not in udev db", path);
586
if (S_ISDIR(info.st_mode)) { /* add a directory */
587
/* check it's not a symbolic link */
588
if (lstat(path, &info) < 0) {
589
log_sys_very_verbose("lstat", path);
593
if (S_ISLNK(info.st_mode)) {
594
log_debug("%s: Symbolic link to directory", path);
599
r = _insert_dir(path);
601
} else { /* add a device */
602
if (!S_ISBLK(info.st_mode)) {
603
log_debug("%s: Not a block device", path);
607
if (!_insert_dev(path, info.st_rdev))
616
static void _full_scan(int dev_scan)
620
if (_cache.has_scanned && !dev_scan)
623
_insert_dirs(&_cache.dirs);
625
dm_list_iterate_items(dl, &_cache.files)
626
_insert_file(dl->dir);
628
_cache.has_scanned = 1;
629
init_full_scan_done(1);
632
int dev_cache_has_scanned(void)
634
return _cache.has_scanned;
637
void dev_cache_scan(int do_scan)
640
_cache.has_scanned = 1;
645
static int _init_preferred_names(struct cmd_context *cmd)
647
const struct config_node *cn;
648
const struct config_value *v;
649
struct dm_pool *scratch = NULL;
654
_cache.preferred_names_matcher = NULL;
656
if (!(cn = find_config_tree_node(cmd, "devices/preferred_names")) ||
657
cn->v->type == CFG_EMPTY_ARRAY) {
658
log_very_verbose("devices/preferred_names not found in config file: "
659
"using built-in preferences");
663
for (v = cn->v; v; v = v->next) {
664
if (v->type != CFG_STRING) {
665
log_error("preferred_names patterns must be enclosed in quotes");
672
if (!(scratch = dm_pool_create("preferred device name matcher", 1024)))
675
if (!(regex = dm_pool_alloc(scratch, sizeof(*regex) * count))) {
676
log_error("Failed to allocate preferred device name "
681
for (v = cn->v, i = count - 1; v; v = v->next, i--) {
682
if (!(regex[i] = dm_pool_strdup(scratch, v->v.str))) {
683
log_error("Failed to allocate a preferred device name "
689
if (!(_cache.preferred_names_matcher =
690
dm_regex_create(_cache.mem, regex, count))) {
691
log_error("Preferred device name pattern matcher creation failed.");
698
dm_pool_destroy(scratch);
703
int dev_cache_init(struct cmd_context *cmd)
706
_cache.has_scanned = 0;
708
if (!(_cache.mem = dm_pool_create("dev_cache", 10 * 1024)))
711
if (!(_cache.names = dm_hash_create(128))) {
712
dm_pool_destroy(_cache.mem);
717
if (!(_cache.devices = btree_create(_cache.mem))) {
718
log_error("Couldn't create binary tree for dev-cache.");
722
if (!(_cache.dev_dir = _strdup(cmd->dev_dir))) {
723
log_error("strdup dev_dir failed.");
727
dm_list_init(&_cache.dirs);
728
dm_list_init(&_cache.files);
730
if (!_init_preferred_names(cmd))
740
static void _check_closed(struct device *dev)
743
log_error("Device '%s' has been left open.", dev_name(dev));
746
static void _check_for_open_devices(void)
748
dm_hash_iter(_cache.names, (dm_hash_iterate_fn) _check_closed);
751
void dev_cache_exit(void)
754
_check_for_open_devices();
756
if (_cache.preferred_names_matcher)
757
_cache.preferred_names_matcher = NULL;
760
dm_pool_destroy(_cache.mem);
765
dm_hash_destroy(_cache.names);
769
_cache.devices = NULL;
770
_cache.has_scanned = 0;
771
dm_list_init(&_cache.dirs);
772
dm_list_init(&_cache.files);
775
int dev_cache_add_dir(const char *path)
780
if (stat(path, &st)) {
781
log_error("Ignoring %s: %s", path, strerror(errno));
786
if (!S_ISDIR(st.st_mode)) {
787
log_error("Ignoring %s: Not a directory", path);
791
if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
792
log_error("dir_list allocation failed");
796
strcpy(dl->dir, path);
797
dm_list_add(&_cache.dirs, &dl->list);
801
int dev_cache_add_loopfile(const char *path)
806
if (stat(path, &st)) {
807
log_error("Ignoring %s: %s", path, strerror(errno));
812
if (!S_ISREG(st.st_mode)) {
813
log_error("Ignoring %s: Not a regular file", path);
817
if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
818
log_error("dir_list allocation failed for file");
822
strcpy(dl->dir, path);
823
dm_list_add(&_cache.files, &dl->list);
827
/* Check cached device name is still valid before returning it */
828
/* This should be a rare occurrence */
829
/* set quiet if the cache is expected to be out-of-date */
830
/* FIXME Make rest of code pass/cache struct device instead of dev_name */
831
const char *dev_name_confirmed(struct device *dev, int quiet)
837
if ((dev->flags & DEV_REGULAR))
838
return dev_name(dev);
840
while ((r = stat(name = dm_list_item(dev->aliases.n,
841
struct str_list)->str, &buf)) ||
842
(buf.st_rdev != dev->dev)) {
845
log_sys_debug("stat", name);
847
log_sys_error("stat", name);
850
log_debug("Path %s no longer valid for device(%d,%d)",
851
name, (int) MAJOR(dev->dev),
852
(int) MINOR(dev->dev));
854
log_error("Path %s no longer valid for device(%d,%d)",
855
name, (int) MAJOR(dev->dev),
856
(int) MINOR(dev->dev));
858
/* Remove the incorrect hash entry */
859
dm_hash_remove(_cache.names, name);
861
/* Leave list alone if there isn't an alternative name */
862
/* so dev_name will always find something to return. */
863
/* Otherwise add the name to the correct device. */
864
if (dm_list_size(&dev->aliases) > 1) {
865
dm_list_del(dev->aliases.n);
867
_insert(name, 0, obtain_device_list_from_udev());
871
/* Scanning issues this inappropriately sometimes. */
872
log_debug("Aborting - please provide new pathname for what "
873
"used to be %s", name);
877
return dev_name(dev);
880
struct device *dev_cache_get(const char *name, struct dev_filter *f)
883
struct device *d = (struct device *) dm_hash_lookup(_cache.names, name);
885
if (d && (d->flags & DEV_REGULAR))
888
/* If the entry's wrong, remove it */
889
if (d && (stat(name, &buf) || (buf.st_rdev != d->dev))) {
890
dm_hash_remove(_cache.names, name);
895
_insert(name, 0, obtain_device_list_from_udev());
896
d = (struct device *) dm_hash_lookup(_cache.names, name);
899
d = (struct device *) dm_hash_lookup(_cache.names, name);
903
return (d && (!f || (d->flags & DEV_REGULAR) ||
904
f->passes_filter(f, d))) ? d : NULL;
907
struct dev_iter *dev_iter_create(struct dev_filter *f, int dev_scan)
909
struct dev_iter *di = dm_malloc(sizeof(*di));
912
log_error("dev_iter allocation failed");
916
if (dev_scan && !trust_cache()) {
917
/* Flag gets reset between each command */
918
if (!full_scan_done())
919
persistent_filter_wipe(f); /* Calls _full_scan(1) */
923
di->current = btree_first(_cache.devices);
925
di->filter->use_count++;
930
void dev_iter_destroy(struct dev_iter *iter)
932
iter->filter->use_count--;
936
static struct device *_iter_next(struct dev_iter *iter)
938
struct device *d = btree_get_data(iter->current);
939
iter->current = btree_next(iter->current);
943
struct device *dev_iter_get(struct dev_iter *iter)
945
while (iter->current) {
946
struct device *d = _iter_next(iter);
947
if (!iter->filter || (d->flags & DEV_REGULAR) ||
948
iter->filter->passes_filter(iter->filter, d))
955
void dev_reset_error_count(struct cmd_context *cmd)
957
struct dev_iter iter;
962
iter.current = btree_first(_cache.devices);
964
_iter_next(&iter)->error_count = 0;
967
int dev_fd(struct device *dev)
972
const char *dev_name(const struct device *dev)
974
return (dev) ? dm_list_item(dev->aliases.n, struct str_list)->str :