summaryrefslogtreecommitdiff
path: root/tools/reporter.c
blob: e62858f42dc58ef4bbaf20be42a4a1eb49cc7d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
/*
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "tools.h"

#include "lib/report/report.h"

typedef enum {
	REPORT_IDX_NULL = -1,
	REPORT_IDX_SINGLE,
	REPORT_IDX_LOG,
	REPORT_IDX_FULL_VGS,
	REPORT_IDX_FULL_LVS,
	REPORT_IDX_FULL_PVS,
	REPORT_IDX_FULL_PVSEGS,
	REPORT_IDX_FULL_SEGS,
	REPORT_IDX_COUNT
} report_idx_t;

#define REPORT_IDX_FULL_START REPORT_IDX_FULL_VGS

struct single_report_args {
	report_type_t report_type;
	char report_prefix[32];
	const char *report_name;
	int args_are_pvs;
	const char *keys;
	const char *options;
	const char *fields_to_compact;
	const char *selection;
};

/* TODO: configure these common report args only once per cmd */
struct report_args {
	int argc;
	char **argv;
	dm_report_group_type_t report_group_type;
	report_type_t report_type;
	int aligned;
	int buffered;
	int headings;
	int field_prefixes;
	int quoted;
	int columns_as_rows;
	const char *separator;
	struct volume_group *full_report_vg;
	int log_only;
	struct single_report_args single_args[REPORT_IDX_COUNT];
};

static int _process_each_devtype(struct cmd_context *cmd, int argc,
				 struct processing_handle *handle)
{
	if (argc)
		log_warn("WARNING: devtypes currently ignores command line arguments.");

	if (!report_devtypes(handle->custom_handle))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

static int _vgs_single(struct cmd_context *cmd __attribute__((unused)),
		       const char *vg_name, struct volume_group *vg,
		       struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   vg, NULL, NULL, NULL, NULL, NULL, NULL))
		return_ECMD_FAILED;

	check_current_backup(vg);

	return ECMD_PROCESSED;
}

static int _do_info_and_status(struct cmd_context *cmd,
				const struct lv_segment *lv_seg,
				struct lv_with_info_and_seg_status *status,
				int do_info, int do_status)
{
	status->lv = lv_seg->lv;

	if (lv_is_historical(lv_seg->lv))
		return 1;

	if (do_status) {
		if (!(status->seg_status.mem = dm_pool_create("reporter_pool", 1024)))
			return_0;

		if (do_info)
			/* both info and status */
			status->info_ok = lv_info_with_seg_status(cmd, lv_seg, status, 1, 1);
		else
			status->info_ok = lv_info_with_seg_status(cmd, lv_seg, status, 0, 0);
	} else if (do_info)
		/* info only */
		status->info_ok = lv_info(cmd, status->lv, 0, &status->info, 1, 1);

	return 1;
}

/* Check if this is really merging origin.
 * In such case, origin is gone, and user should see
 * only data from merged snapshot. Important for thin. */
static int _check_merging_origin(const struct logical_volume *lv,
				 struct lv_with_info_and_seg_status *status,
				 int *merged)
{
	uint32_t device_id;

	*merged = 0;

	switch (status->seg_status.type) {
	case SEG_STATUS_THIN:
		/* Get 'device_id' from active dm-table */
		if (!lv_thin_device_id(lv, &device_id))
			return_0;

		if (lv->snapshot->device_id != device_id)
			return 1;
		break;
	case SEG_STATUS_SNAPSHOT:
		break;
	default:
		/* When inactive, it's technically merging */
		if (status->info_ok && !status->info.exists)
			break;
		return 1;
	}

	/* Origin is gone */
	log_debug_activation("Merge is in progress, reporting merged LV %s.",
			     display_lvname(lv->snapshot->lv));
	*merged = 1;

	return 1;
}

static int _do_lvs_with_info_and_status_single(struct cmd_context *cmd,
					       const struct logical_volume *lv,
					       int do_info, int do_status,
					       struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;
	struct lv_with_info_and_seg_status status = {
		.seg_status.type = SEG_STATUS_NONE
	};
	int r = ECMD_FAILED;
	int merged;

	if (lv_is_merging_origin(lv))
		/* Status is need to know which LV should be shown */
		do_status = 1;

	if (!_do_info_and_status(cmd, first_seg(lv), &status, do_info, do_status))
		goto_out;

	if (lv_is_merging_origin(lv)) {
		if (!_check_merging_origin(lv, &status, &merged))
		      goto_out;
		if (merged && lv_is_thin_volume(lv->snapshot->lv))
			lv = lv->snapshot->lv;
	}

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   lv->vg, lv, NULL, NULL, NULL, &status, NULL))
		goto out;

	r = ECMD_PROCESSED;
out:
	if (status.seg_status.mem)
		dm_pool_destroy(status.seg_status.mem);

	return r;
}

static int _lvs_single(struct cmd_context *cmd, struct logical_volume *lv,
		       struct processing_handle *handle)
{
	return _do_lvs_with_info_and_status_single(cmd, lv, 0, 0, handle);
}

static int _lvs_with_info_single(struct cmd_context *cmd, struct logical_volume *lv,
				 struct processing_handle *handle)
{
	return _do_lvs_with_info_and_status_single(cmd, lv, 1, 0, handle);
}

static int _lvs_with_status_single(struct cmd_context *cmd, struct logical_volume *lv,
				   struct processing_handle *handle)
{
	return _do_lvs_with_info_and_status_single(cmd, lv, 0, 1, handle);
}

static int _lvs_with_info_and_status_single(struct cmd_context *cmd, struct logical_volume *lv,
					    struct processing_handle *handle)
{
	return _do_lvs_with_info_and_status_single(cmd, lv, 1, 1, handle);
}

static int _do_segs_with_info_and_status_single(struct cmd_context *cmd,
						const struct lv_segment *seg,
						int do_info, int do_status,
						struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;
	struct lv_with_info_and_seg_status status = {
		.seg_status.type = SEG_STATUS_NONE
	};
	int r = ECMD_FAILED;
	int merged;

	if (lv_is_merging_origin(seg->lv))
		/* Status is need to know which LV should be shown */
		do_status = 1;

	if (!_do_info_and_status(cmd, seg, &status, do_info, do_status))
		goto_out;

	if (lv_is_merging_origin(seg->lv)) {
		if (!_check_merging_origin(seg->lv, &status, &merged))
			goto_out;
		if (merged && lv_is_thin_volume(seg->lv->snapshot->lv))
			seg = seg->lv->snapshot;
	}

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   seg->lv->vg, seg->lv, NULL, seg, NULL, &status, NULL))
		goto_out;

	r = ECMD_PROCESSED;
out:
	if (status.seg_status.mem)
		dm_pool_destroy(status.seg_status.mem);

	return r;
}

static int _segs_single(struct cmd_context *cmd, struct lv_segment *seg,
			struct processing_handle *handle)
{
	return _do_segs_with_info_and_status_single(cmd, seg, 0, 0, handle);
}

