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