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 | |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame^] | 146 | status_t VolumeBase::setFuseFd(android::base::unique_fd fuseFd) { |
| 147 | if ((mState != State::kChecking) && (mState != State::kEjecting)) { |
| 148 | LOG(WARNING) << getId() << " fuse fd change requires state checking or ejecting"; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 149 | return -EBUSY; |
| 150 | } |
| 151 | |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame^] | 152 | mFuseFd.reset(std::move(fuseFd.get())); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 153 | return OK; |
| 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) { |
| 189 | listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid); |
| 190 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 191 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 192 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 193 | return res; |
| 194 | } |
| 195 | |
| 196 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 197 | return OK; |
| 198 | } |
| 199 | |
| 200 | status_t VolumeBase::destroy() { |
| 201 | CHECK(mCreated); |
| 202 | |
| 203 | if (mState == State::kMounted) { |
| 204 | unmount(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 205 | setState(State::kBadRemoval); |
| 206 | } else { |
| 207 | setState(State::kRemoved); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 210 | auto listener = getListener(); |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 211 | if (listener) { |
| 212 | listener->onVolumeDestroyed(getId()); |
| 213 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 214 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 215 | status_t res = doDestroy(); |
| 216 | mCreated = false; |
| 217 | return res; |
| 218 | } |
| 219 | |
| 220 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 221 | return OK; |
| 222 | } |
| 223 | |
| 224 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 225 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 226 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 227 | return -EBUSY; |
| 228 | } |
| 229 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 230 | setState(State::kChecking); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 231 | status_t res = doMount(); |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 232 | setState(res == OK ? State::kMounted : State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 233 | |
| 234 | return res; |
| 235 | } |
| 236 | |
| 237 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 238 | if (mState != State::kMounted) { |
| 239 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 240 | return -EBUSY; |
| 241 | } |
| 242 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 243 | setState(State::kEjecting); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 244 | for (const auto& vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 245 | if (vol->destroy()) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 246 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 249 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 250 | |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 251 | status_t res = doUnmount(); |
| 252 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 253 | return res; |
| 254 | } |
| 255 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 256 | status_t VolumeBase::format(const std::string& fsType) { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 257 | if (mState == State::kMounted) { |
| 258 | unmount(); |
| 259 | } |
| 260 | |
| 261 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 262 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 263 | return -EBUSY; |
| 264 | } |
| 265 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 266 | setState(State::kFormatting); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 267 | status_t res = doFormat(fsType); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 268 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 269 | return res; |
| 270 | } |
| 271 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 272 | status_t VolumeBase::doFormat(const std::string& fsType) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 273 | return -ENOTSUP; |
| 274 | } |
| 275 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 276 | std::ostream& VolumeBase::operator<<(std::ostream& stream) const { |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 277 | return stream << " VolumeBase{id=" << mId << ",mountFlags=" << mMountFlags |
| 278 | << ",mountUserId=" << mMountUserId << "}"; |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 281 | } // namespace vold |
| 282 | } // namespace android |