2 * feature.c --- convert between features and strings
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
6 * This file can be redistributed under the terms of the GNU Library General
18 #include <ext2fs/ext2fs.h>
19 #include <ext2fs/jfs_user.h>
27 static struct feature feature_list[] = {
28 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
30 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
32 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
38 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
40 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
43 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
45 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
47 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
49 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
51 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
55 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
58 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
60 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
62 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
64 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
66 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
68 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
70 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
72 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
74 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
79 static struct feature jrnl_feature_list[] = {
80 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
83 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
84 "journal_incompat_revoke" },
85 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
86 "journal_async_commit" },
90 const char *e2p_feature2string(int compat, unsigned int mask)
97 for (f = feature_list; f->string; f++) {
98 if ((compat == f->compat) &&
103 case E2P_FEATURE_COMPAT:
106 case E2P_FEATURE_INCOMPAT:
109 case E2P_FEATURE_RO_INCOMPAT:
116 for (fnum = 0; mask >>= 1; fnum++);
117 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
121 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
127 for (f = feature_list; f->string; f++) {
128 if (!strcasecmp(string, f->string)) {
129 *compat_type = f->compat;
134 if (strncasecmp(string, "FEATURE_", 8))
140 *compat_type = E2P_FEATURE_COMPAT;
144 *compat_type = E2P_FEATURE_INCOMPAT;
148 *compat_type = E2P_FEATURE_RO_INCOMPAT;
155 num = strtol(string+9, &eptr, 10);
156 if (num > 32 || num < 0)
164 const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
171 for (f = jrnl_feature_list; f->string; f++) {
172 if ((compat == f->compat) &&
177 case E2P_FEATURE_COMPAT:
180 case E2P_FEATURE_INCOMPAT:
183 case E2P_FEATURE_RO_INCOMPAT:
190 for (fnum = 0; mask >>= 1; fnum++);
191 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
195 int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
201 for (f = jrnl_feature_list; f->string; f++) {
202 if (!strcasecmp(string, f->string)) {
203 *compat_type = f->compat;
208 if (strncasecmp(string, "FEATURE_", 8))
214 *compat_type = E2P_FEATURE_COMPAT;
218 *compat_type = E2P_FEATURE_INCOMPAT;
222 *compat_type = E2P_FEATURE_RO_INCOMPAT;
229 num = strtol(string+9, &eptr, 10);
230 if (num > 32 || num < 0)
237 static char *skip_over_blanks(char *cp)
239 while (*cp && isspace(*cp))
244 static char *skip_over_word(char *cp)
246 while (*cp && !isspace(*cp) && *cp != ',')
252 * Edit a feature set array as requested by the user. The ok_array,
253 * if set, allows the application to limit what features the user is
254 * allowed to set or clear using this function. If clear_ok_array is set,
255 * then use it tell whether or not it is OK to clear a filesystem feature.
257 int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
258 __u32 *clear_ok_array, int *type_err,
259 unsigned int *mask_err)
261 char *cp, *buf, *next;
268 clear_ok_array = ok_array;
275 buf = malloc(strlen(str)+1);
279 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
281 cp = skip_over_blanks(cp);
282 next = skip_over_word(cp);
289 if ((strcasecmp(cp, "none") == 0) ||
290 (strcasecmp(cp, "clear") == 0)) {
305 if (e2p_string2feature(cp, &compat_type, &mask)) {
310 if (clear_ok_array &&
311 !(clear_ok_array[compat_type] & mask)) {
314 *type_err = (compat_type |
315 E2P_FEATURE_NEGATE_FLAG);
320 compat_array[compat_type] &= ~mask;
322 if (ok_array && !(ok_array[compat_type] & mask)) {
325 *type_err = compat_type;
330 compat_array[compat_type] |= mask;
337 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
339 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
343 int main(int argc, char **argv)
345 int compat, compat2, i;
346 unsigned int mask, mask2;
350 for (i = 0; i < 2; i++) {
353 printf("Feature list:\n");
355 printf("\nJournal feature list:\n");
356 f = jrnl_feature_list;
358 for (; f->string; f++) {
360 e2p_string2feature((char *)f->string, &compat,
362 str = e2p_feature2string(compat, mask);
364 e2p_jrnl_string2feature((char *)f->string,
366 str = e2p_jrnl_feature2string(compat, mask);
369 printf("\tCompat = %d, Mask = %u, %s\n",
370 compat, mask, f->string);
371 if (strcmp(f->string, str)) {
372 if (e2p_string2feature((char *) str, &compat2,
374 (compat2 != compat) ||
376 fprintf(stderr, "Failure!\n");