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

274 lines
5.8 KiB
C
Raw Normal View History

/*
2004-03-30 23:35:44 +04:00
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004 Red Hat, Inc. All rights reserved.
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU General Public License v.2.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2002-11-18 17:01:16 +03:00
#include "lib.h"
#include "config.h"
#include "dev-cache.h"
#include "filter-persistent.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
struct pfilter {
char *file;
struct dm_hash_table *devices;
2001-10-23 16:24:55 +04:00
struct dev_filter *real;
};
/*
* entries in the table can be in one of these
* states.
*/
2001-10-23 16:24:55 +04:00
#define PF_BAD_DEVICE ((void *) 1)
#define PF_GOOD_DEVICE ((void *) 2)
static int _init_hash(struct pfilter *pf)
{
if (pf->devices)
dm_hash_destroy(pf->devices);
if (!(pf->devices = dm_hash_create(128))) {
stack;
return 0;
}
return 1;
}
2001-10-23 16:24:55 +04:00
int persistent_filter_wipe(struct dev_filter *f)
{
2001-10-23 16:24:55 +04:00
struct pfilter *pf = (struct pfilter *) f->private;
log_verbose("Wiping cache of LVM-capable devices");
dm_hash_wipe(pf->devices);
2002-11-18 17:01:16 +03:00
/* Trigger complete device scan */
dev_cache_scan(1);
return 1;
2001-10-23 16:24:55 +04:00
}
static int _read_array(struct pfilter *pf, struct config_tree *cft,
2001-10-23 16:24:55 +04:00
const char *path, void *data)
{
2004-05-04 22:28:15 +04:00
const struct config_node *cn;
2001-10-23 16:24:55 +04:00
struct config_value *cv;
if (!(cn = find_config_node(cft->root, path))) {
log_very_verbose("Couldn't find %s array in '%s'",
path, pf->file);
2001-10-23 16:24:55 +04:00
return 0;
}
/*
* iterate through the array, adding
* devices as we go.
*/
for (cv = cn->v; cv; cv = cv->next) {
if (cv->type != CFG_STRING) {
log_verbose("Devices array contains a value "
"which is not a string ... ignoring");
continue;
}
if (!dm_hash_insert(pf->devices, cv->v.str, data))
log_verbose("Couldn't add '%s' to filter ... ignoring",
cv->v.str);
2002-11-18 17:01:16 +03:00
/* Populate dev_cache ourselves */
dev_cache_get(cv->v.str, NULL);
}
2001-10-23 16:24:55 +04:00
return 1;
}
int persistent_filter_load(struct dev_filter *f)
{
struct pfilter *pf = (struct pfilter *) f->private;
int r = 0;
struct config_tree *cft;
2001-10-23 16:24:55 +04:00
2004-05-04 22:28:15 +04:00
if (!(cft = create_config_tree(pf->file))) {
2001-10-23 16:24:55 +04:00
stack;
return 0;
}
2004-05-04 22:28:15 +04:00
if (!read_config_file(cft)) {
2001-10-23 16:24:55 +04:00
stack;
goto out;
}
_read_array(pf, cft, "persistent_filter_cache/valid_devices",
PF_GOOD_DEVICE);
2002-11-18 17:01:16 +03:00
/* We don't gain anything by holding invalid devices */
/* _read_array(pf, cft, "persistent_filter_cache/invalid_devices",
2002-11-18 17:01:16 +03:00
PF_BAD_DEVICE); */
/* Did we find anything? */
if (dm_hash_get_num_entries(pf->devices)) {
2002-11-18 17:01:16 +03:00
/* We populated dev_cache ourselves */
dev_cache_scan(0);
2001-10-23 16:24:55 +04:00
r = 1;
2002-11-18 17:01:16 +03:00
}
log_very_verbose("Loaded persistent filter cache from %s", pf->file);
out:
destroy_config_tree(cft);
return r;
}
2002-11-18 17:01:16 +03:00
static void _write_array(struct pfilter *pf, FILE *fp, const char *path,
2001-10-23 16:24:55 +04:00
void *data)
{
2001-10-23 16:24:55 +04:00
void *d;
int first = 1;
struct dm_hash_node *n;
for (n = dm_hash_get_first(pf->devices); n;
n = dm_hash_get_next(pf->devices, n)) {
d = dm_hash_get_data(pf->devices, n);
2001-10-23 16:24:55 +04:00
if (d != data)
continue;
if (!first)
fprintf(fp, ",\n");
else {
fprintf(fp, "\t%s=[\n", path);
first = 0;
}
fprintf(fp, "\t\t\"%s\"", dm_hash_get_key(pf->devices, n));
}
if (!first)
fprintf(fp, "\n\t]\n");
return;
}
2001-10-23 16:24:55 +04:00
int persistent_filter_dump(struct dev_filter *f)
{
2001-10-23 16:24:55 +04:00
struct pfilter *pf = (struct pfilter *) f->private;
FILE *fp;
if (!dm_hash_get_num_entries(pf->devices)) {
log_very_verbose("Internal persistent device cache empty "
"- not writing to %s", pf->file);
return 0;
}
2002-11-18 17:01:16 +03:00
if (!dev_cache_has_scanned()) {
log_very_verbose("Device cache incomplete - not writing "
"to %s", pf->file);
return 0;
}
2001-10-23 16:24:55 +04:00
log_very_verbose("Dumping persistent device cache to %s", pf->file);
fp = fopen(pf->file, "w");
2001-10-23 16:24:55 +04:00
if (!fp) {
2002-07-11 18:09:26 +04:00
if (errno != EROFS)
log_sys_error("fopen", pf->file);
2001-10-23 16:24:55 +04:00
return 0;
}
2001-10-23 16:24:55 +04:00
fprintf(fp, "# This file is automatically maintained by lvm.\n\n");
fprintf(fp, "persistent_filter_cache {\n");
2001-10-23 16:24:55 +04:00
_write_array(pf, fp, "valid_devices", PF_GOOD_DEVICE);
2002-11-18 17:01:16 +03:00
/* We don't gain anything by remembering invalid devices */
/* _write_array(pf, fp, "invalid_devices", PF_BAD_DEVICE); */
fprintf(fp, "}\n");
2001-10-23 16:24:55 +04:00
fclose(fp);
return 1;
}
2001-10-23 16:24:55 +04:00
static int _lookup_p(struct dev_filter *f, struct device *dev)
{
struct pfilter *pf = (struct pfilter *) f->private;
void *l = dm_hash_lookup(pf->devices, dev_name(dev));
struct str_list *sl;
2001-10-23 16:24:55 +04:00
if (!l) {
l = pf->real->passes_filter(pf->real, dev) ?
PF_GOOD_DEVICE : PF_BAD_DEVICE;
2005-06-01 20:51:55 +04:00
list_iterate_items(sl, &dev->aliases)
dm_hash_insert(pf->devices, sl->str, l);
2005-06-01 20:51:55 +04:00
2004-11-24 23:36:52 +03:00
} else if (l == PF_BAD_DEVICE)
log_debug("%s: Skipping (cached)", dev_name(dev));
2004-11-24 23:36:52 +03:00
return (l == PF_BAD_DEVICE) ? 0 : 1;
}
static void _persistent_destroy(struct dev_filter *f)
{
struct pfilter *pf = (struct pfilter *) f->private;
dm_hash_destroy(pf->devices);
dm_free(pf->file);
2001-10-23 16:24:55 +04:00
pf->real->destroy(pf->real);
dm_free(pf);
dm_free(f);
}
2001-10-23 16:24:55 +04:00
struct dev_filter *persistent_filter_create(struct dev_filter *real,
const char *file)
{
struct pfilter *pf;
struct dev_filter *f = NULL;
if (!(pf = dm_malloc(sizeof(*pf)))) {
stack;
return NULL;
}
memset(pf, 0, sizeof(*pf));
if (!(pf->file = dm_malloc(strlen(file) + 1))) {
stack;
goto bad;
}
strcpy(pf->file, file);
2001-10-23 16:24:55 +04:00
pf->real = real;
if (!(_init_hash(pf))) {
log_error("Couldn't create hash table for persistent filter.");
goto bad;
}
if (!(f = dm_malloc(sizeof(*f)))) {
stack;
goto bad;
}
2001-10-23 16:24:55 +04:00
f->passes_filter = _lookup_p;
f->destroy = _persistent_destroy;
f->private = pf;
return f;
bad:
dm_free(pf->file);
if (pf->devices)
dm_hash_destroy(pf->devices);
dm_free(pf);
dm_free(f);
return NULL;
}