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 | |
| 20 | #include <pthread.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include <fnmatch.h> |
| 22 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 24 | #ifdef __cplusplus |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 25 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 26 | #include <list> |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 27 | #include <mutex> |
| 28 | #include <string> |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | #include <unordered_set> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 33 | #include <cutils/multiuser.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> |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 36 | #include <sysutils/NetlinkEvent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 38 | #include "android/os/IVoldListener.h" |
| 39 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 40 | #include "model/Disk.h" |
| 41 | #include "model/VolumeBase.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 43 | #define DEBUG_APPFUSE 0 |
| 44 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 45 | class VolumeManager { |
| 46 | private: |
| 47 | static VolumeManager *sInstance; |
| 48 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 49 | bool mDebug; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | virtual ~VolumeManager(); |
| 53 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 54 | // TODO: pipe all requests through VM to avoid exposing this lock |
| 55 | std::mutex& getLock() { return mLock; } |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 56 | std::mutex& getCryptLock() { return mCryptLock; } |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 58 | void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; } |
| 59 | android::sp<android::os::IVoldListener> getListener() { return mListener; } |
| 60 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 61 | int start(); |
| 62 | int stop(); |
| 63 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 64 | void handleBlockEvent(NetlinkEvent *evt); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 65 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 66 | class DiskSource { |
| 67 | public: |
| 68 | DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) : |
| 69 | mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) { |
| 70 | } |
| 71 | |
| 72 | bool matches(const std::string& sysPath) { |
| 73 | return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); |
| 74 | } |
| 75 | |
| 76 | const std::string& getNickname() { return mNickname; } |
| 77 | int getFlags() { return mFlags; } |
| 78 | |
| 79 | private: |
| 80 | std::string mSysPattern; |
| 81 | std::string mNickname; |
| 82 | int mFlags; |
| 83 | }; |
| 84 | |
| 85 | void addDiskSource(const std::shared_ptr<DiskSource>& diskSource); |
| 86 | |
| 87 | std::shared_ptr<android::vold::Disk> findDisk(const std::string& id); |
| 88 | std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id); |
| 89 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 90 | void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list); |
| 91 | |
Jeff Sharkey | 3ce1825 | 2017-10-24 11:08:45 -0600 | [diff] [blame] | 92 | int forgetPartition(const std::string& partGuid, const std::string& fsUuid); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 93 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 94 | int onUserAdded(userid_t userId, int userSerialNumber); |
| 95 | int onUserRemoved(userid_t userId); |
| 96 | int onUserStarted(userid_t userId); |
| 97 | int onUserStopped(userid_t userId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 98 | |
| 99 | int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol); |
| 100 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 101 | int remountUid(uid_t uid, const std::string& mode); |
| 102 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 103 | /* Reset all internal state, typically during framework boot */ |
| 104 | int reset(); |
| 105 | /* Prepare for device shutdown, safely unmounting all devices */ |
| 106 | int shutdown(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 107 | /* Unmount all volumes, usually for encryption */ |
| 108 | int unmountAll(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 109 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 110 | int updateVirtualDisk(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 111 | int setDebug(bool enable); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 112 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 113 | static VolumeManager *Instance(); |
| 114 | |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 115 | /* |
| 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 Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 122 | int mkdirs(const std::string& path); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 123 | |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 124 | int createObb(const std::string& path, const std::string& key, int32_t ownerGid, |
| 125 | std::string* outVolId); |
| 126 | int destroyObb(const std::string& volId); |
| 127 | |
| 128 | int mountAppFuse(uid_t uid, pid_t pid, int mountId, android::base::unique_fd* device_fd); |
| 129 | int unmountAppFuse(uid_t uid, pid_t pid, int mountId); |
| 130 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 131 | private: |
| 132 | VolumeManager(); |
Mike Lockwood | 99635f6 | 2010-06-25 23:04:04 -0400 | [diff] [blame] | 133 | void readInitialState(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 134 | |
| 135 | int linkPrimary(userid_t userId); |
| 136 | |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 137 | std::mutex mLock; |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 138 | std::mutex mCryptLock; |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 140 | android::sp<android::os::IVoldListener> mListener; |
| 141 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 142 | std::list<std::shared_ptr<DiskSource>> mDiskSources; |
| 143 | std::list<std::shared_ptr<android::vold::Disk>> mDisks; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 144 | std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 145 | |
Jeff Sharkey | bd3038d | 2015-06-10 09:42:01 -0700 | [diff] [blame] | 146 | std::unordered_map<userid_t, int> mAddedUsers; |
| 147 | std::unordered_set<userid_t> mStartedUsers; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 148 | |
Jeff Sharkey | fa1c677 | 2017-03-25 22:49:13 -0600 | [diff] [blame] | 149 | std::string mVirtualDiskPath; |
| 150 | std::shared_ptr<android::vold::Disk> mVirtualDisk; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 151 | std::shared_ptr<android::vold::VolumeBase> mInternalEmulated; |
| 152 | std::shared_ptr<android::vold::VolumeBase> mPrimary; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 153 | |
| 154 | int mNextObbId; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 155 | }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 156 | |
| 157 | extern "C" { |
| 158 | #endif /* __cplusplus */ |
Chih-Hung Hsieh | aae7938 | 2016-06-10 14:13:59 -0700 | [diff] [blame] | 159 | #define UNMOUNT_NOT_MOUNTED_ERR (-2) |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 160 | int vold_unmountAll(void); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 161 | #ifdef __cplusplus |
| 162 | } |
| 163 | #endif |
| 164 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 165 | #endif |