• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
21 #include "android/os/IVoldListener.h"
22 #include "android/os/IVoldMountCallback.h"
23 
24 #include <cutils/multiuser.h>
25 #include <utils/Errors.h>
26 
27 #include <sys/types.h>
28 #include <list>
29 #include <string>
30 
31 static constexpr userid_t USER_UNKNOWN = ((userid_t)-1);
32 
33 namespace android {
34 namespace vold {
35 
36 /*
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  */
50 class VolumeBase {
51   public:
52     virtual ~VolumeBase();
53 
54     enum class Type {
55         kPublic = 0,
56         kPrivate,
57         kEmulated,
58         kAsec,
59         kObb,
60         kStub,
61     };
62 
63     enum MountFlags {
64         /* Flag that volume is primary external storage */
65         kPrimary = 1 << 0,
66         /* Flag that volume is visible to normal apps */
67         kVisible = 1 << 1,
68     };
69 
70     enum class State {
71         kUnmounted = 0,
72         kChecking,
73         kMounted,
74         kMountedReadOnly,
75         kFormatting,
76         kEjecting,
77         kUnmountable,
78         kRemoved,
79         kBadRemoval,
80     };
81 
getId()82     const std::string& getId() const { return mId; }
getDiskId()83     const std::string& getDiskId() const { return mDiskId; }
getPartGuid()84     const std::string& getPartGuid() const { return mPartGuid; }
getType()85     Type getType() const { return mType; }
getMountFlags()86     int getMountFlags() const { return mMountFlags; }
getMountUserId()87     userid_t getMountUserId() const { return mMountUserId; }
getState()88     State getState() const { return mState; }
getPath()89     const std::string& getPath() const { return mPath; }
getInternalPath()90     const std::string& getInternalPath() const { return mInternalPath; }
getVolumes()91     const std::list<std::shared_ptr<VolumeBase>>& getVolumes() const { return mVolumes; }
92 
93     status_t setDiskId(const std::string& diskId);
94     status_t setPartGuid(const std::string& partGuid);
95     status_t setMountFlags(int mountFlags);
96     status_t setMountUserId(userid_t mountUserId);
97     status_t setMountCallback(const android::sp<android::os::IVoldMountCallback>& callback);
98     status_t setSilent(bool silent);
99 
100     void addVolume(const std::shared_ptr<VolumeBase>& volume);
101     void removeVolume(const std::shared_ptr<VolumeBase>& volume);
102 
103     std::shared_ptr<VolumeBase> findVolume(const std::string& id);
104 
isEmulated()105     bool isEmulated() { return mType == Type::kEmulated; }
106 
107     status_t create();
108     status_t destroy();
109     status_t mount();
110     status_t unmount();
111     status_t format(const std::string& fsType);
112 
113     virtual std::string getRootPath() const;
114 
115     std::ostream& operator<<(std::ostream& stream) const;
116 
117   protected:
118     explicit VolumeBase(Type type);
119 
120     virtual status_t doCreate();
121     virtual status_t doDestroy();
122     virtual status_t doMount() = 0;
123     virtual void doPostMount();
124     virtual status_t doUnmount() = 0;
125     virtual status_t doFormat(const std::string& fsType);
126 
127     status_t setId(const std::string& id);
128     status_t setPath(const std::string& path);
129     status_t setInternalPath(const std::string& internalPath);
130 
131     android::sp<android::os::IVoldListener> getListener() const;
132     android::sp<android::os::IVoldMountCallback> getMountCallback() const;
133 
134   private:
135     /* ID that uniquely references volume while alive */
136     std::string mId;
137     /* ID that uniquely references parent disk while alive */
138     std::string mDiskId;
139     /* Partition GUID of this volume */
140     std::string mPartGuid;
141     /* Volume type */
142     Type mType;
143     /* Flags used when mounting this volume */
144     int mMountFlags;
145     /* User that owns this volume, otherwise -1 */
146     userid_t mMountUserId;
147     /* Flag indicating object is created */
148     bool mCreated;
149     /* Current state of volume */
150     State mState;
151     /* Path to mounted volume */
152     std::string mPath;
153     /* Path to internal backing storage */
154     std::string mInternalPath;
155     /* Flag indicating that volume should emit no events */
156     bool mSilent;
157     android::sp<android::os::IVoldMountCallback> mMountCallback;
158 
159     /* Volumes stacked on top of this volume */
160     std::list<std::shared_ptr<VolumeBase>> mVolumes;
161 
162     void setState(State state);
163 
164     DISALLOW_COPY_AND_ASSIGN(VolumeBase);
165 };
166 
167 }  // namespace vold
168 }  // namespace android
169 
170 #endif
171