1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/lib/device/dev-cache.c

403 lines
7.2 KiB
C
Raw Normal View History

/*
* Copyright (C) 2001 Sistina Software (UK) Limited.
*
2001-10-31 15:47:01 +03:00
* This file is released under the LGPL.
*/
#include "dev-cache.h"
#include "log.h"
#include "pool.h"
#include "hash.h"
#include "list.h"
2001-10-25 15:34:55 +04:00
#include "lvm-types.h"
#include "btree.h"
#include "dbg_malloc.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>
#include <linux/kdev_t.h>
/*
* FIXME: really need to seperate names from the devices since
* multiple names can point to the same device.
*/
struct dev_iter {
2001-10-25 15:34:55 +04:00
struct btree_iter *current;
struct dev_filter *filter;
};
struct dir_list {
2001-10-31 15:47:01 +03:00
struct list list;
char dir[0];
};
static struct {
struct pool *mem;
2001-10-25 15:34:55 +04:00
struct hash_table *names;
struct btree *devices;
int has_scanned;
2001-10-31 15:47:01 +03:00
struct list dirs;
} _cache;
#define _alloc(x) pool_alloc(_cache.mem, (x))
#define _free(x) pool_free(_cache.mem, (x))
2001-10-25 15:34:55 +04:00
static int _insert(const char *path, int rec);
static struct device *_create_dev(dev_t d)
2001-10-25 15:34:55 +04:00
{
struct device *dev;
if (!(dev = _alloc(sizeof(*dev)))) {
stack;
return NULL;
2001-10-25 15:34:55 +04:00
}
2001-10-31 15:47:01 +03:00
list_init(&dev->aliases);
2001-10-25 15:34:55 +04:00
dev->dev = d;
dev->fd = -1;
2001-10-25 15:34:55 +04:00
return dev;
}
static int _add_alias(struct device *dev, const char *path)
{
struct str_list *sl = _alloc(sizeof(*sl));
if (!sl) {
stack;
return 0;
}
if (!(sl->str = pool_strdup(_cache.mem, path))) {
stack;
return 0;
}
2001-10-31 15:47:01 +03:00
list_add(&dev->aliases, &sl->list);
2001-10-25 15:34:55 +04:00
return 1;
}
/*
* Either creates a new dev, or adds an alias to
* an existing dev.
*/
static int _insert_dev(const char *path, dev_t d)
{
struct device *dev;
/* is this device already registered ? */
if (!(dev = (struct device *) btree_lookup(_cache.devices, d))) {
2001-10-25 15:34:55 +04:00
/* create new device */
if (!(dev = _create_dev(d))) {
2001-10-25 15:34:55 +04:00
stack;
return 0;
}
if (!(btree_insert(_cache.devices, d, dev))) {
log_err("Couldn't insert device into binary tree.");
_free(dev);
return 0;
}
}
if (!_add_alias(dev, path)) {
2001-12-17 14:07:33 +03:00
log_err("Couldn't add alias to dev cache.");
2001-10-25 15:34:55 +04:00
return 0;
}
if (!hash_insert(_cache.names, path, dev)) {
2001-12-17 14:07:33 +03:00
log_err("Couldn't add name to hash in dev cache.");
return 0;
}
2001-10-25 15:34:55 +04:00
return 1;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
static char *_join(const char *dir, const char *name)
{
int len = strlen(dir) + strlen(name) + 2;
char *r = dbg_malloc(len);
if (r)
snprintf(r, len, "%s/%s", dir, name);
return r;
}
/*
* Get rid of extra slashes in the path string.
*/
static void _collapse_slashes(char *str)
{
char *ptr;
int was_slash = 0;
for (ptr = str; *ptr; ptr++) {
if (*ptr == '/') {
if (was_slash)
continue;
was_slash = 1;
} else
was_slash = 0;
*str++ = *ptr;
}
*str = *ptr;
}
2001-10-25 15:34:55 +04:00
static int _insert_dir(const char *dir)
{
2001-10-25 15:34:55 +04:00
int n, dirent_count, r = 1;
struct dirent **dirent;
char *path;
2001-10-25 15:34:55 +04:00
dirent_count = scandir(dir, &dirent, NULL, alphasort);
if (dirent_count > 0) {
for (n = 0; n < dirent_count; n++) {
if (dirent[n]->d_name[0] == '.') {
free(dirent[n]);
continue;
}
2001-10-25 15:34:55 +04:00
if (!(path = _join(dir, dirent[n]->d_name))) {
stack;
return 0;
}
2001-10-25 15:34:55 +04:00
_collapse_slashes(path);
r &= _insert(path, 1);
dbg_free(path);
2001-10-25 15:34:55 +04:00
free(dirent[n]);
}
free(dirent);
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
return r;
2001-10-08 17:58:52 +04:00
}
2001-10-25 15:34:55 +04:00
static int _insert(const char *path, int rec)
2001-10-08 17:58:52 +04:00
{
struct stat info;
2001-10-25 15:34:55 +04:00
int r = 0;
2001-10-08 17:58:52 +04:00
if (stat(path, &info) < 0) {
2001-10-10 20:36:32 +04:00
log_sys_very_verbose("stat", path);
2001-10-08 17:58:52 +04:00
return 0;
}
2001-10-25 15:34:55 +04:00
if (S_ISDIR(info.st_mode)) { /* add a directory */
if (rec)
r = _insert_dir(path);
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
} else { /* add a device */
if (!S_ISBLK(info.st_mode)) {
log_debug("%s: Not a block device", path);
2001-10-25 15:34:55 +04:00
return 0;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
if (!_insert_dev(path, info.st_rdev)) {
stack;
return 0;
}
2001-10-25 15:34:55 +04:00
r = 1;
}
2001-10-25 15:34:55 +04:00
return r;
}
static void _full_scan(void)
{
2001-10-31 15:47:01 +03:00
struct list *dh;
if (_cache.has_scanned)
return;
2001-10-31 15:47:01 +03:00
list_iterate(dh, &_cache.dirs) {
struct dir_list *dl = list_item(dh, struct dir_list);
2001-10-25 15:34:55 +04:00
_insert_dir(dl->dir);
2001-10-31 15:47:01 +03:00
};
_cache.has_scanned = 1;
}
int dev_cache_init(void)
{
2001-10-25 15:34:55 +04:00
_cache.names = NULL;
if (!(_cache.mem = pool_create(10 * 1024))) {
stack;
return 0;
}
2001-10-25 15:34:55 +04:00
if (!(_cache.names = hash_create(128))) {
stack;
pool_destroy(_cache.mem);
_cache.mem = 0;
return 0;
}
2001-10-25 15:34:55 +04:00
if (!(_cache.devices = btree_create(_cache.mem))) {
log_err("Couldn't create binary tree for dev-cache.");
goto bad;
}
2001-10-31 15:47:01 +03:00
list_init(&_cache.dirs);
2001-10-08 16:11:33 +04:00
return 1;
2001-10-25 15:34:55 +04:00
bad:
dev_cache_exit();
return 0;
}
2002-01-16 02:47:56 +03:00
void _check_closed(struct device *dev)
{
if (dev->fd >= 0)
log_err("Device '%s' has been left open.", dev_name(dev));
}
static inline void _check_for_open_devices(void)
{
2002-02-25 14:54:15 +03:00
hash_iter(_cache.names, (iterate_fn)_check_closed);
}
void dev_cache_exit(void)
{
_check_for_open_devices();
pool_destroy(_cache.mem);
2001-10-25 15:34:55 +04:00
if (_cache.names)
hash_destroy(_cache.names);
}
int dev_cache_add_dir(const char *path)
{
struct dir_list *dl;
2001-10-23 15:50:49 +04:00
struct stat st;
if (stat(path, &st)) {
log_error("Ignoring %s: %s", path, strerror(errno));
/* But don't fail */
return 1;
}
if (!S_ISDIR(st.st_mode)) {
log_error("Ignoring %s: Not a directory", path);
return 1;
}
if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1)))
return 0;
strcpy(dl->dir, path);
2001-10-31 15:47:01 +03:00
list_add(&_cache.dirs, &dl->list);
return 1;
}
/* Check cached device name is still valid before returning it */
/* This should be a rare occurrence */
/* FIXME Make rest of code pass/cache struct device instead of dev_name */
const char *dev_name_confirmed(struct device *dev)
{
struct stat buf;
char *name;
int r;
while ((r = stat(name = list_item(dev->aliases.n,
struct str_list)->str, &buf)) ||
(buf.st_rdev != dev->dev)) {
if (r < 0)
log_sys_error("stat", name);
log_error("Path %s no longer valid for device(%d,%d)",
name, (int) MAJOR(dev->dev), (int) MINOR(dev->dev));
/* Remove the incorrect hash entry */
hash_remove(_cache.names, name);
/* Leave list alone if there isn't an alternative name */
/* so dev_name will always find something to return. */
/* Otherwise add the name to the correct device. */
if (list_size(&dev->aliases) > 1) {
list_del(dev->aliases.n);
if (!r)
_insert(name, 0);
continue;
}
log_error("Aborting - please provide new pathname for what "
"used to be %s", name);
return NULL;
}
return dev_name(dev);
}
struct device *dev_cache_get(const char *name, struct dev_filter *f)
{
struct stat buf;
2001-10-25 15:34:55 +04:00
struct device *d = (struct device *) hash_lookup(_cache.names, name);
2001-10-08 16:11:33 +04:00
/* If the entry's wrong, remove it */
if (d && (stat(name, &buf) || (buf.st_rdev != d->dev))) {
hash_remove(_cache.names, name);
d = NULL;
}
2001-10-08 17:58:52 +04:00
if (!d) {
_insert(name, 0);
2001-10-25 15:34:55 +04:00
d = (struct device *) hash_lookup(_cache.names, name);
2001-10-08 17:58:52 +04:00
}
2001-10-08 16:11:33 +04:00
return (d && (!f || f->passes_filter(f, d))) ? d : NULL;
}
struct dev_iter *dev_iter_create(struct dev_filter *f)
{
struct dev_iter *di = dbg_malloc(sizeof(*di));
if (!di)
return NULL;
_full_scan();
2001-10-25 15:34:55 +04:00
di->current = btree_first(_cache.devices);
di->filter = f;
return di;
}
void dev_iter_destroy(struct dev_iter *iter)
{
dbg_free(iter);
}
static inline struct device *_iter_next(struct dev_iter *iter)
{
2001-10-25 15:34:55 +04:00
struct device *d = btree_get_data(iter->current);
iter->current = btree_next(iter->current);
return d;
}
struct device *dev_iter_get(struct dev_iter *iter)
{
while (iter->current) {
struct device *d = _iter_next(iter);
if (!iter->filter ||
iter->filter->passes_filter(iter->filter, d))
return d;
}
return NULL;
}