blob: 93ea6e9dd71bcbb88b2926e11f9bb2e0e0035f5f [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Mehatfd7f5872009-10-12 11:32:47 -070018#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070019#include <string.h>
San Mehatfd7f5872009-10-12 11:32:47 -070020#include <errno.h>
Octavian Purdila46c301c2014-05-05 15:13:12 +030021#include <fnmatch.h>
San Mehatf1b736b2009-10-10 17:22:08 -070022
San Mehata2677e42009-12-13 10:40:18 -080023#include <linux/kdev_t.h>
24
25#define LOG_TAG "DirectVolume"
San Mehatf1b736b2009-10-10 17:22:08 -070026
27#include <cutils/log.h>
San Mehatfd7f5872009-10-12 11:32:47 -070028#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070029
San Mehatae10b912009-10-12 14:57:05 -070030#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080031#include "VolumeManager.h"
32#include "ResponseCode.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070033#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070034
San Mehata2677e42009-12-13 10:40:18 -080035// #define PARTITION_DEBUG
36
Octavian Purdila46c301c2014-05-05 15:13:12 +030037PathInfo::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
49PathInfo::~PathInfo()
50{
51 free(pattern);
52}
53
54bool 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 Sharkeyba6ae8d2013-07-15 18:14:25 -070073DirectVolume::DirectVolume(VolumeManager *vm, const fstab_rec* rec, int flags) :
74 Volume(vm, rec, flags) {
San Mehatf1b736b2009-10-10 17:22:08 -070075 mPaths = new PathCollection();
San Mehatdd9b8e92009-10-21 11:06:52 -070076 for (int i = 0; i < MAX_PARTITIONS; i++)
77 mPartMinors[i] = -1;
Cylen Yaob31f33b2014-05-12 15:56:14 +080078 mPendingPartCount = 0;
San Mehata2677e42009-12-13 10:40:18 -080079 mDiskMajor = -1;
80 mDiskMinor = -1;
81 mDiskNumParts = 0;
Cylen Yao33c29d62014-07-22 10:34:49 +080082 mIsDecrypted = 0;
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +053083 mIsValid = true;
San Mehata2677e42009-12-13 10:40:18 -080084
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070085 if (strcmp(rec->mount_point, "auto") != 0) {
86 ALOGE("Vold managed volumes must have auto mount point; ignoring %s",
87 rec->mount_point);
88 }
89
90 char mount[PATH_MAX];
91
92 snprintf(mount, PATH_MAX, "%s/%s", Volume::MEDIA_DIR, rec->label);
93 mMountpoint = strdup(mount);
94 snprintf(mount, PATH_MAX, "%s/%s", Volume::FUSE_DIR, rec->label);
95 mFuseMountpoint = strdup(mount);
96
San Mehata2677e42009-12-13 10:40:18 -080097 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070098}
99
San Mehatae10b912009-10-12 14:57:05 -0700100DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -0700101 PathCollection::iterator it;
102
103 for (it = mPaths->begin(); it != mPaths->end(); ++it)
Octavian Purdila46c301c2014-05-05 15:13:12 +0300104 delete *it;
San Mehatf1b736b2009-10-10 17:22:08 -0700105 delete mPaths;
106}
107
San Mehatae10b912009-10-12 14:57:05 -0700108int DirectVolume::addPath(const char *path) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300109 mPaths->push_back(new PathInfo(path));
San Mehatf1b736b2009-10-10 17:22:08 -0700110 return 0;
111}
112
San Mehata2677e42009-12-13 10:40:18 -0800113dev_t DirectVolume::getDiskDevice() {
114 return MKDEV(mDiskMajor, mDiskMinor);
115}
116
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400117dev_t DirectVolume::getShareDevice() {
118 if (mPartIdx != -1) {
119 return MKDEV(mDiskMajor, mPartIdx);
120 } else {
121 return MKDEV(mDiskMajor, mDiskMinor);
122 }
123}
124
San Mehata2677e42009-12-13 10:40:18 -0800125void DirectVolume::handleVolumeShared() {
126 setState(Volume::State_Shared);
127}
128
129void DirectVolume::handleVolumeUnshared() {
130 setState(Volume::State_Idle);
131}
132
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +0530133int DirectVolume::getUICCVolumeNum(const char *dp) {
134 int mVolNum = -1;
135 if (strstr(dp, ":0:0:0"))
136 mVolNum = 0;
137 else if (strstr(dp, ":0:0:1"))
138 mVolNum = 1;
139
140 return mVolNum;
141}
142
San Mehatae10b912009-10-12 14:57:05 -0700143int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700144 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700145
San Mehatfd7f5872009-10-12 11:32:47 -0700146 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -0700147 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300148 if ((*it)->match(dp)) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +0530149
150 /* Check for UICC prefix in label */
151 if (strstr(getLabel(), "uicc")) {
152 char mLabel[15];
153 int mNum = getUICCVolumeNum(dp);
154 if (mNum < 0) {
155 SLOGE("Invalid uicc volume number");
156 continue;
157 }
158
159 snprintf(mLabel, sizeof(mLabel), "uicc%d", mNum);
160 if (strncmp(getLabel(), mLabel, strlen(mLabel)) != 0)
161 continue;
162 }
163 setValidSysfs(true);
164
San Mehatfd7f5872009-10-12 11:32:47 -0700165 /* We can handle this disk */
166 int action = evt->getAction();
167 const char *devtype = evt->findParam("DEVTYPE");
168
San Mehata2677e42009-12-13 10:40:18 -0800169 if (action == NetlinkEvent::NlActionAdd) {
170 int major = atoi(evt->findParam("MAJOR"));
171 int minor = atoi(evt->findParam("MINOR"));
172 char nodepath[255];
173
174 snprintf(nodepath,
175 sizeof(nodepath), "/dev/block/vold/%d:%d",
176 major, minor);
177 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700178 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800179 strerror(errno));
180 }
181 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700182 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800183 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700184 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800185 }
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100186 /* Send notification iff disk is ready (ie all partitions found) */
187 if (getState() == Volume::State_Idle) {
188 char msg[255];
189
190 snprintf(msg, sizeof(msg),
191 "Volume %s %s disk inserted (%d:%d)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700192 getFuseMountpoint(), mDiskMajor, mDiskMinor);
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100193 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
194 msg, false);
195 }
San Mehata2677e42009-12-13 10:40:18 -0800196 } else if (action == NetlinkEvent::NlActionRemove) {
197 if (!strcmp(devtype, "disk")) {
198 handleDiskRemoved(dp, evt);
199 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700200 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800201 }
202 } else if (action == NetlinkEvent::NlActionChange) {
203 if (!strcmp(devtype, "disk")) {
204 handleDiskChanged(dp, evt);
205 } else {
206 handlePartitionChanged(dp, evt);
207 }
208 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700209 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700210 }
San Mehatfd7f5872009-10-12 11:32:47 -0700211
San Mehatf1b736b2009-10-10 17:22:08 -0700212 return 0;
213 }
214 }
215 errno = ENODEV;
216 return -1;
217}
San Mehatfd7f5872009-10-12 11:32:47 -0700218
Mark Salyzyn3e971272014-01-21 13:27:04 -0800219void DirectVolume::handleDiskAdded(const char * /*devpath*/,
220 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700221 mDiskMajor = atoi(evt->findParam("MAJOR"));
222 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800223
224 const char *tmp = evt->findParam("NPARTS");
225 if (tmp) {
226 mDiskNumParts = atoi(tmp);
227 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700228 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800229 mDiskNumParts = 1;
230 }
231
Cylen Yaob31f33b2014-05-12 15:56:14 +0800232 mPendingPartCount = mDiskNumParts;
233 for (int i = 0; i < MAX_PARTITIONS; i++)
234 mPartMinors[i] = -1;
San Mehatfd7f5872009-10-12 11:32:47 -0700235
236 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800237#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700238 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800239#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700240 setState(Volume::State_Idle);
241 } else {
San Mehata2677e42009-12-13 10:40:18 -0800242#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800243 SLOGD("Dv::diskIns - waiting for %d pending partitions", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800244#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700245 setState(Volume::State_Pending);
246 }
247}
248
San Mehatae10b912009-10-12 14:57:05 -0700249void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700250 int major = atoi(evt->findParam("MAJOR"));
251 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800252
253 int part_num;
254
255 const char *tmp = evt->findParam("PARTN");
256
257 if (tmp) {
258 part_num = atoi(tmp);
259 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700260 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800261 part_num = 1;
262 }
San Mehat59abc3c2009-10-12 14:48:47 -0700263
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700264 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700265 SLOGE("Invalid 'PARTN' value");
266 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700267 }
268
San Mehat2a5b8ce2010-03-10 12:48:57 -0800269 if (part_num > mDiskNumParts) {
270 mDiskNumParts = part_num;
271 }
272
San Mehatdd9b8e92009-10-21 11:06:52 -0700273 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700274 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700275 return;
276 }
San Mehata2677e42009-12-13 10:40:18 -0800277#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700278 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800279#endif
Bruce Beared11b8332010-07-22 13:23:33 -0700280 if (part_num >= MAX_PARTITIONS) {
281 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
Bruce Beared7660902010-07-22 13:23:33 -0700282 } else {
Cylen Yaob31f33b2014-05-12 15:56:14 +0800283 if ((mPartMinors[part_num - 1] == -1) && mPendingPartCount)
284 mPendingPartCount--;
Bruce Beared7660902010-07-22 13:23:33 -0700285 mPartMinors[part_num -1] = minor;
286 }
Bruce Beared7660902010-07-22 13:23:33 -0700287
Cylen Yaob31f33b2014-05-12 15:56:14 +0800288 if (!mPendingPartCount) {
San Mehata2677e42009-12-13 10:40:18 -0800289#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700290 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800291#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800292 if (getState() != Volume::State_Formatting) {
293 setState(Volume::State_Idle);
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700294 if (mRetryMount == true) {
295 mRetryMount = false;
296 mountVol();
297 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800298 }
San Mehat59abc3c2009-10-12 14:48:47 -0700299 } else {
San Mehata2677e42009-12-13 10:40:18 -0800300#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800301 SLOGD("Dv:partAdd: pending %d disk", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800302#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700303 }
San Mehatfd7f5872009-10-12 11:32:47 -0700304}
305
Mark Salyzyn3e971272014-01-21 13:27:04 -0800306void DirectVolume::handleDiskChanged(const char * /*devpath*/,
307 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700308 int major = atoi(evt->findParam("MAJOR"));
309 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800310
311 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
312 return;
313 }
314
San Mehat97ac40e2010-03-24 10:24:19 -0700315 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800316 const char *tmp = evt->findParam("NPARTS");
317 if (tmp) {
318 mDiskNumParts = atoi(tmp);
319 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700320 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800321 mDiskNumParts = 1;
322 }
323
Cylen Yaob31f33b2014-05-12 15:56:14 +0800324 mPendingPartCount = mDiskNumParts;
325 for (int i = 0; i < MAX_PARTITIONS; i++)
326 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -0800327
San Mehat2a5b8ce2010-03-10 12:48:57 -0800328 if (getState() != Volume::State_Formatting) {
329 if (mDiskNumParts == 0) {
330 setState(Volume::State_Idle);
331 } else {
332 setState(Volume::State_Pending);
333 }
San Mehata2677e42009-12-13 10:40:18 -0800334 }
San Mehata2677e42009-12-13 10:40:18 -0800335}
336
Mark Salyzyn3e971272014-01-21 13:27:04 -0800337void DirectVolume::handlePartitionChanged(const char * /*devpath*/,
338 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700339 int major = atoi(evt->findParam("MAJOR"));
340 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700341 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800342}
343
Mark Salyzyn3e971272014-01-21 13:27:04 -0800344void DirectVolume::handleDiskRemoved(const char * /*devpath*/,
345 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700346 int major = atoi(evt->findParam("MAJOR"));
347 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800348 char msg[255];
Lars Svensson62736612011-04-07 15:17:43 +0200349 bool enabled;
350
351 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
352 mVm->unshareVolume(getLabel(), "ums");
353 }
San Mehata2677e42009-12-13 10:40:18 -0800354
San Mehat97ac40e2010-03-24 10:24:19 -0700355 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800356 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700357 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800358 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
359 msg, false);
padarshr0415e232014-08-22 15:17:41 +0530360
361 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
362
363 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
364 if (providesAsec && mVm->cleanupAsec(this, true)) {
365 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
366 }
367
368 if (Volume::unmountVol(true, false)) {
369 SLOGE("Failed to unmount volume on bad removal (%s)",
370 strerror(errno));
371 // XXX: At this point we're screwed for now
372 } else {
373 SLOGD("Crisis averted");
374 }
375 }
376
San Mehata2677e42009-12-13 10:40:18 -0800377 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700378}
379
Mark Salyzyn3e971272014-01-21 13:27:04 -0800380void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
381 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700382 int major = atoi(evt->findParam("MAJOR"));
383 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800384 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800385 int state;
San Mehata2677e42009-12-13 10:40:18 -0800386
San Mehat97ac40e2010-03-24 10:24:19 -0700387 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800388
389 /*
390 * The framework doesn't need to get notified of
391 * partition removal unless it's mounted. Otherwise
392 * the removal notification will be sent on the Disk
393 * itself
394 */
Ethan75a3e1a2010-07-21 23:07:51 +0800395 state = getState();
396 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800397 return;
398 }
399
400 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
401 /*
402 * Yikes, our mounted partition is going away!
403 */
404
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700405 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
406 if (providesAsec && mVm->cleanupAsec(this, true)) {
407 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
408 }
409
San Mehata2677e42009-12-13 10:40:18 -0800410 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700411 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800412 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
413 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700414
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700415 if (Volume::unmountVol(true, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700416 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800417 strerror(errno));
418 // XXX: At this point we're screwed for now
419 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700420 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800421 }
Ethan75a3e1a2010-07-21 23:07:51 +0800422 } else if (state == Volume::State_Shared) {
423 /* removed during mass storage */
424 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
425 getLabel(), major, minor);
426 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
427 msg, false);
428
429 if (mVm->unshareVolume(getLabel(), "ums")) {
430 SLOGE("Failed to unshare volume on bad removal (%s)",
431 strerror(errno));
432 } else {
433 SLOGD("Crisis averted");
434 }
San Mehata2677e42009-12-13 10:40:18 -0800435 }
San Mehatfd7f5872009-10-12 11:32:47 -0700436}
San Mehat49e2bce2009-10-12 16:29:01 -0700437
San Mehatdd9b8e92009-10-21 11:06:52 -0700438/*
San Mehata2677e42009-12-13 10:40:18 -0800439 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700440 */
San Mehata2677e42009-12-13 10:40:18 -0800441int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700442
443 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800444 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700445 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800446 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
447 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700448 }
449
San Mehata2677e42009-12-13 10:40:18 -0800450 int i;
451 for (i = 0; i < mDiskNumParts; i++) {
452 if (i == max)
453 break;
454 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
455 }
456 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700457 }
San Mehata2677e42009-12-13 10:40:18 -0800458 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
459 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700460}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700461
462/*
463 * Called from base to update device info,
464 * e.g. When setting up an dm-crypt mapping for the sd card.
465 */
466int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
467{
468 PathCollection::iterator it;
469
470 if (mPartIdx == -1) {
471 SLOGE("Can only change device info on a partition\n");
472 return -1;
473 }
474
475 /*
476 * This is to change the sysfs path associated with a partition, in particular,
477 * for an internal SD card partition that is encrypted. Thus, the list is
478 * expected to be only 1 entry long. Check that and bail if not.
479 */
480 if (mPaths->size() != 1) {
481 SLOGE("Cannot change path if there are more than one for a volume\n");
482 return -1;
483 }
484
485 it = mPaths->begin();
Octavian Purdila46c301c2014-05-05 15:13:12 +0300486 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700487 mPaths->erase(it); /* Remove it from the list */
488 addPath(new_path); /* Put the new path on the list */
489
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700490 /* Save away original info so we can restore it when doing factory reset.
491 * Then, when doing the format, it will format the original device in the
492 * clear, otherwise it just formats the encrypted device which is not
493 * readable when the device boots unencrypted after the reset.
494 */
495 mOrigDiskMajor = mDiskMajor;
496 mOrigDiskMinor = mDiskMinor;
497 mOrigPartIdx = mPartIdx;
498 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
499
Ken Sumrall29d8da82011-05-18 17:20:07 -0700500 mDiskMajor = new_major;
501 mDiskMinor = new_minor;
502 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
503 * partition number. They don't have partitions, they are just virtual block
504 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
505 * dm-crypt device is for the userdata partition, which gets minor number 0, and
506 * it is not managed by vold. So the next device is minor number one, which we
507 * will call partition one.
508 */
509 mPartIdx = new_minor;
510 mPartMinors[new_minor-1] = new_minor;
511
512 mIsDecrypted = 1;
513
514 return 0;
515}
516
517/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700518 * Called from base to revert device info to the way it was before a
519 * crypto mapping was created for it.
520 */
521void DirectVolume::revertDeviceInfo(void)
522{
523 if (mIsDecrypted) {
524 mDiskMajor = mOrigDiskMajor;
525 mDiskMinor = mOrigDiskMinor;
526 mPartIdx = mOrigPartIdx;
527 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
528
529 mIsDecrypted = 0;
530 }
531
532 return;
533}
534
535/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700536 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
537 */
538int DirectVolume::getVolInfo(struct volume_info *v)
539{
540 strcpy(v->label, mLabel);
541 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700542 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700543 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
544
545 return 0;
546}