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