1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* parser.c : Utility for the Linux Multiple Devices driver
* Copyright (C) 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
*
* This is the config file parser. Lets rewrite it in Yacc+Lex if it gets
* too complex.
*
* This source is covered by the GNU GPL, the same as all Linux kernel
* sources.
*/
#include "common.h"
#include "config.h"
char *parity_algorithm_table[] = {
"left-asymmetric",
"right-asymmetric",
"left-symmetric",
"right-symmetric",
NULL
};
static int parity_algorithm_to_num(char *val)
{
int i = 0;
char **s = parity_algorithm_table;
while (*s) {
if (strcmp(*s, val) == 0)
return i;
s++; i++;
}
return -1;
}
static int process_entry (char *par, char *val_s)
{
int val, i;
md_superblock_t *sb;
if (!par[0] && !val_s[0]) {
#if DEBUG
printf("Ignoring line\n");
#endif /* DEBUG */
return 0;
}
if (par[0] == '#') {
#if DEBUG
printf("Ignoring comment\n");
#endif /* DEBUG */
return 0;
}
val = isdigit(val_s[0]) ? atoi(val_s) : -1;
if (strcmp(par, "raiddev") == 0) {
if ((cfg = malloc(sizeof(md_cfg_entry_t))) == NULL) {
fprintf(stderr, "couldn't allocate configuration entry\n");
return 1;
}
#if DEBUG
printf("Processing MD device %s\n", val_s);
#endif
memset(cfg, 0, sizeof(*cfg));
if ((cfg->md_name = malloc(strlen(val_s) + 1)) == NULL) {
fprintf(stderr, "out of memory\n");
return 1;
}
strcpy(cfg->md_name, val_s);
cfg->next = cfg_head;
cfg_head = cfg;
cfg->sb.nr_disks = 0;
return 0;
}
if (!cfg)
return 1;
sb = &cfg->sb;
if (strcmp(par, "raid-level") == 0) {
if (val != 4 && val != 5 && val != 1) {
fprintf(stderr, "raid level %d not supported\n", val);
return 1;
}
sb->level = val;
return 0;
} else if (strcmp(par, "nr-raid-disks") == 0) {
if (val < 0)
return 1;
sb->raid_disks = val;
return 0;
} else if (strcmp(par, "nr-spare-disks") == 0) {
if (val < 0)
return 1;
sb->spare_disks = val;
return 0;
} else if (strcmp(par, "parity-algorithm") == 0) {
if (sb->level != 5) {
fprintf(stderr, "parity-algorithm undefined for raid level %d\n", sb->level);
return 1;
}
if (val < 0)
val = parity_algorithm_to_num(val_s);
if (val < 0)
return 1;
sb->parity_algorithm = val;
return 0;
} else if (strcmp(par, "chunk-size") == 0) {
if (sb->level != 4 && sb->level != 5) {
fprintf(stderr, "chunk-size undefined for raid level %d\n", sb->level);
return 1;
}
if (!val || val % 4) {
fprintf(stderr, "invalid chunk-size (%dkB)\n", val);
return 1;
}
sb->chunk_size = val * MD_BLK_SIZ;
return 0;
} else if (strcmp(par, "device") == 0) {
if (sb->nr_disks == MD_SB_DISKS) {
fprintf(stderr, "a maximum of %d devices in a set is supported\n", MD_SB_DISKS);
return 1;
}
i = sb->nr_disks++;
if ((cfg->device_name[i] = malloc(strlen(val_s) + 1)) == NULL) {
fprintf(stderr, "out of memory\n");
return 1;
}
strcpy(cfg->device_name[i], val_s);
sb->disks[i].raid_disk = i;
return 0;
} else if (strcmp(par, "raid-disk") == 0) {
if (!sb->nr_disks) {
fprintf(stderr, "\"device\" line expected\n");
return 1;
}
if (val >= sb->raid_disks) {
fprintf(stderr, "raid-disk should be smaller than raid_disks\n");
return 1;
}
i = sb->nr_disks - 1;
sb->disks[i].raid_disk = val;
return 0;
} else if (strcmp(par, "spare-disk") == 0) {
if (!sb->nr_disks) {
fprintf(stderr, "\"device\" line expected\n");
return 1;
}
i = sb->nr_disks - 1;
sb->disks[i].raid_disk = i;
return 0;
} else if (strcmp(par, "parity-disk") == 0) {
if (!sb->nr_disks) {
fprintf(stderr, "\"device\" line expected\n");
return 1;
}
if (sb->level != 4) {
fprintf(stderr, "parity_disk only supported for raid level 4\n");
return 1;
}
i = sb->nr_disks - 1;
sb->disks[i].raid_disk = sb->raid_disks - 1;
return 0;
}
fprintf(stderr, "unrecognized option %s\n", par);
return 1;
}
int parse_config (FILE *fp)
{
int nr = 0;
char line[MAX_LINE_LENGTH], par[MAX_LINE_LENGTH], val[MAX_LINE_LENGTH];
printf("parsing configuration file\n");
while (1) {
if ((fgets(line, MAX_LINE_LENGTH, fp)) == NULL)
break;
nr++;
#if DEBUG
printf("\n");
printf("parsing %s", line);
#endif /* DEBUG */
par[0] = 0; val[0] = 0;
sscanf(line, "\t%s\t%s\n", par, val);
#if DEBUG
printf("par == %s, val == %s\n", par, val);
#endif /* DEBUG */
if (process_entry(par, val)) {
printf("detected error on line %d:\n\t%s", nr, line);
return 1;
}
}
#if DEBUG
printf("finished to parse configuration file\n");
#endif /* DEBUG */
return 0;
}
|