]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/e2p/feature.c
Rename the feature "fname_encoding" to be "casefold".
[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 20#include <ext2fs/ext2fs.h>
81f95d43 21#include <ext2fs/kernel-jbd.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" },
8b39e4cf
TT
71 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_READONLY,
72 "read-only" },
0c18d036
LX
73 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_PROJECT,
74 "project"},
611d3413
JQ
75 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS,
76 "shared_blocks"},
faae7aa0
TT
77 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_VERITY,
78 "verity"},
477007e1 79
d7b701de
TT
80 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
81 "compression" },
82 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
83 "filetype" },
84 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
85 "needs_recovery" },
c2204b32
TT
86 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
87 "journal_dev" },
ba1e1254
TT
88 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
89 "extent" },
39dc1c45
TT
90 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
91 "extents" },
c046ac7f
TT
92 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
93 "meta_bg" },
477007e1
TT
94 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
95 "64bit" },
0f5eba75
AD
96 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP,
97 "mmp" },
c2d4300b 98 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
0f5eba75 99 "flex_bg"},
660b4c3b
TT
100 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_EA_INODE,
101 "ea_inode"},
102 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_DIRDATA,
103 "dirdata"},
2ed0adbc
DW
104 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CSUM_SEED,
105 "metadata_csum_seed"},
660b4c3b
TT
106 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_LARGEDIR,
107 "large_dir"},
94846158 108 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_INLINE_DATA,
660b4c3b 109 "inline_data"},
22f22ab1
TT
110 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT,
111 "encrypt"},
28887533
TT
112 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CASEFOLD,
113 "casefold"},
114 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CASEFOLD,
e7236a94 115 "fname_encoding"},
d7b701de
TT
116 { 0, 0, 0 },
117};
118
03efde8a
AD
119static struct feature jrnl_feature_list[] = {
120 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
121 "journal_checksum" },
122
123 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
124 "journal_incompat_revoke" },
8c778b3a
TT
125 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_64BIT,
126 "journal_64bit" },
03efde8a
AD
127 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_ASYNC_COMMIT,
128 "journal_async_commit" },
382fad03
DW
129 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_CSUM_V2,
130 "journal_checksum_v2" },
38d5adf3
DW
131 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_CSUM_V3,
132 "journal_checksum_v3" },
03efde8a
AD
133 { 0, 0, 0 },
134};
135
36caf25f 136const char *e2p_feature2string(int compat, unsigned int mask)
d7b701de
TT
137{
138 struct feature *f;
139 static char buf[20];
140 char fchar;
141 int fnum;
142
143 for (f = feature_list; f->string; f++) {
144 if ((compat == f->compat) &&
145 (mask == f->mask))
146 return f->string;
147 }
148 switch (compat) {
149 case E2P_FEATURE_COMPAT:
150 fchar = 'C';
151 break;
152 case E2P_FEATURE_INCOMPAT:
153 fchar = 'I';
154 break;
155 case E2P_FEATURE_RO_INCOMPAT:
156 fchar = 'R';
157 break;
158 default:
159 fchar = '?';
160 break;
161 }
162 for (fnum = 0; mask >>= 1; fnum++);
163 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
164 return buf;
165}
166
36caf25f 167int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
d7b701de
TT
168{
169 struct feature *f;
170 char *eptr;
171 int num;
172
173 for (f = feature_list; f->string; f++) {
174 if (!strcasecmp(string, f->string)) {
36caf25f 175 *compat_type = f->compat;
d7b701de
TT
176 *mask = f->mask;
177 return 0;
178 }
179 }
180 if (strncasecmp(string, "FEATURE_", 8))
181 return 1;
182
183 switch (string[8]) {
184 case 'c':
185 case 'C':
36caf25f 186 *compat_type = E2P_FEATURE_COMPAT;
d7b701de
TT
187 break;
188 case 'i':
189 case 'I':
36caf25f 190 *compat_type = E2P_FEATURE_INCOMPAT;
d7b701de
TT
191 break;
192 case 'r':
193 case 'R':
36caf25f 194 *compat_type = E2P_FEATURE_RO_INCOMPAT;
d7b701de
TT
195 break;
196 default:
197 return 1;
198 }
199 if (string[9] == 0)
200 return 1;
201 num = strtol(string+9, &eptr, 10);
63cd76d6 202 if (num > 31 || num < 0)
d7b701de
TT
203 return 1;
204 if (*eptr)
205 return 1;
206 *mask = 1 << num;
207 return 0;
208}
209
03efde8a
AD
210const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
211{
212 struct feature *f;
213 static char buf[20];
214 char fchar;
215 int fnum;
216
217 for (f = jrnl_feature_list; f->string; f++) {
218 if ((compat == f->compat) &&
219 (mask == f->mask))
220 return f->string;
221 }
222 switch (compat) {
223 case E2P_FEATURE_COMPAT:
224 fchar = 'C';
225 break;
226 case E2P_FEATURE_INCOMPAT:
227 fchar = 'I';
228 break;
229 case E2P_FEATURE_RO_INCOMPAT:
230 fchar = 'R';
231 break;
232 default:
233 fchar = '?';
234 break;
235 }
236 for (fnum = 0; mask >>= 1; fnum++);
237 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
238 return buf;
239}
240
241int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
242{
243 struct feature *f;
244 char *eptr;
245 int num;
246
247 for (f = jrnl_feature_list; f->string; f++) {
248 if (!strcasecmp(string, f->string)) {
249 *compat_type = f->compat;
250 *mask = f->mask;
251 return 0;
252 }
253 }
254 if (strncasecmp(string, "FEATURE_", 8))
255 return 1;
256
257 switch (string[8]) {
258 case 'c':
259 case 'C':
260 *compat_type = E2P_FEATURE_COMPAT;
261 break;
262 case 'i':
263 case 'I':
264 *compat_type = E2P_FEATURE_INCOMPAT;
265 break;
266 case 'r':
267 case 'R':
268 *compat_type = E2P_FEATURE_RO_INCOMPAT;
269 break;
270 default:
271 return 1;
272 }
273 if (string[9] == 0)
274 return 1;
275 num = strtol(string+9, &eptr, 10);
63cd76d6 276 if (num > 31 || num < 0)
03efde8a
AD
277 return 1;
278 if (*eptr)
279 return 1;
280 *mask = 1 << num;
281 return 0;
282}
d7b701de
TT
283static char *skip_over_blanks(char *cp)
284{
285 while (*cp && isspace(*cp))
286 cp++;
287 return cp;
288}
289
36caf25f 290static char *skip_over_word(char *cp)
d7b701de 291{
944ab713 292 while (*cp && !isspace(*cp) && *cp != ',')
d7b701de
TT
293 cp++;
294 return cp;
295}
296
944ab713
TT
297/*
298 * Edit a feature set array as requested by the user. The ok_array,
299 * if set, allows the application to limit what features the user is
efc6f628 300 * allowed to set or clear using this function. If clear_ok_array is set,
7100351d 301 * then use it tell whether or not it is OK to clear a filesystem feature.
944ab713 302 */
7100351d 303int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
efc6f628 304 __u32 *clear_ok_array, int *type_err,
7100351d 305 unsigned int *mask_err)
d7b701de 306{
2711ca1c
BB
307 char *cp, *buf, *next;
308 int neg;
36caf25f
TT
309 unsigned int mask;
310 int compat_type;
2711ca1c 311 int rc = 0;
d7b701de 312
7100351d
TT
313 if (!clear_ok_array)
314 clear_ok_array = ok_array;
315
316 if (type_err)
317 *type_err = 0;
318 if (mask_err)
319 *mask_err = 0;
320
d7b701de 321 buf = malloc(strlen(str)+1);
944ab713 322 if (!buf)
d7b701de 323 return 1;
d7b701de 324 strcpy(buf, str);
9dc6ad1e 325 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
d7b701de
TT
326 neg = 0;
327 cp = skip_over_blanks(cp);
328 next = skip_over_word(cp);
efc6f628 329
d7b701de
TT
330 if (*next == 0)
331 next = 0;
332 else
333 *next = 0;
9dc6ad1e
TT
334
335 if ((strcasecmp(cp, "none") == 0) ||
336 (strcasecmp(cp, "clear") == 0)) {
337 compat_array[0] = 0;
338 compat_array[1] = 0;
339 compat_array[2] = 0;
340 continue;
341 }
342
d7b701de
TT
343 switch (*cp) {
344 case '-':
345 case '^':
346 neg++;
9e30fb23 347 /* fallthrough */
d7b701de
TT
348 case '+':
349 cp++;
350 break;
351 }
2711ca1c
BB
352 if (e2p_string2feature(cp, &compat_type, &mask)) {
353 rc = 1;
354 break;
355 }
7100351d 356 if (neg) {
efc6f628 357 if (clear_ok_array &&
7100351d
TT
358 !(clear_ok_array[compat_type] & mask)) {
359 rc = 1;
360 if (type_err)
efc6f628 361 *type_err = (compat_type |
7100351d
TT
362 E2P_FEATURE_NEGATE_FLAG);
363 if (mask_err)
364 *mask_err = mask;
365 break;
366 }
36caf25f 367 compat_array[compat_type] &= ~mask;
7100351d
TT
368 } else {
369 if (ok_array && !(ok_array[compat_type] & mask)) {
370 rc = 1;
371 if (type_err)
372 *type_err = compat_type;
373 if (mask_err)
374 *mask_err = mask;
375 break;
376 }
36caf25f 377 compat_array[compat_type] |= mask;
7100351d 378 }
d7b701de 379 }
2711ca1c
BB
380 free(buf);
381 return rc;
d7b701de 382}
7100351d
TT
383
384int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
385{
386 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
387}
03efde8a
AD
388
389#ifdef TEST_PROGRAM
390int main(int argc, char **argv)
391{
392 int compat, compat2, i;
393 unsigned int mask, mask2;
394 const char *str;
395 struct feature *f;
396
397 for (i = 0; i < 2; i++) {
398 if (i == 0) {
399 f = feature_list;
400 printf("Feature list:\n");
401 } else {
402 printf("\nJournal feature list:\n");
403 f = jrnl_feature_list;
404 }
405 for (; f->string; f++) {
406 if (i == 0) {
407 e2p_string2feature((char *)f->string, &compat,
408 &mask);
409 str = e2p_feature2string(compat, mask);
410 } else {
411 e2p_jrnl_string2feature((char *)f->string,
412 &compat, &mask);
413 str = e2p_jrnl_feature2string(compat, mask);
414 }
415
416 printf("\tCompat = %d, Mask = %u, %s\n",
417 compat, mask, f->string);
418 if (strcmp(f->string, str)) {
419 if (e2p_string2feature((char *) str, &compat2,
420 &mask2) ||
421 (compat2 != compat) ||
422 (mask2 != mask)) {
423 fprintf(stderr, "Failure!\n");
424 exit(1);
425 }
426 }
427 }
428 }
429 exit(0);
430}
431#endif