• 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 
23 #include <cutils/multiuser.h>
24 #include <utils/Errors.h>
25 
26 #include <sys/types.h>
27 #include <list>
28 #include <string>
29 
30 static constexpr userid_t USER_UNKNOWN = ((userid_t)-1);
31 
32 namespace android {
33 namespace vold {
34 
35 /*
36  * Representation of a mounted volume ready for presentation.
37  *
38  * Various subclasses handle the different mounting prerequisites, such as
39  * encryption details, etc.  Volumes can also be "stacked" above other
40  * volumes to help communicate dependencies.  For example, an ASEC volume
41  * can be stacked on a vfat volume.
42  *
43  * Mounted volumes can be asked to manage bind mounts to present themselves
44  * to specific users on the device.
45  *
46  * When an unmount is requested, the volume recursively unmounts any stacked
47  * volumes and removes any bind mounts before finally unmounting itself.
48  */
49 class VolumeBase {
50   public:
51     virtual ~VolumeBase();
52 
53     enum class Type {
54         kPublic = 0,
55         kPrivate,
56         kEmulated,
57         kAsec,
58         kObb,
59         kStub,
60     };
61 
62     enum MountFlags {
63         /* Flag that volume is primary external storage */
64         kPrimary = 1 << 0,
65         /* Flag that volume is visible to normal apps */
66         kVisible = 1 << 1,
67     };
68 
69     enum class State {
70         kUnmounted = 0,
71         kChecking,
72         kMounted,
73         kMountedReadOnly,
74         kFormatting,
75         kEjecting,
76         kUnmountable,
77         kRemoved,
78         kBadRemoval,
79     };
80 
getId()81     const std::string& getId() const { return mId; }
getDiskId()82     const std::string& getDiskId() const { return mDiskId; }
getPartGuid()83     const std::string& getPartGuid() const { return mPartGuid; }
getType()84     Type getType() const { return mType; }
getMountFlags()85     int getMountFlags() const { return mMountFlags; }
getMountUserId()86     userid_t getMountUserId() const { return mMountUserId; }
getState()87     State getState() const { return mState; }
getPath()88     const std::string& getPath() const { return mPath; }
getInternalPath()89     const std::string& getInternalPath() const { return mInternalPath; }
90 
91     status_t setDiskId(const std::string& diskId);
92     status_t setPartGuid(const std::string& partGuid);
93     status_t setMountFlags(int mountFlags);
94     status_t setMountUserId(userid_t mountUserId);
95     status_t setSilent(bool silent);
96 
97     void addVolume(const std::shared_ptr<VolumeBase>& volume);
98     void removeVolume(const std::shared_ptr<VolumeBase>& volume);
99 
100     std::shared_ptr<VolumeBase> findVolume(const std::string& id);
101 
isEmulated()102     bool isEmulated() { return mType == Type::kEmulated; }
103 
104     status_t create();
105     status_t destroy();
106     status_t mount();
107     status_t unmount();
108     status_t format(const std::string& fsType);
109 
110     std::ostream& operator<<(std::ostream& stream) const;
111 
112   protected:
113     explicit VolumeBase(Type type);
114 
115     virtual status_t doCreate();
116     virtual status_t doDestroy();
117     virtual status_t doMount() = 0;
118     virtual status_t doUnmount() = 0;
119     virtual status_t doFormat(const std::string& fsType);
120 
121     status_t setId(const std::string& id);
122     status_t setPath(const std::string& path);
123     status_t setInternalPath(const std::string& internalPath);
124 
125     android::sp<android::os::IVoldListener> getListener() const;
126 
127   private:
128     /* ID that uniquely references volume while alive */
129     std::string mId;
130     /* ID that uniquely references parent disk while alive */
131     std::string mDiskId;
132     /* Partition GUID of this volume */
133     std::string mPartGuid;
134     /* Volume type */
135     Type mType;
136     /* Flags used when mounting this volume */
137     int mMountFlags;
138     /* User that owns this volume, otherwise -1 */
139     userid_t mMountUserId;
140     /* Flag indicating object is created */
141     bool mCreated;
142     /* Current state of volume */
143     State mState;
144     /* Path to mounted volume */
145     std::string mPath;
146     /* Path to internal backing storage */
147     std::string mInternalPath;
148     /* Flag indicating that volume should emit no events */
149     bool mSilent;
150 
151     /* Volumes stacked on top of this volume */
152     std::list<std::shared_ptr<VolumeBase>> mVolumes;
153 
154     void setState(State state);
155 
156     DISALLOW_COPY_AND_ASSIGN(VolumeBase);
157 };
158 
159 }  // namespace vold
160 }  // namespace android
161 
162 #endif
163