static int _segs_with_info_single(struct cmd_context *cmd, struct lv_segment *seg,
				  struct processing_handle *handle)
{
	return _do_segs_with_info_and_status_single(cmd, seg, 1, 0, handle);
}

static int _segs_with_status_single(struct cmd_context *cmd, struct lv_segment *seg,
				    struct processing_handle *handle)
{
	return _do_segs_with_info_and_status_single(cmd, seg, 0, 1, handle);
}

static int _segs_with_info_and_status_single(struct cmd_context *cmd, struct lv_segment *seg,
					     struct processing_handle *handle)
{
	return _do_segs_with_info_and_status_single(cmd, seg, 1, 1, handle);
}

static int _lvsegs_single(struct cmd_context *cmd, struct logical_volume *lv,
			  struct processing_handle *handle)
{
	if (!arg_is_set(cmd, all_ARG) && !lv_is_visible(lv))
		return ECMD_PROCESSED;

	return process_each_segment_in_lv(cmd, lv, handle, _segs_single);
}

static int _lvsegs_with_info_single(struct cmd_context *cmd, struct logical_volume *lv,
				    struct processing_handle *handle)
{
	if (!arg_is_set(cmd, all_ARG) && !lv_is_visible(lv))
		return ECMD_PROCESSED;

	return process_each_segment_in_lv(cmd, lv, handle, _segs_with_info_single);
}

static int _lvsegs_with_status_single(struct cmd_context *cmd, struct logical_volume *lv,
				      struct processing_handle *handle)
{
	if (!arg_is_set(cmd, all_ARG) && !lv_is_visible(lv))
		return ECMD_PROCESSED;

	return process_each_segment_in_lv(cmd, lv, handle, _segs_with_status_single);
}

static int _lvsegs_with_info_and_status_single(struct cmd_context *cmd, struct logical_volume *lv,
					       struct processing_handle *handle)
{
	if (!arg_is_set(cmd, all_ARG) && !lv_is_visible(lv))
		return ECMD_PROCESSED;

	return process_each_segment_in_lv(cmd, lv, handle, _segs_with_info_and_status_single);
}

static int _do_pvsegs_sub_single(struct cmd_context *cmd,
				 struct volume_group *vg,
				 struct pv_segment *pvseg,
				 int do_info,
				 int do_status,
				 struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;
	int ret = ECMD_PROCESSED;
	struct lv_segment *seg = pvseg->lvseg;

	struct segment_type _freeseg_type = {
		.flags = SEG_VIRTUAL | SEG_CANNOT_BE_ZEROED,
		.name = "free",
	};

	struct volume_group _free_vg = {
		.cmd = cmd,
		.name = "",
		.pvs = DM_LIST_HEAD_INIT(_free_vg.pvs),
		.lvs = DM_LIST_HEAD_INIT(_free_vg.lvs),
		.historical_lvs = DM_LIST_HEAD_INIT(_free_vg.historical_lvs),
		.tags = DM_LIST_HEAD_INIT(_free_vg.tags),
	};

	struct logical_volume _free_logical_volume = {
		.name = "",
		.vg = vg ?: &_free_vg,
		.status = VISIBLE_LV,
		.major = -1,
		.minor = -1,
		.snapshot_segs = DM_LIST_HEAD_INIT(_free_logical_volume.snapshot_segs),
		.segments = DM_LIST_HEAD_INIT(_free_logical_volume.segments),
		.tags = DM_LIST_HEAD_INIT(_free_logical_volume.tags),
		.segs_using_this_lv = DM_LIST_HEAD_INIT(_free_logical_volume.segs_using_this_lv),
		.indirect_glvs = DM_LIST_HEAD_INIT(_free_logical_volume.indirect_glvs),
	};

	struct lv_segment _free_lv_segment = {
		.lv = &_free_logical_volume,
		.segtype = &_freeseg_type,
		.len = pvseg->len,
		.origin_list = DM_LIST_HEAD_INIT(_free_lv_segment.origin_list),
		.tags = DM_LIST_HEAD_INIT(_free_lv_segment.tags),
	};

	struct lv_with_info_and_seg_status status = {
		.seg_status.type = SEG_STATUS_NONE,
		.lv = &_free_logical_volume
	};

	if (seg && !_do_info_and_status(cmd, seg, &status, do_info, do_status))
		goto_out;

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   vg, seg ? seg->lv : &_free_logical_volume,
			   pvseg->pv, seg ? : &_free_lv_segment, pvseg,
			   &status, pv_label(pvseg->pv))) {
		ret = ECMD_FAILED;
		goto_out;
	}

 out:
	if (status.seg_status.mem)
		dm_pool_destroy(status.seg_status.mem);

	return ret;
}

static int _pvsegs_sub_single(struct cmd_context *cmd,
			      struct volume_group *vg,
			      struct pv_segment *pvseg,
			      struct processing_handle *handle)
{
	return _do_pvsegs_sub_single(cmd, vg, pvseg, 0, 0, handle);
}

static int _pvsegs_with_lv_info_sub_single(struct cmd_context *cmd,
					   struct volume_group *vg,
					   struct pv_segment *pvseg,
					   struct processing_handle *handle)
{
	return _do_pvsegs_sub_single(cmd, vg, pvseg, 1, 0, handle);
}

static int _pvsegs_with_lv_status_sub_single(struct cmd_context *cmd,
					     struct volume_group *vg,
					     struct pv_segment *pvseg,
					     struct processing_handle *handle)
{
	return _do_pvsegs_sub_single(cmd, vg, pvseg, 0, 1, handle);
}

static int _pvsegs_with_lv_info_and_status_sub_single(struct cmd_context *cmd,
						      struct volume_group *vg,
						      struct pv_segment *pvseg,
						      struct processing_handle *handle)
{
	return _do_pvsegs_sub_single(cmd, vg, pvseg, 1, 1, handle);
}

static int _pvsegs_single(struct cmd_context *cmd,
			  struct volume_group *vg,
			  struct physical_volume *pv,
			  struct processing_handle *handle)
{
	return process_each_segment_in_pv(cmd, vg, pv, handle, _pvsegs_sub_single);
}

static int _pvsegs_with_lv_info_single(struct cmd_context *cmd,
				       struct volume_group *vg,
				       struct physical_volume *pv,
				       struct processing_handle *handle)
{
	return process_each_segment_in_pv(cmd, vg, pv, handle, _pvsegs_with_lv_info_sub_single);
}

static int _pvsegs_with_lv_status_single(struct cmd_context *cmd,
					 struct volume_group *vg,
					 struct physical_volume *pv,
					 struct processing_handle *handle)
{
	return process_each_segment_in_pv(cmd, vg, pv, handle, _pvsegs_with_lv_status_sub_single);
}

static int _pvsegs_with_lv_info_and_status_single(struct cmd_context *cmd,
						  struct volume_group *vg,
						  struct physical_volume *pv,
						  struct processing_handle *handle)
{
	return process_each_segment_in_pv(cmd, vg, pv, handle, _pvsegs_with_lv_info_and_status_sub_single);
}

