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
18 #include "dev-cache.h"
20 #include "filter-persistent.h"
22 #include "lvm-string.h"
31 struct dm_hash_table *devices;
32 struct dev_filter *real;
37 * The hash table holds one of these two states
40 #define PF_BAD_DEVICE ((void *) 1)
41 #define PF_GOOD_DEVICE ((void *) 2)
43 static int _init_hash(struct pfilter *pf)
46 dm_hash_destroy(pf->devices);
48 if (!(pf->devices = dm_hash_create(128)))
54 int persistent_filter_wipe(struct dev_filter *f)
56 struct pfilter *pf = (struct pfilter *) f->private;
58 log_verbose("Wiping cache of LVM-capable devices");
59 dm_hash_wipe(pf->devices);
61 /* Trigger complete device scan */
67 static int _read_array(struct pfilter *pf, struct config_tree *cft,
68 const char *path, void *data)
70 const struct config_node *cn;
71 const struct config_value *cv;
73 if (!(cn = find_config_node(cft->root, path))) {
74 log_very_verbose("Couldn't find %s array in '%s'",
80 * iterate through the array, adding
83 for (cv = cn->v; cv; cv = cv->next) {
84 if (cv->type != CFG_STRING) {
85 log_verbose("Devices array contains a value "
86 "which is not a string ... ignoring");
90 if (!dm_hash_insert(pf->devices, cv->v.str, data))
91 log_verbose("Couldn't add '%s' to filter ... ignoring",
93 /* Populate dev_cache ourselves */
94 dev_cache_get(cv->v.str, NULL);
99 int persistent_filter_load(struct dev_filter *f, struct config_tree **cft_out)
101 struct pfilter *pf = (struct pfilter *) f->private;
102 struct config_tree *cft;
106 if (!stat(pf->file, &info))
107 pf->ctime = info.st_ctime;
109 log_very_verbose("%s: stat failed: %s", pf->file,
114 if (!(cft = create_config_tree(pf->file, 1)))
117 if (!read_config_file(cft))
120 _read_array(pf, cft, "persistent_filter_cache/valid_devices",
122 /* We don't gain anything by holding invalid devices */
123 /* _read_array(pf, cft, "persistent_filter_cache/invalid_devices",
126 /* Did we find anything? */
127 if (dm_hash_get_num_entries(pf->devices)) {
128 /* We populated dev_cache ourselves */
133 log_very_verbose("Loaded persistent filter cache from %s", pf->file);
139 destroy_config_tree(cft);
143 static void _write_array(struct pfilter *pf, FILE *fp, const char *path,
148 char buf[2 * PATH_MAX];
149 struct dm_hash_node *n;
151 for (n = dm_hash_get_first(pf->devices); n;
152 n = dm_hash_get_next(pf->devices, n)) {
153 d = dm_hash_get_data(pf->devices, n);
161 fprintf(fp, "\t%s=[\n", path);
165 escape_double_quotes(buf, dm_hash_get_key(pf->devices, n));
166 fprintf(fp, "\t\t\"%s\"", buf);
170 fprintf(fp, "\n\t]\n");
173 int persistent_filter_dump(struct dev_filter *f, int merge_existing)
177 struct stat info, info2;
178 struct config_tree *cft = NULL;
185 pf = (struct pfilter *) f->private;
187 if (!dm_hash_get_num_entries(pf->devices)) {
188 log_very_verbose("Internal persistent device cache empty "
189 "- not writing to %s", pf->file);
192 if (!dev_cache_has_scanned()) {
193 log_very_verbose("Device cache incomplete - not writing "
198 log_very_verbose("Dumping persistent device cache to %s", pf->file);
201 if ((lockfd = fcntl_lock_file(pf->file, F_WRLCK, 0)) < 0)
205 * Ensure we locked the file we expected
207 if (fstat(lockfd, &info)) {
208 log_sys_error("fstat", pf->file);
211 if (stat(pf->file, &info2)) {
212 log_sys_error("stat", pf->file);
216 if (is_same_inode(info, info2))
219 fcntl_unlock_file(lockfd);
223 * If file contents changed since we loaded it, merge new contents
225 if (merge_existing && info.st_ctime != pf->ctime)
226 /* Keep cft open to avoid losing lock */
227 persistent_filter_load(f, &cft);
229 tmp_file = alloca(strlen(pf->file) + 5);
230 sprintf(tmp_file, "%s.tmp", pf->file);
232 if (!(fp = fopen(tmp_file, "w"))) {
233 /* EACCES has been reported over NFS */
234 if (errno != EROFS && errno != EACCES)
235 log_sys_error("fopen", tmp_file);
239 fprintf(fp, "# This file is automatically maintained by lvm.\n\n");
240 fprintf(fp, "persistent_filter_cache {\n");
242 _write_array(pf, fp, "valid_devices", PF_GOOD_DEVICE);
243 /* We don't gain anything by remembering invalid devices */
244 /* _write_array(pf, fp, "invalid_devices", PF_BAD_DEVICE); */
247 if (lvm_fclose(fp, tmp_file))
250 if (rename(tmp_file, pf->file))
251 log_error("%s: rename to %s failed: %s", tmp_file, pf->file,
257 fcntl_unlock_file(lockfd);
260 destroy_config_tree(cft);
265 static int _lookup_p(struct dev_filter *f, struct device *dev)
267 struct pfilter *pf = (struct pfilter *) f->private;
268 void *l = dm_hash_lookup(pf->devices, dev_name(dev));
272 if (l == PF_BAD_DEVICE) {
273 log_debug("%s: Skipping (cached)", dev_name(dev));
277 /* Test dm devices every time, so cache them as GOOD. */
278 if (MAJOR(dev->dev) == dm_major()) {
280 dm_list_iterate_items(sl, &dev->aliases)
281 dm_hash_insert(pf->devices, sl->str, PF_GOOD_DEVICE);
282 if (!device_is_usable(dev)) {
283 log_debug("%s: Skipping unusable device", dev_name(dev));
286 return pf->real->passes_filter(pf->real, dev);
291 l = pf->real->passes_filter(pf->real, dev) ? PF_GOOD_DEVICE : PF_BAD_DEVICE;
293 dm_list_iterate_items(sl, &dev->aliases)
294 dm_hash_insert(pf->devices, sl->str, l);
297 return (l == PF_BAD_DEVICE) ? 0 : 1;
300 static void _persistent_destroy(struct dev_filter *f)
302 struct pfilter *pf = (struct pfilter *) f->private;
305 log_error(INTERNAL_ERROR "Destroying persistent filter while in use %u times.", f->use_count);
307 dm_hash_destroy(pf->devices);
309 pf->real->destroy(pf->real);
314 struct dev_filter *persistent_filter_create(struct dev_filter *real,
318 struct dev_filter *f = NULL;
321 if (!(pf = dm_zalloc(sizeof(*pf))))
324 if (!(pf->file = dm_malloc(strlen(file) + 1)))
327 strcpy(pf->file, file);
330 if (!(_init_hash(pf))) {
331 log_error("Couldn't create hash table for persistent filter.");
335 if (!(f = dm_malloc(sizeof(*f))))
338 /* Only merge cache file before dumping it if it changed externally. */
339 if (!stat(pf->file, &info))
340 pf->ctime = info.st_ctime;
342 f->passes_filter = _lookup_p;
343 f->destroy = _persistent_destroy;
352 dm_hash_destroy(pf->devices);