2 * feature.c --- convert between features and strings
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
20 #include <ext2fs/ext2fs.h>
21 #include <ext2fs/jfs_user.h>
29 static struct feature feature_list[] = {
30 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
32 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
38 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
40 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
42 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
44 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXCLUDE_BITMAP,
46 { E2P_FEATURE_COMPAT, EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
49 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
51 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
55 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
57 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
59 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
61 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
63 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_QUOTA,
65 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_BIGALLOC,
67 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM,
69 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_REPLICA,
72 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
74 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
76 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
78 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
80 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
82 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
84 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
86 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
88 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP,
90 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
92 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_EA_INODE,
94 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_DIRDATA,
96 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_LARGEDIR,
98 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_INLINE_DATA,
103 static struct feature jrnl_feature_list[] = {
104 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
105 "journal_checksum" },
107 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
108 "journal_incompat_revoke" },
109 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_64BIT,
111 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
112 "journal_async_commit" },
113 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_CSUM_V2,
114 "journal_checksum_v2" },
118 const char *e2p_feature2string(int compat, unsigned int mask)
125 for (f = feature_list; f->string; f++) {
126 if ((compat == f->compat) &&
131 case E2P_FEATURE_COMPAT:
134 case E2P_FEATURE_INCOMPAT:
137 case E2P_FEATURE_RO_INCOMPAT:
144 for (fnum = 0; mask >>= 1; fnum++);
145 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
149 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
155 for (f = feature_list; f->string; f++) {
156 if (!strcasecmp(string, f->string)) {
157 *compat_type = f->compat;
162 if (strncasecmp(string, "FEATURE_", 8))
168 *compat_type = E2P_FEATURE_COMPAT;
172 *compat_type = E2P_FEATURE_INCOMPAT;
176 *compat_type = E2P_FEATURE_RO_INCOMPAT;
183 num = strtol(string+9, &eptr, 10);
184 if (num > 32 || num < 0)
192 const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
199 for (f = jrnl_feature_list; f->string; f++) {
200 if ((compat == f->compat) &&
205 case E2P_FEATURE_COMPAT:
208 case E2P_FEATURE_INCOMPAT:
211 case E2P_FEATURE_RO_INCOMPAT:
218 for (fnum = 0; mask >>= 1; fnum++);
219 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
223 int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
229 for (f = jrnl_feature_list; f->string; f++) {
230 if (!strcasecmp(string, f->string)) {
231 *compat_type = f->compat;
236 if (strncasecmp(string, "FEATURE_", 8))
242 *compat_type = E2P_FEATURE_COMPAT;
246 *compat_type = E2P_FEATURE_INCOMPAT;
250 *compat_type = E2P_FEATURE_RO_INCOMPAT;
257 num = strtol(string+9, &eptr, 10);
258 if (num > 32 || num < 0)
265 static char *skip_over_blanks(char *cp)
267 while (*cp && isspace(*cp))
272 static char *skip_over_word(char *cp)
274 while (*cp && !isspace(*cp) && *cp != ',')
280 * Edit a feature set array as requested by the user. The ok_array,
281 * if set, allows the application to limit what features the user is
282 * allowed to set or clear using this function. If clear_ok_array is set,
283 * then use it tell whether or not it is OK to clear a filesystem feature.
285 int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
286 __u32 *clear_ok_array, int *type_err,
287 unsigned int *mask_err)
289 char *cp, *buf, *next;
296 clear_ok_array = ok_array;
303 buf = malloc(strlen(str)+1);
307 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
309 cp = skip_over_blanks(cp);
310 next = skip_over_word(cp);
317 if ((strcasecmp(cp, "none") == 0) ||
318 (strcasecmp(cp, "clear") == 0)) {
334 if (e2p_string2feature(cp, &compat_type, &mask)) {
339 if (clear_ok_array &&
340 !(clear_ok_array[compat_type] & mask)) {
343 *type_err = (compat_type |
344 E2P_FEATURE_NEGATE_FLAG);
349 compat_array[compat_type] &= ~mask;
351 if (ok_array && !(ok_array[compat_type] & mask)) {
354 *type_err = compat_type;
359 compat_array[compat_type] |= mask;
366 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
368 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
372 int main(int argc, char **argv)
374 int compat, compat2, i;
375 unsigned int mask, mask2;
379 for (i = 0; i < 2; i++) {
382 printf("Feature list:\n");
384 printf("\nJournal feature list:\n");
385 f = jrnl_feature_list;
387 for (; f->string; f++) {
389 e2p_string2feature((char *)f->string, &compat,
391 str = e2p_feature2string(compat, mask);
393 e2p_jrnl_string2feature((char *)f->string,
395 str = e2p_jrnl_feature2string(compat, mask);
398 printf("\tCompat = %d, Mask = %u, %s\n",
399 compat, mask, f->string);
400 if (strcmp(f->string, str)) {
401 if (e2p_string2feature((char *) str, &compat2,
403 (compat2 != compat) ||
405 fprintf(stderr, "Failure!\n");