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