Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * dm-init.c
  5 * Copyright (C) 2017 The Chromium OS Authors <chromium-os-dev@chromium.org>
  6 *
  7 * This file is released under the GPLv2.
  8 */
  9
 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>
 17
 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
 23
 24static char *create;
 25
 26static char *waitfor[DM_MAX_WAITFOR];
 27
 28/*
 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>]
 33 *
 34 * See Documentation/admin-guide/device-mapper/dm-init.rst for dm-mod.create="..." format
 35 * details.
 36 */
 37
 38struct dm_device {
 39	struct dm_ioctl dmi;
 40	struct dm_target_spec *table[DM_MAX_TARGETS];
 41	char *target_args_array[DM_MAX_TARGETS];
 42	struct list_head list;
 43};
 44
 45static const char * const dm_allowed_targets[] __initconst = {
 46	"crypt",
 47	"delay",
 48	"linear",
 49	"snapshot-origin",
 50	"striped",
 51	"verity",
 52};
 53
 54static int __init dm_verify_target_type(const char *target)
 55{
 56	unsigned int i;
 57
 58	for (i = 0; i < ARRAY_SIZE(dm_allowed_targets); i++) {
 59		if (!strcmp(dm_allowed_targets[i], target))
 60			return 0;
 61	}
 62	return -EINVAL;
 63}
 64
 65static void __init dm_setup_cleanup(struct list_head *devices)
 66{
 67	struct dm_device *dev, *tmp;
 68	unsigned int i;
 69
 70	list_for_each_entry_safe(dev, tmp, devices, list) {
 71		list_del(&dev->list);
 72		for (i = 0; i < dev->dmi.target_count; i++) {
 73			kfree(dev->table[i]);
 74			kfree(dev->target_args_array[i]);
 75		}
 76		kfree(dev);
 77	}
 78}
 79
 80/**
 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
 84 *
 85 * Find a @separator and replace it by '\0'.
 86 * Remove leading and trailing spaces.
 87 * Return the remainder string after the @separator.
 88 */
 89static char __init *str_field_delimit(char **str, char separator)
 90{
 91	char *s;
 92
 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 */
 97	if (s)
 98		*s = '\0';
 99	*str = strim(*str);
100	return s ? ++s : NULL;
101}
102
103/**
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>[, ...]
108 *
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.
111 */
112static char __init *dm_parse_table_entry(struct dm_device *dev, char *str)
113{
114	const unsigned int n = dev->dmi.target_count - 1;
115	struct dm_target_spec *sp;
116	unsigned int i;
117	/* fields:  */
118	char *field[4];
119	char *next;
120
121	field[0] = str;
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], ' ');
125		if (!field[i + 1])
126			return ERR_PTR(-EINVAL);
127	}
128	/* Delimit last field that can be terminated by comma */
129	next = str_field_delimit(&field[i], ',');
130
131	sp = kzalloc(sizeof(*sp), GFP_KERNEL);
132	if (!sp)
133		return ERR_PTR(-ENOMEM);
134	dev->table[n] = sp;
135
136	/* start_sector */
137	if (kstrtoull(field[0], 0, &sp->sector_start))
138		return ERR_PTR(-EINVAL);
139	/* num_sector */
140	if (kstrtoull(field[1], 0, &sp->length))
141		return ERR_PTR(-EINVAL);
142	/* target_type */
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);
147	}
148	/* target_args */
149	dev->target_args_array[n] = kstrndup(field[3], DM_MAX_STR_SIZE,
150					     GFP_KERNEL);
151	if (!dev->target_args_array[n])
152		return ERR_PTR(-ENOMEM);
153
154	return next;
155}
156
157/**
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:
161 *	<table>[,<table>+]
162 */
163static int __init dm_parse_table(struct dm_device *dev, char *str)
164{
165	char *table_entry = str;
166
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);
172			return -EINVAL;
173		}
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);
178		}
179	}
180
181	return 0;
182}
183
184/**
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[; ...]
189 *
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.
192 */
193static char __init *dm_parse_device_entry(struct dm_device *dev, char *str)
194{
195	/* There are 5 fields: name,uuid,minor,flags,table; */
196	char *field[5];
197	unsigned int i;
198	char *next;
199
200	field[0] = str;
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], ',');
204		if (!field[i+1])
205			return ERR_PTR(-EINVAL);
206	}
207	/* Delimit last field that can be delimited by semi-colon */
208	next = str_field_delimit(&field[i], ';');
209
210	/* name */
211	strscpy(dev->dmi.name, field[0], sizeof(dev->dmi.name));
212	/* uuid */
213	strscpy(dev->dmi.uuid, field[1], sizeof(dev->dmi.uuid));
214	/* minor */
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;
219	}
220	/* flags */
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);
225	/* table */
226	if (dm_parse_table(dev, field[4]))
227		return ERR_PTR(-EINVAL);
228
229	return next;
230}
231
232/**
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>+]
237 */
238static int __init dm_parse_devices(struct list_head *devices, char *str)
239{
240	unsigned long ndev = 0;
241	struct dm_device *dev;
242	char *device = str;
243
244	DMDEBUG("parsing \"%s\"", str);
245	while (device) {
246		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
247		if (!dev)
248			return -ENOMEM;
249		list_add_tail(&dev->list, devices);
250
251		if (++ndev > DM_MAX_DEVICES) {
252			DMERR("too many devices %lu > %d",
253			      ndev, DM_MAX_DEVICES);
254			return -EINVAL;
255		}
256
257		device = dm_parse_device_entry(dev, device);
258		if (IS_ERR(device)) {
259			DMERR("couldn't parse device");
260			return PTR_ERR(device);
261		}
262	}
263
264	return 0;
265}
266
267/**
268 * dm_init_init - parse "dm-mod.create=" argument and configure drivers
269 */
270static int __init dm_init_init(void)
271{
272	struct dm_device *dev;
273	LIST_HEAD(devices);
274	char *str;
275	int i, r;
276
277	if (!create)
278		return 0;
279
280	if (strlen(create) >= DM_MAX_STR_SIZE) {
281		DMERR("Argument is too big. Limit is %d", DM_MAX_STR_SIZE);
282		return -EINVAL;
283	}
284	str = kstrndup(create, DM_MAX_STR_SIZE, GFP_KERNEL);
285	if (!str)
286		return -ENOMEM;
287
288	r = dm_parse_devices(&devices, str);
289	if (r)
290		goto out;
291
292	DMINFO("waiting for all devices to be available before creating mapped devices");
293	wait_for_device_probe();
294
295	for (i = 0; i < ARRAY_SIZE(waitfor); i++) {
296		if (waitfor[i]) {
 
 
297			DMINFO("waiting for device %s ...", waitfor[i]);
298			while (!dm_get_dev_t(waitfor[i]))
299				msleep(5);
300		}
301	}
302
303	if (waitfor[0])
304		DMINFO("all devices available");
305
306	list_for_each_entry(dev, &devices, list) {
307		if (dm_early_create(&dev->dmi, dev->table,
308				    dev->target_args_array))
309			break;
310	}
311out:
312	kfree(str);
313	dm_setup_cleanup(&devices);
314	return r;
315}
316
317late_initcall(dm_init_init);
318
319module_param(create, charp, 0);
320MODULE_PARM_DESC(create, "Create a mapped device in early boot");
321
322module_param_array(waitfor, charp, NULL, 0);
323MODULE_PARM_DESC(waitfor, "Devices to wait for before setting up tables");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3/*
 
  4 * Copyright (C) 2017 The Chromium OS Authors <chromium-os-dev@chromium.org>
  5 *
  6 * This file is released under the GPLv2.
  7 */
  8
  9#include <linux/ctype.h>
 10#include <linux/delay.h>
 11#include <linux/device.h>
 12#include <linux/device-mapper.h>
 13#include <linux/init.h>
 14#include <linux/list.h>
 15#include <linux/moduleparam.h>
 16
 17#define DM_MSG_PREFIX "init"
 18#define DM_MAX_DEVICES 256
 19#define DM_MAX_TARGETS 256
 20#define DM_MAX_STR_SIZE 4096
 21#define DM_MAX_WAITFOR 256
 22
 23static char *create;
 24
 25static char *waitfor[DM_MAX_WAITFOR];
 26
 27/*
 28 * Format: dm-mod.create=<name>,<uuid>,<minor>,<flags>,<table>[,<table>+][;<name>,<uuid>,<minor>,<flags>,<table>[,<table>+]+]
 29 * Table format: <start_sector> <num_sectors> <target_type> <target_args>
 30 * Block devices to wait for to become available before setting up tables:
 31 * dm-mod.waitfor=<device1>[,..,<deviceN>]
 32 *
 33 * See Documentation/admin-guide/device-mapper/dm-init.rst for dm-mod.create="..." format
 34 * details.
 35 */
 36
 37struct dm_device {
 38	struct dm_ioctl dmi;
 39	struct dm_target_spec *table[DM_MAX_TARGETS];
 40	char *target_args_array[DM_MAX_TARGETS];
 41	struct list_head list;
 42};
 43
 44static const char * const dm_allowed_targets[] __initconst = {
 45	"crypt",
 46	"delay",
 47	"linear",
 48	"snapshot-origin",
 49	"striped",
 50	"verity",
 51};
 52
 53static int __init dm_verify_target_type(const char *target)
 54{
 55	unsigned int i;
 56
 57	for (i = 0; i < ARRAY_SIZE(dm_allowed_targets); i++) {
 58		if (!strcmp(dm_allowed_targets[i], target))
 59			return 0;
 60	}
 61	return -EINVAL;
 62}
 63
 64static void __init dm_setup_cleanup(struct list_head *devices)
 65{
 66	struct dm_device *dev, *tmp;
 67	unsigned int i;
 68
 69	list_for_each_entry_safe(dev, tmp, devices, list) {
 70		list_del(&dev->list);
 71		for (i = 0; i < dev->dmi.target_count; i++) {
 72			kfree(dev->table[i]);
 73			kfree(dev->target_args_array[i]);
 74		}
 75		kfree(dev);
 76	}
 77}
 78
 79/**
 80 * str_field_delimit - delimit a string based on a separator char.
 81 * @str: the pointer to the string to delimit.
 82 * @separator: char that delimits the field
 83 *
 84 * Find a @separator and replace it by '\0'.
 85 * Remove leading and trailing spaces.
 86 * Return the remainder string after the @separator.
 87 */
 88static char __init *str_field_delimit(char **str, char separator)
 89{
 90	char *s;
 91
 92	/* TODO: add support for escaped characters */
 93	*str = skip_spaces(*str);
 94	s = strchr(*str, separator);
 95	/* Delimit the field and remove trailing spaces */
 96	if (s)
 97		*s = '\0';
 98	*str = strim(*str);
 99	return s ? ++s : NULL;
100}
101
102/**
103 * dm_parse_table_entry - parse a table entry
104 * @dev: device to store the parsed information.
105 * @str: the pointer to a string with the format:
106 *	<start_sector> <num_sectors> <target_type> <target_args>[, ...]
107 *
108 * Return the remainder string after the table entry, i.e, after the comma which
109 * delimits the entry or NULL if reached the end of the string.
110 */
111static char __init *dm_parse_table_entry(struct dm_device *dev, char *str)
112{
113	const unsigned int n = dev->dmi.target_count - 1;
114	struct dm_target_spec *sp;
115	unsigned int i;
116	/* fields:  */
117	char *field[4];
118	char *next;
119
120	field[0] = str;
121	/* Delimit first 3 fields that are separated by space */
122	for (i = 0; i < ARRAY_SIZE(field) - 1; i++) {
123		field[i + 1] = str_field_delimit(&field[i], ' ');
124		if (!field[i + 1])
125			return ERR_PTR(-EINVAL);
126	}
127	/* Delimit last field that can be terminated by comma */
128	next = str_field_delimit(&field[i], ',');
129
130	sp = kzalloc(sizeof(*sp), GFP_KERNEL);
131	if (!sp)
132		return ERR_PTR(-ENOMEM);
133	dev->table[n] = sp;
134
135	/* start_sector */
136	if (kstrtoull(field[0], 0, &sp->sector_start))
137		return ERR_PTR(-EINVAL);
138	/* num_sector */
139	if (kstrtoull(field[1], 0, &sp->length))
140		return ERR_PTR(-EINVAL);
141	/* target_type */
142	strscpy(sp->target_type, field[2], sizeof(sp->target_type));
143	if (dm_verify_target_type(sp->target_type)) {
144		DMERR("invalid type \"%s\"", sp->target_type);
145		return ERR_PTR(-EINVAL);
146	}
147	/* target_args */
148	dev->target_args_array[n] = kstrndup(field[3], DM_MAX_STR_SIZE,
149					     GFP_KERNEL);
150	if (!dev->target_args_array[n])
151		return ERR_PTR(-ENOMEM);
152
153	return next;
154}
155
156/**
157 * dm_parse_table - parse "dm-mod.create=" table field
158 * @dev: device to store the parsed information.
159 * @str: the pointer to a string with the format:
160 *	<table>[,<table>+]
161 */
162static int __init dm_parse_table(struct dm_device *dev, char *str)
163{
164	char *table_entry = str;
165
166	while (table_entry) {
167		DMDEBUG("parsing table \"%s\"", str);
168		if (++dev->dmi.target_count > DM_MAX_TARGETS) {
169			DMERR("too many targets %u > %d",
170			      dev->dmi.target_count, DM_MAX_TARGETS);
171			return -EINVAL;
172		}
173		table_entry = dm_parse_table_entry(dev, table_entry);
174		if (IS_ERR(table_entry)) {
175			DMERR("couldn't parse table");
176			return PTR_ERR(table_entry);
177		}
178	}
179
180	return 0;
181}
182
183/**
184 * dm_parse_device_entry - parse a device entry
185 * @dev: device to store the parsed information.
186 * @str: the pointer to a string with the format:
187 *	name,uuid,minor,flags,table[; ...]
188 *
189 * Return the remainder string after the table entry, i.e, after the semi-colon
190 * which delimits the entry or NULL if reached the end of the string.
191 */
192static char __init *dm_parse_device_entry(struct dm_device *dev, char *str)
193{
194	/* There are 5 fields: name,uuid,minor,flags,table; */
195	char *field[5];
196	unsigned int i;
197	char *next;
198
199	field[0] = str;
200	/* Delimit first 4 fields that are separated by comma */
201	for (i = 0; i < ARRAY_SIZE(field) - 1; i++) {
202		field[i+1] = str_field_delimit(&field[i], ',');
203		if (!field[i+1])
204			return ERR_PTR(-EINVAL);
205	}
206	/* Delimit last field that can be delimited by semi-colon */
207	next = str_field_delimit(&field[i], ';');
208
209	/* name */
210	strscpy(dev->dmi.name, field[0], sizeof(dev->dmi.name));
211	/* uuid */
212	strscpy(dev->dmi.uuid, field[1], sizeof(dev->dmi.uuid));
213	/* minor */
214	if (strlen(field[2])) {
215		if (kstrtoull(field[2], 0, &dev->dmi.dev))
216			return ERR_PTR(-EINVAL);
217		dev->dmi.flags |= DM_PERSISTENT_DEV_FLAG;
218	}
219	/* flags */
220	if (!strcmp(field[3], "ro"))
221		dev->dmi.flags |= DM_READONLY_FLAG;
222	else if (strcmp(field[3], "rw"))
223		return ERR_PTR(-EINVAL);
224	/* table */
225	if (dm_parse_table(dev, field[4]))
226		return ERR_PTR(-EINVAL);
227
228	return next;
229}
230
231/**
232 * dm_parse_devices - parse "dm-mod.create=" argument
233 * @devices: list of struct dm_device to store the parsed information.
234 * @str: the pointer to a string with the format:
235 *	<device>[;<device>+]
236 */
237static int __init dm_parse_devices(struct list_head *devices, char *str)
238{
239	unsigned long ndev = 0;
240	struct dm_device *dev;
241	char *device = str;
242
243	DMDEBUG("parsing \"%s\"", str);
244	while (device) {
245		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
246		if (!dev)
247			return -ENOMEM;
248		list_add_tail(&dev->list, devices);
249
250		if (++ndev > DM_MAX_DEVICES) {
251			DMERR("too many devices %lu > %d",
252			      ndev, DM_MAX_DEVICES);
253			return -EINVAL;
254		}
255
256		device = dm_parse_device_entry(dev, device);
257		if (IS_ERR(device)) {
258			DMERR("couldn't parse device");
259			return PTR_ERR(device);
260		}
261	}
262
263	return 0;
264}
265
266/**
267 * dm_init_init - parse "dm-mod.create=" argument and configure drivers
268 */
269static int __init dm_init_init(void)
270{
271	struct dm_device *dev;
272	LIST_HEAD(devices);
273	char *str;
274	int i, r;
275
276	if (!create)
277		return 0;
278
279	if (strlen(create) >= DM_MAX_STR_SIZE) {
280		DMERR("Argument is too big. Limit is %d", DM_MAX_STR_SIZE);
281		return -EINVAL;
282	}
283	str = kstrndup(create, DM_MAX_STR_SIZE, GFP_KERNEL);
284	if (!str)
285		return -ENOMEM;
286
287	r = dm_parse_devices(&devices, str);
288	if (r)
289		goto out;
290
291	DMINFO("waiting for all devices to be available before creating mapped devices");
292	wait_for_device_probe();
293
294	for (i = 0; i < ARRAY_SIZE(waitfor); i++) {
295		if (waitfor[i]) {
296			dev_t dev;
297
298			DMINFO("waiting for device %s ...", waitfor[i]);
299			while (early_lookup_bdev(waitfor[i], &dev))
300				fsleep(5000);
301		}
302	}
303
304	if (waitfor[0])
305		DMINFO("all devices available");
306
307	list_for_each_entry(dev, &devices, list) {
308		if (dm_early_create(&dev->dmi, dev->table,
309				    dev->target_args_array))
310			break;
311	}
312out:
313	kfree(str);
314	dm_setup_cleanup(&devices);
315	return r;
316}
317
318late_initcall(dm_init_init);
319
320module_param(create, charp, 0);
321MODULE_PARM_DESC(create, "Create a mapped device in early boot");
322
323module_param_array(waitfor, charp, NULL, 0);
324MODULE_PARM_DESC(waitfor, "Devices to wait for before setting up tables");