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.
19 #include <ext2fs/ext2fs.h>
20 #include <ext2fs/jfs_user.h>
28 static struct feature feature_list[] = {
29 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
31 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
33 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
35 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
37 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
39 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
41 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
44 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
46 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
48 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
50 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
52 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
54 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
56 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
59 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
61 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
63 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
65 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
67 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
69 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
71 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
73 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
75 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
80 static struct feature jrnl_feature_list[] = {
81 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
84 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
85 "journal_incompat_revoke" },
86 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
87 "journal_async_commit" },
91 const char *e2p_feature2string(int compat, unsigned int mask)
98 for (f = feature_list; f->string; f++) {
99 if ((compat == f->compat) &&
104 case E2P_FEATURE_COMPAT:
107 case E2P_FEATURE_INCOMPAT:
110 case E2P_FEATURE_RO_INCOMPAT:
117 for (fnum = 0; mask >>= 1; fnum++);
118 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
122 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
128 for (f = feature_list; f->string; f++) {
129 if (!strcasecmp(string, f->string)) {
130 *compat_type = f->compat;
135 if (strncasecmp(string, "FEATURE_", 8))
141 *compat_type = E2P_FEATURE_COMPAT;
145 *compat_type = E2P_FEATURE_INCOMPAT;
149 *compat_type = E2P_FEATURE_RO_INCOMPAT;
156 num = strtol(string+9, &eptr, 10);
157 if (num > 32 || num < 0)
165 const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
172 for (f = jrnl_feature_list; f->string; f++) {
173 if ((compat == f->compat) &&
178 case E2P_FEATURE_COMPAT:
181 case E2P_FEATURE_INCOMPAT:
184 case E2P_FEATURE_RO_INCOMPAT:
191 for (fnum = 0; mask >>= 1; fnum++);
192 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
196 int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
202 for (f = jrnl_feature_list; f->string; f++) {
203 if (!strcasecmp(string, f->string)) {
204 *compat_type = f->compat;
209 if (strncasecmp(string, "FEATURE_", 8))
215 *compat_type = E2P_FEATURE_COMPAT;
219 *compat_type = E2P_FEATURE_INCOMPAT;
223 *compat_type = E2P_FEATURE_RO_INCOMPAT;
230 num = strtol(string+9, &eptr, 10);
231 if (num > 32 || num < 0)
238 static char *skip_over_blanks(char *cp)
240 while (*cp && isspace(*cp))
245 static char *skip_over_word(char *cp)
247 while (*cp && !isspace(*cp) && *cp != ',')
253 * Edit a feature set array as requested by the user. The ok_array,
254 * if set, allows the application to limit what features the user is
255 * allowed to set or clear using this function. If clear_ok_array is set,
256 * then use it tell whether or not it is OK to clear a filesystem feature.
258 int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
259 __u32 *clear_ok_array, int *type_err,
260 unsigned int *mask_err)
262 char *cp, *buf, *next;
269 clear_ok_array = ok_array;
276 buf = malloc(strlen(str)+1);
280 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
282 cp = skip_over_blanks(cp);
283 next = skip_over_word(cp);
290 if ((strcasecmp(cp, "none") == 0) ||
291 (strcasecmp(cp, "clear") == 0)) {
306 if (e2p_string2feature(cp, &compat_type, &mask)) {
311 if (clear_ok_array &&
312 !(clear_ok_array[compat_type] & mask)) {
315 *type_err = (compat_type |
316 E2P_FEATURE_NEGATE_FLAG);
321 compat_array[compat_type] &= ~mask;
323 if (ok_array && !(ok_array[compat_type] & mask)) {
326 *type_err = compat_type;
331 compat_array[compat_type] |= mask;
338 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
340 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
344 int main(int argc, char **argv)
346 int compat, compat2, i;
347 unsigned int mask, mask2;
351 for (i = 0; i < 2; i++) {
354 printf("Feature list:\n");
356 printf("\nJournal feature list:\n");
357 f = jrnl_feature_list;
359 for (; f->string; f++) {
361 e2p_string2feature((char *)f->string, &compat,
363 str = e2p_feature2string(compat, mask);
365 e2p_jrnl_string2feature((char *)f->string,
367 str = e2p_jrnl_feature2string(compat, mask);
370 printf("\tCompat = %d, Mask = %u, %s\n",
371 compat, mask, f->string);
372 if (strcmp(f->string, str)) {
373 if (e2p_string2feature((char *) str, &compat2,
375 (compat2 != compat) ||
377 fprintf(stderr, "Failure!\n");