1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-10 05:18:36 +03:00
lvm2/lib/filters/filter-persistent.c

223 lines
4.2 KiB
C
Raw Normal View History

/*
* Copyright (C) 2001 Sistina Software (UK) Limited.
*
* This file is released under the GPL.
*/
#include "config.h"
#include "dev-cache.h"
#include "hash.h"
#include "dbg_malloc.h"
#include "log.h"
#include "filter-persistent.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
struct pfilter {
char *file;
struct 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)
hash_destroy(pf->devices);
pf->devices = hash_create(128);
2001-10-23 16:24:55 +04:00
return pf->devices ? 1 : 0;
}
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;
2001-10-23 16:24:55 +04:00
hash_destroy(pf->devices);
return _init_hash(pf);
}
2001-10-23 16:24:55 +04:00
static int _read_array(struct pfilter *pf, struct config_file *cf,
const char *path, void *data)
{
struct config_node *cn;
struct config_value *cv;
if (!(cn = find_config_node(cf->root, "/valid_devices", '/'))) {
2001-10-23 15:50:49 +04:00
log_info("Couldn't find 'valid_devices' array in '%s'",
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) {
2001-10-23 15:50:49 +04:00
log_info("Valid_devices array contains a value "
"which is not a string ... ignoring");
continue;
}
2001-10-23 16:24:55 +04:00
if (!hash_insert(pf->devices, cv->v.str, data))
2001-10-23 15:50:49 +04:00
log_info("Couldn't add '%s' to filter ... ignoring",
cv->v.str);
}
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_file *cf;
if (!(cf = create_config_file())) {
stack;
return 0;
}
if (!read_config(cf, pf->file)) {
stack;
goto out;
}
if (_read_array(pf, cf, "/valid_devices", PF_GOOD_DEVICE) &&
_read_array(pf, cf, "/invalid_devices", PF_BAD_DEVICE))
r = 1;
out:
destroy_config_file(cf);
return r;
}
2001-10-23 16:24:55 +04:00
static void _write_array(struct pfilter *pf, FILE *fp, const char *path,
void *data)
{
2001-10-23 16:24:55 +04:00
void *d;
int first = 1;
struct hash_node *n;
2001-10-23 16:24:55 +04:00
fprintf(fp, "%s=[\n", path);
for (n = hash_get_first(pf->devices); n;
n = hash_get_next(pf->devices, n)) {
2001-10-23 16:24:55 +04:00
d = hash_get_data(pf->devices, n);
if (d != data)
continue;
if (!first)
fprintf(fp, ",\n");
else
first = 0;
fprintf(fp, "\t\"%s\"", hash_get_key(pf->devices, n));
}
fprintf(fp, "\n]\n");
}
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;
2001-10-23 16:24:55 +04:00
FILE *fp = fopen(pf->file, "w");
2001-10-23 16:24:55 +04:00
log_very_verbose("Dumping persistent device cache to %s", pf->file);
2001-10-23 16:24:55 +04:00
if (!fp) {
log_info("Couldn't open '%s' for to hold valid devices.",
pf->file);
return 0;
}
2001-10-23 16:24:55 +04:00
fprintf(fp, "# This file is automatically maintained by lvm.\n\n");
2001-10-23 16:24:55 +04:00
_write_array(pf, fp, "valid_devices", PF_GOOD_DEVICE);
_write_array(pf, fp, "invalid_devices", PF_BAD_DEVICE);
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 = hash_lookup(pf->devices, dev->name);
2001-10-23 16:24:55 +04:00
if (!l) {
l = pf->real->passes_filter(pf->real, dev) ?
PF_GOOD_DEVICE : PF_BAD_DEVICE;
2001-10-23 16:24:55 +04:00
hash_insert(pf->devices, dev->name, l);
}
2001-10-23 16:24:55 +04:00
return l == PF_GOOD_DEVICE;
}
static void _destroy(struct dev_filter *f)
{
struct pfilter *pf = (struct pfilter *) f->private;
hash_destroy(pf->devices);
dbg_free(pf->file);
2001-10-23 16:24:55 +04:00
pf->real->destroy(pf->real);
dbg_free(pf);
dbg_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 = dbg_malloc(sizeof(*pf)))) {
stack;
return NULL;
}
memset(pf, 0, sizeof(*pf));
if (!(pf->file = dbg_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_err("Couldn't create hash table for persistent filter.");
goto bad;
}
if (!(f = dbg_malloc(sizeof(*f)))) {
stack;
goto bad;
}
2001-10-23 16:24:55 +04:00
f->passes_filter = _lookup_p;
f->destroy = _destroy;
f->private = pf;
return f;
bad:
dbg_free(pf->file);
if (pf->devices)
hash_destroy(pf->devices);
dbg_free(pf);
dbg_free(f);
return NULL;
}