blob: 0fe9c5f7d5e9c8f011008d861825ac35c339859b [file] [log] [blame]
Will Drewrya058da82010-06-09 17:47:38 -05001/* do_mounts_dm.c
2 * Copyright (C) 2010 The Chromium OS Authors <chromium-os-dev@chromium.org>
3 * All Rights Reserved.
4 * Based on do_mounts_md.c
5 *
6 * This file is released under the GPL.
7 */
8#include <linux/device-mapper.h>
9#include <linux/fs.h>
10#include <linux/string.h>
11
12#include "do_mounts.h"
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -080013#include "../drivers/md/dm.h"
Will Drewrya058da82010-06-09 17:47:38 -050014
15#define DM_MAX_NAME 32
16#define DM_MAX_UUID 129
17#define DM_NO_UUID "none"
18
19#define DM_MSG_PREFIX "init"
20
21/* Separators used for parsing the dm= argument. */
22#define DM_FIELD_SEP ' '
23#define DM_LINE_SEP ','
24
25/*
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.
30 */
31
32struct dm_setup_target {
33 sector_t begin;
34 sector_t length;
35 char *type;
36 char *params;
37 /* simple singly linked list */
38 struct dm_setup_target *next;
39};
40
41static struct {
42 int minor;
43 int ro;
44 char name[DM_MAX_NAME];
45 char uuid[DM_MAX_UUID];
46 char *targets;
47 struct dm_setup_target *target;
48 int target_count;
49} dm_setup_args __initdata;
50
51static __initdata int dm_early_setup;
52
53static size_t __init get_dm_option(char *str, char **next, char sep)
54{
55 size_t len = 0;
56 char *endp = NULL;
57
58 if (!str)
59 return 0;
60
61 endp = strchr(str, sep);
62 if (!endp) { /* act like strchrnul */
63 len = strlen(str);
64 endp = str + len;
65 } else {
66 len = endp - str;
67 }
68
69 if (endp == str)
70 return 0;
71
72 if (!next)
73 return len;
74
75 if (*endp == 0) {
76 /* Don't advance past the nul. */
77 *next = endp;
78 } else {
79 *next = endp + 1;
80 }
81 return len;
82}
83
84static int __init dm_setup_args_init(void)
85{
86 dm_setup_args.minor = 0;
87 dm_setup_args.ro = 0;
88 dm_setup_args.target = NULL;
89 dm_setup_args.target_count = 0;
90 return 0;
91}
92
93static int __init dm_setup_cleanup(void)
94{
95 struct dm_setup_target *target = dm_setup_args.target;
96 struct dm_setup_target *old_target = NULL;
97 while (target) {
98 kfree(target->type);
99 kfree(target->params);
100 old_target = target;
101 target = target->next;
102 kfree(old_target);
103 dm_setup_args.target_count--;
104 }
105 BUG_ON(dm_setup_args.target_count);
106 return 0;
107}
108
109static char * __init dm_setup_parse_device_args(char *str)
110{
111 char *next = NULL;
112 size_t len = 0;
113
114 /* Grab the logical name of the device to be exported to udev */
115 len = get_dm_option(str, &next, DM_FIELD_SEP);
116 if (!len) {
117 DMERR("failed to parse device name");
118 goto parse_fail;
119 }
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);
123
124 /* Grab the UUID value or "none" */
125 len = get_dm_option(str, &next, DM_FIELD_SEP);
126 if (!len) {
127 DMERR("failed to parse device uuid");
128 goto parse_fail;
129 }
130 len = min(len + 1, sizeof(dm_setup_args.uuid));
131 strlcpy(dm_setup_args.uuid, str, len);
132 str = skip_spaces(next);
133
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;
139 } else {
140 DMERR("failed to parse table mode");
141 goto parse_fail;
142 }
143 str = skip_spaces(str + 3);
144
145 return str;
146
147parse_fail:
148 return NULL;
149}
150
151static void __init dm_substitute_devices(char *str, size_t str_len)
152{
153 char *candidate = str;
154 char *candidate_end = str;
155 char old_char;
156 size_t len = 0;
157 dev_t dev;
158
159 if (str_len < 3)
160 return;
161
162 while (str && *str) {
163 candidate = strchr(str, '/');
164 if (!candidate)
165 break;
166
167 /* Avoid embedded slashes */
168 if (candidate != str && *(candidate - 1) != DM_FIELD_SEP) {
169 str = strchr(candidate, DM_FIELD_SEP);
170 continue;
171 }
172
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 */
176 continue;
177
178 /* Temporarily terminate with a nul */
179 candidate_end--;
180 old_char = *candidate_end;
181 *candidate_end = '\0';
182
183 DMDEBUG("converting candidate device '%s' to dev_t", candidate);
184 /* Use the boot-time specific device naming */
185 dev = name_to_dev_t(candidate);
186 *candidate_end = old_char;
187
188 DMDEBUG(" -> %u", dev);
189 /* No suitable replacement found */
190 if (!dev)
191 continue;
192
193 /* Rewrite the /dev/path as a major:minor */
194 len = snprintf(candidate, len, "%u:%u", MAJOR(dev), MINOR(dev));
195 if (!len) {
196 DMERR("error substituting device major/minor.");
197 break;
198 }
199 candidate += len;
200 /* Pad out with spaces (fixing our nul) */
201 while (candidate < candidate_end)
202 *(candidate++) = DM_FIELD_SEP;
203 }
204}
205
206static int __init dm_setup_parse_targets(char *str)
207{
208 char *next = NULL;
209 size_t len = 0;
210 struct dm_setup_target **target = NULL;
211
212 /* Targets are defined as per the table format but with a
213 * comma as a newline separator. */
214 target = &dm_setup_args.target;
215 while (str && *str) {
216 *target = kzalloc(sizeof(struct dm_setup_target), GFP_KERNEL);
217 if (!*target) {
218 DMERR("failed to allocate memory for target %d",
219 dm_setup_args.target_count);
220 goto parse_fail;
221 }
222 dm_setup_args.target_count++;
223
224 (*target)->begin = simple_strtoull(str, &next, 10);
225 if (!next || *next != DM_FIELD_SEP) {
226 DMERR("failed to parse starting sector for target %d",
227 dm_setup_args.target_count - 1);
228 goto parse_fail;
229 }
230 str = skip_spaces(next + 1);
231
232 (*target)->length = simple_strtoull(str, &next, 10);
233 if (!next || *next != DM_FIELD_SEP) {
234 DMERR("failed to parse length for target %d",
235 dm_setup_args.target_count - 1);
236 goto parse_fail;
237 }
238 str = skip_spaces(next + 1);
239
240 len = get_dm_option(str, &next, DM_FIELD_SEP);
241 if (!len ||
242 !((*target)->type = kstrndup(str, len, GFP_KERNEL))) {
243 DMERR("failed to parse type for target %d",
244 dm_setup_args.target_count - 1);
245 goto parse_fail;
246 }
247 str = skip_spaces(next);
248
249 len = get_dm_option(str, &next, DM_LINE_SEP);
250 if (!len ||
251 !((*target)->params = kstrndup(str, len, GFP_KERNEL))) {
252 DMERR("failed to parse params for target %d",
253 dm_setup_args.target_count - 1);
254 goto parse_fail;
255 }
256 str = skip_spaces(next);
257
258 /* Before moving on, walk through the copied target and
259 * attempt to replace all /dev/xxx with the major:minor number.
260 * It may not be possible to resolve them traditionally at
261 * boot-time. */
262 dm_substitute_devices((*target)->params, len);
263
264 target = &((*target)->next);
265 }
266 DMDEBUG("parsed %d targets", dm_setup_args.target_count);
267
268 return 0;
269
270parse_fail:
271 return 1;
272}
273
274/*
275 * Parse the command-line parameters given our kernel, but do not
276 * actually try to invoke the DM device now; that is handled by
277 * dm_setup_drive after the low-level disk drivers have initialised.
278 * dm format is as follows:
279 * dm="name uuid fmode,[table line 1],[table line 2],..."
280 * May be used with root=/dev/dm-0 as it always uses the first dm minor.
281 */
282
283static int __init dm_setup(char *str)
284{
285 dm_setup_args_init();
286
287 str = dm_setup_parse_device_args(str);
288 if (!str) {
289 DMDEBUG("str is NULL");
290 goto parse_fail;
291 }
292
293 /* Target parsing is delayed until we have dynamic memory */
294 dm_setup_args.targets = str;
295
296 printk(KERN_INFO "dm: will configure '%s' on dm-%d\n",
297 dm_setup_args.name, dm_setup_args.minor);
298
299 dm_early_setup = 1;
300 return 1;
301
302parse_fail:
303 printk(KERN_WARNING "dm: Invalid arguments supplied to dm=.\n");
304 return 0;
305}
306
307
308static void __init dm_setup_drive(void)
309{
310 struct mapped_device *md = NULL;
311 struct dm_table *table = NULL;
312 struct dm_setup_target *target;
313 char *uuid = dm_setup_args.uuid;
314 fmode_t fmode = FMODE_READ;
315
316 /* Finish parsing the targets. */
317 if (dm_setup_parse_targets(dm_setup_args.targets))
318 goto parse_fail;
319
320 if (dm_create(dm_setup_args.minor, &md)) {
321 DMDEBUG("failed to create the device");
322 goto dm_create_fail;
323 }
324 DMDEBUG("created device '%s'", dm_device_name(md));
325
326 /* In addition to flagging the table below, the disk must be
327 * set explicitly ro/rw. */
328 set_disk_ro(dm_disk(md), dm_setup_args.ro);
329
330 if (!dm_setup_args.ro)
331 fmode |= FMODE_WRITE;
332 if (dm_table_create(&table, fmode, dm_setup_args.target_count, md)) {
333 DMDEBUG("failed to create the table");
334 goto dm_table_create_fail;
335 }
336
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -0800337 dm_lock_md_type(md);
Will Drewrya058da82010-06-09 17:47:38 -0500338 target = dm_setup_args.target;
339 while (target) {
340 DMINFO("adding target '%llu %llu %s %s'",
341 (unsigned long long) target->begin,
342 (unsigned long long) target->length, target->type,
343 target->params);
344 if (dm_table_add_target(table, target->type, target->begin,
345 target->length, target->params)) {
346 DMDEBUG("failed to add the target to the table");
347 goto add_target_fail;
348 }
349 target = target->next;
350 }
351
352 if (dm_table_complete(table)) {
353 DMDEBUG("failed to complete the table");
354 goto table_complete_fail;
355 }
356
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -0800357 if (dm_get_md_type(md) == DM_TYPE_NONE) {
358 dm_set_md_type(md, dm_table_get_type(table));
359 if (dm_setup_md_queue(md, table)) {
360 DMWARN("unable to set up device queue for new table.");
361 goto setup_md_queue_fail;
362 }
363 } else if (dm_get_md_type(md) != dm_table_get_type(table)) {
364 DMWARN("can't change device type after initial table load.");
365 goto setup_md_queue_fail;
366 }
367
Will Drewrya058da82010-06-09 17:47:38 -0500368 /* Suspend the device so that we can bind it to the table. */
369 if (dm_suspend(md, 0)) {
370 DMDEBUG("failed to suspend the device pre-bind");
371 goto suspend_fail;
372 }
373
374 /* Bind the table to the device. This is the only way to associate
375 * md->map with the table and set the disk capacity directly. */
376 if (dm_swap_table(md, table)) { /* should return NULL. */
377 DMDEBUG("failed to bind the device to the table");
378 goto table_bind_fail;
379 }
380
381 /* Finally, resume and the device should be ready. */
382 if (dm_resume(md)) {
383 DMDEBUG("failed to resume the device");
384 goto resume_fail;
385 }
386
387 /* Export the dm device via the ioctl interface */
388 if (!strcmp(DM_NO_UUID, dm_setup_args.uuid))
389 uuid = NULL;
390 if (dm_ioctl_export(md, dm_setup_args.name, uuid)) {
391 DMDEBUG("failed to export device with given name and uuid");
392 goto export_fail;
393 }
394 printk(KERN_INFO "dm: dm-%d is ready\n", dm_setup_args.minor);
395
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -0800396 dm_unlock_md_type(md);
Will Drewrya058da82010-06-09 17:47:38 -0500397 dm_setup_cleanup();
398 return;
399
400export_fail:
401resume_fail:
402table_bind_fail:
403suspend_fail:
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -0800404setup_md_queue_fail:
Will Drewrya058da82010-06-09 17:47:38 -0500405table_complete_fail:
406add_target_fail:
Badhri Jagan Sridharanae8b4902016-02-08 16:47:41 -0800407 dm_unlock_md_type(md);
Will Drewrya058da82010-06-09 17:47:38 -0500408dm_table_create_fail:
409 dm_put(md);
410dm_create_fail:
411 dm_setup_cleanup();
412parse_fail:
413 printk(KERN_WARNING "dm: starting dm-%d (%s) failed\n",
414 dm_setup_args.minor, dm_setup_args.name);
415}
416
417__setup("dm=", dm_setup);
418
419void __init dm_run_setup(void)
420{
421 if (!dm_early_setup)
422 return;
423 printk(KERN_INFO "dm: attempting early device configuration.\n");
424 dm_setup_drive();
425}