San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 18 | #include <stdlib.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 29 | #include <linux/kdev_t.h> |
| 30 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 31 | #define LOG_TAG "Vold" |
| 32 | |
| 33 | #include <cutils/log.h> |
| 34 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 35 | #include <android-base/logging.h> |
| 36 | #include <android-base/stringprintf.h> |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 37 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 38 | #include "Devmapper.h" |
| 39 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 40 | #define DEVMAPPER_BUFFER_SIZE 4096 |
| 41 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | |
| 44 | static const char* kVoldPrefix = "vold:"; |
| 45 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 46 | void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize, |
| 47 | const char *name, unsigned flags) { |
| 48 | memset(io, 0, dataSize); |
| 49 | io->data_size = dataSize; |
| 50 | io->data_start = sizeof(struct dm_ioctl); |
| 51 | io->version[0] = 4; |
| 52 | io->version[1] = 0; |
| 53 | io->version[2] = 0; |
| 54 | io->flags = flags; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 55 | if (name) { |
Chih-Wei Huang | 7929deb | 2013-02-10 22:57:14 +0800 | [diff] [blame] | 56 | size_t ret = strlcpy(io->name, name, sizeof(io->name)); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 57 | if (ret >= sizeof(io->name)) |
| 58 | abort(); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 59 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 62 | int Devmapper::lookupActive(const char *name_raw, char *ubuffer, size_t len) { |
| 63 | auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw); |
| 64 | const char* name = name_string.c_str(); |
| 65 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 66 | char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 67 | if (!buffer) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 68 | SLOGE("Error allocating memory (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | int fd; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 73 | if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 74 | SLOGE("Error opening devmapper (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 75 | free(buffer); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | struct dm_ioctl *io = (struct dm_ioctl *) buffer; |
| 80 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 81 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 82 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 83 | if (errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 84 | SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 85 | } |
| 86 | free(buffer); |
| 87 | close(fd); |
| 88 | return -1; |
| 89 | } |
| 90 | close(fd); |
| 91 | |
| 92 | unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 93 | free(buffer); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 94 | snprintf(ubuffer, len, "/dev/block/dm-%u", minor); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 98 | int Devmapper::create(const char *name_raw, const char *loopFile, const char *key, |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 99 | unsigned long numSectors, char *ubuffer, size_t len) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 100 | auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw); |
| 101 | const char* name = name_string.c_str(); |
| 102 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 103 | char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 104 | if (!buffer) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 105 | SLOGE("Error allocating memory (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | int fd; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 110 | if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 111 | SLOGE("Error opening devmapper (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 112 | free(buffer); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | struct dm_ioctl *io = (struct dm_ioctl *) buffer; |
| 117 | |
| 118 | // Create the DM device |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 119 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 120 | |
| 121 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 122 | SLOGE("Error creating device mapping (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 123 | free(buffer); |
| 124 | close(fd); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | // Set the legacy geometry |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 129 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 130 | |
| 131 | char *geoParams = buffer + sizeof(struct dm_ioctl); |
| 132 | // bps=512 spc=8 res=32 nft=2 sec=8190 mid=0xf0 spt=63 hds=64 hid=0 bspf=8 rdcl=2 infs=1 bkbs=2 |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 133 | strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 134 | geoParams += strlen(geoParams) + 1; |
| 135 | geoParams = (char *) _align(geoParams, 8); |
| 136 | if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 137 | SLOGE("Error setting device geometry (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 138 | free(buffer); |
| 139 | close(fd); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | // Retrieve the device number we were allocated |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 144 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 145 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 146 | SLOGE("Error retrieving devmapper status (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 147 | free(buffer); |
| 148 | close(fd); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 153 | snprintf(ubuffer, len, "/dev/block/dm-%u", minor); |
| 154 | |
| 155 | // Load the table |
| 156 | struct dm_target_spec *tgt; |
| 157 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 158 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 159 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 160 | io->target_count = 1; |
| 161 | tgt->status = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 162 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 163 | tgt->sector_start = 0; |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 164 | tgt->length = numSectors; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 165 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 166 | strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 167 | |
| 168 | char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 169 | snprintf(cryptParams, |
| 170 | DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)), |
| 171 | "twofish %s 0 %s 0", key, loopFile); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 172 | cryptParams += strlen(cryptParams) + 1; |
| 173 | cryptParams = (char *) _align(cryptParams, 8); |
| 174 | tgt->next = cryptParams - buffer; |
| 175 | |
| 176 | if (ioctl(fd, DM_TABLE_LOAD, io)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 177 | SLOGE("Error loading mapping table (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 178 | free(buffer); |
| 179 | close(fd); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | // Resume the new table |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 184 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 185 | |
| 186 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 187 | SLOGE("Error Resuming (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 188 | free(buffer); |
| 189 | close(fd); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | free(buffer); |
| 194 | |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 195 | close(fd); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 199 | int Devmapper::destroy(const char *name_raw) { |
| 200 | auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw); |
| 201 | const char* name = name_string.c_str(); |
| 202 | |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 203 | char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 204 | if (!buffer) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 205 | SLOGE("Error allocating memory (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | int fd; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 210 | if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 211 | SLOGE("Error opening devmapper (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 212 | free(buffer); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | struct dm_ioctl *io = (struct dm_ioctl *) buffer; |
| 217 | |
| 218 | // Create the DM device |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 219 | ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 220 | |
| 221 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 222 | if (errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 223 | SLOGE("Error destroying device mapping (%s)", strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 224 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 225 | free(buffer); |
| 226 | close(fd); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | free(buffer); |
| 231 | close(fd); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 235 | int Devmapper::destroyAll() { |
| 236 | char *buffer = (char *) malloc(1024 * 64); |
| 237 | if (!buffer) { |
| 238 | SLOGE("Error allocating memory (%s)", strerror(errno)); |
| 239 | return -1; |
| 240 | } |
| 241 | memset(buffer, 0, (1024 * 64)); |
| 242 | |
| 243 | char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE); |
| 244 | if (!buffer2) { |
| 245 | SLOGE("Error allocating memory (%s)", strerror(errno)); |
| 246 | free(buffer); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | int fd; |
| 251 | if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) { |
| 252 | SLOGE("Error opening devmapper (%s)", strerror(errno)); |
| 253 | free(buffer); |
| 254 | free(buffer2); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | struct dm_ioctl *io = (struct dm_ioctl *) buffer; |
| 259 | ioctlInit(io, (1024 * 64), NULL, 0); |
| 260 | |
| 261 | if (ioctl(fd, DM_LIST_DEVICES, io)) { |
| 262 | SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno)); |
| 263 | free(buffer); |
| 264 | free(buffer2); |
| 265 | close(fd); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start); |
| 270 | if (!n->dev) { |
| 271 | free(buffer); |
| 272 | free(buffer2); |
| 273 | close(fd); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | unsigned nxt = 0; |
| 278 | do { |
| 279 | n = (struct dm_name_list *) (((char *) n) + nxt); |
| 280 | if (strncmp(n->name, kVoldPrefix, strlen(kVoldPrefix)) == 0) { |
| 281 | LOG(DEBUG) << "Tearing down stale dm device named " << n->name; |
| 282 | |
| 283 | memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE); |
| 284 | struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2; |
| 285 | ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0); |
| 286 | if (ioctl(fd, DM_DEV_REMOVE, io2)) { |
| 287 | if (errno != ENXIO) { |
| 288 | PLOG(WARNING) << "Failed to destroy dm device named " << n->name; |
| 289 | } |
| 290 | } |
| 291 | } else { |
| 292 | LOG(VERBOSE) << "Found unmanaged dm device named " << n->name; |
| 293 | } |
| 294 | nxt = n->next; |
| 295 | } while (nxt); |
| 296 | |
| 297 | free(buffer); |
| 298 | free(buffer2); |
| 299 | close(fd); |
| 300 | return 0; |
| 301 | } |
| 302 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 303 | void *Devmapper::_align(void *ptr, unsigned int a) |
| 304 | { |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 305 | unsigned long agn = --a; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 306 | |
| 307 | return (void *) (((unsigned long) ptr + agn) & ~agn); |
| 308 | } |