2 * Copyright (C) 2010 The Chromium OS Authors <chromium-os-dev@chromium.org>
4 * Based on do_mounts_md.c
6 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
10 #include <linux/string.h>
12 #include "do_mounts.h"
13 #include "../drivers/md/dm.h"
15 #define DM_MAX_NAME 32
16 #define DM_MAX_UUID 129
17 #define DM_NO_UUID "none"
19 #define DM_MSG_PREFIX "init"
21 /* Separators used for parsing the dm= argument. */
22 #define DM_FIELD_SEP ' '
23 #define DM_LINE_SEP ','
26 * When the device-mapper and any targets are compiled into the kernel
27 * (not a module), one target may be created and used as the root device at
28 * boot time with the parameters given with the boot line dm=...
29 * The code for that is here.
32 struct dm_setup_target {
37 /* simple singly linked list */
38 struct dm_setup_target *next;
44 char name[DM_MAX_NAME];
45 char uuid[DM_MAX_UUID];
47 struct dm_setup_target *target;
49 } dm_setup_args __initdata;
51 static __initdata int dm_early_setup;
53 static size_t __init get_dm_option(char *str, char **next, char sep)
61 endp = strchr(str, sep);
62 if (!endp) { /* act like strchrnul */
76 /* Don't advance past the nul. */
84 static int __init dm_setup_args_init(void)
86 dm_setup_args.minor = 0;
88 dm_setup_args.target = NULL;
89 dm_setup_args.target_count = 0;
93 static int __init dm_setup_cleanup(void)
95 struct dm_setup_target *target = dm_setup_args.target;
96 struct dm_setup_target *old_target = NULL;
99 kfree(target->params);
101 target = target->next;
103 dm_setup_args.target_count--;
105 BUG_ON(dm_setup_args.target_count);
109 static char * __init dm_setup_parse_device_args(char *str)
114 /* Grab the logical name of the device to be exported to udev */
115 len = get_dm_option(str, &next, DM_FIELD_SEP);
117 DMERR("failed to parse device name");
120 len = min(len + 1, sizeof(dm_setup_args.name));
121 strlcpy(dm_setup_args.name, str, len); /* includes nul */
122 str = skip_spaces(next);
124 /* Grab the UUID value or "none" */
125 len = get_dm_option(str, &next, DM_FIELD_SEP);
127 DMERR("failed to parse device uuid");
130 len = min(len + 1, sizeof(dm_setup_args.uuid));
131 strlcpy(dm_setup_args.uuid, str, len);
132 str = skip_spaces(next);
134 /* Determine if the table/device will be read only or read-write */
135 if (!strncmp("ro,", str, 3)) {
136 dm_setup_args.ro = 1;
137 } else if (!strncmp("rw,", str, 3)) {
138 dm_setup_args.ro = 0;
140 DMERR("failed to parse table mode");
143 str = skip_spaces(str + 3);
151 static void __init dm_substitute_devices(char *str, size_t str_len)
153 char *candidate = str;
154 char *candidate_end = str;
162 while (str && *str) {
163 candidate = strchr(str, '/');
167 /* Avoid embedded slashes */
168 if (candidate != str && *(candidate - 1) != DM_FIELD_SEP) {
169 str = strchr(candidate, DM_FIELD_SEP);
173 len = get_dm_option(candidate, &candidate_end, DM_FIELD_SEP);
174 str = skip_spaces(candidate_end);
175 if (len < 3 || len > 37) /* name_to_dev_t max; maj:mix min */
178 /* Temporarily terminate with a nul */
181 old_char = *candidate_end;
182 *candidate_end = '\0';
184 DMDEBUG("converting candidate device '%s' to dev_t", candidate);
185 /* Use the boot-time specific device naming */
186 dev = name_to_dev_t(candidate);
187 *candidate_end = old_char;
189 DMDEBUG(" -> %u", dev);
190 /* No suitable replacement found */
194 /* Rewrite the /dev/path as a major:minor */
195 len = snprintf(candidate, len, "%u:%u", MAJOR(dev), MINOR(dev));
197 DMERR("error substituting device major/minor.");
201 /* Pad out with spaces (fixing our nul) */
202 while (candidate < candidate_end)
203 *(candidate++) = DM_FIELD_SEP;
207 static int __init dm_setup_parse_targets(char *str)
211 struct dm_setup_target **target = NULL;
213 /* Targets are defined as per the table format but with a
214 * comma as a newline separator. */
215 target = &dm_setup_args.target;
216 while (str && *str) {
217 *target = kzalloc(sizeof(struct dm_setup_target), GFP_KERNEL);
219 DMERR("failed to allocate memory for target %d",
220 dm_setup_args.target_count);
223 dm_setup_args.target_count++;
225 (*target)->begin = simple_strtoull(str, &next, 10);
226 if (!next || *next != DM_FIELD_SEP) {
227 DMERR("failed to parse starting sector for target %d",
228 dm_setup_args.target_count - 1);
231 str = skip_spaces(next + 1);
233 (*target)->length = simple_strtoull(str, &next, 10);
234 if (!next || *next != DM_FIELD_SEP) {
235 DMERR("failed to parse length for target %d",
236 dm_setup_args.target_count - 1);
239 str = skip_spaces(next + 1);
241 len = get_dm_option(str, &next, DM_FIELD_SEP);
243 !((*target)->type = kstrndup(str, len, GFP_KERNEL))) {
244 DMERR("failed to parse type for target %d",
245 dm_setup_args.target_count - 1);
248 str = skip_spaces(next);
250 len = get_dm_option(str, &next, DM_LINE_SEP);
252 !((*target)->params = kstrndup(str, len, GFP_KERNEL))) {
253 DMERR("failed to parse params for target %d",
254 dm_setup_args.target_count - 1);
257 str = skip_spaces(next);
259 /* Before moving on, walk through the copied target and
260 * attempt to replace all /dev/xxx with the major:minor number.
261 * It may not be possible to resolve them traditionally at
263 dm_substitute_devices((*target)->params, len);
265 target = &((*target)->next);
267 DMDEBUG("parsed %d targets", dm_setup_args.target_count);
276 * Parse the command-line parameters given our kernel, but do not
277 * actually try to invoke the DM device now; that is handled by
278 * dm_setup_drive after the low-level disk drivers have initialised.
279 * dm format is as follows:
280 * dm="name uuid fmode,[table line 1],[table line 2],..."
281 * May be used with root=/dev/dm-0 as it always uses the first dm minor.
284 static int __init dm_setup(char *str)
286 dm_setup_args_init();
288 str = dm_setup_parse_device_args(str);
290 DMDEBUG("str is NULL");
294 /* Target parsing is delayed until we have dynamic memory */
295 dm_setup_args.targets = str;
297 printk(KERN_INFO "dm: will configure '%s' on dm-%d\n",
298 dm_setup_args.name, dm_setup_args.minor);
304 printk(KERN_WARNING "dm: Invalid arguments supplied to dm=.\n");
309 static void __init dm_setup_drive(void)
311 struct mapped_device *md = NULL;
312 struct dm_table *table = NULL;
313 struct dm_setup_target *target;
314 char *uuid = dm_setup_args.uuid;
315 fmode_t fmode = FMODE_READ;
317 /* Finish parsing the targets. */
318 if (dm_setup_parse_targets(dm_setup_args.targets))
321 if (dm_create(dm_setup_args.minor, &md)) {
322 DMDEBUG("failed to create the device");
325 DMDEBUG("created device '%s'", dm_device_name(md));
327 /* In addition to flagging the table below, the disk must be
328 * set explicitly ro/rw. */
329 set_disk_ro(dm_disk(md), dm_setup_args.ro);
331 if (!dm_setup_args.ro)
332 fmode |= FMODE_WRITE;
333 if (dm_table_create(&table, fmode, dm_setup_args.target_count, md)) {
334 DMDEBUG("failed to create the table");
335 goto dm_table_create_fail;
339 target = dm_setup_args.target;
341 DMINFO("adding target '%llu %llu %s %s'",
342 (unsigned long long) target->begin,
343 (unsigned long long) target->length, target->type,
345 if (dm_table_add_target(table, target->type, target->begin,
346 target->length, target->params)) {
347 DMDEBUG("failed to add the target to the table");
348 goto add_target_fail;
350 target = target->next;
353 if (dm_table_complete(table)) {
354 DMDEBUG("failed to complete the table");
355 goto table_complete_fail;
358 if (dm_get_md_type(md) == DM_TYPE_NONE) {
359 dm_set_md_type(md, dm_table_get_type(table));
360 if (dm_setup_md_queue(md)) {
361 DMWARN("unable to set up device queue for new table.");
362 goto setup_md_queue_fail;
364 } else if (dm_get_md_type(md) != dm_table_get_type(table)) {
365 DMWARN("can't change device type after initial table load.");
366 goto setup_md_queue_fail;
369 /* Suspend the device so that we can bind it to the table. */
370 if (dm_suspend(md, 0)) {
371 DMDEBUG("failed to suspend the device pre-bind");
375 /* Bind the table to the device. This is the only way to associate
376 * md->map with the table and set the disk capacity directly. */
377 if (dm_swap_table(md, table)) { /* should return NULL. */
378 DMDEBUG("failed to bind the device to the table");
379 goto table_bind_fail;
382 /* Finally, resume and the device should be ready. */
384 DMDEBUG("failed to resume the device");
388 /* Export the dm device via the ioctl interface */
389 if (!strcmp(DM_NO_UUID, dm_setup_args.uuid))
391 if (dm_ioctl_export(md, dm_setup_args.name, uuid)) {
392 DMDEBUG("failed to export device with given name and uuid");
395 printk(KERN_INFO "dm: dm-%d is ready\n", dm_setup_args.minor);
397 dm_unlock_md_type(md);
408 dm_unlock_md_type(md);
409 dm_table_create_fail:
414 printk(KERN_WARNING "dm: starting dm-%d (%s) failed\n",
415 dm_setup_args.minor, dm_setup_args.name);
418 __setup("dm=", dm_setup);
420 void __init dm_run_setup(void)
424 printk(KERN_INFO "dm: attempting early device configuration.\n");