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> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/mount.h> |
| 25 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 26 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 27 | |
| 28 | #define LOG_TAG "Vold" |
| 29 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 30 | #include <openssl/md5.h> |
| 31 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 32 | #include <cutils/log.h> |
| 33 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 34 | #include <sysutils/NetlinkEvent.h> |
| 35 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 37 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 38 | #include "ResponseCode.h" |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 39 | #include "Loop.h" |
| 40 | #include "Fat.h" |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 41 | #include "Devmapper.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 42 | #include "Process.h" |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 43 | #include "Asec.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame^] | 44 | #include "cryptfs.h" |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 45 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | VolumeManager *VolumeManager::sInstance = NULL; |
| 47 | |
| 48 | VolumeManager *VolumeManager::Instance() { |
| 49 | if (!sInstance) |
| 50 | sInstance = new VolumeManager(); |
| 51 | return sInstance; |
| 52 | } |
| 53 | |
| 54 | VolumeManager::VolumeManager() { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 55 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 56 | mVolumes = new VolumeCollection(); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 57 | mActiveContainers = new AsecIdCollection(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 58 | mBroadcaster = NULL; |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 59 | mUsbMassStorageEnabled = false; |
| 60 | mUsbConnected = false; |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 61 | mUmsSharingCount = 0; |
| 62 | mSavedDirtyRatio = -1; |
| 63 | // set dirty ratio to 0 when UMS is active |
| 64 | mUmsDirtyRatio = 0; |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 65 | |
| 66 | readInitialState(); |
| 67 | } |
| 68 | |
| 69 | void VolumeManager::readInitialState() { |
| 70 | FILE *fp; |
| 71 | char state[255]; |
| 72 | |
| 73 | /* |
| 74 | * Read the initial mass storage enabled state |
| 75 | */ |
| 76 | if ((fp = fopen("/sys/devices/virtual/usb_composite/usb_mass_storage/enable", "r"))) { |
| 77 | if (fgets(state, sizeof(state), fp)) { |
| 78 | mUsbMassStorageEnabled = !strncmp(state, "1", 1); |
| 79 | } else { |
| 80 | SLOGE("Failed to read usb_mass_storage enabled state (%s)", strerror(errno)); |
| 81 | } |
| 82 | fclose(fp); |
| 83 | } else { |
| 84 | SLOGD("USB mass storage support is not enabled in the kernel"); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Read the initial USB connected state |
| 89 | */ |
| 90 | if ((fp = fopen("/sys/devices/virtual/switch/usb_configuration/state", "r"))) { |
| 91 | if (fgets(state, sizeof(state), fp)) { |
| 92 | mUsbConnected = !strncmp(state, "1", 1); |
| 93 | } else { |
| 94 | SLOGE("Failed to read usb_configuration switch (%s)", strerror(errno)); |
| 95 | } |
| 96 | fclose(fp); |
| 97 | } else { |
| 98 | SLOGD("usb_configuration switch is not enabled in the kernel"); |
| 99 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | VolumeManager::~VolumeManager() { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 103 | delete mVolumes; |
| 104 | delete mActiveContainers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 107 | char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 108 | static const char* digits = "0123456789abcdef"; |
| 109 | |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 110 | unsigned char sig[MD5_DIGEST_LENGTH]; |
| 111 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 112 | if (buffer == NULL) { |
| 113 | SLOGE("Destination buffer is NULL"); |
| 114 | errno = ESPIPE; |
| 115 | return NULL; |
| 116 | } else if (id == NULL) { |
| 117 | SLOGE("Source buffer is NULL"); |
| 118 | errno = ESPIPE; |
| 119 | return NULL; |
| 120 | } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) { |
| 121 | SLOGE("Target hash buffer size < %d bytes (%d)", |
| 122 | MD5_ASCII_LENGTH_PLUS_NULL, len); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 123 | errno = ESPIPE; |
| 124 | return NULL; |
| 125 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 126 | |
| 127 | MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 128 | |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 129 | char *p = buffer; |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 130 | for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 131 | *p++ = digits[sig[i] >> 4]; |
| 132 | *p++ = digits[sig[i] & 0x0F]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 133 | } |
Kenny Root | acc9e7d | 2010-06-18 19:06:50 -0700 | [diff] [blame] | 134 | *p = '\0'; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 135 | |
| 136 | return buffer; |
| 137 | } |
| 138 | |
| 139 | void VolumeManager::setDebug(bool enable) { |
| 140 | mDebug = enable; |
| 141 | VolumeCollection::iterator it; |
| 142 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
| 143 | (*it)->setDebug(enable); |
| 144 | } |
| 145 | } |
| 146 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 147 | int VolumeManager::start() { |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int VolumeManager::stop() { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int VolumeManager::addVolume(Volume *v) { |
| 156 | mVolumes->push_back(v); |
| 157 | return 0; |
| 158 | } |
| 159 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 160 | void VolumeManager::notifyUmsAvailable(bool available) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 161 | char msg[255]; |
| 162 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 163 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 164 | (available ? "available" : "unavailable")); |
| 165 | SLOGD(msg); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 166 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 167 | msg, false); |
| 168 | } |
| 169 | |
| 170 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 171 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 172 | const char *name = evt->findParam("SWITCH_NAME"); |
| 173 | const char *state = evt->findParam("SWITCH_STATE"); |
| 174 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 175 | if (!name || !state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 176 | SLOGW("Switch %s event missing name/state info", devpath); |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 180 | bool oldAvailable = massStorageAvailable(); |
| 181 | if (!strcmp(name, "usb_configuration")) { |
| 182 | mUsbConnected = !strcmp(state, "1"); |
| 183 | SLOGD("USB %s", mUsbConnected ? "connected" : "disconnected"); |
| 184 | bool newAvailable = massStorageAvailable(); |
| 185 | if (newAvailable != oldAvailable) { |
| 186 | notifyUmsAvailable(newAvailable); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 187 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 190 | void VolumeManager::handleUsbCompositeEvent(NetlinkEvent *evt) { |
| 191 | const char *function = evt->findParam("FUNCTION"); |
| 192 | const char *enabled = evt->findParam("ENABLED"); |
| 193 | |
| 194 | if (!function || !enabled) { |
| 195 | SLOGW("usb_composite event missing function/enabled info"); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (!strcmp(function, "usb_mass_storage")) { |
| 200 | bool oldAvailable = massStorageAvailable(); |
| 201 | mUsbMassStorageEnabled = !strcmp(enabled, "1"); |
| 202 | SLOGD("usb_mass_storage function %s", mUsbMassStorageEnabled ? "enabled" : "disabled"); |
| 203 | bool newAvailable = massStorageAvailable(); |
| 204 | if (newAvailable != oldAvailable) { |
| 205 | notifyUmsAvailable(newAvailable); |
| 206 | } |
| 207 | } |
| 208 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 209 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 210 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 211 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 212 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 213 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 214 | VolumeCollection::iterator it; |
| 215 | bool hit = false; |
| 216 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 217 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 218 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 219 | SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 220 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 221 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 227 | #ifdef NETLINK_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 228 | SLOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 229 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 233 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 234 | VolumeCollection::iterator i; |
| 235 | |
| 236 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 237 | char *buffer; |
| 238 | asprintf(&buffer, "%s %s %d", |
| 239 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 240 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 241 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 242 | free(buffer); |
| 243 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 244 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 247 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 248 | int VolumeManager::formatVolume(const char *label) { |
| 249 | Volume *v = lookupVolume(label); |
| 250 | |
| 251 | if (!v) { |
| 252 | errno = ENOENT; |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | return v->formatVol(); |
| 257 | } |
| 258 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 259 | int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) { |
| 260 | char idHash[33]; |
| 261 | if (!asecHash(sourceFile, idHash, sizeof(idHash))) { |
| 262 | SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno)); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | memset(mountPath, 0, mountPathLen); |
| 267 | snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash); |
| 268 | |
| 269 | if (access(mountPath, F_OK)) { |
| 270 | errno = ENOENT; |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 277 | int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 278 | char asecFileName[255]; |
| 279 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 280 | |
| 281 | memset(buffer, 0, maxlen); |
| 282 | if (access(asecFileName, F_OK)) { |
| 283 | errno = ENOENT; |
| 284 | return -1; |
| 285 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 286 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 287 | snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
San Mehat | 8b8f71b | 2010-01-11 09:17:25 -0800 | [diff] [blame] | 291 | int VolumeManager::createAsec(const char *id, unsigned int numSectors, |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 292 | const char *fstype, const char *key, int ownerUid) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 293 | struct asec_superblock sb; |
| 294 | memset(&sb, 0, sizeof(sb)); |
| 295 | |
| 296 | sb.magic = ASEC_SB_MAGIC; |
| 297 | sb.ver = ASEC_SB_VER; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 298 | |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 299 | if (numSectors < ((1024*1024)/512)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 300 | SLOGE("Invalid container size specified (%d sectors)", numSectors); |
San Mehat | d31e380 | 2010-02-18 08:37:45 -0800 | [diff] [blame] | 301 | errno = EINVAL; |
| 302 | return -1; |
| 303 | } |
| 304 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 305 | if (lookupVolume(id)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 306 | SLOGE("ASEC id '%s' currently exists", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 307 | errno = EADDRINUSE; |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | char asecFileName[255]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 312 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 313 | |
| 314 | if (!access(asecFileName, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 315 | SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)", |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 316 | asecFileName, strerror(errno)); |
| 317 | errno = EADDRINUSE; |
| 318 | return -1; |
| 319 | } |
| 320 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 321 | /* |
| 322 | * Add some headroom |
| 323 | */ |
| 324 | unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; |
| 325 | unsigned numImgSectors = numSectors + fatSize + 2; |
| 326 | |
| 327 | if (numImgSectors % 63) { |
| 328 | numImgSectors += (63 - (numImgSectors % 63)); |
| 329 | } |
| 330 | |
| 331 | // Add +1 for our superblock which is at the end |
| 332 | if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 333 | SLOGE("ASEC image file creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 337 | char idHash[33]; |
| 338 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 339 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 340 | unlink(asecFileName); |
| 341 | return -1; |
| 342 | } |
| 343 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 344 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 345 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 346 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 347 | unlink(asecFileName); |
| 348 | return -1; |
| 349 | } |
| 350 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 351 | char dmDevice[255]; |
| 352 | bool cleanupDm = false; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 353 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 354 | if (strcmp(key, "none")) { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 355 | // XXX: This is all we support for now |
| 356 | sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 357 | if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 358 | sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 359 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 360 | Loop::destroyByDevice(loopDevice); |
| 361 | unlink(asecFileName); |
| 362 | return -1; |
| 363 | } |
| 364 | cleanupDm = true; |
| 365 | } else { |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 366 | sb.c_cipher = ASEC_SB_C_CIPHER_NONE; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 367 | strcpy(dmDevice, loopDevice); |
| 368 | } |
| 369 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 370 | /* |
| 371 | * Drop down the superblock at the end of the file |
| 372 | */ |
| 373 | |
| 374 | int sbfd = open(loopDevice, O_RDWR); |
| 375 | if (sbfd < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 376 | 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] | 377 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 378 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 379 | } |
| 380 | Loop::destroyByDevice(loopDevice); |
| 381 | unlink(asecFileName); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) { |
| 386 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 387 | SLOGE("Failed to lseek for superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 388 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 389 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 390 | } |
| 391 | Loop::destroyByDevice(loopDevice); |
| 392 | unlink(asecFileName); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 397 | close(sbfd); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 398 | SLOGE("Failed to write superblock (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 399 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 400 | Devmapper::destroy(idHash); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 401 | } |
| 402 | Loop::destroyByDevice(loopDevice); |
| 403 | unlink(asecFileName); |
| 404 | return -1; |
| 405 | } |
| 406 | close(sbfd); |
| 407 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 408 | if (strcmp(fstype, "none")) { |
| 409 | if (strcmp(fstype, "fat")) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 410 | SLOGW("Unknown fstype '%s' specified for container", fstype); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 411 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 412 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 413 | if (Fat::format(dmDevice, numImgSectors)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 414 | SLOGE("ASEC FAT format failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 415 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 416 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 417 | } |
San Mehat | eb13a90 | 2010-01-07 12:12:50 -0800 | [diff] [blame] | 418 | Loop::destroyByDevice(loopDevice); |
| 419 | unlink(asecFileName); |
| 420 | return -1; |
| 421 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 422 | char mountPoint[255]; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 423 | |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 424 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
| 425 | if (mkdir(mountPoint, 0777)) { |
| 426 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 427 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 428 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 429 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 430 | } |
| 431 | Loop::destroyByDevice(loopDevice); |
| 432 | unlink(asecFileName); |
| 433 | return -1; |
| 434 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 435 | } |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 436 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 437 | if (Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 438 | 0, 0000, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 439 | SLOGE("ASEC FAT mount failed (%s)", strerror(errno)); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 440 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 441 | Devmapper::destroy(idHash); |
San Mehat | a1091cb | 2010-02-28 20:17:20 -0800 | [diff] [blame] | 442 | } |
| 443 | Loop::destroyByDevice(loopDevice); |
| 444 | unlink(asecFileName); |
| 445 | return -1; |
| 446 | } |
| 447 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 448 | SLOGI("Created raw secure container %s (no filesystem)", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 449 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 450 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 451 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | int VolumeManager::finalizeAsec(const char *id) { |
| 456 | char asecFileName[255]; |
| 457 | char loopDevice[255]; |
| 458 | char mountPoint[255]; |
| 459 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 460 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 461 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 462 | char idHash[33]; |
| 463 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 464 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 469 | SLOGE("Unable to finalize %s (%s)", id, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 470 | return -1; |
| 471 | } |
| 472 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 473 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 474 | // XXX: |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 475 | if (Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 476 | SLOGE("ASEC finalize mount failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 477 | return -1; |
| 478 | } |
| 479 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 480 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 481 | SLOGD("ASEC %s finalized", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 482 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 486 | int VolumeManager::renameAsec(const char *id1, const char *id2) { |
| 487 | char *asecFilename1; |
| 488 | char *asecFilename2; |
| 489 | char mountPoint[255]; |
| 490 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 491 | asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1); |
| 492 | asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 493 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 494 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 495 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 496 | SLOGW("Rename attempt when src mounted"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 497 | errno = EBUSY; |
| 498 | goto out_err; |
| 499 | } |
| 500 | |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 501 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2); |
| 502 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 503 | SLOGW("Rename attempt when dst mounted"); |
San Mehat | 96956ed | 2010-02-24 08:42:51 -0800 | [diff] [blame] | 504 | errno = EBUSY; |
| 505 | goto out_err; |
| 506 | } |
| 507 | |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 508 | if (!access(asecFilename2, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 509 | SLOGE("Rename attempt when dst exists"); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 510 | errno = EADDRINUSE; |
| 511 | goto out_err; |
| 512 | } |
| 513 | |
| 514 | if (rename(asecFilename1, asecFilename2)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 515 | SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno)); |
San Mehat | 048b080 | 2010-01-23 08:17:06 -0800 | [diff] [blame] | 516 | goto out_err; |
| 517 | } |
| 518 | |
| 519 | free(asecFilename1); |
| 520 | free(asecFilename2); |
| 521 | return 0; |
| 522 | |
| 523 | out_err: |
| 524 | free(asecFilename1); |
| 525 | free(asecFilename2); |
| 526 | return -1; |
| 527 | } |
| 528 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 529 | #define UNMOUNT_RETRIES 5 |
| 530 | #define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 531 | int VolumeManager::unmountAsec(const char *id, bool force) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 532 | char asecFileName[255]; |
| 533 | char mountPoint[255]; |
| 534 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 535 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 536 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 537 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 538 | char idHash[33]; |
| 539 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 540 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 544 | return unmountLoopImage(id, idHash, asecFileName, mountPoint, force); |
| 545 | } |
| 546 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 547 | int VolumeManager::unmountObb(const char *fileName, bool force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 548 | char mountPoint[255]; |
| 549 | |
| 550 | char idHash[33]; |
| 551 | if (!asecHash(fileName, idHash, sizeof(idHash))) { |
| 552 | SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno)); |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 557 | |
| 558 | return unmountLoopImage(fileName, idHash, fileName, mountPoint, force); |
| 559 | } |
| 560 | |
| 561 | int VolumeManager::unmountLoopImage(const char *id, const char *idHash, |
| 562 | const char *fileName, const char *mountPoint, bool force) { |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 563 | if (!isMountpointMounted(mountPoint)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 564 | SLOGE("Unmount request for %s when not mounted", id); |
Kenny Root | 918e5f9 | 2010-09-30 18:00:52 -0700 | [diff] [blame] | 565 | errno = ENOENT; |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 566 | return -1; |
| 567 | } |
San Mehat | 2396993 | 2010-01-09 07:08:06 -0800 | [diff] [blame] | 568 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 569 | int i, rc; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 570 | for (i = 1; i <= UNMOUNT_RETRIES; i++) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 571 | rc = umount(mountPoint); |
| 572 | if (!rc) { |
| 573 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 574 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 575 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 576 | SLOGI("Container %s unmounted OK", id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 577 | rc = 0; |
| 578 | break; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 579 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 580 | SLOGW("%s unmount attempt %d failed (%s)", |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 581 | id, i, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 582 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 583 | int action = 0; // default is to just complain |
| 584 | |
| 585 | if (force) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 586 | if (i > (UNMOUNT_RETRIES - 2)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 587 | action = 2; // SIGKILL |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 588 | else if (i > (UNMOUNT_RETRIES - 3)) |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 589 | action = 1; // SIGHUP |
| 590 | } |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 591 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 592 | Process::killProcessesWithOpenFiles(mountPoint, action); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 593 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | if (rc) { |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 597 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 598 | SLOGE("Failed to unmount container %s (%s)", id, strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 599 | return -1; |
| 600 | } |
| 601 | |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 602 | int retries = 10; |
| 603 | |
| 604 | while(retries--) { |
| 605 | if (!rmdir(mountPoint)) { |
| 606 | break; |
| 607 | } |
| 608 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 609 | SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 610 | usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS); |
San Mehat | 12f4b89 | 2010-02-24 11:43:22 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (!retries) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 614 | SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno)); |
San Mehat | f5c6198 | 2010-02-03 11:04:46 -0800 | [diff] [blame] | 615 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 616 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 617 | if (Devmapper::destroy(idHash) && errno != ENXIO) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 618 | SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 622 | if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 623 | Loop::destroyByDevice(loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 624 | } else { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 625 | SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 626 | } |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 627 | |
| 628 | AsecIdCollection::iterator it; |
| 629 | for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 630 | ContainerData* cd = *it; |
| 631 | if (!strcmp(cd->id, id)) { |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 632 | free(*it); |
| 633 | mActiveContainers->erase(it); |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | if (it == mActiveContainers->end()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 638 | SLOGW("mActiveContainers is inconsistent!"); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 639 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 643 | int VolumeManager::destroyAsec(const char *id, bool force) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 644 | char asecFileName[255]; |
| 645 | char mountPoint[255]; |
| 646 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 647 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
San Mehat | 55013f7 | 2010-02-24 12:12:34 -0800 | [diff] [blame] | 648 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 649 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 650 | if (isMountpointMounted(mountPoint)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 651 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 652 | SLOGD("Unmounting container before destroy"); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 653 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 654 | if (unmountAsec(id, force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 655 | SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 659 | |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 660 | if (unlink(asecFileName)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 661 | SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); |
San Mehat | 0586d54 | 2010-01-12 15:38:59 -0800 | [diff] [blame] | 662 | return -1; |
| 663 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 664 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 665 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 666 | SLOGD("ASEC %s destroyed", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 667 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { |
| 672 | char asecFileName[255]; |
| 673 | char mountPoint[255]; |
| 674 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 675 | snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id); |
| 676 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 677 | |
| 678 | if (isMountpointMounted(mountPoint)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 679 | SLOGE("ASEC %s already mounted", id); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 680 | errno = EBUSY; |
| 681 | return -1; |
| 682 | } |
| 683 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 684 | char idHash[33]; |
| 685 | if (!asecHash(id, idHash, sizeof(idHash))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 686 | SLOGE("Hash of '%s' failed (%s)", id, strerror(errno)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 687 | return -1; |
| 688 | } |
Kenny Root | 7b18a7b | 2010-03-15 13:13:41 -0700 | [diff] [blame] | 689 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 690 | char loopDevice[255]; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 691 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 692 | if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 693 | SLOGE("ASEC loop device creation failed (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 694 | return -1; |
| 695 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 696 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 697 | SLOGD("New loop device created at %s", loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 698 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 699 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 700 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 701 | SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 702 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | char dmDevice[255]; |
| 706 | bool cleanupDm = false; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 707 | int fd; |
| 708 | unsigned int nr_sec = 0; |
| 709 | |
| 710 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 711 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 712 | Loop::destroyByDevice(loopDevice); |
| 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 717 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 718 | Loop::destroyByDevice(loopDevice); |
| 719 | close(fd); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Validate superblock |
| 725 | */ |
| 726 | struct asec_superblock sb; |
| 727 | memset(&sb, 0, sizeof(sb)); |
| 728 | if (lseek(fd, ((nr_sec-1) * 512), SEEK_SET) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 729 | SLOGE("lseek failed (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 730 | close(fd); |
| 731 | Loop::destroyByDevice(loopDevice); |
| 732 | return -1; |
| 733 | } |
| 734 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 735 | SLOGE("superblock read failed (%s)", strerror(errno)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 736 | close(fd); |
| 737 | Loop::destroyByDevice(loopDevice); |
| 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | close(fd); |
| 742 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 743 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 744 | SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 745 | } |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 746 | if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 747 | SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 748 | Loop::destroyByDevice(loopDevice); |
| 749 | errno = EMEDIUMTYPE; |
| 750 | return -1; |
| 751 | } |
| 752 | nr_sec--; // We don't want the devmapping to extend onto our superblock |
| 753 | |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 754 | if (strcmp(key, "none")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 755 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 756 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 757 | dmDevice, sizeof(dmDevice))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 758 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 759 | Loop::destroyByDevice(loopDevice); |
| 760 | return -1; |
| 761 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 762 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 763 | SLOGD("New devmapper instance created at %s", dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 764 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 765 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 766 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 767 | SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 768 | } |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 769 | } |
| 770 | cleanupDm = true; |
| 771 | } else { |
| 772 | strcpy(dmDevice, loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if (mkdir(mountPoint, 0777)) { |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 776 | if (errno != EEXIST) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 777 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 778 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 779 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 780 | } |
| 781 | Loop::destroyByDevice(loopDevice); |
| 782 | return -1; |
| 783 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 786 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, |
San Mehat | cff5ec3 | 2010-01-08 12:31:44 -0800 | [diff] [blame] | 787 | 0222, false)) { |
| 788 | // 0227, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 789 | SLOGE("ASEC mount failed (%s)", strerror(errno)); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 790 | if (cleanupDm) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 791 | Devmapper::destroy(idHash); |
San Mehat | b78a32c | 2010-01-10 13:02:12 -0800 | [diff] [blame] | 792 | } |
| 793 | Loop::destroyByDevice(loopDevice); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 794 | return -1; |
| 795 | } |
| 796 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 797 | mActiveContainers->push_back(new ContainerData(strdup(id), ASEC)); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 798 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 799 | SLOGD("ASEC %s mounted", id); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 800 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 804 | /** |
| 805 | * Mounts an image file <code>img</code>. |
| 806 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 807 | int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 808 | char mountPoint[255]; |
| 809 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 810 | char idHash[33]; |
| 811 | if (!asecHash(img, idHash, sizeof(idHash))) { |
| 812 | SLOGE("Hash of '%s' failed (%s)", img, strerror(errno)); |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash); |
| 817 | |
| 818 | if (isMountpointMounted(mountPoint)) { |
| 819 | SLOGE("Image %s already mounted", img); |
| 820 | errno = EBUSY; |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | char loopDevice[255]; |
| 825 | if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) { |
| 826 | if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) { |
| 827 | SLOGE("Image loop device creation failed (%s)", strerror(errno)); |
| 828 | return -1; |
| 829 | } |
| 830 | if (mDebug) { |
| 831 | SLOGD("New loop device created at %s", loopDevice); |
| 832 | } |
| 833 | } else { |
| 834 | if (mDebug) { |
| 835 | SLOGD("Found active loopback for %s at %s", img, loopDevice); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | char dmDevice[255]; |
| 840 | bool cleanupDm = false; |
| 841 | int fd; |
| 842 | unsigned int nr_sec = 0; |
| 843 | |
| 844 | if ((fd = open(loopDevice, O_RDWR)) < 0) { |
| 845 | SLOGE("Failed to open loopdevice (%s)", strerror(errno)); |
| 846 | Loop::destroyByDevice(loopDevice); |
| 847 | return -1; |
| 848 | } |
| 849 | |
| 850 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 851 | SLOGE("Failed to get loop size (%s)", strerror(errno)); |
| 852 | Loop::destroyByDevice(loopDevice); |
| 853 | close(fd); |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | close(fd); |
| 858 | |
| 859 | if (strcmp(key, "none")) { |
| 860 | if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) { |
| 861 | if (Devmapper::create(idHash, loopDevice, key, nr_sec, |
| 862 | dmDevice, sizeof(dmDevice))) { |
| 863 | SLOGE("ASEC device mapping failed (%s)", strerror(errno)); |
| 864 | Loop::destroyByDevice(loopDevice); |
| 865 | return -1; |
| 866 | } |
| 867 | if (mDebug) { |
| 868 | SLOGD("New devmapper instance created at %s", dmDevice); |
| 869 | } |
| 870 | } else { |
| 871 | if (mDebug) { |
| 872 | SLOGD("Found active devmapper for %s at %s", img, dmDevice); |
| 873 | } |
| 874 | } |
| 875 | cleanupDm = true; |
| 876 | } else { |
| 877 | strcpy(dmDevice, loopDevice); |
| 878 | } |
| 879 | |
| 880 | if (mkdir(mountPoint, 0755)) { |
| 881 | if (errno != EEXIST) { |
| 882 | SLOGE("Mountpoint creation failed (%s)", strerror(errno)); |
| 883 | if (cleanupDm) { |
| 884 | Devmapper::destroy(idHash); |
| 885 | } |
| 886 | Loop::destroyByDevice(loopDevice); |
| 887 | return -1; |
| 888 | } |
| 889 | } |
| 890 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 891 | if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 892 | 0227, false)) { |
| 893 | SLOGE("Image mount failed (%s)", strerror(errno)); |
| 894 | if (cleanupDm) { |
| 895 | Devmapper::destroy(idHash); |
| 896 | } |
| 897 | Loop::destroyByDevice(loopDevice); |
| 898 | return -1; |
| 899 | } |
| 900 | |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 901 | mActiveContainers->push_back(new ContainerData(strdup(img), OBB)); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 902 | if (mDebug) { |
| 903 | SLOGD("Image %s mounted", img); |
| 904 | } |
| 905 | return 0; |
| 906 | } |
| 907 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 908 | int VolumeManager::mountVolume(const char *label) { |
| 909 | Volume *v = lookupVolume(label); |
| 910 | |
| 911 | if (!v) { |
| 912 | errno = ENOENT; |
| 913 | return -1; |
| 914 | } |
| 915 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 916 | return v->mountVol(); |
| 917 | } |
| 918 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 919 | int VolumeManager::listMountedObbs(SocketClient* cli) { |
| 920 | char device[256]; |
| 921 | char mount_path[256]; |
| 922 | char rest[256]; |
| 923 | FILE *fp; |
| 924 | char line[1024]; |
| 925 | |
| 926 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 927 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 928 | return -1; |
| 929 | } |
| 930 | |
| 931 | // Create a string to compare against that has a trailing slash |
| 932 | int loopDirLen = sizeof(Volume::LOOPDIR); |
| 933 | char loopDir[loopDirLen + 2]; |
| 934 | strcpy(loopDir, Volume::LOOPDIR); |
| 935 | loopDir[loopDirLen++] = '/'; |
| 936 | loopDir[loopDirLen] = '\0'; |
| 937 | |
| 938 | while(fgets(line, sizeof(line), fp)) { |
| 939 | line[strlen(line)-1] = '\0'; |
| 940 | |
| 941 | /* |
| 942 | * Should look like: |
| 943 | * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ... |
| 944 | */ |
| 945 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 946 | |
| 947 | if (!strncmp(mount_path, loopDir, loopDirLen)) { |
| 948 | int fd = open(device, O_RDONLY); |
| 949 | if (fd >= 0) { |
| 950 | struct loop_info64 li; |
| 951 | if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) { |
| 952 | cli->sendMsg(ResponseCode::AsecListResult, |
| 953 | (const char*) li.lo_file_name, false); |
| 954 | } |
| 955 | close(fd); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | fclose(fp); |
| 961 | return 0; |
| 962 | } |
| 963 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 964 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 965 | |
| 966 | if (strcmp(method, "ums")) { |
| 967 | errno = ENOSYS; |
| 968 | return -1; |
| 969 | } |
| 970 | |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 971 | *avail = massStorageAvailable(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 972 | return 0; |
| 973 | } |
| 974 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 975 | int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) { |
| 976 | Volume *v = lookupVolume(label); |
| 977 | |
| 978 | if (!v) { |
| 979 | errno = ENOENT; |
| 980 | return -1; |
| 981 | } |
| 982 | |
| 983 | if (strcmp(method, "ums")) { |
| 984 | errno = ENOSYS; |
| 985 | return -1; |
| 986 | } |
| 987 | |
| 988 | if (v->getState() != Volume::State_Shared) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 989 | *enabled = false; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 990 | } else { |
| 991 | *enabled = true; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 996 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 997 | |
| 998 | if (!strcmp(cmd, "ums")) { |
| 999 | if (!strcmp(arg, "connect")) { |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 1000 | notifyUmsAvailable(true); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1001 | } else if (!strcmp(arg, "disconnect")) { |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 1002 | notifyUmsAvailable(false); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1003 | } else { |
| 1004 | errno = EINVAL; |
| 1005 | return -1; |
| 1006 | } |
| 1007 | } else { |
| 1008 | errno = EINVAL; |
| 1009 | return -1; |
| 1010 | } |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 1015 | Volume *v = lookupVolume(label); |
| 1016 | |
| 1017 | if (!v) { |
| 1018 | errno = ENOENT; |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Eventually, we'll want to support additional share back-ends, |
| 1024 | * some of which may work while the media is mounted. For now, |
| 1025 | * we just support UMS |
| 1026 | */ |
| 1027 | if (strcmp(method, "ums")) { |
| 1028 | errno = ENOSYS; |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
| 1032 | if (v->getState() == Volume::State_NoMedia) { |
| 1033 | errno = ENODEV; |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1037 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1038 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1039 | errno = EBUSY; |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 1043 | dev_t d = v->getShareDevice(); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1044 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 1045 | // This volume does not support raw disk access |
| 1046 | errno = EINVAL; |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
| 1050 | int fd; |
| 1051 | char nodepath[255]; |
| 1052 | snprintf(nodepath, |
| 1053 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 1054 | MAJOR(d), MINOR(d)); |
| 1055 | |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 1056 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", |
| 1057 | O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1058 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
| 1061 | |
| 1062 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1063 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1064 | close(fd); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | close(fd); |
| 1069 | v->handleVolumeShared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1070 | if (mUmsSharingCount++ == 0) { |
| 1071 | FILE* fp; |
| 1072 | mSavedDirtyRatio = -1; // in case we fail |
| 1073 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1074 | char line[16]; |
| 1075 | if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) { |
| 1076 | fprintf(fp, "%d\n", mUmsDirtyRatio); |
| 1077 | } else { |
| 1078 | SLOGE("Failed to read dirty_ratio (%s)", strerror(errno)); |
| 1079 | } |
| 1080 | fclose(fp); |
| 1081 | } else { |
| 1082 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1083 | } |
| 1084 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 1089 | Volume *v = lookupVolume(label); |
| 1090 | |
| 1091 | if (!v) { |
| 1092 | errno = ENOENT; |
| 1093 | return -1; |
| 1094 | } |
| 1095 | |
| 1096 | if (strcmp(method, "ums")) { |
| 1097 | errno = ENOSYS; |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
| 1101 | if (v->getState() != Volume::State_Shared) { |
| 1102 | errno = EINVAL; |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1106 | int fd; |
San Mehat | 0cde53c | 2009-12-22 08:32:33 -0800 | [diff] [blame] | 1107 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1108 | SLOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | char ch = 0; |
| 1113 | if (write(fd, &ch, 1) < 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1114 | SLOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1115 | close(fd); |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
| 1119 | close(fd); |
| 1120 | v->handleVolumeUnshared(); |
Mike Lockwood | a28056b | 2010-10-28 15:21:24 -0400 | [diff] [blame] | 1121 | if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) { |
| 1122 | FILE* fp; |
| 1123 | if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) { |
| 1124 | fprintf(fp, "%d\n", mSavedDirtyRatio); |
| 1125 | fclose(fp); |
| 1126 | } else { |
| 1127 | SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno)); |
| 1128 | } |
| 1129 | mSavedDirtyRatio = -1; |
| 1130 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1131 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame^] | 1134 | extern "C" int vold_unmountVol(const char *label) { |
| 1135 | VolumeManager *vm = VolumeManager::Instance(); |
| 1136 | return vm->unmountVolume(label, true); |
| 1137 | } |
| 1138 | |
| 1139 | extern "C" int vold_getNumDirectVolumes(void) { |
| 1140 | VolumeManager *vm = VolumeManager::Instance(); |
| 1141 | return vm->getNumDirectVolumes(); |
| 1142 | } |
| 1143 | |
| 1144 | int VolumeManager::getNumDirectVolumes(void) { |
| 1145 | VolumeCollection::iterator i; |
| 1146 | int n=0; |
| 1147 | |
| 1148 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1149 | if ((*i)->getShareDevice() != (dev_t)0) { |
| 1150 | n++; |
| 1151 | } |
| 1152 | } |
| 1153 | return n; |
| 1154 | } |
| 1155 | |
| 1156 | extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) { |
| 1157 | VolumeManager *vm = VolumeManager::Instance(); |
| 1158 | return vm->getDirectVolumeList(vol_list); |
| 1159 | } |
| 1160 | |
| 1161 | int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) { |
| 1162 | VolumeCollection::iterator i; |
| 1163 | int n=0; |
| 1164 | dev_t d; |
| 1165 | |
| 1166 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 1167 | if ((d=(*i)->getShareDevice()) != (dev_t)0) { |
| 1168 | (*i)->getVolInfo(&vol_list[n]); |
| 1169 | snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev), |
| 1170 | "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d)); |
| 1171 | n++; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1178 | int VolumeManager::unmountVolume(const char *label, bool force) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1179 | Volume *v = lookupVolume(label); |
| 1180 | |
| 1181 | if (!v) { |
| 1182 | errno = ENOENT; |
| 1183 | return -1; |
| 1184 | } |
| 1185 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1186 | if (v->getState() == Volume::State_NoMedia) { |
| 1187 | errno = ENODEV; |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1191 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1192 | SLOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1193 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1194 | errno = EBUSY; |
| 1195 | return -1; |
| 1196 | } |
| 1197 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1198 | cleanupAsec(v, force); |
San Mehat | 8870516 | 2010-01-15 09:26:28 -0800 | [diff] [blame] | 1199 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 1200 | return v->unmountVol(force); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1203 | /* |
| 1204 | * Looks up a volume by it's label or mount-point |
| 1205 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1206 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 1207 | VolumeCollection::iterator i; |
| 1208 | |
| 1209 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 1210 | if (label[0] == '/') { |
| 1211 | if (!strcmp(label, (*i)->getMountpoint())) |
| 1212 | return (*i); |
| 1213 | } else { |
| 1214 | if (!strcmp(label, (*i)->getLabel())) |
| 1215 | return (*i); |
| 1216 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1217 | } |
| 1218 | return NULL; |
| 1219 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1220 | |
| 1221 | bool VolumeManager::isMountpointMounted(const char *mp) |
| 1222 | { |
| 1223 | char device[256]; |
| 1224 | char mount_path[256]; |
| 1225 | char rest[256]; |
| 1226 | FILE *fp; |
| 1227 | char line[1024]; |
| 1228 | |
| 1229 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 1230 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | while(fgets(line, sizeof(line), fp)) { |
| 1235 | line[strlen(line)-1] = '\0'; |
| 1236 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 1237 | if (!strcmp(mount_path, mp)) { |
| 1238 | fclose(fp); |
| 1239 | return true; |
| 1240 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | fclose(fp); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1247 | int VolumeManager::cleanupAsec(Volume *v, bool force) { |
| 1248 | while(mActiveContainers->size()) { |
| 1249 | AsecIdCollection::iterator it = mActiveContainers->begin(); |
Kenny Root | cbacf78 | 2010-09-24 15:11:48 -0700 | [diff] [blame] | 1250 | ContainerData* cd = *it; |
| 1251 | SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint()); |
| 1252 | if (cd->type == ASEC) { |
| 1253 | if (unmountAsec(cd->id, force)) { |
| 1254 | SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno)); |
| 1255 | return -1; |
| 1256 | } |
| 1257 | } else if (cd->type == OBB) { |
| 1258 | if (unmountObb(cd->id, force)) { |
| 1259 | SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno)); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | } else { |
| 1263 | SLOGE("Unknown container type %d!", cd->type); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 1264 | return -1; |
| 1265 | } |
| 1266 | } |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |