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