blob: 7e8ac68cba75a293fce392b7d26d702ff6703ba9 [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
36DirectVolume::DirectVolume(VolumeManager *vm, const char *label,
37 const char *mount_point, int partIdx) :
38 Volume(vm, label, mount_point) {
San Mehatf1b736b2009-10-10 17:22:08 -070039 mPartIdx = partIdx;
San Mehata2677e42009-12-13 10:40:18 -080040
San Mehatf1b736b2009-10-10 17:22:08 -070041 mPaths = new PathCollection();
San Mehatdd9b8e92009-10-21 11:06:52 -070042 for (int i = 0; i < MAX_PARTITIONS; i++)
43 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -080044 mPendingPartMap = 0;
45 mDiskMajor = -1;
46 mDiskMinor = -1;
47 mDiskNumParts = 0;
48
49 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070050}
51
San Mehatae10b912009-10-12 14:57:05 -070052DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070053 PathCollection::iterator it;
54
55 for (it = mPaths->begin(); it != mPaths->end(); ++it)
56 free(*it);
57 delete mPaths;
58}
59
San Mehatae10b912009-10-12 14:57:05 -070060int DirectVolume::addPath(const char *path) {
San Mehatf1b736b2009-10-10 17:22:08 -070061 mPaths->push_back(strdup(path));
62 return 0;
63}
64
Ken Sumrall29d8da82011-05-18 17:20:07 -070065void DirectVolume::setFlags(int flags) {
66 mFlags = flags;
67}
68
San Mehata2677e42009-12-13 10:40:18 -080069dev_t DirectVolume::getDiskDevice() {
70 return MKDEV(mDiskMajor, mDiskMinor);
71}
72
Mike Lockwood2dfe2972010-09-17 18:50:51 -040073dev_t DirectVolume::getShareDevice() {
74 if (mPartIdx != -1) {
75 return MKDEV(mDiskMajor, mPartIdx);
76 } else {
77 return MKDEV(mDiskMajor, mDiskMinor);
78 }
79}
80
San Mehata2677e42009-12-13 10:40:18 -080081void DirectVolume::handleVolumeShared() {
82 setState(Volume::State_Shared);
83}
84
85void DirectVolume::handleVolumeUnshared() {
86 setState(Volume::State_Idle);
87}
88
San Mehatae10b912009-10-12 14:57:05 -070089int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070090 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070091
San Mehatfd7f5872009-10-12 11:32:47 -070092 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070093 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070094 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -070095 /* We can handle this disk */
96 int action = evt->getAction();
97 const char *devtype = evt->findParam("DEVTYPE");
98
San Mehata2677e42009-12-13 10:40:18 -080099 if (action == NetlinkEvent::NlActionAdd) {
100 int major = atoi(evt->findParam("MAJOR"));
101 int minor = atoi(evt->findParam("MINOR"));
102 char nodepath[255];
103
104 snprintf(nodepath,
105 sizeof(nodepath), "/dev/block/vold/%d:%d",
106 major, minor);
107 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700108 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800109 strerror(errno));
110 }
111 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700112 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800113 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700114 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800115 }
116 } else if (action == NetlinkEvent::NlActionRemove) {
117 if (!strcmp(devtype, "disk")) {
118 handleDiskRemoved(dp, evt);
119 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700120 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800121 }
122 } else if (action == NetlinkEvent::NlActionChange) {
123 if (!strcmp(devtype, "disk")) {
124 handleDiskChanged(dp, evt);
125 } else {
126 handlePartitionChanged(dp, evt);
127 }
128 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700129 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700130 }
San Mehatfd7f5872009-10-12 11:32:47 -0700131
San Mehatf1b736b2009-10-10 17:22:08 -0700132 return 0;
133 }
134 }
135 errno = ENODEV;
136 return -1;
137}
San Mehatfd7f5872009-10-12 11:32:47 -0700138
San Mehatae10b912009-10-12 14:57:05 -0700139void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700140 mDiskMajor = atoi(evt->findParam("MAJOR"));
141 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800142
143 const char *tmp = evt->findParam("NPARTS");
144 if (tmp) {
145 mDiskNumParts = atoi(tmp);
146 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700147 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800148 mDiskNumParts = 1;
149 }
150
San Mehata2677e42009-12-13 10:40:18 -0800151 char msg[255];
San Mehatfd7f5872009-10-12 11:32:47 -0700152
153 int partmask = 0;
154 int i;
San Mehat59abc3c2009-10-12 14:48:47 -0700155 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -0700156 partmask |= (1 << i);
157 }
158 mPendingPartMap = partmask;
159
160 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800161#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700162 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800163#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700164 setState(Volume::State_Idle);
165 } else {
San Mehata2677e42009-12-13 10:40:18 -0800166#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700167 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
San Mehatfd7f5872009-10-12 11:32:47 -0700168 mDiskNumParts, mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800169#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700170 setState(Volume::State_Pending);
171 }
San Mehata2677e42009-12-13 10:40:18 -0800172
173 snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)",
174 getLabel(), getMountpoint(), mDiskMajor, mDiskMinor);
175 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
176 msg, false);
San Mehatfd7f5872009-10-12 11:32:47 -0700177}
178
San Mehatae10b912009-10-12 14:57:05 -0700179void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700180 int major = atoi(evt->findParam("MAJOR"));
181 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800182
183 int part_num;
184
185 const char *tmp = evt->findParam("PARTN");
186
187 if (tmp) {
188 part_num = atoi(tmp);
189 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700190 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800191 part_num = 1;
192 }
San Mehat59abc3c2009-10-12 14:48:47 -0700193
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700194 if (part_num > MAX_PARTITIONS || part_num < 1) {
Nick Kralevichcc8e96c2011-04-29 16:07:45 -0700195 SLOGE("Invalid 'PARTN' value");
196 return;
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700197 }
198
San Mehat2a5b8ce2010-03-10 12:48:57 -0800199 if (part_num > mDiskNumParts) {
200 mDiskNumParts = part_num;
201 }
202
San Mehatdd9b8e92009-10-21 11:06:52 -0700203 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700204 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700205 return;
206 }
San Mehata2677e42009-12-13 10:40:18 -0800207#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700208 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800209#endif
Chih-Wei Huangdef8f312010-11-17 18:53:09 +0800210 if (part_num > MAX_PARTITIONS) {
211 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS);
Bruce Beared7660902010-07-22 13:23:33 -0700212 } else {
213 mPartMinors[part_num -1] = minor;
214 }
San Mehat59abc3c2009-10-12 14:48:47 -0700215 mPendingPartMap &= ~(1 << part_num);
Bruce Beared7660902010-07-22 13:23:33 -0700216
San Mehat59abc3c2009-10-12 14:48:47 -0700217 if (!mPendingPartMap) {
San Mehata2677e42009-12-13 10:40:18 -0800218#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700219 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800220#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800221 if (getState() != Volume::State_Formatting) {
222 setState(Volume::State_Idle);
223 }
San Mehat59abc3c2009-10-12 14:48:47 -0700224 } else {
San Mehata2677e42009-12-13 10:40:18 -0800225#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700226 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800227#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700228 }
San Mehatfd7f5872009-10-12 11:32:47 -0700229}
230
San Mehata2677e42009-12-13 10:40:18 -0800231void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700232 int major = atoi(evt->findParam("MAJOR"));
233 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800234
235 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
236 return;
237 }
238
San Mehat97ac40e2010-03-24 10:24:19 -0700239 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800240 const char *tmp = evt->findParam("NPARTS");
241 if (tmp) {
242 mDiskNumParts = atoi(tmp);
243 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700244 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800245 mDiskNumParts = 1;
246 }
247
San Mehata2677e42009-12-13 10:40:18 -0800248 int partmask = 0;
249 int i;
250 for (i = 1; i <= mDiskNumParts; i++) {
251 partmask |= (1 << i);
252 }
253 mPendingPartMap = partmask;
254
San Mehat2a5b8ce2010-03-10 12:48:57 -0800255 if (getState() != Volume::State_Formatting) {
256 if (mDiskNumParts == 0) {
257 setState(Volume::State_Idle);
258 } else {
259 setState(Volume::State_Pending);
260 }
San Mehata2677e42009-12-13 10:40:18 -0800261 }
San Mehata2677e42009-12-13 10:40:18 -0800262}
263
264void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700265 int major = atoi(evt->findParam("MAJOR"));
266 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700267 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800268}
269
San Mehatae10b912009-10-12 14:57:05 -0700270void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700271 int major = atoi(evt->findParam("MAJOR"));
272 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800273 char msg[255];
274
San Mehat97ac40e2010-03-24 10:24:19 -0700275 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800276 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
277 getLabel(), getMountpoint(), major, minor);
278 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
279 msg, false);
280 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700281}
282
San Mehatae10b912009-10-12 14:57:05 -0700283void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700284 int major = atoi(evt->findParam("MAJOR"));
285 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800286 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800287 int state;
San Mehata2677e42009-12-13 10:40:18 -0800288
San Mehat97ac40e2010-03-24 10:24:19 -0700289 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800290
291 /*
292 * The framework doesn't need to get notified of
293 * partition removal unless it's mounted. Otherwise
294 * the removal notification will be sent on the Disk
295 * itself
296 */
Ethan75a3e1a2010-07-21 23:07:51 +0800297 state = getState();
298 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800299 return;
300 }
301
302 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
303 /*
304 * Yikes, our mounted partition is going away!
305 */
306
307 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
308 getLabel(), getMountpoint(), major, minor);
309 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
310 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700311
312 if (mVm->cleanupAsec(this, true)) {
313 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
314 }
315
San Mehat4ba89482010-02-18 09:00:18 -0800316 if (Volume::unmountVol(true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700317 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800318 strerror(errno));
319 // XXX: At this point we're screwed for now
320 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700321 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800322 }
Ethan75a3e1a2010-07-21 23:07:51 +0800323 } else if (state == Volume::State_Shared) {
324 /* removed during mass storage */
325 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
326 getLabel(), major, minor);
327 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
328 msg, false);
329
330 if (mVm->unshareVolume(getLabel(), "ums")) {
331 SLOGE("Failed to unshare volume on bad removal (%s)",
332 strerror(errno));
333 } else {
334 SLOGD("Crisis averted");
335 }
San Mehata2677e42009-12-13 10:40:18 -0800336 }
San Mehatfd7f5872009-10-12 11:32:47 -0700337}
San Mehat49e2bce2009-10-12 16:29:01 -0700338
San Mehatdd9b8e92009-10-21 11:06:52 -0700339/*
San Mehata2677e42009-12-13 10:40:18 -0800340 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700341 */
San Mehata2677e42009-12-13 10:40:18 -0800342int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700343
344 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800345 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700346 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800347 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
348 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700349 }
350
San Mehata2677e42009-12-13 10:40:18 -0800351 int i;
352 for (i = 0; i < mDiskNumParts; i++) {
353 if (i == max)
354 break;
355 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
356 }
357 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700358 }
San Mehata2677e42009-12-13 10:40:18 -0800359 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
360 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700361}
Ken Sumrall29d8da82011-05-18 17:20:07 -0700362
363/*
364 * Called from base to update device info,
365 * e.g. When setting up an dm-crypt mapping for the sd card.
366 */
367int DirectVolume::updateDeviceInfo(char *new_path, int new_major, int new_minor)
368{
369 PathCollection::iterator it;
370
371 if (mPartIdx == -1) {
372 SLOGE("Can only change device info on a partition\n");
373 return -1;
374 }
375
376 /*
377 * This is to change the sysfs path associated with a partition, in particular,
378 * for an internal SD card partition that is encrypted. Thus, the list is
379 * expected to be only 1 entry long. Check that and bail if not.
380 */
381 if (mPaths->size() != 1) {
382 SLOGE("Cannot change path if there are more than one for a volume\n");
383 return -1;
384 }
385
386 it = mPaths->begin();
387 free(*it); /* Free the string storage */
388 mPaths->erase(it); /* Remove it from the list */
389 addPath(new_path); /* Put the new path on the list */
390
391 mDiskMajor = new_major;
392 mDiskMinor = new_minor;
393 /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
394 * partition number. They don't have partitions, they are just virtual block
395 * devices, and minor number 0 is the first dm-crypt device. Luckily the first
396 * dm-crypt device is for the userdata partition, which gets minor number 0, and
397 * it is not managed by vold. So the next device is minor number one, which we
398 * will call partition one.
399 */
400 mPartIdx = new_minor;
401 mPartMinors[new_minor-1] = new_minor;
402
403 mIsDecrypted = 1;
404
405 return 0;
406}
407
408/*
409 * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
410 */
411int DirectVolume::getVolInfo(struct volume_info *v)
412{
413 strcpy(v->label, mLabel);
414 strcpy(v->mnt_point, mMountpoint);
415 v->flags=mFlags;
416 /* Other fields of struct volume_info are filled in by the caller or cryptfs.c */
417
418 return 0;
419}