San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [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 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 21 | #include <mntent.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mount.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 30 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 31 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | |
| 33 | #define LOG_TAG "Vold" |
| 34 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 35 | #include <openssl/md5.h> |
| 36 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 37 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #include <cutils/log.h> |
| 39 | |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 40 | #include <selinux/android.h> |
| 41 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 42 | #include <sysutils/NetlinkEvent.h> |
| 43 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
| 45 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 47 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 48 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 49 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 51 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 52 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 53 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 54 | #include "Asec.h" |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 55 | #include "VoldUtil.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 56 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 57 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 58 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 59 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 60 | #define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\ |
| 61 | + (number & (~((1U << po2) - 1)))) |
| 62 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 63 | /* writes superblock at end of file or device given by name */ |
| 64 | static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) { |
| 65 | int sbfd = open(name, O_RDWR); |
| 66 | if (sbfd < 0) { |
| 67 | SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno)); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 72 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
| 73 | close(sbfd); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 78 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
| 79 | close(sbfd); |
| 80 | return -1; |
| 81 | } |
| 82 | close(sbfd); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int adjustSectorNumExt4(unsigned numSectors) { |
Daniel Rosenberg | e9196fe | 2014-06-10 17:16:03 -0700 | [diff] [blame] | 87 | // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for |
| 88 | // preventing costly operations or unexpected ENOSPC error. |
| 89 | // Ext4::format() uses default block size without clustering. |
| 90 | unsigned clusterSectors = 4096 / 512; |
| 91 | unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0); |
| 92 | numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 93 | return ROUND_UP_POWER_OF_2(numSectors, 3); |
| 94 | } |
| 95 | |
| 96 | static int adjustSectorNumFAT(unsigned numSectors) { |
| 97 | /* |
| 98 | * Add some headroom |
| 99 | */ |
| 100 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 101 | numSectors += fatSize + 2; |
| 102 | /* |
| 103 | * FAT is aligned to 32 kb with 512b sectors. |
| 104 | */ |
| 105 | return ROUND_UP_POWER_OF_2(numSectors, 6); |
| 106 | } |
| 107 | |
| 108 | static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) { |
| 109 | if (Loop::lookupActive(idHash, buffer, len)) { |
| 110 | if (Loop::create(idHash, asecFileName, buffer, len)) { |
| 111 | SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno)); |
| 112 | return -1; |
| 113 | } |
| 114 | if (debug) { |
| 115 | SLOGD("New loop device created at %s", buffer); |
| 116 | } |
| 117 | } else { |
| 118 | if (debug) { |
| 119 | SLOGD("Found active loopback for %s at %s", asecFileName, buffer); |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) { |
| 126 | if (strcmp(key, "none")) { |
| 127 | if (Devmapper::lookupActive(idHash, buffer, len)) { |
| 128 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, |
| 129 | buffer, len)) { |
| 130 | SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno)); |
| 131 | return -1; |
| 132 | } |
| 133 | if (debug) { |
| 134 | SLOGD("New devmapper instance created at %s", buffer); |
| 135 | } |
| 136 | } else { |
| 137 | if (debug) { |
| 138 | SLOGD("Found active devmapper for %s at %s", asecFileName, buffer); |
| 139 | } |
| 140 | } |
| 141 | *createdDMDevice = true; |
| 142 | } else { |
| 143 | strcpy(buffer, loopDevice); |
| 144 | *createdDMDevice = false; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void waitForDevMapper(const char *dmDevice) { |
| 150 | /* |
| 151 | * Wait for the device mapper node to be created. Sometimes it takes a |
| 152 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 153 | * but that would take more effort. |
| 154 | */ |
| 155 | int tries = 25; |
| 156 | while (tries--) { |
| 157 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 158 | break; |
| 159 | } |
| 160 | usleep(40 * 1000); |
| 161 | } |
| 162 | } |
| 163 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 164 | VolumeManager *VolumeManager::sInstance = NULL; |
| 165 | |
| 166 | VolumeManager *VolumeManager::Instance() { |
| 167 | if (!sInstance) |
| 168 | sInstance = new VolumeManager(); |
| 169 | return sInstance; |
| 170 | } |
| 171 | |
| 172 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 173 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 174 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 175 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 176 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 177 | mUmsSharingCount = 0; |
| 178 | mSavedDirtyRatio = -1; |
| 179 | // set dirty ratio to 0 when UMS is active |
| 180 | mUmsDirtyRatio = 0; |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 181 | mVolManagerDisabled = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 185 | delete mVolumes; |
| 186 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 189 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 190 | static const char* digits = "0123456789abcdef"; |
| 191 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 192 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 193 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 194 | if (buffer == NULL) { |
| 195 | SLOGE("Destination buffer is NULL"); |
| 196 | errno = ESPIPE; |
| 197 | return NULL; |
| 198 | } else if (id == NULL) { |
| 199 | SLOGE("Source buffer is NULL"); |
| 200 | errno = ESPIPE; |
| 201 | return NULL; |
| 202 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 203 | SLOGE("Target hash buffer size < %d bytes (%zu)", |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 204 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 205 | errno = ESPIPE; |
| 206 | return NULL; |
| 207 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 208 | |
| 209 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 210 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 211 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 212 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 213 | *p++ = digits[sig[i] >> 4]; |
| 214 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 215 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 216 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 217 | |
| 218 | return buffer; |
| 219 | } |
| 220 | |
| 221 | void VolumeManager::setDebug(bool enable) { |
| 222 | mDebug = enable; |
| 223 | VolumeCollection::iterator it; |
| 224 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 225 | (*it)->setDebug(enable); |
| 226 | } |
| 227 | } |
| 228 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 229 | int VolumeManager::start() { |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int VolumeManager::stop() { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int VolumeManager::addVolume(Volume *v) { |
| 238 | mVolumes->push_back(v); |
| 239 | return 0; |
| 240 | } |
| 241 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 242 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 243 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 244 | const char *devpath = evt->findParam("DEVPATH"); |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 245 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 246 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 247 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 248 | VolumeCollection::iterator it; |
| 249 | bool hit = false; |
| 250 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 251 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 252 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 253 | SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 254 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 255 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 261 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 262 | SLOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 263 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 267 | int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 268 | VolumeCollection::iterator i; |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 269 | char msg[256]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 270 | |
| 271 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 272 | char *buffer; |
| 273 | asprintf(&buffer, "%s %s %d", |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 274 | (*i)->getLabel(), (*i)->getFuseMountpoint(), |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 275 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 276 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 277 | free(buffer); |
JP Abgrall | 40b64a6 | 2014-07-24 18:02:16 -0700 | [diff] [blame] | 278 | if (broadcast) { |
| 279 | if((*i)->getUuid()) { |
| 280 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(), |
| 281 | (*i)->getFuseMountpoint(), (*i)->getUuid()); |
| 282 | mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange, |
| 283 | msg, false); |
| 284 | } |
| 285 | if((*i)->getUserLabel()) { |
| 286 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(), |
| 287 | (*i)->getFuseMountpoint(), (*i)->getUserLabel()); |
| 288 | mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange, |
| 289 | msg, false); |
| 290 | } |
| 291 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 292 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 293 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 296 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 297 | int VolumeManager::formatVolume(const char *label, bool wipe) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 298 | Volume *v = lookupVolume(label); |
| 299 | |
| 300 | if (!v) { |
| 301 | errno = ENOENT; |
| 302 | return -1; |
| 303 | } |
| 304 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 305 | if (mVolManagerDisabled) { |
| 306 | errno = EBUSY; |
| 307 | return -1; |
| 308 | } |
| 309 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 310 | return v->formatVol(wipe); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 313 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 314 | char idHash[33]; |
| 315 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 316 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | memset(mountPath, 0, mountPathLen); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 321 | int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 322 | if ((written < 0) || (written >= mountPathLen)) { |
| 323 | errno = EINVAL; |
| 324 | return -1; |
| 325 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 326 | |
| 327 | if (access(mountPath, F_OK)) { |
| 328 | errno = ENOENT; |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 335 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 336 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 337 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 338 | if (!isLegalAsecId(id)) { |
| 339 | SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id); |
| 340 | errno = EINVAL; |
| 341 | return -1; |
| 342 | } |
| 343 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 344 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 345 | SLOGE("Couldn't find ASEC %s", id); |
| 346 | return -1; |
| 347 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 348 | |
| 349 | memset(buffer, 0, maxlen); |
| 350 | if (access(asecFileName, F_OK)) { |
| 351 | errno = ENOENT; |
| 352 | return -1; |
| 353 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 354 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 355 | int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
| 356 | if ((written < 0) || (written >= maxlen)) { |
| 357 | SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id); |
| 358 | errno = EINVAL; |
| 359 | return -1; |
| 360 | } |
| 361 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 365 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 366 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 367 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 368 | if (!isLegalAsecId(id)) { |
| 369 | SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id); |
| 370 | errno = EINVAL; |
| 371 | return -1; |
| 372 | } |
| 373 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 374 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 375 | SLOGE("Couldn't find ASEC %s", id); |
| 376 | return -1; |
| 377 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 378 | |
| 379 | memset(buffer, 0, maxlen); |
| 380 | if (access(asecFileName, F_OK)) { |
| 381 | errno = ENOENT; |
| 382 | return -1; |
| 383 | } |
| 384 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 385 | int written = snprintf(buffer, maxlen, "%s", asecFileName); |
| 386 | if ((written < 0) || (written >= maxlen)) { |
| 387 | errno = EINVAL; |
| 388 | return -1; |
| 389 | } |
| 390 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 394 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 395 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 396 | struct asec_superblock sb; |
| 397 | memset(&sb, 0, sizeof(sb)); |
| 398 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 399 | if (!isLegalAsecId(id)) { |
| 400 | SLOGE("createAsec: Invalid asec id \"%s\"", id); |
| 401 | errno = EINVAL; |
| 402 | return -1; |
| 403 | } |
| 404 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 405 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 406 | bool usingExt4 = false; |
| 407 | if (wantFilesystem) { |
| 408 | usingExt4 = !strcmp(fstype, "ext4"); |
| 409 | if (usingExt4) { |
| 410 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 411 | } else if (strcmp(fstype, "fat")) { |
| 412 | SLOGE("Invalid filesystem type %s", fstype); |
| 413 | errno = EINVAL; |
| 414 | return -1; |
| 415 | } |
| 416 | } |
| 417 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 418 | sb.magic = ASEC_SB_MAGIC; |
| 419 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 420 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 421 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 422 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 423 | errno = EINVAL; |
| 424 | return -1; |
| 425 | } |
| 426 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 427 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 428 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 429 | errno = EADDRINUSE; |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 434 | |
| 435 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 436 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 437 | asecFileName, strerror(errno)); |
| 438 | errno = EADDRINUSE; |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT; |
| 443 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 444 | int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
| 445 | if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) { |
| 446 | errno = EINVAL; |
| 447 | return -1; |
| 448 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 449 | |
| 450 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 451 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 452 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 453 | errno = EADDRINUSE; |
| 454 | return -1; |
| 455 | } |
| 456 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 457 | unsigned numImgSectors; |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 458 | if (usingExt4) |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 459 | numImgSectors = adjustSectorNumExt4(numSectors); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 460 | else |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 461 | numImgSectors = adjustSectorNumFAT(numSectors); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 462 | |
| 463 | // Add +1 for our superblock which is at the end |
| 464 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 465 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 466 | return -1; |
| 467 | } |
| 468 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 469 | char idHash[33]; |
| 470 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 471 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 472 | unlink(asecFileName); |
| 473 | return -1; |
| 474 | } |
| 475 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 476 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 477 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 478 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 479 | unlink(asecFileName); |
| 480 | return -1; |
| 481 | } |
| 482 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 483 | char dmDevice[255]; |
| 484 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 485 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 486 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 487 | // XXX: This is all we support for now |
| 488 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 489 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 490 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 491 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 492 | Loop::destroyByDevice(loopDevice); |
| 493 | unlink(asecFileName); |
| 494 | return -1; |
| 495 | } |
| 496 | cleanupDm = true; |
| 497 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 498 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 499 | strcpy(dmDevice, loopDevice); |
| 500 | } |
| 501 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 502 | /* |
| 503 | * Drop down the superblock at the end of the file |
| 504 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 505 | if (writeSuperBlock(loopDevice, &sb, numImgSectors)) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 506 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 507 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 508 | } |
| 509 | Loop::destroyByDevice(loopDevice); |
| 510 | unlink(asecFileName); |
| 511 | return -1; |
| 512 | } |
| 513 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 514 | if (wantFilesystem) { |
| 515 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 516 | char mountPoint[255]; |
| 517 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 518 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 519 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 520 | SLOGE("ASEC fs format failed: couldn't construct mountPoint"); |
| 521 | if (cleanupDm) { |
| 522 | Devmapper::destroy(idHash); |
| 523 | } |
| 524 | Loop::destroyByDevice(loopDevice); |
| 525 | unlink(asecFileName); |
| 526 | return -1; |
| 527 | } |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 528 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 529 | if (usingExt4) { |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 530 | formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 531 | } else { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 532 | formatStatus = Fat::format(dmDevice, numImgSectors, 0); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 533 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 534 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 535 | if (formatStatus < 0) { |
| 536 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 537 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 538 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 539 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 540 | Loop::destroyByDevice(loopDevice); |
| 541 | unlink(asecFileName); |
| 542 | return -1; |
| 543 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 544 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 545 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 546 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 547 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 548 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 549 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 550 | } |
| 551 | Loop::destroyByDevice(loopDevice); |
| 552 | unlink(asecFileName); |
| 553 | return -1; |
| 554 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 555 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 556 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 557 | int mountStatus; |
| 558 | if (usingExt4) { |
| 559 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 560 | } else { |
| 561 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 562 | false); |
| 563 | } |
| 564 | |
| 565 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 566 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 567 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 568 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 569 | } |
| 570 | Loop::destroyByDevice(loopDevice); |
| 571 | unlink(asecFileName); |
| 572 | return -1; |
| 573 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 574 | |
| 575 | if (usingExt4) { |
| 576 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 577 | if (dirfd >= 0) { |
| 578 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 579 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 580 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 581 | } |
| 582 | close(dirfd); |
| 583 | } |
| 584 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 585 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 586 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 587 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 588 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 589 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 593 | int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) { |
| 594 | char asecFileName[255]; |
| 595 | char mountPoint[255]; |
| 596 | bool cleanupDm = false; |
| 597 | |
| 598 | if (!isLegalAsecId(id)) { |
| 599 | SLOGE("resizeAsec: Invalid asec id \"%s\"", id); |
| 600 | errno = EINVAL; |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 605 | SLOGE("Couldn't find ASEC %s", id); |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 610 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 611 | SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id); |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | if (isMountpointMounted(mountPoint)) { |
| 616 | SLOGE("ASEC %s mounted. Unmount before resizing", id); |
| 617 | errno = EBUSY; |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | struct asec_superblock sb; |
| 622 | int fd; |
| 623 | unsigned int oldNumSec = 0; |
| 624 | |
| 625 | if ((fd = open(asecFileName, O_RDONLY)) < 0) { |
| 626 | SLOGE("Failed to open ASEC file (%s)", strerror(errno)); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | struct stat info; |
| 631 | if (fstat(fd, &info) < 0) { |
| 632 | SLOGE("Failed to get file size (%s)", strerror(errno)); |
| 633 | close(fd); |
| 634 | return -1; |
| 635 | } |
| 636 | |
| 637 | oldNumSec = info.st_size / 512; |
| 638 | |
| 639 | unsigned numImgSectors; |
| 640 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) |
| 641 | numImgSectors = adjustSectorNumExt4(numSectors); |
| 642 | else |
| 643 | numImgSectors = adjustSectorNumFAT(numSectors); |
| 644 | /* |
| 645 | * add one block for the superblock |
| 646 | */ |
| 647 | SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 648 | if (oldNumSec == numImgSectors + 1) { |
| 649 | SLOGW("Size unchanged; ignoring resize request"); |
| 650 | return 0; |
| 651 | } else if (oldNumSec > numImgSectors + 1) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 652 | SLOGE("Only growing is currently supported."); |
| 653 | close(fd); |
| 654 | return -1; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Try to read superblock. |
| 659 | */ |
| 660 | memset(&sb, 0, sizeof(struct asec_superblock)); |
| 661 | if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) { |
| 662 | SLOGE("lseek failed (%s)", strerror(errno)); |
| 663 | close(fd); |
| 664 | return -1; |
| 665 | } |
| 666 | if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) { |
| 667 | SLOGE("superblock read failed (%s)", strerror(errno)); |
| 668 | close(fd); |
| 669 | return -1; |
| 670 | } |
| 671 | close(fd); |
| 672 | |
| 673 | if (mDebug) { |
| 674 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
| 675 | } |
| 676 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
| 677 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
| 678 | errno = EMEDIUMTYPE; |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) { |
| 683 | SLOGE("Only ext4 partitions are supported for resize"); |
| 684 | errno = EINVAL; |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) { |
| 689 | SLOGE("Resize of ASEC image file failed. Could not resize %s", id); |
| 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Drop down a copy of the superblock at the end of the file |
| 695 | */ |
| 696 | if (writeSuperBlock(asecFileName, &sb, numImgSectors)) |
| 697 | goto fail; |
| 698 | |
| 699 | char idHash[33]; |
| 700 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 701 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 702 | goto fail; |
| 703 | } |
| 704 | |
| 705 | char loopDevice[255]; |
| 706 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 707 | goto fail; |
| 708 | |
| 709 | char dmDevice[255]; |
| 710 | |
| 711 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) { |
| 712 | Loop::destroyByDevice(loopDevice); |
| 713 | goto fail; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Wait for the device mapper node to be created. |
| 718 | */ |
| 719 | waitForDevMapper(dmDevice); |
| 720 | |
| 721 | if (Ext4::resize(dmDevice, numImgSectors)) { |
| 722 | SLOGE("Unable to resize %s (%s)", id, strerror(errno)); |
| 723 | if (cleanupDm) { |
| 724 | Devmapper::destroy(idHash); |
| 725 | } |
| 726 | Loop::destroyByDevice(loopDevice); |
| 727 | goto fail; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | fail: |
| 732 | Loop::resizeImageFile(asecFileName, oldNumSec); |
| 733 | return -1; |
| 734 | } |
| 735 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 736 | int VolumeManager::finalizeAsec(const char *id) { |
| 737 | char asecFileName[255]; |
| 738 | char loopDevice[255]; |
| 739 | char mountPoint[255]; |
| 740 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 741 | if (!isLegalAsecId(id)) { |
| 742 | SLOGE("finalizeAsec: Invalid asec id \"%s\"", id); |
| 743 | errno = EINVAL; |
| 744 | return -1; |
| 745 | } |
| 746 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 747 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 748 | SLOGE("Couldn't find ASEC %s", id); |
| 749 | return -1; |
| 750 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 751 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 752 | char idHash[33]; |
| 753 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 754 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 759 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 760 | return -1; |
| 761 | } |
| 762 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 763 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 764 | struct asec_superblock sb; |
| 765 | |
| 766 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 767 | return -1; |
| 768 | } |
| 769 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 770 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 771 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 772 | SLOGE("ASEC finalize failed: couldn't construct mountPoint"); |
| 773 | return -1; |
| 774 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 775 | |
| 776 | int result = 0; |
| 777 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 778 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 779 | } else { |
| 780 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 781 | } |
| 782 | |
| 783 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 784 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 788 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 789 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 790 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 794 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 795 | char asecFileName[255]; |
| 796 | char loopDevice[255]; |
| 797 | char mountPoint[255]; |
| 798 | |
| 799 | if (gid < AID_APP) { |
| 800 | SLOGE("Group ID is not in application range"); |
| 801 | return -1; |
| 802 | } |
| 803 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 804 | if (!isLegalAsecId(id)) { |
| 805 | SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id); |
| 806 | errno = EINVAL; |
| 807 | return -1; |
| 808 | } |
| 809 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 810 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 811 | SLOGE("Couldn't find ASEC %s", id); |
| 812 | return -1; |
| 813 | } |
| 814 | |
| 815 | char idHash[33]; |
| 816 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 817 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 818 | return -1; |
| 819 | } |
| 820 | |
| 821 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 822 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 823 | return -1; |
| 824 | } |
| 825 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 826 | unsigned long nr_sec = 0; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 827 | struct asec_superblock sb; |
| 828 | |
| 829 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 830 | return -1; |
| 831 | } |
| 832 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 833 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 834 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 835 | SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id); |
| 836 | return -1; |
| 837 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 838 | |
| 839 | int result = 0; |
| 840 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 845 | false /* read-only */, |
| 846 | true /* remount */, |
| 847 | false /* executable */); |
| 848 | if (ret) { |
| 849 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | char *paths[] = { mountPoint, NULL }; |
| 854 | |
| 855 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 856 | if (fts) { |
| 857 | // Traverse the entire hierarchy and chown to system UID. |
| 858 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 859 | // We don't care about the lost+found directory. |
| 860 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * There can only be one file marked as private right now. |
| 866 | * This should be more robust, but it satisfies the requirements |
| 867 | * we have for right now. |
| 868 | */ |
| 869 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 870 | |
| 871 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW); |
| 872 | if (fd < 0) { |
| 873 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 874 | result = -1; |
| 875 | continue; |
| 876 | } |
| 877 | |
| 878 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 879 | |
| 880 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 881 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 882 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 883 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 884 | } |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 885 | |
Stephen Smalley | 5093e61 | 2014-02-12 09:43:08 -0500 | [diff] [blame] | 886 | if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) { |
Robert Craig | b9e3ba5 | 2014-02-04 10:53:00 -0500 | [diff] [blame] | 887 | SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno)); |
| 888 | result |= -1; |
| 889 | } |
| 890 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 891 | close(fd); |
| 892 | } |
| 893 | fts_close(fts); |
| 894 | |
| 895 | // Finally make the directory readable by everyone. |
| 896 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 897 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 898 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 899 | result |= -1; |
| 900 | } |
| 901 | close(dirfd); |
| 902 | } else { |
| 903 | result |= -1; |
| 904 | } |
| 905 | |
| 906 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 907 | true /* read-only */, |
| 908 | true /* remount */, |
| 909 | true /* execute */); |
| 910 | |
| 911 | if (result) { |
| 912 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 913 | return -1; |
| 914 | } |
| 915 | |
| 916 | if (mDebug) { |
| 917 | SLOGD("ASEC %s permissions fixed", id); |
| 918 | } |
| 919 | return 0; |
| 920 | } |
| 921 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 922 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 923 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 924 | char *asecFilename2; |
| 925 | char mountPoint[255]; |
| 926 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 927 | const char *dir; |
| 928 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 929 | if (!isLegalAsecId(id1)) { |
| 930 | SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1); |
| 931 | errno = EINVAL; |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | if (!isLegalAsecId(id2)) { |
| 936 | SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2); |
| 937 | errno = EINVAL; |
| 938 | return -1; |
| 939 | } |
| 940 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 941 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 942 | SLOGE("Couldn't find ASEC %s", id1); |
| 943 | return -1; |
| 944 | } |
| 945 | |
| 946 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 947 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 948 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
| 949 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 950 | SLOGE("Rename failed: couldn't construct mountpoint"); |
| 951 | goto out_err; |
| 952 | } |
| 953 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 954 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 955 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 956 | errno = EBUSY; |
| 957 | goto out_err; |
| 958 | } |
| 959 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 960 | written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 961 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 962 | SLOGE("Rename failed: couldn't construct mountpoint2"); |
| 963 | goto out_err; |
| 964 | } |
| 965 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 966 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 967 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 968 | errno = EBUSY; |
| 969 | goto out_err; |
| 970 | } |
| 971 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 972 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 973 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 974 | errno = EADDRINUSE; |
| 975 | goto out_err; |
| 976 | } |
| 977 | |
| 978 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 979 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 980 | goto out_err; |
| 981 | } |
| 982 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 983 | free(asecFilename2); |
| 984 | return 0; |
| 985 | |
| 986 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 987 | free(asecFilename2); |
| 988 | return -1; |
| 989 | } |
| 990 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 991 | #define UNMOUNT_RETRIES 5 |
| 992 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 993 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 994 | char asecFileName[255]; |
| 995 | char mountPoint[255]; |
| 996 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 997 | if (!isLegalAsecId(id)) { |
| 998 | SLOGE("unmountAsec: Invalid asec id \"%s\"", id); |
| 999 | errno = EINVAL; |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1003 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1004 | SLOGE("Couldn't find ASEC %s", id); |
| 1005 | return -1; |
| 1006 | } |
| 1007 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1008 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1009 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1010 | SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id); |
| 1011 | return -1; |
| 1012 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1013 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1014 | char idHash[33]; |
| 1015 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1016 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1017 | return -1; |
| 1018 | } |
| 1019 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1020 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 1021 | } |
| 1022 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1023 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1024 | char mountPoint[255]; |
| 1025 | |
| 1026 | char idHash[33]; |
| 1027 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 1028 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1032 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 1033 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1034 | SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName); |
| 1035 | return -1; |
| 1036 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1037 | |
| 1038 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 1039 | } |
| 1040 | |
| 1041 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 1042 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1043 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1044 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 1045 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 1048 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1049 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1050 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1051 | rc = umount(mountPoint); |
| 1052 | if (!rc) { |
| 1053 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1054 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1055 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1056 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1057 | rc = 0; |
| 1058 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1059 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1060 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1061 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1062 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1063 | int action = 0; // default is to just complain |
| 1064 | |
| 1065 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1066 | if (i > (UNMOUNT_RETRIES - 2)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1067 | action = 2; // SIGKILL |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1068 | else if (i > (UNMOUNT_RETRIES - 3)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1069 | action = 1; // SIGHUP |
| 1070 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 1071 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 1072 | Process::killProcessesWithOpenFiles(mountPoint, action); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1073 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1077 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1078 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1079 | return -1; |
| 1080 | } |
| 1081 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1082 | int retries = 10; |
| 1083 | |
| 1084 | while(retries--) { |
| 1085 | if (!rmdir(mountPoint)) { |
| 1086 | break; |
| 1087 | } |
| 1088 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1089 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1090 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1094 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 1095 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1096 | |
Paul Lawrence | 60dec16 | 2014-09-02 10:52:15 -0700 | [diff] [blame] | 1097 | for (i=1; i <= UNMOUNT_RETRIES; i++) { |
| 1098 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
| 1099 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
| 1100 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
| 1101 | continue; |
| 1102 | } else { |
| 1103 | break; |
| 1104 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1108 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1109 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1110 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1111 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1112 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1113 | |
| 1114 | AsecIdCollection::iterator it; |
| 1115 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1116 | ContainerData* cd = *it; |
| 1117 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1118 | free(*it); |
| 1119 | mActiveContainers->erase(it); |
| 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1124 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1125 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
| 1128 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1129 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1130 | char asecFileName[255]; |
| 1131 | char mountPoint[255]; |
| 1132 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1133 | if (!isLegalAsecId(id)) { |
| 1134 | SLOGE("destroyAsec: Invalid asec id \"%s\"", id); |
| 1135 | errno = EINVAL; |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1139 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1140 | SLOGE("Couldn't find ASEC %s", id); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1144 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1145 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
| 1146 | SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id); |
| 1147 | return -1; |
| 1148 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1149 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1150 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1151 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1152 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1153 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1154 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1155 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1156 | return -1; |
| 1157 | } |
| 1158 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1159 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1160 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1161 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 1162 | return -1; |
| 1163 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1164 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1165 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1166 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1167 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1171 | /* |
| 1172 | * Legal ASEC ids consist of alphanumeric characters, '-', |
| 1173 | * '_', or '.'. ".." is not allowed. The first or last character |
| 1174 | * of the ASEC id cannot be '.' (dot). |
| 1175 | */ |
| 1176 | bool VolumeManager::isLegalAsecId(const char *id) const { |
| 1177 | size_t i; |
| 1178 | size_t len = strlen(id); |
| 1179 | |
| 1180 | if (len == 0) { |
| 1181 | return false; |
| 1182 | } |
| 1183 | if ((id[0] == '.') || (id[len - 1] == '.')) { |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | for (i = 0; i < len; i++) { |
| 1188 | if (id[i] == '.') { |
| 1189 | // i=0 is guaranteed never to have a dot. See above. |
| 1190 | if (id[i-1] == '.') return false; |
| 1191 | continue; |
| 1192 | } |
| 1193 | if (id[i] == '_' || id[i] == '-') continue; |
| 1194 | if (id[i] >= 'a' && id[i] <= 'z') continue; |
| 1195 | if (id[i] >= 'A' && id[i] <= 'Z') continue; |
| 1196 | if (id[i] >= '0' && id[i] <= '9') continue; |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1203 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
| 1204 | int dirfd = open(dir, O_DIRECTORY); |
| 1205 | if (dirfd < 0) { |
| 1206 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1207 | return false; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Nick Kralevich | 25e581a | 2015-02-06 08:55:08 -0800 | [diff] [blame] | 1210 | struct stat sb; |
| 1211 | bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0) |
| 1212 | && S_ISREG(sb.st_mode); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1213 | |
| 1214 | close(dirfd); |
| 1215 | |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 1220 | const char **directory) const { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1221 | char *asecName; |
| 1222 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1223 | if (!isLegalAsecId(id)) { |
| 1224 | SLOGE("findAsec: Invalid asec id \"%s\"", id); |
| 1225 | errno = EINVAL; |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1229 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 1230 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 1231 | return -1; |
| 1232 | } |
| 1233 | |
| 1234 | const char *dir; |
| 1235 | if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) { |
| 1236 | dir = Volume::SEC_ASECDIR_INT; |
| 1237 | } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) { |
| 1238 | dir = Volume::SEC_ASECDIR_EXT; |
| 1239 | } else { |
| 1240 | free(asecName); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | if (directory != NULL) { |
| 1245 | *directory = dir; |
| 1246 | } |
| 1247 | |
| 1248 | if (asecPath != NULL) { |
| 1249 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1250 | if ((written < 0) || (size_t(written) >= asecPathLen)) { |
| 1251 | SLOGE("findAsec failed for %s: couldn't construct ASEC path", id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1252 | free(asecName); |
| 1253 | return -1; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | free(asecName); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1261 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1262 | char asecFileName[255]; |
| 1263 | char mountPoint[255]; |
| 1264 | |
Nick Kralevich | 0de7c61 | 2014-01-27 14:58:06 -0800 | [diff] [blame] | 1265 | if (!isLegalAsecId(id)) { |
| 1266 | SLOGE("mountAsec: Invalid asec id \"%s\"", id); |
| 1267 | errno = EINVAL; |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1271 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 1272 | SLOGE("Couldn't find ASEC %s", id); |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1276 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 1277 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1278 | SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1279 | return -1; |
| 1280 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1283 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1284 | errno = EBUSY; |
| 1285 | return -1; |
| 1286 | } |
| 1287 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1288 | char idHash[33]; |
| 1289 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1290 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1291 | return -1; |
| 1292 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 1293 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1294 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1295 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug)) |
| 1296 | return -1; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1297 | |
| 1298 | char dmDevice[255]; |
| 1299 | bool cleanupDm = false; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1300 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1301 | unsigned long nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1302 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1303 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1304 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 1305 | return -1; |
| 1306 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1307 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1308 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1309 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1310 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1311 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1312 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 1313 | Loop::destroyByDevice(loopDevice); |
| 1314 | errno = EMEDIUMTYPE; |
| 1315 | return -1; |
| 1316 | } |
| 1317 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 1318 | |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1319 | if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) { |
| 1320 | Loop::destroyByDevice(loopDevice); |
| 1321 | return -1; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1324 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1325 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1326 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1327 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1328 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1329 | } |
| 1330 | Loop::destroyByDevice(loopDevice); |
| 1331 | return -1; |
| 1332 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1335 | /* |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1336 | * Wait for the device mapper node to be created. |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1337 | */ |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1338 | waitForDevMapper(dmDevice); |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 1339 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1340 | int result; |
| 1341 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1342 | result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1343 | } else { |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 1344 | result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1348 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1349 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1350 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 1351 | } |
| 1352 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1353 | return -1; |
| 1354 | } |
| 1355 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1356 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1357 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1358 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 1359 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1363 | Volume* VolumeManager::getVolumeForFile(const char *fileName) { |
| 1364 | VolumeCollection::iterator i; |
| 1365 | |
| 1366 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 1367 | const char* mountPoint = (*i)->getFuseMountpoint(); |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1368 | if (!strncmp(fileName, mountPoint, strlen(mountPoint))) { |
| 1369 | return *i; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1376 | /** |
| 1377 | * Mounts an image file <code>img</code>. |
| 1378 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1379 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1380 | char mountPoint[255]; |
| 1381 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1382 | char idHash[33]; |
| 1383 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1384 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1388 | int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 1389 | if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 1390 | SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img); |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1391 | return -1; |
| 1392 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1393 | |
| 1394 | if (isMountpointMounted(mountPoint)) { |
| 1395 | SLOGE("Image %s already mounted", img); |
| 1396 | errno = EBUSY; |
| 1397 | return -1; |
| 1398 | } |
| 1399 | |
| 1400 | char loopDevice[255]; |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1401 | if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug)) |
| 1402 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1403 | |
| 1404 | char dmDevice[255]; |
| 1405 | bool cleanupDm = false; |
| 1406 | int fd; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1407 | unsigned long nr_sec = 0; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1408 | |
| 1409 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 1410 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1411 | Loop::destroyByDevice(loopDevice); |
| 1412 | return -1; |
| 1413 | } |
| 1414 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1415 | get_blkdev_size(fd, &nr_sec); |
| 1416 | if (nr_sec == 0) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1417 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1418 | Loop::destroyByDevice(loopDevice); |
| 1419 | close(fd); |
| 1420 | return -1; |
| 1421 | } |
| 1422 | |
| 1423 | close(fd); |
| 1424 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1425 | if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) { |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 1426 | Loop::destroyByDevice(loopDevice); |
| 1427 | return -1; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | if (mkdir(mountPoint, 0755)) { |
| 1431 | if (errno != EEXIST) { |
| 1432 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1433 | if (cleanupDm) { |
| 1434 | Devmapper::destroy(idHash); |
| 1435 | } |
| 1436 | Loop::destroyByDevice(loopDevice); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | } |
| 1440 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1441 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1442 | 0227, false)) { |
| 1443 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1444 | if (cleanupDm) { |
| 1445 | Devmapper::destroy(idHash); |
| 1446 | } |
| 1447 | Loop::destroyByDevice(loopDevice); |
| 1448 | return -1; |
| 1449 | } |
| 1450 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1451 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1452 | if (mDebug) { |
| 1453 | SLOGD("Image %s mounted", img); |
| 1454 | } |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1458 | int VolumeManager::mountVolume(const char *label) { |
| 1459 | Volume *v = lookupVolume(label); |
| 1460 | |
| 1461 | if (!v) { |
| 1462 | errno = ENOENT; |
| 1463 | return -1; |
| 1464 | } |
| 1465 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1466 | return v->mountVol(); |
| 1467 | } |
| 1468 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1469 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1470 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1471 | if (fp == NULL) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1472 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | // Create a string to compare against that has a trailing slash |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1477 | int loopDirLen = strlen(Volume::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1478 | char loopDir[loopDirLen + 2]; |
| 1479 | strcpy(loopDir, Volume::LOOPDIR); |
| 1480 | loopDir[loopDirLen++] = '/'; |
| 1481 | loopDir[loopDirLen] = '\0'; |
| 1482 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1483 | mntent* mentry; |
| 1484 | while ((mentry = getmntent(fp)) != NULL) { |
| 1485 | if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) { |
| 1486 | int fd = open(mentry->mnt_fsname, O_RDONLY); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1487 | if (fd >= 0) { |
| 1488 | struct loop_info64 li; |
| 1489 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1490 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1491 | (const char*) li.lo_file_name, false); |
| 1492 | } |
| 1493 | close(fd); |
| 1494 | } |
| 1495 | } |
| 1496 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1497 | endmntent(fp); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1501 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 1502 | Volume *v = lookupVolume(label); |
| 1503 | |
| 1504 | if (!v) { |
| 1505 | errno = ENOENT; |
| 1506 | return -1; |
| 1507 | } |
| 1508 | |
| 1509 | if (strcmp(method, "ums")) { |
| 1510 | errno = ENOSYS; |
| 1511 | return -1; |
| 1512 | } |
| 1513 | |
| 1514 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1515 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 1516 | } else { |
| 1517 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1518 | } |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1522 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1523 | Volume *v = lookupVolume(label); |
| 1524 | |
| 1525 | if (!v) { |
| 1526 | errno = ENOENT; |
| 1527 | return -1; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Eventually, we'll want to support additional share back-ends, |
| 1532 | * some of which may work while the media is mounted. For now, |
| 1533 | * we just support UMS |
| 1534 | */ |
| 1535 | if (strcmp(method, "ums")) { |
| 1536 | errno = ENOSYS; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | if (v->getState() == Volume::State_NoMedia) { |
| 1541 | errno = ENODEV; |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1545 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1546 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1547 | errno = EBUSY; |
| 1548 | return -1; |
| 1549 | } |
| 1550 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1551 | if (mVolManagerDisabled) { |
| 1552 | errno = EBUSY; |
| 1553 | return -1; |
| 1554 | } |
| 1555 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 1556 | dev_t d = v->getShareDevice(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1557 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1558 | // This volume does not support raw disk access |
| 1559 | errno = EINVAL; |
| 1560 | return -1; |
| 1561 | } |
| 1562 | |
| 1563 | int fd; |
| 1564 | char nodepath[255]; |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1565 | int written = snprintf(nodepath, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1566 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 1567 | major(d), minor(d)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1568 | |
rpcraig | d1c226f | 2012-10-09 06:58:16 -0400 | [diff] [blame] | 1569 | if ((written < 0) || (size_t(written) >= sizeof(nodepath))) { |
| 1570 | SLOGE("shareVolume failed: couldn't construct nodepath"); |
| 1571 | return -1; |
| 1572 | } |
| 1573 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1574 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1575 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1576 | return -1; |
| 1577 | } |
| 1578 | |
| 1579 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1580 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1581 | close(fd); |
| 1582 | return -1; |
| 1583 | } |
| 1584 | |
| 1585 | close(fd); |
| 1586 | v->handleVolumeShared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1587 | if (mUmsSharingCount++ == 0) { |
| 1588 | FILE* fp; |
| 1589 | mSavedDirtyRatio = -1; // in case we fail |
| 1590 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1591 | char line[16]; |
| 1592 | if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) { |
| 1593 | fprintf(fp, "%d\n", mUmsDirtyRatio); |
| 1594 | } else { |
| 1595 | SLOGE("Failed to read dirty_ratio (%s)", strerror(errno)); |
| 1596 | } |
| 1597 | fclose(fp); |
| 1598 | } else { |
| 1599 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1600 | } |
| 1601 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1606 | Volume *v = lookupVolume(label); |
| 1607 | |
| 1608 | if (!v) { |
| 1609 | errno = ENOENT; |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
| 1613 | if (strcmp(method, "ums")) { |
| 1614 | errno = ENOSYS; |
| 1615 | return -1; |
| 1616 | } |
| 1617 | |
| 1618 | if (v->getState() != Volume::State_Shared) { |
| 1619 | errno = EINVAL; |
| 1620 | return -1; |
| 1621 | } |
| 1622 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1623 | int fd; |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1624 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1625 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1626 | return -1; |
| 1627 | } |
| 1628 | |
| 1629 | char ch = 0; |
| 1630 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1631 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1632 | close(fd); |
| 1633 | return -1; |
| 1634 | } |
| 1635 | |
| 1636 | close(fd); |
| 1637 | v->handleVolumeUnshared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1638 | if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) { |
| 1639 | FILE* fp; |
| 1640 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1641 | fprintf(fp, "%d\n", mSavedDirtyRatio); |
| 1642 | fclose(fp); |
| 1643 | } else { |
| 1644 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1645 | } |
| 1646 | mSavedDirtyRatio = -1; |
| 1647 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1648 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1651 | extern "C" int vold_disableVol(const char *label) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1652 | VolumeManager *vm = VolumeManager::Instance(); |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1653 | vm->disableVolumeManager(); |
| 1654 | vm->unshareVolume(label, "ums"); |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1655 | return vm->unmountVolume(label, true, false); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | extern "C" int vold_getNumDirectVolumes(void) { |
| 1659 | VolumeManager *vm = VolumeManager::Instance(); |
| 1660 | return vm->getNumDirectVolumes(); |
| 1661 | } |
| 1662 | |
| 1663 | int VolumeManager::getNumDirectVolumes(void) { |
| 1664 | VolumeCollection::iterator i; |
| 1665 | int n=0; |
| 1666 | |
| 1667 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1668 | if ((*i)->getShareDevice() != (dev_t)0) { |
| 1669 | n++; |
| 1670 | } |
| 1671 | } |
| 1672 | return n; |
| 1673 | } |
| 1674 | |
| 1675 | extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) { |
| 1676 | VolumeManager *vm = VolumeManager::Instance(); |
| 1677 | return vm->getDirectVolumeList(vol_list); |
| 1678 | } |
| 1679 | |
| 1680 | int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) { |
| 1681 | VolumeCollection::iterator i; |
| 1682 | int n=0; |
| 1683 | dev_t d; |
| 1684 | |
| 1685 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1686 | if ((d=(*i)->getShareDevice()) != (dev_t)0) { |
| 1687 | (*i)->getVolInfo(&vol_list[n]); |
| 1688 | snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev), |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 1689 | "/dev/block/vold/%d:%d", major(d), minor(d)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1690 | n++; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1697 | int VolumeManager::unmountVolume(const char *label, bool force, bool revert) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1698 | Volume *v = lookupVolume(label); |
| 1699 | |
| 1700 | if (!v) { |
| 1701 | errno = ENOENT; |
| 1702 | return -1; |
| 1703 | } |
| 1704 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1705 | if (v->getState() == Volume::State_NoMedia) { |
| 1706 | errno = ENODEV; |
| 1707 | return -1; |
| 1708 | } |
| 1709 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1710 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1711 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1712 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1713 | errno = EBUSY; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1714 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1717 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1718 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1719 | return v->unmountVol(force, revert); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1722 | extern "C" int vold_unmountAllAsecs(void) { |
| 1723 | int rc; |
| 1724 | |
| 1725 | VolumeManager *vm = VolumeManager::Instance(); |
| 1726 | rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT); |
| 1727 | if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) { |
| 1728 | rc = -1; |
| 1729 | } |
| 1730 | return rc; |
| 1731 | } |
| 1732 | |
| 1733 | #define ID_BUF_LEN 256 |
| 1734 | #define ASEC_SUFFIX ".asec" |
| 1735 | #define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1) |
| 1736 | int VolumeManager::unmountAllAsecsInDir(const char *directory) { |
| 1737 | DIR *d = opendir(directory); |
| 1738 | int rc = 0; |
| 1739 | |
| 1740 | if (!d) { |
| 1741 | SLOGE("Could not open asec dir %s", directory); |
| 1742 | return -1; |
| 1743 | } |
| 1744 | |
| 1745 | size_t dirent_len = offsetof(struct dirent, d_name) + |
Elliott Hughes | 8c480f7 | 2012-10-26 16:57:19 -0700 | [diff] [blame] | 1746 | fpathconf(dirfd(d), _PC_NAME_MAX) + 1; |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1747 | |
| 1748 | struct dirent *dent = (struct dirent *) malloc(dirent_len); |
| 1749 | if (dent == NULL) { |
| 1750 | SLOGE("Failed to allocate memory for asec dir"); |
| 1751 | return -1; |
| 1752 | } |
| 1753 | |
| 1754 | struct dirent *result; |
| 1755 | while (!readdir_r(d, dent, &result) && result != NULL) { |
| 1756 | if (dent->d_name[0] == '.') |
| 1757 | continue; |
| 1758 | if (dent->d_type != DT_REG) |
| 1759 | continue; |
| 1760 | size_t name_len = strlen(dent->d_name); |
| 1761 | if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) && |
| 1762 | !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) { |
| 1763 | char id[ID_BUF_LEN]; |
| 1764 | strlcpy(id, dent->d_name, name_len - 4); |
| 1765 | if (unmountAsec(id, true)) { |
| 1766 | /* Register the error, but try to unmount more asecs */ |
| 1767 | rc = -1; |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | closedir(d); |
| 1772 | |
| 1773 | free(dent); |
| 1774 | |
| 1775 | return rc; |
| 1776 | } |
| 1777 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1778 | /* |
| 1779 | * Looks up a volume by it's label or mount-point |
| 1780 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1781 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1782 | VolumeCollection::iterator i; |
| 1783 | |
| 1784 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1785 | if (label[0] == '/') { |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 1786 | if (!strcmp(label, (*i)->getFuseMountpoint())) |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1787 | return (*i); |
| 1788 | } else { |
| 1789 | if (!strcmp(label, (*i)->getLabel())) |
| 1790 | return (*i); |
| 1791 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1792 | } |
| 1793 | return NULL; |
| 1794 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1795 | |
| 1796 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1797 | { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1798 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 1799 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1800 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
| 1803 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1804 | bool found_mp = false; |
| 1805 | mntent* mentry; |
| 1806 | while ((mentry = getmntent(fp)) != NULL) { |
| 1807 | if (strcmp(mentry->mnt_dir, mp) == 0) { |
| 1808 | found_mp = true; |
| 1809 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1810 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1811 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 1812 | endmntent(fp); |
| 1813 | return found_mp; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1816 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1817 | int rc = 0; |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1818 | |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1819 | char asecFileName[255]; |
| 1820 | |
| 1821 | AsecIdCollection removeAsec; |
| 1822 | AsecIdCollection removeObb; |
| 1823 | |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1824 | for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end(); |
| 1825 | ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1826 | ContainerData* cd = *it; |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1827 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1828 | if (cd->type == ASEC) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1829 | if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) { |
| 1830 | SLOGE("Couldn't find ASEC %s; cleaning up", cd->id); |
| 1831 | removeAsec.push_back(cd); |
| 1832 | } else { |
| 1833 | SLOGD("Found ASEC at path %s", asecFileName); |
| 1834 | if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT, |
| 1835 | strlen(Volume::SEC_ASECDIR_EXT))) { |
| 1836 | removeAsec.push_back(cd); |
| 1837 | } |
| 1838 | } |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1839 | } else if (cd->type == OBB) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1840 | if (v == getVolumeForFile(cd->id)) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1841 | removeObb.push_back(cd); |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1842 | } |
| 1843 | } else { |
| 1844 | SLOGE("Unknown container type %d!", cd->type); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1845 | } |
| 1846 | } |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1847 | |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1848 | for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1849 | ContainerData *cd = *it; |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 1850 | SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel()); |
| 1851 | if (unmountAsec(cd->id, force)) { |
| 1852 | SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno)); |
| 1853 | rc = -1; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) { |
| 1858 | ContainerData *cd = *it; |
| 1859 | SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel()); |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1860 | if (unmountObb(cd->id, force)) { |
| 1861 | SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno)); |
| 1862 | rc = -1; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | return rc; |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1869 | int VolumeManager::mkdirs(char* path) { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1870 | // Require that path lives under a volume we manage and is mounted |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1871 | const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE"); |
| 1872 | const char* root = NULL; |
Marco Nelissen | 5ab02e7 | 2013-10-15 15:22:28 -0700 | [diff] [blame] | 1873 | if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) { |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1874 | root = emulated_source; |
| 1875 | } else { |
| 1876 | Volume* vol = getVolumeForFile(path); |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1877 | if (vol && vol->getState() == Volume::State_Mounted) { |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1878 | root = vol->getMountpoint(); |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | if (!root) { |
Cylen Yao | 27cfee3 | 2014-05-02 19:23:42 +0800 | [diff] [blame] | 1883 | SLOGE("Failed to find mounted volume for %s", path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 1884 | return -EINVAL; |
| 1885 | } |
| 1886 | |
| 1887 | /* fs_mkdirs() does symlink checking and relative path enforcement */ |
| 1888 | return fs_mkdirs(path, 0700); |
| 1889 | } |