static int _pvs_single(struct cmd_context *cmd, struct volume_group *vg,
		       struct physical_volume *pv,
		       struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   vg, NULL, pv, NULL, NULL, NULL, NULL))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

static int _label_single(struct cmd_context *cmd, struct label *label,
		         struct processing_handle *handle)
{
	struct selection_handle *sh = handle->selection_handle;

	if (!report_object(sh ? : handle->custom_handle, sh != NULL,
			   NULL, NULL, NULL, NULL, NULL, NULL, label))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

static int _pvs_in_vg(struct cmd_context *cmd, const char *vg_name,
		      struct volume_group *vg,
		      struct processing_handle *handle)
{
	return process_each_pv_in_vg(cmd, vg, handle, &_pvs_single);
}

static int _pvsegs_in_vg(struct cmd_context *cmd, const char *vg_name,
			 struct volume_group *vg,
			 struct processing_handle *handle)
{
	return process_each_pv_in_vg(cmd, vg, handle, &_pvsegs_single);
}

static int _get_final_report_type(struct report_args *args,
				  struct single_report_args *single_args,
				  report_type_t report_type,
				  int *lv_info_needed,
				  int *lv_segment_status_needed,
				  report_type_t *final_report_type)
{
	/* Do we need to acquire LV device info in addition? */
	*lv_info_needed = (report_type & (LVSINFO | LVSINFOSTATUS)) ? 1 : 0;

	/* Do we need to acquire LV device status in addition? */
	*lv_segment_status_needed = (report_type & (LVSSTATUS | LVSINFOSTATUS)) ? 1 : 0;

	/* Ensure options selected are compatible */
	if (report_type & SEGS)
		report_type |= LVS;
	if (report_type & PVSEGS)
		report_type |= PVS;
	if ((report_type & (LVS | LVSINFO | LVSSTATUS | LVSINFOSTATUS)) &&
	    (report_type & (PVS | LABEL)) && !(single_args->args_are_pvs || (args->full_report_vg && single_args->report_type == PVSEGS))) {
		log_error("Can't report LV and PV fields at the same time in %sreport type \"%s\"%s%s.",
			  args->full_report_vg ? "sub" : "" , single_args->report_prefix,
			  args->full_report_vg ? " in VG " : "",
			  args->full_report_vg ? args->full_report_vg->name: "");
		return 0;
	}

	/* Change report type if fields specified makes this necessary */
	if (report_type & FULL)
		report_type = FULL;
	else if ((report_type & PVSEGS) ||
		 ((report_type & (PVS | LABEL)) && (report_type & (LVS | LVSINFO | LVSSTATUS | LVSINFOSTATUS))))
		report_type = PVSEGS;
	else if ((report_type & PVS) ||
		 ((report_type & LABEL) && (report_type & VGS)))
		report_type = PVS;
	else if (report_type & SEGS)
		report_type = SEGS;
	else if (report_type & (LVS | LVSINFO | LVSSTATUS | LVSINFOSTATUS))
		report_type = LVS;

	if (args->full_report_vg && (report_type != single_args->report_type)) {
		/* FIXME: Tell user about which columns exactly are incorrectly used for that report type... */
		log_error("Subreport of type \"%s\" for VG %s contains columns which lead to change of report type. "
			  "Add these columns to proper subreport type.", single_args->report_prefix, args->full_report_vg->name);
		return 0;
	}

	*final_report_type = report_type;
	return 1;
}

static int _report_all_in_vg(struct cmd_context *cmd, struct processing_handle *handle,
			     struct volume_group *vg, report_type_t type,
			     int do_lv_info, int do_lv_seg_status)
{
	int r = 0;

	switch (type) {
		case VGS:
			r = _vgs_single(cmd, vg->name, vg, handle);
			break;
		case LVS:
			r = process_each_lv_in_vg(cmd, vg, NULL, NULL, 0, handle, NULL,
						  do_lv_info && !do_lv_seg_status ? &_lvs_with_info_single :
						  !do_lv_info && do_lv_seg_status ? &_lvs_with_status_single :
						  do_lv_info && do_lv_seg_status ? &_lvs_with_info_and_status_single :
										   &_lvs_single);
			break;
		case SEGS:
			r = process_each_lv_in_vg(cmd, vg, NULL, NULL, 0, handle, NULL,
						  do_lv_info && !do_lv_seg_status ? &_lvsegs_with_info_single :
						  !do_lv_info && do_lv_seg_status ? &_lvsegs_with_status_single :
						  do_lv_info && do_lv_seg_status ? &_lvsegs_with_info_and_status_single :
										   &_lvsegs_single);
			break;
		case PVS:
			r = process_each_pv_in_vg(cmd, vg, handle, &_pvs_single);
			break;
		case PVSEGS:
			r = process_each_pv_in_vg(cmd, vg, handle,
						  do_lv_info && !do_lv_seg_status ? &_pvsegs_with_lv_info_single :
						  !do_lv_info && do_lv_seg_status ? &_pvsegs_with_lv_status_single :
						  do_lv_info && do_lv_seg_status ? &_pvsegs_with_lv_info_and_status_single :
										   &_pvsegs_single);
			break;
		default:
			log_error(INTERNAL_ERROR "_report_all_in_vg: incorrect report type");
			break;
	}

	return r;
}

static int _report_all_in_lv(struct cmd_context *cmd, struct processing_handle *handle,
			     struct logical_volume *lv, report_type_t type,
			     int do_lv_info, int do_lv_seg_status)
{
	int r = 0;

	switch (type) {
		case LVS:
			r = _do_lvs_with_info_and_status_single(cmd, lv, do_lv_info, do_lv_seg_status, handle);
			break;
		case SEGS:
			r = process_each_segment_in_lv(cmd, lv, handle,
						       do_lv_info && !do_lv_seg_status ? &_segs_with_info_single :
							       !do_lv_info && do_lv_seg_status ? &_segs_with_status_single :
							       do_lv_info && do_lv_seg_status ? &_segs_with_info_and_status_single :
												&_segs_single);
			break;
		default:
			log_error(INTERNAL_ERROR "_report_all_in_lv: incorrect report type");
			break;
	}

	return r;
}

static int _report_all_in_pv(struct cmd_context *cmd, struct processing_handle *handle,
			     struct physical_volume *pv, report_type_t type,
			     int do_lv_info, int do_lv_seg_status)
{
	int r = 0;

	switch (type) {
		case PVS:
			r = _pvs_single(cmd, pv->vg, pv, handle);
			break;
		case PVSEGS:
			r = process_each_segment_in_pv(cmd, pv->vg, pv, handle,
						       do_lv_info && !do_lv_seg_status ? &_pvsegs_with_lv_info_sub_single :
						       !do_lv_info && do_lv_seg_status ? &_pvsegs_with_lv_status_sub_single :
						       do_lv_info && do_lv_seg_status ? &_pvsegs_with_lv_info_and_status_sub_single :
											&_pvsegs_sub_single);
			break;
		default:
			log_error(INTERNAL_ERROR "_report_all_in_pv: incorrect report type");
			break;
	}

	return r;
}

int report_for_selection(struct cmd_context *cmd,
			 struct processing_handle *parent_handle,
			 struct physical_volume *pv,
			 struct volume_group *vg,
			 struct logical_volume *lv)
{
	struct selection_handle *sh = parent_handle->selection_handle;
	struct report_args args = {0};
	struct single_report_args *single_args = &args.single_args[REPORT_IDX_SINGLE];
	int do_lv_info, do_lv_seg_status;
	struct processing_handle *handle;
	int r = 0;

