2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_VOLD_VOLUME_MANAGER_H
18 #define ANDROID_VOLD_VOLUME_MANAGER_H
28 #include <unordered_map>
29 #include <unordered_set>
31 #include <android-base/unique_fd.h>
32 #include <cutils/multiuser.h>
33 #include <sysutils/NetlinkEvent.h>
34 #include <utils/List.h>
35 #include <utils/Timers.h>
37 #include "android/os/IVoldListener.h"
39 #include "model/Disk.h"
40 #include "model/VolumeBase.h"
44 static VolumeManager* sInstance;
49 virtual ~VolumeManager();
51 // TODO: pipe all requests through VM to avoid exposing this lock
52 std::mutex& getLock() { return mLock; }
53 std::mutex& getCryptLock() { return mCryptLock; }
55 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
56 android::sp<android::os::IVoldListener> getListener() const { return mListener; }
60 void handleBlockEvent(NetlinkEvent* evt);
64 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
65 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
67 bool matches(const std::string& sysPath) {
68 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
71 const std::string& getNickname() const { return mNickname; }
72 int getFlags() const { return mFlags; }
75 std::string mSysPattern;
76 std::string mNickname;
80 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
82 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
83 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
85 template <typename Fn>
86 std::shared_ptr<android::vold::VolumeBase> findVolumeWithFilter(Fn fn) {
87 for (const auto& vol : mInternalEmulatedVolumes) {
92 for (const auto& disk : mDisks) {
93 for (const auto& vol : disk->getVolumes()) {
103 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
105 const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; }
107 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
109 int onUserAdded(userid_t userId, int userSerialNumber);
110 int onUserRemoved(userid_t userId);
111 int onUserStarted(userid_t userId);
112 int onUserStopped(userid_t userId);
114 void createPendingDisksIfNeeded();
115 int onSecureKeyguardStateChanged(bool isShowing);
117 int remountUid(uid_t uid, int32_t remountMode) { return 0; }
118 int handleAppStorageDirs(int uid, int pid,
119 bool doUnmount, const std::vector<std::string>& packageNames);
121 /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */
123 /* Reset all internal state, typically during framework boot */
125 /* Prepare for device shutdown, safely unmounting all devices */
127 /* Unmount all volumes, usually for encryption */
130 int updateVirtualDisk();
131 int setDebug(bool enable);
133 bool forkAndRemountStorage(int uid, int pid, bool doUnmount,
134 const std::vector<std::string>& packageNames);
136 static VolumeManager* Instance();
139 * Creates a directory 'path' for an application, automatically creating
140 * directories along the given path if they don't exist yet.
143 * path = /storage/emulated/0/Android/data/com.foo/files/
145 * This function will first match the first part of the path with the volume
146 * root of any known volumes; in this case, "/storage/emulated/0" matches
147 * with the volume root of the emulated volume for user 0.
149 * The subseqent part of the path must start with one of the well-known
150 * Android/ data directories, /Android/data, /Android/obb or
153 * The final part of the path is application specific. This function will
154 * create all directories, including the application-specific ones, and
155 * set the UID of all app-specific directories below the well-known data
156 * directories to the 'appUid' argument. In the given example, the UID
157 * of /storage/emulated/0/Android/data/com.foo and
158 * /storage/emulated/0/Android/data/com.foo/files would be set to 'appUid'.
160 * The UID/GID of the parent directories will be set according to the
161 * requirements of the underlying filesystem and are of no concern to the
164 * If fixupExistingOnly is set, we make sure to fixup any existing dirs and
165 * files in the passed in path, but only if that path exists; if it doesn't
166 * exist, this function doesn't create them.
168 * If skipIfDirExists is set, we will not fix any existing dirs, we will
169 * only create app dirs if it doesn't exist.
171 * Validates that given paths are absolute and that they contain no relative
172 * "." or ".." paths or symlinks. Last path segment is treated as filename
173 * and ignored, unless the path ends with "/". Also ensures that path
174 * belongs to a volume managed by vold.
176 int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false,
177 bool skipIfDirExists = false);
180 * Fixes up an existing application directory, as if it was created with
181 * setupAppDir() above. This includes fixing up the UID/GID, permissions and
182 * project IDs of the contained files and directories.
184 int fixupAppDir(const std::string& path, int32_t appUid);
186 // Called before zygote starts to ensure dir exists so zygote can bind mount them.
187 int ensureAppDirsCreated(const std::vector<std::string>& paths, int32_t appUid);
189 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
190 std::string* outVolId);
191 int destroyObb(const std::string& volId);
193 int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
194 const std::string& fsType, const std::string& fsUuid,
195 const std::string& fsLabel, int32_t flags, std::string* outVolId);
196 int destroyStubVolume(const std::string& volId);
198 int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
199 int unmountAppFuse(uid_t uid, int mountId);
200 int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
204 void readInitialState();
206 int linkPrimary(userid_t userId);
208 void createEmulatedVolumesForUser(userid_t userId);
209 void destroyEmulatedVolumesForUser(userid_t userId);
211 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
212 void handleDiskChanged(dev_t device);
213 void handleDiskRemoved(dev_t device);
215 bool updateFuseMountedProperty();
218 std::mutex mCryptLock;
220 android::sp<android::os::IVoldListener> mListener;
222 std::list<std::shared_ptr<DiskSource>> mDiskSources;
223 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
224 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
225 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
226 std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes;
228 std::unordered_map<userid_t, int> mAddedUsers;
229 // This needs to be a regular set because we care about the ordering here;
230 // user 0 should always go first, because it is responsible for sdcardfs.
231 std::set<userid_t> mStartedUsers;
233 std::string mVirtualDiskPath;
234 std::shared_ptr<android::vold::Disk> mVirtualDisk;
235 std::shared_ptr<android::vold::VolumeBase> mPrimary;
239 bool mSecureKeyguardShowing;