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