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