feature.c revision 16c581d0e8d931f3cf0005de07481d7380ca4c0c
1/*
2 * feature.c --- convert between features and strings
3 *
4 * Copyright (C) 1999  Theodore Ts'o <tytso@mit.edu>
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <ctype.h>
16#include <errno.h>
17
18#include "e2p.h"
19#include <ext2fs/ext2fs.h>
20#include <ext2fs/jfs_user.h>
21
22struct feature {
23	int		compat;
24	unsigned int	mask;
25	const char	*string;
26};
27
28static struct feature feature_list[] = {
29	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
30			"dir_prealloc" },
31	{	E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
32			"has_journal" },
33	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
34			"imagic_inodes" },
35	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
36			"ext_attr" },
37	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
38			"dir_index" },
39	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
40			"resize_inode" },
41	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
42			"lazy_bg" },
43	{	E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXCLUDE_BITMAP,
44			"snapshot_bitmap" },
45
46	{	E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
47			"sparse_super" },
48	{	E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
49			"large_file" },
50	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
51			"huge_file" },
52	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
53			"uninit_bg" },
54	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
55			"uninit_groups" },
56	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
57			"dir_nlink" },
58	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
59			"extra_isize" },
60	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_QUOTA,
61			"quota" },
62	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_BIGALLOC,
63			"bigalloc"},
64	{	E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM,
65			"metadata_csum"},
66
67	{	E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
68			"compression" },
69	{	E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
70			"filetype" },
71	{	E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
72			"needs_recovery" },
73	{	E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
74			"journal_dev" },
75	{	E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
76			"extent" },
77	{	E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
78			"extents" },
79	{	E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
80			"meta_bg" },
81	{	E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
82			"64bit" },
83	{       E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
84                        "flex_bg"},
85	{	0, 0, 0 },
86};
87
88static struct feature jrnl_feature_list[] = {
89       {       E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
90                       "journal_checksum" },
91
92       {       E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
93                       "journal_incompat_revoke" },
94       {       E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
95                       "journal_async_commit" },
96       {       0, 0, 0 },
97};
98
99const char *e2p_feature2string(int compat, unsigned int mask)
100{
101	struct feature  *f;
102	static char buf[20];
103	char	fchar;
104	int	fnum;
105
106	for (f = feature_list; f->string; f++) {
107		if ((compat == f->compat) &&
108		    (mask == f->mask))
109			return f->string;
110	}
111	switch (compat) {
112	case  E2P_FEATURE_COMPAT:
113		fchar = 'C';
114		break;
115	case E2P_FEATURE_INCOMPAT:
116		fchar = 'I';
117		break;
118	case E2P_FEATURE_RO_INCOMPAT:
119		fchar = 'R';
120		break;
121	default:
122		fchar = '?';
123		break;
124	}
125	for (fnum = 0; mask >>= 1; fnum++);
126	sprintf(buf, "FEATURE_%c%d", fchar, fnum);
127	return buf;
128}
129
130int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
131{
132	struct feature  *f;
133	char		*eptr;
134	int		num;
135
136	for (f = feature_list; f->string; f++) {
137		if (!strcasecmp(string, f->string)) {
138			*compat_type = f->compat;
139			*mask = f->mask;
140			return 0;
141		}
142	}
143	if (strncasecmp(string, "FEATURE_", 8))
144		return 1;
145
146	switch (string[8]) {
147	case 'c':
148	case 'C':
149		*compat_type = E2P_FEATURE_COMPAT;
150		break;
151	case 'i':
152	case 'I':
153		*compat_type = E2P_FEATURE_INCOMPAT;
154		break;
155	case 'r':
156	case 'R':
157		*compat_type = E2P_FEATURE_RO_INCOMPAT;
158		break;
159	default:
160		return 1;
161	}
162	if (string[9] == 0)
163		return 1;
164	num = strtol(string+9, &eptr, 10);
165	if (num > 32 || num < 0)
166		return 1;
167	if (*eptr)
168		return 1;
169	*mask = 1 << num;
170	return 0;
171}
172
173const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
174{
175	struct feature  *f;
176	static char buf[20];
177	char	fchar;
178	int	fnum;
179
180	for (f = jrnl_feature_list; f->string; f++) {
181		if ((compat == f->compat) &&
182		    (mask == f->mask))
183			return f->string;
184	}
185	switch (compat) {
186	case  E2P_FEATURE_COMPAT:
187		fchar = 'C';
188		break;
189	case E2P_FEATURE_INCOMPAT:
190		fchar = 'I';
191		break;
192	case E2P_FEATURE_RO_INCOMPAT:
193		fchar = 'R';
194		break;
195	default:
196		fchar = '?';
197		break;
198	}
199	for (fnum = 0; mask >>= 1; fnum++);
200	sprintf(buf, "FEATURE_%c%d", fchar, fnum);
201	return buf;
202}
203
204int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
205{
206	struct feature  *f;
207	char		*eptr;
208	int		num;
209
210	for (f = jrnl_feature_list; f->string; f++) {
211		if (!strcasecmp(string, f->string)) {
212			*compat_type = f->compat;
213			*mask = f->mask;
214			return 0;
215		}
216	}
217	if (strncasecmp(string, "FEATURE_", 8))
218		return 1;
219
220	switch (string[8]) {
221	case 'c':
222	case 'C':
223		*compat_type = E2P_FEATURE_COMPAT;
224		break;
225	case 'i':
226	case 'I':
227		*compat_type = E2P_FEATURE_INCOMPAT;
228		break;
229	case 'r':
230	case 'R':
231		*compat_type = E2P_FEATURE_RO_INCOMPAT;
232		break;
233	default:
234		return 1;
235	}
236	if (string[9] == 0)
237		return 1;
238	num = strtol(string+9, &eptr, 10);
239	if (num > 32 || num < 0)
240		return 1;
241	if (*eptr)
242		return 1;
243	*mask = 1 << num;
244	return 0;
245}
246static char *skip_over_blanks(char *cp)
247{
248	while (*cp && isspace(*cp))
249		cp++;
250	return cp;
251}
252
253static char *skip_over_word(char *cp)
254{
255	while (*cp && !isspace(*cp) && *cp != ',')
256		cp++;
257	return cp;
258}
259
260/*
261 * Edit a feature set array as requested by the user.  The ok_array,
262 * if set, allows the application to limit what features the user is
263 * allowed to set or clear using this function.  If clear_ok_array is set,
264 * then use it tell whether or not it is OK to clear a filesystem feature.
265 */
266int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
267		      __u32 *clear_ok_array, int *type_err,
268		      unsigned int *mask_err)
269{
270	char		*cp, *buf, *next;
271	int		neg;
272	unsigned int	mask;
273	int		compat_type;
274	int		rc = 0;
275
276	if (!clear_ok_array)
277		clear_ok_array = ok_array;
278
279	if (type_err)
280		*type_err = 0;
281	if (mask_err)
282		*mask_err = 0;
283
284	buf = malloc(strlen(str)+1);
285	if (!buf)
286		return 1;
287	strcpy(buf, str);
288	for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
289		neg = 0;
290		cp = skip_over_blanks(cp);
291		next = skip_over_word(cp);
292
293		if (*next == 0)
294			next = 0;
295		else
296			*next = 0;
297
298		if ((strcasecmp(cp, "none") == 0) ||
299		    (strcasecmp(cp, "clear") == 0)) {
300			compat_array[0] = 0;
301			compat_array[1] = 0;
302			compat_array[2] = 0;
303			continue;
304		}
305
306		switch (*cp) {
307		case '-':
308		case '^':
309			neg++;
310		case '+':
311			cp++;
312			break;
313		}
314		if (e2p_string2feature(cp, &compat_type, &mask)) {
315			rc = 1;
316			break;
317		}
318		if (neg) {
319			if (clear_ok_array &&
320			    !(clear_ok_array[compat_type] & mask)) {
321				rc = 1;
322				if (type_err)
323					*type_err = (compat_type |
324						     E2P_FEATURE_NEGATE_FLAG);
325				if (mask_err)
326					*mask_err = mask;
327				break;
328			}
329			compat_array[compat_type] &= ~mask;
330		} else {
331			if (ok_array && !(ok_array[compat_type] & mask)) {
332				rc = 1;
333				if (type_err)
334					*type_err = compat_type;
335				if (mask_err)
336					*mask_err = mask;
337				break;
338			}
339			compat_array[compat_type] |= mask;
340		}
341	}
342	free(buf);
343	return rc;
344}
345
346int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
347{
348	return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
349}
350
351#ifdef TEST_PROGRAM
352int main(int argc, char **argv)
353{
354	int compat, compat2, i;
355	unsigned int mask, mask2;
356	const char *str;
357	struct feature *f;
358
359	for (i = 0; i < 2; i++) {
360		if (i == 0) {
361			f = feature_list;
362			printf("Feature list:\n");
363		} else {
364			printf("\nJournal feature list:\n");
365			f = jrnl_feature_list;
366		}
367		for (; f->string; f++) {
368			if (i == 0) {
369				e2p_string2feature((char *)f->string, &compat,
370						   &mask);
371				str = e2p_feature2string(compat, mask);
372			} else {
373				e2p_jrnl_string2feature((char *)f->string,
374							&compat, &mask);
375				str = e2p_jrnl_feature2string(compat, mask);
376			}
377
378			printf("\tCompat = %d, Mask = %u, %s\n",
379			       compat, mask, f->string);
380			if (strcmp(f->string, str)) {
381				if (e2p_string2feature((char *) str, &compat2,
382						       &mask2) ||
383				    (compat2 != compat) ||
384				    (mask2 != mask)) {
385					fprintf(stderr, "Failure!\n");
386					exit(1);
387				}
388			}
389		}
390	}
391	exit(0);
392}
393#endif
394