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 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <string.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 27 | #include <sys/mount.h> |
| 28 | |
| 29 | #include <linux/kdev_t.h> |
| 30 | |
| 31 | #include <cutils/properties.h> |
| 32 | |
| 33 | #include "diskmbr.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | |
| 35 | #define LOG_TAG "Vold" |
| 36 | |
| 37 | #include <cutils/log.h> |
| 38 | |
| 39 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 40 | #include "VolumeManager.h" |
| 41 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 42 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 43 | #include "Process.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 44 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 45 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 46 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 47 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 48 | |
| 49 | /* |
| 50 | * Secure directory - stuff that only root can see |
| 51 | */ |
| 52 | const char *Volume::SECDIR = "/mnt/secure"; |
| 53 | |
| 54 | /* |
| 55 | * Secure staging directory - where media is mounted for preparation |
| 56 | */ |
| 57 | const char *Volume::SEC_STGDIR = "/mnt/secure/staging"; |
| 58 | |
| 59 | /* |
| 60 | * Path to the directory on the media which contains publicly accessable |
| 61 | * asec imagefiles. This path will be obscured before the mount is |
| 62 | * exposed to non priviledged users. |
| 63 | */ |
| 64 | const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/android_secure"; |
| 65 | |
| 66 | /* |
| 67 | * Path to where *only* root can access asec imagefiles |
| 68 | */ |
| 69 | const char *Volume::SEC_ASECDIR = "/mnt/secure/asec"; |
| 70 | |
| 71 | /* |
| 72 | * Path to where secure containers are mounted |
| 73 | */ |
| 74 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 75 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 76 | static const char *stateToStr(int state) { |
| 77 | if (state == Volume::State_Init) |
| 78 | return "Initializing"; |
| 79 | else if (state == Volume::State_NoMedia) |
| 80 | return "No-Media"; |
| 81 | else if (state == Volume::State_Idle) |
| 82 | return "Idle-Unmounted"; |
| 83 | else if (state == Volume::State_Pending) |
| 84 | return "Pending"; |
| 85 | else if (state == Volume::State_Mounted) |
| 86 | return "Mounted"; |
| 87 | else if (state == Volume::State_Unmounting) |
| 88 | return "Unmounting"; |
| 89 | else if (state == Volume::State_Checking) |
| 90 | return "Checking"; |
| 91 | else if (state == Volume::State_Formatting) |
| 92 | return "Formatting"; |
| 93 | else if (state == Volume::State_Shared) |
| 94 | return "Shared-Unmounted"; |
| 95 | else if (state == Volume::State_SharedMnt) |
| 96 | return "Shared-Mounted"; |
| 97 | else |
| 98 | return "Unknown-Error"; |
| 99 | } |
| 100 | |
| 101 | Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) { |
| 102 | mVm = vm; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 103 | mLabel = strdup(label); |
| 104 | mMountpoint = strdup(mount_point); |
| 105 | mState = Volume::State_Init; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 106 | mCurrentlyMountedKdev = -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | Volume::~Volume() { |
| 110 | free(mLabel); |
| 111 | free(mMountpoint); |
| 112 | } |
| 113 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 114 | dev_t Volume::getDiskDevice() { |
| 115 | return MKDEV(0, 0); |
| 116 | }; |
| 117 | |
| 118 | void Volume::handleVolumeShared() { |
| 119 | } |
| 120 | |
| 121 | void Volume::handleVolumeUnshared() { |
| 122 | } |
| 123 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 124 | int Volume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 125 | errno = ENOSYS; |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 130 | char msg[255]; |
| 131 | int oldState = mState; |
| 132 | |
| 133 | if (oldState == state) { |
| 134 | LOGW("Duplicate state (%d)\n", state); |
| 135 | return; |
| 136 | } |
| 137 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 138 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 139 | |
| 140 | LOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
| 141 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 142 | snprintf(msg, sizeof(msg), |
| 143 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
| 144 | getMountpoint(), oldState, stateToStr(oldState), mState, |
| 145 | stateToStr(mState)); |
| 146 | |
| 147 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 148 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 149 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 150 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 151 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
| 152 | mode_t mode = 0660 | S_IFBLK; |
| 153 | dev_t dev = (major << 8) | minor; |
| 154 | if (mknod(path, mode, dev) < 0) { |
| 155 | if (errno != EEXIST) { |
| 156 | return -1; |
| 157 | } |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 162 | int Volume::formatVol() { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 163 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 164 | if (getState() == Volume::State_NoMedia) { |
| 165 | errno = ENODEV; |
| 166 | return -1; |
| 167 | } else if (getState() != Volume::State_Idle) { |
| 168 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 172 | if (isMountpointMounted(getMountpoint())) { |
| 173 | LOGW("Volume is idle but appears to be mounted - fixing"); |
| 174 | setState(Volume::State_Mounted); |
| 175 | // mCurrentlyMountedKdev = XXX |
| 176 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 180 | char devicePath[255]; |
| 181 | dev_t diskNode = getDiskDevice(); |
| 182 | dev_t partNode = MKDEV(MAJOR(diskNode), 1); // XXX: Hmmm |
| 183 | |
| 184 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 185 | MAJOR(diskNode), MINOR(diskNode)); |
| 186 | |
San Mehat | 62e5dd2 | 2010-02-06 09:53:22 -0800 | [diff] [blame] | 187 | LOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 188 | |
| 189 | if (initializeMbr(devicePath)) { |
| 190 | LOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 191 | goto err; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 194 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 195 | MAJOR(partNode), MINOR(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 196 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 197 | if (Fat::format(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 198 | LOGE("Failed to format (%s)", strerror(errno)); |
| 199 | goto err; |
| 200 | } |
| 201 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 202 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 203 | err: |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | bool Volume::isMountpointMounted(const char *path) { |
| 208 | char device[256]; |
| 209 | char mount_path[256]; |
| 210 | char rest[256]; |
| 211 | FILE *fp; |
| 212 | char line[1024]; |
| 213 | |
| 214 | if (!(fp = fopen("/proc/mounts", "r"))) { |
| 215 | LOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | while(fgets(line, sizeof(line), fp)) { |
| 220 | line[strlen(line)-1] = '\0'; |
| 221 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 222 | if (!strcmp(mount_path, path)) { |
| 223 | fclose(fp); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |
| 229 | fclose(fp); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | int Volume::mountVol() { |
| 234 | dev_t deviceNodes[4]; |
| 235 | int n, i, rc = 0; |
| 236 | char errmsg[255]; |
| 237 | |
| 238 | if (getState() == Volume::State_NoMedia) { |
| 239 | snprintf(errmsg, sizeof(errmsg), |
| 240 | "Volume %s %s mount failed - no media", |
| 241 | getLabel(), getMountpoint()); |
| 242 | mVm->getBroadcaster()->sendBroadcast( |
| 243 | ResponseCode::VolumeMountFailedNoMedia, |
| 244 | errmsg, false); |
| 245 | errno = ENODEV; |
| 246 | return -1; |
| 247 | } else if (getState() != Volume::State_Idle) { |
| 248 | errno = EBUSY; |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | if (isMountpointMounted(getMountpoint())) { |
| 253 | LOGW("Volume is idle but appears to be mounted - fixing"); |
| 254 | setState(Volume::State_Mounted); |
| 255 | // mCurrentlyMountedKdev = XXX |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 260 | if (!n) { |
| 261 | LOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | for (i = 0; i < n; i++) { |
| 266 | char devicePath[255]; |
| 267 | |
| 268 | sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]), |
| 269 | MINOR(deviceNodes[i])); |
| 270 | |
| 271 | LOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
| 272 | |
| 273 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 274 | setState(Volume::State_Checking); |
| 275 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 276 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 277 | if (errno == ENODATA) { |
| 278 | LOGW("%s does not contain a FAT filesystem\n", devicePath); |
| 279 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 280 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 281 | errno = EIO; |
| 282 | /* Badness - abort the mount */ |
| 283 | LOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
| 284 | setState(Volume::State_Idle); |
| 285 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 286 | } |
| 287 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 288 | /* |
| 289 | * Mount the device on our internal staging mountpoint so we can |
| 290 | * muck with it before exposing it to non priviledged users. |
| 291 | */ |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 292 | errno = 0; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 293 | if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, 1000, 1015, 0702, true)) { |
| 294 | LOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
| 295 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 296 | } |
| 297 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 298 | LOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint()); |
| 299 | |
| 300 | if (createBindMounts()) { |
| 301 | LOGE("Failed to create bindmounts (%s)", strerror(errno)); |
| 302 | umount("/mnt/secure/staging"); |
| 303 | setState(Volume::State_Idle); |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Now that the bindmount trickery is done, atomically move the |
| 309 | * whole subtree to expose it to non priviledged users. |
| 310 | */ |
| 311 | if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) { |
| 312 | LOGE("Failed to move mount (%s)", strerror(errno)); |
| 313 | umount("/mnt/secure/staging"); |
| 314 | setState(Volume::State_Idle); |
| 315 | return -1; |
| 316 | } |
| 317 | setState(Volume::State_Mounted); |
| 318 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 319 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 320 | } |
| 321 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 322 | LOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
| 323 | setState(Volume::State_Idle); |
| 324 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 325 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 326 | } |
| 327 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 328 | int Volume::createBindMounts() { |
| 329 | unsigned long flags; |
| 330 | |
| 331 | /* |
| 332 | * Ensure that /android_secure exists and is a directory |
| 333 | */ |
| 334 | if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) { |
| 335 | if (errno == ENOENT) { |
| 336 | if (mkdir(SEC_STG_SECIMGDIR, 0777)) { |
| 337 | LOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 338 | return -1; |
| 339 | } |
| 340 | } else { |
| 341 | LOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 342 | return -1; |
| 343 | } |
| 344 | } else { |
| 345 | struct stat sbuf; |
| 346 | |
| 347 | if (stat(SEC_STG_SECIMGDIR, &sbuf)) { |
| 348 | LOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 349 | return -1; |
| 350 | } |
| 351 | if (!S_ISDIR(sbuf.st_mode)) { |
| 352 | LOGE("%s is not a directory", SEC_STG_SECIMGDIR); |
| 353 | errno = ENOTDIR; |
| 354 | return -1; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll |
| 360 | * have a root only accessable mountpoint for it. |
| 361 | */ |
| 362 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) { |
| 363 | LOGE("Failed to bind mount points %s -> %s (%s)", |
| 364 | SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno)); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to |
| 370 | * obscure the underlying directory from everybody - sneaky eh? ;) |
| 371 | */ |
| 372 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) { |
| 373 | LOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 374 | umount("/mnt/asec_secure"); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int Volume::doMoveMount(const char *src, const char *dst, bool force) { |
| 382 | unsigned int flags = MS_MOVE; |
| 383 | int retries = 5; |
| 384 | |
| 385 | while(retries--) { |
| 386 | if (!mount(src, dst, "", flags, NULL)) { |
| 387 | LOGD("Moved mount %s -> %s sucessfully", src, dst); |
| 388 | return 0; |
| 389 | } else if (errno != EBUSY) { |
| 390 | LOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno)); |
| 391 | return -1; |
| 392 | } |
| 393 | int action = 0; |
| 394 | |
| 395 | if (force) { |
| 396 | if (retries == 1) { |
| 397 | action = 2; // SIGKILL |
| 398 | } else if (retries == 2) { |
| 399 | action = 1; // SIGHUP |
| 400 | } |
| 401 | } |
| 402 | LOGW("Failed to move %s -> %s (%s, retries %d, action %d)", |
| 403 | src, dst, strerror(errno), retries, action); |
| 404 | Process::killProcessesWithOpenFiles(src, action); |
| 405 | usleep(1000*250); |
| 406 | } |
| 407 | |
| 408 | errno = EBUSY; |
| 409 | LOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno)); |
| 410 | return -1; |
| 411 | } |
| 412 | |
| 413 | int Volume::doUnmount(const char *path, bool force) { |
| 414 | int retries = 10; |
| 415 | |
| 416 | while (retries--) { |
| 417 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
| 418 | LOGI("%s sucessfully unmounted", path); |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int action = 0; |
| 423 | |
| 424 | if (force) { |
| 425 | if (retries == 1) { |
| 426 | action = 2; // SIGKILL |
| 427 | } else if (retries == 2) { |
| 428 | action = 1; // SIGHUP |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | LOGW("Failed to unmount %s (%s, retries %d, action %d)", |
| 433 | path, strerror(errno), retries, action); |
| 434 | |
| 435 | Process::killProcessesWithOpenFiles(path, action); |
| 436 | usleep(1000*1000); |
| 437 | } |
| 438 | errno = EBUSY; |
| 439 | LOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
| 440 | return -1; |
| 441 | } |
| 442 | |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 443 | int Volume::unmountVol(bool force) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 444 | int i, rc; |
| 445 | |
| 446 | if (getState() != Volume::State_Mounted) { |
| 447 | LOGE("Volume %s unmount request when not mounted", getLabel()); |
| 448 | errno = EINVAL; |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 453 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 454 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 455 | /* |
| 456 | * First move the mountpoint back to our internal staging point |
| 457 | * so nobody else can muck with it while we work. |
| 458 | */ |
| 459 | if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) { |
| 460 | LOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno)); |
| 461 | setState(Volume::State_Mounted); |
| 462 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 463 | } |
| 464 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 465 | /* |
| 466 | * Unmount the tmpfs which was obscuring the asec image directory |
| 467 | * from non root users |
| 468 | */ |
| 469 | |
| 470 | if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) { |
| 471 | LOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
| 472 | goto fail_republish; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 473 | } |
| 474 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 475 | /* |
| 476 | * Remove the bindmount we were using to keep a reference to |
| 477 | * the previously obscured directory. |
| 478 | */ |
| 479 | |
| 480 | if (doUnmount(Volume::SEC_ASECDIR, force)) { |
| 481 | LOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno)); |
| 482 | goto fail_remount_tmpfs; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Finally, unmount the actual block device from the staging dir |
| 487 | */ |
| 488 | if (doUnmount(Volume::SEC_STGDIR, force)) { |
| 489 | LOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno)); |
| 490 | goto fail_recreate_bindmount; |
| 491 | } |
| 492 | |
| 493 | LOGI("%s unmounted sucessfully", getMountpoint()); |
| 494 | |
| 495 | setState(Volume::State_Idle); |
| 496 | mCurrentlyMountedKdev = -1; |
| 497 | return 0; |
| 498 | |
| 499 | /* |
| 500 | * Failure handling - try to restore everything back the way it was |
| 501 | */ |
| 502 | fail_recreate_bindmount: |
| 503 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) { |
| 504 | LOGE("Failed to restore bindmount after failure! - Storage will appear offline!"); |
| 505 | goto out_nomedia; |
| 506 | } |
| 507 | fail_remount_tmpfs: |
| 508 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) { |
| 509 | LOGE("Failed to restore tmpfs after failure! - Storage will appear offline!"); |
| 510 | goto out_nomedia; |
| 511 | } |
| 512 | fail_republish: |
| 513 | if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) { |
| 514 | LOGE("Failed to republish mount after failure! - Storage will appear offline!"); |
| 515 | goto out_nomedia; |
| 516 | } |
| 517 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 518 | setState(Volume::State_Mounted); |
| 519 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame^] | 520 | |
| 521 | out_nomedia: |
| 522 | setState(Volume::State_NoMedia); |
| 523 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | int Volume::initializeMbr(const char *deviceNode) { |
| 527 | int fd, rc; |
| 528 | unsigned char block[512]; |
| 529 | struct dos_partition part; |
| 530 | unsigned int nr_sec; |
| 531 | |
| 532 | if ((fd = open(deviceNode, O_RDWR)) < 0) { |
| 533 | LOGE("Error opening disk file (%s)", strerror(errno)); |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | if (ioctl(fd, BLKGETSIZE, &nr_sec)) { |
| 538 | LOGE("Unable to get device size (%s)", strerror(errno)); |
| 539 | close(fd); |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | memset(&part, 0, sizeof(part)); |
| 544 | part.dp_flag = 0x80; |
| 545 | part.dp_typ = 0xc; |
| 546 | part.dp_start = ((1024 * 64) / 512) + 1; |
| 547 | part.dp_size = nr_sec - part.dp_start; |
| 548 | |
| 549 | memset(block, 0, sizeof(block)); |
| 550 | block[0x1fe] = 0x55; |
| 551 | block[0x1ff] = 0xaa; |
| 552 | |
| 553 | dos_partition_enc(block + DOSPARTOFF, &part); |
| 554 | |
| 555 | if (write(fd, block, sizeof(block)) < 0) { |
| 556 | LOGE("Error writing MBR (%s)", strerror(errno)); |
| 557 | close(fd); |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | if (ioctl(fd, BLKRRPART, NULL) < 0) { |
| 562 | LOGE("Error re-reading partition table (%s)", strerror(errno)); |
| 563 | close(fd); |
| 564 | return -1; |
| 565 | } |
| 566 | close(fd); |
| 567 | return 0; |
| 568 | } |