San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 22 | #include <fts.h> |
| 23 | #include <unistd.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mount.h> |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 28 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 29 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 30 | |
| 31 | #define LOG_TAG "Vold" |
| 32 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 33 | #include <openssl/md5.h> |
| 34 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | #include <cutils/log.h> |
| 36 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 37 | #include <sysutils/NetlinkEvent.h> |
| 38 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
| 40 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 42 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 43 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 44 | #include "Loop.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 45 | #include "Ext4.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 46 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 47 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 48 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 49 | #include "Asec.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 50 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 51 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 52 | #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" |
| 53 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 54 | VolumeManager *VolumeManager::sInstance = NULL; |
| 55 | |
| 56 | VolumeManager *VolumeManager::Instance() { |
| 57 | if (!sInstance) |
| 58 | sInstance = new VolumeManager(); |
| 59 | return sInstance; |
| 60 | } |
| 61 | |
| 62 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 63 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 64 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 65 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 66 | mBroadcaster = NULL; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 67 | mUmsSharingCount = 0; |
| 68 | mSavedDirtyRatio = -1; |
| 69 | // set dirty ratio to 0 when UMS is active |
| 70 | mUmsDirtyRatio = 0; |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 71 | mVolManagerDisabled = 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 75 | delete mVolumes; |
| 76 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 79 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 80 | static const char* digits = "0123456789abcdef"; |
| 81 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 82 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 83 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 84 | if (buffer == NULL) { |
| 85 | SLOGE("Destination buffer is NULL"); |
| 86 | errno = ESPIPE; |
| 87 | return NULL; |
| 88 | } else if (id == NULL) { |
| 89 | SLOGE("Source buffer is NULL"); |
| 90 | errno = ESPIPE; |
| 91 | return NULL; |
| 92 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
| 93 | SLOGE("Target hash buffer size < %d bytes (%d)", |
| 94 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 95 | errno = ESPIPE; |
| 96 | return NULL; |
| 97 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 98 | |
| 99 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 100 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 101 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 102 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 103 | *p++ = digits[sig[i] >> 4]; |
| 104 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 105 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 106 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 107 | |
| 108 | return buffer; |
| 109 | } |
| 110 | |
| 111 | void VolumeManager::setDebug(bool enable) { |
| 112 | mDebug = enable; |
| 113 | VolumeCollection::iterator it; |
| 114 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 115 | (*it)->setDebug(enable); |
| 116 | } |
| 117 | } |
| 118 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 119 | int VolumeManager::start() { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int VolumeManager::stop() { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int VolumeManager::addVolume(Volume *v) { |
| 128 | mVolumes->push_back(v); |
| 129 | return 0; |
| 130 | } |
| 131 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 132 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 133 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 134 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 135 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 136 | VolumeCollection::iterator it; |
| 137 | bool hit = false; |
| 138 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 139 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 140 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 141 | SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 142 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 143 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 149 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 150 | SLOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 151 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 155 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 156 | VolumeCollection::iterator i; |
| 157 | |
| 158 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 159 | char *buffer; |
| 160 | asprintf(&buffer, "%s %s %d", |
| 161 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 162 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 163 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 164 | free(buffer); |
| 165 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 166 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 169 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 170 | int VolumeManager::formatVolume(const char *label) { |
| 171 | Volume *v = lookupVolume(label); |
| 172 | |
| 173 | if (!v) { |
| 174 | errno = ENOENT; |
| 175 | return -1; |
| 176 | } |
| 177 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 178 | if (mVolManagerDisabled) { |
| 179 | errno = EBUSY; |
| 180 | return -1; |
| 181 | } |
| 182 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 183 | return v->formatVol(); |
| 184 | } |
| 185 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 186 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 187 | char idHash[33]; |
| 188 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 189 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | memset(mountPath, 0, mountPathLen); |
| 194 | snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 195 | |
| 196 | if (access(mountPath, F_OK)) { |
| 197 | errno = ENOENT; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 204 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 205 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 206 | |
| 207 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 208 | SLOGE("Couldn't find ASEC %s", id); |
| 209 | return -1; |
| 210 | } |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 211 | |
| 212 | memset(buffer, 0, maxlen); |
| 213 | if (access(asecFileName, F_OK)) { |
| 214 | errno = ENOENT; |
| 215 | return -1; |
| 216 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 217 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 218 | snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 222 | int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) { |
| 223 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 224 | |
| 225 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 226 | SLOGE("Couldn't find ASEC %s", id); |
| 227 | return -1; |
| 228 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 229 | |
| 230 | memset(buffer, 0, maxlen); |
| 231 | if (access(asecFileName, F_OK)) { |
| 232 | errno = ENOENT; |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | snprintf(buffer, maxlen, "%s", asecFileName); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 240 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype, |
| 241 | const char *key, const int ownerUid, bool isExternal) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 242 | struct asec_superblock sb; |
| 243 | memset(&sb, 0, sizeof(sb)); |
| 244 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 245 | const bool wantFilesystem = strcmp(fstype, "none"); |
| 246 | bool usingExt4 = false; |
| 247 | if (wantFilesystem) { |
| 248 | usingExt4 = !strcmp(fstype, "ext4"); |
| 249 | if (usingExt4) { |
| 250 | sb.c_opts |= ASEC_SB_C_OPTS_EXT4; |
| 251 | } else if (strcmp(fstype, "fat")) { |
| 252 | SLOGE("Invalid filesystem type %s", fstype); |
| 253 | errno = EINVAL; |
| 254 | return -1; |
| 255 | } |
| 256 | } |
| 257 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 258 | sb.magic = ASEC_SB_MAGIC; |
| 259 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 260 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 261 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 262 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 263 | errno = EINVAL; |
| 264 | return -1; |
| 265 | } |
| 266 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 267 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 268 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 269 | errno = EADDRINUSE; |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | char asecFileName[255]; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 274 | |
| 275 | if (!findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 276 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
| 277 | asecFileName, strerror(errno)); |
| 278 | errno = EADDRINUSE; |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT; |
| 283 | |
| 284 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 285 | |
| 286 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 287 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 288 | asecFileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 289 | errno = EADDRINUSE; |
| 290 | return -1; |
| 291 | } |
| 292 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 293 | /* |
| 294 | * Add some headroom |
| 295 | */ |
| 296 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 297 | unsigned numImgSectors = numSectors + fatSize + 2; |
| 298 | |
| 299 | if (numImgSectors % 63) { |
| 300 | numImgSectors += (63 - (numImgSectors % 63)); |
| 301 | } |
| 302 | |
| 303 | // Add +1 for our superblock which is at the end |
| 304 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 305 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 309 | char idHash[33]; |
| 310 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 311 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 312 | unlink(asecFileName); |
| 313 | return -1; |
| 314 | } |
| 315 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 316 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 317 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 318 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 319 | unlink(asecFileName); |
| 320 | return -1; |
| 321 | } |
| 322 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 323 | char dmDevice[255]; |
| 324 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 325 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 326 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 327 | // XXX: This is all we support for now |
| 328 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 329 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 330 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 331 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 332 | Loop::destroyByDevice(loopDevice); |
| 333 | unlink(asecFileName); |
| 334 | return -1; |
| 335 | } |
| 336 | cleanupDm = true; |
| 337 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 338 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 339 | strcpy(dmDevice, loopDevice); |
| 340 | } |
| 341 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 342 | /* |
| 343 | * Drop down the superblock at the end of the file |
| 344 | */ |
| 345 | |
| 346 | int sbfd = open(loopDevice, O_RDWR); |
| 347 | if (sbfd < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 348 | SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 349 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 350 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 351 | } |
| 352 | Loop::destroyByDevice(loopDevice); |
| 353 | unlink(asecFileName); |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 358 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 359 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 360 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 361 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 362 | } |
| 363 | Loop::destroyByDevice(loopDevice); |
| 364 | unlink(asecFileName); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 369 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 370 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 371 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 372 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 373 | } |
| 374 | Loop::destroyByDevice(loopDevice); |
| 375 | unlink(asecFileName); |
| 376 | return -1; |
| 377 | } |
| 378 | close(sbfd); |
| 379 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 380 | if (wantFilesystem) { |
| 381 | int formatStatus; |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 382 | char mountPoint[255]; |
| 383 | |
| 384 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 385 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 386 | if (usingExt4) { |
rpcraig | a54e13a | 2012-09-21 14:17:08 -0400 | [diff] [blame] | 387 | formatStatus = Ext4::format(dmDevice, mountPoint); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 388 | } else { |
| 389 | formatStatus = Fat::format(dmDevice, numImgSectors); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 390 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 391 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 392 | if (formatStatus < 0) { |
| 393 | SLOGE("ASEC fs format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 394 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 395 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 396 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 397 | Loop::destroyByDevice(loopDevice); |
| 398 | unlink(asecFileName); |
| 399 | return -1; |
| 400 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 401 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 402 | if (mkdir(mountPoint, 0000)) { |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 403 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 404 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 405 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 406 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 407 | } |
| 408 | Loop::destroyByDevice(loopDevice); |
| 409 | unlink(asecFileName); |
| 410 | return -1; |
| 411 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 412 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 413 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 414 | int mountStatus; |
| 415 | if (usingExt4) { |
| 416 | mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false); |
| 417 | } else { |
| 418 | mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000, |
| 419 | false); |
| 420 | } |
| 421 | |
| 422 | if (mountStatus) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 423 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 424 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 425 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 426 | } |
| 427 | Loop::destroyByDevice(loopDevice); |
| 428 | unlink(asecFileName); |
| 429 | return -1; |
| 430 | } |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 431 | |
| 432 | if (usingExt4) { |
| 433 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 434 | if (dirfd >= 0) { |
| 435 | if (fchown(dirfd, ownerUid, AID_SYSTEM) |
| 436 | || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) { |
| 437 | SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint); |
| 438 | } |
| 439 | close(dirfd); |
| 440 | } |
| 441 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 442 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 443 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 444 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 445 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 446 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int VolumeManager::finalizeAsec(const char *id) { |
| 451 | char asecFileName[255]; |
| 452 | char loopDevice[255]; |
| 453 | char mountPoint[255]; |
| 454 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 455 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 456 | SLOGE("Couldn't find ASEC %s", id); |
| 457 | return -1; |
| 458 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 459 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 460 | char idHash[33]; |
| 461 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 462 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 467 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 468 | return -1; |
| 469 | } |
| 470 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 471 | unsigned int nr_sec = 0; |
| 472 | struct asec_superblock sb; |
| 473 | |
| 474 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 475 | return -1; |
| 476 | } |
| 477 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 478 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 479 | |
| 480 | int result = 0; |
| 481 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 482 | result = Ext4::doMount(loopDevice, mountPoint, true, true, true); |
| 483 | } else { |
| 484 | result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false); |
| 485 | } |
| 486 | |
| 487 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 488 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 489 | return -1; |
| 490 | } |
| 491 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 492 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 493 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 494 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 498 | int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) { |
| 499 | char asecFileName[255]; |
| 500 | char loopDevice[255]; |
| 501 | char mountPoint[255]; |
| 502 | |
| 503 | if (gid < AID_APP) { |
| 504 | SLOGE("Group ID is not in application range"); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 509 | SLOGE("Couldn't find ASEC %s", id); |
| 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | char idHash[33]; |
| 514 | if (!asecHash(id, idHash, sizeof(idHash))) { |
| 515 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 520 | SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno)); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | unsigned int nr_sec = 0; |
| 525 | struct asec_superblock sb; |
| 526 | |
| 527 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 532 | |
| 533 | int result = 0; |
| 534 | if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) { |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int ret = Ext4::doMount(loopDevice, mountPoint, |
| 539 | false /* read-only */, |
| 540 | true /* remount */, |
| 541 | false /* executable */); |
| 542 | if (ret) { |
| 543 | SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno)); |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | char *paths[] = { mountPoint, NULL }; |
| 548 | |
| 549 | FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL); |
| 550 | if (fts) { |
| 551 | // Traverse the entire hierarchy and chown to system UID. |
| 552 | for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) { |
| 553 | // We don't care about the lost+found directory. |
| 554 | if (!strcmp(ftsent->fts_name, "lost+found")) { |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * There can only be one file marked as private right now. |
| 560 | * This should be more robust, but it satisfies the requirements |
| 561 | * we have for right now. |
| 562 | */ |
| 563 | const bool privateFile = !strcmp(ftsent->fts_name, filename); |
| 564 | |
| 565 | int fd = open(ftsent->fts_accpath, O_NOFOLLOW); |
| 566 | if (fd < 0) { |
| 567 | SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno)); |
| 568 | result = -1; |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM); |
| 573 | |
| 574 | if (ftsent->fts_info & FTS_D) { |
Kenny Root | 1a673c8 | 2012-05-10 16:45:29 -0700 | [diff] [blame] | 575 | result |= fchmod(fd, 0755); |
Kenny Root | 348c8ab | 2012-05-10 15:39:53 -0700 | [diff] [blame] | 576 | } else if (ftsent->fts_info & FTS_F) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 577 | result |= fchmod(fd, privateFile ? 0640 : 0644); |
| 578 | } |
| 579 | close(fd); |
| 580 | } |
| 581 | fts_close(fts); |
| 582 | |
| 583 | // Finally make the directory readable by everyone. |
| 584 | int dirfd = open(mountPoint, O_DIRECTORY); |
| 585 | if (dirfd < 0 || fchmod(dirfd, 0755)) { |
| 586 | SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno)); |
| 587 | result |= -1; |
| 588 | } |
| 589 | close(dirfd); |
| 590 | } else { |
| 591 | result |= -1; |
| 592 | } |
| 593 | |
| 594 | result |= Ext4::doMount(loopDevice, mountPoint, |
| 595 | true /* read-only */, |
| 596 | true /* remount */, |
| 597 | true /* execute */); |
| 598 | |
| 599 | if (result) { |
| 600 | SLOGE("ASEC fix permissions failed (%s)", strerror(errno)); |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | if (mDebug) { |
| 605 | SLOGD("ASEC %s permissions fixed", id); |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 610 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 611 | char asecFilename1[255]; |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 612 | char *asecFilename2; |
| 613 | char mountPoint[255]; |
| 614 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 615 | const char *dir; |
| 616 | |
| 617 | if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) { |
| 618 | SLOGE("Couldn't find ASEC %s", id1); |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | asprintf(&asecFilename2, "%s/%s.asec", dir, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 623 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 624 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 625 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 626 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 627 | errno = EBUSY; |
| 628 | goto out_err; |
| 629 | } |
| 630 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 631 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 632 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 633 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 634 | errno = EBUSY; |
| 635 | goto out_err; |
| 636 | } |
| 637 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 638 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 639 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 640 | errno = EADDRINUSE; |
| 641 | goto out_err; |
| 642 | } |
| 643 | |
| 644 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 645 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 646 | goto out_err; |
| 647 | } |
| 648 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 649 | free(asecFilename2); |
| 650 | return 0; |
| 651 | |
| 652 | out_err: |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 653 | free(asecFilename2); |
| 654 | return -1; |
| 655 | } |
| 656 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 657 | #define UNMOUNT_RETRIES 5 |
| 658 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 659 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 660 | char asecFileName[255]; |
| 661 | char mountPoint[255]; |
| 662 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 663 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 664 | SLOGE("Couldn't find ASEC %s", id); |
| 665 | return -1; |
| 666 | } |
| 667 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 668 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 669 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 670 | char idHash[33]; |
| 671 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 672 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 673 | return -1; |
| 674 | } |
| 675 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 676 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 677 | } |
| 678 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 679 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 680 | char mountPoint[255]; |
| 681 | |
| 682 | char idHash[33]; |
| 683 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 684 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 689 | |
| 690 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 691 | } |
| 692 | |
| 693 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 694 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 695 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 696 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 697 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 698 | return -1; |
| 699 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 700 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 701 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 702 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 703 | rc = umount(mountPoint); |
| 704 | if (!rc) { |
| 705 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 706 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 707 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 708 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 709 | rc = 0; |
| 710 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 711 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 712 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 713 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 714 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 715 | int action = 0; // default is to just complain |
| 716 | |
| 717 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 718 | if (i > (UNMOUNT_RETRIES - 2)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 719 | action = 2; // SIGKILL |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 720 | else if (i > (UNMOUNT_RETRIES - 3)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 721 | action = 1; // SIGHUP |
| 722 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 723 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 724 | Process::killProcessesWithOpenFiles(mountPoint, action); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 725 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 729 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 730 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 734 | int retries = 10; |
| 735 | |
| 736 | while(retries--) { |
| 737 | if (!rmdir(mountPoint)) { |
| 738 | break; |
| 739 | } |
| 740 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 741 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 742 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 746 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 747 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 748 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 749 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 750 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 754 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 755 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 756 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 757 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 758 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 759 | |
| 760 | AsecIdCollection::iterator it; |
| 761 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 762 | ContainerData* cd = *it; |
| 763 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 764 | free(*it); |
| 765 | mActiveContainers->erase(it); |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 770 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 771 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 775 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 776 | char asecFileName[255]; |
| 777 | char mountPoint[255]; |
| 778 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 779 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 780 | SLOGE("Couldn't find ASEC %s", id); |
| 781 | return -1; |
| 782 | } |
| 783 | |
San Mehat | 55013f7 | 2010-02-24 12:12:34 -0800 | [diff] [blame] | 784 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 785 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 786 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 787 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 788 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 789 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 790 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 791 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 792 | return -1; |
| 793 | } |
| 794 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 795 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 796 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 797 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 798 | return -1; |
| 799 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 800 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 801 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 802 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 803 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 807 | bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const { |
| 808 | int dirfd = open(dir, O_DIRECTORY); |
| 809 | if (dirfd < 0) { |
| 810 | SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno)); |
| 811 | return -1; |
| 812 | } |
| 813 | |
| 814 | bool ret = false; |
| 815 | |
| 816 | if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) { |
| 817 | ret = true; |
| 818 | } |
| 819 | |
| 820 | close(dirfd); |
| 821 | |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen, |
| 826 | const char **directory) const { |
| 827 | int dirfd, fd; |
| 828 | const int idLen = strlen(id); |
| 829 | char *asecName; |
| 830 | |
| 831 | if (asprintf(&asecName, "%s.asec", id) < 0) { |
| 832 | SLOGE("Couldn't allocate string to write ASEC name"); |
| 833 | return -1; |
| 834 | } |
| 835 | |
| 836 | const char *dir; |
| 837 | if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) { |
| 838 | dir = Volume::SEC_ASECDIR_INT; |
| 839 | } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) { |
| 840 | dir = Volume::SEC_ASECDIR_EXT; |
| 841 | } else { |
| 842 | free(asecName); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | if (directory != NULL) { |
| 847 | *directory = dir; |
| 848 | } |
| 849 | |
| 850 | if (asecPath != NULL) { |
| 851 | int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName); |
| 852 | if (written < 0 || static_cast<size_t>(written) >= asecPathLen) { |
| 853 | free(asecName); |
| 854 | return -1; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | free(asecName); |
| 859 | return 0; |
| 860 | } |
| 861 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 862 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 863 | char asecFileName[255]; |
| 864 | char mountPoint[255]; |
| 865 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 866 | if (findAsec(id, asecFileName, sizeof(asecFileName))) { |
| 867 | SLOGE("Couldn't find ASEC %s", id); |
| 868 | return -1; |
| 869 | } |
| 870 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 871 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 872 | |
| 873 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 874 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 875 | errno = EBUSY; |
| 876 | return -1; |
| 877 | } |
| 878 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 879 | char idHash[33]; |
| 880 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 881 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 882 | return -1; |
| 883 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 884 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 885 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 886 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 887 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 888 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 889 | return -1; |
| 890 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 891 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 892 | SLOGD("New loop device created at %s", loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 893 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 894 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 895 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 896 | SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 897 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | char dmDevice[255]; |
| 901 | bool cleanupDm = false; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 902 | int fd; |
| 903 | unsigned int nr_sec = 0; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 904 | struct asec_superblock sb; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 905 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 906 | if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) { |
| 907 | return -1; |
| 908 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 909 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 910 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 911 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 912 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 913 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 914 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 915 | Loop::destroyByDevice(loopDevice); |
| 916 | errno = EMEDIUMTYPE; |
| 917 | return -1; |
| 918 | } |
| 919 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 920 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 921 | if (strcmp(key, "none")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 922 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 923 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 924 | dmDevice, sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 925 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 926 | Loop::destroyByDevice(loopDevice); |
| 927 | return -1; |
| 928 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 929 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 930 | SLOGD("New devmapper instance created at %s", dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 931 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 932 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 933 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 934 | SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 935 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 936 | } |
| 937 | cleanupDm = true; |
| 938 | } else { |
| 939 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 942 | if (mkdir(mountPoint, 0000)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 943 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 944 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 945 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 946 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 947 | } |
| 948 | Loop::destroyByDevice(loopDevice); |
| 949 | return -1; |
| 950 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Kenny Root | cdc2a1c | 2012-05-03 13:49:46 -0700 | [diff] [blame] | 953 | /* |
| 954 | * The device mapper node needs to be created. Sometimes it takes a |
| 955 | * while. Wait for up to 1 second. We could also inspect incoming uevents, |
| 956 | * but that would take more effort. |
| 957 | */ |
| 958 | int tries = 25; |
| 959 | while (tries--) { |
| 960 | if (!access(dmDevice, F_OK) || errno != ENOENT) { |
| 961 | break; |
| 962 | } |
| 963 | usleep(40 * 1000); |
| 964 | } |
| 965 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 966 | int result; |
| 967 | if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) { |
| 968 | result = Ext4::doMount(dmDevice, mountPoint, true, false, true); |
| 969 | } else { |
| 970 | result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false); |
| 971 | } |
| 972 | |
| 973 | if (result) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 974 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 975 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 976 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 977 | } |
| 978 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 979 | return -1; |
| 980 | } |
| 981 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 982 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 983 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 984 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 985 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 986 | return 0; |
| 987 | } |
| 988 | |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 989 | Volume* VolumeManager::getVolumeForFile(const char *fileName) { |
| 990 | VolumeCollection::iterator i; |
| 991 | |
| 992 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 993 | const char* mountPoint = (*i)->getMountpoint(); |
| 994 | if (!strncmp(fileName, mountPoint, strlen(mountPoint))) { |
| 995 | return *i; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1002 | /** |
| 1003 | * Mounts an image file <code>img</code>. |
| 1004 | */ |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1005 | int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1006 | char mountPoint[255]; |
| 1007 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1008 | char idHash[33]; |
| 1009 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 1010 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 1011 | return -1; |
| 1012 | } |
| 1013 | |
| 1014 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 1015 | |
| 1016 | if (isMountpointMounted(mountPoint)) { |
| 1017 | SLOGE("Image %s already mounted", img); |
| 1018 | errno = EBUSY; |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | char loopDevice[255]; |
| 1023 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 1024 | if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) { |
| 1025 | SLOGE("Image loop device creation failed (%s)", strerror(errno)); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | if (mDebug) { |
| 1029 | SLOGD("New loop device created at %s", loopDevice); |
| 1030 | } |
| 1031 | } else { |
| 1032 | if (mDebug) { |
| 1033 | SLOGD("Found active loopback for %s at %s", img, loopDevice); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | char dmDevice[255]; |
| 1038 | bool cleanupDm = false; |
| 1039 | int fd; |
| 1040 | unsigned int nr_sec = 0; |
| 1041 | |
| 1042 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 1043 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 1044 | Loop::destroyByDevice(loopDevice); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 1049 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 1050 | Loop::destroyByDevice(loopDevice); |
| 1051 | close(fd); |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | close(fd); |
| 1056 | |
| 1057 | if (strcmp(key, "none")) { |
| 1058 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 1059 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
| 1060 | dmDevice, sizeof(dmDevice))) { |
| 1061 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 1062 | Loop::destroyByDevice(loopDevice); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | if (mDebug) { |
| 1066 | SLOGD("New devmapper instance created at %s", dmDevice); |
| 1067 | } |
| 1068 | } else { |
| 1069 | if (mDebug) { |
| 1070 | SLOGD("Found active devmapper for %s at %s", img, dmDevice); |
| 1071 | } |
| 1072 | } |
| 1073 | cleanupDm = true; |
| 1074 | } else { |
| 1075 | strcpy(dmDevice, loopDevice); |
| 1076 | } |
| 1077 | |
| 1078 | if (mkdir(mountPoint, 0755)) { |
| 1079 | if (errno != EEXIST) { |
| 1080 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 1081 | if (cleanupDm) { |
| 1082 | Devmapper::destroy(idHash); |
| 1083 | } |
| 1084 | Loop::destroyByDevice(loopDevice); |
| 1085 | return -1; |
| 1086 | } |
| 1087 | } |
| 1088 | |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 1089 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1090 | 0227, false)) { |
| 1091 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 1092 | if (cleanupDm) { |
| 1093 | Devmapper::destroy(idHash); |
| 1094 | } |
| 1095 | Loop::destroyByDevice(loopDevice); |
| 1096 | return -1; |
| 1097 | } |
| 1098 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1099 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 1100 | if (mDebug) { |
| 1101 | SLOGD("Image %s mounted", img); |
| 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1106 | int VolumeManager::mountVolume(const char *label) { |
| 1107 | Volume *v = lookupVolume(label); |
| 1108 | |
| 1109 | if (!v) { |
| 1110 | errno = ENOENT; |
| 1111 | return -1; |
| 1112 | } |
| 1113 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1114 | return v->mountVol(); |
| 1115 | } |
| 1116 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1117 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
| 1118 | char device[256]; |
| 1119 | char mount_path[256]; |
| 1120 | char rest[256]; |
| 1121 | FILE *fp; |
| 1122 | char line[1024]; |
| 1123 | |
| 1124 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 1125 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
| 1129 | // Create a string to compare against that has a trailing slash |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1130 | int loopDirLen = strlen(Volume::LOOPDIR); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 1131 | char loopDir[loopDirLen + 2]; |
| 1132 | strcpy(loopDir, Volume::LOOPDIR); |
| 1133 | loopDir[loopDirLen++] = '/'; |
| 1134 | loopDir[loopDirLen] = '\0'; |
| 1135 | |
| 1136 | while(fgets(line, sizeof(line), fp)) { |
| 1137 | line[strlen(line)-1] = '\0'; |
| 1138 | |
| 1139 | /* |
| 1140 | * Should look like: |
| 1141 | * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ... |
| 1142 | */ |
| 1143 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1144 | |
| 1145 | if (!strncmp(mount_path, loopDir, loopDirLen)) { |
| 1146 | int fd = open(device, O_RDONLY); |
| 1147 | if (fd >= 0) { |
| 1148 | struct loop_info64 li; |
| 1149 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 1150 | cli->sendMsg(ResponseCode::AsecListResult, |
| 1151 | (const char*) li.lo_file_name, false); |
| 1152 | } |
| 1153 | close(fd); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | fclose(fp); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1162 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 1163 | Volume *v = lookupVolume(label); |
| 1164 | |
| 1165 | if (!v) { |
| 1166 | errno = ENOENT; |
| 1167 | return -1; |
| 1168 | } |
| 1169 | |
| 1170 | if (strcmp(method, "ums")) { |
| 1171 | errno = ENOSYS; |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1176 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 1177 | } else { |
| 1178 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 1179 | } |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1183 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1184 | Volume *v = lookupVolume(label); |
| 1185 | |
| 1186 | if (!v) { |
| 1187 | errno = ENOENT; |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * Eventually, we'll want to support additional share back-ends, |
| 1193 | * some of which may work while the media is mounted. For now, |
| 1194 | * we just support UMS |
| 1195 | */ |
| 1196 | if (strcmp(method, "ums")) { |
| 1197 | errno = ENOSYS; |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | if (v->getState() == Volume::State_NoMedia) { |
| 1202 | errno = ENODEV; |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1206 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1207 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1208 | errno = EBUSY; |
| 1209 | return -1; |
| 1210 | } |
| 1211 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1212 | if (mVolManagerDisabled) { |
| 1213 | errno = EBUSY; |
| 1214 | return -1; |
| 1215 | } |
| 1216 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 1217 | dev_t d = v->getShareDevice(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1218 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1219 | // This volume does not support raw disk access |
| 1220 | errno = EINVAL; |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | int fd; |
| 1225 | char nodepath[255]; |
| 1226 | snprintf(nodepath, |
| 1227 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 1228 | MAJOR(d), MINOR(d)); |
| 1229 | |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1230 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1231 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1236 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1237 | close(fd); |
| 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | close(fd); |
| 1242 | v->handleVolumeShared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1243 | if (mUmsSharingCount++ == 0) { |
| 1244 | FILE* fp; |
| 1245 | mSavedDirtyRatio = -1; // in case we fail |
| 1246 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1247 | char line[16]; |
| 1248 | if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) { |
| 1249 | fprintf(fp, "%d\n", mUmsDirtyRatio); |
| 1250 | } else { |
| 1251 | SLOGE("Failed to read dirty_ratio (%s)", strerror(errno)); |
| 1252 | } |
| 1253 | fclose(fp); |
| 1254 | } else { |
| 1255 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1256 | } |
| 1257 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1262 | Volume *v = lookupVolume(label); |
| 1263 | |
| 1264 | if (!v) { |
| 1265 | errno = ENOENT; |
| 1266 | return -1; |
| 1267 | } |
| 1268 | |
| 1269 | if (strcmp(method, "ums")) { |
| 1270 | errno = ENOSYS; |
| 1271 | return -1; |
| 1272 | } |
| 1273 | |
| 1274 | if (v->getState() != Volume::State_Shared) { |
| 1275 | errno = EINVAL; |
| 1276 | return -1; |
| 1277 | } |
| 1278 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1279 | int fd; |
Mike Lockwood | 97f2fc1 | 2011-06-07 10:51:38 -0700 | [diff] [blame] | 1280 | if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1281 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1282 | return -1; |
| 1283 | } |
| 1284 | |
| 1285 | char ch = 0; |
| 1286 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1287 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1288 | close(fd); |
| 1289 | return -1; |
| 1290 | } |
| 1291 | |
| 1292 | close(fd); |
| 1293 | v->handleVolumeUnshared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1294 | if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) { |
| 1295 | FILE* fp; |
| 1296 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1297 | fprintf(fp, "%d\n", mSavedDirtyRatio); |
| 1298 | fclose(fp); |
| 1299 | } else { |
| 1300 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1301 | } |
| 1302 | mSavedDirtyRatio = -1; |
| 1303 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1304 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1307 | extern "C" int vold_disableVol(const char *label) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1308 | VolumeManager *vm = VolumeManager::Instance(); |
Ken Sumrall | 3b17005 | 2011-07-11 15:38:57 -0700 | [diff] [blame] | 1309 | vm->disableVolumeManager(); |
| 1310 | vm->unshareVolume(label, "ums"); |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1311 | return vm->unmountVolume(label, true, false); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | extern "C" int vold_getNumDirectVolumes(void) { |
| 1315 | VolumeManager *vm = VolumeManager::Instance(); |
| 1316 | return vm->getNumDirectVolumes(); |
| 1317 | } |
| 1318 | |
| 1319 | int VolumeManager::getNumDirectVolumes(void) { |
| 1320 | VolumeCollection::iterator i; |
| 1321 | int n=0; |
| 1322 | |
| 1323 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1324 | if ((*i)->getShareDevice() != (dev_t)0) { |
| 1325 | n++; |
| 1326 | } |
| 1327 | } |
| 1328 | return n; |
| 1329 | } |
| 1330 | |
| 1331 | extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) { |
| 1332 | VolumeManager *vm = VolumeManager::Instance(); |
| 1333 | return vm->getDirectVolumeList(vol_list); |
| 1334 | } |
| 1335 | |
| 1336 | int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) { |
| 1337 | VolumeCollection::iterator i; |
| 1338 | int n=0; |
| 1339 | dev_t d; |
| 1340 | |
| 1341 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1342 | if ((d=(*i)->getShareDevice()) != (dev_t)0) { |
| 1343 | (*i)->getVolInfo(&vol_list[n]); |
| 1344 | snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev), |
| 1345 | "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d)); |
| 1346 | n++; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1353 | int VolumeManager::unmountVolume(const char *label, bool force, bool revert) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1354 | Volume *v = lookupVolume(label); |
| 1355 | |
| 1356 | if (!v) { |
| 1357 | errno = ENOENT; |
| 1358 | return -1; |
| 1359 | } |
| 1360 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1361 | if (v->getState() == Volume::State_NoMedia) { |
| 1362 | errno = ENODEV; |
| 1363 | return -1; |
| 1364 | } |
| 1365 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1366 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1367 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1368 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1369 | errno = EBUSY; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 1370 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1373 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1374 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1375 | return v->unmountVol(force, revert); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 1378 | extern "C" int vold_unmountAllAsecs(void) { |
| 1379 | int rc; |
| 1380 | |
| 1381 | VolumeManager *vm = VolumeManager::Instance(); |
| 1382 | rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT); |
| 1383 | if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) { |
| 1384 | rc = -1; |
| 1385 | } |
| 1386 | return rc; |
| 1387 | } |
| 1388 | |
| 1389 | #define ID_BUF_LEN 256 |
| 1390 | #define ASEC_SUFFIX ".asec" |
| 1391 | #define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1) |
| 1392 | int VolumeManager::unmountAllAsecsInDir(const char *directory) { |
| 1393 | DIR *d = opendir(directory); |
| 1394 | int rc = 0; |
| 1395 | |
| 1396 | if (!d) { |
| 1397 | SLOGE("Could not open asec dir %s", directory); |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
| 1401 | size_t dirent_len = offsetof(struct dirent, d_name) + |
| 1402 | pathconf(directory, _PC_NAME_MAX) + 1; |
| 1403 | |
| 1404 | struct dirent *dent = (struct dirent *) malloc(dirent_len); |
| 1405 | if (dent == NULL) { |
| 1406 | SLOGE("Failed to allocate memory for asec dir"); |
| 1407 | return -1; |
| 1408 | } |
| 1409 | |
| 1410 | struct dirent *result; |
| 1411 | while (!readdir_r(d, dent, &result) && result != NULL) { |
| 1412 | if (dent->d_name[0] == '.') |
| 1413 | continue; |
| 1414 | if (dent->d_type != DT_REG) |
| 1415 | continue; |
| 1416 | size_t name_len = strlen(dent->d_name); |
| 1417 | if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) && |
| 1418 | !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) { |
| 1419 | char id[ID_BUF_LEN]; |
| 1420 | strlcpy(id, dent->d_name, name_len - 4); |
| 1421 | if (unmountAsec(id, true)) { |
| 1422 | /* Register the error, but try to unmount more asecs */ |
| 1423 | rc = -1; |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | closedir(d); |
| 1428 | |
| 1429 | free(dent); |
| 1430 | |
| 1431 | return rc; |
| 1432 | } |
| 1433 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1434 | /* |
| 1435 | * Looks up a volume by it's label or mount-point |
| 1436 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1437 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1438 | VolumeCollection::iterator i; |
| 1439 | |
| 1440 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1441 | if (label[0] == '/') { |
| 1442 | if (!strcmp(label, (*i)->getMountpoint())) |
| 1443 | return (*i); |
| 1444 | } else { |
| 1445 | if (!strcmp(label, (*i)->getLabel())) |
| 1446 | return (*i); |
| 1447 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1448 | } |
| 1449 | return NULL; |
| 1450 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1451 | |
| 1452 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1453 | { |
| 1454 | char device[256]; |
| 1455 | char mount_path[256]; |
| 1456 | char rest[256]; |
| 1457 | FILE *fp; |
| 1458 | char line[1024]; |
| 1459 | |
| 1460 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1461 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1462 | return false; |
| 1463 | } |
| 1464 | |
| 1465 | while(fgets(line, sizeof(line), fp)) { |
| 1466 | line[strlen(line)-1] = '\0'; |
| 1467 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1468 | if (!strcmp(mount_path, mp)) { |
| 1469 | fclose(fp); |
| 1470 | return true; |
| 1471 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | fclose(fp); |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1478 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1479 | int rc = unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT); |
| 1480 | |
| 1481 | AsecIdCollection toUnmount; |
| 1482 | // Find the remaining OBB files that are on external storage. |
| 1483 | for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end(); |
| 1484 | ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1485 | ContainerData* cd = *it; |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1486 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1487 | if (cd->type == ASEC) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1488 | // nothing |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1489 | } else if (cd->type == OBB) { |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1490 | if (v == getVolumeForFile(cd->id)) { |
| 1491 | toUnmount.push_back(cd); |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1492 | } |
| 1493 | } else { |
| 1494 | SLOGE("Unknown container type %d!", cd->type); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1495 | } |
| 1496 | } |
Kenny Root | 93ecb38 | 2012-08-09 11:28:37 -0700 | [diff] [blame] | 1497 | |
| 1498 | for (AsecIdCollection::iterator it = toUnmount.begin(); it != toUnmount.end(); ++it) { |
| 1499 | ContainerData *cd = *it; |
| 1500 | SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint()); |
| 1501 | if (unmountObb(cd->id, force)) { |
| 1502 | SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno)); |
| 1503 | rc = -1; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | return rc; |
| 1508 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |