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