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