Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / lib / filters / filter-persistent.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
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.
10  *
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
14  */
15
16 #include "lib.h"
17 #include "config.h"
18 #include "dev-cache.h"
19 #include "filter.h"
20 #include "filter-persistent.h"
21 #include "lvm-file.h"
22 #include "lvm-string.h"
23 #include "activate.h"
24
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29 struct pfilter {
30         char *file;
31         struct dm_hash_table *devices;
32         struct dev_filter *real;
33         time_t ctime;
34 };
35
36 /*
37  * The hash table holds one of these two states
38  * against each entry.
39  */
40 #define PF_BAD_DEVICE ((void *) 1)
41 #define PF_GOOD_DEVICE ((void *) 2)
42
43 static int _init_hash(struct pfilter *pf)
44 {
45         if (pf->devices)
46                 dm_hash_destroy(pf->devices);
47
48         if (!(pf->devices = dm_hash_create(128)))
49                 return_0;
50
51         return 1;
52 }
53
54 int persistent_filter_wipe(struct dev_filter *f)
55 {
56         struct pfilter *pf = (struct pfilter *) f->private;
57
58         log_verbose("Wiping cache of LVM-capable devices");
59         dm_hash_wipe(pf->devices);
60
61         /* Trigger complete device scan */
62         dev_cache_scan(1);
63
64         return 1;
65 }
66
67 static int _read_array(struct pfilter *pf, struct config_tree *cft,
68                        const char *path, void *data)
69 {
70         const struct config_node *cn;
71         const struct config_value *cv;
72
73         if (!(cn = find_config_node(cft->root, path))) {
74                 log_very_verbose("Couldn't find %s array in '%s'",
75                                  path, pf->file);
76                 return 0;
77         }
78
79         /*
80          * iterate through the array, adding
81          * devices as we go.
82          */
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");
87                         continue;
88                 }
89
90                 if (!dm_hash_insert(pf->devices, cv->v.str, data))
91                         log_verbose("Couldn't add '%s' to filter ... ignoring",
92                                     cv->v.str);
93                 /* Populate dev_cache ourselves */
94                 dev_cache_get(cv->v.str, NULL);
95         }
96         return 1;
97 }
98
99 int persistent_filter_load(struct dev_filter *f, struct config_tree **cft_out)
100 {
101         struct pfilter *pf = (struct pfilter *) f->private;
102         struct config_tree *cft;
103         struct stat info;
104         int r = 0;
105
106         if (!stat(pf->file, &info))
107                 pf->ctime = info.st_ctime;
108         else {
109                 log_very_verbose("%s: stat failed: %s", pf->file,
110                                  strerror(errno));
111                 return_0;
112         }
113
114         if (!(cft = create_config_tree(pf->file, 1)))
115                 return_0;
116
117         if (!read_config_file(cft))
118                 goto_out;
119
120         _read_array(pf, cft, "persistent_filter_cache/valid_devices",
121                     PF_GOOD_DEVICE);
122         /* We don't gain anything by holding invalid devices */
123         /* _read_array(pf, cft, "persistent_filter_cache/invalid_devices",
124            PF_BAD_DEVICE); */
125
126         /* Did we find anything? */
127         if (dm_hash_get_num_entries(pf->devices)) {
128                 /* We populated dev_cache ourselves */
129                 dev_cache_scan(0);
130                 r = 1;
131         }
132
133         log_very_verbose("Loaded persistent filter cache from %s", pf->file);
134
135       out:
136         if (r && cft_out)
137                 *cft_out = cft;
138         else
139                 destroy_config_tree(cft);
140         return r;
141 }
142
143 static void _write_array(struct pfilter *pf, FILE *fp, const char *path,
144                          void *data)
145 {
146         void *d;
147         int first = 1;
148         char buf[2 * PATH_MAX];
149         struct dm_hash_node *n;
150
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);
154
155                 if (d != data)
156                         continue;
157
158                 if (!first)
159                         fprintf(fp, ",\n");
160                 else {
161                         fprintf(fp, "\t%s=[\n", path);
162                         first = 0;
163                 }
164
165                 escape_double_quotes(buf, dm_hash_get_key(pf->devices, n));
166                 fprintf(fp, "\t\t\"%s\"", buf);
167         }
168
169         if (!first)
170                 fprintf(fp, "\n\t]\n");
171 }
172
173 int persistent_filter_dump(struct dev_filter *f, int merge_existing)
174 {
175         struct pfilter *pf;
176         char *tmp_file;
177         struct stat info, info2;
178         struct config_tree *cft = NULL;
179         FILE *fp;
180         int lockfd;
181         int r = 0;
182
183         if (!f)
184                 return_0;
185         pf = (struct pfilter *) f->private;
186
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);
190                 return 0;
191         }
192         if (!dev_cache_has_scanned()) {
193                 log_very_verbose("Device cache incomplete - not writing "
194                                  "to %s", pf->file);
195                 return 0;
196         }
197
198         log_very_verbose("Dumping persistent device cache to %s", pf->file);
199
200         while (1) {
201                 if ((lockfd = fcntl_lock_file(pf->file, F_WRLCK, 0)) < 0)
202                         return_0;
203
204                 /*
205                  * Ensure we locked the file we expected
206                  */
207                 if (fstat(lockfd, &info)) {
208                         log_sys_error("fstat", pf->file);
209                         goto out;
210                 }
211                 if (stat(pf->file, &info2)) {
212                         log_sys_error("stat", pf->file);
213                         goto out;
214                 }
215
216                 if (is_same_inode(info, info2))
217                         break;
218         
219                 fcntl_unlock_file(lockfd);
220         }
221
222         /*
223          * If file contents changed since we loaded it, merge new contents
224          */
225         if (merge_existing && info.st_ctime != pf->ctime)
226                 /* Keep cft open to avoid losing lock */
227                 persistent_filter_load(f, &cft);
228
229         tmp_file = alloca(strlen(pf->file) + 5);
230         sprintf(tmp_file, "%s.tmp", pf->file);
231
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);
236                 goto out;
237         }
238
239         fprintf(fp, "# This file is automatically maintained by lvm.\n\n");
240         fprintf(fp, "persistent_filter_cache {\n");
241
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); */
245
246         fprintf(fp, "}\n");
247         if (lvm_fclose(fp, tmp_file))
248                 goto_out;
249
250         if (rename(tmp_file, pf->file))
251                 log_error("%s: rename to %s failed: %s", tmp_file, pf->file,
252                           strerror(errno));
253
254         r = 1;
255
256 out:
257         fcntl_unlock_file(lockfd);
258
259         if (cft)
260                 destroy_config_tree(cft);
261
262         return r;
263 }
264
265 static int _lookup_p(struct dev_filter *f, struct device *dev)
266 {
267         struct pfilter *pf = (struct pfilter *) f->private;
268         void *l = dm_hash_lookup(pf->devices, dev_name(dev));
269         struct str_list *sl;
270
271         /* Cached BAD? */
272         if (l == PF_BAD_DEVICE) {
273                 log_debug("%s: Skipping (cached)", dev_name(dev));
274                 return 0;
275         }
276
277         /* Test dm devices every time, so cache them as GOOD. */
278         if (MAJOR(dev->dev) == dm_major()) {
279                 if (!l)
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));
284                         return 0;
285                 }
286                 return pf->real->passes_filter(pf->real, dev);
287         }
288
289         /* Uncached */
290         if (!l) {
291                 l = pf->real->passes_filter(pf->real, dev) ?  PF_GOOD_DEVICE : PF_BAD_DEVICE;
292
293                 dm_list_iterate_items(sl, &dev->aliases)
294                         dm_hash_insert(pf->devices, sl->str, l);
295         }
296
297         return (l == PF_BAD_DEVICE) ? 0 : 1;
298 }
299
300 static void _persistent_destroy(struct dev_filter *f)
301 {
302         struct pfilter *pf = (struct pfilter *) f->private;
303
304         if (f->use_count)
305                 log_error(INTERNAL_ERROR "Destroying persistent filter while in use %u times.", f->use_count);
306
307         dm_hash_destroy(pf->devices);
308         dm_free(pf->file);
309         pf->real->destroy(pf->real);
310         dm_free(pf);
311         dm_free(f);
312 }
313
314 struct dev_filter *persistent_filter_create(struct dev_filter *real,
315                                             const char *file)
316 {
317         struct pfilter *pf;
318         struct dev_filter *f = NULL;
319         struct stat info;
320
321         if (!(pf = dm_zalloc(sizeof(*pf))))
322                 return_NULL;
323
324         if (!(pf->file = dm_malloc(strlen(file) + 1)))
325                 goto_bad;
326
327         strcpy(pf->file, file);
328         pf->real = real;
329
330         if (!(_init_hash(pf))) {
331                 log_error("Couldn't create hash table for persistent filter.");
332                 goto bad;
333         }
334
335         if (!(f = dm_malloc(sizeof(*f))))
336                 goto_bad;
337
338         /* Only merge cache file before dumping it if it changed externally. */
339         if (!stat(pf->file, &info))
340                 pf->ctime = info.st_ctime;
341
342         f->passes_filter = _lookup_p;
343         f->destroy = _persistent_destroy;
344         f->use_count = 0;
345         f->private = pf;
346
347         return f;
348
349       bad:
350         dm_free(pf->file);
351         if (pf->devices)
352                 dm_hash_destroy(pf->devices);
353         dm_free(pf);
354         dm_free(f);
355         return NULL;
356 }