dm-init.c 8.1 KB

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