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