1 /* $NetBSD: dev-cache.c,v 1.4 2009/12/02 00:58:03 haad Exp $ */
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "dev-cache.h"
20 #include "lvm-types.h"
23 #include "filter-persistent.h"
24 #include "toolcontext.h"
27 #include <sys/param.h>
35 struct btree_iter *current;
36 struct dev_filter *filter;
46 struct dm_hash_table *names;
47 struct btree *devices;
48 struct dm_regex *preferred_names_matcher;
56 #define _alloc(x) dm_pool_zalloc(_cache.mem, (x))
57 #define _free(x) dm_pool_free(_cache.mem, (x))
58 #define _strdup(x) dm_pool_strdup(_cache.mem, (x))
60 static int _insert(const char *path, int rec);
62 struct device *dev_create_file(const char *filename, struct device *dev,
63 struct str_list *alias, int use_malloc)
69 if (!(dev = dm_malloc(sizeof(*dev)))) {
70 log_error("struct device allocation failed");
73 if (!(alias = dm_malloc(sizeof(*alias)))) {
74 log_error("struct str_list allocation failed");
78 if (!(alias->str = dm_strdup(filename))) {
79 log_error("filename strdup failed");
84 dev->flags = DEV_ALLOCED;
86 if (!(dev = _alloc(sizeof(*dev)))) {
87 log_error("struct device allocation failed");
90 if (!(alias = _alloc(sizeof(*alias)))) {
91 log_error("struct str_list allocation failed");
95 if (!(alias->str = _strdup(filename))) {
96 log_error("filename strdup failed");
100 } else if (!(alias->str = dm_strdup(filename))) {
101 log_error("filename strdup failed");
105 dev->flags |= DEV_REGULAR;
106 dm_list_init(&dev->aliases);
107 dm_list_add(&dev->aliases, &alias->list);
108 dev->end = UINT64_C(0);
112 dev->block_size = -1;
113 dev->read_ahead = -1;
114 memset(dev->pvid, 0, sizeof(dev->pvid));
115 dm_list_init(&dev->open_list);
120 static struct device *_dev_create(dev_t d)
124 if (!(dev = _alloc(sizeof(*dev)))) {
125 log_error("struct device allocation failed");
129 dm_list_init(&dev->aliases);
133 dev->block_size = -1;
134 dev->read_ahead = -1;
135 dev->end = UINT64_C(0);
136 memset(dev->pvid, 0, sizeof(dev->pvid));
137 dm_list_init(&dev->open_list);
142 void dev_set_preferred_name(struct str_list *sl, struct device *dev)
145 * Don't interfere with ordering specified in config file.
147 if (_cache.preferred_names_matcher)
150 log_debug("%s: New preferred name", sl->str);
151 dm_list_del(&sl->list);
152 dm_list_add_h(&dev->aliases, &sl->list);
155 /* Return 1 if we prefer path1 else return 0 */
156 static int _compare_paths(const char *path0, const char *path1)
158 int slash0 = 0, slash1 = 0;
161 char p0[PATH_MAX], p1[PATH_MAX];
163 struct stat stat0, stat1;
166 * FIXME Better to compare patterns one-at-a-time against all names.
168 if (_cache.preferred_names_matcher) {
169 m0 = dm_regex_match(_cache.preferred_names_matcher, path0);
170 m1 = dm_regex_match(_cache.preferred_names_matcher, path1);
188 /* Return the path with fewer slashes */
189 for (p = path0; p++; p = (const char *) strchr(p, '/'))
192 for (p = path1; p++; p = (const char *) strchr(p, '/'))
200 strncpy(p0, path0, PATH_MAX);
201 strncpy(p1, path1, PATH_MAX);
205 /* We prefer symlinks - they exist for a reason!
206 * So we prefer a shorter path before the first symlink in the name.
207 * FIXME Configuration option to invert this? */
209 s0 = strchr(s0, '/');
210 s1 = strchr(s1, '/');
215 if (lstat(p0, &stat0)) {
216 log_sys_very_verbose("lstat", p0);
219 if (lstat(p1, &stat1)) {
220 log_sys_very_verbose("lstat", p1);
223 if (S_ISLNK(stat0.st_mode) && !S_ISLNK(stat1.st_mode))
225 if (!S_ISLNK(stat0.st_mode) && S_ISLNK(stat1.st_mode))
233 /* ASCII comparison */
234 if (strcmp(path0, path1) < 0)
240 static int _add_alias(struct device *dev, const char *path)
242 struct str_list *sl = _alloc(sizeof(*sl));
243 struct str_list *strl;
250 /* Is name already there? */
251 dm_list_iterate_items(strl, &dev->aliases) {
252 if (!strcmp(strl->str, path)) {
253 log_debug("%s: Already in device cache", path);
258 if (!(sl->str = dm_pool_strdup(_cache.mem, path)))
261 if (!dm_list_empty(&dev->aliases)) {
262 oldpath = dm_list_item(dev->aliases.n, struct str_list)->str;
263 prefer_old = _compare_paths(path, oldpath);
264 log_debug("%s: Aliased to %s in device cache%s",
265 path, oldpath, prefer_old ? "" : " (preferred name)");
268 log_debug("%s: Added to device cache", path);
271 dm_list_add(&dev->aliases, &sl->list);
273 dm_list_add_h(&dev->aliases, &sl->list);
279 * Either creates a new dev, or adds an alias to
282 static int _insert_dev(const char *path, dev_t d)
285 static dev_t loopfile_count = 0;
288 /* Generate pretend device numbers for loopfiles */
290 if (dm_hash_lookup(_cache.names, path))
292 d = ++loopfile_count;
296 /* is this device already registered ? */
297 if (!(dev = (struct device *) btree_lookup(_cache.devices,
299 /* create new device */
301 if (!(dev = dev_create_file(path, NULL, NULL, 0)))
303 } else if (!(dev = _dev_create(d)))
306 if (!(btree_insert(_cache.devices, (uint32_t) d, dev))) {
307 log_error("Couldn't insert device into binary tree.");
313 if (!loopfile && !_add_alias(dev, path)) {
314 log_error("Couldn't add alias to dev cache.");
318 if (!dm_hash_insert(_cache.names, path, dev)) {
319 log_error("Couldn't add name to hash in dev cache.");
326 static char *_join(const char *dir, const char *name)
328 size_t len = strlen(dir) + strlen(name) + 2;
329 char *r = dm_malloc(len);
331 snprintf(r, len, "%s/%s", dir, name);
337 * Get rid of extra slashes in the path string.
339 static void _collapse_slashes(char *str)
344 for (ptr = str; *ptr; ptr++) {
358 static int _insert_dir(const char *dir)
360 int n, dirent_count, r = 1;
361 struct dirent **dirent;
364 dirent_count = scandir(dir, &dirent, NULL, alphasort);
365 if (dirent_count > 0) {
366 for (n = 0; n < dirent_count; n++) {
367 if (dirent[n]->d_name[0] == '.') {
372 if (!(path = _join(dir, dirent[n]->d_name)))
375 _collapse_slashes(path);
376 r &= _insert(path, 1);
387 static int _insert_file(const char *path)
391 if (stat(path, &info) < 0) {
392 log_sys_very_verbose("stat", path);
396 if (!S_ISREG(info.st_mode)) {
397 log_debug("%s: Not a regular file", path);
401 if (!_insert_dev(path, 0))
407 static int _insert(const char *path, int rec)
412 if (stat(path, &info) < 0) {
413 log_sys_very_verbose("stat", path);
417 if (S_ISDIR(info.st_mode)) { /* add a directory */
418 /* check it's not a symbolic link */
419 if (lstat(path, &info) < 0) {
420 log_sys_very_verbose("lstat", path);
424 if (S_ISLNK(info.st_mode)) {
425 log_debug("%s: Symbolic link to directory", path);
430 r = _insert_dir(path);
436 * In NetBSD we have two different types of devices
437 * raw and block. I can insert only existing
438 * raw and block device.
440 if (S_ISBLK(info.st_mode)) {
441 log_debug("%s: Not a raw device", path);
444 if (nbsd_check_dev(MAJOR(info.st_rdev),path) < 0) {
445 log_debug("%s: Not a known raw device", path);
448 #elif defined(__DragonFly__)
450 * This never happens, but oh well...
452 if (S_ISBLK(info.st_mode)) {
453 log_debug("%s: Not a raw device", path);
458 * We avoid adding devctl to the cache because reading from it
459 * locks lvm up. devctl doesn't seem to be caught later in the
460 * filter because it has major number 0, and lvm seems to assume
461 * dm also has major 0.
463 if (!strcmp(path, "/dev/devctl")) {
464 log_debug("%s: It's devctl, no interest");
468 if (!strncmp(path, "/dev/tun", strlen("/dev/tun"))) {
469 log_debug("%s: Not adding tun devices");
473 if (!strncmp(path, "/dev/cd", strlen("/dev/cd")) ||
474 !strncmp(path, "/dev/acd", strlen("/dev/acd"))) {
475 log_debug("%s: Not adding CD drives");
479 if (!strcmp(path, "/dev/vn")) {
480 log_debug("%s: Not adding vn autoclone dev");
484 if (!strncmp(path, "/dev/md", strlen("/dev/md"))) {
485 log_debug("%s: Not adding malloc disks");
489 if (!strncmp(path, "/dev/fd", strlen("/dev/fd"))) {
490 log_debug("%s: Not adding floppy disks or fds");
494 if (!S_ISBLK(info.st_mode))
495 log_debug("%s: Not a block device", path);
497 if (!_insert_dev(path, info.st_rdev)) {
507 static void _full_scan(int dev_scan)
511 if (_cache.has_scanned && !dev_scan)
514 dm_list_iterate_items(dl, &_cache.dirs)
515 _insert_dir(dl->dir);
517 dm_list_iterate_items(dl, &_cache.files)
518 _insert_file(dl->dir);
520 _cache.has_scanned = 1;
521 init_full_scan_done(1);
524 int dev_cache_has_scanned(void)
526 return _cache.has_scanned;
529 void dev_cache_scan(int do_scan)
532 _cache.has_scanned = 1;
537 static int _init_preferred_names(struct cmd_context *cmd)
539 const struct config_node *cn;
540 struct config_value *v;
541 struct dm_pool *scratch = NULL;
546 _cache.preferred_names_matcher = NULL;
548 if (!(cn = find_config_tree_node(cmd, "devices/preferred_names")) ||
549 cn->v->type == CFG_EMPTY_ARRAY) {
550 log_very_verbose("devices/preferred_names not found in config file: "
551 "using built-in preferences");
555 for (v = cn->v; v; v = v->next) {
556 if (v->type != CFG_STRING) {
557 log_error("preferred_names patterns must be enclosed in quotes");
564 if (!(scratch = dm_pool_create("preferred device name matcher", 1024)))
567 if (!(regex = dm_pool_alloc(scratch, sizeof(*regex) * count))) {
568 log_error("Failed to allocate preferred device name "
573 for (v = cn->v, i = count - 1; v; v = v->next, i--) {
574 if (!(regex[i] = dm_pool_strdup(scratch, v->v.str))) {
575 log_error("Failed to allocate a preferred device name "
581 if (!(_cache.preferred_names_matcher =
582 dm_regex_create(_cache.mem,(const char **) regex, count))) {
583 log_error("Preferred device name pattern matcher creation failed.");
590 dm_pool_destroy(scratch);
595 int dev_cache_init(struct cmd_context *cmd)
598 _cache.has_scanned = 0;
600 if (!(_cache.mem = dm_pool_create("dev_cache", 10 * 1024)))
603 if (!(_cache.names = dm_hash_create(128))) {
604 dm_pool_destroy(_cache.mem);
609 if (!(_cache.devices = btree_create(_cache.mem))) {
610 log_error("Couldn't create binary tree for dev-cache.");
614 dm_list_init(&_cache.dirs);
615 dm_list_init(&_cache.files);
617 if (!_init_preferred_names(cmd))
627 static void _check_closed(struct device *dev)
630 log_error("Device '%s' has been left open.", dev_name(dev));
633 static void _check_for_open_devices(void)
635 dm_hash_iter(_cache.names, (dm_hash_iterate_fn) _check_closed);
638 void dev_cache_exit(void)
641 _check_for_open_devices();
643 if (_cache.preferred_names_matcher)
644 _cache.preferred_names_matcher = NULL;
647 dm_pool_destroy(_cache.mem);
652 dm_hash_destroy(_cache.names);
656 _cache.devices = NULL;
657 _cache.has_scanned = 0;
658 dm_list_init(&_cache.dirs);
659 dm_list_init(&_cache.files);
662 int dev_cache_add_dir(const char *path)
667 if (stat(path, &st)) {
668 log_error("Ignoring %s: %s", path, strerror(errno));
673 if (!S_ISDIR(st.st_mode)) {
674 log_error("Ignoring %s: Not a directory", path);
678 if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
679 log_error("dir_list allocation failed");
683 strcpy(dl->dir, path);
684 dm_list_add(&_cache.dirs, &dl->list);
688 int dev_cache_add_loopfile(const char *path)
693 if (stat(path, &st)) {
694 log_error("Ignoring %s: %s", path, strerror(errno));
699 if (!S_ISREG(st.st_mode)) {
700 log_error("Ignoring %s: Not a regular file", path);
704 if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) {
705 log_error("dir_list allocation failed for file");
709 strcpy(dl->dir, path);
710 dm_list_add(&_cache.files, &dl->list);
714 /* Check cached device name is still valid before returning it */
715 /* This should be a rare occurrence */
716 /* set quiet if the cache is expected to be out-of-date */
717 /* FIXME Make rest of code pass/cache struct device instead of dev_name */
718 const char *dev_name_confirmed(struct device *dev, int quiet)
724 if ((dev->flags & DEV_REGULAR))
725 return dev_name(dev);
727 while ((r = stat(name = dm_list_item(dev->aliases.n,
728 struct str_list)->str, &buf)) ||
729 (buf.st_rdev != dev->dev)) {
732 log_sys_debug("stat", name);
734 log_sys_error("stat", name);
737 log_debug("Path %s no longer valid for device(%d,%d)",
738 name, (int) MAJOR(dev->dev),
739 (int) MINOR(dev->dev));
741 log_error("Path %s no longer valid for device(%d,%d)",
742 name, (int) MAJOR(dev->dev),
743 (int) MINOR(dev->dev));
745 /* Remove the incorrect hash entry */
746 dm_hash_remove(_cache.names, name);
748 /* Leave list alone if there isn't an alternative name */
749 /* so dev_name will always find something to return. */
750 /* Otherwise add the name to the correct device. */
751 if (dm_list_size(&dev->aliases) > 1) {
752 dm_list_del(dev->aliases.n);
758 /* Scanning issues this inappropriately sometimes. */
759 log_debug("Aborting - please provide new pathname for what "
760 "used to be %s", name);
764 return dev_name(dev);
767 struct device *dev_cache_get(const char *name, struct dev_filter *f)
770 struct device *d = (struct device *) dm_hash_lookup(_cache.names, name);
772 if (d && (d->flags & DEV_REGULAR))
775 /* If the entry's wrong, remove it */
776 if (d && (stat(name, &buf) || (buf.st_rdev != d->dev))) {
777 dm_hash_remove(_cache.names, name);
783 d = (struct device *) dm_hash_lookup(_cache.names, name);
786 d = (struct device *) dm_hash_lookup(_cache.names, name);
790 return (d && (!f || (d->flags & DEV_REGULAR) ||
791 f->passes_filter(f, d))) ? d : NULL;
794 struct dev_iter *dev_iter_create(struct dev_filter *f, int dev_scan)
796 struct dev_iter *di = dm_malloc(sizeof(*di));
799 log_error("dev_iter allocation failed");
803 if (dev_scan && !trust_cache()) {
804 /* Flag gets reset between each command */
805 if (!full_scan_done())
806 persistent_filter_wipe(f); /* Calls _full_scan(1) */
810 di->current = btree_first(_cache.devices);
816 void dev_iter_destroy(struct dev_iter *iter)
821 static struct device *_iter_next(struct dev_iter *iter)
823 struct device *d = btree_get_data(iter->current);
824 iter->current = btree_next(iter->current);
828 struct device *dev_iter_get(struct dev_iter *iter)
830 while (iter->current) {
831 struct device *d = _iter_next(iter);
832 if (!iter->filter || (d->flags & DEV_REGULAR) ||
833 iter->filter->passes_filter(iter->filter, d))
840 int dev_fd(struct device *dev)
845 const char *dev_name(const struct device *dev)
847 return (dev) ? dm_list_item(dev->aliases.n, struct str_list)->str :