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