blob: b7c73ae9a0ce05db927acf20b8cf87dadb1f37db [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;
San Mehata2677e42009-12-13 10:40:18 -080078 mPendingPartMap = 0;
79 mDiskMajor = -1;
80 mDiskMinor = -1;
81 mDiskNumParts = 0;
82
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070083 if (strcmp(rec->mount_point, "auto") != 0) {
84 ALOGE("Vold managed volumes must have auto mount point; ignoring %s",
85 rec->mount_point);
86 }
87
88 char mount[PATH_MAX];
89
90 snprintf(mount, PATH_MAX, "%s/%s", Volume::MEDIA_DIR, rec->label);
91 mMountpoint = strdup(mount);
92 snprintf(mount, PATH_MAX, "%s/%s", Volume::FUSE_DIR, rec->label);
93 mFuseMountpoint = strdup(mount);
94
San Mehata2677e42009-12-13 10:40:18 -080095 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070096}
97
San Mehatae10b912009-10-12 14:57:05 -070098DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070099 PathCollection::iterator it;
100
101 for (it = mPaths->begin(); it != mPaths->end(); ++it)
Octavian Purdila46c301c2014-05-05 15:13:12 +0300102 delete *it;
San Mehatf1b736b2009-10-10 17:22:08 -0700103 delete mPaths;
104}
105
San Mehatae10b912009-10-12 14:57:05 -0700106int DirectVolume::addPath(const char *path) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300107 mPaths->push_back(new PathInfo(path));
San Mehatf1b736b2009-10-10 17:22:08 -0700108 return 0;
109}
110
San Mehata2677e42009-12-13 10:40:18 -0800111dev_t DirectVolume::getDiskDevice() {
112 return MKDEV(mDiskMajor, mDiskMinor);
113}
114
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400115dev_t DirectVolume::getShareDevice() {
116 if (mPartIdx != -1) {
117 return MKDEV(mDiskMajor, mPartIdx);
118 } else {
119 return MKDEV(mDiskMajor, mDiskMinor);
120 }
121}
122
San Mehata2677e42009-12-13 10:40:18 -0800123void DirectVolume::handleVolumeShared() {
124 setState(Volume::State_Shared);
125}
126
127void DirectVolume::handleVolumeUnshared() {
128 setState(Volume::State_Idle);
129}
130
San Mehatae10b912009-10-12 14:57:05 -0700131int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700132 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700133
San Mehatfd7f5872009-10-12 11:32:47 -0700134 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -0700135 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
Octavian Purdila46c301c2014-05-05 15:13:12 +0300136 if ((*it)->match(dp)) {
San Mehatfd7f5872009-10-12 11:32:47 -0700137 /* We can handle this disk */
138 int action = evt->getAction();
139 const char *devtype = evt->findParam("DEVTYPE");
140
San Mehata2677e42009-12-13 10:40:18 -0800141 if (action == NetlinkEvent::NlActionAdd) {
142 int major = atoi(evt->findParam("MAJOR"));
143 int minor = atoi(evt->findParam("MINOR"));
144 char nodepath[255];
145
146 snprintf(nodepath,
147 sizeof(nodepath), "/dev/block/vold/%d:%d",
148 major, minor);
149 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700150 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800151 strerror(errno));
152 }
153 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700154 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800155 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700156 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800157 }
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100158 /* Send notification iff disk is ready (ie all partitions found) */
159 if (getState() == Volume::State_Idle) {
160 char msg[255];
161
162 snprintf(msg, sizeof(msg),
163 "Volume %s %s disk inserted (%d:%d)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700164 getFuseMountpoint(), mDiskMajor, mDiskMinor);
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100165 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
166 msg, false);
167 }
San Mehata2677e42009-12-13 10:40:18 -0800168 } else if (action == NetlinkEvent::NlActionRemove) {
169 if (!strcmp(devtype, "disk")) {
170 handleDiskRemoved(dp, evt);
171 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700172 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800173 }
174 } else if (action == NetlinkEvent::NlActionChange) {
175 if (!strcmp(devtype, "disk")) {
176 handleDiskChanged(dp, evt);
177 } else {
178 handlePartitionChanged(dp, evt);
179 }
180 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700181 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700182 }
San Mehatfd7f5872009-10-12 11:32:47 -0700183
San Mehatf1b736b2009-10-10 17:22:08 -0700184 return 0;
185 }
186 }
187 errno = ENODEV;
188 return -1;
189}
San Mehatfd7f5872009-10-12 11:32:47 -0700190
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800191void DirectVolume::handleDiskAdded(const char * /*devpath*/,
192 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700193 mDiskMajor = atoi(evt->findParam("MAJOR"));
194 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800195
196 const char *tmp = evt->findParam("NPARTS");
197 if (tmp) {
198 mDiskNumParts = atoi(tmp);
199 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700200 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800201 mDiskNumParts = 1;
202 }
203
San Mehatfd7f5872009-10-12 11:32:47 -0700204 int partmask = 0;
205 int i;
San Mehat59abc3c2009-10-12 14:48:47 -0700206 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -0700207 partmask |= (1 << i);
208 }
209 mPendingPartMap = partmask;
210
211 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800212#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700213 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800214#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700215 setState(Volume::State_Idle);
216 } else {
San Mehata2677e42009-12-13 10:40:18 -0800217#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700218 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
San Mehatfd7f5872009-10-12 11:32:47 -0700219 mDiskNumParts, mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800220#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700221 setState(Volume::State_Pending);
222 }
223}
224
San Mehatae10b912009-10-12 14:57:05 -0700225void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700226 int major = atoi(evt->findParam("MAJOR"));
227 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800228
229 int part_num;
230
231 const char *tmp = evt->findParam("PARTN");
232
233 if (tmp) {
234 part_num = atoi(tmp);
235 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700236 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800237 part_num = 1;
238 }
San Mehat59abc3c2009-10-12 14:48:47 -0700239
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700240 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700241 SLOGE("Invalid 'PARTN' value");
242 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700243 }
244
San Mehat2a5b8ce2010-03-10 12:48:57 -0800245 if (part_num > mDiskNumParts) {
246 mDiskNumParts = part_num;
247 }
248
San Mehatdd9b8e92009-10-21 11:06:52 -0700249 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700250 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700251 return;
252 }
San Mehata2677e42009-12-13 10:40:18 -0800253#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700254 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800255#endif
Bruce Beared11b8332010-07-22 13:23:33 -0700256 if (part_num >= MAX_PARTITIONS) {
257 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
Bruce Beared7660902010-07-22 13:23:33 -0700258 } else {
259 mPartMinors[part_num -1] = minor;
260 }
San Mehat59abc3c2009-10-12 14:48:47 -0700261 mPendingPartMap &= ~(1 << part_num);
Bruce Beared7660902010-07-22 13:23:33 -0700262
San Mehat59abc3c2009-10-12 14:48:47 -0700263 if (!mPendingPartMap) {
San Mehata2677e42009-12-13 10:40:18 -0800264#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700265 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800266#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800267 if (getState() != Volume::State_Formatting) {
268 setState(Volume::State_Idle);
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700269 if (mRetryMount == true) {
270 mRetryMount = false;
271 mountVol();
272 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800273 }
San Mehat59abc3c2009-10-12 14:48:47 -0700274 } else {
San Mehata2677e42009-12-13 10:40:18 -0800275#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700276 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800277#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700278 }
San Mehatfd7f5872009-10-12 11:32:47 -0700279}
280
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800281void DirectVolume::handleDiskChanged(const char * /*devpath*/,
282 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700283 int major = atoi(evt->findParam("MAJOR"));
284 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800285
286 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
287 return;
288 }
289
San Mehat97ac40e2010-03-24 10:24:19 -0700290 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800291 const char *tmp = evt->findParam("NPARTS");
292 if (tmp) {
293 mDiskNumParts = atoi(tmp);
294 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700295 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800296 mDiskNumParts = 1;
297 }
298
San Mehata2677e42009-12-13 10:40:18 -0800299 int partmask = 0;
300 int i;
301 for (i = 1; i <= mDiskNumParts; i++) {
302 partmask |= (1 << i);
303 }
304 mPendingPartMap = partmask;
305
San Mehat2a5b8ce2010-03-10 12:48:57 -0800306 if (getState() != Volume::State_Formatting) {
307 if (mDiskNumParts == 0) {
308 setState(Volume::State_Idle);
309 } else {
310 setState(Volume::State_Pending);
311 }
San Mehata2677e42009-12-13 10:40:18 -0800312 }
San Mehata2677e42009-12-13 10:40:18 -0800313}
314
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800315void DirectVolume::handlePartitionChanged(const char * /*devpath*/,
316 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700317 int major = atoi(evt->findParam("MAJOR"));
318 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700319 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800320}
321
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800322void DirectVolume::handleDiskRemoved(const char * /*devpath*/,
323 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700324 int major = atoi(evt->findParam("MAJOR"));
325 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800326 char msg[255];
Lars Svensson62736612011-04-07 15:17:43 +0200327 bool enabled;
328
Robert Chiras961d20d2014-05-27 10:40:37 +0300329 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
330 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
331 /*
332 * Yikes, our mounted disk is going away!
333 */
334
335 doUnmount(major, minor);
336 } else if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
Lars Svensson62736612011-04-07 15:17:43 +0200337 mVm->unshareVolume(getLabel(), "ums");
338 }
San Mehata2677e42009-12-13 10:40:18 -0800339
San Mehata2677e42009-12-13 10:40:18 -0800340 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700341 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800342 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
343 msg, false);
344 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700345}
346
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800347void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
348 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700349 int major = atoi(evt->findParam("MAJOR"));
350 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800351 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800352 int state;
San Mehata2677e42009-12-13 10:40:18 -0800353
San Mehat97ac40e2010-03-24 10:24:19 -0700354 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800355
356 /*
357 * The framework doesn't need to get notified of
358 * partition removal unless it's mounted. Otherwise
359 * the removal notification will be sent on the Disk
360 * itself
361 */
Ethan75a3e1a2010-07-21 23:07:51 +0800362 state = getState();
363 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800364 return;
365 }
Robert Chiras961d20d2014-05-27 10:40:37 +0300366
San Mehata2677e42009-12-13 10:40:18 -0800367 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
368 /*
369 * Yikes, our mounted partition is going away!
370 */
Robert Chiras961d20d2014-05-27 10:40:37 +0300371 doUnmount(major, minor);
Ethan75a3e1a2010-07-21 23:07:51 +0800372 } else if (state == Volume::State_Shared) {
373 /* removed during mass storage */
374 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
375 getLabel(), major, minor);
376 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
377 msg, false);
378
379 if (mVm->unshareVolume(getLabel(), "ums")) {
380 SLOGE("Failed to unshare volume on bad removal (%s)",
381 strerror(errno));
382 } else {
383 SLOGD("Crisis averted");
384 }
San Mehata2677e42009-12-13 10:40:18 -0800385 }
San Mehatfd7f5872009-10-12 11:32:47 -0700386}
San Mehat49e2bce2009-10-12 16:29:01 -0700387
Robert Chiras961d20d2014-05-27 10:40:37 +0300388void DirectVolume::doUnmount(int major, int minor) {
389 char msg[255];
390 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
391 if (providesAsec && mVm->cleanupAsec(this, true)) {
392 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
393 }
394
395 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
396 getLabel(), getFuseMountpoint(), major, minor);
397 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
398 msg, false);
399
400 if (Volume::unmountVol(true, false)) {
401 SLOGE("Failed to unmount volume on bad removal (%s)",
402 strerror(errno));
403 // XXX: At this point we're screwed for now
404 } else {
405 SLOGD("Crisis averted");
406 }
407}
408
San Mehatdd9b8e92009-10-21 11:06:52 -0700409/*
San Mehata2677e42009-12-13 10:40:18 -0800410 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700411 */
San Mehata2677e42009-12-13 10:40:18 -0800412int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700413
414 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800415 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700416 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800417 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
418 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700419 }
420
San Mehata2677e42009-12-13 10:40:18 -0800421 int i;
422 for (i = 0; i < mDiskNumParts; i++) {
423 if (i == max)
424 break;
425 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
426 }
427 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700428 }
San Mehata2677e42009-12-13 10:40:18 -0800429 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
430 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700431}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700432
433/*
434 * Called from base to update device info,
435 * e.g. When setting up an dm-crypt mapping for the sd card.
436 */
437int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
438{
439 PathCollection::iterator it;
440
441 if (mPartIdx == -1) {
442 SLOGE("Can only change device info on a partition\n");
443 return -1;
444 }
445
446 /*
447 * This is to change the sysfs path associated with a partition, in particular,
448 * for an internal SD card partition that is encrypted. Thus, the list is
449 * expected to be only 1 entry long. Check that and bail if not.
450 */
451 if (mPaths->size() != 1) {
452 SLOGE("Cannot change path if there are more than one for a volume\n");
453 return -1;
454 }
455
456 it = mPaths->begin();
Octavian Purdila46c301c2014-05-05 15:13:12 +0300457 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700458 mPaths->erase(it); /* Remove it from the list */
459 addPath(new_path); /* Put the new path on the list */
460
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700461 /* Save away original info so we can restore it when doing factory reset.
462 * Then, when doing the format, it will format the original device in the
463 * clear, otherwise it just formats the encrypted device which is not
464 * readable when the device boots unencrypted after the reset.
465 */
466 mOrigDiskMajor = mDiskMajor;
467 mOrigDiskMinor = mDiskMinor;
468 mOrigPartIdx = mPartIdx;
469 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
470
Ken Sumrall29d8da82011-05-18 17:20:07 -0700471 mDiskMajor = new_major;
472 mDiskMinor = new_minor;
473 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
474 * partition number. They don't have partitions, they are just virtual block
475 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
476 * dm-crypt device is for the userdata partition, which gets minor number 0, and
477 * it is not managed by vold. So the next device is minor number one, which we
478 * will call partition one.
479 */
480 mPartIdx = new_minor;
481 mPartMinors[new_minor-1] = new_minor;
482
483 mIsDecrypted = 1;
484
485 return 0;
486}
487
488/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700489 * Called from base to revert device info to the way it was before a
490 * crypto mapping was created for it.
491 */
492void DirectVolume::revertDeviceInfo(void)
493{
494 if (mIsDecrypted) {
495 mDiskMajor = mOrigDiskMajor;
496 mDiskMinor = mOrigDiskMinor;
497 mPartIdx = mOrigPartIdx;
498 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
499
500 mIsDecrypted = 0;
501 }
502
503 return;
504}
505
506/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700507 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
508 */
509int DirectVolume::getVolInfo(struct volume_info *v)
510{
511 strcpy(v->label, mLabel);
512 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700513 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700514 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
515
516 return 0;
517}