2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <linux/kdev_t.h>
24 #define LOG_TAG "DirectVolume"
26 #include <cutils/log.h>
27 #include <sysutils/NetlinkEvent.h>
29 #include "DirectVolume.h"
30 #include "VolumeManager.h"
31 #include "ResponseCode.h"
34 // #define PARTITION_DEBUG
36 DirectVolume::DirectVolume(VolumeManager *vm, const char *label,
37 const char *mount_point, int partIdx) :
38 Volume(vm, label, mount_point) {
41 mPaths = new PathCollection();
42 for (int i = 0; i < MAX_PARTITIONS; i++)
49 setState(Volume::State_NoMedia);
52 DirectVolume::~DirectVolume() {
53 PathCollection::iterator it;
55 for (it = mPaths->begin(); it != mPaths->end(); ++it)
60 int DirectVolume::addPath(const char *path) {
61 mPaths->push_back(strdup(path));
65 void DirectVolume::setFlags(int flags) {
69 dev_t DirectVolume::getDiskDevice() {
70 return MKDEV(mDiskMajor, mDiskMinor);
73 dev_t DirectVolume::getShareDevice() {
75 return MKDEV(mDiskMajor, mPartIdx);
77 return MKDEV(mDiskMajor, mDiskMinor);
81 void DirectVolume::handleVolumeShared() {
82 setState(Volume::State_Shared);
85 void DirectVolume::handleVolumeUnshared() {
86 setState(Volume::State_Idle);
89 int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
90 const char *dp = evt->findParam("DEVPATH");
92 PathCollection::iterator it;
93 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
94 if (!strncmp(dp, *it, strlen(*it))) {
95 /* We can handle this disk */
96 int action = evt->getAction();
97 const char *devtype = evt->findParam("DEVTYPE");
99 if (action == NetlinkEvent::NlActionAdd) {
100 int major = atoi(evt->findParam("MAJOR"));
101 int minor = atoi(evt->findParam("MINOR"));
105 sizeof(nodepath), "/dev/block/vold/%d:%d",
107 if (createDeviceNode(nodepath, major, minor)) {
108 SLOGE("Error making device node '%s' (%s)", nodepath,
111 if (!strcmp(devtype, "disk")) {
112 handleDiskAdded(dp, evt);
114 handlePartitionAdded(dp, evt);
116 /* Send notification iff disk is ready (ie all partitions found) */
117 if (getState() == Volume::State_Idle) {
120 snprintf(msg, sizeof(msg),
121 "Volume %s %s disk inserted (%d:%d)", getLabel(),
122 getMountpoint(), mDiskMajor, mDiskMinor);
123 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
126 } else if (action == NetlinkEvent::NlActionRemove) {
127 if (!strcmp(devtype, "disk")) {
128 handleDiskRemoved(dp, evt);
130 handlePartitionRemoved(dp, evt);
132 } else if (action == NetlinkEvent::NlActionChange) {
133 if (!strcmp(devtype, "disk")) {
134 handleDiskChanged(dp, evt);
136 handlePartitionChanged(dp, evt);
139 SLOGW("Ignoring non add/remove/change event");
149 void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
150 mDiskMajor = atoi(evt->findParam("MAJOR"));
151 mDiskMinor = atoi(evt->findParam("MINOR"));
153 const char *tmp = evt->findParam("NPARTS");
155 mDiskNumParts = atoi(tmp);
157 SLOGW("Kernel block uevent missing 'NPARTS'");
163 for (i = 1; i <= mDiskNumParts; i++) {
164 partmask |= (1 << i);
166 mPendingPartMap = partmask;
168 if (mDiskNumParts == 0) {
169 #ifdef PARTITION_DEBUG
170 SLOGD("Dv::diskIns - No partitions - good to go son!");
172 setState(Volume::State_Idle);
174 #ifdef PARTITION_DEBUG
175 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
176 mDiskNumParts, mPendingPartMap);
178 setState(Volume::State_Pending);
182 void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
183 int major = atoi(evt->findParam("MAJOR"));
184 int minor = atoi(evt->findParam("MINOR"));
188 const char *tmp = evt->findParam("PARTN");
191 part_num = atoi(tmp);
193 SLOGW("Kernel block uevent missing 'PARTN'");
197 if (part_num > MAX_PARTITIONS || part_num < 1) {
198 SLOGE("Invalid 'PARTN' value");
202 if (part_num > mDiskNumParts) {
203 mDiskNumParts = part_num;
206 if (major != mDiskMajor) {
207 SLOGE("Partition '%s' has a different major than its disk!", devpath);
210 #ifdef PARTITION_DEBUG
211 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
213 if (part_num >= MAX_PARTITIONS) {
214 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
216 mPartMinors[part_num -1] = minor;
218 mPendingPartMap &= ~(1 << part_num);
220 if (!mPendingPartMap) {
221 #ifdef PARTITION_DEBUG
222 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
224 if (getState() != Volume::State_Formatting) {
225 setState(Volume::State_Idle);
226 if (mRetryMount == true) {
232 #ifdef PARTITION_DEBUG
233 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
238 void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) {
239 int major = atoi(evt->findParam("MAJOR"));
240 int minor = atoi(evt->findParam("MINOR"));
242 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
246 SLOGI("Volume %s disk has changed", getLabel());
247 const char *tmp = evt->findParam("NPARTS");
249 mDiskNumParts = atoi(tmp);
251 SLOGW("Kernel block uevent missing 'NPARTS'");
257 for (i = 1; i <= mDiskNumParts; i++) {
258 partmask |= (1 << i);
260 mPendingPartMap = partmask;
262 if (getState() != Volume::State_Formatting) {
263 if (mDiskNumParts == 0) {
264 setState(Volume::State_Idle);
266 setState(Volume::State_Pending);
271 void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) {
272 int major = atoi(evt->findParam("MAJOR"));
273 int minor = atoi(evt->findParam("MINOR"));
274 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
277 void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
278 int major = atoi(evt->findParam("MAJOR"));
279 int minor = atoi(evt->findParam("MINOR"));
283 if (mVm->shareEnabled(getLabel(), "ums", &enabled) == 0 && enabled) {
284 mVm->unshareVolume(getLabel(), "ums");
287 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
288 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
289 getLabel(), getMountpoint(), major, minor);
290 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
292 setState(Volume::State_NoMedia);
295 void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
296 int major = atoi(evt->findParam("MAJOR"));
297 int minor = atoi(evt->findParam("MINOR"));
301 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
304 * The framework doesn't need to get notified of
305 * partition removal unless it's mounted. Otherwise
306 * the removal notification will be sent on the Disk
310 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
314 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
316 * Yikes, our mounted partition is going away!
319 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
320 getLabel(), getMountpoint(), major, minor);
321 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
324 if (mVm->cleanupAsec(this, true)) {
325 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
328 if (Volume::unmountVol(true, false)) {
329 SLOGE("Failed to unmount volume on bad removal (%s)",
331 // XXX: At this point we're screwed for now
333 SLOGD("Crisis averted");
335 } else if (state == Volume::State_Shared) {
336 /* removed during mass storage */
337 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
338 getLabel(), major, minor);
339 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
342 if (mVm->unshareVolume(getLabel(), "ums")) {
343 SLOGE("Failed to unshare volume on bad removal (%s)",
346 SLOGD("Crisis averted");
352 * Called from base to get a list of devicenodes for mounting
354 int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
356 if (mPartIdx == -1) {
357 // If the disk has no partitions, try the disk itself
358 if (!mDiskNumParts) {
359 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
364 for (i = 0; i < mDiskNumParts; i++) {
367 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
369 return mDiskNumParts;
371 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
376 * Called from base to update device info,
377 * e.g. When setting up an dm-crypt mapping for the sd card.
379 int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
381 PathCollection::iterator it;
383 if (mPartIdx == -1) {
384 SLOGE("Can only change device info on a partition\n");
389 * This is to change the sysfs path associated with a partition, in particular,
390 * for an internal SD card partition that is encrypted. Thus, the list is
391 * expected to be only 1 entry long. Check that and bail if not.
393 if (mPaths->size() != 1) {
394 SLOGE("Cannot change path if there are more than one for a volume\n");
398 it = mPaths->begin();
399 free(*it); /* Free the string storage */
400 mPaths->erase(it); /* Remove it from the list */
401 addPath(new_path); /* Put the new path on the list */
403 /* Save away original info so we can restore it when doing factory reset.
404 * Then, when doing the format, it will format the original device in the
405 * clear, otherwise it just formats the encrypted device which is not
406 * readable when the device boots unencrypted after the reset.
408 mOrigDiskMajor = mDiskMajor;
409 mOrigDiskMinor = mDiskMinor;
410 mOrigPartIdx = mPartIdx;
411 memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
413 mDiskMajor = new_major;
414 mDiskMinor = new_minor;
415 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
416 * partition number. They don't have partitions, they are just virtual block
417 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
418 * dm-crypt device is for the userdata partition, which gets minor number 0, and
419 * it is not managed by vold. So the next device is minor number one, which we
420 * will call partition one.
422 mPartIdx = new_minor;
423 mPartMinors[new_minor-1] = new_minor;
431 * Called from base to revert device info to the way it was before a
432 * crypto mapping was created for it.
434 void DirectVolume::revertDeviceInfo(void)
437 mDiskMajor = mOrigDiskMajor;
438 mDiskMinor = mOrigDiskMinor;
439 mPartIdx = mOrigPartIdx;
440 memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
449 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
451 int DirectVolume::getVolInfo(struct volume_info *v)
453 strcpy(v->label, mLabel);
454 strcpy(v->mnt_point, mMountpoint);
456 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */