do_mounts_dm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* do_mounts_dm.c
  2. * Copyright (C) 2010 The Chromium OS Authors <[email protected]>
  3. * All Rights Reserved.
  4. * Based on do_mounts_md.c
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/async.h>
  9. #include <linux/ctype.h>
  10. #include <linux/device-mapper.h>
  11. #include <linux/fs.h>
  12. #include <linux/string.h>
  13. #include <linux/delay.h>
  14. #include "do_mounts.h"
  15. #define DM_MAX_DEVICES 256
  16. #define DM_MAX_TARGETS 256
  17. #define DM_MAX_NAME 32
  18. #define DM_MAX_UUID 129
  19. #define DM_NO_UUID "none"
  20. #define DM_MSG_PREFIX "init"
  21. /* Separators used for parsing the dm= argument. */
  22. #define DM_FIELD_SEP " "
  23. #define DM_LINE_SEP ","
  24. #define DM_ANY_SEP DM_FIELD_SEP DM_LINE_SEP
  25. /*
  26. * When the device-mapper and any targets are compiled into the kernel
  27. * (not a module), one or more device-mappers may be created and used
  28. * as the root device at boot time with the parameters given with the
  29. * boot line dm=...
  30. *
  31. * Multiple device-mappers can be stacked specifing the number of
  32. * devices. A device can have multiple targets if the the number of
  33. * targets is specified.
  34. *
  35. * TODO(taysom:defect 32847)
  36. * In the future, the <num> field will be mandatory.
  37. *
  38. * <device> ::= [<num>] <device-mapper>+
  39. * <device-mapper> ::= <head> "," <target>+
  40. * <head> ::= <name> <uuid> <mode> [<num>]
  41. * <target> ::= <start> <length> <type> <options> ","
  42. * <mode> ::= "ro" | "rw"
  43. * <uuid> ::= xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | "none"
  44. * <type> ::= "verity" | "bootcache" | ...
  45. *
  46. * Example:
  47. * 2 vboot none ro 1,
  48. * 0 1768000 bootcache
  49. * device=aa55b119-2a47-8c45-946a-5ac57765011f+1
  50. * signature=76e9be054b15884a9fa85973e9cb274c93afadb6
  51. * cache_start=1768000 max_blocks=100000 size_limit=23 max_trace=20000,
  52. * vroot none ro 1,
  53. * 0 1740800 verity payload=254:0 hashtree=254:0 hashstart=1740800 alg=sha1
  54. * root_hexdigest=76e9be054b15884a9fa85973e9cb274c93afadb6
  55. * salt=5b3549d54d6c7a3837b9b81ed72e49463a64c03680c47835bef94d768e5646fe
  56. *
  57. * Notes:
  58. * 1. uuid is a label for the device and we set it to "none".
  59. * 2. The <num> field will be optional initially and assumed to be 1.
  60. * Once all the scripts that set these fields have been set, it will
  61. * be made mandatory.
  62. */
  63. struct dm_setup_target {
  64. sector_t begin;
  65. sector_t length;
  66. char *type;
  67. char *params;
  68. /* simple singly linked list */
  69. struct dm_setup_target *next;
  70. };
  71. struct dm_device {
  72. int minor;
  73. int ro;
  74. char name[DM_MAX_NAME];
  75. char uuid[DM_MAX_UUID];
  76. unsigned long num_targets;
  77. struct dm_setup_target *target;
  78. int target_count;
  79. struct dm_device *next;
  80. };
  81. struct dm_option {
  82. char *start;
  83. char *next;
  84. size_t len;
  85. char delim;
  86. };
  87. static struct {
  88. unsigned long num_devices;
  89. char *str;
  90. } dm_setup_args __initdata;
  91. static __initdata int dm_early_setup;
  92. static int __init get_dm_option(struct dm_option *opt, const char *accept)
  93. {
  94. char *str = opt->next;
  95. char *endp;
  96. if (!str)
  97. return 0;
  98. str = skip_spaces(str);
  99. opt->start = str;
  100. endp = strpbrk(str, accept);
  101. if (!endp) { /* act like strchrnul */
  102. opt->len = strlen(str);
  103. endp = str + opt->len;
  104. } else {
  105. opt->len = endp - str;
  106. }
  107. opt->delim = *endp;
  108. if (*endp == 0) {
  109. /* Don't advance past the nul. */
  110. opt->next = endp;
  111. } else {
  112. opt->next = endp + 1;
  113. }
  114. return opt->len != 0;
  115. }
  116. static int __init dm_setup_cleanup(struct dm_device *devices)
  117. {
  118. struct dm_device *dev = devices;
  119. while (dev) {
  120. struct dm_device *old_dev = dev;
  121. struct dm_setup_target *target = dev->target;
  122. while (target) {
  123. struct dm_setup_target *old_target = target;
  124. kfree(target->type);
  125. kfree(target->params);
  126. target = target->next;
  127. kfree(old_target);
  128. dev->target_count--;
  129. }
  130. BUG_ON(dev->target_count);
  131. dev = dev->next;
  132. kfree(old_dev);
  133. }
  134. return 0;
  135. }
  136. static char * __init dm_parse_device(struct dm_device *dev, char *str)
  137. {
  138. struct dm_option opt;
  139. size_t len;
  140. /* Grab the logical name of the device to be exported to udev */
  141. opt.next = str;
  142. if (!get_dm_option(&opt, DM_FIELD_SEP)) {
  143. DMERR("failed to parse device name");
  144. goto parse_fail;
  145. }
  146. len = min(opt.len + 1, sizeof(dev->name));
  147. strlcpy(dev->name, opt.start, len); /* includes nul */
  148. /* Grab the UUID value or "none" */
  149. if (!get_dm_option(&opt, DM_FIELD_SEP)) {
  150. DMERR("failed to parse device uuid");
  151. goto parse_fail;
  152. }
  153. len = min(opt.len + 1, sizeof(dev->uuid));
  154. strlcpy(dev->uuid, opt.start, len);
  155. /* Determine if the table/device will be read only or read-write */
  156. get_dm_option(&opt, DM_ANY_SEP);
  157. if (!strncmp("ro", opt.start, opt.len)) {
  158. dev->ro = 1;
  159. } else if (!strncmp("rw", opt.start, opt.len)) {
  160. dev->ro = 0;
  161. } else {
  162. DMERR("failed to parse table mode");
  163. goto parse_fail;
  164. }
  165. /* Optional number field */
  166. /* XXX: The <num> field will be mandatory in the next round */
  167. if (opt.delim == DM_FIELD_SEP[0]) {
  168. if (!get_dm_option(&opt, DM_LINE_SEP))
  169. return NULL;
  170. dev->num_targets = simple_strtoul(opt.start, NULL, 10);
  171. } else {
  172. dev->num_targets = 1;
  173. }
  174. if (dev->num_targets > DM_MAX_TARGETS) {
  175. DMERR("too many targets %lu > %d",
  176. dev->num_targets, DM_MAX_TARGETS);
  177. }
  178. return opt.next;
  179. parse_fail:
  180. return NULL;
  181. }
  182. static char * __init dm_parse_targets(struct dm_device *dev, char *str)
  183. {
  184. struct dm_option opt;
  185. struct dm_setup_target **target = &dev->target;
  186. unsigned long num_targets = dev->num_targets;
  187. unsigned long i;
  188. /* Targets are defined as per the table format but with a
  189. * comma as a newline separator. */
  190. opt.next = str;
  191. for (i = 0; i < num_targets; i++) {
  192. *target = kzalloc(sizeof(struct dm_setup_target), GFP_KERNEL);
  193. if (!*target) {
  194. DMERR("failed to allocate memory for target %s<%ld>",
  195. dev->name, i);
  196. goto parse_fail;
  197. }
  198. dev->target_count++;
  199. if (!get_dm_option(&opt, DM_FIELD_SEP)) {
  200. DMERR("failed to parse starting sector"
  201. " for target %s<%ld>", dev->name, i);
  202. goto parse_fail;
  203. }
  204. (*target)->begin = simple_strtoull(opt.start, NULL, 10);
  205. if (!get_dm_option(&opt, DM_FIELD_SEP)) {
  206. DMERR("failed to parse length for target %s<%ld>",
  207. dev->name, i);
  208. goto parse_fail;
  209. }
  210. (*target)->length = simple_strtoull(opt.start, NULL, 10);
  211. if (get_dm_option(&opt, DM_FIELD_SEP))
  212. (*target)->type = kstrndup(opt.start, opt.len,
  213. GFP_KERNEL);
  214. if (!((*target)->type)) {
  215. DMERR("failed to parse type for target %s<%ld>",
  216. dev->name, i);
  217. goto parse_fail;
  218. }
  219. if (get_dm_option(&opt, DM_LINE_SEP))
  220. (*target)->params = kstrndup(opt.start, opt.len,
  221. GFP_KERNEL);
  222. if (!((*target)->params)) {
  223. DMERR("failed to parse params for target %s<%ld>",
  224. dev->name, i);
  225. goto parse_fail;
  226. }
  227. target = &((*target)->next);
  228. }
  229. DMDEBUG("parsed %d targets", dev->target_count);
  230. return opt.next;
  231. parse_fail:
  232. return NULL;
  233. }
  234. static struct dm_device * __init dm_parse_args(void)
  235. {
  236. struct dm_device *devices = NULL;
  237. struct dm_device **tail = &devices;
  238. struct dm_device *dev;
  239. char *str = dm_setup_args.str;
  240. unsigned long num_devices = dm_setup_args.num_devices;
  241. unsigned long i;
  242. if (!str)
  243. return NULL;
  244. for (i = 0; i < num_devices; i++) {
  245. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  246. if (!dev) {
  247. DMERR("failed to allocated memory for dev");
  248. goto error;
  249. }
  250. *tail = dev;
  251. tail = &dev->next;
  252. /*
  253. * devices are given minor numbers 0 - n-1
  254. * in the order they are found in the arg
  255. * string.
  256. */
  257. dev->minor = i;
  258. str = dm_parse_device(dev, str);
  259. if (!str) /* NULL indicates error in parsing, bail */
  260. goto error;
  261. str = dm_parse_targets(dev, str);
  262. if (!str)
  263. goto error;
  264. }
  265. return devices;
  266. error:
  267. dm_setup_cleanup(devices);
  268. return NULL;
  269. }
  270. /*
  271. * Parse the command-line parameters given our kernel, but do not
  272. * actually try to invoke the DM device now; that is handled by
  273. * dm_setup_drives after the low-level disk drivers have initialised.
  274. * dm format is described at the top of the file.
  275. *
  276. * Because dm minor numbers are assigned in assending order starting with 0,
  277. * You can assume the first device is /dev/dm-0, the next device is /dev/dm-1,
  278. * and so forth.
  279. */
  280. static int __init dm_setup(char *str)
  281. {
  282. struct dm_option opt;
  283. unsigned long num_devices;
  284. if (!str) {
  285. DMDEBUG("str is NULL");
  286. goto parse_fail;
  287. }
  288. opt.next = str;
  289. if (!get_dm_option(&opt, DM_FIELD_SEP))
  290. goto parse_fail;
  291. if (isdigit(opt.start[0])) { /* XXX: Optional number field */
  292. num_devices = simple_strtoul(opt.start, NULL, 10);
  293. str = opt.next;
  294. } else {
  295. num_devices = 1;
  296. /* Don't advance str */
  297. }
  298. if (num_devices > DM_MAX_DEVICES) {
  299. DMDEBUG("too many devices %lu > %d",
  300. num_devices, DM_MAX_DEVICES);
  301. }
  302. dm_setup_args.str = str;
  303. dm_setup_args.num_devices = num_devices;
  304. DMINFO("will configure %lu devices", num_devices);
  305. dm_early_setup = 1;
  306. return 1;
  307. parse_fail:
  308. DMWARN("Invalid arguments supplied to dm=.");
  309. return 0;
  310. }
  311. static void __init dm_setup_drives(void)
  312. {
  313. struct mapped_device *md = NULL;
  314. struct dm_table *table = NULL;
  315. struct dm_setup_target *target;
  316. struct dm_device *dev;
  317. char *uuid = NULL;
  318. fmode_t fmode = FMODE_READ;
  319. struct dm_device *devices;
  320. devices = dm_parse_args();
  321. for (dev = devices; dev; dev = dev->next) {
  322. if (dm_create(dev->minor, &md)) {
  323. DMDEBUG("failed to create the device");
  324. goto dm_create_fail;
  325. }
  326. DMDEBUG("created device '%s'", dm_device_name(md));
  327. /*
  328. * In addition to flagging the table below, the disk must be
  329. * set explicitly ro/rw.
  330. */
  331. set_disk_ro(dm_disk(md), dev->ro);
  332. if (!dev->ro)
  333. fmode |= FMODE_WRITE;
  334. if (dm_table_create(&table, fmode, dev->target_count, md)) {
  335. DMDEBUG("failed to create the table");
  336. goto dm_table_create_fail;
  337. }
  338. dm_lock_md_type(md);
  339. for (target = dev->target; target; target = target->next) {
  340. DMINFO("adding target '%llu %llu %s %s'",
  341. (unsigned long long) target->begin,
  342. (unsigned long long) target->length,
  343. target->type, target->params);
  344. if (dm_table_add_target(table, target->type,
  345. target->begin,
  346. target->length,
  347. target->params)) {
  348. DMDEBUG("failed to add the target"
  349. " to the table");
  350. goto add_target_fail;
  351. }
  352. }
  353. if (dm_table_complete(table)) {
  354. DMDEBUG("failed to complete the table");
  355. goto table_complete_fail;
  356. }
  357. /* Suspend the device so that we can bind it to the table. */
  358. if (dm_suspend(md, 0)) {
  359. DMDEBUG("failed to suspend the device pre-bind");
  360. goto suspend_fail;
  361. }
  362. /* Initial table load: acquire type of table. */
  363. dm_set_md_type(md, dm_table_get_type(table));
  364. /* Setup md->queue to reflect md's type. */
  365. if (dm_setup_md_queue(md, table)) {
  366. DMWARN("unable to set up device queue for new table.");
  367. goto setup_md_queue_fail;
  368. }
  369. /*
  370. * Bind the table to the device. This is the only way
  371. * to associate md->map with the table and set the disk
  372. * capacity directly.
  373. */
  374. if (dm_swap_table(md, table)) { /* should return NULL. */
  375. DMDEBUG("failed to bind the device to the table");
  376. goto table_bind_fail;
  377. }
  378. /* Finally, resume and the device should be ready. */
  379. if (dm_resume(md)) {
  380. DMDEBUG("failed to resume the device");
  381. goto resume_fail;
  382. }
  383. /* Export the dm device via the ioctl interface */
  384. if (!strcmp(DM_NO_UUID, dev->uuid))
  385. uuid = NULL;
  386. if (dm_ioctl_export(md, dev->name, uuid)) {
  387. DMDEBUG("failed to export device with given"
  388. " name and uuid");
  389. goto export_fail;
  390. }
  391. dm_unlock_md_type(md);
  392. DMINFO("dm-%d is ready", dev->minor);
  393. }
  394. dm_setup_cleanup(devices);
  395. return;
  396. export_fail:
  397. resume_fail:
  398. table_bind_fail:
  399. setup_md_queue_fail:
  400. suspend_fail:
  401. table_complete_fail:
  402. add_target_fail:
  403. dm_unlock_md_type(md);
  404. dm_table_create_fail:
  405. dm_put(md);
  406. dm_create_fail:
  407. DMWARN("starting dm-%d (%s) failed",
  408. dev->minor, dev->name);
  409. dm_setup_cleanup(devices);
  410. }
  411. __setup("dm=", dm_setup);
  412. void __init dm_run_setup(void)
  413. {
  414. if (!dm_early_setup)
  415. return;
  416. DMINFO("attempting early device configuration.");
  417. dm_setup_drives();
  418. }