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,
47 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
49 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
51 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
55 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
57 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
59 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
61 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_QUOTA,
63 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_BIGALLOC,
65 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM,
68 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
70 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
72 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
74 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
76 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
78 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
80 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
82 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
84 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
89 static struct feature jrnl_feature_list[] = {
90 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
93 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
94 "journal_incompat_revoke" },
95 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
96 "journal_async_commit" },
100 const char *e2p_feature2string(int compat, unsigned int mask)
107 for (f = feature_list; f->string; f++) {
108 if ((compat == f->compat) &&
113 case E2P_FEATURE_COMPAT:
116 case E2P_FEATURE_INCOMPAT:
119 case E2P_FEATURE_RO_INCOMPAT:
126 for (fnum = 0; mask >>= 1; fnum++);
127 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
131 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
137 for (f = feature_list; f->string; f++) {
138 if (!strcasecmp(string, f->string)) {
139 *compat_type = f->compat;
144 if (strncasecmp(string, "FEATURE_", 8))
150 *compat_type = E2P_FEATURE_COMPAT;
154 *compat_type = E2P_FEATURE_INCOMPAT;
158 *compat_type = E2P_FEATURE_RO_INCOMPAT;
165 num = strtol(string+9, &eptr, 10);
166 if (num > 32 || num < 0)
174 const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
181 for (f = jrnl_feature_list; f->string; f++) {
182 if ((compat == f->compat) &&
187 case E2P_FEATURE_COMPAT:
190 case E2P_FEATURE_INCOMPAT:
193 case E2P_FEATURE_RO_INCOMPAT:
200 for (fnum = 0; mask >>= 1; fnum++);
201 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
205 int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
211 for (f = jrnl_feature_list; f->string; f++) {
212 if (!strcasecmp(string, f->string)) {
213 *compat_type = f->compat;
218 if (strncasecmp(string, "FEATURE_", 8))
224 *compat_type = E2P_FEATURE_COMPAT;
228 *compat_type = E2P_FEATURE_INCOMPAT;
232 *compat_type = E2P_FEATURE_RO_INCOMPAT;
239 num = strtol(string+9, &eptr, 10);
240 if (num > 32 || num < 0)
247 static char *skip_over_blanks(char *cp)
249 while (*cp && isspace(*cp))
254 static char *skip_over_word(char *cp)
256 while (*cp && !isspace(*cp) && *cp != ',')
262 * Edit a feature set array as requested by the user. The ok_array,
263 * if set, allows the application to limit what features the user is
264 * allowed to set or clear using this function. If clear_ok_array is set,
265 * then use it tell whether or not it is OK to clear a filesystem feature.
267 int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
268 __u32 *clear_ok_array, int *type_err,
269 unsigned int *mask_err)
271 char *cp, *buf, *next;
278 clear_ok_array = ok_array;
285 buf = malloc(strlen(str)+1);
289 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
291 cp = skip_over_blanks(cp);
292 next = skip_over_word(cp);
299 if ((strcasecmp(cp, "none") == 0) ||
300 (strcasecmp(cp, "clear") == 0)) {
316 if (e2p_string2feature(cp, &compat_type, &mask)) {
321 if (clear_ok_array &&
322 !(clear_ok_array[compat_type] & mask)) {
325 *type_err = (compat_type |
326 E2P_FEATURE_NEGATE_FLAG);
331 compat_array[compat_type] &= ~mask;
333 if (ok_array && !(ok_array[compat_type] & mask)) {
336 *type_err = compat_type;
341 compat_array[compat_type] |= mask;
348 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
350 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
354 int main(int argc, char **argv)
356 int compat, compat2, i;
357 unsigned int mask, mask2;
361 for (i = 0; i < 2; i++) {
364 printf("Feature list:\n");
366 printf("\nJournal feature list:\n");
367 f = jrnl_feature_list;
369 for (; f->string; f++) {
371 e2p_string2feature((char *)f->string, &compat,
373 str = e2p_feature2string(compat, mask);
375 e2p_jrnl_string2feature((char *)f->string,
377 str = e2p_jrnl_feature2string(compat, mask);
380 printf("\tCompat = %d, Mask = %u, %s\n",
381 compat, mask, f->string);
382 if (strcmp(f->string, str)) {
383 if (e2p_string2feature((char *) str, &compat2,
385 (compat2 != compat) ||
387 fprintf(stderr, "Failure!\n");