OSDN Git Service

vold: Accept Linux GPT partitions on external SD cards
[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 <pthread.h>
21 #include <fnmatch.h>
22 #include <stdlib.h>
23
24 #include <list>
25 #include <mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <unordered_set>
29
30 #include <android-base/unique_fd.h>
31 #include <cutils/multiuser.h>
32 #include <utils/List.h>
33 #include <utils/Timers.h>
34 #include <sysutils/NetlinkEvent.h>
35
36 #include "android/os/IVoldListener.h"
37
38 #include "model/Disk.h"
39 #include "model/DiskPartition.h"
40 #include "model/VolumeBase.h"
41
42 #define DEBUG_APPFUSE 0
43
44 class VolumeManager {
45 private:
46     static VolumeManager *sInstance;
47
48     bool                   mDebug;
49
50 public:
51     virtual ~VolumeManager();
52
53     // TODO: pipe all requests through VM to avoid exposing this lock
54     std::mutex& getLock() { return mLock; }
55     std::mutex& getCryptLock() { return mCryptLock; }
56
57     void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
58     android::sp<android::os::IVoldListener> getListener() { return mListener; }
59
60     int start();
61     int stop();
62
63     void handleBlockEvent(NetlinkEvent *evt);
64
65     class DiskSource {
66     public:
67         DiskSource(const std::string& sysPattern, const std::string& nickname,
68                         int partnum, int flags,
69                         const std::string& fstype, const std::string mntopts) :
70                 mSysPattern(sysPattern), mNickname(nickname),
71                 mPartNum(partnum), mFlags(flags),
72                 mFsType(fstype), mMntOpts(mntopts) {
73         }
74
75         bool matches(const std::string& sysPath) {
76             return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
77         }
78
79         const std::string& getNickname() { return mNickname; }
80         int getPartNum() { return mPartNum; }
81         int getFlags() { return mFlags; }
82         const std::string& getFsType() { return mFsType; }
83         const std::string& getMntOpts() { return mMntOpts; }
84
85     private:
86         std::string mSysPattern;
87         std::string mNickname;
88         int mPartNum;
89         int mFlags;
90         std::string mFsType;
91         std::string mMntOpts;
92     };
93
94     void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
95
96     std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
97     std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
98
99     void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
100
101     int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
102
103     int onUserAdded(userid_t userId, int userSerialNumber);
104     int onUserRemoved(userid_t userId);
105     int onUserStarted(userid_t userId);
106     int onUserStopped(userid_t userId);
107
108     int onSecureKeyguardStateChanged(bool isShowing);
109
110     int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
111
112     int remountUid(uid_t uid, const std::string& mode);
113
114     /* Reset all internal state, typically during framework boot */
115     int reset();
116     /* Prepare for device shutdown, safely unmounting all devices */
117     int shutdown();
118     /* Unmount all volumes, usually for encryption */
119     int unmountAll();
120
121     int updateVirtualDisk();
122     int setDebug(bool enable);
123
124     static VolumeManager *Instance();
125
126     /*
127      * Ensure that all directories along given path exist, creating parent
128      * directories as needed.  Validates that given path is absolute and that
129      * it contains no relative "." or ".." paths or symlinks.  Last path segment
130      * is treated as filename and ignored, unless the path ends with "/".  Also
131      * ensures that path belongs to a volume managed by vold.
132      */
133     int mkdirs(const std::string& path);
134
135     int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
136             std::string* outVolId);
137     int destroyObb(const std::string& volId);
138
139     int mountAppFuse(uid_t uid, pid_t pid, int mountId, android::base::unique_fd* device_fd);
140     int unmountAppFuse(uid_t uid, pid_t pid, int mountId);
141
142 private:
143     VolumeManager();
144     void readInitialState();
145
146     int linkPrimary(userid_t userId);
147
148     void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
149     void handleDiskChanged(dev_t device);
150     void handleDiskRemoved(dev_t device);
151
152     std::mutex mLock;
153     std::mutex mCryptLock;
154
155     android::sp<android::os::IVoldListener> mListener;
156
157     std::list<std::shared_ptr<DiskSource>> mDiskSources;
158     std::list<std::shared_ptr<android::vold::Disk>> mDisks;
159     std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
160     std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
161
162     std::unordered_map<userid_t, int> mAddedUsers;
163     std::unordered_set<userid_t> mStartedUsers;
164
165     std::string mVirtualDiskPath;
166     std::shared_ptr<android::vold::Disk> mVirtualDisk;
167     std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
168     std::shared_ptr<android::vold::VolumeBase> mPrimary;
169
170     int mNextObbId;
171     bool mSecureKeyguardShowing;
172 };
173
174 #endif