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> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 19 | #include <string.h> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 21 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 22 | #include <linux/kdev_t.h> |
| 23 | |
| 24 | #define LOG_TAG "DirectVolume" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 25 | |
| 26 | #include <cutils/log.h> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 27 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 28 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 29 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 30 | #include "VolumeManager.h" |
| 31 | #include "ResponseCode.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 32 | #include "cryptfs.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 33 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 34 | // #define PARTITION_DEBUG |
| 35 | |
| 36 | DirectVolume::DirectVolume(VolumeManager *vm, const char *label, |
| 37 | const char *mount_point, int partIdx) : |
| 38 | Volume(vm, label, mount_point) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | mPartIdx = partIdx; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 40 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | mPaths = new PathCollection(); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 42 | for (int i = 0; i < MAX_PARTITIONS; i++) |
| 43 | mPartMinors[i] = -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 44 | mPendingPartMap = 0; |
| 45 | mDiskMajor = -1; |
| 46 | mDiskMinor = -1; |
| 47 | mDiskNumParts = 0; |
| 48 | |
| 49 | setState(Volume::State_NoMedia); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 52 | DirectVolume::~DirectVolume() { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | PathCollection::iterator it; |
| 54 | |
| 55 | for (it = mPaths->begin(); it != mPaths->end(); ++it) |
| 56 | free(*it); |
| 57 | delete mPaths; |
| 58 | } |
| 59 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 60 | int DirectVolume::addPath(const char *path) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | mPaths->push_back(strdup(path)); |
| 62 | return 0; |
| 63 | } |
| 64 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 65 | void DirectVolume::setFlags(int flags) { |
| 66 | mFlags = flags; |
| 67 | } |
| 68 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 69 | dev_t DirectVolume::getDiskDevice() { |
| 70 | return MKDEV(mDiskMajor, mDiskMinor); |
| 71 | } |
| 72 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 73 | dev_t DirectVolume::getShareDevice() { |
| 74 | if (mPartIdx != -1) { |
| 75 | return MKDEV(mDiskMajor, mPartIdx); |
| 76 | } else { |
| 77 | return MKDEV(mDiskMajor, mDiskMinor); |
| 78 | } |
| 79 | } |
| 80 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 81 | void DirectVolume::handleVolumeShared() { |
| 82 | setState(Volume::State_Shared); |
| 83 | } |
| 84 | |
| 85 | void DirectVolume::handleVolumeUnshared() { |
| 86 | setState(Volume::State_Idle); |
| 87 | } |
| 88 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 89 | int DirectVolume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 90 | const char *dp = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 91 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 92 | PathCollection::iterator it; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 93 | for (it = mPaths->begin(); it != mPaths->end(); ++it) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 94 | if (!strncmp(dp, *it, strlen(*it))) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 95 | /* We can handle this disk */ |
| 96 | int action = evt->getAction(); |
| 97 | const char *devtype = evt->findParam("DEVTYPE"); |
| 98 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 99 | if (action == NetlinkEvent::NlActionAdd) { |
| 100 | int major = atoi(evt->findParam("MAJOR")); |
| 101 | int minor = atoi(evt->findParam("MINOR")); |
| 102 | char nodepath[255]; |
| 103 | |
| 104 | snprintf(nodepath, |
| 105 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 106 | major, minor); |
| 107 | if (createDeviceNode(nodepath, major, minor)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 108 | SLOGE("Error making device node '%s' (%s)", nodepath, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 109 | strerror(errno)); |
| 110 | } |
| 111 | if (!strcmp(devtype, "disk")) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 112 | handleDiskAdded(dp, evt); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 113 | } else { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 114 | handlePartitionAdded(dp, evt); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 115 | } |
| 116 | } else if (action == NetlinkEvent::NlActionRemove) { |
| 117 | if (!strcmp(devtype, "disk")) { |
| 118 | handleDiskRemoved(dp, evt); |
| 119 | } else { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 120 | handlePartitionRemoved(dp, evt); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 121 | } |
| 122 | } else if (action == NetlinkEvent::NlActionChange) { |
| 123 | if (!strcmp(devtype, "disk")) { |
| 124 | handleDiskChanged(dp, evt); |
| 125 | } else { |
| 126 | handlePartitionChanged(dp, evt); |
| 127 | } |
| 128 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 129 | SLOGW("Ignoring non add/remove/change event"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 130 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 131 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | errno = ENODEV; |
| 136 | return -1; |
| 137 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 138 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 139 | void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 140 | mDiskMajor = atoi(evt->findParam("MAJOR")); |
| 141 | mDiskMinor = atoi(evt->findParam("MINOR")); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 142 | |
| 143 | const char *tmp = evt->findParam("NPARTS"); |
| 144 | if (tmp) { |
| 145 | mDiskNumParts = atoi(tmp); |
| 146 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 147 | SLOGW("Kernel block uevent missing 'NPARTS'"); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 148 | mDiskNumParts = 1; |
| 149 | } |
| 150 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 151 | char msg[255]; |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 152 | |
| 153 | int partmask = 0; |
| 154 | int i; |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 155 | for (i = 1; i <= mDiskNumParts; i++) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 156 | partmask |= (1 << i); |
| 157 | } |
| 158 | mPendingPartMap = partmask; |
| 159 | |
| 160 | if (mDiskNumParts == 0) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 161 | #ifdef PARTITION_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 162 | SLOGD("Dv::diskIns - No partitions - good to go son!"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 163 | #endif |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 164 | setState(Volume::State_Idle); |
| 165 | } else { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 166 | #ifdef PARTITION_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 167 | SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)", |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 168 | mDiskNumParts, mPendingPartMap); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 169 | #endif |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 170 | setState(Volume::State_Pending); |
| 171 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 172 | |
| 173 | snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)", |
| 174 | getLabel(), getMountpoint(), mDiskMajor, mDiskMinor); |
| 175 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted, |
| 176 | msg, false); |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 177 | } |
| 178 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 179 | void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 180 | int major = atoi(evt->findParam("MAJOR")); |
| 181 | int minor = atoi(evt->findParam("MINOR")); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 182 | |
| 183 | int part_num; |
| 184 | |
| 185 | const char *tmp = evt->findParam("PARTN"); |
| 186 | |
| 187 | if (tmp) { |
| 188 | part_num = atoi(tmp); |
| 189 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 190 | SLOGW("Kernel block uevent missing 'PARTN'"); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 191 | part_num = 1; |
| 192 | } |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 193 | |
Nick Kralevich | f3d3ce5 | 2011-04-18 11:16:13 -0700 | [diff] [blame] | 194 | if (part_num > MAX_PARTITIONS || part_num < 1) { |
Nick Kralevich | cc8e96c | 2011-04-29 16:07:45 -0700 | [diff] [blame] | 195 | SLOGE("Invalid 'PARTN' value"); |
| 196 | return; |
Nick Kralevich | f3d3ce5 | 2011-04-18 11:16:13 -0700 | [diff] [blame] | 197 | } |
| 198 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 199 | if (part_num > mDiskNumParts) { |
| 200 | mDiskNumParts = part_num; |
| 201 | } |
| 202 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 203 | if (major != mDiskMajor) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 204 | SLOGE("Partition '%s' has a different major than its disk!", devpath); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 207 | #ifdef PARTITION_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 208 | SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 209 | #endif |
Bruce Beare | d11b833 | 2010-07-22 13:23:33 -0700 | [diff] [blame] | 210 | if (part_num >= MAX_PARTITIONS) { |
| 211 | SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1); |
Bruce Beare | d766090 | 2010-07-22 13:23:33 -0700 | [diff] [blame] | 212 | } else { |
| 213 | mPartMinors[part_num -1] = minor; |
| 214 | } |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 215 | mPendingPartMap &= ~(1 << part_num); |
Bruce Beare | d766090 | 2010-07-22 13:23:33 -0700 | [diff] [blame] | 216 | |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 217 | if (!mPendingPartMap) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 218 | #ifdef PARTITION_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 219 | SLOGD("Dv:partAdd: Got all partitions - ready to rock!"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 220 | #endif |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 221 | if (getState() != Volume::State_Formatting) { |
| 222 | setState(Volume::State_Idle); |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 223 | if (mRetryMount == true) { |
| 224 | mRetryMount = false; |
| 225 | mountVol(); |
| 226 | } |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 227 | } |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 228 | } else { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 229 | #ifdef PARTITION_DEBUG |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 230 | SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 231 | #endif |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 232 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 233 | } |
| 234 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 235 | void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 236 | int major = atoi(evt->findParam("MAJOR")); |
| 237 | int minor = atoi(evt->findParam("MINOR")); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 238 | |
| 239 | if ((major != mDiskMajor) || (minor != mDiskMinor)) { |
| 240 | return; |
| 241 | } |
| 242 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 243 | SLOGI("Volume %s disk has changed", getLabel()); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 244 | const char *tmp = evt->findParam("NPARTS"); |
| 245 | if (tmp) { |
| 246 | mDiskNumParts = atoi(tmp); |
| 247 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 248 | SLOGW("Kernel block uevent missing 'NPARTS'"); |
San Mehat | 7b8f2db | 2010-01-04 14:03:36 -0800 | [diff] [blame] | 249 | mDiskNumParts = 1; |
| 250 | } |
| 251 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 252 | int partmask = 0; |
| 253 | int i; |
| 254 | for (i = 1; i <= mDiskNumParts; i++) { |
| 255 | partmask |= (1 << i); |
| 256 | } |
| 257 | mPendingPartMap = partmask; |
| 258 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 259 | if (getState() != Volume::State_Formatting) { |
| 260 | if (mDiskNumParts == 0) { |
| 261 | setState(Volume::State_Idle); |
| 262 | } else { |
| 263 | setState(Volume::State_Pending); |
| 264 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 265 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 269 | int major = atoi(evt->findParam("MAJOR")); |
| 270 | int minor = atoi(evt->findParam("MINOR")); |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 271 | SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 272 | } |
| 273 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 274 | void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 275 | int major = atoi(evt->findParam("MAJOR")); |
| 276 | int minor = atoi(evt->findParam("MINOR")); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 277 | char msg[255]; |
Lars Svensson | 6273661 | 2011-04-07 15:17:43 +0200 | [diff] [blame^] | 278 | bool enabled; |
| 279 | |
| 280 | if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) { |
| 281 | mVm->unshareVolume(getLabel(), "ums"); |
| 282 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 283 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 284 | SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 285 | snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)", |
| 286 | getLabel(), getMountpoint(), major, minor); |
| 287 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved, |
| 288 | msg, false); |
| 289 | setState(Volume::State_NoMedia); |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 290 | } |
| 291 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 292 | void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) { |
Kenny Root | da62e7c | 2010-03-24 20:18:00 -0700 | [diff] [blame] | 293 | int major = atoi(evt->findParam("MAJOR")); |
| 294 | int minor = atoi(evt->findParam("MINOR")); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 295 | char msg[255]; |
Ethan | 75a3e1a | 2010-07-21 23:07:51 +0800 | [diff] [blame] | 296 | int state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 297 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 298 | SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * The framework doesn't need to get notified of |
| 302 | * partition removal unless it's mounted. Otherwise |
| 303 | * the removal notification will be sent on the Disk |
| 304 | * itself |
| 305 | */ |
Ethan | 75a3e1a | 2010-07-21 23:07:51 +0800 | [diff] [blame] | 306 | state = getState(); |
| 307 | if (state != Volume::State_Mounted && state != Volume::State_Shared) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
| 311 | if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) { |
| 312 | /* |
| 313 | * Yikes, our mounted partition is going away! |
| 314 | */ |
| 315 | |
| 316 | snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)", |
| 317 | getLabel(), getMountpoint(), major, minor); |
| 318 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval, |
| 319 | msg, false); |
San Mehat | 1a06eda | 2010-04-15 12:58:50 -0700 | [diff] [blame] | 320 | |
| 321 | if (mVm->cleanupAsec(this, true)) { |
| 322 | SLOGE("Failed to cleanup ASEC - unmount will probably fail!"); |
| 323 | } |
| 324 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 325 | if (Volume::unmountVol(true, false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 326 | SLOGE("Failed to unmount volume on bad removal (%s)", |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 327 | strerror(errno)); |
| 328 | // XXX: At this point we're screwed for now |
| 329 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 330 | SLOGD("Crisis averted"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 331 | } |
Ethan | 75a3e1a | 2010-07-21 23:07:51 +0800 | [diff] [blame] | 332 | } else if (state == Volume::State_Shared) { |
| 333 | /* removed during mass storage */ |
| 334 | snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)", |
| 335 | getLabel(), major, minor); |
| 336 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval, |
| 337 | msg, false); |
| 338 | |
| 339 | if (mVm->unshareVolume(getLabel(), "ums")) { |
| 340 | SLOGE("Failed to unshare volume on bad removal (%s)", |
| 341 | strerror(errno)); |
| 342 | } else { |
| 343 | SLOGD("Crisis averted"); |
| 344 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 345 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 346 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 347 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 348 | /* |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 349 | * Called from base to get a list of devicenodes for mounting |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 350 | */ |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 351 | int DirectVolume::getDeviceNodes(dev_t *devs, int max) { |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 352 | |
| 353 | if (mPartIdx == -1) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 354 | // If the disk has no partitions, try the disk itself |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 355 | if (!mDiskNumParts) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 356 | devs[0] = MKDEV(mDiskMajor, mDiskMinor); |
| 357 | return 1; |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 358 | } |
| 359 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 360 | int i; |
| 361 | for (i = 0; i < mDiskNumParts; i++) { |
| 362 | if (i == max) |
| 363 | break; |
| 364 | devs[i] = MKDEV(mDiskMajor, mPartMinors[i]); |
| 365 | } |
| 366 | return mDiskNumParts; |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 367 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 368 | devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]); |
| 369 | return 1; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 370 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Called from base to update device info, |
| 374 | * e.g. When setting up an dm-crypt mapping for the sd card. |
| 375 | */ |
| 376 | int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor) |
| 377 | { |
| 378 | PathCollection::iterator it; |
| 379 | |
| 380 | if (mPartIdx == -1) { |
| 381 | SLOGE("Can only change device info on a partition\n"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * This is to change the sysfs path associated with a partition, in particular, |
| 387 | * for an internal SD card partition that is encrypted. Thus, the list is |
| 388 | * expected to be only 1 entry long. Check that and bail if not. |
| 389 | */ |
| 390 | if (mPaths->size() != 1) { |
| 391 | SLOGE("Cannot change path if there are more than one for a volume\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | it = mPaths->begin(); |
| 396 | free(*it); /* Free the string storage */ |
| 397 | mPaths->erase(it); /* Remove it from the list */ |
| 398 | addPath(new_path); /* Put the new path on the list */ |
| 399 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 400 | /* Save away original info so we can restore it when doing factory reset. |
| 401 | * Then, when doing the format, it will format the original device in the |
| 402 | * clear, otherwise it just formats the encrypted device which is not |
| 403 | * readable when the device boots unencrypted after the reset. |
| 404 | */ |
| 405 | mOrigDiskMajor = mDiskMajor; |
| 406 | mOrigDiskMinor = mDiskMinor; |
| 407 | mOrigPartIdx = mPartIdx; |
| 408 | memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors)); |
| 409 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 410 | mDiskMajor = new_major; |
| 411 | mDiskMinor = new_minor; |
| 412 | /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for |
| 413 | * partition number. They don't have partitions, they are just virtual block |
| 414 | * devices, and minor number 0 is the first dm-crypt device. Luckily the first |
| 415 | * dm-crypt device is for the userdata partition, which gets minor number 0, and |
| 416 | * it is not managed by vold. So the next device is minor number one, which we |
| 417 | * will call partition one. |
| 418 | */ |
| 419 | mPartIdx = new_minor; |
| 420 | mPartMinors[new_minor-1] = new_minor; |
| 421 | |
| 422 | mIsDecrypted = 1; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 428 | * Called from base to revert device info to the way it was before a |
| 429 | * crypto mapping was created for it. |
| 430 | */ |
| 431 | void DirectVolume::revertDeviceInfo(void) |
| 432 | { |
| 433 | if (mIsDecrypted) { |
| 434 | mDiskMajor = mOrigDiskMajor; |
| 435 | mDiskMinor = mOrigDiskMinor; |
| 436 | mPartIdx = mOrigPartIdx; |
| 437 | memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors)); |
| 438 | |
| 439 | mIsDecrypted = 0; |
| 440 | } |
| 441 | |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | /* |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 446 | * Called from base to give cryptfs all the info it needs to encrypt eligible volumes |
| 447 | */ |
| 448 | int DirectVolume::getVolInfo(struct volume_info *v) |
| 449 | { |
| 450 | strcpy(v->label, mLabel); |
| 451 | strcpy(v->mnt_point, mMountpoint); |
| 452 | v->flags=mFlags; |
| 453 | /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */ |
| 454 | |
| 455 | return 0; |
| 456 | } |