San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 19 | #include <string.h> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 21 | |
| 22 | #define LOG_TAG "Vold" |
| 23 | |
| 24 | #include <cutils/log.h> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 25 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 26 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 27 | #include "DirectVolume.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 28 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 29 | DirectVolume::DirectVolume(const char *label, const char *mount_point, int partIdx) : |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 30 | Volume(label, mount_point) { |
| 31 | mPartIdx = partIdx; |
| 32 | |
| 33 | mPaths = new PathCollection(); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame^] | 34 | for (int i = 0; i < MAX_PARTITIONS; i++) |
| 35 | mPartMinors[i] = -1; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 38 | DirectVolume::~DirectVolume() { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | PathCollection::iterator it; |
| 40 | |
| 41 | for (it = mPaths->begin(); it != mPaths->end(); ++it) |
| 42 | free(*it); |
| 43 | delete mPaths; |
| 44 | } |
| 45 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 46 | int DirectVolume::addPath(const char *path) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | mPaths->push_back(strdup(path)); |
| 48 | return 0; |
| 49 | } |
| 50 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 51 | int DirectVolume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 52 | const char *dp = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 54 | PathCollection::iterator it; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 55 | for (it = mPaths->begin(); it != mPaths->end(); ++it) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 56 | if (!strncmp(dp, *it, strlen(*it))) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 57 | /* 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 68 | } else { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 69 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 75 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 76 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | errno = ENODEV; |
| 81 | return -1; |
| 82 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 83 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 84 | void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) { |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame^] | 85 | mDiskMajor = atoi(evt->findParam("MAJOR")); |
| 86 | mDiskMinor = atoi(evt->findParam("MAJOR")); |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 87 | mDiskNumParts = atoi(evt->findParam("NPARTS")); |
| 88 | |
| 89 | int partmask = 0; |
| 90 | int i; |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 91 | for (i = 1; i <= mDiskNumParts; i++) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 92 | 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 Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 106 | void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 107 | int major = atoi(evt->findParam("MAJOR")); |
| 108 | int minor = atoi(evt->findParam("MINOR")); |
| 109 | int part_num = atoi(evt->findParam("PARTN")); |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 110 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame^] | 111 | 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 Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 117 | 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 Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 124 | } |
| 125 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 126 | void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 129 | void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 130 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 131 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame^] | 132 | /* |
| 133 | * Called from Volume to determine the major/minor numbers |
| 134 | * to be used for mounting |
| 135 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 136 | int DirectVolume::prepareToMount(int *major, int *minor) { |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame^] | 137 | *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 Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 165 | } |