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