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
,
100 { E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_ENCRYPT
,
105 static struct feature jrnl_feature_list
[] = {
106 { E2P_FEATURE_COMPAT
, JFS_FEATURE_COMPAT_CHECKSUM
,
107 "journal_checksum" },
109 { E2P_FEATURE_INCOMPAT
, JFS_FEATURE_INCOMPAT_REVOKE
,
110 "journal_incompat_revoke" },
111 { E2P_FEATURE_INCOMPAT
, JFS_FEATURE_INCOMPAT_64BIT
,
113 { E2P_FEATURE_INCOMPAT
, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT
,
114 "journal_async_commit" },
115 { E2P_FEATURE_INCOMPAT
, JFS_FEATURE_INCOMPAT_CSUM_V2
,
116 "journal_checksum_v2" },
117 { E2P_FEATURE_INCOMPAT
, JFS_FEATURE_INCOMPAT_CSUM_V3
,
118 "journal_checksum_v3" },
122 const char *e2p_feature2string(int compat
, unsigned int mask
)
129 for (f
= feature_list
; f
->string
; f
++) {
130 if ((compat
== f
->compat
) &&
135 case E2P_FEATURE_COMPAT
:
138 case E2P_FEATURE_INCOMPAT
:
141 case E2P_FEATURE_RO_INCOMPAT
:
148 for (fnum
= 0; mask
>>= 1; fnum
++);
149 sprintf(buf
, "FEATURE_%c%d", fchar
, fnum
);
153 int e2p_string2feature(char *string
, int *compat_type
, unsigned int *mask
)
159 for (f
= feature_list
; f
->string
; f
++) {
160 if (!strcasecmp(string
, f
->string
)) {
161 *compat_type
= f
->compat
;
166 if (strncasecmp(string
, "FEATURE_", 8))
172 *compat_type
= E2P_FEATURE_COMPAT
;
176 *compat_type
= E2P_FEATURE_INCOMPAT
;
180 *compat_type
= E2P_FEATURE_RO_INCOMPAT
;
187 num
= strtol(string
+9, &eptr
, 10);
188 if (num
> 32 || num
< 0)
196 const char *e2p_jrnl_feature2string(int compat
, unsigned int mask
)
203 for (f
= jrnl_feature_list
; f
->string
; f
++) {
204 if ((compat
== f
->compat
) &&
209 case E2P_FEATURE_COMPAT
:
212 case E2P_FEATURE_INCOMPAT
:
215 case E2P_FEATURE_RO_INCOMPAT
:
222 for (fnum
= 0; mask
>>= 1; fnum
++);
223 sprintf(buf
, "FEATURE_%c%d", fchar
, fnum
);
227 int e2p_jrnl_string2feature(char *string
, int *compat_type
, unsigned int *mask
)
233 for (f
= jrnl_feature_list
; f
->string
; f
++) {
234 if (!strcasecmp(string
, f
->string
)) {
235 *compat_type
= f
->compat
;
240 if (strncasecmp(string
, "FEATURE_", 8))
246 *compat_type
= E2P_FEATURE_COMPAT
;
250 *compat_type
= E2P_FEATURE_INCOMPAT
;
254 *compat_type
= E2P_FEATURE_RO_INCOMPAT
;
261 num
= strtol(string
+9, &eptr
, 10);
262 if (num
> 32 || num
< 0)
269 static char *skip_over_blanks(char *cp
)
271 while (*cp
&& isspace(*cp
))
276 static char *skip_over_word(char *cp
)
278 while (*cp
&& !isspace(*cp
) && *cp
!= ',')
284 * Edit a feature set array as requested by the user. The ok_array,
285 * if set, allows the application to limit what features the user is
286 * allowed to set or clear using this function. If clear_ok_array is set,
287 * then use it tell whether or not it is OK to clear a filesystem feature.
289 int e2p_edit_feature2(const char *str
, __u32
*compat_array
, __u32
*ok_array
,
290 __u32
*clear_ok_array
, int *type_err
,
291 unsigned int *mask_err
)
293 char *cp
, *buf
, *next
;
300 clear_ok_array
= ok_array
;
307 buf
= malloc(strlen(str
)+1);
311 for (cp
= buf
; cp
&& *cp
; cp
= next
? next
+1 : 0) {
313 cp
= skip_over_blanks(cp
);
314 next
= skip_over_word(cp
);
321 if ((strcasecmp(cp
, "none") == 0) ||
322 (strcasecmp(cp
, "clear") == 0)) {
338 if (e2p_string2feature(cp
, &compat_type
, &mask
)) {
343 if (clear_ok_array
&&
344 !(clear_ok_array
[compat_type
] & mask
)) {
347 *type_err
= (compat_type
|
348 E2P_FEATURE_NEGATE_FLAG
);
353 compat_array
[compat_type
] &= ~mask
;
355 if (ok_array
&& !(ok_array
[compat_type
] & mask
)) {
358 *type_err
= compat_type
;
363 compat_array
[compat_type
] |= mask
;
370 int e2p_edit_feature(const char *str
, __u32
*compat_array
, __u32
*ok_array
)
372 return e2p_edit_feature2(str
, compat_array
, ok_array
, 0, 0, 0);
376 int main(int argc
, char **argv
)
378 int compat
, compat2
, i
;
379 unsigned int mask
, mask2
;
383 for (i
= 0; i
< 2; i
++) {
386 printf("Feature list:\n");
388 printf("\nJournal feature list:\n");
389 f
= jrnl_feature_list
;
391 for (; f
->string
; f
++) {
393 e2p_string2feature((char *)f
->string
, &compat
,
395 str
= e2p_feature2string(compat
, mask
);
397 e2p_jrnl_string2feature((char *)f
->string
,
399 str
= e2p_jrnl_feature2string(compat
, mask
);
402 printf("\tCompat = %d, Mask = %u, %s\n",
403 compat
, mask
, f
->string
);
404 if (strcmp(f
->string
, str
)) {
405 if (e2p_string2feature((char *) str
, &compat2
,
407 (compat2
!= compat
) ||
409 fprintf(stderr
, "Failure!\n");