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