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