reporter.c revision 1.1 1 1.1 haad /* $NetBSD: reporter.c,v 1.1 2008/12/22 00:19:07 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 haad if (!arg_count(cmd, all_ARG) && !lv_is_visible(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 haad dm_list_init(&_free_logical_volume.tags);
94 1.1 haad dm_list_init(&_free_logical_volume.segments);
95 1.1 haad dm_list_init(&_free_logical_volume.segs_using_this_lv);
96 1.1 haad
97 1.1 haad if (!report_object(handle, vg, seg ? seg->lv : &_free_logical_volume, pvseg->pv,
98 1.1 haad seg ? : &_free_lv_segment, pvseg))
99 1.1 haad ret = ECMD_FAILED;
100 1.1 haad
101 1.1 haad return ret;
102 1.1 haad }
103 1.1 haad
104 1.1 haad static int _lvsegs_single(struct cmd_context *cmd, struct logical_volume *lv,
105 1.1 haad void *handle)
106 1.1 haad {
107 1.1 haad if (!arg_count(cmd, all_ARG) && !lv_is_visible(lv))
108 1.1 haad return ECMD_PROCESSED;
109 1.1 haad
110 1.1 haad return process_each_segment_in_lv(cmd, lv, handle, _segs_single);
111 1.1 haad }
112 1.1 haad
113 1.1 haad static int _pvsegs_single(struct cmd_context *cmd, struct volume_group *vg,
114 1.1 haad struct physical_volume *pv, void *handle)
115 1.1 haad {
116 1.1 haad return process_each_segment_in_pv(cmd, vg, pv, handle,
117 1.1 haad _pvsegs_sub_single);
118 1.1 haad }
119 1.1 haad
120 1.1 haad static int _pvs_single(struct cmd_context *cmd, struct volume_group *vg,
121 1.1 haad struct physical_volume *pv, void *handle)
122 1.1 haad {
123 1.1 haad struct pv_list *pvl;
124 1.1 haad int ret = ECMD_PROCESSED;
125 1.1 haad const char *vg_name = NULL;
126 1.1 haad
127 1.1 haad if (is_pv(pv) && !is_orphan(pv) && !vg) {
128 1.1 haad vg_name = pv_vg_name(pv);
129 1.1 haad
130 1.1 haad if (!(vg = vg_lock_and_read(cmd, vg_name, (char *)&pv->vgid,
131 1.1 haad LCK_VG_READ, CLUSTERED, 0))) {
132 1.1 haad log_error("Skipping volume group %s", vg_name);
133 1.1 haad return ECMD_FAILED;
134 1.1 haad }
135 1.1 haad
136 1.1 haad /*
137 1.1 haad * Replace possibly incomplete PV structure with new one
138 1.1 haad * allocated in vg_read() path.
139 1.1 haad */
140 1.1 haad if (!(pvl = find_pv_in_vg(vg, pv_dev_name(pv)))) {
141 1.1 haad log_error("Unable to find \"%s\" in volume group \"%s\"",
142 1.1 haad pv_dev_name(pv), vg->name);
143 1.1 haad ret = ECMD_FAILED;
144 1.1 haad goto out;
145 1.1 haad }
146 1.1 haad
147 1.1 haad pv = pvl->pv;
148 1.1 haad }
149 1.1 haad
150 1.1 haad if (!report_object(handle, vg, NULL, pv, NULL, NULL))
151 1.1 haad ret = ECMD_FAILED;
152 1.1 haad
153 1.1 haad out:
154 1.1 haad if (vg_name)
155 1.1 haad unlock_vg(cmd, vg_name);
156 1.1 haad
157 1.1 haad return ret;
158 1.1 haad }
159 1.1 haad
160 1.1 haad static int _pvs_in_vg(struct cmd_context *cmd, const char *vg_name,
161 1.1 haad struct volume_group *vg,
162 1.1 haad int consistent __attribute((unused)),
163 1.1 haad void *handle)
164 1.1 haad {
165 1.1 haad if (!vg) {
166 1.1 haad log_error("Volume group %s not found", vg_name);
167 1.1 haad return ECMD_FAILED;
168 1.1 haad }
169 1.1 haad
170 1.1 haad return process_each_pv_in_vg(cmd, vg, NULL, handle, &_pvs_single);
171 1.1 haad }
172 1.1 haad
173 1.1 haad static int _pvsegs_in_vg(struct cmd_context *cmd, const char *vg_name,
174 1.1 haad struct volume_group *vg,
175 1.1 haad int consistent __attribute((unused)),
176 1.1 haad void *handle)
177 1.1 haad {
178 1.1 haad if (!vg) {
179 1.1 haad log_error("Volume group %s not found", vg_name);
180 1.1 haad return ECMD_FAILED;
181 1.1 haad }
182 1.1 haad
183 1.1 haad return process_each_pv_in_vg(cmd, vg, NULL, handle, &_pvsegs_single);
184 1.1 haad }
185 1.1 haad
186 1.1 haad static int _report(struct cmd_context *cmd, int argc, char **argv,
187 1.1 haad report_type_t report_type)
188 1.1 haad {
189 1.1 haad void *report_handle;
190 1.1 haad const char *opts;
191 1.1 haad char *str;
192 1.1 haad const char *keys = NULL, *options = NULL, *separator;
193 1.1 haad int r = ECMD_PROCESSED;
194 1.1 haad int aligned, buffered, headings, field_prefixes, quoted;
195 1.1 haad int columns_as_rows;
196 1.1 haad unsigned args_are_pvs;
197 1.1 haad
198 1.1 haad aligned = find_config_tree_int(cmd, "report/aligned",
199 1.1 haad DEFAULT_REP_ALIGNED);
200 1.1 haad buffered = find_config_tree_int(cmd, "report/buffered",
201 1.1 haad DEFAULT_REP_BUFFERED);
202 1.1 haad headings = find_config_tree_int(cmd, "report/headings",
203 1.1 haad DEFAULT_REP_HEADINGS);
204 1.1 haad separator = find_config_tree_str(cmd, "report/separator",
205 1.1 haad DEFAULT_REP_SEPARATOR);
206 1.1 haad field_prefixes = find_config_tree_int(cmd, "report/prefixes",
207 1.1 haad DEFAULT_REP_PREFIXES);
208 1.1 haad quoted = find_config_tree_int(cmd, "report/quoted",
209 1.1 haad DEFAULT_REP_QUOTED);
210 1.1 haad columns_as_rows = find_config_tree_int(cmd, "report/columns_as_rows",
211 1.1 haad DEFAULT_REP_COLUMNS_AS_ROWS);
212 1.1 haad
213 1.1 haad args_are_pvs = (report_type == PVS || report_type == PVSEGS) ? 1 : 0;
214 1.1 haad
215 1.1 haad switch (report_type) {
216 1.1 haad case LVS:
217 1.1 haad keys = find_config_tree_str(cmd, "report/lvs_sort",
218 1.1 haad DEFAULT_LVS_SORT);
219 1.1 haad if (!arg_count(cmd, verbose_ARG))
220 1.1 haad options = find_config_tree_str(cmd,
221 1.1 haad "report/lvs_cols",
222 1.1 haad DEFAULT_LVS_COLS);
223 1.1 haad else
224 1.1 haad options = find_config_tree_str(cmd,
225 1.1 haad "report/lvs_cols_verbose",
226 1.1 haad DEFAULT_LVS_COLS_VERB);
227 1.1 haad break;
228 1.1 haad case VGS:
229 1.1 haad keys = find_config_tree_str(cmd, "report/vgs_sort",
230 1.1 haad DEFAULT_VGS_SORT);
231 1.1 haad if (!arg_count(cmd, verbose_ARG))
232 1.1 haad options = find_config_tree_str(cmd,
233 1.1 haad "report/vgs_cols",
234 1.1 haad DEFAULT_VGS_COLS);
235 1.1 haad else
236 1.1 haad options = find_config_tree_str(cmd,
237 1.1 haad "report/vgs_cols_verbose",
238 1.1 haad DEFAULT_VGS_COLS_VERB);
239 1.1 haad break;
240 1.1 haad case PVS:
241 1.1 haad keys = find_config_tree_str(cmd, "report/pvs_sort",
242 1.1 haad DEFAULT_PVS_SORT);
243 1.1 haad if (!arg_count(cmd, verbose_ARG))
244 1.1 haad options = find_config_tree_str(cmd,
245 1.1 haad "report/pvs_cols",
246 1.1 haad DEFAULT_PVS_COLS);
247 1.1 haad else
248 1.1 haad options = find_config_tree_str(cmd,
249 1.1 haad "report/pvs_cols_verbose",
250 1.1 haad DEFAULT_PVS_COLS_VERB);
251 1.1 haad break;
252 1.1 haad case SEGS:
253 1.1 haad keys = find_config_tree_str(cmd, "report/segs_sort",
254 1.1 haad DEFAULT_SEGS_SORT);
255 1.1 haad if (!arg_count(cmd, verbose_ARG))
256 1.1 haad options = find_config_tree_str(cmd,
257 1.1 haad "report/segs_cols",
258 1.1 haad DEFAULT_SEGS_COLS);
259 1.1 haad else
260 1.1 haad options = find_config_tree_str(cmd,
261 1.1 haad "report/segs_cols_verbose",
262 1.1 haad DEFAULT_SEGS_COLS_VERB);
263 1.1 haad break;
264 1.1 haad case PVSEGS:
265 1.1 haad keys = find_config_tree_str(cmd, "report/pvsegs_sort",
266 1.1 haad DEFAULT_PVSEGS_SORT);
267 1.1 haad if (!arg_count(cmd, verbose_ARG))
268 1.1 haad options = find_config_tree_str(cmd,
269 1.1 haad "report/pvsegs_cols",
270 1.1 haad DEFAULT_PVSEGS_COLS);
271 1.1 haad else
272 1.1 haad options = find_config_tree_str(cmd,
273 1.1 haad "report/pvsegs_cols_verbose",
274 1.1 haad DEFAULT_PVSEGS_COLS_VERB);
275 1.1 haad break;
276 1.1 haad }
277 1.1 haad
278 1.1 haad /* If -o supplied use it, else use default for report_type */
279 1.1 haad if (arg_count(cmd, options_ARG)) {
280 1.1 haad opts = arg_str_value(cmd, options_ARG, "");
281 1.1 haad if (!opts || !*opts) {
282 1.1 haad log_error("Invalid options string: %s", opts);
283 1.1 haad return EINVALID_CMD_LINE;
284 1.1 haad }
285 1.1 haad if (*opts == '+') {
286 1.1 haad if (!(str = dm_pool_alloc(cmd->mem,
287 1.1 haad strlen(options) + strlen(opts) + 1))) {
288 1.1 haad log_error("options string allocation failed");
289 1.1 haad return ECMD_FAILED;
290 1.1 haad }
291 1.1 haad strcpy(str, options);
292 1.1 haad strcat(str, ",");
293 1.1 haad strcat(str, opts + 1);
294 1.1 haad options = str;
295 1.1 haad } else
296 1.1 haad options = opts;
297 1.1 haad }
298 1.1 haad
299 1.1 haad /* -O overrides default sort settings */
300 1.1 haad if (arg_count(cmd, sort_ARG))
301 1.1 haad keys = arg_str_value(cmd, sort_ARG, "");
302 1.1 haad
303 1.1 haad if (arg_count(cmd, separator_ARG))
304 1.1 haad separator = arg_str_value(cmd, separator_ARG, " ");
305 1.1 haad if (arg_count(cmd, separator_ARG))
306 1.1 haad aligned = 0;
307 1.1 haad if (arg_count(cmd, aligned_ARG))
308 1.1 haad aligned = 1;
309 1.1 haad if (arg_count(cmd, unbuffered_ARG) && !arg_count(cmd, sort_ARG))
310 1.1 haad buffered = 0;
311 1.1 haad if (arg_count(cmd, noheadings_ARG))
312 1.1 haad headings = 0;
313 1.1 haad if (arg_count(cmd, nameprefixes_ARG)) {
314 1.1 haad aligned = 0;
315 1.1 haad field_prefixes = 1;
316 1.1 haad }
317 1.1 haad if (arg_count(cmd, unquoted_ARG))
318 1.1 haad quoted = 0;
319 1.1 haad if (arg_count(cmd, rows_ARG))
320 1.1 haad columns_as_rows = 1;
321 1.1 haad
322 1.1 haad if (!(report_handle = report_init(cmd, options, keys, &report_type,
323 1.1 haad separator, aligned, buffered,
324 1.1 haad headings, field_prefixes, quoted,
325 1.1 haad columns_as_rows))) {
326 1.1 haad stack;
327 1.1 haad return ECMD_FAILED;
328 1.1 haad }
329 1.1 haad
330 1.1 haad /* Ensure options selected are compatible */
331 1.1 haad if (report_type & SEGS)
332 1.1 haad report_type |= LVS;
333 1.1 haad if (report_type & PVSEGS)
334 1.1 haad report_type |= PVS;
335 1.1 haad if ((report_type & LVS) && (report_type & PVS) && !args_are_pvs) {
336 1.1 haad log_error("Can't report LV and PV fields at the same time");
337 1.1 haad dm_report_free(report_handle);
338 1.1 haad return ECMD_FAILED;
339 1.1 haad }
340 1.1 haad
341 1.1 haad /* Change report type if fields specified makes this necessary */
342 1.1 haad if ((report_type & PVSEGS) ||
343 1.1 haad ((report_type & PVS) && (report_type & LVS)))
344 1.1 haad report_type = PVSEGS;
345 1.1 haad else if (report_type & PVS)
346 1.1 haad report_type = PVS;
347 1.1 haad else if (report_type & SEGS)
348 1.1 haad report_type = SEGS;
349 1.1 haad else if (report_type & LVS)
350 1.1 haad report_type = LVS;
351 1.1 haad
352 1.1 haad switch (report_type) {
353 1.1 haad case LVS:
354 1.1 haad r = process_each_lv(cmd, argc, argv, LCK_VG_READ, report_handle,
355 1.1 haad &_lvs_single);
356 1.1 haad break;
357 1.1 haad case VGS:
358 1.1 haad r = process_each_vg(cmd, argc, argv, LCK_VG_READ, 0,
359 1.1 haad report_handle, &_vgs_single);
360 1.1 haad break;
361 1.1 haad case PVS:
362 1.1 haad if (args_are_pvs)
363 1.1 haad r = process_each_pv(cmd, argc, argv, NULL, LCK_VG_READ,
364 1.1 haad report_handle, &_pvs_single);
365 1.1 haad else
366 1.1 haad r = process_each_vg(cmd, argc, argv, LCK_VG_READ, 0,
367 1.1 haad report_handle, &_pvs_in_vg);
368 1.1 haad break;
369 1.1 haad case SEGS:
370 1.1 haad r = process_each_lv(cmd, argc, argv, LCK_VG_READ, report_handle,
371 1.1 haad &_lvsegs_single);
372 1.1 haad break;
373 1.1 haad case PVSEGS:
374 1.1 haad if (args_are_pvs)
375 1.1 haad r = process_each_pv(cmd, argc, argv, NULL, LCK_VG_READ,
376 1.1 haad report_handle, &_pvsegs_single);
377 1.1 haad else
378 1.1 haad r = process_each_vg(cmd, argc, argv, LCK_VG_READ, 0,
379 1.1 haad report_handle, &_pvsegs_in_vg);
380 1.1 haad break;
381 1.1 haad }
382 1.1 haad
383 1.1 haad dm_report_output(report_handle);
384 1.1 haad
385 1.1 haad dm_report_free(report_handle);
386 1.1 haad return r;
387 1.1 haad }
388 1.1 haad
389 1.1 haad int lvs(struct cmd_context *cmd, int argc, char **argv)
390 1.1 haad {
391 1.1 haad report_type_t type;
392 1.1 haad
393 1.1 haad if (arg_count(cmd, segments_ARG))
394 1.1 haad type = SEGS;
395 1.1 haad else
396 1.1 haad type = LVS;
397 1.1 haad
398 1.1 haad return _report(cmd, argc, argv, type);
399 1.1 haad }
400 1.1 haad
401 1.1 haad int vgs(struct cmd_context *cmd, int argc, char **argv)
402 1.1 haad {
403 1.1 haad return _report(cmd, argc, argv, VGS);
404 1.1 haad }
405 1.1 haad
406 1.1 haad int pvs(struct cmd_context *cmd, int argc, char **argv)
407 1.1 haad {
408 1.1 haad report_type_t type;
409 1.1 haad
410 1.1 haad if (arg_count(cmd, segments_ARG))
411 1.1 haad type = PVSEGS;
412 1.1 haad else
413 1.1 haad type = PVS;
414 1.1 haad
415 1.1 haad return _report(cmd, argc, argv, type);
416 1.1 haad }
417