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