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