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