	single_args->report_type = sh->orig_report_type | sh->report_type;
	single_args->args_are_pvs = sh->orig_report_type == PVS;

	if (!_get_final_report_type(&args, single_args,
				    single_args->report_type,
				    &do_lv_info, &do_lv_seg_status,
				    &sh->report_type))
		return_0;

	if (!(handle = init_processing_handle(cmd, parent_handle)))
		return_0;

	/*
	 * We're already reporting for select so override
	 * internal_report_for_select to 0 as we can call
	 * process_each_* functions again and we could
	 * end up in an infinite loop if we didn't stop
	 * internal reporting for select right here.
	 *
	 * So the overall call trace from top to bottom looks like this:
	 *
	 * process_each_* (top-level one, using processing_handle with internal reporting enabled and selection_handle) ->
	 *   select_match_*(processing_handle with selection_handle) ->
	 *     report for selection ->
	 *     	 (creating new processing_handle here with internal reporting disabled!!!)
	 *       reporting_fn OR process_each_* (using *new* processing_handle with original selection_handle) 
	 *
	 * The selection_handle is still reused so we can track
	 * whether any of the items the top-level one is composed
	 * of are still selected or not unerneath. Do not destroy
	 * this selection handle - it needs to be passed to upper
	 * layers to check the overall selection status.
	 */
	handle->internal_report_for_select = 0;
	handle->selection_handle = sh;

	/*
	 * Remember:
	 *   sh->orig_report_type is the original report type requested (what are we selecting? PV/VG/LV?)
	 *   sh->report_type is the report type actually used (it counts with all types of fields used in selection criteria)
	 */
	switch (sh->orig_report_type) {
		case LVS:
			r = _report_all_in_lv(cmd, handle, lv, sh->report_type, do_lv_info, do_lv_seg_status);
			break;
		case VGS:
			r = _report_all_in_vg(cmd, handle, vg, sh->report_type, do_lv_info, do_lv_seg_status);
			break;
		case PVS:
			r = _report_all_in_pv(cmd, handle, pv, sh->report_type, do_lv_info, do_lv_seg_status);
			break;
		default:
			log_error(INTERNAL_ERROR "report_for_selection: incorrect report type");
			break;
	}

