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