blob: 3573b1a9065963991e42570740ce0be7a7e2b0aa [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Sharkey36801cc2015-03-13 16:09:20 -070017#ifndef ANDROID_VOLD_VOLUME_MANAGER_H
18#define ANDROID_VOLD_VOLUME_MANAGER_H
San Mehatf1b736b2009-10-10 17:22:08 -070019
Jeff Sharkey36801cc2015-03-13 16:09:20 -070020#include <fnmatch.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070021#include <pthread.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include <list>
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070025#include <mutex>
Martijn Coenen745e0a92019-12-03 16:11:39 +010026#include <set>
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070027#include <string>
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070028#include <unordered_map>
29#include <unordered_set>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070030
Jeff Sharkey11c2d382017-09-11 10:32:01 -060031#include <android-base/unique_fd.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070032#include <cutils/multiuser.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070033#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070034#include <utils/List.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070035#include <utils/Timers.h>
San Mehatf1b736b2009-10-10 17:22:08 -070036
Jeff Sharkey814e9d32017-09-13 11:49:44 -060037#include "android/os/IVoldListener.h"
38
Jeff Sharkey11c2d382017-09-11 10:32:01 -060039#include "model/Disk.h"
40#include "model/VolumeBase.h"
San Mehatf1b736b2009-10-10 17:22:08 -070041
42class VolumeManager {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070043 private:
44 static VolumeManager* sInstance;
San Mehatf1b736b2009-10-10 17:22:08 -070045
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070046 bool mDebug;
San Mehatf1b736b2009-10-10 17:22:08 -070047
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070048 public:
San Mehatf1b736b2009-10-10 17:22:08 -070049 virtual ~VolumeManager();
50
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070051 // TODO: pipe all requests through VM to avoid exposing this lock
52 std::mutex& getLock() { return mLock; }
Jeff Sharkey83b559c2017-09-12 16:30:52 -060053 std::mutex& getCryptLock() { return mCryptLock; }
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070054
Jeff Sharkey814e9d32017-09-13 11:49:44 -060055 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
Greg Kaiser2bc201e2018-12-18 08:42:08 -080056 android::sp<android::os::IVoldListener> getListener() const { return mListener; }
Jeff Sharkey814e9d32017-09-13 11:49:44 -060057
San Mehatf1b736b2009-10-10 17:22:08 -070058 int start();
San Mehatf1b736b2009-10-10 17:22:08 -070059
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070060 void handleBlockEvent(NetlinkEvent* evt);
San Mehatf1b736b2009-10-10 17:22:08 -070061
Jeff Sharkey36801cc2015-03-13 16:09:20 -070062 class DiskSource {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070063 public:
64 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
65 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
Jeff Sharkey36801cc2015-03-13 16:09:20 -070066
67 bool matches(const std::string& sysPath) {
68 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
69 }
70
Greg Kaiser2bc201e2018-12-18 08:42:08 -080071 const std::string& getNickname() const { return mNickname; }
72 int getFlags() const { return mFlags; }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070073
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070074 private:
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 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 Coenen0a7e9922020-01-24 16:17:32 +010085 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 Kaiser2bc201e2018-12-18 08:42:08 -0800103 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700104
Martijn Coenen745e0a92019-12-03 16:11:39 +0100105 const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; }
Zima438b242019-09-25 14:37:38 +0100106
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600107 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700108
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700109 int onUserAdded(userid_t userId, int userSerialNumber);
110 int onUserRemoved(userid_t userId);
Sudheer Shanka4112c122019-04-29 10:46:35 -0700111 int onUserStarted(userid_t userId);
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700112 int onUserStopped(userid_t userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113
Martijn Coenencf5916f2020-01-03 14:36:45 +0100114 void createPendingDisksIfNeeded();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700115 int onSecureKeyguardStateChanged(bool isShowing);
116
Ricky Waie78c78c2021-01-14 15:51:54 +0000117 int remountUid(uid_t uid, int32_t remountMode) { return 0; }
Ricky Waia2ca11e2021-01-15 14:03:23 +0000118 int handleAppStorageDirs(int uid, int pid,
119 bool doUnmount, const std::vector<std::string>& packageNames);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700120
Martijn Coenen23c04452020-04-29 07:49:41 +0200121 /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */
122 int abortFuse();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700123 /* Reset all internal state, typically during framework boot */
124 int reset();
125 /* Prepare for device shutdown, safely unmounting all devices */
126 int shutdown();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700127 /* Unmount all volumes, usually for encryption */
128 int unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700129
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600130 int updateVirtualDisk();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700131 int setDebug(bool enable);
San Mehatd9a4e352010-03-12 13:32:47 -0800132
Ricky Waia2ca11e2021-01-15 14:03:23 +0000133 bool forkAndRemountStorage(int uid, int pid, bool doUnmount,
134 const std::vector<std::string>& packageNames);
Linus Tufvesson75973cb2020-03-23 11:59:43 +0000135
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700136 static VolumeManager* Instance();
San Mehatf1b736b2009-10-10 17:22:08 -0700137
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700138 /*
Martijn Coenen13ff6682019-12-24 12:57:16 +0100139 * Creates a directory 'path' for an application, automatically creating
Martijn Coenen8a68a072020-02-12 15:29:02 +0100140 * directories along the given path if they don't exist yet.
Martijn Coenen13ff6682019-12-24 12:57:16 +0100141 *
142 * Example:
143 * path = /storage/emulated/0/Android/data/com.foo/files/
Martijn Coenen13ff6682019-12-24 12:57:16 +0100144 *
Martijn Coenen8a68a072020-02-12 15:29:02 +0100145 * 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 Coenen13ff6682019-12-24 12:57:16 +0100157 * 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 Coenen8a68a072020-02-12 15:29:02 +0100160 * The UID/GID of the parent directories will be set according to the
Martijn Coenen13ff6682019-12-24 12:57:16 +0100161 * requirements of the underlying filesystem and are of no concern to the
162 * caller.
163 *
Martijn Coenen816f4d92020-02-18 15:06:37 +0100164 * 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 Waibbfb6ea2020-12-03 15:32:52 +0000168 * 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 Coenen13ff6682019-12-24 12:57:16 +0100171 * 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 Sharkey71ebe152013-09-17 17:24:38 -0700175 */
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000176 int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false,
177 bool skipIfDirExists = false);
Martijn Coenen816f4d92020-02-18 15:06:37 +0100178
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 Sharkey71ebe152013-09-17 17:24:38 -0700185
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000186 // 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 Sharkey11c2d382017-09-11 10:32:01 -0600189 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700190 std::string* outVolId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600191 int destroyObb(const std::string& volId);
192
Risan8c9f3322018-10-29 08:52:56 +0900193 int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
194 const std::string& fsType, const std::string& fsUuid,
Risan82e90de2020-02-04 16:07:21 +0900195 const std::string& fsLabel, int32_t flags, std::string* outVolId);
Risan8c9f3322018-10-29 08:52:56 +0900196 int destroyStubVolume(const std::string& volId);
197
Risan8f6198d2018-10-26 20:56:45 -0600198 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 Sharkey11c2d382017-09-11 10:32:01 -0600201
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700202 private:
San Mehatf1b736b2009-10-10 17:22:08 -0700203 VolumeManager();
Mike Lockwood99635f62010-06-25 23:04:04 -0400204 void readInitialState();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700205
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700206 int linkPrimary(userid_t userId);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700207
Zima438b242019-09-25 14:37:38 +0100208 void createEmulatedVolumesForUser(userid_t userId);
209 void destroyEmulatedVolumesForUser(userid_t userId);
210
Jeff Sharkey401b2602017-12-14 22:15:20 -0700211 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
212 void handleDiskChanged(dev_t device);
213 void handleDiskRemoved(dev_t device);
214
Ricky Wai07e64a42020-02-11 14:31:24 +0000215 bool updateFuseMountedProperty();
216
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700217 std::mutex mLock;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600218 std::mutex mCryptLock;
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700219
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600220 android::sp<android::os::IVoldListener> mListener;
221
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700222 std::list<std::shared_ptr<DiskSource>> mDiskSources;
223 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700224 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600225 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
Zima438b242019-09-25 14:37:38 +0100226 std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700228 std::unordered_map<userid_t, int> mAddedUsers;
Martijn Coenen745e0a92019-12-03 16:11:39 +0100229 // 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 Sharkey36801cc2015-03-13 16:09:20 -0700232
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600233 std::string mVirtualDiskPath;
234 std::shared_ptr<android::vold::Disk> mVirtualDisk;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 std::shared_ptr<android::vold::VolumeBase> mPrimary;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600236
237 int mNextObbId;
Risan82e90de2020-02-04 16:07:21 +0900238 int mNextStubId;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700239 bool mSecureKeyguardShowing;
San Mehatf1b736b2009-10-10 17:22:08 -0700240};
Ken Sumrall29d8da82011-05-18 17:20:07 -0700241
San Mehatf1b736b2009-10-10 17:22:08 -0700242#endif