blob: 62c0560addc1114582566a35a4946a3fee6382e9 [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>
San Mehatf1b736b2009-10-10 17:22:08 -070021
San Mehata2677e42009-12-13 10:40:18 -080022#include <linux/kdev_t.h>
23
24#define LOG_TAG "DirectVolume"
San Mehatf1b736b2009-10-10 17:22:08 -070025
26#include <cutils/log.h>
San Mehatfd7f5872009-10-12 11:32:47 -070027#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070028
San Mehatae10b912009-10-12 14:57:05 -070029#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080030#include "VolumeManager.h"
31#include "ResponseCode.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070032#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070033
San Mehata2677e42009-12-13 10:40:18 -080034// #define PARTITION_DEBUG
35
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070036DirectVolume::DirectVolume(VolumeManager *vm, const fstab_rec* rec, int flags) :
37 Volume(vm, rec, flags) {
San Mehatf1b736b2009-10-10 17:22:08 -070038 mPaths = new PathCollection();
San Mehatdd9b8e92009-10-21 11:06:52 -070039 for (int i = 0; i < MAX_PARTITIONS; i++)
40 mPartMinors[i] = -1;
Cylen Yaob31f33b2014-05-12 15:56:14 +080041 mPendingPartCount = 0;
San Mehata2677e42009-12-13 10:40:18 -080042 mDiskMajor = -1;
43 mDiskMinor = -1;
44 mDiskNumParts = 0;
45
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070046 if (strcmp(rec->mount_point, "auto") != 0) {
47 ALOGE("Vold managed volumes must have auto mount point; ignoring %s",
48 rec->mount_point);
49 }
50
51 char mount[PATH_MAX];
52
53 snprintf(mount, PATH_MAX, "%s/%s", Volume::MEDIA_DIR, rec->label);
54 mMountpoint = strdup(mount);
55 snprintf(mount, PATH_MAX, "%s/%s", Volume::FUSE_DIR, rec->label);
56 mFuseMountpoint = strdup(mount);
57
San Mehata2677e42009-12-13 10:40:18 -080058 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070059}
60
San Mehatae10b912009-10-12 14:57:05 -070061DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070062 PathCollection::iterator it;
63
64 for (it = mPaths->begin(); it != mPaths->end(); ++it)
65 free(*it);
66 delete mPaths;
67}
68
San Mehatae10b912009-10-12 14:57:05 -070069int DirectVolume::addPath(const char *path) {
San Mehatf1b736b2009-10-10 17:22:08 -070070 mPaths->push_back(strdup(path));
71 return 0;
72}
73
San Mehata2677e42009-12-13 10:40:18 -080074dev_t DirectVolume::getDiskDevice() {
75 return MKDEV(mDiskMajor, mDiskMinor);
76}
77
Mike Lockwood2dfe2972010-09-17 18:50:51 -040078dev_t DirectVolume::getShareDevice() {
79 if (mPartIdx != -1) {
80 return MKDEV(mDiskMajor, mPartIdx);
81 } else {
82 return MKDEV(mDiskMajor, mDiskMinor);
83 }
84}
85
San Mehata2677e42009-12-13 10:40:18 -080086void DirectVolume::handleVolumeShared() {
87 setState(Volume::State_Shared);
88}
89
90void DirectVolume::handleVolumeUnshared() {
91 setState(Volume::State_Idle);
92}
93
San Mehatae10b912009-10-12 14:57:05 -070094int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070095 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070096
San Mehatfd7f5872009-10-12 11:32:47 -070097 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070098 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070099 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -0700100 /* We can handle this disk */
101 int action = evt->getAction();
102 const char *devtype = evt->findParam("DEVTYPE");
103
San Mehata2677e42009-12-13 10:40:18 -0800104 if (action == NetlinkEvent::NlActionAdd) {
105 int major = atoi(evt->findParam("MAJOR"));
106 int minor = atoi(evt->findParam("MINOR"));
107 char nodepath[255];
108
109 snprintf(nodepath,
110 sizeof(nodepath), "/dev/block/vold/%d:%d",
111 major, minor);
112 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700113 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800114 strerror(errno));
115 }
116 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700117 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800118 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700119 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800120 }
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100121 /* Send notification iff disk is ready (ie all partitions found) */
122 if (getState() == Volume::State_Idle) {
123 char msg[255];
124
125 snprintf(msg, sizeof(msg),
126 "Volume %s %s disk inserted (%d:%d)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700127 getFuseMountpoint(), mDiskMajor, mDiskMinor);
Magnus Malmborn3dafc262011-01-19 12:26:52 +0100128 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
129 msg, false);
130 }
San Mehata2677e42009-12-13 10:40:18 -0800131 } else if (action == NetlinkEvent::NlActionRemove) {
132 if (!strcmp(devtype, "disk")) {
133 handleDiskRemoved(dp, evt);
134 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700135 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800136 }
137 } else if (action == NetlinkEvent::NlActionChange) {
138 if (!strcmp(devtype, "disk")) {
139 handleDiskChanged(dp, evt);
140 } else {
141 handlePartitionChanged(dp, evt);
142 }
143 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700144 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700145 }
San Mehatfd7f5872009-10-12 11:32:47 -0700146
San Mehatf1b736b2009-10-10 17:22:08 -0700147 return 0;
148 }
149 }
150 errno = ENODEV;
151 return -1;
152}
San Mehatfd7f5872009-10-12 11:32:47 -0700153
Mark Salyzyn3e971272014-01-21 13:27:04 -0800154void DirectVolume::handleDiskAdded(const char * /*devpath*/,
155 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700156 mDiskMajor = atoi(evt->findParam("MAJOR"));
157 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800158
159 const char *tmp = evt->findParam("NPARTS");
160 if (tmp) {
161 mDiskNumParts = atoi(tmp);
162 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700163 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800164 mDiskNumParts = 1;
165 }
166
Cylen Yaob31f33b2014-05-12 15:56:14 +0800167 mPendingPartCount = mDiskNumParts;
168 for (int i = 0; i < MAX_PARTITIONS; i++)
169 mPartMinors[i] = -1;
San Mehatfd7f5872009-10-12 11:32:47 -0700170
171 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800172#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700173 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800174#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700175 setState(Volume::State_Idle);
176 } else {
San Mehata2677e42009-12-13 10:40:18 -0800177#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800178 SLOGD("Dv::diskIns - waiting for %d pending partitions", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800179#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700180 setState(Volume::State_Pending);
181 }
182}
183
San Mehatae10b912009-10-12 14:57:05 -0700184void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700185 int major = atoi(evt->findParam("MAJOR"));
186 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800187
188 int part_num;
189
190 const char *tmp = evt->findParam("PARTN");
191
192 if (tmp) {
193 part_num = atoi(tmp);
194 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700195 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800196 part_num = 1;
197 }
San Mehat59abc3c2009-10-12 14:48:47 -0700198
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700199 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700200 SLOGE("Invalid 'PARTN' value");
201 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700202 }
203
San Mehat2a5b8ce2010-03-10 12:48:57 -0800204 if (part_num > mDiskNumParts) {
205 mDiskNumParts = part_num;
206 }
207
San Mehatdd9b8e92009-10-21 11:06:52 -0700208 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700209 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700210 return;
211 }
San Mehata2677e42009-12-13 10:40:18 -0800212#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700213 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800214#endif
Bruce Beared11b8332010-07-22 13:23:33 -0700215 if (part_num >= MAX_PARTITIONS) {
216 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
Bruce Beared7660902010-07-22 13:23:33 -0700217 } else {
Cylen Yaob31f33b2014-05-12 15:56:14 +0800218 if ((mPartMinors[part_num - 1] == -1) && mPendingPartCount)
219 mPendingPartCount--;
Bruce Beared7660902010-07-22 13:23:33 -0700220 mPartMinors[part_num -1] = minor;
221 }
Bruce Beared7660902010-07-22 13:23:33 -0700222
Cylen Yaob31f33b2014-05-12 15:56:14 +0800223 if (!mPendingPartCount) {
San Mehata2677e42009-12-13 10:40:18 -0800224#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700225 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800226#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800227 if (getState() != Volume::State_Formatting) {
228 setState(Volume::State_Idle);
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700229 if (mRetryMount == true) {
230 mRetryMount = false;
231 mountVol();
232 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800233 }
San Mehat59abc3c2009-10-12 14:48:47 -0700234 } else {
San Mehata2677e42009-12-13 10:40:18 -0800235#ifdef PARTITION_DEBUG
Cylen Yaob31f33b2014-05-12 15:56:14 +0800236 SLOGD("Dv:partAdd: pending %d disk", mPendingPartCount);
San Mehata2677e42009-12-13 10:40:18 -0800237#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700238 }
San Mehatfd7f5872009-10-12 11:32:47 -0700239}
240
Mark Salyzyn3e971272014-01-21 13:27:04 -0800241void DirectVolume::handleDiskChanged(const char * /*devpath*/,
242 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700243 int major = atoi(evt->findParam("MAJOR"));
244 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800245
246 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
247 return;
248 }
249
San Mehat97ac40e2010-03-24 10:24:19 -0700250 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800251 const char *tmp = evt->findParam("NPARTS");
252 if (tmp) {
253 mDiskNumParts = atoi(tmp);
254 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700255 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800256 mDiskNumParts = 1;
257 }
258
Cylen Yaob31f33b2014-05-12 15:56:14 +0800259 mPendingPartCount = mDiskNumParts;
260 for (int i = 0; i < MAX_PARTITIONS; i++)
261 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -0800262
San Mehat2a5b8ce2010-03-10 12:48:57 -0800263 if (getState() != Volume::State_Formatting) {
264 if (mDiskNumParts == 0) {
265 setState(Volume::State_Idle);
266 } else {
267 setState(Volume::State_Pending);
268 }
San Mehata2677e42009-12-13 10:40:18 -0800269 }
San Mehata2677e42009-12-13 10:40:18 -0800270}
271
Mark Salyzyn3e971272014-01-21 13:27:04 -0800272void DirectVolume::handlePartitionChanged(const char * /*devpath*/,
273 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700274 int major = atoi(evt->findParam("MAJOR"));
275 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700276 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800277}
278
Mark Salyzyn3e971272014-01-21 13:27:04 -0800279void DirectVolume::handleDiskRemoved(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 char msg[255];
Lars Svensson62736612011-04-07 15:17:43 +0200284 bool enabled;
285
286 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
287 mVm->unshareVolume(getLabel(), "ums");
288 }
San Mehata2677e42009-12-13 10:40:18 -0800289
San Mehat97ac40e2010-03-24 10:24:19 -0700290 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800291 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700292 getLabel(), getFuseMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800293 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
294 msg, false);
295 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700296}
297
Mark Salyzyn3e971272014-01-21 13:27:04 -0800298void DirectVolume::handlePartitionRemoved(const char * /*devpath*/,
299 NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700300 int major = atoi(evt->findParam("MAJOR"));
301 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800302 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800303 int state;
San Mehata2677e42009-12-13 10:40:18 -0800304
San Mehat97ac40e2010-03-24 10:24:19 -0700305 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800306
307 /*
308 * The framework doesn't need to get notified of
309 * partition removal unless it's mounted. Otherwise
310 * the removal notification will be sent on the Disk
311 * itself
312 */
Ethan75a3e1a2010-07-21 23:07:51 +0800313 state = getState();
314 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800315 return;
316 }
317
318 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
319 /*
320 * Yikes, our mounted partition is going away!
321 */
322
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700323 bool providesAsec = (getFlags() & VOL_PROVIDES_ASEC) != 0;
324 if (providesAsec && mVm->cleanupAsec(this, true)) {
325 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
326 }
327
San Mehata2677e42009-12-13 10:40:18 -0800328 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%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::VolumeBadRemoval,
331 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700332
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700333 if (Volume::unmountVol(true, false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700334 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800335 strerror(errno));
336 // XXX: At this point we're screwed for now
337 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700338 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800339 }
Ethan75a3e1a2010-07-21 23:07:51 +0800340 } else if (state == Volume::State_Shared) {
341 /* removed during mass storage */
342 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
343 getLabel(), major, minor);
344 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
345 msg, false);
346
347 if (mVm->unshareVolume(getLabel(), "ums")) {
348 SLOGE("Failed to unshare volume on bad removal (%s)",
349 strerror(errno));
350 } else {
351 SLOGD("Crisis averted");
352 }
San Mehata2677e42009-12-13 10:40:18 -0800353 }
San Mehatfd7f5872009-10-12 11:32:47 -0700354}
San Mehat49e2bce2009-10-12 16:29:01 -0700355
San Mehatdd9b8e92009-10-21 11:06:52 -0700356/*
San Mehata2677e42009-12-13 10:40:18 -0800357 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700358 */
San Mehata2677e42009-12-13 10:40:18 -0800359int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700360
361 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800362 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700363 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800364 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
365 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700366 }
367
San Mehata2677e42009-12-13 10:40:18 -0800368 int i;
369 for (i = 0; i < mDiskNumParts; i++) {
370 if (i == max)
371 break;
372 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
373 }
374 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700375 }
San Mehata2677e42009-12-13 10:40:18 -0800376 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
377 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700378}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700379
380/*
381 * Called from base to update device info,
382 * e.g. When setting up an dm-crypt mapping for the sd card.
383 */
384int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
385{
386 PathCollection::iterator it;
387
388 if (mPartIdx == -1) {
389 SLOGE("Can only change device info on a partition\n");
390 return -1;
391 }
392
393 /*
394 * This is to change the sysfs path associated with a partition, in particular,
395 * for an internal SD card partition that is encrypted. Thus, the list is
396 * expected to be only 1 entry long. Check that and bail if not.
397 */
398 if (mPaths->size() != 1) {
399 SLOGE("Cannot change path if there are more than one for a volume\n");
400 return -1;
401 }
402
403 it = mPaths->begin();
404 free(*it); /* Free the string storage */
405 mPaths->erase(it); /* Remove it from the list */
406 addPath(new_path); /* Put the new path on the list */
407
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700408 /* Save away original info so we can restore it when doing factory reset.
409 * Then, when doing the format, it will format the original device in the
410 * clear, otherwise it just formats the encrypted device which is not
411 * readable when the device boots unencrypted after the reset.
412 */
413 mOrigDiskMajor = mDiskMajor;
414 mOrigDiskMinor = mDiskMinor;
415 mOrigPartIdx = mPartIdx;
416 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
417
Ken Sumrall29d8da82011-05-18 17:20:07 -0700418 mDiskMajor = new_major;
419 mDiskMinor = new_minor;
420 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
421 * partition number. They don't have partitions, they are just virtual block
422 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
423 * dm-crypt device is for the userdata partition, which gets minor number 0, and
424 * it is not managed by vold. So the next device is minor number one, which we
425 * will call partition one.
426 */
427 mPartIdx = new_minor;
428 mPartMinors[new_minor-1] = new_minor;
429
430 mIsDecrypted = 1;
431
432 return 0;
433}
434
435/*
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700436 * Called from base to revert device info to the way it was before a
437 * crypto mapping was created for it.
438 */
439void DirectVolume::revertDeviceInfo(void)
440{
441 if (mIsDecrypted) {
442 mDiskMajor = mOrigDiskMajor;
443 mDiskMinor = mOrigDiskMinor;
444 mPartIdx = mOrigPartIdx;
445 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
446
447 mIsDecrypted = 0;
448 }
449
450 return;
451}
452
453/*
Ken Sumrall29d8da82011-05-18 17:20:07 -0700454 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
455 */
456int DirectVolume::getVolInfo(struct volume_info *v)
457{
458 strcpy(v->label, mLabel);
459 strcpy(v->mnt_point, mMountpoint);
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700460 v->flags = getFlags();
Ken Sumrall29d8da82011-05-18 17:20:07 -0700461 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
462
463 return 0;
464}