	/*
	 * Keep the selection handle provided from the caller -
	 * do not destroy it - the caller will still use it to
	 * pass the result through it to layers above.
	 */
	handle->selection_handle = NULL;
	destroy_processing_handle(cmd, handle);
	return r;
}

static void _check_pv_list(struct cmd_context *cmd, struct report_args *args, struct single_report_args *single_args)
{
	int i;

	if (!args->argv)
		return;

	single_args->args_are_pvs = (single_args->report_type == PVS ||
				     single_args->report_type == LABEL ||
				     single_args->report_type == PVSEGS) ? 1 : 0;

	if (single_args->args_are_pvs && args->argc) {
		for (i = 0; i < args->argc; i++) {
			if (*args->argv[i] == '@') {
				/*
				 * Tags are metadata related, not label
				 * related, change report type accordingly!
				 */
				if (single_args->report_type == LABEL)
					single_args->report_type = PVS;
				break;
			}
		}
	}
}

static void _del_option_from_list(struct dm_list *sll, const char *prefix,
				  size_t prefix_len, const char *str)
{
	struct dm_list *slh;
	struct dm_str_list *sl;
	const char *a = str, *b;

	dm_list_uniterate(slh, sll, sll) {
		sl = dm_list_item(slh, struct dm_str_list);

		/* exact match */
		if (!strcmp(str, sl->str)) {
			dm_list_del(slh);
			return;
		}

		/* also try to match with known prefix */
		b = sl->str;
		if (!strncmp(prefix, a, prefix_len)) {
			a += prefix_len;
			if (*a == '_')
				a++;
		}
		if (!strncmp(prefix, b, prefix_len)) {
			b += prefix_len;
			if (*b == '_')
				b++;
		}
		if (!strcmp(a, b)) {
			dm_list_del(slh);
			return;
		}
	}
}

#define _get_report_idx(report_type,single_report_type) \
	((((report_type) != FULL) && ((report_type) == single_report_type)) ? REPORT_IDX_SINGLE : REPORT_IDX_FULL_ ## single_report_type)

static report_idx_t _get_report_idx_from_name(report_type_t report_type, const char *name)
{
	report_idx_t idx;

	if (!name || !*name)
		return REPORT_IDX_NULL;

	/* Change to basic report type for comparison. */
	if ((report_type == LABEL) || (report_type == PVSEGS))
		report_type = PVS;
	else if (report_type == SEGS)
		report_type = LVS;

	if (!strcasecmp(name, "log"))
		idx = REPORT_IDX_LOG;
	else if (!strcasecmp(name, "vg"))
		idx = _get_report_idx(report_type, VGS);
	else if (!strcasecmp(name, "pv"))
		idx = _get_report_idx(report_type, PVS);
	else if (!strcasecmp(name, "lv"))
		idx = _get_report_idx(report_type, LVS);
	else if (!strcasecmp(name, "pvseg")) {
		idx = (report_type == FULL) ? _get_report_idx(report_type, PVSEGS)
					    : _get_report_idx(report_type, PVS);
	} else if (!strcasecmp(name, "seg"))
		idx = (report_type == FULL) ? _get_report_idx(report_type, SEGS)
					    : _get_report_idx(report_type, LVS);
	else {
		idx = REPORT_IDX_NULL;
		log_error("Unknonwn report specifier in "
			  "report option list: %s.", name);
	}

	return idx;
}

static int _should_process_report_idx(report_type_t report_type, int allow_single, report_idx_t idx)
{
	if (((idx == REPORT_IDX_LOG) && (report_type != CMDLOG)) ||
	    ((idx == REPORT_IDX_SINGLE) && !allow_single) ||
	    ((idx >= REPORT_IDX_FULL_START) && report_type != FULL))
		return 0;

	return 1;
}

enum opts_list_type {
	OPTS_REPLACE,
	OPTS_ADD,
	OPTS_REMOVE,
	OPTS_COMPACT
};

static int _get_report_options(struct cmd_context *cmd,
			       struct report_args *args,
			       struct single_report_args *single_args)
{
	int action;
	struct arg_value_group_list *current_group;
	struct dm_list *final_opts_list[REPORT_IDX_COUNT] = { NULL };
	struct dm_list *opts_list = NULL;
	struct dm_str_list *sl;
	struct dm_pool *mem;
	const char *report_name = NULL;
	const char *opts;
	report_idx_t idx = REPORT_IDX_SINGLE;
	int r = ECMD_FAILED;

	if (!arg_is_set(cmd, options_ARG))
		return ECMD_PROCESSED;

	if (!(mem = dm_pool_create("report_options", 128))) {
		log_error("Failed to create temporary mempool to process report options.");
		return ECMD_FAILED;
	}

	if (single_args->report_type == CMDLOG) {
		if (!(final_opts_list[REPORT_IDX_LOG] = str_to_str_list(mem, single_args->options, ",", 1)))
			goto_out;
	} else if (single_args->report_type == FULL) {
		if (!(final_opts_list[REPORT_IDX_FULL_VGS] = str_to_str_list(mem, args->single_args[REPORT_IDX_FULL_VGS].options, ",", 1)) ||
		    !(final_opts_list[REPORT_IDX_FULL_PVS] = str_to_str_list(mem, args->single_args[REPORT_IDX_FULL_PVS].options, ",", 1)) ||
		    !(final_opts_list[REPORT_IDX_FULL_LVS] = str_to_str_list(mem, args->single_args[REPORT_IDX_FULL_LVS].options, ",", 1)) ||
		    !(final_opts_list[REPORT_IDX_FULL_PVSEGS] = str_to_str_list(mem, args->single_args[REPORT_IDX_FULL_PVSEGS].options, ",", 1)) ||
		    !(final_opts_list[REPORT_IDX_FULL_SEGS] = str_to_str_list(mem, args->single_args[REPORT_IDX_FULL_SEGS].options, ",", 1)))
			goto_out;
	} else {
		if (!(final_opts_list[REPORT_IDX_SINGLE] = str_to_str_list(mem, single_args->options, ",", 1)))
			goto_out;
	}

	dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
		if (!grouped_arg_is_set(current_group->arg_values, options_ARG))
			continue;

		if (grouped_arg_is_set(current_group->arg_values, configreport_ARG)) {
			report_name = grouped_arg_str_value(current_group->arg_values, configreport_ARG, NULL);
			if ((idx = _get_report_idx_from_name(single_args->report_type, report_name)) == REPORT_IDX_NULL)
				goto_out;
		}

		opts = grouped_arg_str_value(current_group->arg_values, options_ARG, NULL);
		if (!opts || !*opts) {
			log_error("Invalid options string: %s", opts);
			r = EINVALID_CMD_LINE;
			goto out;
		}

		switch (*opts) {
			case '+':
				action = OPTS_ADD;
				opts++;
				break;
			case '-':
				action = OPTS_REMOVE;
				opts++;
				break;
			case '#':
				action = OPTS_COMPACT;
				opts++;
				break;
			default:
				action = OPTS_REPLACE;
		}

		if (!_should_process_report_idx(single_args->report_type, !(single_args->report_type & (CMDLOG | FULL)), idx))
			continue;

		if ((action != OPTS_COMPACT) &&
		    !(opts_list = str_to_str_list(mem, opts, ",", 1)))
			goto_out;

		switch (action) {
			case OPTS_ADD:
				dm_list_splice(final_opts_list[idx], opts_list);
				break;
			case OPTS_REMOVE:
				dm_list_iterate_items(sl, opts_list)
					_del_option_from_list(final_opts_list[idx], args->single_args[idx].report_prefix,
							      strlen(args->single_args[idx].report_prefix), sl->str);
				break;
			case OPTS_COMPACT:
				args->single_args[idx].fields_to_compact = opts;
				break;
			case OPTS_REPLACE:
				final_opts_list[idx] = opts_list;
				break;
		}
	}

	if (single_args->report_type == CMDLOG) {
		if (!(single_args->options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_LOG], ",")))
			goto_out;
	} else if (single_args->report_type == FULL) {
		if (!(args->single_args[REPORT_IDX_FULL_VGS].options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_FULL_VGS], ",")) ||
		    !(args->single_args[REPORT_IDX_FULL_PVS].options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_FULL_PVS], ",")) ||
		    !(args->single_args[REPORT_IDX_FULL_LVS].options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_FULL_LVS], ",")) ||
		    !(args->single_args[REPORT_IDX_FULL_PVSEGS].options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_FULL_PVSEGS], ",")) ||
		    !(args->single_args[REPORT_IDX_FULL_SEGS].options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_FULL_SEGS], ",")))
			goto_out;
	} else {
		if (!(single_args->options = str_list_to_str(cmd->mem, final_opts_list[REPORT_IDX_SINGLE], ",")))
			goto_out;
	}

	r = ECMD_PROCESSED;
out:
	dm_pool_destroy(mem);
	return r;
}

static int _get_report_keys(struct cmd_context *cmd,
			    struct report_args *args,
			    struct single_report_args *single_args)
{
	struct arg_value_group_list *current_group;
	const char *report_name = NULL;
	report_idx_t idx = REPORT_IDX_SINGLE;
	int r = ECMD_FAILED;

	dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
		if (!grouped_arg_is_set(current_group->arg_values, sort_ARG))
			continue;

		if (grouped_arg_is_set(current_group->arg_values, configreport_ARG)) {
			report_name = grouped_arg_str_value(current_group->arg_values, configreport_ARG, NULL);
			if ((idx = _get_report_idx_from_name(single_args->report_type, report_name)) == REPORT_IDX_NULL)
				goto_out;
		}

		if (!_should_process_report_idx(single_args->report_type, !(single_args->report_type & (CMDLOG | FULL)), idx))
			continue;

		args->single_args[idx].keys = grouped_arg_str_value(current_group->arg_values, sort_ARG, NULL);
	}

	r = ECMD_PROCESSED;
out:
	return r;
}

static int _do_report_get_selection(struct cmd_context *cmd,
				    report_type_t report_type,
				    int allow_single,
				    struct report_args *args,
				    const char **last_selection)
{
	struct arg_value_group_list *current_group;
	const char *final_selection = NULL, *selection = NULL;
	const char *report_name = NULL;
	report_idx_t idx = REPORT_IDX_SINGLE;

	dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
		if (!grouped_arg_is_set(current_group->arg_values, select_ARG))
			continue;

		if (grouped_arg_is_set(current_group->arg_values, configreport_ARG)) {
			report_name = grouped_arg_str_value(current_group->arg_values, configreport_ARG, NULL);
			if ((idx = _get_report_idx_from_name(report_type, report_name)) == REPORT_IDX_NULL)
				return_0;
		}

		selection = grouped_arg_str_value(current_group->arg_values, select_ARG, NULL);

		if (!_should_process_report_idx(report_type, allow_single, idx))
			continue;
		if (args)
			args->single_args[idx].selection = selection;
		final_selection = selection;
	}

	if (last_selection)
		*last_selection = final_selection;

	return 1;
}

static int _get_report_selection(struct cmd_context *cmd,
				 struct report_args *args,
				 struct single_report_args *single_args)
{
	return _do_report_get_selection(cmd, single_args->report_type, !(single_args->report_type & (CMDLOG | FULL)),
					args, NULL) ? ECMD_PROCESSED : ECMD_FAILED;
}

