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