dm-init.c 8.0 KB

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