blob: 9de7aea8b77720218c476d7852fbc55b29f90385 [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
329 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
330 mVm->unshareVolume(getLabel(), "ums");
331 }
San Mehata2677e42009-12-13 10:40:18 -0800332
San Mehat97ac40e2010-03-24 10:24:19 -0700333 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800334 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700335 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800336 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
337 msg, false);
338 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700339}
340
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800341void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
342 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700343 int major = atoi(evt->findParam("MAJOR"));
344 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800345 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800346 int state;
San Mehata2677e42009-12-13 10:40:18 -0800347
San Mehat97ac40e2010-03-24 10:24:19 -0700348 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800349
350 /*
351 * The framework doesn't need to get notified of
352 * partition removal unless it's mounted. Otherwise
353 * the removal notification will be sent on the Disk
354 * itself
355 */
Ethan75a3e1a2010-07-21 23:07:51 +0800356 state = getState();
357 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800358 return;
359 }
360
361 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
362 /*
363 * Yikes, our mounted partition is going away!
364 */
365
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700366 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
367 if (providesAsec && mVm->cleanupAsec(this, true)) {
368 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
369 }
370
San Mehata2677e42009-12-13 10:40:18 -0800371 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700372 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800373 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
374 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700375
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700376 if (Volume::unmountVol(true, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700377 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800378 strerror(errno));
379 // XXX: At this point we're screwed for now
380 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700381 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800382 }
Ethan75a3e1a2010-07-21 23:07:51 +0800383 } else if (state == Volume::State_Shared) {
384 /* removed during mass storage */
385 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
386 getLabel(), major, minor);
387 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
388 msg, false);
389
390 if (mVm->unshareVolume(getLabel(), "ums")) {
391 SLOGE("Failed to unshare volume on bad removal (%s)",
392 strerror(errno));
393 } else {
394 SLOGD("Crisis averted");
395 }
San Mehata2677e42009-12-13 10:40:18 -0800396 }
San Mehatfd7f5872009-10-12 11:32:47 -0700397}
San Mehat49e2bce2009-10-12 16:29:01 -0700398
San Mehatdd9b8e92009-10-21 11:06:52 -0700399/*
San Mehata2677e42009-12-13 10:40:18 -0800400 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700401 */
San Mehata2677e42009-12-13 10:40:18 -0800402int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700403
404 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800405 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700406 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800407 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
408 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700409 }
410
San Mehata2677e42009-12-13 10:40:18 -0800411 int i;
412 for (i = 0; i < mDiskNumParts; i++) {
413 if (i == max)
414 break;
415 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
416 }
417 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700418 }
San Mehata2677e42009-12-13 10:40:18 -0800419 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
420 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700421}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700422
423/*
424 * Called from base to update device info,
425 * e.g. When setting up an dm-crypt mapping for the sd card.
426 */
427int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
428{
429 PathCollection::iterator it;
430
431 if (mPartIdx == -1) {
432 SLOGE("Can only change device info on a partition\n");
433 return -1;
434 }
435
436 /*
437 * This is to change the sysfs path associated with a partition, in particular,
438 * for an internal SD card partition that is encrypted. Thus, the list is
439 * expected to be only 1 entry long. Check that and bail if not.
440 */
441 if (mPaths->size() != 1) {
442 SLOGE("Cannot change path if there are more than one for a volume\n");
443 return -1;
444 }
445
446 it = mPaths->begin();
Octavian Purdila46c301c2014-05-05 15:13:12 +0300447 delete *it; /* Free the string storage */
Ken Sumrall29d8da82011-05-18 17:20:07 -0700448 mPaths->erase(it); /* Remove it from the list */
449 addPath(new_path); /* Put the new path on the list */
450
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700451 /* Save away original info so we can restore it when doing factory reset.
452 * Then, when doing the format, it will format the original device in the
453 * clear, otherwise it just formats the encrypted device which is not
454 * readable when the device boots unencrypted after the reset.
455 */
456 mOrigDiskMajor = mDiskMajor;
457 mOrigDiskMinor = mDiskMinor;
458 mOrigPartIdx = mPartIdx;
459 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
460
Ken Sumrall29d8da82011-05-18 17:20:07 -0700461 mDiskMajor = new_major;
462 mDiskMinor = new_minor;
463 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
464 * partition number. They don't have partitions, they are just virtual block
465 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
466 * dm-crypt device is for the userdata partition, which gets minor number 0, and
467 * it is not managed by vold. So the next device is minor number one, which we
468 * will call partition one.
469 */
470 mPartIdx = new_minor;
471 mPartMinors[new_minor-1] = new_minor;
472
473 mIsDecrypted = 1;
474
475 return 0;
476}
477
478/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700479 * Called from base to revert device info to the way it was before a
480 * crypto mapping was created for it.
481 */
482void DirectVolume::revertDeviceInfo(void)
483{
484 if (mIsDecrypted) {
485 mDiskMajor = mOrigDiskMajor;
486 mDiskMinor = mOrigDiskMinor;
487 mPartIdx = mOrigPartIdx;
488 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
489
490 mIsDecrypted = 0;
491 }
492
493 return;
494}
495
496/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700497 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
498 */
499int DirectVolume::getVolInfo(struct volume_info *v)
500{
501 strcpy(v->label, mLabel);
502 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700503 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700504 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
505
506 return 0;
507}