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