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