blob: 6af4ccb9ad47953914b2459739436864b97e6a08 [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)) {
95 LOGE("Error making device node '%s' (%s)", nodepath,
96 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 {
116 LOGW("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 Roota9f423d2010-03-24 15:37:48 -0700127 int major, minor;
128 if (!getMajorMinorNum(evt, &major, &minor)) {
129 LOGE("Could not determine major:minor in handleDiskAdded");
130 return;
131 }
San Mehat7b8f2db2010-01-04 14:03:36 -0800132
133 const char *tmp = evt->findParam("NPARTS");
134 if (tmp) {
135 mDiskNumParts = atoi(tmp);
136 } else {
137 LOGW("Kernel block uevent missing 'NPARTS'");
138 mDiskNumParts = 1;
139 }
140
San Mehata2677e42009-12-13 10:40:18 -0800141 char msg[255];
San Mehatfd7f5872009-10-12 11:32:47 -0700142
143 int partmask = 0;
144 int i;
San Mehat59abc3c2009-10-12 14:48:47 -0700145 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -0700146 partmask |= (1 << i);
147 }
148 mPendingPartMap = partmask;
149
150 if (mDiskNumParts == 0) {
San Mehata2677e42009-12-13 10:40:18 -0800151#ifdef PARTITION_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700152 LOGD("Dv::diskIns - No partitions - good to go son!");
San Mehata2677e42009-12-13 10:40:18 -0800153#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700154 setState(Volume::State_Idle);
155 } else {
San Mehata2677e42009-12-13 10:40:18 -0800156#ifdef PARTITION_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700157 LOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
158 mDiskNumParts, mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800159#endif
San Mehatfd7f5872009-10-12 11:32:47 -0700160 setState(Volume::State_Pending);
161 }
San Mehata2677e42009-12-13 10:40:18 -0800162
163 snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)",
164 getLabel(), getMountpoint(), mDiskMajor, mDiskMinor);
165 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,
166 msg, false);
San Mehatfd7f5872009-10-12 11:32:47 -0700167}
168
San Mehatae10b912009-10-12 14:57:05 -0700169void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
Kenny Roota9f423d2010-03-24 15:37:48 -0700170 int major, minor;
171 if (!getMajorMinorNum(evt, &major, &minor)) {
172 LOGE("Could not determine major:minor in handlePartitionAdded");
173 return;
174 }
San Mehat7b8f2db2010-01-04 14:03:36 -0800175
176 int part_num;
177
178 const char *tmp = evt->findParam("PARTN");
179
180 if (tmp) {
181 part_num = atoi(tmp);
182 } else {
183 LOGW("Kernel block uevent missing 'PARTN'");
184 part_num = 1;
185 }
San Mehat59abc3c2009-10-12 14:48:47 -0700186
San Mehat2a5b8ce2010-03-10 12:48:57 -0800187 if (part_num > mDiskNumParts) {
188 mDiskNumParts = part_num;
189 }
190
San Mehatdd9b8e92009-10-21 11:06:52 -0700191 if (major != mDiskMajor) {
192 LOGE("Partition '%s' has a different major than its disk!", devpath);
193 return;
194 }
San Mehata2677e42009-12-13 10:40:18 -0800195#ifdef PARTITION_DEBUG
196 LOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
197#endif
San Mehatdd9b8e92009-10-21 11:06:52 -0700198 mPartMinors[part_num -1] = minor;
199
San Mehat59abc3c2009-10-12 14:48:47 -0700200 mPendingPartMap &= ~(1 << part_num);
201 if (!mPendingPartMap) {
San Mehata2677e42009-12-13 10:40:18 -0800202#ifdef PARTITION_DEBUG
San Mehat59abc3c2009-10-12 14:48:47 -0700203 LOGD("Dv:partAdd: Got all partitions - ready to rock!");
San Mehata2677e42009-12-13 10:40:18 -0800204#endif
San Mehat2a5b8ce2010-03-10 12:48:57 -0800205 if (getState() != Volume::State_Formatting) {
206 setState(Volume::State_Idle);
207 }
San Mehat59abc3c2009-10-12 14:48:47 -0700208 } else {
San Mehata2677e42009-12-13 10:40:18 -0800209#ifdef PARTITION_DEBUG
San Mehat59abc3c2009-10-12 14:48:47 -0700210 LOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
San Mehata2677e42009-12-13 10:40:18 -0800211#endif
San Mehat59abc3c2009-10-12 14:48:47 -0700212 }
San Mehatfd7f5872009-10-12 11:32:47 -0700213}
214
San Mehata2677e42009-12-13 10:40:18 -0800215void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Roota9f423d2010-03-24 15:37:48 -0700216 int major, minor;
217 if (!getMajorMinorNum(evt, &major, &minor)) {
218 LOGE("Could not determine major:minor in handleDiskChanged");
219 return;
220 }
San Mehata2677e42009-12-13 10:40:18 -0800221
222 if ((major != mDiskMajor) || (minor != mDiskMinor)) {
223 return;
224 }
225
226 LOGI("Volume %s disk has changed", getLabel());
San Mehat7b8f2db2010-01-04 14:03:36 -0800227 const char *tmp = evt->findParam("NPARTS");
228 if (tmp) {
229 mDiskNumParts = atoi(tmp);
230 } else {
231 LOGW("Kernel block uevent missing 'NPARTS'");
232 mDiskNumParts = 1;
233 }
234
San Mehata2677e42009-12-13 10:40:18 -0800235 int partmask = 0;
236 int i;
237 for (i = 1; i <= mDiskNumParts; i++) {
238 partmask |= (1 << i);
239 }
240 mPendingPartMap = partmask;
241
San Mehat2a5b8ce2010-03-10 12:48:57 -0800242 if (getState() != Volume::State_Formatting) {
243 if (mDiskNumParts == 0) {
244 setState(Volume::State_Idle);
245 } else {
246 setState(Volume::State_Pending);
247 }
San Mehata2677e42009-12-13 10:40:18 -0800248 }
San Mehata2677e42009-12-13 10:40:18 -0800249}
250
251void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) {
Kenny Roota9f423d2010-03-24 15:37:48 -0700252 int major, minor;
253 if (!getMajorMinorNum(evt, &major, &minor)) {
254 LOGE("Could not determine major:minor in handlePartitionChanged");
255 return;
256 }
257
San Mehat2a5b8ce2010-03-10 12:48:57 -0800258 LOGD("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 Roota9f423d2010-03-24 15:37:48 -0700262 int major, minor;
263 if (!getMajorMinorNum(evt, &major, &minor)) {
264 LOGE("Could not determine major:minor in handleDiskRemoved");
265 return;
266 }
267
San Mehata2677e42009-12-13 10:40:18 -0800268 char msg[255];
269
270 LOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
271 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)",
272 getLabel(), getMountpoint(), major, minor);
273 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved,
274 msg, false);
275 setState(Volume::State_NoMedia);
San Mehatfd7f5872009-10-12 11:32:47 -0700276}
277
San Mehatae10b912009-10-12 14:57:05 -0700278void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
Kenny Roota9f423d2010-03-24 15:37:48 -0700279 int major, minor;
280 if (!getMajorMinorNum(evt, &major, &minor)) {
281 LOGE("Could not determine major:minor in handlePartitionRemoved");
282 return;
283 }
284
San Mehata2677e42009-12-13 10:40:18 -0800285 char msg[255];
286
287 LOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
288
289 /*
290 * The framework doesn't need to get notified of
291 * partition removal unless it's mounted. Otherwise
292 * the removal notification will be sent on the Disk
293 * itself
294 */
295 if (getState() != Volume::State_Mounted) {
296 return;
297 }
298
299 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) {
300 /*
301 * Yikes, our mounted partition is going away!
302 */
303
304 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)",
305 getLabel(), getMountpoint(), major, minor);
306 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
307 msg, false);
San Mehat4ba89482010-02-18 09:00:18 -0800308 if (Volume::unmountVol(true)) {
San Mehata2677e42009-12-13 10:40:18 -0800309 LOGE("Failed to unmount volume on bad removal (%s)",
310 strerror(errno));
311 // XXX: At this point we're screwed for now
312 } else {
313 LOGD("Crisis averted");
314 }
315 }
San Mehatfd7f5872009-10-12 11:32:47 -0700316}
San Mehat49e2bce2009-10-12 16:29:01 -0700317
San Mehatdd9b8e92009-10-21 11:06:52 -0700318/*
San Mehata2677e42009-12-13 10:40:18 -0800319 * Called from base to get a list of devicenodes for mounting
San Mehatdd9b8e92009-10-21 11:06:52 -0700320 */
San Mehata2677e42009-12-13 10:40:18 -0800321int DirectVolume::getDeviceNodes(dev_t *devs, int max) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700322
323 if (mPartIdx == -1) {
San Mehata2677e42009-12-13 10:40:18 -0800324 // If the disk has no partitions, try the disk itself
San Mehatdd9b8e92009-10-21 11:06:52 -0700325 if (!mDiskNumParts) {
San Mehata2677e42009-12-13 10:40:18 -0800326 devs[0] = MKDEV(mDiskMajor, mDiskMinor);
327 return 1;
San Mehatdd9b8e92009-10-21 11:06:52 -0700328 }
329
San Mehata2677e42009-12-13 10:40:18 -0800330 int i;
331 for (i = 0; i < mDiskNumParts; i++) {
332 if (i == max)
333 break;
334 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]);
335 }
336 return mDiskNumParts;
San Mehatdd9b8e92009-10-21 11:06:52 -0700337 }
San Mehata2677e42009-12-13 10:40:18 -0800338 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]);
339 return 1;
San Mehat49e2bce2009-10-12 16:29:01 -0700340}