1 // SPDX-License-Identifier: GPL-2.0
7 * This file is released under the GPLv2.
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/device-mapper.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/moduleparam.h>
18 #define DM_MSG_PREFIX "init"
19 #define DM_MAX_DEVICES 256
20 #define DM_MAX_TARGETS 256
21 #define DM_MAX_STR_SIZE 4096
22 #define DM_MAX_WAITFOR 256
26 static char *waitfor[DM_MAX_WAITFOR];
29 * Format: dm-mod.create=<name>,<uuid>,<minor>,<flags>,<table>[,<table>+][;<name>,<uuid>,<minor>,<flags>,<table>[,<table>+]+]
30 * Table format: <start_sector> <num_sectors> <target_type> <target_args>
31 * Block devices to wait for to become available before setting up tables:
32 * dm-mod.waitfor=<device1>[,..,<deviceN>]
34 * See Documentation/admin-guide/device-mapper/dm-init.rst for dm-mod.create="..." format
40 struct dm_target_spec *table[DM_MAX_TARGETS];
41 char *target_args_array[DM_MAX_TARGETS];
42 struct list_head list;
45 static const char * const dm_allowed_targets[] __initconst = {
54 static int __init dm_verify_target_type(const char *target)
58 for (i = 0; i < ARRAY_SIZE(dm_allowed_targets); i++) {
59 if (!strcmp(dm_allowed_targets[i], target))
65 static void __init dm_setup_cleanup(struct list_head *devices)
67 struct dm_device *dev, *tmp;
70 list_for_each_entry_safe(dev, tmp, devices, list) {
72 for (i = 0; i < dev->dmi.target_count; i++) {
74 kfree(dev->target_args_array[i]);
81 * str_field_delimit - delimit a string based on a separator char.
82 * @str: the pointer to the string to delimit.
83 * @separator: char that delimits the field
85 * Find a @separator and replace it by '\0'.
86 * Remove leading and trailing spaces.
87 * Return the remainder string after the @separator.
89 static char __init *str_field_delimit(char **str, char separator)
93 /* TODO: add support for escaped characters */
94 *str = skip_spaces(*str);
95 s = strchr(*str, separator);
96 /* Delimit the field and remove trailing spaces */
100 return s ? ++s : NULL;
104 * dm_parse_table_entry - parse a table entry
105 * @dev: device to store the parsed information.
106 * @str: the pointer to a string with the format:
107 * <start_sector> <num_sectors> <target_type> <target_args>[, ...]
109 * Return the remainder string after the table entry, i.e, after the comma which
110 * delimits the entry or NULL if reached the end of the string.
112 static char __init *dm_parse_table_entry(struct dm_device *dev, char *str)
114 const unsigned int n = dev->dmi.target_count - 1;
115 struct dm_target_spec *sp;
122 /* Delimit first 3 fields that are separated by space */
123 for (i = 0; i < ARRAY_SIZE(field) - 1; i++) {
124 field[i + 1] = str_field_delimit(&field[i], ' ');
126 return ERR_PTR(-EINVAL);
128 /* Delimit last field that can be terminated by comma */
129 next = str_field_delimit(&field[i], ',');
131 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
133 return ERR_PTR(-ENOMEM);
137 if (kstrtoull(field[0], 0, &sp->sector_start))
138 return ERR_PTR(-EINVAL);
140 if (kstrtoull(field[1], 0, &sp->length))
141 return ERR_PTR(-EINVAL);
143 strscpy(sp->target_type, field[2], sizeof(sp->target_type));
144 if (dm_verify_target_type(sp->target_type)) {
145 DMERR("invalid type \"%s\"", sp->target_type);
146 return ERR_PTR(-EINVAL);
149 dev->target_args_array[n] = kstrndup(field[3], DM_MAX_STR_SIZE,
151 if (!dev->target_args_array[n])
152 return ERR_PTR(-ENOMEM);
158 * dm_parse_table - parse "dm-mod.create=" table field
159 * @dev: device to store the parsed information.
160 * @str: the pointer to a string with the format:
163 static int __init dm_parse_table(struct dm_device *dev, char *str)
165 char *table_entry = str;
167 while (table_entry) {
168 DMDEBUG("parsing table \"%s\"", str);
169 if (++dev->dmi.target_count > DM_MAX_TARGETS) {
170 DMERR("too many targets %u > %d",
171 dev->dmi.target_count, DM_MAX_TARGETS);
174 table_entry = dm_parse_table_entry(dev, table_entry);
175 if (IS_ERR(table_entry)) {
176 DMERR("couldn't parse table");
177 return PTR_ERR(table_entry);
185 * dm_parse_device_entry - parse a device entry
186 * @dev: device to store the parsed information.
187 * @str: the pointer to a string with the format:
188 * name,uuid,minor,flags,table[; ...]
190 * Return the remainder string after the table entry, i.e, after the semi-colon
191 * which delimits the entry or NULL if reached the end of the string.
193 static char __init *dm_parse_device_entry(struct dm_device *dev, char *str)
195 /* There are 5 fields: name,uuid,minor,flags,table; */
201 /* Delimit first 4 fields that are separated by comma */
202 for (i = 0; i < ARRAY_SIZE(field) - 1; i++) {
203 field[i+1] = str_field_delimit(&field[i], ',');
205 return ERR_PTR(-EINVAL);
207 /* Delimit last field that can be delimited by semi-colon */
208 next = str_field_delimit(&field[i], ';');
211 strscpy(dev->dmi.name, field[0], sizeof(dev->dmi.name));
213 strscpy(dev->dmi.uuid, field[1], sizeof(dev->dmi.uuid));
215 if (strlen(field[2])) {
216 if (kstrtoull(field[2], 0, &dev->dmi.dev))
217 return ERR_PTR(-EINVAL);
218 dev->dmi.flags |= DM_PERSISTENT_DEV_FLAG;
221 if (!strcmp(field[3], "ro"))
222 dev->dmi.flags |= DM_READONLY_FLAG;
223 else if (strcmp(field[3], "rw"))
224 return ERR_PTR(-EINVAL);
226 if (dm_parse_table(dev, field[4]))
227 return ERR_PTR(-EINVAL);
233 * dm_parse_devices - parse "dm-mod.create=" argument
234 * @devices: list of struct dm_device to store the parsed information.
235 * @str: the pointer to a string with the format:
236 * <device>[;<device>+]
238 static int __init dm_parse_devices(struct list_head *devices, char *str)
240 unsigned long ndev = 0;
241 struct dm_device *dev;
244 DMDEBUG("parsing \"%s\"", str);
246 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
249 list_add_tail(&dev->list, devices);
251 if (++ndev > DM_MAX_DEVICES) {
252 DMERR("too many devices %lu > %d",
253 ndev, DM_MAX_DEVICES);
257 device = dm_parse_device_entry(dev, device);
258 if (IS_ERR(device)) {
259 DMERR("couldn't parse device");
260 return PTR_ERR(device);
268 * dm_init_init - parse "dm-mod.create=" argument and configure drivers
270 static int __init dm_init_init(void)
272 struct dm_device *dev;
280 if (strlen(create) >= DM_MAX_STR_SIZE) {
281 DMERR("Argument is too big. Limit is %d", DM_MAX_STR_SIZE);
284 str = kstrndup(create, DM_MAX_STR_SIZE, GFP_KERNEL);
288 r = dm_parse_devices(&devices, str);
292 DMINFO("waiting for all devices to be available before creating mapped devices");
293 wait_for_device_probe();
295 for (i = 0; i < ARRAY_SIZE(waitfor); i++) {
297 DMINFO("waiting for device %s ...", waitfor[i]);
298 while (!dm_get_dev_t(waitfor[i]))
304 DMINFO("all devices available");
306 list_for_each_entry(dev, &devices, list) {
307 if (dm_early_create(&dev->dmi, dev->table,
308 dev->target_args_array))
313 dm_setup_cleanup(&devices);
317 late_initcall(dm_init_init);
319 module_param(create, charp, 0);
320 MODULE_PARM_DESC(create, "Create a mapped device in early boot");
322 module_param_array(waitfor, charp, NULL, 0);
323 MODULE_PARM_DESC(waitfor, "Devices to wait for before setting up tables");