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);
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);
322 if (!(sl->str = dm_pool_strdup(_cache.mem, path)))
325 if (!dm_list_empty(&dev->aliases)) {
326 oldpath = dm_list_item(dev->aliases.n, struct str_list)->str;
327 prefer_old = _compare_paths(path, oldpath);
328 log_debug("%s: Aliased to %s in device cache%s",
329 path, oldpath, prefer_old ? "" : " (preferred name)");
332 log_debug("%s: Added to device cache", path);
335 dm_list_add(&dev->aliases, &sl->list);
337 dm_list_add_h(&dev->aliases, &sl->list);
343 * Either creates a new dev, or adds an alias to
346 static int _insert_dev(const char *path, dev_t d)
349 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 (!loopfile && !_add_alias(dev, path)) {
378 log_error("Couldn't add alias to dev cache.");
382 if (!dm_hash_insert(_cache.names, path, dev)) {
383 log_error("Couldn't add name to hash in dev cache.");
390 static char *_join(const char *dir, const char *name)
392 size_t len = strlen(dir) + strlen(name) + 2;
393 char *r = dm_malloc(len);
395 snprintf(r, len, "%s/%s", dir, name);
401 * Get rid of extra slashes in the path string.
403 static void _collapse_slashes(char *str)
408 for (ptr = str; *ptr; ptr++) {
422 static int _insert_dir(const char *dir)
424 int n, dirent_count, r = 1;
425 struct dirent **dirent;
428 dirent_count = scandir(dir, &dirent, NULL, alphasort);
429 if (dirent_count > 0) {
430 for (n = 0; n < dirent_count; n++) {
431 if (dirent[n]->d_name[0] == '.') {
436 if (!(path = _join(dir, dirent[n]->d_name)))
439 _collapse_slashes(path);
440 r &= _insert(path, 1);
451 static int _insert_file(const char *path)
455 if (stat(path, &info) < 0) {
456 log_sys_very_verbose("stat", path);
460 if (!S_ISREG(info.st_mode)) {
461 log_debug("%s: Not a regular file", path);
465 if (!_insert_dev(path, 0))
471 static int _insert(const char *path, int rec)
476 if (stat(path, &info) < 0) {
477 log_sys_very_verbose("stat", path);
481 if (S_ISDIR(info.st_mode)) { /* add a directory */
482 /* check it's not a symbolic link */
483 if (lstat(path, &info) < 0) {
484 log_sys_very_verbose("lstat", path);
488 if (S_ISLNK(info.st_mode)) {
489 log_debug("%s: Symbolic link to directory", path);
494 r = _insert_dir(path);
496 } else { /* add a device */
497 if (!S_ISBLK(info.st_mode)) {
498 log_debug("%s: Not a block device", path);
502 if (!_insert_dev(path, info.st_rdev))
511 static void _full_scan(int dev_scan)
515 if (_cache.has_scanned && !dev_scan)
518 dm_list_iterate_items(dl, &_cache.dirs)
519 _insert_dir(dl->dir);
521 dm_list_iterate_items(dl, &_cache.files)
522 _insert_file(dl->dir);
524 _cache.has_scanned = 1;
525 init_full_scan_done(1);
528 int dev_cache_has_scanned(void)
530 return _cache.has_scanned;
533 void dev_cache_scan(int do_scan)
536 _cache.has_scanned = 1;
541 static int _init_preferred_names(struct cmd_context *cmd)
543 const struct config_node *cn;
544 const struct config_value *v;
545 struct dm_pool *scratch = NULL;
550 _cache.preferred_names_matcher = NULL;
552 if (!(cn = find_config_tree_node(cmd, "devices/preferred_names")) ||
553 cn->v->type == CFG_EMPTY_ARRAY) {
554 log_very_verbose("devices/preferred_names not found in config file: "
555 "using built-in preferences");
559 for (v = cn->v; v; v = v->next) {
560 if (v->type != CFG_STRING) {
561 log_error("preferred_names patterns must be enclosed in quotes");
568 if (!(scratch = dm_pool_create("preferred device name matcher", 1024)))
571 if (!(regex = dm_pool_alloc(scratch, sizeof(*regex) * count))) {
572 log_error("Failed to allocate preferred device name "
577 for (v = cn->v, i = count - 1; v; v = v->next, i--) {
578 if (!(regex[i] = dm_pool_strdup(scratch, v->v.str))) {
579 log_error("Failed to allocate a preferred device name "
585 if (!(_cache.preferred_names_matcher =
586 dm_regex_create(_cache.mem, regex, count))) {
587 log_error("Preferred device name pattern matcher creation failed.");
594 dm_pool_destroy(scratch);
599 int dev_cache_init(struct cmd_context *cmd)
602 _cache.has_scanned = 0;
604 if (!(_cache.mem = dm_pool_create("dev_cache", 10 * 1024)))
607 if (!(_cache.names = dm_hash_create(128))) {
608 dm_pool_destroy(_cache.mem);
613 if (!(_cache.devices = btree_create(_cache.mem))) {
614 log_error("Couldn't create binary tree for dev-cache.");
618 if (!(_cache.dev_dir = _strdup(cmd->dev_dir))) {
619 log_error("strdup dev_dir failed.");
623 dm_list_init(&_cache.dirs);
624 dm_list_init(&_cache.files);
626 if (!_init_preferred_names(cmd))
636 static void _check_closed(struct device *dev)
639 log_error("Device '%s' has been left open.", dev_name(dev));
642 static void _check_for_open_devices(void)
644 dm_hash_iter(_cache.names, (dm_hash_iterate_fn) _check_closed);
647 void dev_cache_exit(void)
650 _check_for_open_devices();
652 if (_cache.preferred_names_matcher)
653 _cache.preferred_names_matcher = NULL;
656 dm_pool_destroy(_cache.mem);
661 dm_hash_destroy(_cache.names);
665 _cache.devices = NULL;
666 _cache.has_scanned = 0;
667 dm_list_init(&_cache.dirs);
668 dm_list_init(&_cache.files);
671 int dev_cache_add_dir(const char *path)
676 if (stat(path, &st)) {
677 log_error("Ignoring %s: %s", path, strerror(errno));
682 if (!S_ISDIR(st.st_mode)) {
683 log_error("Ignoring %s: Not a directory", path);
687 if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
688 log_error("dir_list allocation failed");
692 strcpy(dl->dir, path);
693 dm_list_add(&_cache.dirs, &dl->list);
697 int dev_cache_add_loopfile(const char *path)
702 if (stat(path, &st)) {
703 log_error("Ignoring %s: %s", path, strerror(errno));
708 if (!S_ISREG(st.st_mode)) {
709 log_error("Ignoring %s: Not a regular file", path);
713 if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
714 log_error("dir_list allocation failed for file");
718 strcpy(dl->dir, path);
719 dm_list_add(&_cache.files, &dl->list);
723 /* Check cached device name is still valid before returning it */
724 /* This should be a rare occurrence */
725 /* set quiet if the cache is expected to be out-of-date */
726 /* FIXME Make rest of code pass/cache struct device instead of dev_name */
727 const char *dev_name_confirmed(struct device *dev, int quiet)
733 if ((dev->flags & DEV_REGULAR))
734 return dev_name(dev);
736 while ((r = stat(name = dm_list_item(dev->aliases.n,
737 struct str_list)->str, &buf)) ||
738 (buf.st_rdev != dev->dev)) {
741 log_sys_debug("stat", name);
743 log_sys_error("stat", name);
746 log_debug("Path %s no longer valid for device(%d,%d)",
747 name, (int) MAJOR(dev->dev),
748 (int) MINOR(dev->dev));
750 log_error("Path %s no longer valid for device(%d,%d)",
751 name, (int) MAJOR(dev->dev),
752 (int) MINOR(dev->dev));
754 /* Remove the incorrect hash entry */
755 dm_hash_remove(_cache.names, name);
757 /* Leave list alone if there isn't an alternative name */
758 /* so dev_name will always find something to return. */
759 /* Otherwise add the name to the correct device. */
760 if (dm_list_size(&dev->aliases) > 1) {
761 dm_list_del(dev->aliases.n);
767 /* Scanning issues this inappropriately sometimes. */
768 log_debug("Aborting - please provide new pathname for what "
769 "used to be %s", name);
773 return dev_name(dev);
776 struct device *dev_cache_get(const char *name, struct dev_filter *f)
779 struct device *d = (struct device *) dm_hash_lookup(_cache.names, name);
781 if (d && (d->flags & DEV_REGULAR))
784 /* If the entry's wrong, remove it */
785 if (d && (stat(name, &buf) || (buf.st_rdev != d->dev))) {
786 dm_hash_remove(_cache.names, name);
792 d = (struct device *) dm_hash_lookup(_cache.names, name);
795 d = (struct device *) dm_hash_lookup(_cache.names, name);
799 return (d && (!f || (d->flags & DEV_REGULAR) ||
800 f->passes_filter(f, d))) ? d : NULL;
803 struct dev_iter *dev_iter_create(struct dev_filter *f, int dev_scan)
805 struct dev_iter *di = dm_malloc(sizeof(*di));
808 log_error("dev_iter allocation failed");
812 if (dev_scan && !trust_cache()) {
813 /* Flag gets reset between each command */
814 if (!full_scan_done())
815 persistent_filter_wipe(f); /* Calls _full_scan(1) */
819 di->current = btree_first(_cache.devices);
821 di->filter->use_count++;
826 void dev_iter_destroy(struct dev_iter *iter)
828 iter->filter->use_count--;
832 static struct device *_iter_next(struct dev_iter *iter)
834 struct device *d = btree_get_data(iter->current);
835 iter->current = btree_next(iter->current);
839 struct device *dev_iter_get(struct dev_iter *iter)
841 while (iter->current) {
842 struct device *d = _iter_next(iter);
843 if (!iter->filter || (d->flags & DEV_REGULAR) ||
844 iter->filter->passes_filter(iter->filter, d))
851 void dev_reset_error_count(struct cmd_context *cmd)
853 struct dev_iter *iter;
856 if (!(iter = dev_iter_create(cmd->filter, 0))) {
857 log_error("Resetting device error count failed");
861 for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter))
862 dev->error_count = 0;
864 dev_iter_destroy(iter);
867 int dev_fd(struct device *dev)
872 const char *dev_name(const struct device *dev)
874 return (dev) ? dm_list_item(dev->aliases.n, struct str_list)->str :