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