blob: 515a33ad9ecefe664c1691f7006358f7b39eb737 [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
22#define LOG_TAG "Vold"
23
24#include <cutils/log.h>
San Mehatfd7f5872009-10-12 11:32:47 -070025#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
San Mehatae10b912009-10-12 14:57:05 -070027#include "DirectVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070028
San Mehatae10b912009-10-12 14:57:05 -070029DirectVolume::DirectVolume(const char *label, const char *mount_point, int partIdx) :
San Mehatf1b736b2009-10-10 17:22:08 -070030 Volume(label, mount_point) {
31 mPartIdx = partIdx;
32
33 mPaths = new PathCollection();
34}
35
San Mehatae10b912009-10-12 14:57:05 -070036DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070037 PathCollection::iterator it;
38
39 for (it = mPaths->begin(); it != mPaths->end(); ++it)
40 free(*it);
41 delete mPaths;
42}
43
San Mehatae10b912009-10-12 14:57:05 -070044int DirectVolume::addPath(const char *path) {
San Mehatf1b736b2009-10-10 17:22:08 -070045 mPaths->push_back(strdup(path));
46 return 0;
47}
48
San Mehatae10b912009-10-12 14:57:05 -070049int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070050 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070051
San Mehatfd7f5872009-10-12 11:32:47 -070052 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070053 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070054 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -070055 /* We can handle this disk */
56 int action = evt->getAction();
57 const char *devtype = evt->findParam("DEVTYPE");
58
59 if (!strcmp(devtype, "disk")) {
60 if (action == NetlinkEvent::NlActionAdd)
61 handleDiskAdded(dp, evt);
62 else if (action == NetlinkEvent::NlActionRemove)
63 handleDiskRemoved(dp, evt);
64 else
65 LOGD("Ignoring non add/remove event");
San Mehatf1b736b2009-10-10 17:22:08 -070066 } else {
San Mehatfd7f5872009-10-12 11:32:47 -070067 if (action == NetlinkEvent::NlActionAdd)
68 handlePartitionAdded(dp, evt);
69 else if (action == NetlinkEvent::NlActionRemove)
70 handlePartitionRemoved(dp, evt);
71 else
72 LOGD("Ignoring non add/remove event");
San Mehatf1b736b2009-10-10 17:22:08 -070073 }
San Mehatfd7f5872009-10-12 11:32:47 -070074
San Mehatf1b736b2009-10-10 17:22:08 -070075 return 0;
76 }
77 }
78 errno = ENODEV;
79 return -1;
80}
San Mehatfd7f5872009-10-12 11:32:47 -070081
San Mehatae10b912009-10-12 14:57:05 -070082void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070083 mDiskMaj = atoi(evt->findParam("MAJOR"));
84 mDiskNumParts = atoi(evt->findParam("NPARTS"));
85
86 int partmask = 0;
87 int i;
San Mehat59abc3c2009-10-12 14:48:47 -070088 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -070089 partmask |= (1 << i);
90 }
91 mPendingPartMap = partmask;
92
93 if (mDiskNumParts == 0) {
94 LOGD("Dv::diskIns - No partitions - good to go son!");
95 setState(Volume::State_Idle);
96 } else {
97 LOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
98 mDiskNumParts, mPendingPartMap);
99 setState(Volume::State_Pending);
100 }
101}
102
San Mehatae10b912009-10-12 14:57:05 -0700103void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700104 int major = atoi(evt->findParam("MAJOR"));
105 int minor = atoi(evt->findParam("MINOR"));
106 int part_num = atoi(evt->findParam("PARTN"));
San Mehat59abc3c2009-10-12 14:48:47 -0700107
108 mPendingPartMap &= ~(1 << part_num);
109 if (!mPendingPartMap) {
110 LOGD("Dv:partAdd: Got all partitions - ready to rock!");
111 setState(Volume::State_Idle);
112 } else {
113 LOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
114 }
San Mehatfd7f5872009-10-12 11:32:47 -0700115}
116
San Mehatae10b912009-10-12 14:57:05 -0700117void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700118}
119
San Mehatae10b912009-10-12 14:57:05 -0700120void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700121}
San Mehat49e2bce2009-10-12 16:29:01 -0700122
123int DirectVolume::prepareToMount(int *major, int *minor) {
124 errno = ENOSYS;
125 return -1;
126}