dm-init.c 7.4 KB

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