blob: 0d781d0316baaa29a747cc6b77d35756a88e66d7 [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"
San Mehatf1b736b2009-10-10 17:22:08 -070032
San Mehata2677e42009-12-13 10:40:18 -080033// #define PARTITION_DEBUG
34
35DirectVolume::DirectVolume(VolumeManager *vm, const char *label,
36 const char *mount_point, int partIdx) :
37 Volume(vm, label, mount_point) {
San Mehatf1b736b2009-10-10 17:22:08 -070038 mPartIdx = partIdx;
San Mehata2677e42009-12-13 10:40:18 -080039
San Mehatf1b736b2009-10-10 17:22:08 -070040 mPaths = new PathCollection();
San Mehatdd9b8e92009-10-21 11:06:52 -070041 for (int i = 0; i < MAX_PARTITIONS; i++)
42 mPartMinors[i] = -1;
San Mehata2677e42009-12-13 10:40:18 -080043 mPendingPartMap = 0;
44 mDiskMajor = -1;
45 mDiskMinor = -1;
46 mDiskNumParts = 0;
47
48 setState(Volume::State_NoMedia);
San Mehatf1b736b2009-10-10 17:22:08 -070049}
50
San Mehatae10b912009-10-12 14:57:05 -070051DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070052 PathCollection::iterator it;
53
54 for (it = mPaths->begin(); it != mPaths->end(); ++it)
55 free(*it);
56 delete mPaths;
57}
58
San Mehatae10b912009-10-12 14:57:05 -070059int DirectVolume::addPath(const char *path) {
San Mehatf1b736b2009-10-10 17:22:08 -070060 mPaths->push_back(strdup(path));
61 return 0;
62}
63
San Mehata2677e42009-12-13 10:40:18 -080064dev_t DirectVolume::getDiskDevice() {
65 return MKDEV(mDiskMajor, mDiskMinor);
66}
67
68void DirectVolume::handleVolumeShared() {
69 setState(Volume::State_Shared);
70}
71
72void DirectVolume::handleVolumeUnshared() {
73 setState(Volume::State_Idle);
74}
75
San Mehatae10b912009-10-12 14:57:05 -070076int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070077 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070078
San Mehatfd7f5872009-10-12 11:32:47 -070079 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070080 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070081 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -070082 /* We can handle this disk */
83 int action = evt->getAction();
84 const char *devtype = evt->findParam("DEVTYPE");
85
San Mehata2677e42009-12-13 10:40:18 -080086 if (action == NetlinkEvent::NlActionAdd) {
87 int major = atoi(evt->findParam("MAJOR"));
88 int minor = atoi(evt->findParam("MINOR"));
89 char nodepath[255];
90
91 snprintf(nodepath,
92 sizeof(nodepath), "/dev/block/vold/%d:%d",
93 major, minor);
94 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -070095 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -080096 strerror(errno));
97 }
98 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -070099 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800100 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700101 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800102 }
103 } else if (action == NetlinkEvent::NlActionRemove) {
104 if (!strcmp(devtype, "disk")) {
105 handleDiskRemoved(dp, evt);
106 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700107 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800108 }
109 } else if (action == NetlinkEvent::NlActionChange) {
110 if (!strcmp(devtype, "disk")) {
111 handleDiskChanged(dp, evt);
112 } else {
113 handlePartitionChanged(dp, evt);
114 }
115 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700116 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700117 }
San Mehatfd7f5872009-10-12 11:32:47 -0700118
San Mehatf1b736b2009-10-10 17:22:08 -0700119 return 0;
120 }
121 }
122 errno = ENODEV;
123 return -1;
124}
San Mehatfd7f5872009-10-12 11:32:47 -0700125
San Mehatae10b912009-10-12 14:57:05 -0700126void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700127 mDiskMajor = atoi(evt->findParam("MAJOR"));
128 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800129
130 const char *tmp = evt->findParam("NPARTS");
131 if (tmp) {
132 mDiskNumParts = atoi(tmp);
133 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700134 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800135 mDiskNumParts = 1;
136 }
137
San Mehata2677e42009-12-13 10:40:18 -0800138 char msg[255];
San Mehatfd7f5872009-10-12 11:32:47 -0700139
140 int partmask = 0;
141 int i;
San Mehat59abc3c2009-10-12 14:48:47 -0700142 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -0700143 partmask |= (1 << i);
144 }
145 mPendingPartMap = partmask;
146
147 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800148#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700149 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800150#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700151 setState(Volume::State_Idle);
152 } else {
San Mehata2677e42009-12-13 10:40:18 -0800153#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700154 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
San Mehatfd7f5872009-10-12 11:32:47 -0700155 mDiskNumParts, mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800156#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700157 setState(Volume::State_Pending);
158 }
San Mehata2677e42009-12-13 10:40:18 -0800159
160 snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)",
161 getLabel(), getMountpoint(), mDiskMajor, mDiskMinor);
162 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
163 msg, false);
San Mehatfd7f5872009-10-12 11:32:47 -0700164}
165
San Mehatae10b912009-10-12 14:57:05 -0700166void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700167 int major = atoi(evt->findParam("MAJOR"));
168 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800169
170 int part_num;
171
172 const char *tmp = evt->findParam("PARTN");
173
174 if (tmp) {
175 part_num = atoi(tmp);
176 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700177 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800178 part_num = 1;
179 }
San Mehat59abc3c2009-10-12 14:48:47 -0700180
San Mehat2a5b8ce2010-03-10 12:48:57 -0800181 if (part_num > mDiskNumParts) {
182 mDiskNumParts = part_num;
183 }
184
San Mehatdd9b8e92009-10-21 11:06:52 -0700185 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700186 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700187 return;
188 }
San Mehata2677e42009-12-13 10:40:18 -0800189#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700190 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800191#endif
Chih-Wei Huangdef8f312010-11-17 18:53:09 +0800192 if (part_num > MAX_PARTITIONS) {
193 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS);
Bruce Beared7660902010-07-22 13:23:33 -0700194 } else {
195 mPartMinors[part_num -1] = minor;
196 }
San Mehat59abc3c2009-10-12 14:48:47 -0700197 mPendingPartMap &= ~(1 << part_num);
Bruce Beared7660902010-07-22 13:23:33 -0700198
San Mehat59abc3c2009-10-12 14:48:47 -0700199 if (!mPendingPartMap) {
San Mehata2677e42009-12-13 10:40:18 -0800200#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700201 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800202#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800203 if (getState() != Volume::State_Formatting) {
204 setState(Volume::State_Idle);
205 }
San Mehat59abc3c2009-10-12 14:48:47 -0700206 } else {
San Mehata2677e42009-12-13 10:40:18 -0800207#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700208 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800209#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700210 }
San Mehatfd7f5872009-10-12 11:32:47 -0700211}
212
San Mehata2677e42009-12-13 10:40:18 -0800213void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700214 int major = atoi(evt->findParam("MAJOR"));
215 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800216
217 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
218 return;
219 }
220
San Mehat97ac40e2010-03-24 10:24:19 -0700221 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800222 const char *tmp = evt->findParam("NPARTS");
223 if (tmp) {
224 mDiskNumParts = atoi(tmp);
225 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700226 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800227 mDiskNumParts = 1;
228 }
229
San Mehata2677e42009-12-13 10:40:18 -0800230 int partmask = 0;
231 int i;
232 for (i = 1; i <= mDiskNumParts; i++) {
233 partmask |= (1 << i);
234 }
235 mPendingPartMap = partmask;
236
San Mehat2a5b8ce2010-03-10 12:48:57 -0800237 if (getState() != Volume::State_Formatting) {
238 if (mDiskNumParts == 0) {
239 setState(Volume::State_Idle);
240 } else {
241 setState(Volume::State_Pending);
242 }
San Mehata2677e42009-12-13 10:40:18 -0800243 }
San Mehata2677e42009-12-13 10:40:18 -0800244}
245
246void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700247 int major = atoi(evt->findParam("MAJOR"));
248 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700249 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800250}
251
San Mehatae10b912009-10-12 14:57:05 -0700252void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700253 int major = atoi(evt->findParam("MAJOR"));
254 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800255 char msg[255];
256
San Mehat97ac40e2010-03-24 10:24:19 -0700257 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800258 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
259 getLabel(), getMountpoint(), major, minor);
260 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
261 msg, false);
262 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700263}
264
San Mehatae10b912009-10-12 14:57:05 -0700265void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700266 int major = atoi(evt->findParam("MAJOR"));
267 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800268 char msg[255];
Ethan75a3e1a2010-07-21 23:07:51 +0800269 int state;
San Mehata2677e42009-12-13 10:40:18 -0800270
San Mehat97ac40e2010-03-24 10:24:19 -0700271 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800272
273 /*
274 * The framework doesn't need to get notified of
275 * partition removal unless it's mounted. Otherwise
276 * the removal notification will be sent on the Disk
277 * itself
278 */
Ethan75a3e1a2010-07-21 23:07:51 +0800279 state = getState();
280 if (state != Volume::State_Mounted && state != Volume::State_Shared) {
San Mehata2677e42009-12-13 10:40:18 -0800281 return;
282 }
283
284 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
285 /*
286 * Yikes, our mounted partition is going away!
287 */
288
289 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
290 getLabel(), getMountpoint(), major, minor);
291 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
292 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700293
294 if (mVm->cleanupAsec(this, true)) {
295 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
296 }
297
San Mehat4ba89482010-02-18 09:00:18 -0800298 if (Volume::unmountVol(true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700299 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800300 strerror(errno));
301 // XXX: At this point we're screwed for now
302 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700303 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800304 }
Ethan75a3e1a2010-07-21 23:07:51 +0800305 } else if (state == Volume::State_Shared) {
306 /* removed during mass storage */
307 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
308 getLabel(), major, minor);
309 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
310 msg, false);
311
312 if (mVm->unshareVolume(getLabel(), "ums")) {
313 SLOGE("Failed to unshare volume on bad removal (%s)",
314 strerror(errno));
315 } else {
316 SLOGD("Crisis averted");
317 }
San Mehata2677e42009-12-13 10:40:18 -0800318 }
San Mehatfd7f5872009-10-12 11:32:47 -0700319}
San Mehat49e2bce2009-10-12 16:29:01 -0700320
San Mehatdd9b8e92009-10-21 11:06:52 -0700321/*
San Mehata2677e42009-12-13 10:40:18 -0800322 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700323 */
San Mehata2677e42009-12-13 10:40:18 -0800324int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700325
326 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800327 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700328 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800329 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
330 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700331 }
332
San Mehata2677e42009-12-13 10:40:18 -0800333 int i;
334 for (i = 0; i < mDiskNumParts; i++) {
335 if (i == max)
336 break;
337 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
338 }
339 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700340 }
San Mehata2677e42009-12-13 10:40:18 -0800341 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
342 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700343}