blob: a2d6c5bec30fd6a8a63a14e0d03a2d7f3832afa8 [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 Crowley8915d622018-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>
26#include <string>
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070027#include <unordered_map>
28#include <unordered_set>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070029
Jeff Sharkey11c2d382017-09-11 10:32:01 -060030#include <android-base/unique_fd.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070031#include <cutils/multiuser.h>
Paul Crowley8915d622018-09-18 15:14:18 -070032#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070033#include <utils/List.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070034#include <utils/Timers.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035
Jeff Sharkey814e9d32017-09-13 11:49:44 -060036#include "android/os/IVoldListener.h"
37
Jeff Sharkey11c2d382017-09-11 10:32:01 -060038#include "model/Disk.h"
39#include "model/VolumeBase.h"
San Mehatf1b736b2009-10-10 17:22:08 -070040
Jeff Sharkey11c2d382017-09-11 10:32:01 -060041#define DEBUG_APPFUSE 0
42
San Mehatf1b736b2009-10-10 17:22:08 -070043class VolumeManager {
Paul Crowley8915d622018-09-18 15:14:18 -070044 private:
45 static VolumeManager* sInstance;
San Mehatf1b736b2009-10-10 17:22:08 -070046
Paul Crowley8915d622018-09-18 15:14:18 -070047 bool mDebug;
San Mehatf1b736b2009-10-10 17:22:08 -070048
Paul Crowley8915d622018-09-18 15:14:18 -070049 public:
San Mehatf1b736b2009-10-10 17:22:08 -070050 virtual ~VolumeManager();
51
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070052 // TODO: pipe all requests through VM to avoid exposing this lock
53 std::mutex& getLock() { return mLock; }
Jeff Sharkey83b559c2017-09-12 16:30:52 -060054 std::mutex& getCryptLock() { return mCryptLock; }
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070055
Jeff Sharkey814e9d32017-09-13 11:49:44 -060056 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
57 android::sp<android::os::IVoldListener> getListener() { return mListener; }
58
San Mehatf1b736b2009-10-10 17:22:08 -070059 int start();
60 int stop();
61
Paul Crowley8915d622018-09-18 15:14:18 -070062 void handleBlockEvent(NetlinkEvent* evt);
San Mehatf1b736b2009-10-10 17:22:08 -070063
Jeff Sharkey36801cc2015-03-13 16:09:20 -070064 class DiskSource {
Paul Crowley8915d622018-09-18 15:14:18 -070065 public:
66 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
67 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
Jeff Sharkey36801cc2015-03-13 16:09:20 -070068
69 bool matches(const std::string& sysPath) {
70 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
71 }
72
73 const std::string& getNickname() { return mNickname; }
74 int getFlags() { return mFlags; }
75
Paul Crowley8915d622018-09-18 15:14:18 -070076 private:
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077 std::string mSysPattern;
78 std::string mNickname;
79 int mFlags;
80 };
81
82 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
83
84 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
85 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
86
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070087 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
88
Jeff Sharkey3ce18252017-10-24 11:08:45 -060089 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070090
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070091 int onUserAdded(userid_t userId, int userSerialNumber);
92 int onUserRemoved(userid_t userId);
93 int onUserStarted(userid_t userId);
94 int onUserStopped(userid_t userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095
Jeff Sharkey401b2602017-12-14 22:15:20 -070096 int onSecureKeyguardStateChanged(bool isShowing);
97
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098 int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
99
Jeff Sharkey66270a22015-06-24 11:49:24 -0700100 int remountUid(uid_t uid, const std::string& mode);
101
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102 /* Reset all internal state, typically during framework boot */
103 int reset();
104 /* Prepare for device shutdown, safely unmounting all devices */
105 int shutdown();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700106 /* Unmount all volumes, usually for encryption */
107 int unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700108
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600109 int updateVirtualDisk();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700110 int setDebug(bool enable);
San Mehatd9a4e352010-03-12 13:32:47 -0800111
Paul Crowley8915d622018-09-18 15:14:18 -0700112 static VolumeManager* Instance();
San Mehatf1b736b2009-10-10 17:22:08 -0700113
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700114 /*
115 * Ensure that all directories along given path exist, creating parent
116 * directories as needed. Validates that given path is absolute and that
117 * it contains no relative "." or ".." paths or symlinks. Last path segment
118 * is treated as filename and ignored, unless the path ends with "/". Also
119 * ensures that path belongs to a volume managed by vold.
120 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600121 int mkdirs(const std::string& path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700122
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600123 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
Paul Crowley8915d622018-09-18 15:14:18 -0700124 std::string* outVolId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600125 int destroyObb(const std::string& volId);
126
Risan8c9f3322018-10-29 08:52:56 +0900127 int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
128 const std::string& fsType, const std::string& fsUuid,
129 const std::string& fsLabel, std::string* outVolId);
130 int destroyStubVolume(const std::string& volId);
131
Risan5f53cd32018-10-26 20:56:45 -0600132 int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
133 int unmountAppFuse(uid_t uid, int mountId);
134 int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600135
Paul Crowley8915d622018-09-18 15:14:18 -0700136 private:
San Mehatf1b736b2009-10-10 17:22:08 -0700137 VolumeManager();
Mike Lockwood99635f62010-06-25 23:04:04 -0400138 void readInitialState();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139
140 int linkPrimary(userid_t userId);
141
Jeff Sharkey401b2602017-12-14 22:15:20 -0700142 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
143 void handleDiskChanged(dev_t device);
144 void handleDiskRemoved(dev_t device);
145
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700146 std::mutex mLock;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600147 std::mutex mCryptLock;
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700148
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600149 android::sp<android::os::IVoldListener> mListener;
150
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700151 std::list<std::shared_ptr<DiskSource>> mDiskSources;
152 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700153 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600154 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
Risan8c9f3322018-10-29 08:52:56 +0900155 std::list<std::shared_ptr<android::vold::VolumeBase>> mStubVolumes;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700157 std::unordered_map<userid_t, int> mAddedUsers;
158 std::unordered_set<userid_t> mStartedUsers;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600160 std::string mVirtualDiskPath;
161 std::shared_ptr<android::vold::Disk> mVirtualDisk;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700162 std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
163 std::shared_ptr<android::vold::VolumeBase> mPrimary;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600164
165 int mNextObbId;
Risan8c9f3322018-10-29 08:52:56 +0900166 int mNextStubVolumeId;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700167 bool mSecureKeyguardShowing;
San Mehatf1b736b2009-10-10 17:22:08 -0700168};
Ken Sumrall29d8da82011-05-18 17:20:07 -0700169
San Mehatf1b736b2009-10-10 17:22:08 -0700170#endif