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(); |
| 34 | } |
| 35 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 36 | DirectVolume::~DirectVolume() { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 37 | PathCollection::iterator it; |
| 38 | |
| 39 | for (it = mPaths->begin(); it != mPaths->end(); ++it) |
| 40 | free(*it); |
| 41 | delete mPaths; |
| 42 | } |
| 43 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 44 | int DirectVolume::addPath(const char *path) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | mPaths->push_back(strdup(path)); |
| 46 | return 0; |
| 47 | } |
| 48 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 49 | int DirectVolume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 50 | const char *dp = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 51 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 52 | PathCollection::iterator it; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | for (it = mPaths->begin(); it != mPaths->end(); ++it) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 54 | if (!strncmp(dp, *it, strlen(*it))) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 55 | /* 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 66 | } else { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 67 | 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 73 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 74 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | errno = ENODEV; |
| 79 | return -1; |
| 80 | } |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 81 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 82 | void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 83 | mDiskMaj = atoi(evt->findParam("MAJOR")); |
| 84 | mDiskNumParts = atoi(evt->findParam("NPARTS")); |
| 85 | |
| 86 | int partmask = 0; |
| 87 | int i; |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 88 | for (i = 1; i <= mDiskNumParts; i++) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 89 | 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 Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 103 | void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 104 | int major = atoi(evt->findParam("MAJOR")); |
| 105 | int minor = atoi(evt->findParam("MINOR")); |
| 106 | int part_num = atoi(evt->findParam("PARTN")); |
San Mehat | 59abc3c | 2009-10-12 14:48:47 -0700 | [diff] [blame] | 107 | |
| 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 Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 117 | void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 118 | } |
| 119 | |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame^] | 120 | void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 121 | } |