xref: /netbsd-src/external/gpl2/lvm2/dist/lib/format_text/flags.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: flags.c,v 1.1.1.2 2009/12/02 00:26:29 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "metadata.h"
2056a34939Shaad #include "import-export.h"
2156a34939Shaad #include "lvm-string.h"
2256a34939Shaad 
2356a34939Shaad /*
2456a34939Shaad  * Bitsets held in the 'status' flags get
2556a34939Shaad  * converted into arrays of strings.
2656a34939Shaad  */
2756a34939Shaad struct flag {
2856a34939Shaad 	const int mask;
2956a34939Shaad 	const char *description;
3056a34939Shaad 	int kind;
3156a34939Shaad };
3256a34939Shaad 
3356a34939Shaad static struct flag _vg_flags[] = {
3456a34939Shaad 	{EXPORTED_VG, "EXPORTED", STATUS_FLAG},
3556a34939Shaad 	{RESIZEABLE_VG, "RESIZEABLE", STATUS_FLAG},
3656a34939Shaad 	{PVMOVE, "PVMOVE", STATUS_FLAG},
3756a34939Shaad 	{LVM_READ, "READ", STATUS_FLAG},
3856a34939Shaad 	{LVM_WRITE, "WRITE", STATUS_FLAG},
3956a34939Shaad 	{CLUSTERED, "CLUSTERED", STATUS_FLAG},
4056a34939Shaad 	{SHARED, "SHARED", STATUS_FLAG},
4156a34939Shaad 	{PARTIAL_VG, NULL, 0},
4256a34939Shaad 	{PRECOMMITTED, NULL, 0},
4356a34939Shaad 	{0, NULL, 0}
4456a34939Shaad };
4556a34939Shaad 
4656a34939Shaad static struct flag _pv_flags[] = {
4756a34939Shaad 	{ALLOCATABLE_PV, "ALLOCATABLE", STATUS_FLAG},
4856a34939Shaad 	{EXPORTED_VG, "EXPORTED", STATUS_FLAG},
4956a34939Shaad 	{MISSING_PV, "MISSING", COMPATIBLE_FLAG},
5056a34939Shaad 	{0, NULL, 0}
5156a34939Shaad };
5256a34939Shaad 
5356a34939Shaad static struct flag _lv_flags[] = {
5456a34939Shaad 	{LVM_READ, "READ", STATUS_FLAG},
5556a34939Shaad 	{LVM_WRITE, "WRITE", STATUS_FLAG},
5656a34939Shaad 	{FIXED_MINOR, "FIXED_MINOR", STATUS_FLAG},
5756a34939Shaad 	{VISIBLE_LV, "VISIBLE", STATUS_FLAG},
5856a34939Shaad 	{PVMOVE, "PVMOVE", STATUS_FLAG},
5956a34939Shaad 	{LOCKED, "LOCKED", STATUS_FLAG},
6056a34939Shaad 	{MIRROR_NOTSYNCED, "NOTSYNCED", STATUS_FLAG},
6156a34939Shaad 	{MIRROR_IMAGE, NULL, 0},
6256a34939Shaad 	{MIRROR_LOG, NULL, 0},
6356a34939Shaad 	{MIRRORED, NULL, 0},
6456a34939Shaad 	{VIRTUAL, NULL, 0},
6556a34939Shaad 	{SNAPSHOT, NULL, 0},
6656a34939Shaad 	{ACTIVATE_EXCL, NULL, 0},
6756a34939Shaad 	{CONVERTING, NULL, 0},
6856a34939Shaad 	{PARTIAL_LV, NULL, 0},
6956a34939Shaad 	{POSTORDER_FLAG, NULL, 0},
70*7c604eeaShaad 	{VIRTUAL_ORIGIN, NULL, 0},
7156a34939Shaad 	{0, NULL, 0}
7256a34939Shaad };
7356a34939Shaad 
_get_flags(int type)7456a34939Shaad static struct flag *_get_flags(int type)
7556a34939Shaad {
7656a34939Shaad 	switch (type & ~STATUS_FLAG) {
7756a34939Shaad 	case VG_FLAGS:
7856a34939Shaad 		return _vg_flags;
7956a34939Shaad 
8056a34939Shaad 	case PV_FLAGS:
8156a34939Shaad 		return _pv_flags;
8256a34939Shaad 
8356a34939Shaad 	case LV_FLAGS:
8456a34939Shaad 		return _lv_flags;
8556a34939Shaad 	}
8656a34939Shaad 
87*7c604eeaShaad 	log_error("Unknown flag set requested.");
8856a34939Shaad 	return NULL;
8956a34939Shaad }
9056a34939Shaad 
9156a34939Shaad /*
9256a34939Shaad  * Converts a bitset to an array of string values,
9356a34939Shaad  * using one of the tables defined at the top of
9456a34939Shaad  * the file.
9556a34939Shaad  */
print_flags(uint32_t status,int type,char * buffer,size_t size)9656a34939Shaad int print_flags(uint32_t status, int type, char *buffer, size_t size)
9756a34939Shaad {
9856a34939Shaad 	int f, first = 1;
9956a34939Shaad 	struct flag *flags;
10056a34939Shaad 
10156a34939Shaad 	if (!(flags = _get_flags(type)))
10256a34939Shaad 		return_0;
10356a34939Shaad 
10456a34939Shaad 	if (!emit_to_buffer(&buffer, &size, "["))
10556a34939Shaad 		return 0;
10656a34939Shaad 
10756a34939Shaad 	for (f = 0; flags[f].mask; f++) {
10856a34939Shaad 		if (status & flags[f].mask) {
10956a34939Shaad 			status &= ~flags[f].mask;
11056a34939Shaad 
11156a34939Shaad 			if ((type & STATUS_FLAG) != flags[f].kind)
11256a34939Shaad 				continue;
11356a34939Shaad 
11456a34939Shaad 			/* Internal-only flag? */
11556a34939Shaad 			if (!flags[f].description)
11656a34939Shaad 				continue;
11756a34939Shaad 
11856a34939Shaad 			if (!first) {
11956a34939Shaad 				if (!emit_to_buffer(&buffer, &size, ", "))
12056a34939Shaad 					return 0;
12156a34939Shaad 			} else
12256a34939Shaad 				first = 0;
12356a34939Shaad 
12456a34939Shaad 			if (!emit_to_buffer(&buffer, &size, "\"%s\"",
12556a34939Shaad 			    flags[f].description))
12656a34939Shaad 				return 0;
12756a34939Shaad 		}
12856a34939Shaad 	}
12956a34939Shaad 
13056a34939Shaad 	if (!emit_to_buffer(&buffer, &size, "]"))
13156a34939Shaad 		return 0;
13256a34939Shaad 
13356a34939Shaad 	if (status)
13456a34939Shaad 		log_error("Metadata inconsistency: Not all flags successfully "
13556a34939Shaad 			  "exported.");
13656a34939Shaad 
13756a34939Shaad 	return 1;
13856a34939Shaad }
13956a34939Shaad 
read_flags(uint32_t * status,int type,struct config_value * cv)14056a34939Shaad int read_flags(uint32_t *status, int type, struct config_value *cv)
14156a34939Shaad {
14256a34939Shaad 	int f;
14356a34939Shaad 	uint32_t s = 0;
14456a34939Shaad 	struct flag *flags;
14556a34939Shaad 
14656a34939Shaad 	if (!(flags = _get_flags(type)))
14756a34939Shaad 		return_0;
14856a34939Shaad 
14956a34939Shaad 	if (cv->type == CFG_EMPTY_ARRAY)
15056a34939Shaad 		goto out;
15156a34939Shaad 
15256a34939Shaad 	while (cv) {
15356a34939Shaad 		if (cv->type != CFG_STRING) {
154*7c604eeaShaad 			log_error("Status value is not a string.");
15556a34939Shaad 			return 0;
15656a34939Shaad 		}
15756a34939Shaad 
15856a34939Shaad 		for (f = 0; flags[f].description; f++)
15956a34939Shaad 			if (!strcmp(flags[f].description, cv->v.str)) {
16056a34939Shaad 				s |= flags[f].mask;
16156a34939Shaad 				break;
16256a34939Shaad 			}
16356a34939Shaad 
16456a34939Shaad 		if (type == VG_FLAGS && !strcmp(cv->v.str, "PARTIAL")) {
16556a34939Shaad 			/*
16656a34939Shaad 			 * Exception: We no longer write this flag out, but it
16756a34939Shaad 			 * might be encountered in old backup files, so restore
16856a34939Shaad 			 * it in that case. It is never part of live metadata
16956a34939Shaad 			 * though, so only vgcfgrestore needs to be concerned
17056a34939Shaad 			 * by this case.
17156a34939Shaad 			 */
17256a34939Shaad 			s |= PARTIAL_VG;
17356a34939Shaad 		} else if (!flags[f].description && (type & STATUS_FLAG)) {
174*7c604eeaShaad 			log_error("Unknown status flag '%s'.", cv->v.str);
17556a34939Shaad 			return 0;
17656a34939Shaad 		}
17756a34939Shaad 
17856a34939Shaad 		cv = cv->next;
17956a34939Shaad 	}
18056a34939Shaad 
18156a34939Shaad       out:
18256a34939Shaad 	*status |= s;
18356a34939Shaad 	return 1;
18456a34939Shaad }
185