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