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