]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/e2p/feature.c
e2fuzz: fix clang warning
[thirdparty/e2fsprogs.git] / lib / e2p / feature.c
CommitLineData
d7b701de
TT
1/*
2 * feature.c --- convert between features and strings
efc6f628 3 *
d7b701de 4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
efc6f628 5 *
543547a5
TT
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
d7b701de
TT
10 */
11
d1154eb4 12#include "config.h"
d7b701de
TT
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
36caf25f 16#include <ctype.h>
d7b701de
TT
17#include <errno.h>
18
19#include "e2p.h"
03efde8a
AD
20#include <ext2fs/ext2fs.h>
21#include <ext2fs/jfs_user.h>
d7b701de 22
d7b701de
TT
23struct feature {
24 int compat;
25 unsigned int mask;
36caf25f 26 const char *string;
d7b701de
TT
27};
28
e2207ce5 29static struct feature feature_list[] = {
d7b701de
TT
30 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
31 "dir_prealloc" },
32 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
33 "has_journal" },
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
35 "imagic_inodes" },
342d847d
TT
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
37 "ext_attr" },
49d5ddcc
TT
38 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
39 "dir_index" },
40 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
41 "resize_inode" },
f5fa2007
TT
42 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
43 "lazy_bg" },
89efc88e 44 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXCLUDE_BITMAP,
16c581d0 45 "snapshot_bitmap" },
65c6c3e0
TT
46 { E2P_FEATURE_COMPAT, EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
47 "sparse_super2" },
477007e1 48
d7b701de
TT
49 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
50 "sparse_super" },
51 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
52 "large_file" },
477007e1
TT
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
54 "huge_file" },
2930dad2
TT
55 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
56 "uninit_bg" },
477007e1 57 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
3542ba0d 58 "uninit_groups" },
477007e1
TT
59 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
60 "dir_nlink" },
61 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
62 "extra_isize" },
0edcc270
AK
63 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_QUOTA,
64 "quota" },
412376ef
TT
65 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_BIGALLOC,
66 "bigalloc"},
89efc88e
TT
67 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM,
68 "metadata_csum"},
991211f6
TT
69 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_REPLICA,
70 "replica" },
477007e1 71
d7b701de
TT
72 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
73 "compression" },
74 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
75 "filetype" },
76 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
77 "needs_recovery" },
c2204b32
TT
78 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
79 "journal_dev" },
ba1e1254
TT
80 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
81 "extent" },
39dc1c45
TT
82 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
83 "extents" },
c046ac7f
TT
84 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
85 "meta_bg" },
477007e1
TT
86 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
87 "64bit" },
0f5eba75
AD
88 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP,
89 "mmp" },
c2d4300b 90 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
0f5eba75 91 "flex_bg"},
660b4c3b
TT
92 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_EA_INODE,
93 "ea_inode"},
94 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_DIRDATA,
95 "dirdata"},
96 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_LARGEDIR,
97 "large_dir"},
94846158 98 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_INLINE_DATA,
660b4c3b 99 "inline_data"},
d7b701de
TT
100 { 0, 0, 0 },
101};
102
03efde8a
AD
103static struct feature jrnl_feature_list[] = {
104 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
105 "journal_checksum" },
106
107 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
108 "journal_incompat_revoke" },
8c778b3a
TT
109 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_64BIT,
110 "journal_64bit" },
03efde8a
AD
111 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
112 "journal_async_commit" },
382fad03
DW
113 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_CSUM_V2,
114 "journal_checksum_v2" },
38d5adf3
DW
115 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_CSUM_V3,
116 "journal_checksum_v3" },
03efde8a
AD
117 { 0, 0, 0 },
118};
119
36caf25f 120const char *e2p_feature2string(int compat, unsigned int mask)
d7b701de
TT
121{
122 struct feature *f;
123 static char buf[20];
124 char fchar;
125 int fnum;
126
127 for (f = feature_list; f->string; f++) {
128 if ((compat == f->compat) &&
129 (mask == f->mask))
130 return f->string;
131 }
132 switch (compat) {
133 case E2P_FEATURE_COMPAT:
134 fchar = 'C';
135 break;
136 case E2P_FEATURE_INCOMPAT:
137 fchar = 'I';
138 break;
139 case E2P_FEATURE_RO_INCOMPAT:
140 fchar = 'R';
141 break;
142 default:
143 fchar = '?';
144 break;
145 }
146 for (fnum = 0; mask >>= 1; fnum++);
147 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
148 return buf;
149}
150
36caf25f 151int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
d7b701de
TT
152{
153 struct feature *f;
154 char *eptr;
155 int num;
156
157 for (f = feature_list; f->string; f++) {
158 if (!strcasecmp(string, f->string)) {
36caf25f 159 *compat_type = f->compat;
d7b701de
TT
160 *mask = f->mask;
161 return 0;
162 }
163 }
164 if (strncasecmp(string, "FEATURE_", 8))
165 return 1;
166
167 switch (string[8]) {
168 case 'c':
169 case 'C':
36caf25f 170 *compat_type = E2P_FEATURE_COMPAT;
d7b701de
TT
171 break;
172 case 'i':
173 case 'I':
36caf25f 174 *compat_type = E2P_FEATURE_INCOMPAT;
d7b701de
TT
175 break;
176 case 'r':
177 case 'R':
36caf25f 178 *compat_type = E2P_FEATURE_RO_INCOMPAT;
d7b701de
TT
179 break;
180 default:
181 return 1;
182 }
183 if (string[9] == 0)
184 return 1;
185 num = strtol(string+9, &eptr, 10);
186 if (num > 32 || num < 0)
187 return 1;
188 if (*eptr)
189 return 1;
190 *mask = 1 << num;
191 return 0;
192}
193
03efde8a
AD
194const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
195{
196 struct feature *f;
197 static char buf[20];
198 char fchar;
199 int fnum;
200
201 for (f = jrnl_feature_list; f->string; f++) {
202 if ((compat == f->compat) &&
203 (mask == f->mask))
204 return f->string;
205 }
206 switch (compat) {
207 case E2P_FEATURE_COMPAT:
208 fchar = 'C';
209 break;
210 case E2P_FEATURE_INCOMPAT:
211 fchar = 'I';
212 break;
213 case E2P_FEATURE_RO_INCOMPAT:
214 fchar = 'R';
215 break;
216 default:
217 fchar = '?';
218 break;
219 }
220 for (fnum = 0; mask >>= 1; fnum++);
221 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
222 return buf;
223}
224
225int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
226{
227 struct feature *f;
228 char *eptr;
229 int num;
230
231 for (f = jrnl_feature_list; f->string; f++) {
232 if (!strcasecmp(string, f->string)) {
233 *compat_type = f->compat;
234 *mask = f->mask;
235 return 0;
236 }
237 }
238 if (strncasecmp(string, "FEATURE_", 8))
239 return 1;
240
241 switch (string[8]) {
242 case 'c':
243 case 'C':
244 *compat_type = E2P_FEATURE_COMPAT;
245 break;
246 case 'i':
247 case 'I':
248 *compat_type = E2P_FEATURE_INCOMPAT;
249 break;
250 case 'r':
251 case 'R':
252 *compat_type = E2P_FEATURE_RO_INCOMPAT;
253 break;
254 default:
255 return 1;
256 }
257 if (string[9] == 0)
258 return 1;
259 num = strtol(string+9, &eptr, 10);
260 if (num > 32 || num < 0)
261 return 1;
262 if (*eptr)
263 return 1;
264 *mask = 1 << num;
265 return 0;
266}
d7b701de
TT
267static char *skip_over_blanks(char *cp)
268{
269 while (*cp && isspace(*cp))
270 cp++;
271 return cp;
272}
273
36caf25f 274static char *skip_over_word(char *cp)
d7b701de 275{
944ab713 276 while (*cp && !isspace(*cp) && *cp != ',')
d7b701de
TT
277 cp++;
278 return cp;
279}
280
944ab713
TT
281/*
282 * Edit a feature set array as requested by the user. The ok_array,
283 * if set, allows the application to limit what features the user is
efc6f628 284 * allowed to set or clear using this function. If clear_ok_array is set,
7100351d 285 * then use it tell whether or not it is OK to clear a filesystem feature.
944ab713 286 */
7100351d 287int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
efc6f628 288 __u32 *clear_ok_array, int *type_err,
7100351d 289 unsigned int *mask_err)
d7b701de 290{
2711ca1c
BB
291 char *cp, *buf, *next;
292 int neg;
36caf25f
TT
293 unsigned int mask;
294 int compat_type;
2711ca1c 295 int rc = 0;
d7b701de 296
7100351d
TT
297 if (!clear_ok_array)
298 clear_ok_array = ok_array;
299
300 if (type_err)
301 *type_err = 0;
302 if (mask_err)
303 *mask_err = 0;
304
d7b701de 305 buf = malloc(strlen(str)+1);
944ab713 306 if (!buf)
d7b701de 307 return 1;
d7b701de 308 strcpy(buf, str);
9dc6ad1e 309 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
d7b701de
TT
310 neg = 0;
311 cp = skip_over_blanks(cp);
312 next = skip_over_word(cp);
efc6f628 313
d7b701de
TT
314 if (*next == 0)
315 next = 0;
316 else
317 *next = 0;
9dc6ad1e
TT
318
319 if ((strcasecmp(cp, "none") == 0) ||
320 (strcasecmp(cp, "clear") == 0)) {
321 compat_array[0] = 0;
322 compat_array[1] = 0;
323 compat_array[2] = 0;
324 continue;
325 }
326
d7b701de
TT
327 switch (*cp) {
328 case '-':
329 case '^':
330 neg++;
9e30fb23 331 /* fallthrough */
d7b701de
TT
332 case '+':
333 cp++;
334 break;
335 }
2711ca1c
BB
336 if (e2p_string2feature(cp, &compat_type, &mask)) {
337 rc = 1;
338 break;
339 }
7100351d 340 if (neg) {
efc6f628 341 if (clear_ok_array &&
7100351d
TT
342 !(clear_ok_array[compat_type] & mask)) {
343 rc = 1;
344 if (type_err)
efc6f628 345 *type_err = (compat_type |
7100351d
TT
346 E2P_FEATURE_NEGATE_FLAG);
347 if (mask_err)
348 *mask_err = mask;
349 break;
350 }
36caf25f 351 compat_array[compat_type] &= ~mask;
7100351d
TT
352 } else {
353 if (ok_array && !(ok_array[compat_type] & mask)) {
354 rc = 1;
355 if (type_err)
356 *type_err = compat_type;
357 if (mask_err)
358 *mask_err = mask;
359 break;
360 }
36caf25f 361 compat_array[compat_type] |= mask;
7100351d 362 }
d7b701de 363 }
2711ca1c
BB
364 free(buf);
365 return rc;
d7b701de 366}
7100351d
TT
367
368int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
369{
370 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
371}
03efde8a
AD
372
373#ifdef TEST_PROGRAM
374int main(int argc, char **argv)
375{
376 int compat, compat2, i;
377 unsigned int mask, mask2;
378 const char *str;
379 struct feature *f;
380
381 for (i = 0; i < 2; i++) {
382 if (i == 0) {
383 f = feature_list;
384 printf("Feature list:\n");
385 } else {
386 printf("\nJournal feature list:\n");
387 f = jrnl_feature_list;
388 }
389 for (; f->string; f++) {
390 if (i == 0) {
391 e2p_string2feature((char *)f->string, &compat,
392 &mask);
393 str = e2p_feature2string(compat, mask);
394 } else {
395 e2p_jrnl_string2feature((char *)f->string,
396 &compat, &mask);
397 str = e2p_jrnl_feature2string(compat, mask);
398 }
399
400 printf("\tCompat = %d, Mask = %u, %s\n",
401 compat, mask, f->string);
402 if (strcmp(f->string, str)) {
403 if (e2p_string2feature((char *) str, &compat2,
404 &mask2) ||
405 (compat2 != compat) ||
406 (mask2 != mask)) {
407 fprintf(stderr, "Failure!\n");
408 exit(1);
409 }
410 }
411 }
412 }
413 exit(0);
414}
415#endif