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