blob: 64d774437ebbca98bc4a7ca41048c3737d7890c5 [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;
San Mehata2677e42009-12-13 10:40:18 -080083
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070084 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 Mehata2677e42009-12-13 10:40:18 -080096 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070097}
98
San Mehatae10b912009-10-12 14:57:05 -070099DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -0700100 PathCollection::iterator it;
101
102 for (it = mPaths->begin(); it != mPaths->end(); ++it)
Octavian Purdila46c301c2014-05-05 15:13:12 +0300103 delete *it;
San Mehatf1b736b2009-10-10 17:22:08 -0700104 delete mPaths;
105}
106
San Mehatae10b912009-10-12 14:57:05 -0700107int DirectVolume::addPath(const char *path) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300108 mPaths->push_back(new PathInfo(path));
San Mehatf1b736b2009-10-10 17:22:08 -0700109 return 0;
110}
111
San Mehata2677e42009-12-13 10:40:18 -0800112dev_t DirectVolume::getDiskDevice() {
113 return MKDEV(mDiskMajor, mDiskMinor);
114}
115
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400116dev_t DirectVolume::getShareDevice() {
117 if (mPartIdx != -1) {
118 return MKDEV(mDiskMajor, mPartIdx);
119 } else {
120 return MKDEV(mDiskMajor, mDiskMinor);
121 }
122}
123
San Mehata2677e42009-12-13 10:40:18 -0800124void DirectVolume::handleVolumeShared() {
125 setState(Volume::State_Shared);
126}
127
128void DirectVolume::handleVolumeUnshared() {
129 setState(Volume::State_Idle);
130}
131
San Mehatae10b912009-10-12 14:57:05 -0700132int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700133 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700134
San Mehatfd7f5872009-10-12 11:32:47 -0700135 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -0700136 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300137 if ((*it)->match(dp)) {
San Mehatfd7f5872009-10-12 11:32:47 -0700138 /* We can handle this disk */
139 int action = evt->getAction();
140 const char *devtype = evt->findParam("DEVTYPE");
141
San Mehata2677e42009-12-13 10:40:18 -0800142 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 Mehat97ac40e2010-03-24 10:24:19 -0700151 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800152 strerror(errno));
153 }
154 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700155 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800156 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700157 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800158 }
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100159 /* 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 Sharkeyba6ae8d2013-07-15 18:14:25 -0700165 getFuseMountpoint(), mDiskMajor, mDiskMinor);
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100166 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
167 msg, false);
168 }
San Mehata2677e42009-12-13 10:40:18 -0800169 } else if (action == NetlinkEvent::NlActionRemove) {
170 if (!strcmp(devtype, "disk")) {
171 handleDiskRemoved(dp, evt);
172 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700173 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800174 }
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 Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700183 }
San Mehatfd7f5872009-10-12 11:32:47 -0700184
San Mehatf1b736b2009-10-10 17:22:08 -0700185 return 0;
186 }
187 }
188 errno = ENODEV;
189 return -1;
190}
San Mehatfd7f5872009-10-12 11:32:47 -0700191
Mark Salyzyn3e971272014-01-21 13:27:04 -0800192void DirectVolume::handleDiskAdded(const char * /*devpath*/,
193 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700194 mDiskMajor = atoi(evt->findParam("MAJOR"));
195 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800196
197 const char *tmp = evt->findParam("NPARTS");
198 if (tmp) {
199 mDiskNumParts = atoi(tmp);
200 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700201 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800202 mDiskNumParts = 1;
203 }
204
Cylen Yaob31f33b2014-05-12 15:56:14 +0800205 mPendingPartCount = mDiskNumParts;
206 for (int i = 0; i < MAX_PARTITIONS; i++)
207 mPartMinors[i] = -1;
San Mehatfd7f5872009-10-12 11:32:47 -0700208
209 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800210#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700211 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800212#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700213 setState(Volume::State_Idle);
214 } else {
San Mehata2677e42009-12-13 10:40:18 -0800215#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800216 SLOGD("Dv::diskIns - waiting for %d pending partitions", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800217#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700218 setState(Volume::State_Pending);
219 }
220}
221
San Mehatae10b912009-10-12 14:57:05 -0700222void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700223 int major = atoi(evt->findParam("MAJOR"));
224 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800225
226 int part_num;
227
228 const char *tmp = evt->findParam("PARTN");
229
230 if (tmp) {
231 part_num = atoi(tmp);
232 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700233 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800234 part_num = 1;
235 }
San Mehat59abc3c2009-10-12 14:48:47 -0700236
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700237 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700238 SLOGE("Invalid 'PARTN' value");
239 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700240 }
241
San Mehat2a5b8ce2010-03-10 12:48:57 -0800242 if (part_num > mDiskNumParts) {
243 mDiskNumParts = part_num;
244 }
245
San Mehatdd9b8e92009-10-21 11:06:52 -0700246 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700247 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700248 return;
249 }
San Mehata2677e42009-12-13 10:40:18 -0800250#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700251 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800252#endif
Bruce Beared11b8332010-07-22 13:23:33 -0700253 if (part_num >= MAX_PARTITIONS) {
254 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
Bruce Beared7660902010-07-22 13:23:33 -0700255 } else {
Cylen Yaob31f33b2014-05-12 15:56:14 +0800256 if ((mPartMinors[part_num - 1] == -1) && mPendingPartCount)
257 mPendingPartCount--;
Bruce Beared7660902010-07-22 13:23:33 -0700258 mPartMinors[part_num -1] = minor;
259 }
Bruce Beared7660902010-07-22 13:23:33 -0700260
Cylen Yaob31f33b2014-05-12 15:56:14 +0800261 if (!mPendingPartCount) {
San Mehata2677e42009-12-13 10:40:18 -0800262#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700263 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800264#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800265 if (getState() != Volume::State_Formatting) {
266 setState(Volume::State_Idle);
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700267 if (mRetryMount == true) {
268 mRetryMount = false;
269 mountVol();
270 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800271 }
San Mehat59abc3c2009-10-12 14:48:47 -0700272 } else {
San Mehata2677e42009-12-13 10:40:18 -0800273#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800274 SLOGD("Dv:partAdd: pending %d disk", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800275#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700276 }
San Mehatfd7f5872009-10-12 11:32:47 -0700277}
278
Mark Salyzyn3e971272014-01-21 13:27:04 -0800279void DirectVolume::handleDiskChanged(const char * /*devpath*/,
280 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700281 int major = atoi(evt->findParam("MAJOR"));
282 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800283
284 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
285 return;
286 }
287
San Mehat97ac40e2010-03-24 10:24:19 -0700288 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800289 const char *tmp = evt->findParam("NPARTS");
290 if (tmp) {
291 mDiskNumParts = atoi(tmp);
292 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700293 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800294 mDiskNumParts = 1;
295 }
296
Cylen Yaob31f33b2014-05-12 15:56:14 +0800297 mPendingPartCount = mDiskNumParts;
298 for (int i = 0; i < MAX_PARTITIONS; i++)
299 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -0800300
San Mehat2a5b8ce2010-03-10 12:48:57 -0800301 if (getState() != Volume::State_Formatting) {
302 if (mDiskNumParts == 0) {
303 setState(Volume::State_Idle);
304 } else {
305 setState(Volume::State_Pending);
306 }
San Mehata2677e42009-12-13 10:40:18 -0800307 }
San Mehata2677e42009-12-13 10:40:18 -0800308}
309
Mark Salyzyn3e971272014-01-21 13:27:04 -0800310void DirectVolume::handlePartitionChanged(const char * /*devpath*/,
311 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700312 int major = atoi(evt->findParam("MAJOR"));
313 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700314 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800315}
316
Mark Salyzyn3e971272014-01-21 13:27:04 -0800317void DirectVolume::handleDiskRemoved(const char * /*devpath*/,
318 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700319 int major = atoi(evt->findParam("MAJOR"));
320 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800321 char msg[255];
Lars Svensson62736612011-04-07 15:17:43 +0200322 bool enabled;
323
Robert Chiras961d20d2014-05-27 10:40:37 +0300324 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 Svensson62736612011-04-07 15:17:43 +0200332 mVm->unshareVolume(getLabel(), "ums");
333 }
San Mehata2677e42009-12-13 10:40:18 -0800334
San Mehata2677e42009-12-13 10:40:18 -0800335 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700336 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800337 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
338 msg, false);
339 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700340}
341
Mark Salyzyn3e971272014-01-21 13:27:04 -0800342void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
343 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700344 int major = atoi(evt->findParam("MAJOR"));
345 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800346 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800347 int state;
San Mehata2677e42009-12-13 10:40:18 -0800348
San Mehat97ac40e2010-03-24 10:24:19 -0700349 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800350
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 */
Ethan75a3e1a2010-07-21 23:07:51 +0800357 state = getState();
358 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800359 return;
360 }
Robert Chiras961d20d2014-05-27 10:40:37 +0300361
San Mehata2677e42009-12-13 10:40:18 -0800362 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
363 /*
364 * Yikes, our mounted partition is going away!
365 */
Robert Chiras961d20d2014-05-27 10:40:37 +0300366 doUnmount(major, minor);
Ethan75a3e1a2010-07-21 23:07:51 +0800367 } 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 Mehata2677e42009-12-13 10:40:18 -0800380 }
San Mehatfd7f5872009-10-12 11:32:47 -0700381}
San Mehat49e2bce2009-10-12 16:29:01 -0700382
Robert Chiras961d20d2014-05-27 10:40:37 +0300383void 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 Mehatdd9b8e92009-10-21 11:06:52 -0700404/*
San Mehata2677e42009-12-13 10:40:18 -0800405 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700406 */
San Mehata2677e42009-12-13 10:40:18 -0800407int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700408
409 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800410 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700411 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800412 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
413 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700414 }
415
San Mehata2677e42009-12-13 10:40:18 -0800416 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 Mehatdd9b8e92009-10-21 11:06:52 -0700423 }
San Mehata2677e42009-12-13 10:40:18 -0800424 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
425 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700426}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700427
428/*
429 * Called from base to update device info,
430 * e.g. When setting up an dm-crypt mapping for the sd card.
431 */
432int 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 Purdila46c301c2014-05-05 15:13:12 +0300452 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700453 mPaths->erase(it); /* Remove it from the list */
454 addPath(new_path); /* Put the new path on the list */
455
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700456 /* 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 Sumrall29d8da82011-05-18 17:20:07 -0700466 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 Sumrall0b8b5972011-08-31 16:14:23 -0700484 * Called from base to revert device info to the way it was before a
485 * crypto mapping was created for it.
486 */
487void 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 Sumrall29d8da82011-05-18 17:20:07 -0700502 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
503 */
504int DirectVolume::getVolInfo(struct volume_info *v)
505{
506 strcpy(v->label, mLabel);
507 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700508 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700509 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
510
511 return 0;
512}