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