int report_get_single_selection(struct cmd_context *cmd, report_type_t report_type, const char **selection)
{
	return _do_report_get_selection(cmd, report_type, 1, NULL, selection);
}

static int _set_report_prefix_and_name(struct report_args *args,
				       struct single_report_args *single_args)
{
	const char *report_prefix, *report_desc;
	size_t len;

	if (single_args->report_type == FULL) {
		single_args->report_prefix[0] = '\0';
		single_args->report_name = single_args->report_prefix;
		return 1;
	}

	(void) report_get_prefix_and_desc(single_args->report_type,
					  &report_prefix, &report_desc);
	len = strlen(report_prefix);
	if (report_prefix[len - 1] == '_')
		len--;

	if (!len) {
		log_error(INTERNAL_ERROR "_set_report_prefix_and_name: no prefix "
			  "found for report type 0x%x", single_args->report_type);
		return 0;
	}

	if (!dm_strncpy(single_args->report_prefix, report_prefix, sizeof(single_args->report_prefix))) {
		log_error("_set_report_prefix_and_name: dm_strncpy failed");
		return 0;
	}
	single_args->report_prefix[len] = '\0';

	if (args->report_group_type != DM_REPORT_GROUP_BASIC)
		single_args->report_name = single_args->report_prefix;
	else
		single_args->report_name = report_desc;

	return 1;
}

static int _do_report(struct cmd_context *cmd, struct processing_handle *handle,
		      struct report_args *args, struct single_report_args *single_args)
{
	void *orig_custom_handle = handle->custom_handle;
	report_type_t report_type = single_args->report_type;
	void *report_handle = NULL;
	int lv_info_needed;
	int lv_segment_status_needed;
	int report_in_group = 0;
	int r = ECMD_FAILED;

	if (!(report_handle = report_init(cmd, single_args->options, single_args->keys, &report_type,
					  args->separator, args->aligned, args->buffered,
					  args->headings, args->field_prefixes, args->quoted,
					  args->columns_as_rows, single_args->selection, 0)))
		goto_out;

	handle->custom_handle = report_handle;

	if (!_get_final_report_type(args, single_args, report_type, &lv_info_needed,
				    &lv_segment_status_needed, &report_type))
		goto_out;

	if (!(args->log_only && (single_args->report_type != CMDLOG))) {
		if (!dm_report_group_push(cmd->cmd_report.report_group, report_handle, (void *) single_args->report_name))
			goto_out;
		report_in_group = 1;
	}

	switch (report_type) {
		case DEVTYPES:
			r = _process_each_devtype(cmd, args->argc, handle);
			break;
		case LVSINFO:
			/* fall through */
		case LVSSTATUS:
			/* fall through */
		case LVSINFOSTATUS:
			/* fall through */
		case LVS:
			if (args->full_report_vg)
				r = _report_all_in_vg(cmd, handle, args->full_report_vg, LVS, lv_info_needed, lv_segment_status_needed);
			else
				r = process_each_lv(cmd, args->argc, args->argv, NULL, NULL, 0, handle, NULL,
						    lv_info_needed && !lv_segment_status_needed ? &_lvs_with_info_single :
						    !lv_info_needed && lv_segment_status_needed ? &_lvs_with_status_single :
						    lv_info_needed && lv_segment_status_needed ? &_lvs_with_info_and_status_single :
												 &_lvs_single);
			break;
		case VGS:
			if (args->full_report_vg)
				r = _report_all_in_vg(cmd, handle, args->full_report_vg, VGS, lv_info_needed, lv_segment_status_needed);
			else
				r = process_each_vg(cmd, args->argc, args->argv, NULL, NULL,
						    0, 0, handle, &_vgs_single);
			break;
		case LABEL:
			r = process_each_label(cmd, args->argc, args->argv,
					       handle, &_label_single);
			break;
		case PVS:
			if (args->full_report_vg)
				r = _report_all_in_vg(cmd, handle, args->full_report_vg, PVS, lv_info_needed, lv_segment_status_needed);
			else {
				if (single_args->args_are_pvs)
					r = process_each_pv(cmd, args->argc, args->argv, NULL,
							    arg_is_set(cmd, all_ARG), 0,
							    handle, &_pvs_single);
				else
					r = process_each_vg(cmd, args->argc, args->argv, NULL, NULL,
							    0, 0, handle, &_pvs_in_vg);
			}
			break;
		case SEGS:
			if (args->full_report_vg)
				r = _report_all_in_vg(cmd, handle, args->full_report_vg, SEGS, lv_info_needed, lv_segment_status_needed);
			else
				r = process_each_lv(cmd, args->argc, args->argv, NULL, NULL, 0, handle, NULL,
						    lv_info_needed && !lv_segment_status_needed ? &_lvsegs_with_info_single :
						    !lv_info_needed && lv_segment_status_needed ? &_lvsegs_with_status_single :
						    lv_info_needed && lv_segment_status_needed ? &_lvsegs_with_info_and_status_single :
												 &_lvsegs_single);
			break;
		case PVSEGS:
			if (args->full_report_vg)
				r = _report_all_in_vg(cmd, handle, args->full_report_vg, PVSEGS, lv_info_needed, lv_segment_status_needed);
			else {
				if (single_args->args_are_pvs)
					r = process_each_pv(cmd, args->argc, args->argv, NULL,
							    arg_is_set(cmd, all_ARG), 0,
							    handle,
							    lv_info_needed && !lv_segment_status_needed ? &_pvsegs_with_lv_info_single :
							    !lv_info_needed && lv_segment_status_needed ? &_pvsegs_with_lv_status_single :
							    lv_info_needed && lv_segment_status_needed ? &_pvsegs_with_lv_info_and_status_single :
												 &_pvsegs_single);
				else
					r = process_each_vg(cmd, args->argc, args->argv, NULL, NULL,
							    0, 0, handle, &_pvsegs_in_vg);
			}
			break;
		case FULL:
			/*
			 * Full report's subreports already covered by combinations above with args->full_report_vg.
			 * We shouldn't see report_type == FULL in this function.
			 */
			log_error(INTERNAL_ERROR "_do_report: full report requested at incorrect level");
			break;
		case CMDLOG:
			/* Log is reported throughout the code via report_cmdlog calls. */
			break;
		default:
			log_error(INTERNAL_ERROR "_do_report: unknown report type.");
			return 0;
	}

	if (find_config_tree_bool(cmd, report_compact_output_CFG, NULL)) {
		if (!dm_report_compact_fields(report_handle))
			log_error("Failed to compact report output.");
	} else if (single_args->fields_to_compact) {
		if (!dm_report_compact_given_fields(report_handle, single_args->fields_to_compact))
			log_error("Failed to compact given columns in report output.");
	}

	if (!(args->log_only && (single_args->report_type != CMDLOG)))
		dm_report_output(report_handle);

out:
	if (report_handle) {
		if (report_in_group && !dm_report_group_pop(cmd->cmd_report.report_group))
			stack;
		dm_report_free(report_handle);
	}

