blob: cd12cf44f14c03c0f19a5991a836279e10751a31 [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();
San Mehatdd9b8e92009-10-21 11:06:52 -070034 for (int i = 0; i < MAX_PARTITIONS; i++)
35 mPartMinors[i] = -1;
San Mehatf1b736b2009-10-10 17:22:08 -070036}
37
San Mehatae10b912009-10-12 14:57:05 -070038DirectVolume::~DirectVolume() {
San Mehatf1b736b2009-10-10 17:22:08 -070039 PathCollection::iterator it;
40
41 for (it = mPaths->begin(); it != mPaths->end(); ++it)
42 free(*it);
43 delete mPaths;
44}
45
San Mehatae10b912009-10-12 14:57:05 -070046int DirectVolume::addPath(const char *path) {
San Mehatf1b736b2009-10-10 17:22:08 -070047 mPaths->push_back(strdup(path));
48 return 0;
49}
50
San Mehatae10b912009-10-12 14:57:05 -070051int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -070052 const char *dp = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -070053
San Mehatfd7f5872009-10-12 11:32:47 -070054 PathCollection::iterator it;
San Mehatf1b736b2009-10-10 17:22:08 -070055 for (it = mPaths->begin(); it != mPaths->end(); ++it) {
San Mehatf1b736b2009-10-10 17:22:08 -070056 if (!strncmp(dp, *it, strlen(*it))) {
San Mehatfd7f5872009-10-12 11:32:47 -070057 /* We can handle this disk */
58 int action = evt->getAction();
59 const char *devtype = evt->findParam("DEVTYPE");
60
61 if (!strcmp(devtype, "disk")) {
62 if (action == NetlinkEvent::NlActionAdd)
63 handleDiskAdded(dp, evt);
64 else if (action == NetlinkEvent::NlActionRemove)
65 handleDiskRemoved(dp, evt);
66 else
67 LOGD("Ignoring non add/remove event");
San Mehatf1b736b2009-10-10 17:22:08 -070068 } else {
San Mehatfd7f5872009-10-12 11:32:47 -070069 if (action == NetlinkEvent::NlActionAdd)
70 handlePartitionAdded(dp, evt);
71 else if (action == NetlinkEvent::NlActionRemove)
72 handlePartitionRemoved(dp, evt);
73 else
74 LOGD("Ignoring non add/remove event");
San Mehatf1b736b2009-10-10 17:22:08 -070075 }
San Mehatfd7f5872009-10-12 11:32:47 -070076
San Mehatf1b736b2009-10-10 17:22:08 -070077 return 0;
78 }
79 }
80 errno = ENODEV;
81 return -1;
82}
San Mehatfd7f5872009-10-12 11:32:47 -070083
San Mehatae10b912009-10-12 14:57:05 -070084void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
San Mehatdd9b8e92009-10-21 11:06:52 -070085 mDiskMajor = atoi(evt->findParam("MAJOR"));
86 mDiskMinor = atoi(evt->findParam("MAJOR"));
San Mehatfd7f5872009-10-12 11:32:47 -070087 mDiskNumParts = atoi(evt->findParam("NPARTS"));
88
89 int partmask = 0;
90 int i;
San Mehat59abc3c2009-10-12 14:48:47 -070091 for (i = 1; i <= mDiskNumParts; i++) {
San Mehatfd7f5872009-10-12 11:32:47 -070092 partmask |= (1 << i);
93 }
94 mPendingPartMap = partmask;
95
96 if (mDiskNumParts == 0) {
97 LOGD("Dv::diskIns - No partitions - good to go son!");
98 setState(Volume::State_Idle);
99 } else {
100 LOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
101 mDiskNumParts, mPendingPartMap);
102 setState(Volume::State_Pending);
103 }
104}
105
San Mehatae10b912009-10-12 14:57:05 -0700106void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700107 int major = atoi(evt->findParam("MAJOR"));
108 int minor = atoi(evt->findParam("MINOR"));
109 int part_num = atoi(evt->findParam("PARTN"));
San Mehat59abc3c2009-10-12 14:48:47 -0700110
San Mehatdd9b8e92009-10-21 11:06:52 -0700111 if (major != mDiskMajor) {
112 LOGE("Partition '%s' has a different major than its disk!", devpath);
113 return;
114 }
115 mPartMinors[part_num -1] = minor;
116
San Mehat59abc3c2009-10-12 14:48:47 -0700117 mPendingPartMap &= ~(1 << part_num);
118 if (!mPendingPartMap) {
119 LOGD("Dv:partAdd: Got all partitions - ready to rock!");
120 setState(Volume::State_Idle);
121 } else {
122 LOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap);
123 }
San Mehatfd7f5872009-10-12 11:32:47 -0700124}
125
San Mehatae10b912009-10-12 14:57:05 -0700126void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700127}
128
San Mehatae10b912009-10-12 14:57:05 -0700129void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
San Mehatfd7f5872009-10-12 11:32:47 -0700130}
San Mehat49e2bce2009-10-12 16:29:01 -0700131
San Mehatdd9b8e92009-10-21 11:06:52 -0700132/*
133 * Called from Volume to determine the major/minor numbers
134 * to be used for mounting
135 */
San Mehat49e2bce2009-10-12 16:29:01 -0700136int DirectVolume::prepareToMount(int *major, int *minor) {
San Mehatdd9b8e92009-10-21 11:06:52 -0700137 *major = mDiskMajor;
138
139 if (mPartIdx == -1) {
140 /* No specific partition specified */
141
142 if (!mDiskNumParts) {
143 *minor = mDiskMinor;
144 return 0;
145 }
146
147 /*
148 * XXX: Use first partition for now.
149 * The right thing to do would be to choose
150 * this based on the partition type.
151 *
152 */
153
154 *minor = mPartMinors[0];
155 return 0;
156 }
157
158 if (mPartIdx - 1 > mDiskNumParts) {
159 errno = EINVAL;
160 return -1;
161 }
162
163 *minor = mPartMinors[mPartIdx-1];
164 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700165}