]> sourceware.org Git - lvm2.git/blob - lib/cache/lvmetad.c
Invert some return codes (which used to get ignored anyway) in cases when
[lvm2.git] / lib / cache / lvmetad.c
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
3 *
4 * This file is part of LVM2.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
9 *
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13 */
14
15 #include "lib.h"
16 #include "toolcontext.h"
17 #include "metadata.h"
18 #include "device.h"
19 #include "lvmetad.h"
20 #include "lvmcache.h"
21 #include "lvmetad-client.h"
22 #include "format-text.h" // TODO for disk_locn, used as a DA representation
23 #include "filter.h"
24
25 static int _using_lvmetad = 0;
26 static daemon_handle _lvmetad;
27
28 void lvmetad_init(void)
29 {
30 const char *socket = getenv("LVM_LVMETAD_SOCKET");
31 if (_using_lvmetad) { /* configured by the toolcontext */
32 _lvmetad = lvmetad_open(socket ?: DEFAULT_RUN_DIR "/lvmetad.socket");
33 if (_lvmetad.socket_fd < 0 || _lvmetad.error) {
34 log_warn("Failed to connect to lvmetad: %s. Falling back to scanning.", strerror(_lvmetad.error));
35 _using_lvmetad = 0;
36 }
37 }
38 }
39
40 /*
41 * Helper; evaluate the reply from lvmetad, check for errors, print diagnostics
42 * and return a summary success/failure exit code. Frees up the reply resources
43 * as well.
44 */
45 static int _lvmetad_handle_reply(daemon_reply reply, const char *action, const char *object) {
46 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
47 log_error("Request to %s %s in lvmetad has failed. Reason: %s",
48 action, object, reply.error ? strerror(reply.error) :
49 daemon_reply_str(reply, "reason", "Unknown."));
50 daemon_reply_destroy(reply);
51 return 0;
52 }
53
54 daemon_reply_destroy(reply);
55 return 1;
56 }
57
58 static int _read_mda(struct lvmcache_info *info,
59 struct format_type *fmt,
60 const struct dm_config_node *cn)
61 {
62 struct metadata_area_ops *ops;
63
64 dm_list_iterate_items(ops, &fmt->mda_ops)
65 if (ops->mda_import_text && ops->mda_import_text(info, cn))
66 return 1;
67
68 return 0;
69 }
70
71 static struct lvmcache_info *_pv_populate_lvmcache(
72 struct cmd_context *cmd, struct dm_config_node *cn, dev_t fallback)
73 {
74 struct device *device;
75 struct id pvid, vgid;
76 char mda_id[32];
77 char da_id[32];
78 int i = 0;
79 struct dm_config_node *mda = NULL;
80 struct dm_config_node *da = NULL;
81 uint64_t offset, size;
82 struct lvmcache_info *info;
83 const char *pvid_txt = dm_config_find_str(cn->child, "id", NULL),
84 *vgid_txt = dm_config_find_str(cn->child, "vgid", NULL),
85 *vgname = dm_config_find_str(cn->child, "vgname", NULL),
86 *fmt_name = dm_config_find_str(cn->child, "format", NULL);
87 dev_t devt = dm_config_find_int(cn->child, "device", 0);
88 uint64_t devsize = dm_config_find_int(cn->child, "dev_size", 0),
89 label_sector = dm_config_find_int(cn->child, "label_sector", 0);
90
91 struct format_type *fmt = fmt_name ? get_format_by_name(cmd, fmt_name) : NULL;
92
93 if (!fmt) {
94 log_warn("No format for PV %s. It is probably missing.", pvid_txt);
95 return NULL;
96 }
97
98 device = dev_cache_get_by_devt(devt, cmd->filter);
99 if (!device && fallback)
100 device = dev_cache_get_by_devt(fallback, cmd->filter);
101
102 if (!device) {
103 log_warn("No device for PV %s.", pvid_txt);
104 return NULL;
105 }
106
107 if (!pvid_txt || !id_read_format(&pvid, pvid_txt)) {
108 log_warn("Missing or ill-formatted PVID for PV: %s.", pvid_txt);
109 return NULL;
110 }
111
112 if (vgid_txt)
113 id_read_format(&vgid, vgid_txt);
114 else
115 strcpy((char*)&vgid, fmt->orphan_vg_name);
116
117 if (!vgname)
118 vgname = fmt->orphan_vg_name;
119
120 info = lvmcache_add(fmt->labeller, (const char *)&pvid, device,
121 vgname, (const char *)&vgid, 0);
122
123 lvmcache_get_label(info)->sector = label_sector;
124 lvmcache_set_device_size(info, devsize);
125 lvmcache_del_das(info);
126 lvmcache_del_mdas(info);
127
128 do {
129 sprintf(mda_id, "mda%d", i);
130 mda = dm_config_find_node(cn->child, mda_id);
131 if (mda)
132 _read_mda(info, fmt, mda);
133 ++i;
134 } while (mda);
135
136 i = 0;
137 do {
138 sprintf(da_id, "da%d", i);
139 da = dm_config_find_node(cn->child, da_id);
140 if (da) {
141 if (!dm_config_get_uint64(da->child, "offset", &offset)) return_0;
142 if (!dm_config_get_uint64(da->child, "size", &size)) return_0;
143 lvmcache_add_da(info, offset, size);
144 }
145 ++i;
146 } while (da);
147
148 return info;
149 }
150
151 struct volume_group *lvmetad_vg_lookup(struct cmd_context *cmd, const char *vgname, const char *vgid)
152 {
153 struct volume_group *vg = NULL;
154 daemon_reply reply;
155 char uuid[64];
156 struct format_instance *fid;
157 struct format_instance_ctx fic;
158 struct dm_config_node *top;
159 const char *name;
160 const char *fmt_name;
161 struct format_type *fmt;
162 struct dm_config_node *pvcn;
163 struct pv_list *pvl;
164 struct lvmcache_info *info;
165
166 if (!_using_lvmetad)
167 return NULL;
168
169 if (vgid) {
170 if (!id_write_format((const struct id*)vgid, uuid, sizeof(uuid)))
171 return_0;
172 reply = daemon_send_simple(_lvmetad, "vg_lookup", "uuid = %s", uuid, NULL);
173 } else {
174 if (!vgname)
175 log_error(INTERNAL_ERROR "VG name required (VGID not available)");
176 reply = daemon_send_simple(_lvmetad, "vg_lookup", "name = %s", vgname, NULL);
177 }
178
179 if (!strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
180
181 top = dm_config_find_node(reply.cft->root, "metadata");
182 name = daemon_reply_str(reply, "name", NULL);
183
184 /* fall back to lvm2 if we don't know better */
185 fmt_name = dm_config_find_str(top, "metadata/format", "lvm2");
186 if (!(fmt = get_format_by_name(cmd, fmt_name))) {
187 log_error(INTERNAL_ERROR
188 "We do not know the format (%s) reported by lvmetad.",
189 fmt_name);
190 return NULL;
191 }
192
193 fic.type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS;
194 fic.context.vg_ref.vg_name = name;
195 fic.context.vg_ref.vg_id = vgid;
196
197 if (!(fid = fmt->ops->create_instance(fmt, &fic)))
198 return_NULL;
199
200 pvcn = dm_config_find_node(top, "metadata/physical_volumes")->child;
201 while (pvcn) {
202 _pv_populate_lvmcache(cmd, pvcn, 0);
203 pvcn = pvcn->sib;
204 }
205
206 top->key = name;
207 vg = import_vg_from_config_tree(reply.cft, fid);
208
209 dm_list_iterate_items(pvl, &vg->pvs) {
210 if ((info = lvmcache_info_from_pvid((const char *)&pvl->pv->id, 0))) {
211 pvl->pv->label_sector = lvmcache_get_label(info)->sector;
212 pvl->pv->dev = lvmcache_device(info);
213 lvmcache_fid_add_mdas_pv(info, fid);
214 } /* else probably missing */
215 }
216
217 lvmcache_update_vg(vg, 0);
218 }
219
220 daemon_reply_destroy(reply);
221 return vg;
222 }
223
224 struct _fixup_baton {
225 int i;
226 int find;
227 int ignore;
228 };
229
230 static int _fixup_ignored(struct metadata_area *mda, void *baton) {
231 struct _fixup_baton *b = baton;
232 if (b->i == b->find)
233 mda_set_ignored(mda, b->ignore);
234 b->i ++;
235 return 1;
236 }
237
238 int lvmetad_vg_update(struct volume_group *vg)
239 {
240 char *buf = NULL;
241 daemon_reply reply;
242 struct dm_hash_node *n;
243 struct metadata_area *mda;
244 char mda_id[128], *num;
245 struct pv_list *pvl;
246 struct lvmcache_info *info;
247 struct _fixup_baton baton;
248
249 if (!vg)
250 return 0;
251
252 if (!_using_lvmetad)
253 return 1; /* fake it */
254
255 /* TODO. This is not entirely correct, since export_vg_to_buffer
256 * adds trailing nodes to the buffer. We may need to use
257 * export_vg_to_config_tree and format the buffer ourselves. It
258 * does, however, work for now, since the garbage is well
259 * formatted and has no conflicting keys with the rest of the
260 * request. */
261 if (!export_vg_to_buffer(vg, &buf)) {
262 log_error("Could not format VG metadata.");
263 return 0;
264 }
265
266 reply = daemon_send_simple(_lvmetad, "vg_update", "vgname = %s", vg->name,
267 "metadata = %b", strchr(buf, '{'),
268 NULL);
269 dm_free(buf);
270
271 if (!_lvmetad_handle_reply(reply, "update VG", vg->name))
272 return 0;
273
274 n = (vg->fid && vg->fid->metadata_areas_index) ?
275 dm_hash_get_first(vg->fid->metadata_areas_index) : NULL;
276 while (n) {
277 mda = dm_hash_get_data(vg->fid->metadata_areas_index, n);
278 strcpy(mda_id, dm_hash_get_key(vg->fid->metadata_areas_index, n));
279 if ((num = strchr(mda_id, '_'))) {
280 *num = 0;
281 ++num;
282 if ((info = lvmcache_info_from_pvid(mda_id, 0))) {
283 memset(&baton, 0, sizeof(baton));
284 baton.find = atoi(num);
285 baton.ignore = mda_is_ignored(mda);
286 lvmcache_foreach_mda(info, _fixup_ignored, &baton);
287 }
288 }
289 n = dm_hash_get_next(vg->fid->metadata_areas_index, n);
290 }
291
292 dm_list_iterate_items(pvl, &vg->pvs) {
293 /* NB. the PV fmt pointer is sometimes wrong during vgconvert */
294 if (pvl->pv->dev && !lvmetad_pv_found(pvl->pv->id, pvl->pv->dev,
295 vg->fid ? vg->fid->fmt : pvl->pv->fmt,
296 pvl->pv->label_sector, NULL))
297 return 0;
298 }
299
300 return 1;
301 }
302
303 int lvmetad_vg_remove(struct volume_group *vg)
304 {
305 char uuid[64];
306 daemon_reply reply;
307
308 if (!_using_lvmetad)
309 return 1; /* just fake it */
310
311 if (!id_write_format(&vg->id, uuid, sizeof(uuid)))
312 return_0;
313
314 reply = daemon_send_simple(_lvmetad, "vg_remove", "uuid = %s", uuid, NULL);
315
316 return _lvmetad_handle_reply(reply, "remove VG", vg->name);
317 }
318
319 int lvmetad_pv_lookup(struct cmd_context *cmd, struct id pvid)
320 {
321 char uuid[64];
322 daemon_reply reply;
323 int result = 1;
324 struct dm_config_node *cn;
325
326 if (!_using_lvmetad)
327 return_0;
328
329 if (!id_write_format(&pvid, uuid, sizeof(uuid)))
330 return_0;
331
332 reply = daemon_send_simple(_lvmetad, "pv_lookup", "uuid = %s", uuid, NULL);
333
334 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
335 _lvmetad_handle_reply(reply, "lookup PVs", "");
336 return_0;
337 }
338
339 cn = dm_config_find_node(reply.cft->root, "physical_volume");
340 if (!_pv_populate_lvmcache(cmd, cn, 0))
341 result = 0;
342
343 daemon_reply_destroy(reply);
344 return result;
345 }
346
347 int lvmetad_pv_lookup_by_devt(struct cmd_context *cmd, dev_t device)
348 {
349 int result = 1;
350 daemon_reply reply;
351 struct dm_config_node *cn;
352
353 if (!_using_lvmetad)
354 return_0;
355
356 reply = daemon_send_simple(_lvmetad, "pv_lookup", "device = %d", device, NULL);
357
358 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
359 _lvmetad_handle_reply(reply, "lookup PVs", "");
360 return_0;
361 }
362
363 cn = dm_config_find_node(reply.cft->root, "physical_volume");
364 if (!_pv_populate_lvmcache(cmd, cn, device))
365 result = 0;
366
367 daemon_reply_destroy(reply);
368 return result;
369 }
370
371 int lvmetad_pv_list_to_lvmcache(struct cmd_context *cmd)
372 {
373 daemon_reply reply;
374 struct dm_config_node *cn;
375
376 if (!_using_lvmetad)
377 return 1;
378
379 reply = daemon_send_simple(_lvmetad, "pv_list", NULL);
380
381 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
382 _lvmetad_handle_reply(reply, "list PVs", "");
383 return_0;
384 }
385
386 cn = dm_config_find_node(reply.cft->root, "physical_volumes")->child;
387 while (cn) {
388 _pv_populate_lvmcache(cmd, cn, 0);
389 cn = cn->sib;
390 }
391
392 daemon_reply_destroy(reply);
393 return 1;
394 }
395
396 int lvmetad_vg_list_to_lvmcache(struct cmd_context *cmd)
397 {
398 struct volume_group *tmp;
399 struct id vgid;
400 const char *vgid_txt;
401 daemon_reply reply;
402 struct dm_config_node *cn;
403
404 if (!_using_lvmetad)
405 return 1;
406
407 reply = daemon_send_simple(_lvmetad, "vg_list", NULL);
408 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
409 _lvmetad_handle_reply(reply, "list VGs", "");
410 return_0;
411 }
412
413 cn = dm_config_find_node(reply.cft->root, "volume_groups")->child;
414 while (cn) {
415 vgid_txt = cn->key;
416 id_read_format(&vgid, vgid_txt);
417 cn = cn->sib;
418
419 /* the call to lvmetad_vg_lookup will poke the VG into lvmcache */
420 tmp = lvmetad_vg_lookup(cmd, NULL, (const char*)&vgid);
421 release_vg(tmp);
422 }
423
424 daemon_reply_destroy(reply);
425 return 1;
426 }
427
428 struct _print_mda_baton {
429 int i;
430 char *buffer;
431 };
432
433 static int _print_mda(struct metadata_area *mda, void *baton)
434 {
435 int result = 0;
436 struct _print_mda_baton *b = baton;
437 char *buf, *mda_txt;
438
439 if (!mda->ops->mda_export_text) /* do nothing */
440 return 1;
441
442 buf = b->buffer;
443 mda_txt = mda->ops->mda_export_text(mda);
444 if (!dm_asprintf(&b->buffer, "%s mda%i { %s }", b->buffer ?: "", b->i, mda_txt))
445 goto_out;
446 b->i ++;
447 result = 1;
448 out:
449 dm_free(mda_txt);
450 dm_free(buf);
451 return result;
452 }
453
454 static int _print_da(struct disk_locn *da, void *baton)
455 {
456 struct _print_mda_baton *b;
457 char *buf;
458
459 if (!da)
460 return 1;
461
462 b = baton;
463 buf = b->buffer;
464 if (!dm_asprintf(&b->buffer, "%s da%i { offset = %" PRIu64
465 " size = %" PRIu64 " }",
466 b->buffer ?: "", b->i, da->offset, da->size))
467 {
468 dm_free(buf);
469 return_0;
470 }
471 b->i ++;
472 dm_free(buf);
473
474 return 1;
475 }
476
477 static const char *_print_mdas(struct lvmcache_info *info)
478 {
479 struct _print_mda_baton baton = { .i = 0, .buffer = NULL };
480
481 if (!lvmcache_foreach_mda(info, &_print_mda, &baton))
482 return NULL;
483 baton.i = 0;
484 if (!lvmcache_foreach_da(info, &_print_da, &baton))
485 return NULL;
486
487 return baton.buffer;
488 }
489
490 int lvmetad_pv_found(struct id pvid, struct device *device, const struct format_type *fmt,
491 uint64_t label_sector, struct volume_group *vg)
492 {
493 char uuid[64];
494 daemon_reply reply;
495 struct lvmcache_info *info;
496 const char *mdas = NULL;
497 char *pvmeta;
498 char *buf = NULL;
499
500 if (!_using_lvmetad)
501 return 1;
502
503 if (!id_write_format(&pvid, uuid, sizeof(uuid)))
504 return_0;
505
506 /* FIXME A more direct route would be much preferable. */
507 if ((info = lvmcache_info_from_pvid((const char *)&pvid, 0)))
508 mdas = _print_mdas(info);
509
510 if (!dm_asprintf(&pvmeta,
511 "{ device = %" PRIu64 "\n"
512 " dev_size = %" PRIu64 "\n"
513 " format = \"%s\"\n"
514 " label_sector = %" PRIu64 "\n"
515 " id = \"%s\"\n"
516 " %s"
517 "}", device->dev,
518 info ? lvmcache_device_size(info) : 0,
519 fmt->name, label_sector, uuid, mdas ?: "")) {
520 dm_free((char *)mdas);
521 return_0;
522 }
523
524 dm_free((char *)mdas);
525
526 if (vg) {
527 /*
528 * TODO. This is not entirely correct, since export_vg_to_buffer
529 * adds trailing garbage to the buffer. We may need to use
530 * export_vg_to_config_tree and format the buffer ourselves. It
531 * does, however, work for now, since the garbage is well
532 * formatted and has no conflicting keys with the rest of the
533 * request.
534 */
535 if (!export_vg_to_buffer(vg, &buf)) {
536 dm_free(pvmeta);
537 return_0;
538 }
539
540 reply = daemon_send_simple(_lvmetad,
541 "pv_found",
542 "pvmeta = %b", pvmeta,
543 "vgname = %s", vg->name,
544 "metadata = %b", strchr(buf, '{'),
545 NULL);
546 } else {
547 /* There are no MDAs on this PV. */
548 reply = daemon_send_simple(_lvmetad,
549 "pv_found",
550 "pvmeta = %b", pvmeta,
551 NULL);
552 }
553
554 dm_free(pvmeta);
555 return _lvmetad_handle_reply(reply, "update PV", uuid);
556 }
557
558 int lvmetad_pv_gone(dev_t device)
559 {
560 if (!_using_lvmetad)
561 return 1;
562
563 daemon_reply reply =
564 daemon_send_simple(_lvmetad, "pv_gone", "device = %d", device, NULL);
565
566 return _lvmetad_handle_reply(reply, "drop PV", "");
567 }
568
569 int lvmetad_active(void)
570 {
571 return _using_lvmetad;
572 }
573
574 void lvmetad_set_active(int active)
575 {
576 _using_lvmetad = active;
577 }
578
579 /*
580 * The following code implements pvscan --lvmetad.
581 */
582
583 struct _pvscan_lvmetad_baton {
584 struct volume_group *vg;
585 struct format_instance *fid;
586 };
587
588 static int _pvscan_lvmetad_single(struct metadata_area *mda, void *baton)
589 {
590 struct _pvscan_lvmetad_baton *b = baton;
591 struct volume_group *this = mda->ops->vg_read(b->fid, "", mda);
592 if (!b->vg || this->seqno > b->vg->seqno)
593 b->vg = this;
594 else if (b->vg)
595 release_vg(this);
596 return 1;
597 }
598
599 static dev_t _parse_devt(const char *str) { /* Oh. */
600 char *where = (char *) str;
601 int major = strtol(str, &where, 10);
602 int minor;
603
604 if (where == str)
605 return -1;
606
607 if (*where != ':')
608 return -1;
609
610 str = ++where;
611 minor = strtol(str, &where, 10);
612
613 if (where == str)
614 return -1;
615
616 if (*where)
617 return -1;
618
619 return MKDEV(major, minor);
620 }
621
622 int pvscan_lvmetad(struct cmd_context *cmd, int argc, char **argv)
623 {
624 struct device *dev;
625 struct label *label;
626 struct lvmcache_info *info;
627 struct physical_volume pv;
628 struct _pvscan_lvmetad_baton baton;
629 /* Create a dummy instance. */
630 struct format_instance_ctx fic = { .type = 0 };
631
632 if (argc != 1) {
633 log_error("Exactly one device parameter required.");
634 return 0;
635 }
636
637 if (!lvmetad_active()) {
638 log_error("Cannot proceed since lvmetad is not active.");
639 return 0;
640 }
641
642 dev = dev_cache_get(argv[0], NULL);
643 if (!dev && _parse_devt(argv[0]) != -1)
644 dev = dev_cache_get_by_devt(_parse_devt(argv[0]), NULL);
645
646 if (!dev) {
647 if (_parse_devt(argv[0]) == -1) {
648 log_error("For devices that do not exist, we need a MAJOR:MINOR pair.");
649 return 0;
650 }
651
652 if (!lvmetad_pv_gone(_parse_devt(argv[0])))
653 goto fatal;
654
655 log_info("Device %s not found and was wiped from lvmetad.", argv[0]);
656 return 1;
657 }
658
659 if (!label_read(dev, &label, 0)) {
660 log_warn("No PV label found on %s.", dev_name(dev));
661 if (!lvmetad_pv_gone(dev->dev))
662 goto fatal;
663 return 1;
664 }
665
666 info = (struct lvmcache_info *) label->info;
667 memset(&pv, 0, sizeof(pv));
668
669 baton.vg = NULL;
670 baton.fid = lvmcache_fmt(info)->ops->create_instance(lvmcache_fmt(info),
671 &fic);
672
673 lvmcache_foreach_mda(info, _pvscan_lvmetad_single, &baton);
674 if (!baton.vg)
675 lvmcache_fmt(info)->ops->destroy_instance(baton.fid);
676
677 /*
678 * NB. If this command failed and we are relying on lvmetad to have an
679 * *exact* image of the system, the lvmetad instance that went out of
680 * sync needs to be killed.
681 */
682 if (!lvmetad_pv_found(*(struct id *)dev->pvid, dev, lvmcache_fmt(info),
683 label->sector, baton.vg))
684 goto fatal;
685
686 release_vg(baton.vg);
687 return 1;
688 fatal:
689 release_vg(baton.vg);
690 /* FIXME kill lvmetad automatically if we can */
691 log_error("Update of lvmetad failed. This is a serious problem.\n "
692 "It is strongly recommended that you restart lvmetad immediately.");
693 return 0;
694 }
This page took 0.069228 seconds and 6 git commands to generate.