blob: dcfbdad8cd6907fa9c9ea170759acd88a372358d [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
20#include <pthread.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include <fnmatch.h>
22#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023
Ken Sumrall29d8da82011-05-18 17:20:07 -070024#ifdef __cplusplus
Jeff Sharkey36801cc2015-03-13 16:09:20 -070025
Jeff Sharkey36801cc2015-03-13 16:09:20 -070026#include <list>
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070027#include <mutex>
28#include <string>
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070029#include <unordered_map>
30#include <unordered_set>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070031
Jeff Sharkey11c2d382017-09-11 10:32:01 -060032#include <android-base/unique_fd.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070033#include <cutils/multiuser.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>
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070036#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070037
Jeff Sharkey814e9d32017-09-13 11:49:44 -060038#include "android/os/IVoldListener.h"
39
Jeff Sharkey11c2d382017-09-11 10:32:01 -060040#include "model/Disk.h"
41#include "model/VolumeBase.h"
San Mehatf1b736b2009-10-10 17:22:08 -070042
Jeff Sharkey11c2d382017-09-11 10:32:01 -060043#define DEBUG_APPFUSE 0
44
San Mehatf1b736b2009-10-10 17:22:08 -070045class VolumeManager {
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070046public:
Keun-young Park375ac252017-08-02 17:45:48 -070047 //TODO remove this with better solution, b/64143519
48 static bool shutting_down;
49
San Mehatf1b736b2009-10-10 17:22:08 -070050private:
51 static VolumeManager *sInstance;
52
San Mehatd9a4e352010-03-12 13:32:47 -080053 bool mDebug;
San Mehatf1b736b2009-10-10 17:22:08 -070054
55public:
56 virtual ~VolumeManager();
57
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070058 // TODO: pipe all requests through VM to avoid exposing this lock
59 std::mutex& getLock() { return mLock; }
Jeff Sharkey83b559c2017-09-12 16:30:52 -060060 std::mutex& getCryptLock() { return mCryptLock; }
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070061
Jeff Sharkey814e9d32017-09-13 11:49:44 -060062 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
63 android::sp<android::os::IVoldListener> getListener() { return mListener; }
64
San Mehatf1b736b2009-10-10 17:22:08 -070065 int start();
66 int stop();
67
San Mehatfd7f5872009-10-12 11:32:47 -070068 void handleBlockEvent(NetlinkEvent *evt);
San Mehatf1b736b2009-10-10 17:22:08 -070069
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070 class DiskSource {
71 public:
72 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) :
73 mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {
74 }
75
76 bool matches(const std::string& sysPath) {
77 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
78 }
79
80 const std::string& getNickname() { return mNickname; }
81 int getFlags() { return mFlags; }
82
83 private:
84 std::string mSysPattern;
85 std::string mNickname;
86 int mFlags;
87 };
88
89 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
90
91 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
92 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
93
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070094 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
95
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070096 int forgetPartition(const std::string& partGuid);
97
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070098 int onUserAdded(userid_t userId, int userSerialNumber);
99 int onUserRemoved(userid_t userId);
100 int onUserStarted(userid_t userId);
101 int onUserStopped(userid_t userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102
103 int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
104
Jeff Sharkey66270a22015-06-24 11:49:24 -0700105 int remountUid(uid_t uid, const std::string& mode);
106
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700107 /* Reset all internal state, typically during framework boot */
108 int reset();
109 /* Prepare for device shutdown, safely unmounting all devices */
110 int shutdown();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700111 /* Unmount all volumes, usually for encryption */
112 int unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600114 int updateVirtualDisk();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700115 int setDebug(bool enable);
San Mehatd9a4e352010-03-12 13:32:47 -0800116
San Mehatf1b736b2009-10-10 17:22:08 -0700117 static VolumeManager *Instance();
118
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700119 /*
120 * Ensure that all directories along given path exist, creating parent
121 * directories as needed. Validates that given path is absolute and that
122 * it contains no relative "." or ".." paths or symlinks. Last path segment
123 * is treated as filename and ignored, unless the path ends with "/". Also
124 * ensures that path belongs to a volume managed by vold.
125 */
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600126 int mkdirs(const char* path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700127
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600128 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
129 std::string* outVolId);
130 int destroyObb(const std::string& volId);
131
132 int mountAppFuse(uid_t uid, pid_t pid, int mountId, android::base::unique_fd* device_fd);
133 int unmountAppFuse(uid_t uid, pid_t pid, int mountId);
134
San Mehatf1b736b2009-10-10 17:22:08 -0700135private:
136 VolumeManager();
Mike Lockwood99635f62010-06-25 23:04:04 -0400137 void readInitialState();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700138
139 int linkPrimary(userid_t userId);
140
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700141 std::mutex mLock;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600142 std::mutex mCryptLock;
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700143
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600144 android::sp<android::os::IVoldListener> mListener;
145
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700146 std::list<std::shared_ptr<DiskSource>> mDiskSources;
147 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600148 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700149
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700150 std::unordered_map<userid_t, int> mAddedUsers;
151 std::unordered_set<userid_t> mStartedUsers;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600153 std::string mVirtualDiskPath;
154 std::shared_ptr<android::vold::Disk> mVirtualDisk;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
156 std::shared_ptr<android::vold::VolumeBase> mPrimary;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600157
158 int mNextObbId;
San Mehatf1b736b2009-10-10 17:22:08 -0700159};
Ken Sumrall29d8da82011-05-18 17:20:07 -0700160
161extern "C" {
162#endif /* __cplusplus */
Chih-Hung Hsiehaae79382016-06-10 14:13:59 -0700163#define UNMOUNT_NOT_MOUNTED_ERR (-2)
Jeff Sharkey9c484982015-03-31 10:35:33 -0700164 int vold_unmountAll(void);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700165#ifdef __cplusplus
166}
167#endif
168
San Mehatf1b736b2009-10-10 17:22:08 -0700169#endif