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