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
27 #include <unordered_map>
28 #include <unordered_set>
30 #include <android-base/unique_fd.h>
31 #include <cutils/multiuser.h>
32 #include <utils/List.h>
33 #include <utils/Timers.h>
34 #include <sysutils/NetlinkEvent.h>
36 #include "android/os/IVoldListener.h"
38 #include "model/Disk.h"
39 #include "model/DiskPartition.h"
40 #include "model/VolumeBase.h"
42 #define DEBUG_APPFUSE 0
46 static VolumeManager *sInstance;
51 virtual ~VolumeManager();
53 // TODO: pipe all requests through VM to avoid exposing this lock
54 std::mutex& getLock() { return mLock; }
55 std::mutex& getCryptLock() { return mCryptLock; }
57 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
58 android::sp<android::os::IVoldListener> getListener() { return mListener; }
63 void handleBlockEvent(NetlinkEvent *evt);
67 DiskSource(const std::string& sysPattern, const std::string& nickname,
68 int partnum, int flags,
69 const std::string& fstype, const std::string mntopts) :
70 mSysPattern(sysPattern), mNickname(nickname),
71 mPartNum(partnum), mFlags(flags),
72 mFsType(fstype), mMntOpts(mntopts) {
75 bool matches(const std::string& sysPath) {
76 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
79 const std::string& getNickname() { return mNickname; }
80 int getPartNum() { return mPartNum; }
81 int getFlags() { return mFlags; }
82 const std::string& getFsType() { return mFsType; }
83 const std::string& getMntOpts() { return mMntOpts; }
86 std::string mSysPattern;
87 std::string mNickname;
94 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
96 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
97 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
99 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
101 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
103 int onUserAdded(userid_t userId, int userSerialNumber);
104 int onUserRemoved(userid_t userId);
105 int onUserStarted(userid_t userId);
106 int onUserStopped(userid_t userId);
108 int onSecureKeyguardStateChanged(bool isShowing);
110 int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
112 int remountUid(uid_t uid, const std::string& mode);
114 /* Reset all internal state, typically during framework boot */
116 /* Prepare for device shutdown, safely unmounting all devices */
118 /* Unmount all volumes, usually for encryption */
121 int updateVirtualDisk();
122 int setDebug(bool enable);
124 static VolumeManager *Instance();
127 * Ensure that all directories along given path exist, creating parent
128 * directories as needed. Validates that given path is absolute and that
129 * it contains no relative "." or ".." paths or symlinks. Last path segment
130 * is treated as filename and ignored, unless the path ends with "/". Also
131 * ensures that path belongs to a volume managed by vold.
133 int mkdirs(const std::string& path);
135 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
136 std::string* outVolId);
137 int destroyObb(const std::string& volId);
139 int mountAppFuse(uid_t uid, pid_t pid, int mountId, android::base::unique_fd* device_fd);
140 int unmountAppFuse(uid_t uid, pid_t pid, int mountId);
144 void readInitialState();
146 int linkPrimary(userid_t userId);
148 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
149 void handleDiskChanged(dev_t device);
150 void handleDiskRemoved(dev_t device);
153 std::mutex mCryptLock;
155 android::sp<android::os::IVoldListener> mListener;
157 std::list<std::shared_ptr<DiskSource>> mDiskSources;
158 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
159 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
160 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
162 std::unordered_map<userid_t, int> mAddedUsers;
163 std::unordered_set<userid_t> mStartedUsers;
165 std::string mVirtualDiskPath;
166 std::shared_ptr<android::vold::Disk> mVirtualDisk;
167 std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
168 std::shared_ptr<android::vold::VolumeBase> mPrimary;
171 bool mSecureKeyguardShowing;