OSDN Git Service

[automerger skipped] Merge "DO NOT MERGE - Mark RQ2A.210105.001 as merged." am:...
[android-x86/system-vold.git] / VolumeManager.h
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
17 #ifndef ANDROID_VOLD_VOLUME_MANAGER_H
18 #define ANDROID_VOLD_VOLUME_MANAGER_H
19
20 #include <fnmatch.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23
24 #include <list>
25 #include <mutex>
26 #include <set>
27 #include <string>
28 #include <unordered_map>
29 #include <unordered_set>
30
31 #include <android-base/unique_fd.h>
32 #include <cutils/multiuser.h>
33 #include <sysutils/NetlinkEvent.h>
34 #include <utils/List.h>
35 #include <utils/Timers.h>
36
37 #include "android/os/IVoldListener.h"
38
39 #include "model/Disk.h"
40 #include "model/VolumeBase.h"
41
42 class VolumeManager {
43   private:
44     static VolumeManager* sInstance;
45
46     bool mDebug;
47
48   public:
49     virtual ~VolumeManager();
50
51     // TODO: pipe all requests through VM to avoid exposing this lock
52     std::mutex& getLock() { return mLock; }
53     std::mutex& getCryptLock() { return mCryptLock; }
54
55     void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
56     android::sp<android::os::IVoldListener> getListener() const { return mListener; }
57
58     int start();
59
60     void handleBlockEvent(NetlinkEvent* evt);
61
62     class DiskSource {
63       public:
64         DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
65             : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
66
67         bool matches(const std::string& sysPath) {
68             return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
69         }
70
71         const std::string& getNickname() const { return mNickname; }
72         int getFlags() const { return mFlags; }
73
74       private:
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
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
103     void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
104
105     const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; }
106
107     int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
108
109     int onUserAdded(userid_t userId, int userSerialNumber);
110     int onUserRemoved(userid_t userId);
111     int onUserStarted(userid_t userId);
112     int onUserStopped(userid_t userId);
113
114     void createPendingDisksIfNeeded();
115     int onSecureKeyguardStateChanged(bool isShowing);
116
117     int remountUid(uid_t uid, int32_t remountMode) { return 0; }
118     int handleAppStorageDirs(int uid, int pid,
119             bool doUnmount, const std::vector<std::string>& packageNames);
120
121     /* Aborts all FUSE filesystems, in case the FUSE daemon is no longer up. */
122     int abortFuse();
123     /* Reset all internal state, typically during framework boot */
124     int reset();
125     /* Prepare for device shutdown, safely unmounting all devices */
126     int shutdown();
127     /* Unmount all volumes, usually for encryption */
128     int unmountAll();
129
130     int updateVirtualDisk();
131     int setDebug(bool enable);
132
133     bool forkAndRemountStorage(int uid, int pid, bool doUnmount,
134         const std::vector<std::string>& packageNames);
135
136     static VolumeManager* Instance();
137
138     /*
139      * Creates a directory 'path' for an application, automatically creating
140      * directories along the given path if they don't exist yet.
141      *
142      * Example:
143      *   path = /storage/emulated/0/Android/data/com.foo/files/
144      *
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
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      *
160      * The UID/GID of the parent directories will be set according to the
161      * requirements of the underlying filesystem and are of no concern to the
162      * caller.
163      *
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      *
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      *
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.
175      */
176     int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false,
177             bool skipIfDirExists = false);
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);
185
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
189     int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
190                   std::string* outVolId);
191     int destroyObb(const std::string& volId);
192
193     int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
194                          const std::string& fsType, const std::string& fsUuid,
195                          const std::string& fsLabel, int32_t flags, std::string* outVolId);
196     int destroyStubVolume(const std::string& volId);
197
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);
201
202   private:
203     VolumeManager();
204     void readInitialState();
205
206     int linkPrimary(userid_t userId);
207
208     void createEmulatedVolumesForUser(userid_t userId);
209     void destroyEmulatedVolumesForUser(userid_t userId);
210
211     void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
212     void handleDiskChanged(dev_t device);
213     void handleDiskRemoved(dev_t device);
214
215     bool updateFuseMountedProperty();
216
217     std::mutex mLock;
218     std::mutex mCryptLock;
219
220     android::sp<android::os::IVoldListener> mListener;
221
222     std::list<std::shared_ptr<DiskSource>> mDiskSources;
223     std::list<std::shared_ptr<android::vold::Disk>> mDisks;
224     std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
225     std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
226     std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes;
227
228     std::unordered_map<userid_t, int> mAddedUsers;
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;
232
233     std::string mVirtualDiskPath;
234     std::shared_ptr<android::vold::Disk> mVirtualDisk;
235     std::shared_ptr<android::vold::VolumeBase> mPrimary;
236
237     int mNextObbId;
238     int mNextStubId;
239     bool mSecureKeyguardShowing;
240 };
241
242 #endif