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