]> sourceware.org Git - lvm2.git/blob - lib/cache/lvmetad.c
Check id_write_format result
[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 || _lvmetad.error) {
20 log_warn("Failed to connect to lvmetad: %s. Falling back to scanning.", strerror(_lvmetad.error));
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 if (!id_write_format((const struct id*)vgid, uuid, sizeof(uuid)))
157 return_0;
158 reply = daemon_send_simple(_lvmetad, "vg_lookup", "uuid = %s", uuid, NULL);
159 } else {
160 if (!vgname)
161 log_error(INTERNAL_ERROR "VG name required (VGID not available)");
162 reply = daemon_send_simple(_lvmetad, "vg_lookup", "name = %s", vgname, NULL);
163 }
164
165 if (!strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
166
167 top = dm_config_find_node(reply.cft->root, "metadata");
168 name = daemon_reply_str(reply, "name", NULL);
169
170 /* fall back to lvm2 if we don't know better */
171 fmt_name = dm_config_find_str(top, "metadata/format", "lvm2");
172 if (!(fmt = get_format_by_name(cmd, fmt_name))) {
173 log_error(INTERNAL_ERROR
174 "We do not know the format (%s) reported by lvmetad.",
175 fmt_name);
176 return NULL;
177 }
178
179 fic.type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS;
180 fic.context.vg_ref.vg_name = name;
181 fic.context.vg_ref.vg_id = vgid;
182
183 if (!(fid = fmt->ops->create_instance(fmt, &fic)))
184 return_NULL;
185
186 pvcn = dm_config_find_node(top, "metadata/physical_volumes")->child;
187 while (pvcn) {
188 _pv_populate_lvmcache(cmd, pvcn, 0);
189 pvcn = pvcn->sib;
190 }
191
192 top->key = name;
193 vg = import_vg_from_config_tree(reply.cft, fid);
194
195 dm_list_iterate_items(pvl, &vg->pvs) {
196 if ((info = lvmcache_info_from_pvid((const char *)&pvl->pv->id, 0))) {
197 pvl->pv->label_sector = lvmcache_get_label(info)->sector;
198 pvl->pv->dev = lvmcache_device(info);
199 lvmcache_fid_add_mdas_pv(info, fid);
200 } /* else probably missing */
201 }
202
203 lvmcache_update_vg(vg, 0);
204 }
205
206 daemon_reply_destroy(reply);
207 return vg;
208 }
209
210 struct _fixup_baton {
211 int i;
212 int find;
213 int ignore;
214 };
215
216 static int _fixup_ignored(struct metadata_area *mda, void *baton) {
217 struct _fixup_baton *b = baton;
218 if (b->i == b->find)
219 mda_set_ignored(mda, b->ignore);
220 b->i ++;
221 return 1;
222 }
223
224 int lvmetad_vg_update(struct volume_group *vg)
225 {
226 char *buf = NULL;
227 daemon_reply reply;
228 struct dm_hash_node *n;
229 struct metadata_area *mda;
230 char mda_id[128], *num;
231 struct pv_list *pvl;
232 struct lvmcache_info *info;
233 struct _fixup_baton baton;
234
235 if (!vg)
236 return 0;
237
238 if (!_using_lvmetad)
239 return 1; /* fake it */
240
241 /* TODO. This is not entirely correct, since export_vg_to_buffer
242 * adds trailing nodes to the buffer. We may need to use
243 * export_vg_to_config_tree and format the buffer ourselves. It
244 * does, however, work for now, since the garbage is well
245 * formatted and has no conflicting keys with the rest of the
246 * request. */
247 if (!export_vg_to_buffer(vg, &buf)) {
248 log_error("Could not format VG metadata.");
249 return 0;
250 }
251
252 reply = daemon_send_simple(_lvmetad, "vg_update", "vgname = %s", vg->name,
253 "metadata = %b", strchr(buf, '{'),
254 NULL);
255 dm_free(buf);
256
257 if (!_lvmetad_handle_reply(reply, "update VG", vg->name))
258 return 0;
259
260 n = (vg->fid && vg->fid->metadata_areas_index) ?
261 dm_hash_get_first(vg->fid->metadata_areas_index) : NULL;
262 while (n) {
263 mda = dm_hash_get_data(vg->fid->metadata_areas_index, n);
264 strcpy(mda_id, dm_hash_get_key(vg->fid->metadata_areas_index, n));
265 if ((num = strchr(mda_id, '_'))) {
266 *num = 0;
267 ++num;
268 if ((info = lvmcache_info_from_pvid(mda_id, 0))) {
269 memset(&baton, 0, sizeof(baton));
270 baton.find = atoi(num);
271 baton.ignore = mda_is_ignored(mda);
272 lvmcache_foreach_mda(info, _fixup_ignored, &baton);
273 }
274 }
275 n = dm_hash_get_next(vg->fid->metadata_areas_index, n);
276 }
277
278 dm_list_iterate_items(pvl, &vg->pvs) {
279 /* NB. the PV fmt pointer is sometimes wrong during vgconvert */
280 if (pvl->pv->dev && !lvmetad_pv_found(pvl->pv->id, pvl->pv->dev,
281 vg->fid ? vg->fid->fmt : pvl->pv->fmt,
282 pvl->pv->label_sector, NULL))
283 return 0;
284 }
285
286 return 1;
287 }
288
289 int lvmetad_vg_remove(struct volume_group *vg)
290 {
291 char uuid[64];
292 daemon_reply reply;
293
294 if (!_using_lvmetad)
295 return 1; /* just fake it */
296
297 if (!id_write_format(&vg->id, uuid, sizeof(uuid)))
298 return_0;
299
300 reply = daemon_send_simple(_lvmetad, "vg_remove", "uuid = %s", uuid, NULL);
301
302 return _lvmetad_handle_reply(reply, "remove VG", vg->name);
303 }
304
305 int lvmetad_pv_lookup(struct cmd_context *cmd, struct id pvid)
306 {
307 char uuid[64];
308 daemon_reply reply;
309 int result = 1;
310 struct dm_config_node *cn;
311
312 if (!_using_lvmetad)
313 return_0;
314
315 if (!id_write_format(&pvid, uuid, sizeof(uuid)))
316 return_0;
317
318 reply = daemon_send_simple(_lvmetad, "pv_lookup", "uuid = %s", uuid, NULL);
319
320 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
321 _lvmetad_handle_reply(reply, "lookup PVs", "");
322 return_0;
323 }
324
325 cn = dm_config_find_node(reply.cft->root, "physical_volume");
326 if (!_pv_populate_lvmcache(cmd, cn, 0))
327 result = 0;
328
329 daemon_reply_destroy(reply);
330 return result;
331 }
332
333 int lvmetad_pv_lookup_by_devt(struct cmd_context *cmd, dev_t device)
334 {
335 int result = 1;
336 daemon_reply reply;
337 struct dm_config_node *cn;
338
339 if (!_using_lvmetad)
340 return_0;
341
342 reply = daemon_send_simple(_lvmetad, "pv_lookup", "device = %d", device, NULL);
343
344 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
345 _lvmetad_handle_reply(reply, "lookup PVs", "");
346 return_0;
347 }
348
349 cn = dm_config_find_node(reply.cft->root, "physical_volume");
350 if (!_pv_populate_lvmcache(cmd, cn, device))
351 result = 0;
352
353 daemon_reply_destroy(reply);
354 return result;
355 }
356
357 int lvmetad_pv_list_to_lvmcache(struct cmd_context *cmd)
358 {
359 daemon_reply reply;
360 struct dm_config_node *cn;
361
362 if (!_using_lvmetad)
363 return_0;
364
365 reply = daemon_send_simple(_lvmetad, "pv_list", NULL);
366
367 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
368 _lvmetad_handle_reply(reply, "list PVs", "");
369 return_0;
370 }
371
372 cn = dm_config_find_node(reply.cft->root, "physical_volumes")->child;
373 while (cn) {
374 _pv_populate_lvmcache(cmd, cn, 0);
375 cn = cn->sib;
376 }
377
378 daemon_reply_destroy(reply);
379 return 1;
380 }
381
382 int lvmetad_vg_list_to_lvmcache(struct cmd_context *cmd)
383 {
384 struct volume_group *tmp;
385 struct id vgid;
386 const char *vgid_txt;
387 daemon_reply reply;
388 struct dm_config_node *cn;
389
390 if (!_using_lvmetad)
391 return_0;
392
393 reply = daemon_send_simple(_lvmetad, "vg_list", NULL);
394 if (reply.error || strcmp(daemon_reply_str(reply, "response", ""), "OK")) {
395 _lvmetad_handle_reply(reply, "list VGs", "");
396 return_0;
397 }
398
399 cn = dm_config_find_node(reply.cft->root, "volume_groups")->child;
400 while (cn) {
401 vgid_txt = cn->key;
402 id_read_format(&vgid, vgid_txt);
403 cn = cn->sib;
404
405 /* the call to lvmetad_vg_lookup will poke the VG into lvmcache */
406 tmp = lvmetad_vg_lookup(cmd, NULL, (const char*)&vgid);
407 release_vg(tmp);
408 }
409
410 daemon_reply_destroy(reply);
411 return 1;
412 }
413
414 struct _print_mda_baton {
415 int i;
416 char *buffer;
417 };
418
419 static int _print_mda(struct metadata_area *mda, void *baton)
420 {
421 int result = 0;
422 struct _print_mda_baton *b = baton;
423 char *buf, *mda_txt;
424
425 if (!mda->ops->mda_export_text) /* do nothing */
426 return 1;
427
428 buf = b->buffer;
429 mda_txt = mda->ops->mda_export_text(mda);
430 if (!dm_asprintf(&b->buffer, "%s mda%i { %s }", b->buffer ?: "", b->i, mda_txt))
431 goto_out;
432 b->i ++;
433 result = 1;
434 out:
435 dm_free(mda_txt);
436 dm_free(buf);
437 return result;
438 }
439
440 static int _print_da(struct disk_locn *da, void *baton)
441 {
442 struct _print_mda_baton *b;
443 char *buf;
444
445 if (!da)
446 return 1;
447
448 b = baton;
449 buf = b->buffer;
450 if (!dm_asprintf(&b->buffer, "%s da%i { offset = %" PRIu64
451 " size = %" PRIu64 " }",
452 b->buffer ?: "", b->i, da->offset, da->size))
453 {
454 dm_free(buf);
455 return_0;
456 }
457 b->i ++;
458 dm_free(buf);
459
460 return 1;
461 }
462
463 static const char *_print_mdas(struct lvmcache_info *info)
464 {
465 struct _print_mda_baton baton = { .i = 0, .buffer = NULL };
466
467 if (!lvmcache_foreach_mda(info, &_print_mda, &baton))
468 return NULL;
469 baton.i = 0;
470 if (!lvmcache_foreach_da(info, &_print_da, &baton))
471 return NULL;
472
473 return baton.buffer;
474 }
475
476 int lvmetad_pv_found(struct id pvid, struct device *device, const struct format_type *fmt,
477 uint64_t label_sector, struct volume_group *vg)
478 {
479 char uuid[64];
480 daemon_reply reply;
481 struct lvmcache_info *info;
482 const char *mdas = NULL;
483 char *pvmeta;
484 char *buf = NULL;
485
486 if (!_using_lvmetad)
487 return 1;
488
489 if (!id_write_format(&pvid, uuid, sizeof(uuid)))
490 return_0;
491
492 /* FIXME A more direct route would be much preferable. */
493 if ((info = lvmcache_info_from_pvid((const char *)&pvid, 0)))
494 mdas = _print_mdas(info);
495
496 if (!dm_asprintf(&pvmeta,
497 "{ device = %" PRIu64 "\n"
498 " dev_size = %" PRIu64 "\n"
499 " format = \"%s\"\n"
500 " label_sector = %" PRIu64 "\n"
501 " id = \"%s\"\n"
502 " %s"
503 "}", device->dev,
504 info ? lvmcache_device_size(info) : 0,
505 fmt->name, label_sector, uuid, mdas ?: "")) {
506 dm_free((char *)mdas);
507 return_0;
508 }
509
510 dm_free((char *)mdas);
511
512 if (vg) {
513 /*
514 * TODO. This is not entirely correct, since export_vg_to_buffer
515 * adds trailing garbage to the buffer. We may need to use
516 * export_vg_to_config_tree and format the buffer ourselves. It
517 * does, however, work for now, since the garbage is well
518 * formatted and has no conflicting keys with the rest of the
519 * request.
520 */
521 export_vg_to_buffer(vg, &buf);
522 reply = daemon_send_simple(_lvmetad,
523 "pv_found",
524 "pvmeta = %b", pvmeta,
525 "vgname = %s", vg->name,
526 "metadata = %b", strchr(buf, '{'),
527 NULL);
528 } else {
529 /* There are no MDAs on this PV. */
530 reply = daemon_send_simple(_lvmetad,
531 "pv_found",
532 "pvmeta = %b", pvmeta,
533 NULL);
534 }
535
536 dm_free(pvmeta);
537 return _lvmetad_handle_reply(reply, "update PV", uuid);
538 }
539
540 int lvmetad_pv_gone(dev_t device)
541 {
542 if (!_using_lvmetad)
543 return 1;
544
545 daemon_reply reply =
546 daemon_send_simple(_lvmetad, "pv_gone", "device = %d", device, NULL);
547
548 return _lvmetad_handle_reply(reply, "drop PV", "");
549 }
550
551 int lvmetad_active(void)
552 {
553 return _using_lvmetad;
554 }
555
556 void lvmetad_set_active(int active)
557 {
558 _using_lvmetad = active;
559 }
560
561 /*
562 * The following code implements pvscan --lvmetad.
563 */
564
565 struct _pvscan_lvmetad_baton {
566 struct volume_group *vg;
567 struct format_instance *fid;
568 };
569
570 static int _pvscan_lvmetad_single(struct metadata_area *mda, void *baton)
571 {
572 struct _pvscan_lvmetad_baton *b = baton;
573 struct volume_group *this = mda->ops->vg_read(b->fid, "", mda);
574 if (!b->vg || this->seqno > b->vg->seqno)
575 b->vg = this;
576 else if (b->vg)
577 release_vg(this);
578 return 1;
579 }
580
581 static dev_t _parse_devt(const char *str) { /* Oh. */
582 char *where = (char *) str;
583 int major = strtol(str, &where, 10);
584 int minor;
585
586 if (where == str)
587 return -1;
588
589 if (*where != ':')
590 return -1;
591
592 str = ++where;
593 minor = strtol(str, &where, 10);
594
595 if (where == str)
596 return -1;
597
598 if (*where)
599 return -1;
600
601 return MKDEV(major, minor);
602 }
603
604 int pvscan_lvmetad(struct cmd_context *cmd, int argc, char **argv)
605 {
606 struct device *dev;
607 struct label *label;
608 struct lvmcache_info *info;
609 struct physical_volume pv;
610 struct _pvscan_lvmetad_baton baton;
611 /* Create a dummy instance. */
612 struct format_instance_ctx fic = { .type = 0 };
613
614 if (argc != 1) {
615 log_error("Exactly one device parameter required.");
616 return 0;
617 }
618
619 if (!lvmetad_active()) {
620 log_error("Cannot proceed since lvmetad is not active.");
621 return 0;
622 }
623
624 dev = dev_cache_get(argv[0], NULL);
625 if (!dev && _parse_devt(argv[0]) != -1)
626 dev = dev_cache_get_by_devt(_parse_devt(argv[0]), NULL);
627
628 if (!dev) {
629 if (_parse_devt(argv[0]) == -1) {
630 log_error("For devices that do not exist, we need a MAJOR:MINOR pair.");
631 return 0;
632 }
633
634 if (!lvmetad_pv_gone(_parse_devt(argv[0])))
635 goto fatal;
636
637 log_info("Device %s not found and was wiped from lvmetad.", argv[0]);
638 return 1;
639 }
640
641 if (!label_read(dev, &label, 0)) {
642 log_warn("No PV label found on %s.", dev_name(dev));
643 if (!lvmetad_pv_gone(dev->dev))
644 goto fatal;
645 return 1;
646 }
647
648 info = (struct lvmcache_info *) label->info;
649 memset(&pv, 0, sizeof(pv));
650
651 baton.vg = NULL;
652 baton.fid = lvmcache_fmt(info)->ops->create_instance(lvmcache_fmt(info),
653 &fic);
654
655 lvmcache_foreach_mda(info, _pvscan_lvmetad_single, &baton);
656 if (!baton.vg)
657 lvmcache_fmt(info)->ops->destroy_instance(baton.fid);
658
659 /*
660 * NB. If this command failed and we are relying on lvmetad to have an
661 * *exact* image of the system, the lvmetad instance that went out of
662 * sync needs to be killed.
663 */
664 if (!lvmetad_pv_found(*(struct id *)dev->pvid, dev, lvmcache_fmt(info),
665 label->sector, baton.vg))
666 goto fatal;
667
668 release_vg(baton.vg);
669 return 1;
670 fatal:
671 release_vg(baton.vg);
672 /* FIXME kill lvmetad automatically if we can */
673 log_error("Update of lvmetad failed. This is a serious problem.\n "
674 "It is strongly recommended that you restart lvmetad immediately.");
675 return 0;
676 }
677
This page took 0.079843 seconds and 6 git commands to generate.