Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Utils.h" |
| 18 | #include "VolumeBase.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
| 20 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 21 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 22 | #include <base/stringprintf.h> |
| 23 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/mount.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | #define DEBUG 1 |
| 34 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace vold { |
| 37 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 38 | VolumeBase::VolumeBase(Type type) : |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 39 | mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState( |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 40 | State::kUnmounted), mSilent(false) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | VolumeBase::~VolumeBase() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 44 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 47 | void VolumeBase::setState(State state) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 48 | mState = state; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 49 | notifyEvent(ResponseCode::VolumeStateChanged, StringPrintf("%d", mState)); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 52 | status_t VolumeBase::setDiskId(const std::string& diskId) { |
| 53 | if (mCreated) { |
| 54 | LOG(WARNING) << getId() << " diskId change requires destroyed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 55 | return -EBUSY; |
| 56 | } |
| 57 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 58 | mDiskId = diskId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 59 | return OK; |
| 60 | } |
| 61 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 62 | status_t VolumeBase::setMountFlags(int mountFlags) { |
| 63 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 64 | LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 65 | return -EBUSY; |
| 66 | } |
| 67 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 68 | mMountFlags = mountFlags; |
| 69 | return OK; |
| 70 | } |
| 71 | |
| 72 | status_t VolumeBase::setMountUserId(userid_t mountUserId) { |
| 73 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 74 | LOG(WARNING) << getId() << " user change requires state unmounted or unmountable"; |
| 75 | return -EBUSY; |
| 76 | } |
| 77 | |
| 78 | mMountUserId = mountUserId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | return OK; |
| 80 | } |
| 81 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 82 | status_t VolumeBase::setSilent(bool silent) { |
| 83 | if (mCreated) { |
| 84 | LOG(WARNING) << getId() << " silence change requires destroyed"; |
| 85 | return -EBUSY; |
| 86 | } |
| 87 | |
| 88 | mSilent = silent; |
| 89 | return OK; |
| 90 | } |
| 91 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 92 | status_t VolumeBase::setId(const std::string& id) { |
| 93 | if (mCreated) { |
| 94 | LOG(WARNING) << getId() << " id change requires not created"; |
| 95 | return -EBUSY; |
| 96 | } |
| 97 | |
| 98 | mId = id; |
| 99 | return OK; |
| 100 | } |
| 101 | |
| 102 | status_t VolumeBase::setPath(const std::string& path) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 103 | if (mState != State::kChecking) { |
| 104 | LOG(WARNING) << getId() << " path change requires state checking"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 105 | return -EBUSY; |
| 106 | } |
| 107 | |
| 108 | mPath = path; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 109 | notifyEvent(ResponseCode::VolumePathChanged, mPath); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 110 | return OK; |
| 111 | } |
| 112 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 113 | status_t VolumeBase::setInternalPath(const std::string& internalPath) { |
| 114 | if (mState != State::kChecking) { |
| 115 | LOG(WARNING) << getId() << " internal path change requires state checking"; |
| 116 | return -EBUSY; |
| 117 | } |
| 118 | |
| 119 | mInternalPath = internalPath; |
| 120 | notifyEvent(ResponseCode::VolumeInternalPathChanged, mInternalPath); |
| 121 | return OK; |
| 122 | } |
| 123 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 124 | void VolumeBase::notifyEvent(int event) { |
| 125 | if (mSilent) return; |
| 126 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 127 | getId().c_str(), false); |
| 128 | } |
| 129 | |
| 130 | void VolumeBase::notifyEvent(int event, const std::string& value) { |
| 131 | if (mSilent) return; |
| 132 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 133 | StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false); |
| 134 | } |
| 135 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 136 | void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 137 | mVolumes.push_back(volume); |
| 138 | } |
| 139 | |
| 140 | void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 141 | mVolumes.remove(volume); |
| 142 | } |
| 143 | |
| 144 | std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) { |
| 145 | for (auto vol : mVolumes) { |
| 146 | if (vol->getId() == id) { |
| 147 | return vol; |
| 148 | } |
| 149 | } |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | status_t VolumeBase::create() { |
| 154 | CHECK(!mCreated); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 155 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 156 | mCreated = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 157 | status_t res = doCreate(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 158 | notifyEvent(ResponseCode::VolumeCreated, StringPrintf("%d %s", mType, mDiskId.c_str())); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 159 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 160 | return res; |
| 161 | } |
| 162 | |
| 163 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | status_t VolumeBase::destroy() { |
| 168 | CHECK(mCreated); |
| 169 | |
| 170 | if (mState == State::kMounted) { |
| 171 | unmount(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 172 | setState(State::kBadRemoval); |
| 173 | } else { |
| 174 | setState(State::kRemoved); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 177 | notifyEvent(ResponseCode::VolumeDestroyed); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 178 | status_t res = doDestroy(); |
| 179 | mCreated = false; |
| 180 | return res; |
| 181 | } |
| 182 | |
| 183 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 188 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 189 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | return -EBUSY; |
| 191 | } |
| 192 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 193 | setState(State::kChecking); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 194 | status_t res = doMount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 195 | if (res == OK) { |
| 196 | setState(State::kMounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 197 | } else { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 198 | setState(State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 205 | if (mState != State::kMounted) { |
| 206 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 207 | return -EBUSY; |
| 208 | } |
| 209 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 210 | setState(State::kEjecting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 211 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 212 | for (auto vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 213 | if (vol->destroy()) { |
| 214 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 215 | << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 218 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 219 | |
| 220 | status_t res = doUnmount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 222 | return res; |
| 223 | } |
| 224 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 225 | status_t VolumeBase::format(const std::string& fsType) { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 226 | if (mState == State::kMounted) { |
| 227 | unmount(); |
| 228 | } |
| 229 | |
| 230 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 231 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 232 | return -EBUSY; |
| 233 | } |
| 234 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 235 | setState(State::kFormatting); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 236 | status_t res = doFormat(fsType); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 237 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 238 | return res; |
| 239 | } |
| 240 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 241 | status_t VolumeBase::doFormat(const std::string& fsType) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 242 | return -ENOTSUP; |
| 243 | } |
| 244 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 245 | } // namespace vold |
| 246 | } // namespace android |