San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_VOLD_VOLUME_MANAGER_H |
| 18 | #define ANDROID_VOLD_VOLUME_MANAGER_H |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 19 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 24 | #include <list> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 25 | #include <mutex> |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 26 | #include <set> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 27 | #include <string> |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | #include <unordered_set> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 30 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 32 | #include <cutils/multiuser.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 33 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 34 | #include <utils/List.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 35 | #include <utils/Timers.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 37 | #include "android/os/IVoldListener.h" |
| 38 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 39 | #include "model/Disk.h" |
| 40 | #include "model/VolumeBase.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 41 | |
| 42 | class VolumeManager { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 43 | private: |
| 44 | static VolumeManager* sInstance; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 46 | bool mDebug; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 48 | public: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 49 | virtual ~VolumeManager(); |
| 50 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 51 | // TODO: pipe all requests through VM to avoid exposing this lock |
| 52 | std::mutex& getLock() { return mLock; } |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 53 | std::mutex& getCryptLock() { return mCryptLock; } |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 54 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 55 | void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; } |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 56 | android::sp<android::os::IVoldListener> getListener() const { return mListener; } |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 57 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 58 | int start(); |
| 59 | int stop(); |
| 60 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 61 | void handleBlockEvent(NetlinkEvent* evt); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 63 | class DiskSource { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 64 | public: |
| 65 | DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) |
| 66 | : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 67 | |
| 68 | bool matches(const std::string& sysPath) { |
| 69 | return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); |
| 70 | } |
| 71 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 72 | const std::string& getNickname() const { return mNickname; } |
| 73 | int getFlags() const { return mFlags; } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 74 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 75 | private: |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | std::string mSysPattern; |
| 77 | std::string mNickname; |
| 78 | int mFlags; |
| 79 | }; |
| 80 | |
| 81 | void addDiskSource(const std::shared_ptr<DiskSource>& diskSource); |
| 82 | |
| 83 | std::shared_ptr<android::vold::Disk> findDisk(const std::string& id); |
| 84 | std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id); |
| 85 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 86 | template <typename Fn> |
| 87 | std::shared_ptr<android::vold::VolumeBase> findVolumeWithFilter(Fn fn) { |
| 88 | for (const auto& vol : mInternalEmulatedVolumes) { |
| 89 | if (fn(*vol)) { |
| 90 | return vol; |
| 91 | } |
| 92 | } |
| 93 | for (const auto& disk : mDisks) { |
| 94 | for (const auto& vol : disk->getVolumes()) { |
| 95 | if (fn(*vol)) { |
| 96 | return vol; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 104 | void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 105 | |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 106 | const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 107 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 108 | int forgetPartition(const std::string& partGuid, const std::string& fsUuid); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 109 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 110 | int onUserAdded(userid_t userId, int userSerialNumber); |
| 111 | int onUserRemoved(userid_t userId); |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 112 | int onUserStarted(userid_t userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 113 | int onUserStopped(userid_t userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 114 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 115 | void createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 116 | int onSecureKeyguardStateChanged(bool isShowing); |
| 117 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 118 | int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol); |
| 119 | |
Sudheer Shanka | 817b911 | 2018-12-13 17:40:28 -0800 | [diff] [blame] | 120 | int remountUid(uid_t uid, int32_t remountMode); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 121 | int remountAppObb(userid_t userId); |
| 122 | |
| 123 | bool addFuseMountedUser(userid_t userId); |
| 124 | bool removeFuseMountedUser(userid_t userId); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 125 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 126 | /* Reset all internal state, typically during framework boot */ |
| 127 | int reset(); |
| 128 | /* Prepare for device shutdown, safely unmounting all devices */ |
| 129 | int shutdown(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 130 | /* Unmount all volumes, usually for encryption */ |
| 131 | int unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 132 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 133 | int updateVirtualDisk(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 134 | int setDebug(bool enable); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 135 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 136 | static VolumeManager* Instance(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 137 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 138 | /* |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 139 | * Creates a directory 'path' for an application, automatically creating |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 140 | * directories along the given path if they don't exist yet. |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 141 | * |
| 142 | * Example: |
| 143 | * path = /storage/emulated/0/Android/data/com.foo/files/ |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 144 | * |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 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. |
| 148 | * |
| 149 | * The subseqent part of the path must start with one of the well-known |
| 150 | * Android/ data directories, /Android/data, /Android/obb or |
| 151 | * /Android/media. |
| 152 | * |
| 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 |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 157 | * of /storage/emulated/0/Android/data/com.foo and |
| 158 | * /storage/emulated/0/Android/data/com.foo/files would be set to 'appUid'. |
| 159 | * |
Martijn Coenen | 8a68a07 | 2020-02-12 15:29:02 +0100 | [diff] [blame] | 160 | * The UID/GID of the parent directories will be set according to the |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 161 | * requirements of the underlying filesystem and are of no concern to the |
| 162 | * caller. |
| 163 | * |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 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. |
| 167 | * |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 168 | * Validates that given paths are absolute and that they contain no relative |
| 169 | * "." or ".." paths or symlinks. Last path segment is treated as filename |
| 170 | * and ignored, unless the path ends with "/". Also ensures that path |
| 171 | * belongs to a volume managed by vold. |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 172 | */ |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 173 | int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false); |
| 174 | |
| 175 | /** |
| 176 | * Fixes up an existing application directory, as if it was created with |
| 177 | * setupAppDir() above. This includes fixing up the UID/GID, permissions and |
| 178 | * project IDs of the contained files and directories. |
| 179 | */ |
| 180 | int fixupAppDir(const std::string& path, int32_t appUid); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 181 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 182 | int createObb(const std::string& path, const std::string& key, int32_t ownerGid, |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 183 | std::string* outVolId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 184 | int destroyObb(const std::string& volId); |
| 185 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 186 | int createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 187 | const std::string& fsType, const std::string& fsUuid, |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 188 | const std::string& fsLabel, int32_t flags, std::string* outVolId); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 189 | int destroyStubVolume(const std::string& volId); |
| 190 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 191 | int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd); |
| 192 | int unmountAppFuse(uid_t uid, int mountId); |
| 193 | int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 194 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 195 | private: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 196 | VolumeManager(); |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 197 | void readInitialState(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 199 | int linkPrimary(userid_t userId); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 200 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 201 | void createEmulatedVolumesForUser(userid_t userId); |
| 202 | void destroyEmulatedVolumesForUser(userid_t userId); |
| 203 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 204 | void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk); |
| 205 | void handleDiskChanged(dev_t device); |
| 206 | void handleDiskRemoved(dev_t device); |
| 207 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 208 | bool updateFuseMountedProperty(); |
| 209 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 210 | std::mutex mLock; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 211 | std::mutex mCryptLock; |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 212 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 213 | android::sp<android::os::IVoldListener> mListener; |
| 214 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 215 | std::list<std::shared_ptr<DiskSource>> mDiskSources; |
| 216 | std::list<std::shared_ptr<android::vold::Disk>> mDisks; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 217 | std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 218 | std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 219 | std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 220 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 221 | std::unordered_map<userid_t, int> mAddedUsers; |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 222 | // This needs to be a regular set because we care about the ordering here; |
| 223 | // user 0 should always go first, because it is responsible for sdcardfs. |
| 224 | std::set<userid_t> mStartedUsers; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 226 | std::string mVirtualDiskPath; |
| 227 | std::shared_ptr<android::vold::Disk> mVirtualDisk; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 228 | std::shared_ptr<android::vold::VolumeBase> mPrimary; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 229 | |
| 230 | int mNextObbId; |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 231 | int mNextStubId; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 232 | bool mSecureKeyguardShowing; |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 233 | |
| 234 | // Set of all user id that fuse is ready to use. |
| 235 | std::unordered_set<userid_t> mFuseMountedUsers; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 236 | }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 237 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 238 | #endif |