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