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(); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 59 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 60 | void handleBlockEvent(NetlinkEvent* evt); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | class DiskSource { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 63 | public: |
| 64 | DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) |
| 65 | : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 66 | |
| 67 | bool matches(const std::string& sysPath) { |
| 68 | return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); |
| 69 | } |
| 70 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 71 | const std::string& getNickname() const { return mNickname; } |
| 72 | int getFlags() const { return mFlags; } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 74 | private: |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 85 | template <typename 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 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 103 | 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] | 104 | |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 105 | const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 106 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 107 | int forgetPartition(const std::string& partGuid, const std::string& fsUuid); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 109 | int onUserAdded(userid_t userId, int userSerialNumber); |
| 110 | int onUserRemoved(userid_t userId); |
Sudheer Shanka | 4112c12 | 2019-04-29 10:46:35 -0700 | [diff] [blame] | 111 | int onUserStarted(userid_t userId); |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 112 | int onUserStopped(userid_t userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 113 | |
Martijn Coenen | cf5916f | 2020-01-03 14:36:45 +0100 | [diff] [blame] | 114 | void createPendingDisksIfNeeded(); |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 115 | int onSecureKeyguardStateChanged(bool isShowing); |
| 116 | |
Ricky Wai | e78c78c | 2021-01-14 15:51:54 +0000 | [diff] [blame] | 117 | int remountUid(uid_t uid, int32_t remountMode) { return 0; } |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 118 | int handleAppStorageDirs(int uid, int pid, |
| 119 | bool doUnmount, const std::vector<std::string>& packageNames); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 120 | |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 121 | /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */ |
| 122 | int abortFuse(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 123 | /* Reset all internal state, typically during framework boot */ |
| 124 | int reset(); |
| 125 | /* Prepare for device shutdown, safely unmounting all devices */ |
| 126 | int shutdown(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 127 | /* Unmount all volumes, usually for encryption */ |
| 128 | int unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 129 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 130 | int updateVirtualDisk(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 131 | int setDebug(bool enable); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 132 | |
Ricky Wai | a2ca11e | 2021-01-15 14:03:23 +0000 | [diff] [blame] | 133 | bool forkAndRemountStorage(int uid, int pid, bool doUnmount, |
| 134 | const std::vector<std::string>& packageNames); |
Linus Tufvesson | 75973cb | 2020-03-23 11:59:43 +0000 | [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 | * |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 168 | * If skipIfDirExists is set, we will not fix any existing dirs, we will |
| 169 | * only create app dirs if it doesn't exist. |
| 170 | * |
Martijn Coenen | 13ff668 | 2019-12-24 12:57:16 +0100 | [diff] [blame] | 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. |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 175 | */ |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 176 | int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false, |
| 177 | bool skipIfDirExists = false); |
Martijn Coenen | 816f4d9 | 2020-02-18 15:06:37 +0100 | [diff] [blame] | 178 | |
| 179 | /** |
| 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. |
| 183 | */ |
| 184 | int fixupAppDir(const std::string& path, int32_t appUid); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 185 | |
Ricky Wai | bbfb6ea | 2020-12-03 15:32:52 +0000 | [diff] [blame] | 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); |
| 188 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 189 | 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] | 190 | std::string* outVolId); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 191 | int destroyObb(const std::string& volId); |
| 192 | |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 193 | int createStubVolume(const std::string& sourcePath, const std::string& mountPath, |
| 194 | const std::string& fsType, const std::string& fsUuid, |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 195 | const std::string& fsLabel, int32_t flags, std::string* outVolId); |
Risan | 8c9f332 | 2018-10-29 08:52:56 +0900 | [diff] [blame] | 196 | int destroyStubVolume(const std::string& volId); |
| 197 | |
Risan | 8f6198d | 2018-10-26 20:56:45 -0600 | [diff] [blame] | 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); |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 201 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 202 | private: |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 203 | VolumeManager(); |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 204 | void readInitialState(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 205 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 206 | int linkPrimary(userid_t userId); |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 207 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 208 | void createEmulatedVolumesForUser(userid_t userId); |
| 209 | void destroyEmulatedVolumesForUser(userid_t userId); |
| 210 | |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 211 | void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk); |
| 212 | void handleDiskChanged(dev_t device); |
| 213 | void handleDiskRemoved(dev_t device); |
| 214 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 215 | bool updateFuseMountedProperty(); |
| 216 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 217 | std::mutex mLock; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 218 | std::mutex mCryptLock; |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 219 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 220 | android::sp<android::os::IVoldListener> mListener; |
| 221 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | std::list<std::shared_ptr<DiskSource>> mDiskSources; |
| 223 | std::list<std::shared_ptr<android::vold::Disk>> mDisks; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 224 | std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 225 | std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 226 | std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 228 | std::unordered_map<userid_t, int> mAddedUsers; |
Martijn Coenen | 745e0a9 | 2019-12-03 16:11:39 +0100 | [diff] [blame] | 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; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 232 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 233 | std::string mVirtualDiskPath; |
| 234 | std::shared_ptr<android::vold::Disk> mVirtualDisk; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 235 | std::shared_ptr<android::vold::VolumeBase> mPrimary; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 236 | |
| 237 | int mNextObbId; |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 238 | int mNextStubId; |
Jeff Sharkey | 401b260 | 2017-12-14 22:15:20 -0700 | [diff] [blame] | 239 | bool mSecureKeyguardShowing; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 240 | }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 241 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 242 | #endif |