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