VolumeBase.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "VolumeBase.h"
  17. #include "Utils.h"
  18. #include "VolumeManager.h"
  19. #include <android-base/logging.h>
  20. #include <android-base/stringprintf.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <sys/mount.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. using android::base::StringPrintf;
  27. namespace android {
  28. namespace vold {
  29. VolumeBase::VolumeBase(Type type)
  30. : mType(type),
  31. mMountFlags(0),
  32. mMountUserId(USER_UNKNOWN),
  33. mCreated(false),
  34. mState(State::kUnmounted),
  35. mSilent(false) {}
  36. VolumeBase::~VolumeBase() {
  37. CHECK(!mCreated);
  38. }
  39. void VolumeBase::setState(State state) {
  40. mState = state;
  41. auto listener = getListener();
  42. if (listener) {
  43. listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState));
  44. }
  45. }
  46. status_t VolumeBase::setDiskId(const std::string& diskId) {
  47. if (mCreated) {
  48. LOG(WARNING) << getId() << " diskId change requires destroyed";
  49. return -EBUSY;
  50. }
  51. mDiskId = diskId;
  52. return OK;
  53. }
  54. status_t VolumeBase::setPartGuid(const std::string& partGuid) {
  55. if (mCreated) {
  56. LOG(WARNING) << getId() << " partGuid change requires destroyed";
  57. return -EBUSY;
  58. }
  59. mPartGuid = partGuid;
  60. return OK;
  61. }
  62. status_t VolumeBase::setMountFlags(int mountFlags) {
  63. if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
  64. LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
  65. return -EBUSY;
  66. }
  67. mMountFlags = mountFlags;
  68. return OK;
  69. }
  70. status_t VolumeBase::setMountUserId(userid_t mountUserId) {
  71. if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
  72. LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
  73. return -EBUSY;
  74. }
  75. mMountUserId = mountUserId;
  76. return OK;
  77. }
  78. status_t VolumeBase::setSilent(bool silent) {
  79. if (mCreated) {
  80. LOG(WARNING) << getId() << " silence change requires destroyed";
  81. return -EBUSY;
  82. }
  83. mSilent = silent;
  84. return OK;
  85. }
  86. status_t VolumeBase::setId(const std::string& id) {
  87. if (mCreated) {
  88. LOG(WARNING) << getId() << " id change requires not created";
  89. return -EBUSY;
  90. }
  91. mId = id;
  92. return OK;
  93. }
  94. status_t VolumeBase::setPath(const std::string& path) {
  95. if (mState != State::kChecking) {
  96. LOG(WARNING) << getId() << " path change requires state checking";
  97. return -EBUSY;
  98. }
  99. mPath = path;
  100. auto listener = getListener();
  101. if (listener) listener->onVolumePathChanged(getId(), mPath);
  102. return OK;
  103. }
  104. status_t VolumeBase::setInternalPath(const std::string& internalPath) {
  105. if (mState != State::kChecking) {
  106. LOG(WARNING) << getId() << " internal path change requires state checking";
  107. return -EBUSY;
  108. }
  109. mInternalPath = internalPath;
  110. auto listener = getListener();
  111. if (listener) {
  112. listener->onVolumeInternalPathChanged(getId(), mInternalPath);
  113. }
  114. return OK;
  115. }
  116. android::sp<android::os::IVoldListener> VolumeBase::getListener() const {
  117. if (mSilent) {
  118. return nullptr;
  119. } else {
  120. return VolumeManager::Instance()->getListener();
  121. }
  122. }
  123. void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
  124. mVolumes.push_back(volume);
  125. }
  126. void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
  127. mVolumes.remove(volume);
  128. }
  129. std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
  130. for (auto vol : mVolumes) {
  131. if (vol->getId() == id) {
  132. return vol;
  133. }
  134. }
  135. return nullptr;
  136. }
  137. status_t VolumeBase::create() {
  138. CHECK(!mCreated);
  139. mCreated = true;
  140. status_t res = doCreate();
  141. auto listener = getListener();
  142. if (listener) {
  143. listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid);
  144. }
  145. setState(State::kUnmounted);
  146. return res;
  147. }
  148. status_t VolumeBase::doCreate() {
  149. return OK;
  150. }
  151. status_t VolumeBase::destroy() {
  152. CHECK(mCreated);
  153. if (mState == State::kMounted) {
  154. unmount();
  155. setState(State::kBadRemoval);
  156. } else {
  157. setState(State::kRemoved);
  158. }
  159. auto listener = getListener();
  160. if (listener) {
  161. listener->onVolumeDestroyed(getId());
  162. }
  163. status_t res = doDestroy();
  164. mCreated = false;
  165. return res;
  166. }
  167. status_t VolumeBase::doDestroy() {
  168. return OK;
  169. }
  170. status_t VolumeBase::mount() {
  171. if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
  172. LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
  173. return -EBUSY;
  174. }
  175. setState(State::kChecking);
  176. status_t res = doMount();
  177. setState(res == OK ? State::kMounted : State::kUnmountable);
  178. return res;
  179. }
  180. status_t VolumeBase::unmount() {
  181. if (mState != State::kMounted) {
  182. LOG(WARNING) << getId() << " unmount requires state mounted";
  183. return -EBUSY;
  184. }
  185. setState(State::kEjecting);
  186. for (const auto& vol : mVolumes) {
  187. if (vol->destroy()) {
  188. LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above";
  189. }
  190. }
  191. mVolumes.clear();
  192. status_t res = doUnmount();
  193. setState(State::kUnmounted);
  194. return res;
  195. }
  196. status_t VolumeBase::format(const std::string& fsType) {
  197. if (mState == State::kMounted) {
  198. unmount();
  199. }
  200. if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
  201. LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
  202. return -EBUSY;
  203. }
  204. setState(State::kFormatting);
  205. status_t res = doFormat(fsType);
  206. setState(State::kUnmounted);
  207. return res;
  208. }
  209. status_t VolumeBase::doFormat(const std::string& fsType) {
  210. return -ENOTSUP;
  211. }
  212. std::ostream& VolumeBase::operator<<(std::ostream& stream) const {
  213. return stream << " VolumeBase{id=" << mId << ",mountFlags=" << mMountFlags
  214. << ",mountUserId=" << mMountUserId << "}";
  215. }
  216. } // namespace vold
  217. } // namespace android