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