blob: 0ac1a0c9a7b243ffa466b43fe220aa55899c775c [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
Mike Lockwood2dfe2972010-09-17 18:50:51 -040068dev_t DirectVolume::getShareDevice() {
69 if (mPartIdx != -1) {
70 return MKDEV(mDiskMajor, mPartIdx);
71 } else {
72 return MKDEV(mDiskMajor, mDiskMinor);
73 }
74}
75
San Mehata2677e42009-12-13 10:40:18 -080076void DirectVolume::handleVolumeShared() {
77 setState(Volume::State_Shared);
78}
79
80void DirectVolume::handleVolumeUnshared() {
81 setState(Volume::State_Idle);
82}
83
San Mehatae10b912009-10-12 14:57:05 -070084int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070085 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070086
San Mehatfd7f5872009-10-12 11:32:47 -070087 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070088 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070089 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -070090 /* We can handle this disk */
91 int action = evt->getAction();
92 const char *devtype = evt->findParam("DEVTYPE");
93
San Mehata2677e42009-12-13 10:40:18 -080094 if (action == NetlinkEvent::NlActionAdd) {
95 int major = atoi(evt->findParam("MAJOR"));
96 int minor = atoi(evt->findParam("MINOR"));
97 char nodepath[255];
98
99 snprintf(nodepath,
100 sizeof(nodepath), "/dev/block/vold/%d:%d",
101 major, minor);
102 if (createDeviceNode(nodepath, major, minor)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700103 SLOGE("Error making device node '%s' (%s)", nodepath,
San Mehata2677e42009-12-13 10:40:18 -0800104 strerror(errno));
105 }
106 if (!strcmp(devtype, "disk")) {
San Mehatfd7f5872009-10-12 11:32:47 -0700107 handleDiskAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800108 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700109 handlePartitionAdded(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800110 }
111 } else if (action == NetlinkEvent::NlActionRemove) {
112 if (!strcmp(devtype, "disk")) {
113 handleDiskRemoved(dp, evt);
114 } else {
San Mehatfd7f5872009-10-12 11:32:47 -0700115 handlePartitionRemoved(dp, evt);
San Mehata2677e42009-12-13 10:40:18 -0800116 }
117 } else if (action == NetlinkEvent::NlActionChange) {
118 if (!strcmp(devtype, "disk")) {
119 handleDiskChanged(dp, evt);
120 } else {
121 handlePartitionChanged(dp, evt);
122 }
123 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700124 SLOGW("Ignoring non add/remove/change event");
San Mehatf1b736b2009-10-10 17:22:08 -0700125 }
San Mehatfd7f5872009-10-12 11:32:47 -0700126
San Mehatf1b736b2009-10-10 17:22:08 -0700127 return 0;
128 }
129 }
130 errno = ENODEV;
131 return -1;
132}
San Mehatfd7f5872009-10-12 11:32:47 -0700133
San Mehatae10b912009-10-12 14:57:05 -0700134void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700135 mDiskMajor = atoi(evt->findParam("MAJOR"));
136 mDiskMinor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800137
138 const char *tmp = evt->findParam("NPARTS");
139 if (tmp) {
140 mDiskNumParts = atoi(tmp);
141 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700142 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800143 mDiskNumParts = 1;
144 }
145
San Mehata2677e42009-12-13 10:40:18 -0800146 char msg[255];
San Mehatfd7f5872009-10-12 11:32:47 -0700147
148 int partmask = 0;
149 int i;
San Mehat59abc3c2009-10-12 14:48:47 -0700150 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -0700151 partmask |= (1 << i);
152 }
153 mPendingPartMap = partmask;
154
155 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800156#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700157 SLOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800158#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700159 setState(Volume::State_Idle);
160 } else {
San Mehata2677e42009-12-13 10:40:18 -0800161#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700162 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
San Mehatfd7f5872009-10-12 11:32:47 -0700163 mDiskNumParts, mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800164#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700165 setState(Volume::State_Pending);
166 }
San Mehata2677e42009-12-13 10:40:18 -0800167
168 snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)",
169 getLabel(), getMountpoint(), mDiskMajor, mDiskMinor);
170 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
171 msg, false);
San Mehatfd7f5872009-10-12 11:32:47 -0700172}
173
San Mehatae10b912009-10-12 14:57:05 -0700174void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700175 int major = atoi(evt->findParam("MAJOR"));
176 int minor = atoi(evt->findParam("MINOR"));
San Mehat7b8f2db2010-01-04 14:03:36 -0800177
178 int part_num;
179
180 const char *tmp = evt->findParam("PARTN");
181
182 if (tmp) {
183 part_num = atoi(tmp);
184 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700185 SLOGW("Kernel block uevent missing 'PARTN'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800186 part_num = 1;
187 }
San Mehat59abc3c2009-10-12 14:48:47 -0700188
Nick Kralevichf3d3ce52011-04-18 11:16:13 -0700189 if (part_num > MAX_PARTITIONS || part_num < 1) {
190 SLOGW("Invalid 'PARTN' value");
191 part_num = 1;
192 }
193
San Mehat2a5b8ce2010-03-10 12:48:57 -0800194 if (part_num > mDiskNumParts) {
195 mDiskNumParts = part_num;
196 }
197
San Mehatdd9b8e92009-10-21 11:06:52 -0700198 if (major != mDiskMajor) {
San Mehat97ac40e2010-03-24 10:24:19 -0700199 SLOGE("Partition '%s' has a different major than its disk!", devpath);
San Mehatdd9b8e92009-10-21 11:06:52 -0700200 return;
201 }
San Mehata2677e42009-12-13 10:40:18 -0800202#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700203 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
San Mehata2677e42009-12-13 10:40:18 -0800204#endif
San Mehatdd9b8e92009-10-21 11:06:52 -0700205 mPartMinors[part_num -1] = minor;
206
San Mehat59abc3c2009-10-12 14:48:47 -0700207 mPendingPartMap &= ~(1 << part_num);
208 if (!mPendingPartMap) {
San Mehata2677e42009-12-13 10:40:18 -0800209#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700210 SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800211#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800212 if (getState() != Volume::State_Formatting) {
213 setState(Volume::State_Idle);
214 }
San Mehat59abc3c2009-10-12 14:48:47 -0700215 } else {
San Mehata2677e42009-12-13 10:40:18 -0800216#ifdef PARTITION_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700217 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800218#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700219 }
San Mehatfd7f5872009-10-12 11:32:47 -0700220}
221
San Mehata2677e42009-12-13 10:40:18 -0800222void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700223 int major = atoi(evt->findParam("MAJOR"));
224 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800225
226 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
227 return;
228 }
229
San Mehat97ac40e2010-03-24 10:24:19 -0700230 SLOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800231 const char *tmp = evt->findParam("NPARTS");
232 if (tmp) {
233 mDiskNumParts = atoi(tmp);
234 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700235 SLOGW("Kernel block uevent missing 'NPARTS'");
San Mehat7b8f2db2010-01-04 14:03:36 -0800236 mDiskNumParts = 1;
237 }
238
San Mehata2677e42009-12-13 10:40:18 -0800239 int partmask = 0;
240 int i;
241 for (i = 1; i <= mDiskNumParts; i++) {
242 partmask |= (1 << i);
243 }
244 mPendingPartMap = partmask;
245
San Mehat2a5b8ce2010-03-10 12:48:57 -0800246 if (getState() != Volume::State_Formatting) {
247 if (mDiskNumParts == 0) {
248 setState(Volume::State_Idle);
249 } else {
250 setState(Volume::State_Pending);
251 }
San Mehata2677e42009-12-13 10:40:18 -0800252 }
San Mehata2677e42009-12-13 10:40:18 -0800253}
254
255void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700256 int major = atoi(evt->findParam("MAJOR"));
257 int minor = atoi(evt->findParam("MINOR"));
San Mehat97ac40e2010-03-24 10:24:19 -0700258 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800259}
260
San Mehatae10b912009-10-12 14:57:05 -0700261void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700262 int major = atoi(evt->findParam("MAJOR"));
263 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800264 char msg[255];
265
San Mehat97ac40e2010-03-24 10:24:19 -0700266 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800267 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
268 getLabel(), getMountpoint(), major, minor);
269 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
270 msg, false);
271 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700272}
273
San Mehatae10b912009-10-12 14:57:05 -0700274void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Rootda62e7c2010-03-24 20:18:00 -0700275 int major = atoi(evt->findParam("MAJOR"));
276 int minor = atoi(evt->findParam("MINOR"));
San Mehata2677e42009-12-13 10:40:18 -0800277 char msg[255];
278
San Mehat97ac40e2010-03-24 10:24:19 -0700279 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
San Mehata2677e42009-12-13 10:40:18 -0800280
281 /*
282 * The framework doesn't need to get notified of
283 * partition removal unless it's mounted. Otherwise
284 * the removal notification will be sent on the Disk
285 * itself
286 */
287 if (getState() != Volume::State_Mounted) {
288 return;
289 }
290
291 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
292 /*
293 * Yikes, our mounted partition is going away!
294 */
295
296 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
297 getLabel(), getMountpoint(), major, minor);
298 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
299 msg, false);
San Mehat1a06eda2010-04-15 12:58:50 -0700300
301 if (mVm->cleanupAsec(this, true)) {
302 SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
303 }
304
San Mehat4ba89482010-02-18 09:00:18 -0800305 if (Volume::unmountVol(true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700306 SLOGE("Failed to unmount volume on bad removal (%s)",
San Mehata2677e42009-12-13 10:40:18 -0800307 strerror(errno));
308 // XXX: At this point we're screwed for now
309 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700310 SLOGD("Crisis averted");
San Mehata2677e42009-12-13 10:40:18 -0800311 }
312 }
San Mehatfd7f5872009-10-12 11:32:47 -0700313}
San Mehat49e2bce2009-10-12 16:29:01 -0700314
San Mehatdd9b8e92009-10-21 11:06:52 -0700315/*
San Mehata2677e42009-12-13 10:40:18 -0800316 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700317 */
San Mehata2677e42009-12-13 10:40:18 -0800318int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700319
320 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800321 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700322 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800323 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
324 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700325 }
326
San Mehata2677e42009-12-13 10:40:18 -0800327 int i;
328 for (i = 0; i < mDiskNumParts; i++) {
329 if (i == max)
330 break;
331 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
332 }
333 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700334 }
San Mehata2677e42009-12-13 10:40:18 -0800335 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
336 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700337}