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