blob: fad3c0004a21fae34f20ab450605c30f48783ac4 [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();
59 int stop();
60
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070061 void handleBlockEvent(NetlinkEvent* evt);
San Mehatf1b736b2009-10-10 17:22:08 -070062
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063 class DiskSource {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070064 public:
65 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
66 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
Jeff Sharkey36801cc2015-03-13 16:09:20 -070067
68 bool matches(const std::string& sysPath) {
69 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
70 }
71
Greg Kaiser2bc201e2018-12-18 08:42:08 -080072 const std::string& getNickname() const { return mNickname; }
73 int getFlags() const { return mFlags; }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070075 private:
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 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
Greg Kaiser2bc201e2018-12-18 08:42:08 -080086 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070087
Martijn Coenen745e0a92019-12-03 16:11:39 +010088 const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; }
Zima438b242019-09-25 14:37:38 +010089
Jeff Sharkey3ce18252017-10-24 11:08:45 -060090 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070091
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070092 int onUserAdded(userid_t userId, int userSerialNumber);
93 int onUserRemoved(userid_t userId);
Sudheer Shanka4112c122019-04-29 10:46:35 -070094 int onUserStarted(userid_t userId);
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070095 int onUserStopped(userid_t userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Jeff Sharkey401b2602017-12-14 22:15:20 -070097 int onSecureKeyguardStateChanged(bool isShowing);
98
Jeff Sharkey36801cc2015-03-13 16:09:20 -070099 int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
100
Sudheer Shanka817b9112018-12-13 17:40:28 -0800101 int remountUid(uid_t uid, int32_t remountMode);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700102
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103 /* Reset all internal state, typically during framework boot */
104 int reset();
105 /* Prepare for device shutdown, safely unmounting all devices */
106 int shutdown();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700107 /* Unmount all volumes, usually for encryption */
108 int unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700109
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600110 int updateVirtualDisk();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700111 int setDebug(bool enable);
San Mehatd9a4e352010-03-12 13:32:47 -0800112
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700113 static VolumeManager* Instance();
San Mehatf1b736b2009-10-10 17:22:08 -0700114
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700115 /*
116 * Ensure that all directories along given path exist, creating parent
117 * directories as needed. Validates that given path is absolute and that
118 * it contains no relative "." or ".." paths or symlinks. Last path segment
119 * is treated as filename and ignored, unless the path ends with "/". Also
120 * ensures that path belongs to a volume managed by vold.
121 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600122 int mkdirs(const std::string& path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700123
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600124 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700125 std::string* outVolId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600126 int destroyObb(const std::string& volId);
127
Risan8c9f3322018-10-29 08:52:56 +0900128 int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
129 const std::string& fsType, const std::string& fsUuid,
130 const std::string& fsLabel, std::string* outVolId);
131 int destroyStubVolume(const std::string& volId);
132
Risan8f6198d2018-10-26 20:56:45 -0600133 int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
134 int unmountAppFuse(uid_t uid, int mountId);
135 int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600136
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700137 private:
San Mehatf1b736b2009-10-10 17:22:08 -0700138 VolumeManager();
Mike Lockwood99635f62010-06-25 23:04:04 -0400139 void readInitialState();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700140
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700141 int linkPrimary(userid_t userId);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700142
Zima438b242019-09-25 14:37:38 +0100143 void createEmulatedVolumesForUser(userid_t userId);
144 void destroyEmulatedVolumesForUser(userid_t userId);
145
Jeff Sharkey401b2602017-12-14 22:15:20 -0700146 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
147 void handleDiskChanged(dev_t device);
148 void handleDiskRemoved(dev_t device);
149
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700150 std::mutex mLock;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600151 std::mutex mCryptLock;
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700152
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600153 android::sp<android::os::IVoldListener> mListener;
154
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 std::list<std::shared_ptr<DiskSource>> mDiskSources;
156 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700157 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600158 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
Risan8c9f3322018-10-29 08:52:56 +0900159 std::list<std::shared_ptr<android::vold::VolumeBase>> mStubVolumes;
Zima438b242019-09-25 14:37:38 +0100160 std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700161
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700162 std::unordered_map<userid_t, int> mAddedUsers;
Martijn Coenen745e0a92019-12-03 16:11:39 +0100163 // This needs to be a regular set because we care about the ordering here;
164 // user 0 should always go first, because it is responsible for sdcardfs.
165 std::set<userid_t> mStartedUsers;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600167 std::string mVirtualDiskPath;
168 std::shared_ptr<android::vold::Disk> mVirtualDisk;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 std::shared_ptr<android::vold::VolumeBase> mPrimary;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600170
171 int mNextObbId;
Risan8c9f3322018-10-29 08:52:56 +0900172 int mNextStubVolumeId;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700173 bool mSecureKeyguardShowing;
San Mehatf1b736b2009-10-10 17:22:08 -0700174};
Ken Sumrall29d8da82011-05-18 17:20:07 -0700175
San Mehatf1b736b2009-10-10 17:22:08 -0700176#endif