blob: cfa1e0b36d54ab11470c6a002f00c62f3abfefee [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
324 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
325 mVm->unshareVolume(getLabel(), "ums");
326 }
San Mehata2677e42009-12-13 10:40:18 -0800327
San Mehat97ac40e2010-03-24 10:24:19 -0700328 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800329 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700330 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800331 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
332 msg, false);
333 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700334}
335
Mark Salyzyn3e971272014-01-21 13:27:04 -0800336void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
337 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700338 int major = atoi(evt->findParam("MAJOR"));
339 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800340 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800341 int state;
San Mehata2677e42009-12-13 10:40:18 -0800342
San Mehat97ac40e2010-03-24 10:24:19 -0700343 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800344
345 /*
346 * The framework doesn't need to get notified of
347 * partition removal unless it's mounted. Otherwise
348 * the removal notification will be sent on the Disk
349 * itself
350 */
Ethan75a3e1a2010-07-21 23:07:51 +0800351 state = getState();
352 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800353 return;
354 }
355
356 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
357 /*
358 * Yikes, our mounted partition is going away!
359 */
360
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700361 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
362 if (providesAsec && mVm->cleanupAsec(this, true)) {
363 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
364 }
365
San Mehata2677e42009-12-13 10:40:18 -0800366 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700367 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800368 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
369 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700370
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700371 if (Volume::unmountVol(true, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700372 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800373 strerror(errno));
374 // XXX: At this point we're screwed for now
375 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700376 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800377 }
Ethan75a3e1a2010-07-21 23:07:51 +0800378 } else if (state == Volume::State_Shared) {
379 /* removed during mass storage */
380 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
381 getLabel(), major, minor);
382 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
383 msg, false);
384
385 if (mVm->unshareVolume(getLabel(), "ums")) {
386 SLOGE("Failed to unshare volume on bad removal (%s)",
387 strerror(errno));
388 } else {
389 SLOGD("Crisis averted");
390 }
San Mehata2677e42009-12-13 10:40:18 -0800391 }
San Mehatfd7f5872009-10-12 11:32:47 -0700392}
San Mehat49e2bce2009-10-12 16:29:01 -0700393
San Mehatdd9b8e92009-10-21 11:06:52 -0700394/*
San Mehata2677e42009-12-13 10:40:18 -0800395 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700396 */
San Mehata2677e42009-12-13 10:40:18 -0800397int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700398
399 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800400 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700401 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800402 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
403 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700404 }
405
San Mehata2677e42009-12-13 10:40:18 -0800406 int i;
407 for (i = 0; i < mDiskNumParts; i++) {
408 if (i == max)
409 break;
410 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
411 }
412 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700413 }
San Mehata2677e42009-12-13 10:40:18 -0800414 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
415 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700416}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700417
418/*
419 * Called from base to update device info,
420 * e.g. When setting up an dm-crypt mapping for the sd card.
421 */
422int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
423{
424 PathCollection::iterator it;
425
426 if (mPartIdx == -1) {
427 SLOGE("Can only change device info on a partition\n");
428 return -1;
429 }
430
431 /*
432 * This is to change the sysfs path associated with a partition, in particular,
433 * for an internal SD card partition that is encrypted. Thus, the list is
434 * expected to be only 1 entry long. Check that and bail if not.
435 */
436 if (mPaths->size() != 1) {
437 SLOGE("Cannot change path if there are more than one for a volume\n");
438 return -1;
439 }
440
441 it = mPaths->begin();
Octavian Purdila46c301c2014-05-05 15:13:12 +0300442 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700443 mPaths->erase(it); /* Remove it from the list */
444 addPath(new_path); /* Put the new path on the list */
445
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700446 /* Save away original info so we can restore it when doing factory reset.
447 * Then, when doing the format, it will format the original device in the
448 * clear, otherwise it just formats the encrypted device which is not
449 * readable when the device boots unencrypted after the reset.
450 */
451 mOrigDiskMajor = mDiskMajor;
452 mOrigDiskMinor = mDiskMinor;
453 mOrigPartIdx = mPartIdx;
454 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
455
Ken Sumrall29d8da82011-05-18 17:20:07 -0700456 mDiskMajor = new_major;
457 mDiskMinor = new_minor;
458 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
459 * partition number. They don't have partitions, they are just virtual block
460 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
461 * dm-crypt device is for the userdata partition, which gets minor number 0, and
462 * it is not managed by vold. So the next device is minor number one, which we
463 * will call partition one.
464 */
465 mPartIdx = new_minor;
466 mPartMinors[new_minor-1] = new_minor;
467
468 mIsDecrypted = 1;
469
470 return 0;
471}
472
473/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700474 * Called from base to revert device info to the way it was before a
475 * crypto mapping was created for it.
476 */
477void DirectVolume::revertDeviceInfo(void)
478{
479 if (mIsDecrypted) {
480 mDiskMajor = mOrigDiskMajor;
481 mDiskMinor = mOrigDiskMinor;
482 mPartIdx = mOrigPartIdx;
483 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
484
485 mIsDecrypted = 0;
486 }
487
488 return;
489}
490
491/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700492 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
493 */
494int DirectVolume::getVolInfo(struct volume_info *v)
495{
496 strcpy(v->label, mLabel);
497 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700498 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700499 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
500
501 return 0;
502}