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_MANAGER_H 18 #define ANDROID_VOLD_VOLUME_MANAGER_H 19 20 #include <fnmatch.h> 21 #include <pthread.h> 22 #include <stdlib.h> 23 24 #include <list> 25 #include <mutex> 26 #include <set> 27 #include <string> 28 #include <unordered_map> 29 #include <unordered_set> 30 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> 36 37 #include "android/os/IVoldListener.h" 38 39 #include "model/Disk.h" 40 #include "model/VolumeBase.h" 41 42 class VolumeManager { 43 private: 44 static VolumeManager* sInstance; 45 46 bool mDebug; 47 48 public: 49 virtual ~VolumeManager(); 50 51 // TODO: pipe all requests through VM to avoid exposing this lock getLock()52 std::mutex& getLock() { return mLock; } getCryptLock()53 std::mutex& getCryptLock() { return mCryptLock; } 54 setListener(android::sp<android::os::IVoldListener> listener)55 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; } getListener()56 android::sp<android::os::IVoldListener> getListener() const { return mListener; } 57 58 int start(); 59 60 void handleBlockEvent(NetlinkEvent* evt); 61 62 class DiskSource { 63 public: DiskSource(const std::string & sysPattern,const std::string & nickname,int flags)64 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) 65 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} 66 matches(const std::string & sysPath)67 bool matches(const std::string& sysPath) { 68 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); 69 } 70 getNickname()71 const std::string& getNickname() const { return mNickname; } getFlags()72 int getFlags() const { return mFlags; } 73 74 private: 75 std::string mSysPattern; 76 std::string mNickname; 77 int mFlags; 78 }; 79 80 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource); 81 82 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id); 83 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id); 84 85 template <typename Fn> findVolumeWithFilter(Fn fn)86 std::shared_ptr<android::vold::VolumeBase> findVolumeWithFilter(Fn fn) { 87 for (const auto& vol : mInternalEmulatedVolumes) { 88 if (fn(*vol)) { 89 return vol; 90 } 91 } 92 for (const auto& disk : mDisks) { 93 for (const auto& vol : disk->getVolumes()) { 94 if (fn(*vol)) { 95 return vol; 96 } 97 } 98 } 99 100 return nullptr; 101 } 102 103 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const; 104 getStartedUsers()105 const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; } 106 107 userid_t getSharedStorageUser(userid_t userId); 108 109 bool forgetPartition(const std::string& partGuid, const std::string& fsUuid); 110 111 int onUserAdded(userid_t userId, int userSerialNumber, userid_t cloneParentUserId); 112 int onUserRemoved(userid_t userId); 113 int onUserStarted(userid_t userId); 114 int onUserStopped(userid_t userId); 115 116 void createPendingDisksIfNeeded(); 117 int onSecureKeyguardStateChanged(bool isShowing); 118 remountUid(uid_t uid,int32_t remountMode)119 int remountUid(uid_t uid, int32_t remountMode) { return 0; } 120 int handleAppStorageDirs(int uid, int pid, 121 bool doUnmount, const std::vector<std::string>& packageNames); 122 123 /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */ 124 int abortFuse(); 125 /* Reset all internal state, typically during framework boot */ 126 int reset(); 127 /* Prepare for device shutdown, safely unmounting all devices */ 128 int shutdown(); 129 /* Unmount all volumes, usually for encryption */ 130 int unmountAll(); 131 132 int updateVirtualDisk(); 133 int setDebug(bool enable); 134 135 bool forkAndRemountStorage(int uid, int pid, bool doUnmount, 136 const std::vector<std::string>& packageNames); 137 138 static VolumeManager* Instance(); 139 140 /* 141 * Creates a directory 'path' for an application, automatically creating 142 * directories along the given path if they don't exist yet. 143 * 144 * Example: 145 * path = /storage/emulated/0/Android/data/com.foo/files/ 146 * 147 * This function will first match the first part of the path with the volume 148 * root of any known volumes; in this case, "/storage/emulated/0" matches 149 * with the volume root of the emulated volume for user 0. 150 * 151 * The subseqent part of the path must start with one of the well-known 152 * Android/ data directories, /Android/data, /Android/obb or 153 * /Android/media. 154 * 155 * The final part of the path is application specific. This function will 156 * create all directories, including the application-specific ones, and 157 * set the UID of all app-specific directories below the well-known data 158 * directories to the 'appUid' argument. In the given example, the UID 159 * of /storage/emulated/0/Android/data/com.foo and 160 * /storage/emulated/0/Android/data/com.foo/files would be set to 'appUid'. 161 * 162 * The UID/GID of the parent directories will be set according to the 163 * requirements of the underlying filesystem and are of no concern to the 164 * caller. 165 * 166 * If fixupExistingOnly is set, we make sure to fixup any existing dirs and 167 * files in the passed in path, but only if that path exists; if it doesn't 168 * exist, this function doesn't create them. 169 * 170 * If skipIfDirExists is set, we will not fix any existing dirs, we will 171 * only create app dirs if it doesn't exist. 172 * 173 * Validates that given paths are absolute and that they contain no relative 174 * "." or ".." paths or symlinks. Last path segment is treated as filename 175 * and ignored, unless the path ends with "/". Also ensures that path 176 * belongs to a volume managed by vold. 177 */ 178 int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false, 179 bool skipIfDirExists = false); 180 181 /** 182 * Fixes up an existing application directory, as if it was created with 183 * setupAppDir() above. This includes fixing up the UID/GID, permissions and 184 * project IDs of the contained files and directories. 185 */ 186 int fixupAppDir(const std::string& path, int32_t appUid); 187 188 // Called before zygote starts to ensure dir exists so zygote can bind mount them. 189 int ensureAppDirsCreated(const std::vector<std::string>& paths, int32_t appUid); 190 191 int createObb(const std::string& path, int32_t ownerGid, std::string* outVolId); 192 int destroyObb(const std::string& volId); 193 194 int createStubVolume(const std::string& sourcePath, const std::string& mountPath, 195 const std::string& fsType, const std::string& fsUuid, 196 const std::string& fsLabel, int32_t flags, std::string* outVolId); 197 int destroyStubVolume(const std::string& volId); 198 199 int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd); 200 int unmountAppFuse(uid_t uid, int mountId); 201 int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags); 202 203 private: 204 VolumeManager(); 205 void readInitialState(); 206 207 int linkPrimary(userid_t userId); 208 209 void createEmulatedVolumesForUser(userid_t userId); 210 void destroyEmulatedVolumesForUser(userid_t userId); 211 212 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk); 213 void handleDiskChanged(dev_t device); 214 void handleDiskRemoved(dev_t device); 215 216 bool updateFuseMountedProperty(); 217 218 std::mutex mLock; 219 std::mutex mCryptLock; 220 221 android::sp<android::os::IVoldListener> mListener; 222 223 std::list<std::shared_ptr<DiskSource>> mDiskSources; 224 std::list<std::shared_ptr<android::vold::Disk>> mDisks; 225 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks; 226 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; 227 std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes; 228 229 std::unordered_map<userid_t, int> mAddedUsers; 230 // Map of users to a user with which they can share storage (eg clone profiles) 231 std::unordered_map<userid_t, userid_t> mSharedStorageUser; 232 // This needs to be a regular set because we care about the ordering here; 233 // user 0 should always go first, because it is responsible for sdcardfs. 234 std::set<userid_t> mStartedUsers; 235 236 std::string mVirtualDiskPath; 237 std::shared_ptr<android::vold::Disk> mVirtualDisk; 238 std::shared_ptr<android::vold::VolumeBase> mPrimary; 239 240 int mNextObbId; 241 int mNextStubId; 242 bool mSecureKeyguardShowing; 243 }; 244 245 namespace android { 246 namespace vold { 247 android::status_t GetStorageSize(int64_t* storageSize); 248 } 249 } // namespace android 250 251 #endif 252