blob: 6f1584619bc3a013bef4d83f3caa7d5780643b0f [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 Sharkey67b8c492017-09-21 17:08:43 -060017#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
Yabin Cuid1104f72015-01-02 13:28:28 -080019#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080022#include <mntent.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/ioctl.h>
27#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080028#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070030#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070031#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080032#include <unistd.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070033#include <array>
San Mehata19b2502010-01-06 10:33:53 -080034
San Mehata2677e42009-12-13 10:40:18 -080035#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070036
Jiyong Park8d21c922019-01-04 13:35:25 +090037#include <ApexProperties.sysprop.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080038#include <android-base/logging.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070039#include <android-base/parseint.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060040#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080041#include <android-base/stringprintf.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070042#include <android-base/strings.h>
Narayan Kamath02efdf52019-11-27 10:53:51 +000043#include <async_safe/log.h>
Jeff Vander Stoep58890832017-10-23 17:12:31 -070044
Jeff Sharkey71ebe152013-09-17 17:24:38 -070045#include <cutils/fs.h>
Jeff Sharkey67b8c492017-09-21 17:08:43 -060046#include <utils/Trace.h>
San Mehatf1b736b2009-10-10 17:22:08 -070047
Robert Craigb9e3ba52014-02-04 10:53:00 -050048#include <selinux/android.h>
49
San Mehatfd7f5872009-10-12 11:32:47 -070050#include <sysutils/NetlinkEvent.h>
51
Kenny Root344ca102012-04-03 17:23:01 -070052#include <private/android_filesystem_config.h>
53
Eric Biggersa701c452018-10-23 13:06:55 -070054#include <fscrypt/fscrypt.h>
Paul Crowleyc6433a22017-10-24 14:54:43 -070055
Risanac02a482018-10-31 21:59:47 -060056#include "AppFuseUtil.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070057#include "Devmapper.h"
Eric Biggersa701c452018-10-23 13:06:55 -070058#include "FsCrypt.h"
San Mehata19b2502010-01-06 10:33:53 -080059#include "Loop.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070060#include "NetlinkManager.h"
61#include "Process.h"
62#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070063#include "VoldNativeService.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070064#include "VoldUtil.h"
65#include "VolumeManager.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070066#include "fs/Ext4.h"
67#include "fs/Vfat.h"
Paul Crowleyc6433a22017-10-24 14:54:43 -070068#include "model/EmulatedVolume.h"
69#include "model/ObbVolume.h"
Zima438b242019-09-25 14:37:38 +010070#include "model/PrivateVolume.h"
Risan8c9f3322018-10-29 08:52:56 +090071#include "model/StubVolume.h"
San Mehat23969932010-01-09 07:08:06 -080072
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000073using android::OK;
Sudheer Shanka53947a32018-08-01 10:24:13 -070074using android::base::GetBoolProperty;
Mark Salyzyn86e81e72018-09-20 10:09:27 -070075using android::base::StartsWith;
Sudheer Shanka53947a32018-08-01 10:24:13 -070076using android::base::StringAppendF;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077using android::base::StringPrintf;
Jeff Sharkey11c2d382017-09-11 10:32:01 -060078using android::base::unique_fd;
Sudheer Shanka023b5392019-02-06 12:39:19 -080079using android::vold::BindMount;
Sudheer Shankaf9b38a52019-02-14 19:09:51 +000080using android::vold::CreateDir;
Sudheer Shanka30df1c62019-02-22 17:03:02 -080081using android::vold::DeleteDirContents;
Sudheer Shanka023b5392019-02-06 12:39:19 -080082using android::vold::DeleteDirContentsAndDir;
Martijn Coenen62a4b272020-01-31 15:23:09 +010083using android::vold::IsFilesystemSupported;
84using android::vold::PrepareAndroidDirs;
Martijn Coenen04bb17f2020-02-10 23:48:11 +010085using android::vold::PrepareAppDirFromRoot;
Zima438b242019-09-25 14:37:38 +010086using android::vold::PrivateVolume;
Sudheer Shanka023b5392019-02-06 12:39:19 -080087using android::vold::Symlink;
88using android::vold::Unlink;
89using android::vold::UnmountTree;
Sudheer Shanka03992e32018-12-12 12:43:38 -080090using android::vold::VoldNativeService;
Zima438b242019-09-25 14:37:38 +010091using android::vold::VolumeBase;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060093static const char* kPathUserMount = "/mnt/user";
94static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
95
96static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
97
Sudheer Shanka53947a32018-08-01 10:24:13 -070098static const std::string kEmptyString("");
99
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600100/* 512MiB is large enough for testing purposes */
101static const unsigned int kSizeVirtualDisk = 536870912;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +0800104static const unsigned int kMajorBlockExperimentalMin = 240;
105static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700106
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700107VolumeManager* VolumeManager::sInstance = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700108
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700109VolumeManager* VolumeManager::Instance() {
110 if (!sInstance) sInstance = new VolumeManager();
San Mehatf1b736b2009-10-10 17:22:08 -0700111 return sInstance;
112}
113
114VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800115 mDebug = false;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600116 mNextObbId = 0;
Risan82e90de2020-02-04 16:07:21 +0900117 mNextStubId = 0;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700118 // For security reasons, assume that a secure keyguard is
119 // showing until we hear otherwise
120 mSecureKeyguardShowing = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700121}
122
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700123VolumeManager::~VolumeManager() {}
San Mehatd9a4e352010-03-12 13:32:47 -0800124
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600125int VolumeManager::updateVirtualDisk() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600126 ATRACE_NAME("VolumeManager::updateVirtualDisk");
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700127 if (GetBoolProperty(kPropVirtualDisk, false)) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600128 if (access(kPathVirtualDisk, F_OK) != 0) {
129 Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
130 }
131
132 if (mVirtualDisk == nullptr) {
133 if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
134 LOG(ERROR) << "Failed to create virtual disk";
135 return -1;
136 }
137
138 struct stat buf;
139 if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
140 PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
141 return -1;
142 }
143
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700144 auto disk = new android::vold::Disk(
145 "virtual", buf.st_rdev, "virtual",
146 android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600147 mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700148 handleDiskAdded(mVirtualDisk);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600149 }
150 } else {
151 if (mVirtualDisk != nullptr) {
152 dev_t device = mVirtualDisk->getDevice();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700153 handleDiskRemoved(device);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600154
155 Loop::destroyByDevice(mVirtualDiskPath.c_str());
156 mVirtualDisk = nullptr;
157 }
158
159 if (access(kPathVirtualDisk, F_OK) == 0) {
160 unlink(kPathVirtualDisk);
161 }
162 }
163 return 0;
164}
165
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700166int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800167 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700168 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800169}
170
San Mehatf1b736b2009-10-10 17:22:08 -0700171int VolumeManager::start() {
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600172 ATRACE_NAME("VolumeManager::start");
173
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700174 // Always start from a clean slate by unmounting everything in
175 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700176 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600178 Devmapper::destroyAll();
179 Loop::destroyAll();
180
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181 // Assume that we always have an emulated volume on internal
182 // storage; the framework will decide if it should be mounted.
Zima438b242019-09-25 14:37:38 +0100183 CHECK(mInternalEmulatedVolumes.empty());
184
185 auto vol = std::shared_ptr<android::vold::VolumeBase>(
186 new android::vold::EmulatedVolume("/data/media", 0));
187 vol->setMountUserId(0);
188 vol->create();
189 mInternalEmulatedVolumes.push_back(vol);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600191 // Consider creating a virtual disk
192 updateVirtualDisk();
193
San Mehatf1b736b2009-10-10 17:22:08 -0700194 return 0;
195}
196
197int VolumeManager::stop() {
Zima438b242019-09-25 14:37:38 +0100198 CHECK(!mInternalEmulatedVolumes.empty());
199 for (const auto& vol : mInternalEmulatedVolumes) {
200 vol->destroy();
201 }
202 mInternalEmulatedVolumes.clear();
203
San Mehatf1b736b2009-10-10 17:22:08 -0700204 return 0;
205}
206
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700207void VolumeManager::handleBlockEvent(NetlinkEvent* evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700208 std::lock_guard<std::mutex> lock(mLock);
209
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700210 if (mDebug) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700211 LOG(DEBUG) << "----------------";
212 LOG(DEBUG) << "handleBlockEvent with action " << (int)evt->getAction();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700213 evt->dump();
214 }
San Mehatf1b736b2009-10-10 17:22:08 -0700215
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700216 std::string eventPath(evt->findParam("DEVPATH") ? evt->findParam("DEVPATH") : "");
217 std::string devType(evt->findParam("DEVTYPE") ? evt->findParam("DEVTYPE") : "");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218
219 if (devType != "disk") return;
220
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600221 int major = std::stoi(evt->findParam("MAJOR"));
222 int minor = std::stoi(evt->findParam("MINOR"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 dev_t device = makedev(major, minor);
224
225 switch (evt->getAction()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700226 case NetlinkEvent::Action::kAdd: {
227 for (const auto& source : mDiskSources) {
228 if (source->matches(eventPath)) {
229 // For now, assume that MMC and virtio-blk (the latter is
230 // emulator-specific; see Disk.cpp for details) devices are SD,
231 // and that everything else is USB
232 int flags = source->getFlags();
233 if (major == kMajorBlockMmc || (android::vold::IsRunningInEmulator() &&
234 major >= (int)kMajorBlockExperimentalMin &&
235 major <= (int)kMajorBlockExperimentalMax)) {
236 flags |= android::vold::Disk::Flags::kSd;
237 } else {
238 flags |= android::vold::Disk::Flags::kUsb;
239 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700240
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700241 auto disk =
242 new android::vold::Disk(eventPath, device, source->getNickname(), flags);
243 handleDiskAdded(std::shared_ptr<android::vold::Disk>(disk));
244 break;
245 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700247 break;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248 }
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700249 case NetlinkEvent::Action::kChange: {
250 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
251 handleDiskChanged(device);
252 break;
253 }
254 case NetlinkEvent::Action::kRemove: {
255 handleDiskRemoved(device);
256 break;
257 }
258 default: {
259 LOG(WARNING) << "Unexpected block event action " << (int)evt->getAction();
260 break;
261 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 }
263}
264
Jeff Sharkey401b2602017-12-14 22:15:20 -0700265void VolumeManager::handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk) {
266 // For security reasons, if secure keyguard is showing, wait
267 // until the user unlocks the device to actually touch it
Martijn Coenencf5916f2020-01-03 14:36:45 +0100268 // Additionally, wait until user 0 is actually started, since we need
269 // the user to be up before we can mount a FUSE daemon to handle the disk.
270 bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700271 if (mSecureKeyguardShowing) {
272 LOG(INFO) << "Found disk at " << disk->getEventPath()
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700273 << " but delaying scan due to secure keyguard";
Jeff Sharkey401b2602017-12-14 22:15:20 -0700274 mPendingDisks.push_back(disk);
Martijn Coenencf5916f2020-01-03 14:36:45 +0100275 } else if (!userZeroStarted) {
276 LOG(INFO) << "Found disk at " << disk->getEventPath()
277 << " but delaying scan due to user zero not having started";
278 mPendingDisks.push_back(disk);
Jeff Sharkey401b2602017-12-14 22:15:20 -0700279 } else {
280 disk->create();
281 mDisks.push_back(disk);
282 }
283}
284
285void VolumeManager::handleDiskChanged(dev_t device) {
286 for (const auto& disk : mDisks) {
287 if (disk->getDevice() == device) {
288 disk->readMetadata();
289 disk->readPartitions();
290 }
291 }
292
293 // For security reasons, we ignore all pending disks, since
294 // we'll scan them once the device is unlocked
295}
296
297void VolumeManager::handleDiskRemoved(dev_t device) {
298 auto i = mDisks.begin();
299 while (i != mDisks.end()) {
300 if ((*i)->getDevice() == device) {
301 (*i)->destroy();
302 i = mDisks.erase(i);
303 } else {
304 ++i;
305 }
306 }
307 auto j = mPendingDisks.begin();
308 while (j != mPendingDisks.end()) {
309 if ((*j)->getDevice() == device) {
310 j = mPendingDisks.erase(j);
311 } else {
312 ++j;
313 }
314 }
315}
316
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800318 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700319 mDiskSources.push_back(diskSource);
320}
321
322std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
323 for (auto disk : mDisks) {
324 if (disk->getId() == id) {
325 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700326 }
327 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700328 return nullptr;
329}
San Mehatf1b736b2009-10-10 17:22:08 -0700330
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700331std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
Zima438b242019-09-25 14:37:38 +0100332 for (const auto& vol : mInternalEmulatedVolumes) {
333 if (vol->getId() == id) {
334 return vol;
335 }
San Mehatf1b736b2009-10-10 17:22:08 -0700336 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700337 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700338 auto vol = disk->findVolume(id);
339 if (vol != nullptr) {
340 return vol;
341 }
342 }
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600343 for (const auto& vol : mObbVolumes) {
344 if (vol->getId() == id) {
345 return vol;
346 }
347 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700348 return nullptr;
349}
350
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800351void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
352 std::list<std::string>& list) const {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700353 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700354 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700355 disk->listVolumes(type, list);
356 }
357}
358
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600359int VolumeManager::forgetPartition(const std::string& partGuid, const std::string& fsUuid) {
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700360 std::string normalizedGuid;
361 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
362 LOG(WARNING) << "Invalid GUID " << partGuid;
363 return -1;
364 }
365
Paul Crowleyc6433a22017-10-24 14:54:43 -0700366 bool success = true;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700367 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
368 if (unlink(keyPath.c_str()) != 0) {
369 LOG(ERROR) << "Failed to unlink " << keyPath;
Paul Crowleyc6433a22017-10-24 14:54:43 -0700370 success = false;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700371 }
Eric Biggersa701c452018-10-23 13:06:55 -0700372 if (fscrypt_is_native()) {
373 if (!fscrypt_destroy_volume_keys(fsUuid)) {
Paul Crowleyc6433a22017-10-24 14:54:43 -0700374 success = false;
375 }
376 }
377 return success ? 0 : -1;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700378}
379
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700380int VolumeManager::linkPrimary(userid_t userId) {
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000381 if (!GetBoolProperty(android::vold::kPropFuse, false)) {
Zima438b242019-09-25 14:37:38 +0100382 std::string source(mPrimary->getPath());
383 if (mPrimary->isEmulated()) {
384 source = StringPrintf("%s/%d", source.c_str(), userId);
385 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
386 }
Zim3623a212019-07-19 16:46:53 +0100387
Zima438b242019-09-25 14:37:38 +0100388 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
389 LOG(DEBUG) << "Linking " << source << " to " << target;
390 Symlink(source, target);
Zim3623a212019-07-19 16:46:53 +0100391 }
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700392 return 0;
393}
394
Zima438b242019-09-25 14:37:38 +0100395void VolumeManager::destroyEmulatedVolumesForUser(userid_t userId) {
396 // Destroy and remove all unstacked EmulatedVolumes for the user
397 auto i = mInternalEmulatedVolumes.begin();
398 while (i != mInternalEmulatedVolumes.end()) {
399 auto vol = *i;
400 if (vol->getMountUserId() == userId) {
401 vol->destroy();
402 i = mInternalEmulatedVolumes.erase(i);
403 } else {
404 i++;
405 }
406 }
407
408 // Destroy and remove all stacked EmulatedVolumes for the user on each mounted private volume
409 std::list<std::string> private_vols;
410 listVolumes(VolumeBase::Type::kPrivate, private_vols);
411 for (const std::string& id : private_vols) {
412 PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get());
413 std::list<std::shared_ptr<VolumeBase>> vols_to_remove;
414 if (pvol->getState() == VolumeBase::State::kMounted) {
415 for (const auto& vol : pvol->getVolumes()) {
416 if (vol->getMountUserId() == userId) {
417 vols_to_remove.push_back(vol);
418 }
419 }
420 for (const auto& vol : vols_to_remove) {
421 vol->destroy();
422 pvol->removeVolume(vol);
423 }
424 } // else EmulatedVolumes will be destroyed on VolumeBase#unmount
425 }
426}
427
428void VolumeManager::createEmulatedVolumesForUser(userid_t userId) {
429 // Create unstacked EmulatedVolumes for the user
430 auto vol = std::shared_ptr<android::vold::VolumeBase>(
431 new android::vold::EmulatedVolume("/data/media", userId));
432 vol->setMountUserId(userId);
433 mInternalEmulatedVolumes.push_back(vol);
434 vol->create();
435
436 // Create stacked EmulatedVolumes for the user on each PrivateVolume
437 std::list<std::string> private_vols;
438 listVolumes(VolumeBase::Type::kPrivate, private_vols);
439 for (const std::string& id : private_vols) {
440 PrivateVolume* pvol = static_cast<PrivateVolume*>(findVolume(id).get());
441 if (pvol->getState() == VolumeBase::State::kMounted) {
442 auto evol =
443 std::shared_ptr<android::vold::VolumeBase>(new android::vold::EmulatedVolume(
444 pvol->getPath() + "/media", pvol->getRawDevice(), pvol->getFsUuid(),
445 userId));
446 evol->setMountUserId(userId);
447 pvol->addVolume(evol);
448 evol->create();
449 } // else EmulatedVolumes will be created per user when on PrivateVolume#doMount
450 }
451}
452
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700453int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
Zima438b242019-09-25 14:37:38 +0100454 LOG(INFO) << "onUserAdded: " << userId;
455
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700456 mAddedUsers[userId] = userSerialNumber;
457 return 0;
458}
459
460int VolumeManager::onUserRemoved(userid_t userId) {
Zima438b242019-09-25 14:37:38 +0100461 LOG(INFO) << "onUserRemoved: " << userId;
462
Zim2d45d9b2019-11-14 16:19:05 +0000463 onUserStopped(userId);
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700464 mAddedUsers.erase(userId);
465 return 0;
466}
467
Sudheer Shankaebce4cc2019-04-29 10:46:35 -0700468int VolumeManager::onUserStarted(userid_t userId) {
Zima438b242019-09-25 14:37:38 +0100469 LOG(INFO) << "onUserStarted: " << userId;
470
Zim2d45d9b2019-11-14 16:19:05 +0000471 if (mStartedUsers.find(userId) == mStartedUsers.end()) {
472 createEmulatedVolumesForUser(userId);
473 }
474
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000475 if (!GetBoolProperty(android::vold::kPropFuse, false)) {
Zima438b242019-09-25 14:37:38 +0100476 // Note that sometimes the system will spin up processes from Zygote
477 // before actually starting the user, so we're okay if Zygote
478 // already created this directory.
479 std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
480 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
481
482 if (mPrimary) {
483 linkPrimary(userId);
484 }
485 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700486
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700487 mStartedUsers.insert(userId);
Martijn Coenencf5916f2020-01-03 14:36:45 +0100488
489 createPendingDisksIfNeeded();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700490 return 0;
491}
492
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700493int VolumeManager::onUserStopped(userid_t userId) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700494 LOG(VERBOSE) << "onUserStopped: " << userId;
Zima438b242019-09-25 14:37:38 +0100495
Zim2d45d9b2019-11-14 16:19:05 +0000496 if (mStartedUsers.find(userId) != mStartedUsers.end()) {
Zima438b242019-09-25 14:37:38 +0100497 destroyEmulatedVolumesForUser(userId);
498 }
499
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700500 mStartedUsers.erase(userId);
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700501 return 0;
502}
503
Martijn Coenencf5916f2020-01-03 14:36:45 +0100504void VolumeManager::createPendingDisksIfNeeded() {
505 bool userZeroStarted = mStartedUsers.find(0) != mStartedUsers.end();
506 if (!mSecureKeyguardShowing && userZeroStarted) {
507 // Now that secure keyguard has been dismissed and user 0 has
508 // started, process any pending disks
Jeff Sharkey401b2602017-12-14 22:15:20 -0700509 for (const auto& disk : mPendingDisks) {
510 disk->create();
511 mDisks.push_back(disk);
512 }
513 mPendingDisks.clear();
514 }
Martijn Coenencf5916f2020-01-03 14:36:45 +0100515}
516
517int VolumeManager::onSecureKeyguardStateChanged(bool isShowing) {
518 mSecureKeyguardShowing = isShowing;
519 createPendingDisksIfNeeded();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700520 return 0;
521}
522
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700523int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
524 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700525 for (userid_t userId : mStartedUsers) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700526 linkPrimary(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700527 }
528 return 0;
529}
530
Narayan Kamath02efdf52019-11-27 10:53:51 +0000531// This code is executed after a fork so it's very important that the set of
532// methods we call here is strictly limited.
533//
534// TODO: Get rid of this guesswork altogether and instead exec a process
535// immediately after fork to do our bindding for us.
536static bool childProcess(const char* storageSource, const char* userSource, int nsFd,
537 struct dirent* de) {
538 if (setns(nsFd, CLONE_NEWNS) != 0) {
539 async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to setns for %s :%s", de->d_name,
540 strerror(errno));
541 return false;
542 }
543
544 // NOTE: Inlined from vold::UnmountTree here to avoid using PLOG methods and
545 // to also protect against future changes that may cause issues across a
546 // fork.
547 if (TEMP_FAILURE_RETRY(umount2("/storage/", MNT_DETACH)) < 0 && errno != EINVAL &&
548 errno != ENOENT) {
549 async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to unmount /storage/ :%s",
550 strerror(errno));
551 return false;
552 }
553
554 if (TEMP_FAILURE_RETRY(mount(storageSource, "/storage", NULL, MS_BIND | MS_REC, NULL)) == -1) {
555 async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s",
556 storageSource, de->d_name, strerror(errno));
557 return false;
558 }
559
560 if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL, MS_REC | MS_SLAVE, NULL)) == -1) {
561 async_safe_format_log(ANDROID_LOG_ERROR, "vold",
562 "Failed to set MS_SLAVE to /storage for %s :%s", de->d_name,
563 strerror(errno));
564 return false;
565 }
566
567 if (TEMP_FAILURE_RETRY(mount(userSource, "/storage/self", NULL, MS_BIND, NULL)) == -1) {
568 async_safe_format_log(ANDROID_LOG_ERROR, "vold", "Failed to mount %s for %s :%s",
569 userSource, de->d_name, strerror(errno));
570 return false;
571 }
572
573 return true;
574}
575
Sudheer Shanka817b9112018-12-13 17:40:28 -0800576int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000577 if (GetBoolProperty(android::vold::kPropFuse, false)) {
Zim3623a212019-07-19 16:46:53 +0100578 // TODO(135341433): Implement fuse specific logic.
579 return 0;
580 }
Sudheer Shanka817b9112018-12-13 17:40:28 -0800581 std::string mode;
582 switch (mountMode) {
583 case VoldNativeService::REMOUNT_MODE_NONE:
584 mode = "none";
585 break;
586 case VoldNativeService::REMOUNT_MODE_DEFAULT:
587 mode = "default";
588 break;
589 case VoldNativeService::REMOUNT_MODE_READ:
590 mode = "read";
591 break;
592 case VoldNativeService::REMOUNT_MODE_WRITE:
Sudheer Shankaa05ea742019-04-12 13:55:28 -0700593 case VoldNativeService::REMOUNT_MODE_LEGACY:
594 case VoldNativeService::REMOUNT_MODE_INSTALLER:
Sudheer Shanka817b9112018-12-13 17:40:28 -0800595 mode = "write";
596 break;
Sudheer Shanka36bdf7a2019-04-18 15:18:30 -0700597 case VoldNativeService::REMOUNT_MODE_FULL:
598 mode = "full";
599 break;
Zim981222f2019-09-09 10:24:44 +0100600 case VoldNativeService::REMOUNT_MODE_PASS_THROUGH:
601 mode = "pass_through";
602 break;
Sudheer Shanka817b9112018-12-13 17:40:28 -0800603 default:
604 PLOG(ERROR) << "Unknown mode " << std::to_string(mountMode);
605 return -1;
606 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700607 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
608
609 DIR* dir;
610 struct dirent* de;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600611 std::string rootName;
612 std::string pidName;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700613 int pidFd;
614 int nsFd;
615 struct stat sb;
616 pid_t child;
Narayan Kamath02efdf52019-11-27 10:53:51 +0000617 std::string userSource;
618 std::string storageSource;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700619
Jiyong Park8d21c922019-01-04 13:35:25 +0900620 static bool apexUpdatable = android::sysprop::ApexProperties::updatable().value_or(false);
621
Jeff Sharkey66270a22015-06-24 11:49:24 -0700622 if (!(dir = opendir("/proc"))) {
623 PLOG(ERROR) << "Failed to opendir";
624 return -1;
625 }
626
627 // Figure out root namespace to compare against below
Jeff Sharkey3472e522017-10-06 18:02:53 -0600628 if (!android::vold::Readlinkat(dirfd(dir), "1/ns/mnt", &rootName)) {
629 PLOG(ERROR) << "Failed to read root namespace";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700630 closedir(dir);
631 return -1;
632 }
633
634 // Poke through all running PIDs look for apps running as UID
635 while ((de = readdir(dir))) {
Jeff Vander Stoep58890832017-10-23 17:12:31 -0700636 pid_t pid;
637 if (de->d_type != DT_DIR) continue;
638 if (!android::base::ParseInt(de->d_name, &pid)) continue;
639
Jeff Sharkey66270a22015-06-24 11:49:24 -0700640 pidFd = -1;
641 nsFd = -1;
642
643 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
644 if (pidFd < 0) {
645 goto next;
646 }
647 if (fstat(pidFd, &sb) != 0) {
648 PLOG(WARNING) << "Failed to stat " << de->d_name;
649 goto next;
650 }
651 if (sb.st_uid != uid) {
652 goto next;
653 }
654
655 // Matches so far, but refuse to touch if in root namespace
656 LOG(DEBUG) << "Found matching PID " << de->d_name;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600657 if (!android::vold::Readlinkat(pidFd, "ns/mnt", &pidName)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700658 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
659 goto next;
660 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600661 if (rootName == pidName) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700662 LOG(WARNING) << "Skipping due to root namespace";
663 goto next;
664 }
665
Jiyong Park8d21c922019-01-04 13:35:25 +0900666 if (apexUpdatable) {
667 std::string exeName;
668 // When ro.apex.bionic_updatable is set to true,
669 // some early native processes have mount namespaces that are different
670 // from that of the init. Therefore, above check can't filter them out.
671 // Since the propagation type of / is 'shared', unmounting /storage
672 // for the early native processes affects other processes including
673 // init. Filter out such processes by skipping if a process is a
674 // non-Java process whose UID is < AID_APP_START. (The UID condition
675 // is required to not filter out child processes spawned by apps.)
676 if (!android::vold::Readlinkat(pidFd, "exe", &exeName)) {
677 PLOG(WARNING) << "Failed to read exe name for " << de->d_name;
678 goto next;
679 }
680 if (!StartsWith(exeName, "/system/bin/app_process") && sb.st_uid < AID_APP_START) {
681 LOG(WARNING) << "Skipping due to native system process";
682 goto next;
683 }
684 }
685
Jeff Sharkey66270a22015-06-24 11:49:24 -0700686 // We purposefully leave the namespace open across the fork
Nick Kraleviche7e89ac2019-03-29 16:03:51 -0700687 // NOLINTNEXTLINE(android-cloexec-open): Deliberately not O_CLOEXEC
688 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700689 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700690 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700691 goto next;
692 }
693
Narayan Kamath02efdf52019-11-27 10:53:51 +0000694 if (mode == "default") {
695 storageSource = "/mnt/runtime/default";
696 } else if (mode == "read") {
697 storageSource = "/mnt/runtime/read";
698 } else if (mode == "write") {
699 storageSource = "/mnt/runtime/write";
700 } else if (mode == "full") {
701 storageSource = "/mnt/runtime/full";
702 } else {
703 // Sane default of no storage visible. No need to fork a child
704 // to remount uid.
705 goto next;
706 }
707
708 // Mount user-specific symlink helper into place
709 userSource = StringPrintf("/mnt/user/%d", multiuser_get_user_id(uid));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700710 if (!(child = fork())) {
Narayan Kamath02efdf52019-11-27 10:53:51 +0000711 if (childProcess(storageSource.c_str(), userSource.c_str(), nsFd, de)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700712 _exit(0);
Narayan Kamath02efdf52019-11-27 10:53:51 +0000713 } else {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700714 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700715 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700716 }
717
718 if (child == -1) {
719 PLOG(ERROR) << "Failed to fork";
720 goto next;
721 } else {
722 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
723 }
724
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700725 next:
Jeff Sharkey66270a22015-06-24 11:49:24 -0700726 close(nsFd);
727 close(pidFd);
728 }
729 closedir(dir);
730 return 0;
731}
732
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700733int VolumeManager::reset() {
734 // Tear down all existing disks/volumes and start from a blank slate so
735 // newly connected framework hears all events.
Zima438b242019-09-25 14:37:38 +0100736 for (const auto& vol : mInternalEmulatedVolumes) {
737 vol->destroy();
Gao Xiangd263da82017-08-14 11:32:13 +0800738 }
Zima438b242019-09-25 14:37:38 +0100739 mInternalEmulatedVolumes.clear();
Zima438b242019-09-25 14:37:38 +0100740
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700741 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700742 disk->destroy();
743 disk->create();
744 }
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600745 updateVirtualDisk();
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700746 mAddedUsers.clear();
Sudheer Shanka023b5392019-02-06 12:39:19 -0800747 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700748 return 0;
749}
750
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700751// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700752int VolumeManager::shutdown() {
Zima438b242019-09-25 14:37:38 +0100753 if (mInternalEmulatedVolumes.empty()) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700754 return 0; // already shutdown
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700755 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700756 android::vold::sSleepOnUnmount = false;
Zima438b242019-09-25 14:37:38 +0100757 for (const auto& vol : mInternalEmulatedVolumes) {
758 vol->destroy();
759 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700760 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700761 disk->destroy();
762 }
Zima438b242019-09-25 14:37:38 +0100763
764 mInternalEmulatedVolumes.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700765 mDisks.clear();
Jeff Sharkey401b2602017-12-14 22:15:20 -0700766 mPendingDisks.clear();
Paul Crowley56292ef2017-10-20 08:07:53 -0700767 android::vold::sSleepOnUnmount = true;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700768 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700769}
770
Jeff Sharkey9c484982015-03-31 10:35:33 -0700771int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700772 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600773 ATRACE_NAME("VolumeManager::unmountAll()");
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700774
Jeff Sharkey9c484982015-03-31 10:35:33 -0700775 // First, try gracefully unmounting all known devices
Zima438b242019-09-25 14:37:38 +0100776 for (const auto& vol : mInternalEmulatedVolumes) {
777 vol->unmount();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700778 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700779 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700780 disk->unmountAll();
781 }
782
783 // Worst case we might have some stale mounts lurking around, so
784 // force unmount those just to be safe.
LongPing.WEI4f046062018-11-23 19:27:35 +0800785 FILE* fp = setmntent("/proc/mounts", "re");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700786 if (fp == NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600787 PLOG(ERROR) << "Failed to open /proc/mounts";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700788 return -errno;
789 }
790
791 // Some volumes can be stacked on each other, so force unmount in
792 // reverse order to give us the best chance of success.
793 std::list<std::string> toUnmount;
794 mntent* mentry;
795 while ((mentry = getmntent(fp)) != NULL) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600796 auto test = std::string(mentry->mnt_dir);
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700797 if ((StartsWith(test, "/mnt/") &&
798#ifdef __ANDROID_DEBUGGABLE__
799 !StartsWith(test, "/mnt/scratch") &&
800#endif
Martijn Coenenb0e977a2020-01-11 19:24:26 +0100801 !StartsWith(test, "/mnt/vendor") && !StartsWith(test, "/mnt/product") &&
802 !StartsWith(test, "/mnt/installer")) ||
Mark Salyzyn86e81e72018-09-20 10:09:27 -0700803 StartsWith(test, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600804 toUnmount.push_front(test);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700805 }
806 }
807 endmntent(fp);
808
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700809 for (const auto& path : toUnmount) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600810 LOG(DEBUG) << "Tearing down stale mount " << path;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700811 android::vold::ForceUnmount(path);
812 }
813
814 return 0;
815}
816
Martijn Coenen8a68a072020-02-12 15:29:02 +0100817int VolumeManager::setupAppDir(const std::string& path, int32_t appUid) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700818 // Only offer to create directories for paths managed by vold
Martijn Coenen13ff6682019-12-24 12:57:16 +0100819 if (!StartsWith(path, "/storage/")) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600820 LOG(ERROR) << "Failed to find mounted volume for " << path;
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700821 return -EINVAL;
822 }
Martijn Coenen13ff6682019-12-24 12:57:16 +0100823
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100824 // Find the volume it belongs to
825 auto filter_fn = [&](const VolumeBase& vol) {
826 if (vol.getState() != VolumeBase::State::kMounted) {
827 // The volume must be mounted
828 return false;
829 }
830 if ((vol.getMountFlags() & VolumeBase::MountFlags::kVisible) == 0) {
831 // and visible
832 return false;
833 }
834 if (vol.getInternalPath().empty()) {
835 return false;
836 }
837 if (vol.getMountUserId() != USER_UNKNOWN &&
838 vol.getMountUserId() != multiuser_get_user_id(appUid)) {
839 // The app dir must be created on a volume with the same user-id
840 return false;
841 }
842 if (!path.empty() && StartsWith(path, vol.getPath())) {
843 return true;
844 }
845
846 return false;
847 };
848 auto volume = findVolumeWithFilter(filter_fn);
849 if (volume == nullptr) {
850 LOG(ERROR) << "Failed to find mounted volume for " << path;
851 return -EINVAL;
852 }
Zimc59d7742020-01-06 21:48:16 +0000853 // Convert paths to lower filesystem paths to avoid making FUSE requests for these reasons:
854 // 1. A FUSE request from vold puts vold at risk of hanging if the FUSE daemon is down
855 // 2. The FUSE daemon prevents requests on /mnt/user/0/emulated/<userid != 0> and a request
856 // on /storage/emulated/10 means /mnt/user/0/emulated/10
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100857 const std::string lowerPath =
858 volume->getInternalPath() + path.substr(volume->getPath().length());
Zimc59d7742020-01-06 21:48:16 +0000859
Martijn Coenen62a4b272020-01-31 15:23:09 +0100860 const std::string volumeRoot = volume->getRootPath(); // eg /data/media/0
861
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100862 // Create the app paths we need from the root
863 return PrepareAppDirFromRoot(lowerPath, volumeRoot, appUid);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700864}
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600865
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600866int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700867 int32_t ownerGid, std::string* outVolId) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600868 int id = mNextObbId++;
869
870 auto vol = std::shared_ptr<android::vold::VolumeBase>(
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700871 new android::vold::ObbVolume(id, sourcePath, sourceKey, ownerGid));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600872 vol->create();
873
874 mObbVolumes.push_back(vol);
875 *outVolId = vol->getId();
876 return android::OK;
877}
878
879int VolumeManager::destroyObb(const std::string& volId) {
880 auto i = mObbVolumes.begin();
881 while (i != mObbVolumes.end()) {
882 if ((*i)->getId() == volId) {
883 (*i)->destroy();
884 i = mObbVolumes.erase(i);
885 } else {
886 ++i;
887 }
888 }
889 return android::OK;
890}
891
Risan8c9f3322018-10-29 08:52:56 +0900892int VolumeManager::createStubVolume(const std::string& sourcePath, const std::string& mountPath,
893 const std::string& fsType, const std::string& fsUuid,
Risan73a7a852020-02-07 18:03:44 +0900894 const std::string& fsLabel, int32_t flags,
Risan82e90de2020-02-04 16:07:21 +0900895 std::string* outVolId) {
896 dev_t stubId = --mNextStubId;
897 auto vol = std::shared_ptr<android::vold::StubVolume>(
898 new android::vold::StubVolume(stubId, sourcePath, mountPath, fsType, fsUuid, fsLabel));
Risan8c9f3322018-10-29 08:52:56 +0900899
Risan73a7a852020-02-07 18:03:44 +0900900 int32_t passedFlags = android::vold::Disk::Flags::kStub;
901 passedFlags |= (flags & android::vold::Disk::Flags::kUsb);
902 passedFlags |= (flags & android::vold::Disk::Flags::kSd);
Risan82e90de2020-02-04 16:07:21 +0900903 // StubDisk doesn't have device node corresponds to it. So, a fake device
Risan73a7a852020-02-07 18:03:44 +0900904 // number is used.
Risan82e90de2020-02-04 16:07:21 +0900905 auto disk = std::shared_ptr<android::vold::Disk>(
Risan73a7a852020-02-07 18:03:44 +0900906 new android::vold::Disk("stub", stubId, "stub", passedFlags));
Risan82e90de2020-02-04 16:07:21 +0900907 disk->initializePartition(vol);
908 handleDiskAdded(disk);
Risan8c9f3322018-10-29 08:52:56 +0900909 *outVolId = vol->getId();
910 return android::OK;
911}
912
913int VolumeManager::destroyStubVolume(const std::string& volId) {
Risan82e90de2020-02-04 16:07:21 +0900914 auto tokens = android::base::Split(volId, ":");
915 CHECK(tokens.size() == 2);
916 dev_t stubId;
917 CHECK(android::base::ParseUint(tokens[1], &stubId));
918 handleDiskRemoved(stubId);
Risan8c9f3322018-10-29 08:52:56 +0900919 return android::OK;
920}
921
Risan8f6198d2018-10-26 20:56:45 -0600922int VolumeManager::mountAppFuse(uid_t uid, int mountId, unique_fd* device_fd) {
Risanac02a482018-10-31 21:59:47 -0600923 return android::vold::MountAppFuse(uid, mountId, device_fd);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600924}
925
Risan8f6198d2018-10-26 20:56:45 -0600926int VolumeManager::unmountAppFuse(uid_t uid, int mountId) {
Risanac02a482018-10-31 21:59:47 -0600927 return android::vold::UnmountAppFuse(uid, mountId);
Risan8f6198d2018-10-26 20:56:45 -0600928}
929
930int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) {
Risanac02a482018-10-31 21:59:47 -0600931 return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600932}