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