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