blob: fdefeafd2d44541b77a9441ec328991bc6b9366a [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;
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 Salyzyn3e971272014-01-21 13:27:04 -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
Cylen Yaob31f33b2014-05-12 15:56:14 +0800204 mPendingPartCount = mDiskNumParts;
205 for (int i = 0; i < MAX_PARTITIONS; i++)
206 mPartMinors[i] = -1;
San Mehatfd7f5872009-10-12 11:32:47 -0700207
208 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800209#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700210 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800211#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700212 setState(Volume::State_Idle);
213 } else {
San Mehata2677e42009-12-13 10:40:18 -0800214#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800215 SLOGD("Dv::diskIns - waiting for %d pending partitions", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800216#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700217 setState(Volume::State_Pending);
218 }
219}
220
San Mehatae10b912009-10-12 14:57:05 -0700221void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700222 int major = atoi(evt->findParam("MAJOR"));
223 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800224
225 int part_num;
226
227 const char *tmp = evt->findParam("PARTN");
228
229 if (tmp) {
230 part_num = atoi(tmp);
231 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700232 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800233 part_num = 1;
234 }
San Mehat59abc3c2009-10-12 14:48:47 -0700235
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700236 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700237 SLOGE("Invalid 'PARTN' value");
238 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700239 }
240
San Mehat2a5b8ce2010-03-10 12:48:57 -0800241 if (part_num > mDiskNumParts) {
242 mDiskNumParts = part_num;
243 }
244
San Mehatdd9b8e92009-10-21 11:06:52 -0700245 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700246 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700247 return;
248 }
San Mehata2677e42009-12-13 10:40:18 -0800249#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700250 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800251#endif
Bruce Beared11b8332010-07-22 13:23:33 -0700252 if (part_num >= MAX_PARTITIONS) {
253 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
Bruce Beared7660902010-07-22 13:23:33 -0700254 } else {
Cylen Yaob31f33b2014-05-12 15:56:14 +0800255 if ((mPartMinors[part_num - 1] == -1) && mPendingPartCount)
256 mPendingPartCount--;
Bruce Beared7660902010-07-22 13:23:33 -0700257 mPartMinors[part_num -1] = minor;
258 }
Bruce Beared7660902010-07-22 13:23:33 -0700259
Cylen Yaob31f33b2014-05-12 15:56:14 +0800260 if (!mPendingPartCount) {
San Mehata2677e42009-12-13 10:40:18 -0800261#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700262 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800263#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800264 if (getState() != Volume::State_Formatting) {
265 setState(Volume::State_Idle);
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700266 if (mRetryMount == true) {
267 mRetryMount = false;
268 mountVol();
269 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800270 }
San Mehat59abc3c2009-10-12 14:48:47 -0700271 } else {
San Mehata2677e42009-12-13 10:40:18 -0800272#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800273 SLOGD("Dv:partAdd: pending %d disk", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800274#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700275 }
San Mehatfd7f5872009-10-12 11:32:47 -0700276}
277
Mark Salyzyn3e971272014-01-21 13:27:04 -0800278void DirectVolume::handleDiskChanged(const char * /*devpath*/,
279 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700280 int major = atoi(evt->findParam("MAJOR"));
281 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800282
283 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
284 return;
285 }
286
San Mehat97ac40e2010-03-24 10:24:19 -0700287 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800288 const char *tmp = evt->findParam("NPARTS");
289 if (tmp) {
290 mDiskNumParts = atoi(tmp);
291 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700292 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800293 mDiskNumParts = 1;
294 }
295
Cylen Yaob31f33b2014-05-12 15:56:14 +0800296 mPendingPartCount = mDiskNumParts;
297 for (int i = 0; i < MAX_PARTITIONS; i++)
298 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -0800299
San Mehat2a5b8ce2010-03-10 12:48:57 -0800300 if (getState() != Volume::State_Formatting) {
301 if (mDiskNumParts == 0) {
302 setState(Volume::State_Idle);
303 } else {
304 setState(Volume::State_Pending);
305 }
San Mehata2677e42009-12-13 10:40:18 -0800306 }
San Mehata2677e42009-12-13 10:40:18 -0800307}
308
Mark Salyzyn3e971272014-01-21 13:27:04 -0800309void DirectVolume::handlePartitionChanged(const char * /*devpath*/,
310 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700311 int major = atoi(evt->findParam("MAJOR"));
312 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700313 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800314}
315
Mark Salyzyn3e971272014-01-21 13:27:04 -0800316void DirectVolume::handleDiskRemoved(const char * /*devpath*/,
317 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700318 int major = atoi(evt->findParam("MAJOR"));
319 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800320 char msg[255];
Lars Svensson62736612011-04-07 15:17:43 +0200321 bool enabled;
322
323 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
324 mVm->unshareVolume(getLabel(), "ums");
325 }
San Mehata2677e42009-12-13 10:40:18 -0800326
San Mehat97ac40e2010-03-24 10:24:19 -0700327 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800328 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700329 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800330 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
331 msg, false);
332 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700333}
334
Mark Salyzyn3e971272014-01-21 13:27:04 -0800335void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
336 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700337 int major = atoi(evt->findParam("MAJOR"));
338 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800339 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800340 int state;
San Mehata2677e42009-12-13 10:40:18 -0800341
San Mehat97ac40e2010-03-24 10:24:19 -0700342 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800343
344 /*
345 * The framework doesn't need to get notified of
346 * partition removal unless it's mounted. Otherwise
347 * the removal notification will be sent on the Disk
348 * itself
349 */
Ethan75a3e1a2010-07-21 23:07:51 +0800350 state = getState();
351 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800352 return;
353 }
354
355 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
356 /*
357 * Yikes, our mounted partition is going away!
358 */
359
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700360 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
361 if (providesAsec && mVm->cleanupAsec(this, true)) {
362 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
363 }
364
San Mehata2677e42009-12-13 10:40:18 -0800365 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700366 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800367 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
368 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700369
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700370 if (Volume::unmountVol(true, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700371 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800372 strerror(errno));
373 // XXX: At this point we're screwed for now
374 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700375 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800376 }
Ethan75a3e1a2010-07-21 23:07:51 +0800377 } else if (state == Volume::State_Shared) {
378 /* removed during mass storage */
379 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
380 getLabel(), major, minor);
381 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
382 msg, false);
383
384 if (mVm->unshareVolume(getLabel(), "ums")) {
385 SLOGE("Failed to unshare volume on bad removal (%s)",
386 strerror(errno));
387 } else {
388 SLOGD("Crisis averted");
389 }
San Mehata2677e42009-12-13 10:40:18 -0800390 }
San Mehatfd7f5872009-10-12 11:32:47 -0700391}
San Mehat49e2bce2009-10-12 16:29:01 -0700392
San Mehatdd9b8e92009-10-21 11:06:52 -0700393/*
San Mehata2677e42009-12-13 10:40:18 -0800394 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700395 */
San Mehata2677e42009-12-13 10:40:18 -0800396int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700397
398 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800399 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700400 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800401 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
402 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700403 }
404
San Mehata2677e42009-12-13 10:40:18 -0800405 int i;
406 for (i = 0; i < mDiskNumParts; i++) {
407 if (i == max)
408 break;
409 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
410 }
411 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700412 }
San Mehata2677e42009-12-13 10:40:18 -0800413 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
414 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700415}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700416
417/*
418 * Called from base to update device info,
419 * e.g. When setting up an dm-crypt mapping for the sd card.
420 */
421int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
422{
423 PathCollection::iterator it;
424
425 if (mPartIdx == -1) {
426 SLOGE("Can only change device info on a partition\n");
427 return -1;
428 }
429
430 /*
431 * This is to change the sysfs path associated with a partition, in particular,
432 * for an internal SD card partition that is encrypted. Thus, the list is
433 * expected to be only 1 entry long. Check that and bail if not.
434 */
435 if (mPaths->size() != 1) {
436 SLOGE("Cannot change path if there are more than one for a volume\n");
437 return -1;
438 }
439
440 it = mPaths->begin();
Octavian Purdila46c301c2014-05-05 15:13:12 +0300441 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700442 mPaths->erase(it); /* Remove it from the list */
443 addPath(new_path); /* Put the new path on the list */
444
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700445 /* Save away original info so we can restore it when doing factory reset.
446 * Then, when doing the format, it will format the original device in the
447 * clear, otherwise it just formats the encrypted device which is not
448 * readable when the device boots unencrypted after the reset.
449 */
450 mOrigDiskMajor = mDiskMajor;
451 mOrigDiskMinor = mDiskMinor;
452 mOrigPartIdx = mPartIdx;
453 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
454
Ken Sumrall29d8da82011-05-18 17:20:07 -0700455 mDiskMajor = new_major;
456 mDiskMinor = new_minor;
457 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
458 * partition number. They don't have partitions, they are just virtual block
459 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
460 * dm-crypt device is for the userdata partition, which gets minor number 0, and
461 * it is not managed by vold. So the next device is minor number one, which we
462 * will call partition one.
463 */
464 mPartIdx = new_minor;
465 mPartMinors[new_minor-1] = new_minor;
466
467 mIsDecrypted = 1;
468
469 return 0;
470}
471
472/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700473 * Called from base to revert device info to the way it was before a
474 * crypto mapping was created for it.
475 */
476void DirectVolume::revertDeviceInfo(void)
477{
478 if (mIsDecrypted) {
479 mDiskMajor = mOrigDiskMajor;
480 mDiskMinor = mOrigDiskMinor;
481 mPartIdx = mOrigPartIdx;
482 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
483
484 mIsDecrypted = 0;
485 }
486
487 return;
488}
489
490/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700491 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
492 */
493int DirectVolume::getVolInfo(struct volume_info *v)
494{
495 strcpy(v->label, mLabel);
496 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700497 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700498 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
499
500 return 0;
501}