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