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 | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 138 | return res; |
| 139 | } |
| 140 | |
| 141 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 142 | return OK; |
| 143 | } |
| 144 | |
| 145 | status_t VolumeBase::destroy() { |
| 146 | CHECK(mCreated); |
| 147 | |
| 148 | if (mState == State::kMounted) { |
| 149 | unmount(); |
| 150 | } |
| 151 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 152 | notifyEvent(ResponseCode::VolumeDestroyed); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 153 | status_t res = doDestroy(); |
| 154 | mCreated = false; |
| 155 | return res; |
| 156 | } |
| 157 | |
| 158 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 159 | return OK; |
| 160 | } |
| 161 | |
| 162 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 163 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 164 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 165 | return -EBUSY; |
| 166 | } |
| 167 | |
| 168 | setState(State::kMounting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 169 | status_t res = doMount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 170 | if (res == OK) { |
| 171 | setState(State::kMounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 172 | } else { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 173 | setState(State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return res; |
| 177 | } |
| 178 | |
| 179 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | if (mState != State::kMounted) { |
| 181 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 182 | return -EBUSY; |
| 183 | } |
| 184 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 185 | setState(State::kUnmounting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 186 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 187 | for (auto vol : mVolumes) { |
| 188 | if (vol->unmount()) { |
| 189 | LOG(WARNING) << getId() << " failed to unmount " << vol->getId() |
| 190 | << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 194 | |
| 195 | status_t res = doUnmount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 196 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 197 | return res; |
| 198 | } |
| 199 | |
| 200 | status_t VolumeBase::format() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 201 | if (mState == State::kMounted) { |
| 202 | unmount(); |
| 203 | } |
| 204 | |
| 205 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 206 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 207 | return -EBUSY; |
| 208 | } |
| 209 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 210 | setState(State::kFormatting); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 211 | status_t res = doFormat(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 212 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 213 | return res; |
| 214 | } |
| 215 | |
| 216 | status_t VolumeBase::doFormat() { |
| 217 | return -ENOTSUP; |
| 218 | } |
| 219 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 220 | } // namespace vold |
| 221 | } // namespace android |