San Mehat | a19b250 | 2010-01-06 10:33:53 -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 | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 24 | #include <sys/mount.h> |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 28 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 29 | #include <linux/kdev_t.h> |
| 30 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -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> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 36 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 37 | #include "Asec.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 38 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 39 | int Loop::dumpState(SocketClient *c) { |
| 40 | int i; |
| 41 | int fd; |
| 42 | char filename[256]; |
| 43 | |
| 44 | for (i = 0; i < LOOP_MAX; i++) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 45 | struct loop_info64 li; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 46 | int rc; |
| 47 | |
| 48 | sprintf(filename, "/dev/block/loop%d", i); |
| 49 | |
| 50 | if ((fd = open(filename, O_RDWR)) < 0) { |
| 51 | if (errno != ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 52 | SLOGE("Unable to open %s (%s)", filename, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 53 | } else { |
| 54 | continue; |
| 55 | } |
| 56 | return -1; |
| 57 | } |
| 58 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 59 | rc = ioctl(fd, LOOP_GET_STATUS64, &li); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 60 | close(fd); |
| 61 | if (rc < 0 && errno == ENXIO) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | if (rc < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 66 | SLOGE("Unable to get loop status for %s (%s)", filename, |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 67 | strerror(errno)); |
| 68 | return -1; |
| 69 | } |
| 70 | char *tmp = NULL; |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 71 | asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number, |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 72 | MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice), |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 73 | MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name, |
| 74 | li.lo_file_name); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 75 | c->sendMsg(0, tmp, false); |
| 76 | free(tmp); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int Loop::lookupActive(const char *id, char *buffer, size_t len) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 82 | int i; |
| 83 | int fd; |
| 84 | char filename[256]; |
| 85 | |
| 86 | memset(buffer, 0, len); |
| 87 | |
| 88 | for (i = 0; i < LOOP_MAX; i++) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 89 | struct loop_info64 li; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 90 | int rc; |
| 91 | |
| 92 | sprintf(filename, "/dev/block/loop%d", i); |
| 93 | |
| 94 | if ((fd = open(filename, O_RDWR)) < 0) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 95 | if (errno != ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 96 | SLOGE("Unable to open %s (%s)", filename, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 97 | } else { |
| 98 | continue; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 99 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 100 | return -1; |
| 101 | } |
| 102 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 103 | rc = ioctl(fd, LOOP_GET_STATUS64, &li); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 104 | close(fd); |
| 105 | if (rc < 0 && errno == ENXIO) { |
| 106 | continue; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (rc < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 110 | SLOGE("Unable to get loop status for %s (%s)", filename, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 111 | strerror(errno)); |
| 112 | return -1; |
| 113 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 114 | if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (i == LOOP_MAX) { |
| 120 | errno = ENOENT; |
| 121 | return -1; |
| 122 | } |
| 123 | strncpy(buffer, filename, len -1); |
| 124 | return 0; |
| 125 | } |
| 126 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 127 | int Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 128 | int i; |
| 129 | int fd; |
| 130 | char filename[256]; |
| 131 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 132 | for (i = 0; i < LOOP_MAX; i++) { |
Kenny Root | 7c16502 | 2011-02-01 15:58:25 -0800 | [diff] [blame] | 133 | struct loop_info64 li; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 134 | int rc; |
| 135 | |
| 136 | sprintf(filename, "/dev/block/loop%d", i); |
| 137 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 138 | /* |
| 139 | * The kernel starts us off with 8 loop nodes, but more |
| 140 | * are created on-demand if needed. |
| 141 | */ |
| 142 | mode_t mode = 0660 | S_IFBLK; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 143 | unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8); |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 144 | if (mknod(filename, mode, dev) < 0) { |
| 145 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 146 | SLOGE("Error creating loop device node (%s)", strerror(errno)); |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | } |
| 150 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 151 | if ((fd = open(filename, O_RDWR)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 152 | SLOGE("Unable to open %s (%s)", filename, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
Kenny Root | 7c16502 | 2011-02-01 15:58:25 -0800 | [diff] [blame] | 156 | rc = ioctl(fd, LOOP_GET_STATUS64, &li); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 157 | if (rc < 0 && errno == ENXIO) |
| 158 | break; |
| 159 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 160 | close(fd); |
| 161 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 162 | if (rc < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 163 | SLOGE("Unable to get loop status for %s (%s)", filename, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 164 | strerror(errno)); |
| 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (i == LOOP_MAX) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 170 | SLOGE("Exhausted all loop devices"); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 171 | errno = ENOSPC; |
| 172 | return -1; |
| 173 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 174 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame] | 175 | strncpy(loopDeviceBuffer, filename, len -1); |
| 176 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 177 | int file_fd; |
| 178 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 179 | if ((file_fd = open(loopFile, O_RDWR)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 180 | SLOGE("Unable to open %s (%s)", loopFile, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 181 | close(fd); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 186 | SLOGE("Error setting up loopback interface (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 187 | close(file_fd); |
| 188 | close(fd); |
| 189 | return -1; |
| 190 | } |
| 191 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 192 | struct loop_info64 li; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 193 | |
| 194 | memset(&li, 0, sizeof(li)); |
Peter Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 195 | strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE); |
| 196 | strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 197 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 198 | if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 199 | SLOGE("Error setting loopback status (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 200 | close(file_fd); |
| 201 | close(fd); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | close(fd); |
| 206 | close(file_fd); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int Loop::destroyByDevice(const char *loopDevice) { |
| 212 | int device_fd; |
| 213 | |
| 214 | device_fd = open(loopDevice, O_RDONLY); |
| 215 | if (device_fd < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 216 | SLOGE("Failed to open loop (%d)", errno); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 221 | SLOGE("Failed to destroy loop (%d)", errno); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 222 | close(device_fd); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | close(device_fd); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 230 | int Loop::destroyByFile(const char * /*loopFile*/) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 231 | errno = ENOSYS; |
| 232 | return -1; |
| 233 | } |
| 234 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 235 | int Loop::createImageFile(const char *file, unsigned int numSectors) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 236 | int fd; |
| 237 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 238 | if ((fd = creat(file, 0600)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 239 | SLOGE("Error creating imagefile (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 243 | if (ftruncate(fd, numSectors * 512) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 244 | SLOGE("Error truncating imagefile (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 245 | close(fd); |
| 246 | return -1; |
| 247 | } |
| 248 | close(fd); |
| 249 | return 0; |
| 250 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 251 | |
| 252 | int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) { |
| 253 | int fd; |
| 254 | struct asec_superblock buffer; |
| 255 | |
| 256 | if ((fd = open(loopDevice, O_RDONLY)) < 0) { |
| 257 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 258 | destroyByDevice(loopDevice); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | if (ioctl(fd, BLKGETSIZE, nr_sec)) { |
| 263 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 264 | destroyByDevice(loopDevice); |
| 265 | close(fd); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Try to read superblock. |
| 271 | */ |
| 272 | memset(&buffer, 0, sizeof(struct asec_superblock)); |
| 273 | if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) { |
| 274 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 275 | close(fd); |
| 276 | destroyByDevice(loopDevice); |
| 277 | return -1; |
| 278 | } |
| 279 | if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 280 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 281 | close(fd); |
| 282 | destroyByDevice(loopDevice); |
| 283 | return -1; |
| 284 | } |
| 285 | close(fd); |
| 286 | |
| 287 | /* |
| 288 | * Superblock successfully read. Copy to caller's struct. |
| 289 | */ |
| 290 | memcpy(sb, &buffer, sizeof(struct asec_superblock)); |
| 291 | return 0; |
| 292 | } |