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> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 28 | #include <sys/param.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 29 | |
| 30 | #include <linux/kdev_t.h> |
| 31 | |
| 32 | #include <cutils/properties.h> |
| 33 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 34 | #include <diskconfig/diskconfig.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
| 37 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #define LOG_TAG "Vold" |
| 39 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 40 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | #include <cutils/log.h> |
| 42 | |
| 43 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 44 | #include "VolumeManager.h" |
| 45 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 46 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 47 | #include "Process.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 48 | #include "cryptfs.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 50 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 51 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 52 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 53 | |
| 54 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 55 | * Media directory - stuff that only media_rw user can see |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 56 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 57 | const char *Volume::MEDIA_DIR = "/mnt/media_rw"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 58 | |
| 59 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 60 | * Fuse directory - location where fuse wrapped filesystems go |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 61 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 62 | const char *Volume::FUSE_DIR = "/storage"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 63 | |
| 64 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 65 | * Path to external storage where *only* root can access ASEC image files |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 66 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 67 | const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 68 | |
| 69 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 70 | * Path to internal storage where *only* root can access ASEC image files |
| 71 | */ |
| 72 | const char *Volume::SEC_ASECDIR_INT = "/data/app-asec"; |
| 73 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 74 | * Path to where secure containers are mounted |
| 75 | */ |
| 76 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 77 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 78 | /* |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 79 | * Path to where OBBs are mounted |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 80 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 81 | const char *Volume::LOOPDIR = "/mnt/obb"; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 82 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 83 | static const char *stateToStr(int state) { |
| 84 | if (state == Volume::State_Init) |
| 85 | return "Initializing"; |
| 86 | else if (state == Volume::State_NoMedia) |
| 87 | return "No-Media"; |
| 88 | else if (state == Volume::State_Idle) |
| 89 | return "Idle-Unmounted"; |
| 90 | else if (state == Volume::State_Pending) |
| 91 | return "Pending"; |
| 92 | else if (state == Volume::State_Mounted) |
| 93 | return "Mounted"; |
| 94 | else if (state == Volume::State_Unmounting) |
| 95 | return "Unmounting"; |
| 96 | else if (state == Volume::State_Checking) |
| 97 | return "Checking"; |
| 98 | else if (state == Volume::State_Formatting) |
| 99 | return "Formatting"; |
| 100 | else if (state == Volume::State_Shared) |
| 101 | return "Shared-Unmounted"; |
| 102 | else if (state == Volume::State_SharedMnt) |
| 103 | return "Shared-Mounted"; |
| 104 | else |
| 105 | return "Unknown-Error"; |
| 106 | } |
| 107 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 108 | Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 109 | mVm = vm; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 110 | mDebug = false; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 111 | mLabel = strdup(rec->label); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 112 | mState = Volume::State_Init; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 113 | mFlags = flags; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 114 | mCurrentlyMountedKdev = -1; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 115 | mPartIdx = rec->partnum; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 116 | mRetryMount = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Volume::~Volume() { |
| 120 | free(mLabel); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 123 | void Volume::protectFromAutorunStupidity() { |
| 124 | char filename[255]; |
| 125 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 126 | snprintf(filename, sizeof(filename), "%s/autorun.inf", getMountpoint()); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 127 | if (!access(filename, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 128 | SLOGW("Volume contains an autorun.inf! - removing"); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 129 | /* |
| 130 | * Ensure the filename is all lower-case so |
| 131 | * the process killer can find the inode. |
| 132 | * Probably being paranoid here but meh. |
| 133 | */ |
| 134 | rename(filename, filename); |
| 135 | Process::killProcessesWithOpenFiles(filename, 2); |
| 136 | if (unlink(filename)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 137 | SLOGE("Failed to remove %s (%s)", filename, strerror(errno)); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 142 | void Volume::setDebug(bool enable) { |
| 143 | mDebug = enable; |
| 144 | } |
| 145 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 146 | dev_t Volume::getDiskDevice() { |
| 147 | return MKDEV(0, 0); |
| 148 | }; |
| 149 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 150 | dev_t Volume::getShareDevice() { |
| 151 | return getDiskDevice(); |
| 152 | } |
| 153 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 154 | void Volume::handleVolumeShared() { |
| 155 | } |
| 156 | |
| 157 | void Volume::handleVolumeUnshared() { |
| 158 | } |
| 159 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 160 | int Volume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 161 | errno = ENOSYS; |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 166 | char msg[255]; |
| 167 | int oldState = mState; |
| 168 | |
| 169 | if (oldState == state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 170 | SLOGW("Duplicate state (%d)\n", state); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 174 | if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) { |
| 175 | mRetryMount = false; |
| 176 | } |
| 177 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 178 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 179 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 180 | SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 181 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 182 | snprintf(msg, sizeof(msg), |
| 183 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 184 | getFuseMountpoint(), oldState, stateToStr(oldState), mState, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 185 | stateToStr(mState)); |
| 186 | |
| 187 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 188 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 189 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 190 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 191 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
| 192 | mode_t mode = 0660 | S_IFBLK; |
| 193 | dev_t dev = (major << 8) | minor; |
| 194 | if (mknod(path, mode, dev) < 0) { |
| 195 | if (errno != EEXIST) { |
| 196 | return -1; |
| 197 | } |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 202 | int Volume::formatVol(bool wipe) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 203 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 204 | if (getState() == Volume::State_NoMedia) { |
| 205 | errno = ENODEV; |
| 206 | return -1; |
| 207 | } else if (getState() != Volume::State_Idle) { |
| 208 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 212 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 213 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 214 | setState(Volume::State_Mounted); |
| 215 | // mCurrentlyMountedKdev = XXX |
| 216 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 220 | bool formatEntireDevice = (mPartIdx == -1); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 221 | char devicePath[255]; |
| 222 | dev_t diskNode = getDiskDevice(); |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 223 | dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 224 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 225 | setState(Volume::State_Formatting); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 226 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 227 | int ret = -1; |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 228 | // Only initialize the MBR if we are formatting the entire device |
| 229 | if (formatEntireDevice) { |
| 230 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 231 | MAJOR(diskNode), MINOR(diskNode)); |
| 232 | |
| 233 | if (initializeMbr(devicePath)) { |
| 234 | SLOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 235 | goto err; |
| 236 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 237 | } |
| 238 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 239 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 240 | MAJOR(partNode), MINOR(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 241 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 242 | if (mDebug) { |
| 243 | SLOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
| 244 | } |
| 245 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 246 | if (Fat::format(devicePath, 0, wipe)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 247 | SLOGE("Failed to format (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 248 | goto err; |
| 249 | } |
| 250 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 251 | ret = 0; |
| 252 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 253 | err: |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 254 | setState(Volume::State_Idle); |
| 255 | return ret; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool Volume::isMountpointMounted(const char *path) { |
| 259 | char device[256]; |
| 260 | char mount_path[256]; |
| 261 | char rest[256]; |
| 262 | FILE *fp; |
| 263 | char line[1024]; |
| 264 | |
| 265 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 266 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
| 270 | while(fgets(line, sizeof(line), fp)) { |
| 271 | line[strlen(line)-1] = '\0'; |
| 272 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 273 | if (!strcmp(mount_path, path)) { |
| 274 | fclose(fp); |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | fclose(fp); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | int Volume::mountVol() { |
| 285 | dev_t deviceNodes[4]; |
| 286 | int n, i, rc = 0; |
| 287 | char errmsg[255]; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 288 | |
| 289 | int flags = getFlags(); |
| 290 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 291 | |
| 292 | // TODO: handle "bind" style mounts, for emulated storage |
| 293 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 294 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 295 | char crypto_state[PROPERTY_VALUE_MAX]; |
| 296 | char encrypt_progress[PROPERTY_VALUE_MAX]; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 297 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 298 | property_get("vold.decrypt", decrypt_state, ""); |
| 299 | property_get("vold.encrypt_progress", encrypt_progress, ""); |
| 300 | |
| 301 | /* Don't try to mount the volumes if we have not yet entered the disk password |
| 302 | * or are in the process of encrypting. |
| 303 | */ |
| 304 | if ((getState() == Volume::State_NoMedia) || |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 305 | ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 306 | snprintf(errmsg, sizeof(errmsg), |
| 307 | "Volume %s %s mount failed - no media", |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 308 | getLabel(), getFuseMountpoint()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 309 | mVm->getBroadcaster()->sendBroadcast( |
| 310 | ResponseCode::VolumeMountFailedNoMedia, |
| 311 | errmsg, false); |
| 312 | errno = ENODEV; |
| 313 | return -1; |
| 314 | } else if (getState() != Volume::State_Idle) { |
| 315 | errno = EBUSY; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 316 | if (getState() == Volume::State_Pending) { |
| 317 | mRetryMount = true; |
| 318 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 323 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 324 | setState(Volume::State_Mounted); |
| 325 | // mCurrentlyMountedKdev = XXX |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 330 | if (!n) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 331 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 335 | /* If we're running encrypted, and the volume is marked as encryptable and nonremovable, |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 336 | * and also marked as providing Asec storage, then we need to decrypt |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 337 | * that partition, and update the volume object to point to it's new decrypted |
| 338 | * block device |
| 339 | */ |
| 340 | property_get("ro.crypto.state", crypto_state, ""); |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 341 | if (providesAsec && |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 342 | ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) && |
| 343 | !strcmp(crypto_state, "encrypted") && !isDecrypted()) { |
| 344 | char new_sys_path[MAXPATHLEN]; |
| 345 | char nodepath[256]; |
| 346 | int new_major, new_minor; |
| 347 | |
| 348 | if (n != 1) { |
| 349 | /* We only expect one device node returned when mounting encryptable volumes */ |
| 350 | SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint()); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]), |
| 355 | new_sys_path, sizeof(new_sys_path), |
| 356 | &new_major, &new_minor)) { |
| 357 | SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint()); |
| 358 | return -1; |
| 359 | } |
| 360 | /* We now have the new sysfs path for the decrypted block device, and the |
| 361 | * majore and minor numbers for it. So, create the device, update the |
| 362 | * path to the new sysfs path, and continue. |
| 363 | */ |
| 364 | snprintf(nodepath, |
| 365 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 366 | new_major, new_minor); |
| 367 | if (createDeviceNode(nodepath, new_major, new_minor)) { |
| 368 | SLOGE("Error making device node '%s' (%s)", nodepath, |
| 369 | strerror(errno)); |
| 370 | } |
| 371 | |
| 372 | // Todo: Either create sys filename from nodepath, or pass in bogus path so |
| 373 | // vold ignores state changes on this internal device. |
| 374 | updateDeviceInfo(nodepath, new_major, new_minor); |
| 375 | |
| 376 | /* Get the device nodes again, because they just changed */ |
| 377 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 378 | if (!n) { |
| 379 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 380 | return -1; |
| 381 | } |
| 382 | } |
| 383 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 384 | for (i = 0; i < n; i++) { |
| 385 | char devicePath[255]; |
| 386 | |
| 387 | sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]), |
| 388 | MINOR(deviceNodes[i])); |
| 389 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 390 | SLOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 391 | |
| 392 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 393 | setState(Volume::State_Checking); |
| 394 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 395 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 396 | if (errno == ENODATA) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 397 | SLOGW("%s does not contain a FAT filesystem\n", devicePath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 398 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 399 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 400 | errno = EIO; |
| 401 | /* Badness - abort the mount */ |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 402 | SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 403 | setState(Volume::State_Idle); |
| 404 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 405 | } |
| 406 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 407 | errno = 0; |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 408 | int gid; |
| 409 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 410 | if (Fat::doMount(devicePath, getMountpoint(), false, false, false, |
| 411 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 412 | SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 413 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 414 | } |
| 415 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 416 | protectFromAutorunStupidity(); |
| 417 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 418 | if (providesAsec && mountAsecExternal() != 0) { |
| 419 | SLOGE("Failed to mount secure area (%s)", strerror(errno)); |
| 420 | umount(getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 421 | setState(Volume::State_Idle); |
| 422 | return -1; |
| 423 | } |
| 424 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 425 | char service[64]; |
| 426 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 427 | property_set("ctl.start", service); |
| 428 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 429 | setState(Volume::State_Mounted); |
| 430 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 431 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 432 | } |
| 433 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 434 | SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 435 | setState(Volume::State_Idle); |
| 436 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 437 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 440 | int Volume::mountAsecExternal() { |
| 441 | char legacy_path[PATH_MAX]; |
| 442 | char secure_path[PATH_MAX]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 443 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 444 | snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint()); |
| 445 | snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint()); |
| 446 | |
| 447 | // Recover legacy secure path |
| 448 | if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) { |
| 449 | if (rename(legacy_path, secure_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 450 | SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 454 | if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 458 | if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
| 459 | SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path, |
| 460 | SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 467 | int Volume::doUnmount(const char *path, bool force) { |
| 468 | int retries = 10; |
| 469 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 470 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 471 | SLOGD("Unmounting {%s}, force = %d", path, force); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 472 | } |
| 473 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 474 | while (retries--) { |
| 475 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 476 | SLOGI("%s sucessfully unmounted", path); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | int action = 0; |
| 481 | |
| 482 | if (force) { |
| 483 | if (retries == 1) { |
| 484 | action = 2; // SIGKILL |
| 485 | } else if (retries == 2) { |
| 486 | action = 1; // SIGHUP |
| 487 | } |
| 488 | } |
| 489 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 490 | SLOGW("Failed to unmount %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 491 | path, strerror(errno), retries, action); |
| 492 | |
| 493 | Process::killProcessesWithOpenFiles(path, action); |
| 494 | usleep(1000*1000); |
| 495 | } |
| 496 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 497 | SLOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 498 | return -1; |
| 499 | } |
| 500 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 501 | int Volume::unmountVol(bool force, bool revert) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 502 | int i, rc; |
| 503 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 504 | int flags = getFlags(); |
| 505 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 506 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 507 | if (getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 508 | SLOGE("Volume %s unmount request when not mounted", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 509 | errno = EINVAL; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 510 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 514 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 515 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 516 | char service[64]; |
| 517 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 518 | property_set("ctl.stop", service); |
| 519 | /* Give it a chance to stop. I wish we had a synchronous way to determine this... */ |
| 520 | sleep(1); |
| 521 | |
| 522 | // TODO: determine failure mode if FUSE times out |
| 523 | |
| 524 | if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) { |
| 525 | SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno)); |
| 526 | goto out_mounted; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 529 | /* Now that the fuse daemon is dead, unmount it */ |
| 530 | if (doUnmount(getFuseMountpoint(), force) != 0) { |
| 531 | SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno)); |
| 532 | goto fail_remount_secure; |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 535 | /* Unmount the real sd card */ |
| 536 | if (doUnmount(getMountpoint(), force) != 0) { |
| 537 | SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno)); |
| 538 | goto fail_remount_secure; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 541 | SLOGI("%s unmounted successfully", getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 542 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 543 | /* If this is an encrypted volume, and we've been asked to undo |
| 544 | * the crypto mapping, then revert the dm-crypt mapping, and revert |
| 545 | * the device info to the original values. |
| 546 | */ |
| 547 | if (revert && isDecrypted()) { |
| 548 | cryptfs_revert_volume(getLabel()); |
| 549 | revertDeviceInfo(); |
| 550 | SLOGI("Encrypted volume %s reverted successfully", getMountpoint()); |
| 551 | } |
| 552 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 553 | setState(Volume::State_Idle); |
| 554 | mCurrentlyMountedKdev = -1; |
| 555 | return 0; |
| 556 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 557 | fail_remount_secure: |
| 558 | if (providesAsec && mountAsecExternal() != 0) { |
| 559 | SLOGE("Failed to remount secure area (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 560 | goto out_nomedia; |
| 561 | } |
| 562 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 563 | out_mounted: |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 564 | setState(Volume::State_Mounted); |
| 565 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 566 | |
| 567 | out_nomedia: |
| 568 | setState(Volume::State_NoMedia); |
| 569 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 570 | } |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame^] | 571 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 572 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 573 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 574 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 575 | memset(&dinfo, 0, sizeof(dinfo)); |
| 576 | |
| 577 | if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 578 | SLOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 579 | return -1; |
| 580 | } |
| 581 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 582 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 583 | dinfo.device = strdup(deviceNode); |
| 584 | dinfo.scheme = PART_SCHEME_MBR; |
| 585 | dinfo.sect_size = 512; |
| 586 | dinfo.skip_lba = 2048; |
| 587 | dinfo.num_lba = 0; |
| 588 | dinfo.num_parts = 1; |
| 589 | |
| 590 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 591 | |
| 592 | pinfo->name = strdup("android_sdcard"); |
| 593 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 594 | pinfo->type = PC_PART_TYPE_FAT32; |
| 595 | pinfo->len_kb = -1; |
| 596 | |
| 597 | int rc = apply_disk_config(&dinfo, 0); |
| 598 | |
| 599 | if (rc) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 600 | SLOGE("Failed to apply disk configuration (%d)", rc); |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 601 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 602 | } |
| 603 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 604 | out: |
| 605 | free(pinfo->name); |
| 606 | free(dinfo.device); |
| 607 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 608 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 609 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 610 | } |