blob: f29df651b71bb00e469794321ca28ae0478af962 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2008 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
17#ifndef ANDROID_VOLD_VOLUME_BASE_H
18#define ANDROID_VOLD_VOLUME_BASE_H
19
20#include "Utils.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070021#include "android/os/IVoldListener.h"
Zim5048b4b2019-11-19 09:16:03 +000022#include "android/os/IVoldMountCallback.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include <cutils/multiuser.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <utils/Errors.h>
26
27#include <sys/types.h>
28#include <list>
29#include <string>
30
Sudheer Shanka40ab6742018-09-18 13:07:45 -070031static constexpr userid_t USER_UNKNOWN = ((userid_t)-1);
32
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033namespace android {
34namespace vold {
35
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036/*
37 * Representation of a mounted volume ready for presentation.
38 *
39 * Various subclasses handle the different mounting prerequisites, such as
40 * encryption details, etc. Volumes can also be "stacked" above other
41 * volumes to help communicate dependencies. For example, an ASEC volume
42 * can be stacked on a vfat volume.
43 *
44 * Mounted volumes can be asked to manage bind mounts to present themselves
45 * to specific users on the device.
46 *
47 * When an unmount is requested, the volume recursively unmounts any stacked
48 * volumes and removes any bind mounts before finally unmounting itself.
49 */
50class VolumeBase {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070051 public:
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052 virtual ~VolumeBase();
53
Jeff Sharkey36801cc2015-03-13 16:09:20 -070054 enum class Type {
55 kPublic = 0,
56 kPrivate,
57 kEmulated,
58 kAsec,
59 kObb,
Risan8c9f3322018-10-29 08:52:56 +090060 kStub,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061 };
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070063 enum MountFlags {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070064 /* Flag that volume is primary external storage */
65 kPrimary = 1 << 0,
Youkichi Hosoi4461c5a2021-11-13 16:27:02 +090066 /*
67 * Flags indicating that volume is visible to normal apps.
68 * kVisibleForRead and kVisibleForWrite correspond to
69 * VolumeInfo.MOUNT_FLAG_VISIBLE_FOR_READ and
70 * VolumeInfo.MOUNT_FLAG_VISIBLE_FOR_WRITE, respectively.
71 */
72 kVisibleForRead = 1 << 1,
73 kVisibleForWrite = 1 << 2,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 };
75
76 enum class State {
77 kUnmounted = 0,
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070078 kChecking,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079 kMounted,
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070080 kMountedReadOnly,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 kFormatting,
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070082 kEjecting,
Jeff Sharkey0fd95352015-04-04 21:38:59 -070083 kUnmountable,
Jeff Sharkey3161fb32015-04-12 16:03:33 -070084 kRemoved,
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070085 kBadRemoval,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086 };
87
Greg Kaiser2bc201e2018-12-18 08:42:08 -080088 const std::string& getId() const { return mId; }
89 const std::string& getDiskId() const { return mDiskId; }
90 const std::string& getPartGuid() const { return mPartGuid; }
91 Type getType() const { return mType; }
92 int getMountFlags() const { return mMountFlags; }
93 userid_t getMountUserId() const { return mMountUserId; }
94 State getState() const { return mState; }
95 const std::string& getPath() const { return mPath; }
96 const std::string& getInternalPath() const { return mInternalPath; }
Zima438b242019-09-25 14:37:38 +010097 const std::list<std::shared_ptr<VolumeBase>>& getVolumes() const { return mVolumes; }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070099 status_t setDiskId(const std::string& diskId);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700100 status_t setPartGuid(const std::string& partGuid);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700101 status_t setMountFlags(int mountFlags);
102 status_t setMountUserId(userid_t mountUserId);
Zim5048b4b2019-11-19 09:16:03 +0000103 status_t setMountCallback(const android::sp<android::os::IVoldMountCallback>& callback);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700104 status_t setSilent(bool silent);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700105
106 void addVolume(const std::shared_ptr<VolumeBase>& volume);
107 void removeVolume(const std::shared_ptr<VolumeBase>& volume);
108
109 std::shared_ptr<VolumeBase> findVolume(const std::string& id);
110
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700111 bool isEmulated() { return mType == Type::kEmulated; }
Youkichi Hosoi4461c5a2021-11-13 16:27:02 +0900112 bool isVisibleForRead() const { return (mMountFlags & MountFlags::kVisibleForRead) != 0; }
113 bool isVisibleForWrite() const { return (mMountFlags & MountFlags::kVisibleForWrite) != 0; }
114 bool isVisible() const { return isVisibleForRead() || isVisibleForWrite(); }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700115
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 status_t create();
117 status_t destroy();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800118 status_t mount();
119 status_t unmount();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700120 status_t format(const std::string& fsType);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121
Martijn Coenen62a4b272020-01-31 15:23:09 +0100122 virtual std::string getRootPath() const;
123
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700124 std::ostream& operator<<(std::ostream& stream) const;
125
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700126 protected:
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700127 explicit VolumeBase(Type type);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800128
Jeff Sharkey9c484982015-03-31 10:35:33 -0700129 virtual status_t doCreate();
130 virtual status_t doDestroy();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131 virtual status_t doMount() = 0;
Martijn Coenen5ec86582020-05-04 14:57:35 +0200132 virtual void doPostMount();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800133 virtual status_t doUnmount() = 0;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700134 virtual status_t doFormat(const std::string& fsType);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700136 status_t setId(const std::string& id);
137 status_t setPath(const std::string& path);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700138 status_t setInternalPath(const std::string& internalPath);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800140 android::sp<android::os::IVoldListener> getListener() const;
Zim5048b4b2019-11-19 09:16:03 +0000141 android::sp<android::os::IVoldMountCallback> getMountCallback() const;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600142
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700143 private:
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700144 /* ID that uniquely references volume while alive */
145 std::string mId;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700146 /* ID that uniquely references parent disk while alive */
147 std::string mDiskId;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700148 /* Partition GUID of this volume */
149 std::string mPartGuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800150 /* Volume type */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700151 Type mType;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700152 /* Flags used when mounting this volume */
153 int mMountFlags;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700154 /* User that owns this volume, otherwise -1 */
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700155 userid_t mMountUserId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156 /* Flag indicating object is created */
157 bool mCreated;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800158 /* Current state of volume */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 State mState;
160 /* Path to mounted volume */
161 std::string mPath;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700162 /* Path to internal backing storage */
163 std::string mInternalPath;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700164 /* Flag indicating that volume should emit no events */
165 bool mSilent;
Zim5048b4b2019-11-19 09:16:03 +0000166 android::sp<android::os::IVoldMountCallback> mMountCallback;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800167
168 /* Volumes stacked on top of this volume */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 std::list<std::shared_ptr<VolumeBase>> mVolumes;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 void setState(State state);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800172
173 DISALLOW_COPY_AND_ASSIGN(VolumeBase);
174};
175
176} // namespace vold
177} // namespace android
178
179#endif