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