]> sourceware.org Git - lvm2.git/blame - tools/reporter.c
thin: tighten discard string conversions
[lvm2.git] / tools / reporter.c
CommitLineData
4c64ed4c 1/*
67cdbd7e 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
ea0cdd28 3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4c64ed4c 4 *
6606c3ae 5 * This file is part of LVM2.
4c64ed4c 6 *
6606c3ae
AK
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
be684599 9 * of the GNU Lesser General Public License v.2.1.
4c64ed4c 10 *
be684599 11 * You should have received a copy of the GNU Lesser General Public License
6606c3ae
AK
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4c64ed4c
AK
14 */
15
16#include "tools.h"
17#include "report.h"
4c64ed4c 18
08f1ddea 19static int _vgs_single(struct cmd_context *cmd __attribute__((unused)),
72b2cb61 20 const char *vg_name, struct volume_group *vg,
13e8c7e4 21 void *handle)
4c64ed4c 22{
651ff9b3
AK
23 if (!report_object(handle, vg, NULL, NULL, NULL, NULL)) {
24 stack;
4c64ed4c 25 return ECMD_FAILED;
651ff9b3 26 }
4c64ed4c 27
24e65464
AK
28 check_current_backup(vg);
29
cfb7bfc7 30 return ECMD_PROCESSED;
4c64ed4c
AK
31}
32
33static int _lvs_single(struct cmd_context *cmd, struct logical_volume *lv,
34 void *handle)
35{
651ff9b3
AK
36 if (!report_object(handle, lv->vg, lv, NULL, NULL, NULL)) {
37 stack;
4c64ed4c 38 return ECMD_FAILED;
651ff9b3 39 }
4c64ed4c 40
cfb7bfc7 41 return ECMD_PROCESSED;
4c64ed4c
AK
42}
43
08f1ddea 44static int _segs_single(struct cmd_context *cmd __attribute__((unused)),
72b2cb61 45 struct lv_segment *seg, void *handle)
4c64ed4c 46{
651ff9b3
AK
47 if (!report_object(handle, seg->lv->vg, seg->lv, NULL, seg, NULL)) {
48 stack;
4c64ed4c 49 return ECMD_FAILED;
651ff9b3 50 }
4c64ed4c 51
cfb7bfc7 52 return ECMD_PROCESSED;
4c64ed4c 53}
d66abfb8 54static int _pvsegs_sub_single(struct cmd_context *cmd,
223c62e7 55 struct volume_group *vg,
c54a9405
AK
56 struct pv_segment *pvseg, void *handle)
57{
c54a9405 58 int ret = ECMD_PROCESSED;
dd007cfd 59 struct lv_segment *seg = pvseg->lvseg;
c54a9405 60
d66abfb8
MB
61 struct volume_group _free_vg = {
62 .cmd = cmd,
aec21154 63 .name = "",
2f25c320 64 .vgmem = NULL,
d66abfb8
MB
65 };
66
36081ccf 67 struct logical_volume _free_logical_volume = {
d66abfb8 68 .vg = vg ?: &_free_vg,
aec21154 69 .name = "",
cccae7e6 70 .snapshot = NULL,
36081ccf
AK
71 .status = VISIBLE_LV,
72 .major = -1,
73 .minor = -1,
74 };
75
76 struct lv_segment _free_lv_segment = {
cccae7e6
AK
77 .lv = &_free_logical_volume,
78 .le = 0,
79 .status = 0,
80 .stripe_size = 0,
81 .area_count = 0,
82 .area_len = 0,
83 .origin = NULL,
84 .cow = NULL,
85 .chunk_size = 0,
86 .region_size = 0,
87 .extents_copied = 0,
88 .log_lv = NULL,
89 .areas = NULL,
36081ccf
AK
90 };
91
cccae7e6 92 _free_lv_segment.segtype = get_segtype_from_string(cmd, "free");
36081ccf 93 _free_lv_segment.len = pvseg->len;
d66abfb8
MB
94 dm_list_init(&_free_vg.pvs);
95 dm_list_init(&_free_vg.lvs);
96 dm_list_init(&_free_vg.tags);
67d66d32
AK
97 dm_list_init(&_free_lv_segment.tags);
98 dm_list_init(&_free_lv_segment.origin_list);
2c44337b
AK
99 dm_list_init(&_free_logical_volume.tags);
100 dm_list_init(&_free_logical_volume.segments);
101 dm_list_init(&_free_logical_volume.segs_using_this_lv);
67d66d32 102 dm_list_init(&_free_logical_volume.snapshot_segs);
36081ccf
AK
103
104 if (!report_object(handle, vg, seg ? seg->lv : &_free_logical_volume, pvseg->pv,
651ff9b3 105 seg ? : &_free_lv_segment, pvseg)) {
dc4d7417 106 ret = ECMD_FAILED;
dd5b5fb7 107 goto_out;
651ff9b3 108 }
2f25c320 109
dd5b5fb7 110 out:
c54a9405
AK
111 return ret;
112}
113
4c64ed4c
AK
114static int _lvsegs_single(struct cmd_context *cmd, struct logical_volume *lv,
115 void *handle)
116{
afd9ba98 117 if (!arg_count(cmd, all_ARG) && !lv_is_visible(lv))
8211a13c
AK
118 return ECMD_PROCESSED;
119
4c64ed4c
AK
120 return process_each_segment_in_lv(cmd, lv, handle, _segs_single);
121}
122
c54a9405
AK
123static int _pvsegs_single(struct cmd_context *cmd, struct volume_group *vg,
124 struct physical_volume *pv, void *handle)
125{
126 return process_each_segment_in_pv(cmd, vg, pv, handle,
127 _pvsegs_sub_single);
128}
129
4c64ed4c
AK
130static int _pvs_single(struct cmd_context *cmd, struct volume_group *vg,
131 struct physical_volume *pv, void *handle)
132{
d38bf361 133 struct pv_list *pvl;
199fa12a 134 int ret = ECMD_PROCESSED;
d38bf361 135 const char *vg_name = NULL;
043b1362 136 struct volume_group *old_vg = vg;
08f1ddea 137 char uuid[64] __attribute__((aligned(8)));
4c64ed4c 138
a95892f7 139 if (is_pv(pv) && !is_orphan(pv) && !vg) {
d38bf361
AK
140 vg_name = pv_vg_name(pv);
141
b8b3508c
DW
142 vg = vg_read(cmd, vg_name, (char *)&pv->vgid, 0);
143 if (vg_read_error(vg)) {
e5f7352b 144 log_error("Skipping volume group %s", vg_name);
077a6755 145 release_vg(vg);
c29d2465
AK
146 return ECMD_FAILED;
147 }
199fa12a 148
d38bf361
AK
149 /*
150 * Replace possibly incomplete PV structure with new one
cccae7e6 151 * allocated in vg_read.
d38bf361 152 */
cccae7e6
AK
153 if (!is_missing_pv(pv)) {
154 if (!(pvl = find_pv_in_vg(vg, pv_dev_name(pv)))) {
155 log_error("Unable to find \"%s\" in volume group \"%s\"",
156 pv_dev_name(pv), vg->name);
157 ret = ECMD_FAILED;
158 goto out;
159 }
160 } else if (!(pvl = find_pv_in_vg_by_uuid(vg, &pv->id))) {
161 if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
162 stack;
163 uuid[0] = '\0';
164 }
165
166 log_error("Unable to find missing PV %s in volume group %s",
167 uuid, vg->name);
d38bf361
AK
168 ret = ECMD_FAILED;
169 goto out;
170 }
171
cccae7e6 172 pv = pvl->pv;
1fa74f62 173 }
4c64ed4c 174
651ff9b3
AK
175 if (!report_object(handle, vg, NULL, pv, NULL, NULL)) {
176 stack;
199fa12a 177 ret = ECMD_FAILED;
651ff9b3 178 }
4c64ed4c 179
dc4d7417 180out:
d38bf361
AK
181 if (vg_name)
182 unlock_vg(cmd, vg_name);
c29d2465 183
043b1362 184 if (!old_vg)
077a6755 185 release_vg(vg);
043b1362 186
199fa12a 187 return ret;
4c64ed4c
AK
188}
189
ea0cdd28
MB
190static int _label_single(struct cmd_context *cmd, struct volume_group *vg,
191 struct physical_volume *pv, void *handle)
192{
651ff9b3
AK
193 if (!report_object(handle, vg, NULL, pv, NULL, NULL)) {
194 stack;
ea0cdd28 195 return ECMD_FAILED;
651ff9b3 196 }
ea0cdd28
MB
197
198 return ECMD_PROCESSED;
199}
200
898b3d4d 201static int _pvs_in_vg(struct cmd_context *cmd, const char *vg_name,
21bc3664 202 struct volume_group *vg,
898b3d4d
AK
203 void *handle)
204{
651ff9b3
AK
205 if (vg_read_error(vg)) {
206 stack;
13e8c7e4 207 return ECMD_FAILED;
651ff9b3 208 }
13e8c7e4 209
898b3d4d
AK
210 return process_each_pv_in_vg(cmd, vg, NULL, handle, &_pvs_single);
211}
212
4bbfe1cf 213static int _pvsegs_in_vg(struct cmd_context *cmd, const char *vg_name,
21bc3664 214 struct volume_group *vg,
4bbfe1cf
AK
215 void *handle)
216{
651ff9b3
AK
217 if (vg_read_error(vg)) {
218 stack;
13e8c7e4 219 return ECMD_FAILED;
651ff9b3 220 }
13e8c7e4 221
4bbfe1cf
AK
222 return process_each_pv_in_vg(cmd, vg, NULL, handle, &_pvsegs_single);
223}
224
4c64ed4c
AK
225static int _report(struct cmd_context *cmd, int argc, char **argv,
226 report_type_t report_type)
227{
228 void *report_handle;
8ef2b021
AK
229 const char *opts;
230 char *str;
231 const char *keys = NULL, *options = NULL, *separator;
cfb7bfc7 232 int r = ECMD_PROCESSED;
30ef4c73 233 int aligned, buffered, headings, field_prefixes, quoted;
1f787aca 234 int columns_as_rows;
898b3d4d 235 unsigned args_are_pvs;
4c64ed4c 236
2293567c 237 aligned = find_config_tree_int(cmd, "report/aligned",
4c64ed4c 238 DEFAULT_REP_ALIGNED);
2293567c 239 buffered = find_config_tree_int(cmd, "report/buffered",
4c64ed4c 240 DEFAULT_REP_BUFFERED);
2293567c 241 headings = find_config_tree_int(cmd, "report/headings",
4c64ed4c 242 DEFAULT_REP_HEADINGS);
2293567c 243 separator = find_config_tree_str(cmd, "report/separator",
4c64ed4c 244 DEFAULT_REP_SEPARATOR);
f419a871
AK
245 field_prefixes = find_config_tree_int(cmd, "report/prefixes",
246 DEFAULT_REP_PREFIXES);
30ef4c73
AK
247 quoted = find_config_tree_int(cmd, "report/quoted",
248 DEFAULT_REP_QUOTED);
1f787aca
AK
249 columns_as_rows = find_config_tree_int(cmd, "report/columns_as_rows",
250 DEFAULT_REP_COLUMNS_AS_ROWS);
4c64ed4c 251
ea0cdd28
MB
252 args_are_pvs = (report_type == PVS ||
253 report_type == LABEL ||
254 report_type == PVSEGS) ? 1 : 0;
898b3d4d 255
4c64ed4c
AK
256 switch (report_type) {
257 case LVS:
2293567c 258 keys = find_config_tree_str(cmd, "report/lvs_sort",
4c64ed4c
AK
259 DEFAULT_LVS_SORT);
260 if (!arg_count(cmd, verbose_ARG))
2293567c 261 options = find_config_tree_str(cmd,
814643d8 262 "report/lvs_cols",
4c64ed4c
AK
263 DEFAULT_LVS_COLS);
264 else
2293567c 265 options = find_config_tree_str(cmd,
4c64ed4c 266 "report/lvs_cols_verbose",
814643d8 267 DEFAULT_LVS_COLS_VERB);
4c64ed4c
AK
268 break;
269 case VGS:
2293567c 270 keys = find_config_tree_str(cmd, "report/vgs_sort",
4c64ed4c
AK
271 DEFAULT_VGS_SORT);
272 if (!arg_count(cmd, verbose_ARG))
2293567c 273 options = find_config_tree_str(cmd,
814643d8 274 "report/vgs_cols",
4c64ed4c
AK
275 DEFAULT_VGS_COLS);
276 else
2293567c 277 options = find_config_tree_str(cmd,
4c64ed4c 278 "report/vgs_cols_verbose",
814643d8 279 DEFAULT_VGS_COLS_VERB);
4c64ed4c 280 break;
ea0cdd28 281 case LABEL:
4c64ed4c 282 case PVS:
2293567c 283 keys = find_config_tree_str(cmd, "report/pvs_sort",
4c64ed4c
AK
284 DEFAULT_PVS_SORT);
285 if (!arg_count(cmd, verbose_ARG))
2293567c 286 options = find_config_tree_str(cmd,
814643d8 287 "report/pvs_cols",
4c64ed4c
AK
288 DEFAULT_PVS_COLS);
289 else
2293567c 290 options = find_config_tree_str(cmd,
4c64ed4c 291 "report/pvs_cols_verbose",
814643d8 292 DEFAULT_PVS_COLS_VERB);
4c64ed4c
AK
293 break;
294 case SEGS:
2293567c 295 keys = find_config_tree_str(cmd, "report/segs_sort",
4c64ed4c
AK
296 DEFAULT_SEGS_SORT);
297 if (!arg_count(cmd, verbose_ARG))
2293567c 298 options = find_config_tree_str(cmd,
814643d8 299 "report/segs_cols",
4c64ed4c
AK
300 DEFAULT_SEGS_COLS);
301 else
2293567c 302 options = find_config_tree_str(cmd,
4c64ed4c 303 "report/segs_cols_verbose",
814643d8 304 DEFAULT_SEGS_COLS_VERB);
4c64ed4c 305 break;
c54a9405 306 case PVSEGS:
2293567c 307 keys = find_config_tree_str(cmd, "report/pvsegs_sort",
c54a9405
AK
308 DEFAULT_PVSEGS_SORT);
309 if (!arg_count(cmd, verbose_ARG))
2293567c 310 options = find_config_tree_str(cmd,
c54a9405
AK
311 "report/pvsegs_cols",
312 DEFAULT_PVSEGS_COLS);
313 else
2293567c 314 options = find_config_tree_str(cmd,
c54a9405
AK
315 "report/pvsegs_cols_verbose",
316 DEFAULT_PVSEGS_COLS_VERB);
317 break;
73e62cdc
ZK
318 default:
319 log_error(INTERNAL_ERROR "Unknown report type.");
320 return ECMD_FAILED;
4c64ed4c
AK
321 }
322
323 /* If -o supplied use it, else use default for report_type */
324 if (arg_count(cmd, options_ARG)) {
325 opts = arg_str_value(cmd, options_ARG, "");
326 if (!opts || !*opts) {
327 log_error("Invalid options string: %s", opts);
8a19ebf4 328 return EINVALID_CMD_LINE;
4c64ed4c
AK
329 }
330 if (*opts == '+') {
2c7fbade
AK
331 if (!(str = dm_pool_alloc(cmd->mem,
332 strlen(options) + strlen(opts) + 1))) {
333 log_error("options string allocation failed");
8a19ebf4 334 return ECMD_FAILED;
2c7fbade 335 }
4c64ed4c 336 strcpy(str, options);
8ef2b021
AK
337 strcat(str, ",");
338 strcat(str, opts + 1);
4c64ed4c
AK
339 options = str;
340 } else
341 options = opts;
342 }
343
344 /* -O overrides default sort settings */
a8fb89ad 345 keys = arg_str_value(cmd, sort_ARG, keys);
4c64ed4c 346
a8fb89ad 347 separator = arg_str_value(cmd, separator_ARG, separator);
4c64ed4c
AK
348 if (arg_count(cmd, separator_ARG))
349 aligned = 0;
350 if (arg_count(cmd, aligned_ARG))
351 aligned = 1;
352 if (arg_count(cmd, unbuffered_ARG) && !arg_count(cmd, sort_ARG))
353 buffered = 0;
354 if (arg_count(cmd, noheadings_ARG))
355 headings = 0;
ec40d928 356 if (arg_count(cmd, nameprefixes_ARG)) {
f419a871
AK
357 aligned = 0;
358 field_prefixes = 1;
359 }
30ef4c73
AK
360 if (arg_count(cmd, unquoted_ARG))
361 quoted = 0;
1f787aca
AK
362 if (arg_count(cmd, rows_ARG))
363 columns_as_rows = 1;
4c64ed4c
AK
364
365 if (!(report_handle = report_init(cmd, options, keys, &report_type,
366 separator, aligned, buffered,
1f787aca
AK
367 headings, field_prefixes, quoted,
368 columns_as_rows))) {
4df5235d
MB
369 if (!strcasecmp(options, "help") || !strcmp(options, "?"))
370 return r;
8a19ebf4
AK
371 stack;
372 return ECMD_FAILED;
373 }
4c64ed4c 374
d838a1e3
AK
375 /* Ensure options selected are compatible */
376 if (report_type & SEGS)
377 report_type |= LVS;
378 if (report_type & PVSEGS)
379 report_type |= PVS;
ea0cdd28 380 if ((report_type & LVS) && (report_type & (PVS | LABEL)) && !args_are_pvs) {
d838a1e3 381 log_error("Can't report LV and PV fields at the same time");
0558b928 382 dm_report_free(report_handle);
8a19ebf4 383 return ECMD_FAILED;
d838a1e3
AK
384 }
385
386 /* Change report type if fields specified makes this necessary */
dd007cfd 387 if ((report_type & PVSEGS) ||
ea0cdd28 388 ((report_type & (PVS | LABEL)) && (report_type & LVS)))
d838a1e3 389 report_type = PVSEGS;
ea0cdd28
MB
390 else if ((report_type & LABEL) && (report_type & VGS))
391 report_type = PVS;
d838a1e3
AK
392 else if (report_type & PVS)
393 report_type = PVS;
dd007cfd
AK
394 else if (report_type & SEGS)
395 report_type = SEGS;
396 else if (report_type & LVS)
397 report_type = LVS;
d838a1e3 398
4c64ed4c
AK
399 switch (report_type) {
400 case LVS:
13e8c7e4 401 r = process_each_lv(cmd, argc, argv, 0, report_handle,
cfb7bfc7 402 &_lvs_single);
4c64ed4c
AK
403 break;
404 case VGS:
13e8c7e4 405 r = process_each_vg(cmd, argc, argv, 0,
cfb7bfc7 406 report_handle, &_vgs_single);
4c64ed4c 407 break;
ea0cdd28 408 case LABEL:
21a98eda 409 r = process_each_pv(cmd, argc, argv, NULL, READ_WITHOUT_LOCK,
ea0cdd28
MB
410 1, report_handle, &_label_single);
411 break;
4c64ed4c 412 case PVS:
898b3d4d 413 if (args_are_pvs)
21a98eda 414 r = process_each_pv(cmd, argc, argv, NULL, 0,
ea0cdd28 415 0, report_handle, &_pvs_single);
898b3d4d 416 else
13e8c7e4 417 r = process_each_vg(cmd, argc, argv, 0,
898b3d4d 418 report_handle, &_pvs_in_vg);
4c64ed4c
AK
419 break;
420 case SEGS:
13e8c7e4 421 r = process_each_lv(cmd, argc, argv, 0, report_handle,
cfb7bfc7 422 &_lvsegs_single);
4c64ed4c 423 break;
c54a9405 424 case PVSEGS:
898b3d4d 425 if (args_are_pvs)
21a98eda 426 r = process_each_pv(cmd, argc, argv, NULL, 0,
ea0cdd28 427 0, report_handle, &_pvsegs_single);
898b3d4d 428 else
13e8c7e4 429 r = process_each_vg(cmd, argc, argv, 0,
4bbfe1cf 430 report_handle, &_pvsegs_in_vg);
c54a9405 431 break;
4c64ed4c
AK
432 }
433
d838a1e3 434 dm_report_output(report_handle);
4c64ed4c 435
d838a1e3 436 dm_report_free(report_handle);
cfb7bfc7 437 return r;
4c64ed4c
AK
438}
439
440int lvs(struct cmd_context *cmd, int argc, char **argv)
441{
442 report_type_t type;
443
444 if (arg_count(cmd, segments_ARG))
445 type = SEGS;
446 else
447 type = LVS;
448
449 return _report(cmd, argc, argv, type);
450}
451
452int vgs(struct cmd_context *cmd, int argc, char **argv)
453{
454 return _report(cmd, argc, argv, VGS);
455}
456
457int pvs(struct cmd_context *cmd, int argc, char **argv)
458{
c54a9405
AK
459 report_type_t type;
460
461 if (arg_count(cmd, segments_ARG))
462 type = PVSEGS;
463 else
ea0cdd28 464 type = LABEL;
c54a9405
AK
465
466 return _report(cmd, argc, argv, type);
4c64ed4c 467}
This page took 0.163802 seconds and 5 git commands to generate.