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