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 "VolumeBase.h" |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 20 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/mount.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 30 | using android::base::StringPrintf; |
| 31 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace vold { |
| 34 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 35 | VolumeBase::VolumeBase(Type type) |
| 36 | : mType(type), |
| 37 | mMountFlags(0), |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 38 | mMountUserId(USER_UNKNOWN), |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 39 | mCreated(false), |
| 40 | mState(State::kUnmounted), |
| 41 | mSilent(false) {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 49 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 50 | auto listener = getListener(); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 51 | if (listener) { |
| 52 | listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState)); |
| 53 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 56 | status_t VolumeBase::setDiskId(const std::string& diskId) { |
| 57 | if (mCreated) { |
| 58 | LOG(WARNING) << getId() << " diskId change requires destroyed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | return -EBUSY; |
| 60 | } |
| 61 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 62 | mDiskId = diskId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 63 | return OK; |
| 64 | } |
| 65 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 66 | status_t VolumeBase::setPartGuid(const std::string& partGuid) { |
| 67 | if (mCreated) { |
| 68 | LOG(WARNING) << getId() << " partGuid change requires destroyed"; |
| 69 | return -EBUSY; |
| 70 | } |
| 71 | |
| 72 | mPartGuid = partGuid; |
| 73 | return OK; |
| 74 | } |
| 75 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 76 | status_t VolumeBase::setMountFlags(int mountFlags) { |
| 77 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 78 | LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | return -EBUSY; |
| 80 | } |
| 81 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 82 | mMountFlags = mountFlags; |
| 83 | return OK; |
| 84 | } |
| 85 | |
| 86 | status_t VolumeBase::setMountUserId(userid_t mountUserId) { |
| 87 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 88 | LOG(WARNING) << getId() << " user change requires state unmounted or unmountable"; |
| 89 | return -EBUSY; |
| 90 | } |
| 91 | |
| 92 | mMountUserId = mountUserId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 93 | return OK; |
| 94 | } |
| 95 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 96 | status_t VolumeBase::setSilent(bool silent) { |
| 97 | if (mCreated) { |
| 98 | LOG(WARNING) << getId() << " silence change requires destroyed"; |
| 99 | return -EBUSY; |
| 100 | } |
| 101 | |
| 102 | mSilent = silent; |
| 103 | return OK; |
| 104 | } |
| 105 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 106 | status_t VolumeBase::setId(const std::string& id) { |
| 107 | if (mCreated) { |
| 108 | LOG(WARNING) << getId() << " id change requires not created"; |
| 109 | return -EBUSY; |
| 110 | } |
| 111 | |
| 112 | mId = id; |
| 113 | return OK; |
| 114 | } |
| 115 | |
| 116 | status_t VolumeBase::setPath(const std::string& path) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 117 | if (mState != State::kChecking) { |
| 118 | LOG(WARNING) << getId() << " path change requires state checking"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 119 | return -EBUSY; |
| 120 | } |
| 121 | |
| 122 | mPath = path; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 123 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 124 | auto listener = getListener(); |
| 125 | if (listener) listener->onVolumePathChanged(getId(), mPath); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 126 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 127 | return OK; |
| 128 | } |
| 129 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 130 | status_t VolumeBase::setInternalPath(const std::string& internalPath) { |
| 131 | if (mState != State::kChecking) { |
| 132 | LOG(WARNING) << getId() << " internal path change requires state checking"; |
| 133 | return -EBUSY; |
| 134 | } |
| 135 | |
| 136 | mInternalPath = internalPath; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 137 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 138 | auto listener = getListener(); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 139 | if (listener) { |
| 140 | listener->onVolumeInternalPathChanged(getId(), mInternalPath); |
| 141 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 142 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 143 | return OK; |
| 144 | } |
| 145 | |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 146 | status_t VolumeBase::setMountCallback( |
| 147 | const android::sp<android::os::IVoldMountCallback>& callback) { |
| 148 | mMountCallback = callback; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 149 | return OK; |
| 150 | } |
| 151 | |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 152 | sp<android::os::IVoldMountCallback> VolumeBase::getMountCallback() const { |
| 153 | return mMountCallback; |
| 154 | } |
| 155 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 156 | android::sp<android::os::IVoldListener> VolumeBase::getListener() const { |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 157 | if (mSilent) { |
| 158 | return nullptr; |
| 159 | } else { |
| 160 | return VolumeManager::Instance()->getListener(); |
| 161 | } |
| 162 | } |
| 163 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 164 | void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 165 | mVolumes.push_back(volume); |
| 166 | } |
| 167 | |
| 168 | void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 169 | mVolumes.remove(volume); |
| 170 | } |
| 171 | |
| 172 | std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) { |
| 173 | for (auto vol : mVolumes) { |
| 174 | if (vol->getId() == id) { |
| 175 | return vol; |
| 176 | } |
| 177 | } |
| 178 | return nullptr; |
| 179 | } |
| 180 | |
| 181 | status_t VolumeBase::create() { |
| 182 | CHECK(!mCreated); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 183 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 184 | mCreated = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 185 | status_t res = doCreate(); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 186 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 187 | auto listener = getListener(); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 188 | if (listener) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 189 | listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid, |
| 190 | mMountUserId); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 191 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 192 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 193 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 194 | return res; |
| 195 | } |
| 196 | |
| 197 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | return OK; |
| 199 | } |
| 200 | |
| 201 | status_t VolumeBase::destroy() { |
| 202 | CHECK(mCreated); |
| 203 | |
| 204 | if (mState == State::kMounted) { |
| 205 | unmount(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 206 | setState(State::kBadRemoval); |
| 207 | } else { |
| 208 | setState(State::kRemoved); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 211 | auto listener = getListener(); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 212 | if (listener) { |
| 213 | listener->onVolumeDestroyed(getId()); |
| 214 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 215 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 216 | status_t res = doDestroy(); |
| 217 | mCreated = false; |
| 218 | return res; |
| 219 | } |
| 220 | |
| 221 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | return OK; |
| 223 | } |
| 224 | |
| 225 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 226 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 227 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 228 | return -EBUSY; |
| 229 | } |
| 230 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 231 | setState(State::kChecking); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 232 | status_t res = doMount(); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 233 | setState(res == OK ? State::kMounted : State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 234 | |
Martijn Coenen | 5ec8658 | 2020-05-04 14:57:35 +0200 | [diff] [blame] | 235 | if (res == OK) { |
| 236 | doPostMount(); |
| 237 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 238 | return res; |
| 239 | } |
| 240 | |
Martijn Coenen | 5ec8658 | 2020-05-04 14:57:35 +0200 | [diff] [blame] | 241 | void VolumeBase::doPostMount() {} |
| 242 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 243 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 244 | if (mState != State::kMounted) { |
| 245 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 246 | return -EBUSY; |
| 247 | } |
| 248 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 249 | setState(State::kEjecting); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 250 | for (const auto& vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 251 | if (vol->destroy()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 252 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 255 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 256 | |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 257 | status_t res = doUnmount(); |
| 258 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 259 | return res; |
| 260 | } |
| 261 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 262 | status_t VolumeBase::format(const std::string& fsType) { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 263 | if (mState == State::kMounted) { |
| 264 | unmount(); |
| 265 | } |
| 266 | |
| 267 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 268 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 269 | return -EBUSY; |
| 270 | } |
| 271 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 272 | setState(State::kFormatting); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 273 | status_t res = doFormat(fsType); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 274 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 275 | return res; |
| 276 | } |
| 277 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 278 | status_t VolumeBase::doFormat(const std::string& fsType) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 279 | return -ENOTSUP; |
| 280 | } |
| 281 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 282 | std::string VolumeBase::getRootPath() const { |
| 283 | // Usually the same as the internal path, except for emulated volumes. |
| 284 | return getInternalPath(); |
| 285 | } |
| 286 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 287 | std::ostream& VolumeBase::operator<<(std::ostream& stream) const { |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 288 | return stream << " VolumeBase{id=" << mId << ",mountFlags=" << mMountFlags |
| 289 | << ",mountUserId=" << mMountUserId << "}"; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 292 | } // namespace vold |
| 293 | } // namespace android |