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