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 | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 39 | mType(type), mFlags(0), mUser(-1), mCreated(false), mState( |
| 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 | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 52 | status_t VolumeBase::setFlags(int flags) { |
| 53 | if (mState != State::kUnmounted) { |
| 54 | LOG(WARNING) << getId() << " flags change requires state unmounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 55 | return -EBUSY; |
| 56 | } |
| 57 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 58 | mFlags = flags; |
| 59 | return OK; |
| 60 | } |
| 61 | |
| 62 | status_t VolumeBase::setUser(userid_t user) { |
| 63 | if (mState != State::kUnmounted) { |
| 64 | LOG(WARNING) << getId() << " user change requires state unmounted"; |
| 65 | return -EBUSY; |
| 66 | } |
| 67 | |
| 68 | mUser = user; |
| 69 | return OK; |
| 70 | } |
| 71 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 72 | status_t VolumeBase::setSilent(bool silent) { |
| 73 | if (mCreated) { |
| 74 | LOG(WARNING) << getId() << " silence change requires destroyed"; |
| 75 | return -EBUSY; |
| 76 | } |
| 77 | |
| 78 | mSilent = silent; |
| 79 | return OK; |
| 80 | } |
| 81 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | status_t VolumeBase::setId(const std::string& id) { |
| 83 | if (mCreated) { |
| 84 | LOG(WARNING) << getId() << " id change requires not created"; |
| 85 | return -EBUSY; |
| 86 | } |
| 87 | |
| 88 | mId = id; |
| 89 | return OK; |
| 90 | } |
| 91 | |
| 92 | status_t VolumeBase::setPath(const std::string& path) { |
| 93 | if (mState != State::kMounting) { |
| 94 | LOG(WARNING) << getId() << " path change requires state mounting"; |
| 95 | return -EBUSY; |
| 96 | } |
| 97 | |
| 98 | mPath = path; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 99 | notifyEvent(ResponseCode::VolumePathChanged, mPath); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 100 | return OK; |
| 101 | } |
| 102 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 103 | void VolumeBase::notifyEvent(int event) { |
| 104 | if (mSilent) return; |
| 105 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 106 | getId().c_str(), false); |
| 107 | } |
| 108 | |
| 109 | void VolumeBase::notifyEvent(int event, const std::string& value) { |
| 110 | if (mSilent) return; |
| 111 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 112 | StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false); |
| 113 | } |
| 114 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 115 | void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 116 | mVolumes.push_back(volume); |
| 117 | } |
| 118 | |
| 119 | void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 120 | mVolumes.remove(volume); |
| 121 | } |
| 122 | |
| 123 | std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) { |
| 124 | for (auto vol : mVolumes) { |
| 125 | if (vol->getId() == id) { |
| 126 | return vol; |
| 127 | } |
| 128 | } |
| 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | status_t VolumeBase::create() { |
| 133 | CHECK(!mCreated); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 135 | mCreated = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 136 | status_t res = doCreate(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 137 | notifyEvent(ResponseCode::VolumeCreated, StringPrintf("%d", mType)); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame^] | 138 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 139 | return res; |
| 140 | } |
| 141 | |
| 142 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 143 | return OK; |
| 144 | } |
| 145 | |
| 146 | status_t VolumeBase::destroy() { |
| 147 | CHECK(mCreated); |
| 148 | |
| 149 | if (mState == State::kMounted) { |
| 150 | unmount(); |
| 151 | } |
| 152 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame^] | 153 | setState(State::kRemoved); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 154 | notifyEvent(ResponseCode::VolumeDestroyed); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 155 | status_t res = doDestroy(); |
| 156 | mCreated = false; |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 161 | return OK; |
| 162 | } |
| 163 | |
| 164 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 165 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 166 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 167 | return -EBUSY; |
| 168 | } |
| 169 | |
| 170 | setState(State::kMounting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 171 | status_t res = doMount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 172 | if (res == OK) { |
| 173 | setState(State::kMounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 174 | } else { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 175 | setState(State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return res; |
| 179 | } |
| 180 | |
| 181 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 182 | if (mState != State::kMounted) { |
| 183 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 184 | return -EBUSY; |
| 185 | } |
| 186 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 187 | setState(State::kUnmounting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 188 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 189 | for (auto vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame^] | 190 | if (vol->destroy()) { |
| 191 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 192 | << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 195 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 196 | |
| 197 | status_t res = doUnmount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 199 | return res; |
| 200 | } |
| 201 | |
| 202 | status_t VolumeBase::format() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 203 | if (mState == State::kMounted) { |
| 204 | unmount(); |
| 205 | } |
| 206 | |
| 207 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 208 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 209 | return -EBUSY; |
| 210 | } |
| 211 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 212 | setState(State::kFormatting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 213 | status_t res = doFormat(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 214 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 215 | return res; |
| 216 | } |
| 217 | |
| 218 | status_t VolumeBase::doFormat() { |
| 219 | return -ENOTSUP; |
| 220 | } |
| 221 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 222 | } // namespace vold |
| 223 | } // namespace android |