	handle->custom_handle = orig_custom_handle;
	return r;
}

static int _full_report_single(struct cmd_context *cmd,
			       const char *vg_name,
			       struct volume_group *vg,
			       struct processing_handle *handle)
{
	struct report_args *args = (struct report_args *) handle->custom_handle;
	int orphan = is_orphan_vg(vg->name);
	int r = ECMD_FAILED;

	if (orphan && dm_list_empty(&vg->pvs))
		return ECMD_PROCESSED;

	args->full_report_vg = vg;

	if (!args->log_only && !dm_report_group_push(cmd->cmd_report.report_group, NULL, NULL))
		goto out;

	if (orphan) {
		if (((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_PVS])) != ECMD_PROCESSED) ||
		    ((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_PVSEGS])) != ECMD_PROCESSED))
			stack;
	} else {
		if (((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_VGS])) != ECMD_PROCESSED) ||
		    ((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_PVS])) != ECMD_PROCESSED) ||
		    ((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_LVS])) != ECMD_PROCESSED) ||
		    ((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_PVSEGS])) != ECMD_PROCESSED) ||
		    ((r = _do_report(cmd, handle, args, &args->single_args[REPORT_IDX_FULL_SEGS])) != ECMD_PROCESSED))
			stack;
	}

	if (!args->log_only && !dm_report_group_pop(cmd->cmd_report.report_group))
		goto_out;
out:
	args->full_report_vg = NULL;
	return r;
}

