]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/e2p/feature.c
Reserve the codepoints for the new INCOMPAT feature ENCRYPT
[thirdparty/e2fsprogs.git] / lib / e2p / feature.c
1 /*
2 * feature.c --- convert between features and strings
3 *
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
5 *
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%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <errno.h>
18
19 #include "e2p.h"
20 #include <ext2fs/ext2fs.h>
21 #include <ext2fs/jfs_user.h>
22
23 struct feature {
24 int compat;
25 unsigned int mask;
26 const char *string;
27 };
28
29 static struct feature feature_list[] = {
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" },
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
37 "ext_attr" },
38 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
39 "dir_index" },
40 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
41 "resize_inode" },
42 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
43 "lazy_bg" },
44 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXCLUDE_BITMAP,
45 "snapshot_bitmap" },
46 { E2P_FEATURE_COMPAT, EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
47 "sparse_super2" },
48
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" },
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
54 "huge_file" },
55 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
56 "uninit_bg" },
57 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
58 "uninit_groups" },
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" },
63 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_QUOTA,
64 "quota" },
65 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_BIGALLOC,
66 "bigalloc"},
67 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_METADATA_CSUM,
68 "metadata_csum"},
69 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_REPLICA,
70 "replica" },
71
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" },
78 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
79 "journal_dev" },
80 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
81 "extent" },
82 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
83 "extents" },
84 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
85 "meta_bg" },
86 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
87 "64bit" },
88 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP,
89 "mmp" },
90 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
91 "flex_bg"},
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"},
98 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_INLINE_DATA,
99 "inline_data"},
100 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT,
101 "encrypt"},
102 { 0, 0, 0 },
103 };
104
105 static struct feature jrnl_feature_list[] = {
106 { E2P_FEATURE_COMPAT, JFS_FEATURE_COMPAT_CHECKSUM,
107 "journal_checksum" },
108
109 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_REVOKE,
110 "journal_incompat_revoke" },
111 { E2P_FEATURE_INCOMPAT, JFS_FEATURE_INCOMPAT_64BIT,
112 "journal_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" },
119 { 0, 0, 0 },
120 };
121
122 const char *e2p_feature2string(int compat, unsigned int mask)
123 {
124 struct feature *f;
125 static char buf[20];
126 char fchar;
127 int fnum;
128
129 for (f = feature_list; f->string; f++) {
130 if ((compat == f->compat) &&
131 (mask == f->mask))
132 return f->string;
133 }
134 switch (compat) {
135 case E2P_FEATURE_COMPAT:
136 fchar = 'C';
137 break;
138 case E2P_FEATURE_INCOMPAT:
139 fchar = 'I';
140 break;
141 case E2P_FEATURE_RO_INCOMPAT:
142 fchar = 'R';
143 break;
144 default:
145 fchar = '?';
146 break;
147 }
148 for (fnum = 0; mask >>= 1; fnum++);
149 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
150 return buf;
151 }
152
153 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
154 {
155 struct feature *f;
156 char *eptr;
157 int num;
158
159 for (f = feature_list; f->string; f++) {
160 if (!strcasecmp(string, f->string)) {
161 *compat_type = f->compat;
162 *mask = f->mask;
163 return 0;
164 }
165 }
166 if (strncasecmp(string, "FEATURE_", 8))
167 return 1;
168
169 switch (string[8]) {
170 case 'c':
171 case 'C':
172 *compat_type = E2P_FEATURE_COMPAT;
173 break;
174 case 'i':
175 case 'I':
176 *compat_type = E2P_FEATURE_INCOMPAT;
177 break;
178 case 'r':
179 case 'R':
180 *compat_type = E2P_FEATURE_RO_INCOMPAT;
181 break;
182 default:
183 return 1;
184 }
185 if (string[9] == 0)
186 return 1;
187 num = strtol(string+9, &eptr, 10);
188 if (num > 32 || num < 0)
189 return 1;
190 if (*eptr)
191 return 1;
192 *mask = 1 << num;
193 return 0;
194 }
195
196 const char *e2p_jrnl_feature2string(int compat, unsigned int mask)
197 {
198 struct feature *f;
199 static char buf[20];
200 char fchar;
201 int fnum;
202
203 for (f = jrnl_feature_list; f->string; f++) {
204 if ((compat == f->compat) &&
205 (mask == f->mask))
206 return f->string;
207 }
208 switch (compat) {
209 case E2P_FEATURE_COMPAT:
210 fchar = 'C';
211 break;
212 case E2P_FEATURE_INCOMPAT:
213 fchar = 'I';
214 break;
215 case E2P_FEATURE_RO_INCOMPAT:
216 fchar = 'R';
217 break;
218 default:
219 fchar = '?';
220 break;
221 }
222 for (fnum = 0; mask >>= 1; fnum++);
223 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
224 return buf;
225 }
226
227 int e2p_jrnl_string2feature(char *string, int *compat_type, unsigned int *mask)
228 {
229 struct feature *f;
230 char *eptr;
231 int num;
232
233 for (f = jrnl_feature_list; f->string; f++) {
234 if (!strcasecmp(string, f->string)) {
235 *compat_type = f->compat;
236 *mask = f->mask;
237 return 0;
238 }
239 }
240 if (strncasecmp(string, "FEATURE_", 8))
241 return 1;
242
243 switch (string[8]) {
244 case 'c':
245 case 'C':
246 *compat_type = E2P_FEATURE_COMPAT;
247 break;
248 case 'i':
249 case 'I':
250 *compat_type = E2P_FEATURE_INCOMPAT;
251 break;
252 case 'r':
253 case 'R':
254 *compat_type = E2P_FEATURE_RO_INCOMPAT;
255 break;
256 default:
257 return 1;
258 }
259 if (string[9] == 0)
260 return 1;
261 num = strtol(string+9, &eptr, 10);
262 if (num > 32 || num < 0)
263 return 1;
264 if (*eptr)
265 return 1;
266 *mask = 1 << num;
267 return 0;
268 }
269 static char *skip_over_blanks(char *cp)
270 {
271 while (*cp && isspace(*cp))
272 cp++;
273 return cp;
274 }
275
276 static char *skip_over_word(char *cp)
277 {
278 while (*cp && !isspace(*cp) && *cp != ',')
279 cp++;
280 return cp;
281 }
282
283 /*
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.
288 */
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)
292 {
293 char *cp, *buf, *next;
294 int neg;
295 unsigned int mask;
296 int compat_type;
297 int rc = 0;
298
299 if (!clear_ok_array)
300 clear_ok_array = ok_array;
301
302 if (type_err)
303 *type_err = 0;
304 if (mask_err)
305 *mask_err = 0;
306
307 buf = malloc(strlen(str)+1);
308 if (!buf)
309 return 1;
310 strcpy(buf, str);
311 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
312 neg = 0;
313 cp = skip_over_blanks(cp);
314 next = skip_over_word(cp);
315
316 if (*next == 0)
317 next = 0;
318 else
319 *next = 0;
320
321 if ((strcasecmp(cp, "none") == 0) ||
322 (strcasecmp(cp, "clear") == 0)) {
323 compat_array[0] = 0;
324 compat_array[1] = 0;
325 compat_array[2] = 0;
326 continue;
327 }
328
329 switch (*cp) {
330 case '-':
331 case '^':
332 neg++;
333 /* fallthrough */
334 case '+':
335 cp++;
336 break;
337 }
338 if (e2p_string2feature(cp, &compat_type, &mask)) {
339 rc = 1;
340 break;
341 }
342 if (neg) {
343 if (clear_ok_array &&
344 !(clear_ok_array[compat_type] & mask)) {
345 rc = 1;
346 if (type_err)
347 *type_err = (compat_type |
348 E2P_FEATURE_NEGATE_FLAG);
349 if (mask_err)
350 *mask_err = mask;
351 break;
352 }
353 compat_array[compat_type] &= ~mask;
354 } else {
355 if (ok_array && !(ok_array[compat_type] & mask)) {
356 rc = 1;
357 if (type_err)
358 *type_err = compat_type;
359 if (mask_err)
360 *mask_err = mask;
361 break;
362 }
363 compat_array[compat_type] |= mask;
364 }
365 }
366 free(buf);
367 return rc;
368 }
369
370 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
371 {
372 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
373 }
374
375 #ifdef TEST_PROGRAM
376 int main(int argc, char **argv)
377 {
378 int compat, compat2, i;
379 unsigned int mask, mask2;
380 const char *str;
381 struct feature *f;
382
383 for (i = 0; i < 2; i++) {
384 if (i == 0) {
385 f = feature_list;
386 printf("Feature list:\n");
387 } else {
388 printf("\nJournal feature list:\n");
389 f = jrnl_feature_list;
390 }
391 for (; f->string; f++) {
392 if (i == 0) {
393 e2p_string2feature((char *)f->string, &compat,
394 &mask);
395 str = e2p_feature2string(compat, mask);
396 } else {
397 e2p_jrnl_string2feature((char *)f->string,
398 &compat, &mask);
399 str = e2p_jrnl_feature2string(compat, mask);
400 }
401
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,
406 &mask2) ||
407 (compat2 != compat) ||
408 (mask2 != mask)) {
409 fprintf(stderr, "Failure!\n");
410 exit(1);
411 }
412 }
413 }
414 }
415 exit(0);
416 }
417 #endif