• 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         /*
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,
74     };
75 
76     enum class State {
77         kUnmounted = 0,
78         kChecking,
79         kMounted,
80         kMountedReadOnly,
81         kFormatting,
82         kEjecting,
83         kUnmountable,
84         kRemoved,
85         kBadRemoval,
86     };
87 
getId()88     const std::string& getId() const { return mId; }
getDiskId()89     const std::string& getDiskId() const { return mDiskId; }
getPartGuid()90     const std::string& getPartGuid() const { return mPartGuid; }
getType()91     Type getType() const { return mType; }
getMountFlags()92     int getMountFlags() const { return mMountFlags; }
getMountUserId()93     userid_t getMountUserId() const { return mMountUserId; }
getState()94     State getState() const { return mState; }
getPath()95     const std::string& getPath() const { return mPath; }
getInternalPath()96     const std::string& getInternalPath() const { return mInternalPath; }
getVolumes()97     const std::list<std::shared_ptr<VolumeBase>>& getVolumes() const { return mVolumes; }
98 
99     status_t setDiskId(const std::string& diskId);
100     status_t setPartGuid(const std::string& partGuid);
101     status_t setMountFlags(int mountFlags);
102     status_t setMountUserId(userid_t mountUserId);
103     status_t setMountCallback(const android::sp<android::os::IVoldMountCallback>& callback);
104     status_t setSilent(bool silent);
105 
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 
isEmulated()111     bool isEmulated() { return mType == Type::kEmulated; }
isVisibleForRead()112     bool isVisibleForRead() const { return (mMountFlags & MountFlags::kVisibleForRead) != 0; }
isVisibleForWrite()113     bool isVisibleForWrite() const { return (mMountFlags & MountFlags::kVisibleForWrite) != 0; }
isVisible()114     bool isVisible() const { return isVisibleForRead() || isVisibleForWrite(); }
115 
116     status_t create();
117     status_t destroy();
118     status_t mount();
119     status_t unmount();
120     status_t format(const std::string& fsType);
121 
122     virtual std::string getRootPath() const;
123 
124     std::ostream& operator<<(std::ostream& stream) const;
125 
126   protected:
127     explicit VolumeBase(Type type);
128 
129     virtual status_t doCreate();
130     virtual status_t doDestroy();
131     virtual status_t doMount() = 0;
132     virtual void doPostMount();
133     virtual status_t doUnmount() = 0;
134     virtual status_t doFormat(const std::string& fsType);
135 
136     status_t setId(const std::string& id);
137     status_t setPath(const std::string& path);
138     status_t setInternalPath(const std::string& internalPath);
139 
140     android::sp<android::os::IVoldListener> getListener() const;
141     android::sp<android::os::IVoldMountCallback> getMountCallback() const;
142 
143   private:
144     /* ID that uniquely references volume while alive */
145     std::string mId;
146     /* ID that uniquely references parent disk while alive */
147     std::string mDiskId;
148     /* Partition GUID of this volume */
149     std::string mPartGuid;
150     /* Volume type */
151     Type mType;
152     /* Flags used when mounting this volume */
153     int mMountFlags;
154     /* User that owns this volume, otherwise -1 */
155     userid_t mMountUserId;
156     /* Flag indicating object is created */
157     bool mCreated;
158     /* Current state of volume */
159     State mState;
160     /* Path to mounted volume */
161     std::string mPath;
162     /* Path to internal backing storage */
163     std::string mInternalPath;
164     /* Flag indicating that volume should emit no events */
165     bool mSilent;
166     android::sp<android::os::IVoldMountCallback> mMountCallback;
167 
168     /* Volumes stacked on top of this volume */
169     std::list<std::shared_ptr<VolumeBase>> mVolumes;
170 
171     void setState(State state);
172 
173     DISALLOW_COPY_AND_ASSIGN(VolumeBase);
174 };
175 
176 }  // namespace vold
177 }  // namespace android
178 
179 #endif
180