#define _set_full_report_single(cmd,args,type,name) \
	do { \
		(args)->single_args[REPORT_IDX_FULL_ ## type].report_type = type; \
		(args)->single_args[REPORT_IDX_FULL_ ## type].keys = find_config_tree_str(cmd, report_ ## name ## _sort_full_CFG, NULL); \
		(args)->single_args[REPORT_IDX_FULL_ ## type].options = find_config_tree_str(cmd, report_ ## name ## _cols_full_CFG, NULL); \
		if (!_set_report_prefix_and_name((args), &(args)->single_args[REPORT_IDX_FULL_ ## type])) \
			return_0; \
	} while (0)

static int _config_report(struct cmd_context *cmd, struct report_args *args, struct single_report_args *single_args)
{
	args->aligned = find_config_tree_bool(cmd, report_aligned_CFG, NULL);
	args->buffered = find_config_tree_bool(cmd, report_buffered_CFG, NULL);
	args->headings = find_config_tree_bool(cmd, report_headings_CFG, NULL);
	args->separator = find_config_tree_str(cmd, report_separator_CFG, NULL);
	args->field_prefixes = find_config_tree_bool(cmd, report_prefixes_CFG, NULL);
	args->quoted = find_config_tree_bool(cmd, report_quoted_CFG, NULL);
	args->columns_as_rows = find_config_tree_bool(cmd, report_columns_as_rows_CFG, NULL);

	/* Check PV specifics and do extra changes/actions if needed. */
	_check_pv_list(cmd, args, single_args);

	if (!_set_report_prefix_and_name(args, single_args))
		return_0;

	switch (single_args->report_type) {
		case DEVTYPES:
			single_args->keys = find_config_tree_str(cmd, report_devtypes_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_devtypes_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_devtypes_cols_verbose_CFG, NULL);
			break;
		case LVS:
			single_args->keys = find_config_tree_str(cmd, report_lvs_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_lvs_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_lvs_cols_verbose_CFG, NULL);
			break;
		case VGS:
			single_args->keys = find_config_tree_str(cmd, report_vgs_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_vgs_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_vgs_cols_verbose_CFG, NULL);
			break;
		case LABEL:
		case PVS:
			single_args->keys = find_config_tree_str(cmd, report_pvs_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_pvs_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_pvs_cols_verbose_CFG, NULL);
			break;
		case SEGS:
			single_args->keys = find_config_tree_str(cmd, report_segs_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_segs_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_segs_cols_verbose_CFG, NULL);
			break;
		case PVSEGS:
			single_args->keys = find_config_tree_str(cmd, report_pvsegs_sort_CFG, NULL);
			if (!arg_is_set(cmd, verbose_ARG))
				single_args->options = find_config_tree_str(cmd, report_pvsegs_cols_CFG, NULL);
			else
				single_args->options = find_config_tree_str(cmd, report_pvsegs_cols_verbose_CFG, NULL);
			break;
		case FULL:
			_set_full_report_single(cmd, args, VGS, vgs);
			_set_full_report_single(cmd, args, LVS, lvs);
			_set_full_report_single(cmd, args, PVS, pvs);
			_set_full_report_single(cmd, args, PVSEGS, pvsegs);
			_set_full_report_single(cmd, args, SEGS, segs);
			break;
		case CMDLOG:
			single_args->keys = find_config_tree_str(cmd, log_command_log_sort_CFG, NULL);
			single_args->options = find_config_tree_str(cmd, log_command_log_cols_CFG, NULL);
			single_args->selection = find_config_tree_str(cmd, log_command_log_selection_CFG, NULL);
			break;
		default:
			log_error(INTERNAL_ERROR "_report: unknown report type.");
			return 0;
	}

	if (single_args->report_type != FULL)
		single_args->fields_to_compact = find_config_tree_str_allow_empty(cmd, report_compact_output_cols_CFG, NULL);

	/* If -o supplied use it, else use default for report_type */
	if ((_get_report_options(cmd, args, single_args) != ECMD_PROCESSED))
		return_0;

	/* -O overrides default sort settings */
	if ((_get_report_keys(cmd, args, single_args) != ECMD_PROCESSED))
		return_0;

	if ((_get_report_selection(cmd, args, single_args) != ECMD_PROCESSED))
		return_0;

	args->separator = arg_str_value(cmd, separator_ARG, args->separator);
	if (arg_is_set(cmd, separator_ARG))
		args->aligned = 0;
	if (arg_is_set(cmd, aligned_ARG))
		args->aligned = 1;
	if (arg_is_set(cmd, unbuffered_ARG) && !arg_is_set(cmd, sort_ARG))
		args->buffered = 0;
	if (arg_is_set(cmd, noheadings_ARG))
		args->headings = 0;
	if (arg_is_set(cmd, nameprefixes_ARG)) {
		args->aligned = 0;
		args->field_prefixes = 1;
	}
	if (arg_is_set(cmd, unquoted_ARG))
		args->quoted = 0;
	if (arg_is_set(cmd, rows_ARG))
		args->columns_as_rows = 1;

	return 1;
}

static int _report(struct cmd_context *cmd, int argc, char **argv, report_type_t report_type)
{
	struct report_args args = {0};
	struct single_report_args *single_args = &args.single_args[REPORT_IDX_SINGLE];
	static char report_name[] = "report";
	struct processing_handle *handle;
	int r;

	/*
	 * Include foreign VGs that contain active LVs.
	 * That shouldn't happen in general, but if it does by some
	 * mistake, then we want to display those VGs and allow the
	 * LVs to be deactivated.
	 */
	cmd->include_active_foreign_vgs = 1;

	args.argc = argc;
	args.argv = argv;
	single_args->report_type = report_type;

	if (!(handle = init_processing_handle(cmd, NULL)))
		return_ECMD_FAILED;

	handle->internal_report_for_select = 0;
	handle->include_historical_lvs = cmd->include_historical_lvs;

	args.report_group_type = cmd->cmd_report.report_group_type;
	args.log_only = cmd->cmd_report.log_only;

	if (!_config_report(cmd, &args, single_args)) {
		destroy_processing_handle(cmd, handle);
		return_ECMD_FAILED;
	}

	if (!args.log_only && !dm_report_group_push(cmd->cmd_report.report_group, NULL, report_name)) {
		log_error("Failed to add main report section to report group.");
		destroy_processing_handle(cmd, handle);
		return ECMD_FAILED;
	}

	if (single_args->report_type == FULL) {
		handle->custom_handle = &args;
		r = process_each_vg(cmd, argc, argv, NULL, NULL, 0, 1, handle, &_full_report_single);
	} else
		r = _do_report(cmd, handle, &args, single_args);

	if (!args.log_only && !dm_report_group_pop(cmd->cmd_report.report_group)) {
		log_error("Failed to finalize main report section in report group.");
		r = ECMD_FAILED;
	}

	destroy_processing_handle(cmd, handle);
	return r;
}

int lvs(struct cmd_context *cmd, int argc, char **argv)
{
	report_type_t type;

	if (arg_is_set(cmd, segments_ARG))
		type = SEGS;
	else
		type = LVS;

	return _report(cmd, argc, argv, type);
}

int vgs(struct cmd_context *cmd, int argc, char **argv)
{
	return _report(cmd, argc, argv, VGS);
}

int pvs(struct cmd_context *cmd, int argc, char **argv)
{
	report_type_t type;

	/*
	 * Without -a, command only looks at PVs and can use hints,
	 * with -a, the command looks at all (non-hinted) devices.
	 */
	if (arg_is_set(cmd, all_ARG))
		cmd->use_hints = 0;

	if (arg_is_set(cmd, segments_ARG))
		type = PVSEGS;
	else
		type = LABEL;

	return _report(cmd, argc, argv, type);
}

int fullreport(struct cmd_context *cmd, int argc, char **argv)
{
	return _report(cmd, argc, argv, FULL);
}

int devtypes(struct cmd_context *cmd, int argc, char **argv)
{
	return _report(cmd, argc, argv, DEVTYPES);
}

#define REPORT_FORMAT_NAME_BASIC "basic"
#define REPORT_FORMAT_NAME_JSON "json"
#define REPORT_FORMAT_NAME_JSON_STD "json_std"

int report_format_init(struct cmd_context *cmd)
{
	int config_set = find_config_tree_node(cmd, report_output_format_CFG, NULL) != NULL;
	const char *config_format_str = find_config_tree_str(cmd, report_output_format_CFG, NULL);
	const char *format_str = arg_str_value(cmd, reportformat_ARG, config_set ? config_format_str : NULL);
	int report_command_log;
	struct report_args args = {0};
	struct single_report_args *single_args;
	struct dm_report_group *new_report_group;
	struct dm_report *tmp_log_rh = NULL;

	args.log_only = arg_is_set(cmd, logonly_ARG);
	report_command_log = args.log_only || find_config_tree_bool(cmd, log_report_command_log_CFG, NULL);

	if (!format_str || !strcmp(format_str, REPORT_FORMAT_NAME_BASIC)) {
		args.report_group_type = (report_command_log && !args.log_only) ? DM_REPORT_GROUP_BASIC
										: DM_REPORT_GROUP_SINGLE;
	} else if (!strcmp(format_str, REPORT_FORMAT_NAME_JSON)) {
		args.report_group_type = DM_REPORT_GROUP_JSON;
	} else if (!strcmp(format_str, REPORT_FORMAT_NAME_JSON_STD)) {
		args.report_group_type = DM_REPORT_GROUP_JSON_STD;
	} else {
		log_error("%s: unknown report format.", format_str);
		log_error("Supported report formats: %s, %s, %s.",
			  REPORT_FORMAT_NAME_BASIC,
			  REPORT_FORMAT_NAME_JSON,
			  REPORT_FORMAT_NAME_JSON_STD);
		return 0;
	}

	cmd->cmd_report.report_group_type = args.report_group_type;
	cmd->cmd_report.log_only = args.log_only;

	if (!(new_report_group = dm_report_group_create(args.report_group_type, NULL))) {
		log_error("Failed to create report group.");
		return 0;
	}

	/*
	 * JSON_STD requires strict type mode. That means all NUM and BIN
	 * fields are always reported as numeric values and not strings which
	 * are synonyms to these numeric values.
	 */
	if (args.report_group_type == DM_REPORT_GROUP_JSON_STD)
		cmd->report_strict_type_mode = 1;
	else
		cmd->report_strict_type_mode = 0;

	if (report_command_log) {
		single_args = &args.single_args[REPORT_IDX_LOG];
		single_args->report_type = CMDLOG;

		if (!_config_report(cmd, &args, single_args))
			goto_bad;

		if (!(tmp_log_rh = report_init(NULL, single_args->options, single_args->keys, &single_args->report_type,
						  args.separator, args.aligned, args.buffered, args.headings,
						  args.field_prefixes, args.quoted, args.columns_as_rows,
						  single_args->selection, 1))) {
			log_error("Failed to create log report.");
			goto bad;
		}

		if (!(dm_report_group_push(new_report_group, tmp_log_rh, (void *) single_args->report_name))) {
			log_error("Failed to add log report to report group.");
			goto bad;
		}

		cmd->cmd_report.log_rh = tmp_log_rh;
		if (!(cmd->cmd_report.log_name = dm_pool_strdup(cmd->libmem, single_args->report_name))) {
			log_error("Failed to set log report name for command context.");
			goto bad;
		}
	}

	cmd->cmd_report.report_group = new_report_group;
	cmd->cmd_report.saved_log_report_state = log_get_report_state();
	log_set_report(cmd->cmd_report.log_rh);

	return 1;
bad:
	if (!dm_report_group_destroy(new_report_group))
		stack;
	if (tmp_log_rh)
		dm_report_free(tmp_log_rh);
	return 0;
}

int lastlog(struct cmd_context *cmd, int argc __attribute((unused)), char **argv __attribute__((unused)))
{
	const char *selection;

	if (!cmd->cmd_report.log_rh) {
		log_error("No log report stored.");
		return ECMD_FAILED;
	}

	if (!_do_report_get_selection(cmd, CMDLOG, 1, NULL, &selection))
		return_ECMD_FAILED;

	if (!dm_report_set_selection(cmd->cmd_report.log_rh, selection)) {
		log_error("Failed to set selection for log report.");
		return ECMD_FAILED;
	}

	return ECMD_PROCESSED;
}