blob: 6a786a809005a9d18ea57b1eb7126bd79b643ba3 [file] [log] [blame]
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001/*
2 * Copyright (C) 2017 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
Jeff Sharkey068c6be2017-09-06 13:47:40 -060019#include "VoldNativeService.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080020
Songchun Fan01c4e912020-03-03 17:58:20 -080021#include <android-base/logging.h>
Songchun Fan01c4e912020-03-03 17:58:20 -080022#include <android-base/strings.h>
23#include <fs_mgr.h>
24#include <fscrypt/fscrypt.h>
25#include <private/android_filesystem_config.h>
26#include <utils/Trace.h>
27
Keun young Park0bccae22021-10-13 16:50:10 -070028#include <stdio.h>
Eric Biggers4859e0c2021-06-24 11:13:24 -070029#include <sys/vfs.h>
Songchun Fan01c4e912020-03-03 17:58:20 -080030#include <fstream>
31#include <thread>
32
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060033#include "Benchmark.h"
Daniel Rosenberg4f684712018-08-28 01:58:49 -070034#include "Checkpoint.h"
Eric Biggersa701c452018-10-23 13:06:55 -070035#include "FsCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080036#include "IdleMaint.h"
Seth Moore5a43d612021-01-19 17:51:51 +000037#include "KeyStorage.h"
Eric Biggersd86a8ab2021-06-15 11:34:00 -070038#include "Keystore.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060039#include "MetadataCrypt.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080040#include "MoveStorage.h"
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070041#include "VoldNativeServiceValidation.h"
Paul Crowley73be12d2020-02-03 12:22:03 -080042#include "VoldUtil.h"
43#include "VolumeManager.h"
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070044#include "cryptfs.h"
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -070045#include "incfs.h"
Jeff Sharkey83b559c2017-09-12 16:30:52 -060046
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070047using namespace std::literals;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060048
49namespace android {
50namespace vold {
51
52namespace {
53
54constexpr const char* kDump = "android.permission.DUMP";
Alex Buynytskyydcfeaa12021-04-01 13:43:14 -070055constexpr auto kIncFsReadNoTimeoutMs = 100;
Jeff Sharkey068c6be2017-09-06 13:47:40 -060056
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060057static binder::Status error(const std::string& msg) {
58 PLOG(ERROR) << msg;
59 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
60}
61
Jeff Sharkey83b559c2017-09-12 16:30:52 -060062static binder::Status translate(int status) {
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060063 if (status == 0) {
64 return binder::Status::ok();
65 } else {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060066 return binder::Status::fromServiceSpecificError(status);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -060067 }
68}
69
Jeff Sharkey83b559c2017-09-12 16:30:52 -060070static binder::Status translateBool(bool status) {
71 if (status) {
72 return binder::Status::ok();
73 } else {
74 return binder::Status::fromServiceSpecificError(status);
75 }
76}
77
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010078#define ENFORCE_SYSTEM_OR_ROOT \
79 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070080 binder::Status status = CheckUidOrRoot(AID_SYSTEM); \
Nikita Ioffe5d85bf32019-10-23 17:19:13 +010081 if (!status.isOk()) { \
82 return status; \
83 } \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070084 }
Jeff Sharkey068c6be2017-09-06 13:47:40 -060085
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070086#define CHECK_ARGUMENT_ID(id) \
87 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070088 binder::Status status = CheckArgumentId((id)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070089 if (!status.isOk()) { \
90 return status; \
91 } \
92 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -060093
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070094#define CHECK_ARGUMENT_PATH(path) \
95 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -070096 binder::Status status = CheckArgumentPath((path)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070097 if (!status.isOk()) { \
98 return status; \
99 } \
100 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600101
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700102#define CHECK_ARGUMENT_HEX(hex) \
103 { \
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700104 binder::Status status = CheckArgumentHex((hex)); \
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700105 if (!status.isOk()) { \
106 return status; \
107 } \
108 }
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600109
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700110#define ACQUIRE_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600111 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); \
112 ATRACE_CALL();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600113
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700114#define ACQUIRE_CRYPT_LOCK \
Jeff Sharkey67b8c492017-09-21 17:08:43 -0600115 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getCryptLock()); \
116 ATRACE_CALL();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600117
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600118} // namespace
119
120status_t VoldNativeService::start() {
121 IPCThreadState::self()->disableBackgroundScheduling(true);
122 status_t ret = BinderService<VoldNativeService>::publish();
123 if (ret != android::OK) {
124 return ret;
125 }
126 sp<ProcessState> ps(ProcessState::self());
127 ps->startThreadPool();
128 ps->giveThreadPoolName();
129 return android::OK;
130}
131
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700132status_t VoldNativeService::dump(int fd, const Vector<String16>& /* args */) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700133 const binder::Status dump_permission = CheckPermission(kDump);
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600134 if (!dump_permission.isOk()) {
Keun young Park0bccae22021-10-13 16:50:10 -0700135 dprintf(fd, "%s\n", dump_permission.toString8().c_str());
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600136 return PERMISSION_DENIED;
137 }
138
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600139 ACQUIRE_LOCK;
Keun young Park0bccae22021-10-13 16:50:10 -0700140 dprintf(fd, "vold is happy!\n");
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600141 return NO_ERROR;
142}
143
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600144binder::Status VoldNativeService::setListener(
Songchun Fan01c4e912020-03-03 17:58:20 -0800145 const android::sp<android::os::IVoldListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100146 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600147 ACQUIRE_LOCK;
148
149 VolumeManager::Instance()->setListener(listener);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700150 return Ok();
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600151}
152
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600153binder::Status VoldNativeService::monitor() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100154 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600155
156 // Simply acquire/release each lock for watchdog
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700157 { ACQUIRE_LOCK; }
158 { ACQUIRE_CRYPT_LOCK; }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600159
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700160 return Ok();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600161}
162
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600163binder::Status VoldNativeService::reset() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100164 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600165 ACQUIRE_LOCK;
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600166
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600167 return translate(VolumeManager::Instance()->reset());
168}
169
170binder::Status VoldNativeService::shutdown() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100171 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600172 ACQUIRE_LOCK;
173
174 return translate(VolumeManager::Instance()->shutdown());
175}
176
Martijn Coenen23c04452020-04-29 07:49:41 +0200177binder::Status VoldNativeService::abortFuse() {
178 ENFORCE_SYSTEM_OR_ROOT;
lijiaziffe76222021-03-17 16:51:16 +0800179 // if acquire lock, maybe lead to a deadlock if lock is held by a
180 // thread that is blocked on a FUSE operation.
181 // abort fuse doesn't need to access any state, so do not acquire lock
Martijn Coenen23c04452020-04-29 07:49:41 +0200182
183 return translate(VolumeManager::Instance()->abortFuse());
184}
185
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600186binder::Status VoldNativeService::onUserAdded(int32_t userId, int32_t userSerial) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100187 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600188 ACQUIRE_LOCK;
189
190 return translate(VolumeManager::Instance()->onUserAdded(userId, userSerial));
191}
192
193binder::Status VoldNativeService::onUserRemoved(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100194 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600195 ACQUIRE_LOCK;
196
197 return translate(VolumeManager::Instance()->onUserRemoved(userId));
198}
199
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700200binder::Status VoldNativeService::onUserStarted(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100201 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600202 ACQUIRE_LOCK;
203
Sudheer Shanka5fceb482019-04-29 10:46:35 -0700204 return translate(VolumeManager::Instance()->onUserStarted(userId));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600205}
206
207binder::Status VoldNativeService::onUserStopped(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100208 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600209 ACQUIRE_LOCK;
210
211 return translate(VolumeManager::Instance()->onUserStopped(userId));
212}
213
Sudheer Shankad484aa92018-07-31 10:07:34 -0700214binder::Status VoldNativeService::addAppIds(const std::vector<std::string>& packageNames,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700215 const std::vector<int32_t>& appIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700216 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700217}
218
Sudheer Shankad484aa92018-07-31 10:07:34 -0700219binder::Status VoldNativeService::addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700220 const std::vector<std::string>& sandboxIds) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700221 return Ok();
Sudheer Shankad484aa92018-07-31 10:07:34 -0700222}
223
Jeff Sharkey401b2602017-12-14 22:15:20 -0700224binder::Status VoldNativeService::onSecureKeyguardStateChanged(bool isShowing) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100225 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700226 ACQUIRE_LOCK;
227
228 return translate(VolumeManager::Instance()->onSecureKeyguardStateChanged(isShowing));
229}
230
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600231binder::Status VoldNativeService::partition(const std::string& diskId, int32_t partitionType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700232 int32_t ratio) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100233 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600234 CHECK_ARGUMENT_ID(diskId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600235 ACQUIRE_LOCK;
236
237 auto disk = VolumeManager::Instance()->findDisk(diskId);
238 if (disk == nullptr) {
239 return error("Failed to find disk " + diskId);
240 }
241 switch (partitionType) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700242 case PARTITION_TYPE_PUBLIC:
243 return translate(disk->partitionPublic());
244 case PARTITION_TYPE_PRIVATE:
245 return translate(disk->partitionPrivate());
246 case PARTITION_TYPE_MIXED:
247 return translate(disk->partitionMixed(ratio));
248 default:
249 return error("Unknown type " + std::to_string(partitionType));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600250 }
251}
252
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600253binder::Status VoldNativeService::forgetPartition(const std::string& partGuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700254 const std::string& fsUuid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100255 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600256 CHECK_ARGUMENT_HEX(partGuid);
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600257 CHECK_ARGUMENT_HEX(fsUuid);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600258 ACQUIRE_LOCK;
259
Jeff Sharkey3ce18252017-10-24 11:08:45 -0600260 return translate(VolumeManager::Instance()->forgetPartition(partGuid, fsUuid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600261}
262
Zim5048b4b2019-11-19 09:16:03 +0000263binder::Status VoldNativeService::mount(
264 const std::string& volId, int32_t mountFlags, int32_t mountUserId,
265 const android::sp<android::os::IVoldMountCallback>& callback) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100266 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600267 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600268 ACQUIRE_LOCK;
269
270 auto vol = VolumeManager::Instance()->findVolume(volId);
271 if (vol == nullptr) {
272 return error("Failed to find volume " + volId);
273 }
274
275 vol->setMountFlags(mountFlags);
276 vol->setMountUserId(mountUserId);
277
Zim5048b4b2019-11-19 09:16:03 +0000278 vol->setMountCallback(callback);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600279 int res = vol->mount();
Zim5048b4b2019-11-19 09:16:03 +0000280 vol->setMountCallback(nullptr);
281
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700282 if (res != OK) {
283 return translate(res);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600284 }
Zim3623a212019-07-19 16:46:53 +0100285
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700286 return translate(OK);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600287}
288
289binder::Status VoldNativeService::unmount(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100290 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600291 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600292 ACQUIRE_LOCK;
293
294 auto vol = VolumeManager::Instance()->findVolume(volId);
295 if (vol == nullptr) {
296 return error("Failed to find volume " + volId);
297 }
298 return translate(vol->unmount());
299}
300
301binder::Status VoldNativeService::format(const std::string& volId, const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100302 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600303 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600304 ACQUIRE_LOCK;
305
306 auto vol = VolumeManager::Instance()->findVolume(volId);
307 if (vol == nullptr) {
308 return error("Failed to find volume " + volId);
309 }
310 return translate(vol->format(fsType));
311}
312
Jeff Sharkey2048a282017-06-15 09:59:43 -0600313static binder::Status pathForVolId(const std::string& volId, std::string* path) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600314 if (volId == "private" || volId == "null") {
Jeff Sharkey2048a282017-06-15 09:59:43 -0600315 *path = "/data";
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600316 } else {
317 auto vol = VolumeManager::Instance()->findVolume(volId);
318 if (vol == nullptr) {
319 return error("Failed to find volume " + volId);
320 }
321 if (vol->getType() != VolumeBase::Type::kPrivate) {
322 return error("Volume " + volId + " not private");
323 }
324 if (vol->getState() != VolumeBase::State::kMounted) {
325 return error("Volume " + volId + " not mounted");
326 }
Jeff Sharkey2048a282017-06-15 09:59:43 -0600327 *path = vol->getPath();
328 if (path->empty()) {
329 return error("Volume " + volId + " missing path");
330 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600331 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700332 return Ok();
Jeff Sharkey2048a282017-06-15 09:59:43 -0600333}
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600334
Jeff Sharkey2048a282017-06-15 09:59:43 -0600335binder::Status VoldNativeService::benchmark(
Songchun Fan01c4e912020-03-03 17:58:20 -0800336 const std::string& volId, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100337 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey2048a282017-06-15 09:59:43 -0600338 CHECK_ARGUMENT_ID(volId);
339 ACQUIRE_LOCK;
340
341 std::string path;
342 auto status = pathForVolId(volId, &path);
343 if (!status.isOk()) return status;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600344
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700345 std::thread([=]() { android::vold::Benchmark(path, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700346 return Ok();
Jeff Sharkey068c6be2017-09-06 13:47:40 -0600347}
348
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700349binder::Status VoldNativeService::moveStorage(
Songchun Fan01c4e912020-03-03 17:58:20 -0800350 const std::string& fromVolId, const std::string& toVolId,
351 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100352 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600353 CHECK_ARGUMENT_ID(fromVolId);
354 CHECK_ARGUMENT_ID(toVolId);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600355 ACQUIRE_LOCK;
356
357 auto fromVol = VolumeManager::Instance()->findVolume(fromVolId);
358 auto toVol = VolumeManager::Instance()->findVolume(toVolId);
359 if (fromVol == nullptr) {
360 return error("Failed to find volume " + fromVolId);
361 } else if (toVol == nullptr) {
362 return error("Failed to find volume " + toVolId);
363 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600364
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700365 std::thread([=]() { android::vold::MoveStorage(fromVol, toVol, listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700366 return Ok();
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600367}
368
369binder::Status VoldNativeService::remountUid(int32_t uid, int32_t remountMode) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100370 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600371 ACQUIRE_LOCK;
372
Sudheer Shanka817b9112018-12-13 17:40:28 -0800373 return translate(VolumeManager::Instance()->remountUid(uid, remountMode));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600374}
375
Ricky Wai6b122572020-02-28 16:30:47 +0000376binder::Status VoldNativeService::remountAppStorageDirs(int uid, int pid,
377 const std::vector<std::string>& packageNames) {
378 ENFORCE_SYSTEM_OR_ROOT;
379 ACQUIRE_LOCK;
380
Ricky Waia2ca11e2021-01-15 14:03:23 +0000381 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
382 false /* doUnmount */, packageNames));
383}
384
385binder::Status VoldNativeService::unmountAppStorageDirs(int uid, int pid,
386 const std::vector<std::string>& packageNames) {
387 ENFORCE_SYSTEM_OR_ROOT;
388 ACQUIRE_LOCK;
389
390 return translate(VolumeManager::Instance()->handleAppStorageDirs(uid, pid,
391 true /* doUnmount */, packageNames));
Ricky Wai6b122572020-02-28 16:30:47 +0000392}
393
Martijn Coenen8a68a072020-02-12 15:29:02 +0100394binder::Status VoldNativeService::setupAppDir(const std::string& path, int32_t appUid) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100395 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600396 CHECK_ARGUMENT_PATH(path);
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600397 ACQUIRE_LOCK;
398
Martijn Coenen8a68a072020-02-12 15:29:02 +0100399 return translate(VolumeManager::Instance()->setupAppDir(path, appUid));
Jeff Sharkey9462bdd2017-09-07 15:27:28 -0600400}
401
Ricky Waibbfb6ea2020-12-03 15:32:52 +0000402binder::Status VoldNativeService::ensureAppDirsCreated(const std::vector<std::string>& paths,
403 int32_t appUid) {
404 ENFORCE_SYSTEM_OR_ROOT;
405 ACQUIRE_LOCK;
406
407 return translate(VolumeManager::Instance()->ensureAppDirsCreated(paths, appUid));
408}
409
Martijn Coenen816f4d92020-02-18 15:06:37 +0100410binder::Status VoldNativeService::fixupAppDir(const std::string& path, int32_t appUid) {
411 ENFORCE_SYSTEM_OR_ROOT;
412 CHECK_ARGUMENT_PATH(path);
413 ACQUIRE_LOCK;
414
415 return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
416}
417
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600418binder::Status VoldNativeService::createObb(const std::string& sourcePath,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700419 const std::string& sourceKey, int32_t ownerGid,
420 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100421 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600422 CHECK_ARGUMENT_PATH(sourcePath);
423 CHECK_ARGUMENT_HEX(sourceKey);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600424 ACQUIRE_LOCK;
425
426 return translate(
Songchun Fan01c4e912020-03-03 17:58:20 -0800427 VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600428}
429
430binder::Status VoldNativeService::destroyObb(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100431 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkeyec4fda22017-09-12 13:19:24 -0600432 CHECK_ARGUMENT_ID(volId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600433 ACQUIRE_LOCK;
434
435 return translate(VolumeManager::Instance()->destroyObb(volId));
436}
437
Risan82e90de2020-02-04 16:07:21 +0900438binder::Status VoldNativeService::createStubVolume(const std::string& sourcePath,
439 const std::string& mountPath,
440 const std::string& fsType,
441 const std::string& fsUuid,
442 const std::string& fsLabel, int32_t flags,
443 std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100444 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900445 CHECK_ARGUMENT_PATH(sourcePath);
446 CHECK_ARGUMENT_PATH(mountPath);
447 CHECK_ARGUMENT_HEX(fsUuid);
448 // Label limitation seems to be different between fs (including allowed characters), so checking
449 // is quite meaningless.
450 ACQUIRE_LOCK;
451
Risan82e90de2020-02-04 16:07:21 +0900452 return translate(VolumeManager::Instance()->createStubVolume(
453 sourcePath, mountPath, fsType, fsUuid, fsLabel, flags, _aidl_return));
Risan8c9f3322018-10-29 08:52:56 +0900454}
455
456binder::Status VoldNativeService::destroyStubVolume(const std::string& volId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100457 ENFORCE_SYSTEM_OR_ROOT;
Risan8c9f3322018-10-29 08:52:56 +0900458 CHECK_ARGUMENT_ID(volId);
459 ACQUIRE_LOCK;
460
461 return translate(VolumeManager::Instance()->destroyStubVolume(volId));
462}
463
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700464binder::Status VoldNativeService::fstrim(
Songchun Fan01c4e912020-03-03 17:58:20 -0800465 int32_t fstrimFlags, const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100466 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600467 ACQUIRE_LOCK;
468
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700469 std::thread([=]() { android::vold::Trim(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700470 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600471}
472
Jin Qiana370c142017-10-17 15:41:45 -0700473binder::Status VoldNativeService::runIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800474 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100475 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700476 ACQUIRE_LOCK;
477
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700478 std::thread([=]() { android::vold::RunIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700479 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700480}
481
482binder::Status VoldNativeService::abortIdleMaint(
Songchun Fan01c4e912020-03-03 17:58:20 -0800483 const android::sp<android::os::IVoldTaskListener>& listener) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100484 ENFORCE_SYSTEM_OR_ROOT;
Jin Qiana370c142017-10-17 15:41:45 -0700485 ACQUIRE_LOCK;
486
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700487 std::thread([=]() { android::vold::AbortIdleMaint(listener); }).detach();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700488 return Ok();
Jin Qiana370c142017-10-17 15:41:45 -0700489}
490
Risan8f6198d2018-10-26 20:56:45 -0600491binder::Status VoldNativeService::mountAppFuse(int32_t uid, int32_t mountId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700492 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100493 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600494 ACQUIRE_LOCK;
495
Risan8f6198d2018-10-26 20:56:45 -0600496 return translate(VolumeManager::Instance()->mountAppFuse(uid, mountId, _aidl_return));
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600497}
498
Risan8f6198d2018-10-26 20:56:45 -0600499binder::Status VoldNativeService::unmountAppFuse(int32_t uid, int32_t mountId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100500 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600501 ACQUIRE_LOCK;
502
Risan8f6198d2018-10-26 20:56:45 -0600503 return translate(VolumeManager::Instance()->unmountAppFuse(uid, mountId));
504}
505
506binder::Status VoldNativeService::openAppFuseFile(int32_t uid, int32_t mountId, int32_t fileId,
507 int32_t flags,
508 android::base::unique_fd* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100509 ENFORCE_SYSTEM_OR_ROOT;
Risan8f6198d2018-10-26 20:56:45 -0600510 ACQUIRE_LOCK;
511
512 int fd = VolumeManager::Instance()->openAppFuseFile(uid, mountId, fileId, flags);
513 if (fd == -1) {
514 return error("Failed to open AppFuse file for uid: " + std::to_string(uid) +
515 " mountId: " + std::to_string(mountId) + " fileId: " + std::to_string(fileId) +
516 " flags: " + std::to_string(flags));
517 }
518
519 *_aidl_return = android::base::unique_fd(fd);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700520 return Ok();
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600521}
522
Eric Biggers0803ba02021-11-08 16:45:58 -0800523// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600524binder::Status VoldNativeService::fdeCheckPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100525 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600526 ACQUIRE_CRYPT_LOCK;
527
Eric Biggers0803ba02021-11-08 16:45:58 -0800528 SLOGE("fdeCheckPassword is no longer supported");
529 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600530}
531
Eric Biggers0803ba02021-11-08 16:45:58 -0800532// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600533binder::Status VoldNativeService::fdeRestart() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100534 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600535 ACQUIRE_CRYPT_LOCK;
536
Eric Biggers0803ba02021-11-08 16:45:58 -0800537 SLOGE("fdeRestart is no longer supported");
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700538 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600539}
540
Eric Biggers0803ba02021-11-08 16:45:58 -0800541// TODO(b/191796797) remove this once caller is removed
542#define CRYPTO_COMPLETE_NOT_ENCRYPTED 1
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600543binder::Status VoldNativeService::fdeComplete(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100544 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600545 ACQUIRE_CRYPT_LOCK;
546
Eric Biggers0803ba02021-11-08 16:45:58 -0800547 SLOGE("fdeComplete is no longer supported");
548 *_aidl_return = CRYPTO_COMPLETE_NOT_ENCRYPTED;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700549 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600550}
551
Eric Biggers0803ba02021-11-08 16:45:58 -0800552// TODO(b/191796797) remove this once caller is removed
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700553binder::Status VoldNativeService::fdeEnable(int32_t passwordType, const std::string& password,
554 int32_t encryptionFlags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100555 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600556 ACQUIRE_CRYPT_LOCK;
557
Eric Biggers0803ba02021-11-08 16:45:58 -0800558 SLOGE("fdeEnable is no longer supported");
559 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600560}
561
Eric Biggers0803ba02021-11-08 16:45:58 -0800562// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600563binder::Status VoldNativeService::fdeChangePassword(int32_t passwordType,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700564 const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100565 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600566 ACQUIRE_CRYPT_LOCK;
567
Eric Biggers0803ba02021-11-08 16:45:58 -0800568 SLOGE("fdeChangePassword is no longer supported");
569 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600570}
571
Eric Biggers0803ba02021-11-08 16:45:58 -0800572// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600573binder::Status VoldNativeService::fdeVerifyPassword(const std::string& password) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100574 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600575 ACQUIRE_CRYPT_LOCK;
576
Eric Biggers0803ba02021-11-08 16:45:58 -0800577 SLOGE("fdeVerifyPassword is no longer supported");
578 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600579}
580
Eric Biggers0803ba02021-11-08 16:45:58 -0800581// TODO(b/191796797) remove this once caller is removed
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700582binder::Status VoldNativeService::fdeGetField(const std::string& key, std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100583 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600584 ACQUIRE_CRYPT_LOCK;
585
Eric Biggers0803ba02021-11-08 16:45:58 -0800586 SLOGE("fdeGetField is no longer supported");
587 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600588}
589
Eric Biggers0803ba02021-11-08 16:45:58 -0800590// TODO(b/191796797) remove this once caller is removed
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700591binder::Status VoldNativeService::fdeSetField(const std::string& key, const std::string& value) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100592 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600593 ACQUIRE_CRYPT_LOCK;
594
Eric Biggers0803ba02021-11-08 16:45:58 -0800595 SLOGE("fdeSetField is no longer supported");
596 return translate(-1);
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600597}
598
Eric Biggers0803ba02021-11-08 16:45:58 -0800599// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600600binder::Status VoldNativeService::fdeGetPasswordType(int32_t* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100601 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600602 ACQUIRE_CRYPT_LOCK;
603
Eric Biggers0803ba02021-11-08 16:45:58 -0800604 SLOGE("fdeGetPasswordType is no longer supported");
605 *_aidl_return = -1;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700606 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600607}
608
Eric Biggers0803ba02021-11-08 16:45:58 -0800609// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600610binder::Status VoldNativeService::fdeGetPassword(std::string* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100611 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600612 ACQUIRE_CRYPT_LOCK;
613
Eric Biggers0803ba02021-11-08 16:45:58 -0800614 SLOGE("fdeGetPassword is no longer supported");
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700615 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600616}
617
Eric Biggers0803ba02021-11-08 16:45:58 -0800618// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600619binder::Status VoldNativeService::fdeClearPassword() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100620 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600621 ACQUIRE_CRYPT_LOCK;
622
Eric Biggers0803ba02021-11-08 16:45:58 -0800623 SLOGE("fdeClearPassword is no longer supported");
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700624 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600625}
626
627binder::Status VoldNativeService::fbeEnable() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100628 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600629 ACQUIRE_CRYPT_LOCK;
630
Paul Crowleyc8a3ef32019-09-11 15:00:08 -0700631 return translateBool(fscrypt_initialize_systemwide_keys());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600632}
633
Eric Biggers0803ba02021-11-08 16:45:58 -0800634// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600635binder::Status VoldNativeService::mountDefaultEncrypted() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100636 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600637 ACQUIRE_CRYPT_LOCK;
638
Eric Biggers0803ba02021-11-08 16:45:58 -0800639 SLOGE("mountDefaultEncrypted is no longer supported");
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700640 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600641}
642
643binder::Status VoldNativeService::initUser0() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100644 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600645 ACQUIRE_CRYPT_LOCK;
646
Eric Biggersa701c452018-10-23 13:06:55 -0700647 return translateBool(fscrypt_init_user0());
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600648}
649
Eric Biggers0803ba02021-11-08 16:45:58 -0800650// TODO(b/191796797) remove this once caller is removed
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600651binder::Status VoldNativeService::isConvertibleToFbe(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100652 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600653 ACQUIRE_CRYPT_LOCK;
654
Eric Biggers0803ba02021-11-08 16:45:58 -0800655 SLOGE("isConvertibleToFbe is no longer supported");
656 *_aidl_return = false;
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700657 return Ok();
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600658}
659
Paul Lawrence236e5e82019-06-25 14:44:33 -0700660binder::Status VoldNativeService::mountFstab(const std::string& blkDevice,
661 const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100662 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800663 ACQUIRE_LOCK;
664
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800665 return translateBool(
666 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, false, false, "null"));
Paul Crowley0fd26262018-01-30 09:48:19 -0800667}
668
Paul Lawrence236e5e82019-06-25 14:44:33 -0700669binder::Status VoldNativeService::encryptFstab(const std::string& blkDevice,
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800670 const std::string& mountPoint, bool shouldFormat,
671 const std::string& fsType) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100672 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley0fd26262018-01-30 09:48:19 -0800673 ACQUIRE_LOCK;
674
Jaegeuk Kim0c52c712020-12-15 09:00:49 -0800675 return translateBool(
676 fscrypt_mount_metadata_encrypted(blkDevice, mountPoint, true, shouldFormat, fsType));
Paul Crowley0fd26262018-01-30 09:48:19 -0800677}
678
Seth Moore5a43d612021-01-19 17:51:51 +0000679binder::Status VoldNativeService::setStorageBindingSeed(const std::vector<uint8_t>& seed) {
680 ENFORCE_SYSTEM_OR_ROOT;
681 ACQUIRE_CRYPT_LOCK;
682
683 return translateBool(setKeyStorageBindingSeed(seed));
684}
685
Songchun Fan01c4e912020-03-03 17:58:20 -0800686binder::Status VoldNativeService::createUserKey(int32_t userId, int32_t userSerial,
687 bool ephemeral) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100688 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600689 ACQUIRE_CRYPT_LOCK;
690
Eric Biggersa701c452018-10-23 13:06:55 -0700691 return translateBool(fscrypt_vold_create_user_key(userId, userSerial, ephemeral));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600692}
693
694binder::Status VoldNativeService::destroyUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100695 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600696 ACQUIRE_CRYPT_LOCK;
697
Eric Biggersa701c452018-10-23 13:06:55 -0700698 return translateBool(fscrypt_destroy_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600699}
700
Satya Tangiralae1361712021-03-15 15:33:08 -0700701static bool token_empty(const std::string& token) {
702 return token.size() == 0 || token == "!";
703}
704
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600705binder::Status VoldNativeService::addUserKeyAuth(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700706 const std::string& token,
707 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100708 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600709 ACQUIRE_CRYPT_LOCK;
710
Satya Tangiralae1361712021-03-15 15:33:08 -0700711 if (!token_empty(token)) {
712 LOG(ERROR) << "Vold doesn't use auth tokens, but non-empty token passed to addUserKeyAuth.";
713 return binder::Status::fromServiceSpecificError(-EINVAL);
714 }
715
716 return translateBool(fscrypt_add_user_key_auth(userId, userSerial, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600717}
718
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700719binder::Status VoldNativeService::clearUserKeyAuth(int32_t userId, int32_t userSerial,
720 const std::string& token,
721 const std::string& secret) {
722 ENFORCE_SYSTEM_OR_ROOT;
723 ACQUIRE_CRYPT_LOCK;
724
Satya Tangiralae1361712021-03-15 15:33:08 -0700725 if (!token_empty(token)) {
726 LOG(ERROR)
727 << "Vold doesn't use auth tokens, but non-empty token passed to clearUserKeyAuth.";
728 return binder::Status::fromServiceSpecificError(-EINVAL);
729 }
730
731 return translateBool(fscrypt_clear_user_key_auth(userId, userSerial, secret));
Barani Muthukumaran9ad51ad2019-10-31 22:59:34 -0700732}
733
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600734binder::Status VoldNativeService::fixateNewestUserKeyAuth(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100735 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600736 ACQUIRE_CRYPT_LOCK;
737
Eric Biggersa701c452018-10-23 13:06:55 -0700738 return translateBool(fscrypt_fixate_newest_user_key_auth(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600739}
740
Eric Biggers1799deb2021-04-06 12:02:56 -0700741binder::Status VoldNativeService::getUnlockedUsers(std::vector<int>* _aidl_return) {
742 ENFORCE_SYSTEM_OR_ROOT;
743 ACQUIRE_CRYPT_LOCK;
744
745 *_aidl_return = fscrypt_get_unlocked_users();
746 return Ok();
747}
748
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600749binder::Status VoldNativeService::unlockUserKey(int32_t userId, int32_t userSerial,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700750 const std::string& token,
751 const std::string& secret) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100752 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600753 ACQUIRE_CRYPT_LOCK;
754
Satya Tangiralae1361712021-03-15 15:33:08 -0700755 if (!token_empty(token)) {
756 LOG(ERROR) << "Vold doesn't use auth tokens, but non-empty token passed to unlockUserKey.";
757 return binder::Status::fromServiceSpecificError(-EINVAL);
758 }
759
760 return translateBool(fscrypt_unlock_user_key(userId, userSerial, secret));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600761}
762
763binder::Status VoldNativeService::lockUserKey(int32_t userId) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100764 ENFORCE_SYSTEM_OR_ROOT;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600765 ACQUIRE_CRYPT_LOCK;
766
Eric Biggersa701c452018-10-23 13:06:55 -0700767 return translateBool(fscrypt_lock_user_key(userId));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600768}
769
Jooyung Han4fcb7072020-01-23 13:23:26 +0900770binder::Status VoldNativeService::prepareUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700771 int32_t userId, int32_t userSerial,
772 int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100773 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700774 std::string empty_string = "";
775 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700776 CHECK_ARGUMENT_HEX(uuid_);
777
778 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700779 return translateBool(fscrypt_prepare_user_storage(uuid_, userId, userSerial, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600780}
781
Jooyung Han4fcb7072020-01-23 13:23:26 +0900782binder::Status VoldNativeService::destroyUserStorage(const std::optional<std::string>& uuid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700783 int32_t userId, int32_t flags) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100784 ENFORCE_SYSTEM_OR_ROOT;
Paul Crowley3b71fc52017-10-09 10:55:21 -0700785 std::string empty_string = "";
786 auto uuid_ = uuid ? *uuid : empty_string;
Paul Crowley06f762d2017-10-16 10:59:51 -0700787 CHECK_ARGUMENT_HEX(uuid_);
788
789 ACQUIRE_CRYPT_LOCK;
Eric Biggersa701c452018-10-23 13:06:55 -0700790 return translateBool(fscrypt_destroy_user_storage(uuid_, userId, flags));
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600791}
792
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700793binder::Status VoldNativeService::prepareSandboxForApp(const std::string& packageName,
794 int32_t appId, const std::string& sandboxId,
795 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700796 return Ok();
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700797}
798
799binder::Status VoldNativeService::destroySandboxForApp(const std::string& packageName,
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700800 const std::string& sandboxId,
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700801 int32_t userId) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700802 return Ok();
Sudheer Shankac7562092018-08-24 10:20:56 -0700803}
804
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700805binder::Status VoldNativeService::startCheckpoint(int32_t retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100806 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700807 ACQUIRE_LOCK;
808
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700809 return cp_startCheckpoint(retry);
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700810}
811
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700812binder::Status VoldNativeService::needsRollback(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100813 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700814 ACQUIRE_LOCK;
815
816 *_aidl_return = cp_needsRollback();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700817 return Ok();
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700818}
819
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700820binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100821 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700822 ACQUIRE_LOCK;
823
824 *_aidl_return = cp_needsCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700825 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700826}
827
David Anderson23850d32020-06-10 23:51:17 -0700828binder::Status VoldNativeService::isCheckpointing(bool* _aidl_return) {
829 ENFORCE_SYSTEM_OR_ROOT;
830 ACQUIRE_LOCK;
831
832 *_aidl_return = cp_isCheckpointing();
833 return Ok();
834}
835
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700836binder::Status VoldNativeService::commitChanges() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100837 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700838 ACQUIRE_LOCK;
839
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700840 return cp_commitChanges();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700841}
842
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700843binder::Status VoldNativeService::prepareCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100844 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700845 ACQUIRE_LOCK;
846
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700847 return cp_prepareCheckpoint();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700848}
849
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700850binder::Status VoldNativeService::restoreCheckpoint(const std::string& mountPoint) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100851 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700852 CHECK_ARGUMENT_PATH(mountPoint);
853 ACQUIRE_LOCK;
854
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700855 return cp_restoreCheckpoint(mountPoint);
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700856}
857
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800858binder::Status VoldNativeService::restoreCheckpointPart(const std::string& mountPoint, int count) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100859 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800860 CHECK_ARGUMENT_PATH(mountPoint);
861 ACQUIRE_LOCK;
862
863 return cp_restoreCheckpoint(mountPoint, count);
864}
865
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700866binder::Status VoldNativeService::markBootAttempt() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100867 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700868 ACQUIRE_LOCK;
869
Daniel Rosenberge334aab2018-10-10 18:52:04 -0700870 return cp_markBootAttempt();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700871}
872
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700873binder::Status VoldNativeService::abortChanges(const std::string& message, bool retry) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100874 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700875 ACQUIRE_LOCK;
876
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700877 cp_abortChanges(message, retry);
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700878 return Ok();
Daniel Rosenberg4f684712018-08-28 01:58:49 -0700879}
880
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800881binder::Status VoldNativeService::supportsCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100882 ENFORCE_SYSTEM_OR_ROOT;
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800883 ACQUIRE_LOCK;
884
885 return cp_supportsCheckpoint(*_aidl_return);
886}
887
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700888binder::Status VoldNativeService::supportsBlockCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100889 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700890 ACQUIRE_LOCK;
891
892 return cp_supportsBlockCheckpoint(*_aidl_return);
893}
894
895binder::Status VoldNativeService::supportsFileCheckpoint(bool* _aidl_return) {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100896 ENFORCE_SYSTEM_OR_ROOT;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700897 ACQUIRE_LOCK;
898
899 return cp_supportsFileCheckpoint(*_aidl_return);
900}
901
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100902binder::Status VoldNativeService::resetCheckpoint() {
Nikita Ioffe5d85bf32019-10-23 17:19:13 +0100903 ENFORCE_SYSTEM_OR_ROOT;
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100904 ACQUIRE_LOCK;
905
906 cp_resetCheckpoint();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700907 return Ok();
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100908}
909
Alex Buynytskyyb02e7b62021-02-11 19:09:10 -0800910static void initializeIncFs() {
911 // Obtaining IncFS features triggers initialization of IncFS.
912 incfs::features();
913}
914
Eric Biggers4859e0c2021-06-24 11:13:24 -0700915// This is missing from the kernel UAPI headers.
916#define ST_RDONLY 0x0001
917
918// FDE devices run the post-fs-data trigger (and hence also earlyBootEnded)
919// multiple times, sometimes prior to the real /data being mounted. That causes
920// keystore2 to try to open a file in /data, causing it to panic or have to be
921// killed by vold later, causing problems (vold failing to connect to keystore2,
922// or keystore2 operations erroring out later). As a workaround to keep FDE
923// working, ignore these too-early calls to earlyBootEnded.
924//
925// This can be removed when support for FDE is removed.
926static bool IgnoreEarlyBootEnded() {
927 // The statfs("/data") below should be sufficient by itself, but to be safe
928 // we also explicitly return false on FBE devices. (This really should be
929 // ro.crypto.type != "block" for "non-FDE devices", but on FDE devices this
930 // is sometimes called before ro.crypto.type gets set.)
931 if (fscrypt_is_native()) return false;
932
933 struct statfs buf;
934 if (statfs(DATA_MNT_POINT, &buf) != 0) {
935 PLOG(ERROR) << "statfs(\"/data\") failed";
936 return false;
937 }
938 if (buf.f_type == TMPFS_MAGIC || (buf.f_flags & ST_RDONLY)) {
939 LOG(INFO) << "Ignoring earlyBootEnded since real /data isn't mounted yet";
940 return true;
941 }
942 return false;
943}
944
Martijn Coeneneed957f2020-11-12 10:59:13 +0100945binder::Status VoldNativeService::earlyBootEnded() {
946 ENFORCE_SYSTEM_OR_ROOT;
947 ACQUIRE_LOCK;
948
Eric Biggers4859e0c2021-06-24 11:13:24 -0700949 if (IgnoreEarlyBootEnded()) return Ok();
950
Alex Buynytskyyb02e7b62021-02-11 19:09:10 -0800951 initializeIncFs();
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700952 Keystore::earlyBootEnded();
Martijn Coeneneed957f2020-11-12 10:59:13 +0100953 return Ok();
954}
955
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800956binder::Status VoldNativeService::incFsEnabled(bool* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700957 ENFORCE_SYSTEM_OR_ROOT;
958
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700959 *_aidl_return = incfs::enabled();
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700960 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -0800961}
962
963binder::Status VoldNativeService::mountIncFs(
Yurii Zubrytskyi3497cb52020-01-10 11:54:06 -0800964 const std::string& backingPath, const std::string& targetDir, int32_t flags,
Songchun Fan046e68a2021-04-27 12:46:02 -0700965 const std::string& sysfsName,
Songchun Fanab55cec2019-12-02 10:50:12 -0800966 ::android::os::incremental::IncrementalFileSystemControlParcel* _aidl_return) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700967 ENFORCE_SYSTEM_OR_ROOT;
Yurii Zubrytskyi40389822021-10-18 22:33:15 -0700968 if (auto status = CheckIncrementalPath(IncrementalPathKind::MountTarget, targetDir);
969 !status.isOk()) {
970 return status;
971 }
972 if (auto status = CheckIncrementalPath(IncrementalPathKind::MountSource, backingPath);
973 !status.isOk()) {
974 return status;
975 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -0700976
Yurii Zubrytskyi40389822021-10-18 22:33:15 -0700977 auto [backingFd, backingSymlink] = OpenDirInProcfs(backingPath);
978 if (!backingFd.ok()) {
979 return translate(-errno);
980 }
981 auto [targetFd, targetSymlink] = OpenDirInProcfs(targetDir);
982 if (!targetFd.ok()) {
983 return translate(-errno);
984 }
985
986 auto control = incfs::mount(backingSymlink, targetSymlink,
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700987 {.flags = IncFsMountFlags(flags),
Alex Buynytskyydcfeaa12021-04-01 13:43:14 -0700988 // Mount with read timeouts.
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700989 .defaultReadTimeoutMs = INCFS_DEFAULT_READ_TIMEOUT_MS,
990 // Mount with read logs disabled.
Songchun Fan046e68a2021-04-27 12:46:02 -0700991 .readLogBufferPages = 0,
992 .sysfsName = sysfsName.c_str()});
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700993 if (!control) {
994 return translate(-errno);
Songchun Fanab55cec2019-12-02 10:50:12 -0800995 }
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -0700996 auto fds = control.releaseFds();
997 using android::base::unique_fd;
998 _aidl_return->cmd.reset(unique_fd(fds[CMD].release()));
999 _aidl_return->pendingReads.reset(unique_fd(fds[PENDING_READS].release()));
1000 _aidl_return->log.reset(unique_fd(fds[LOGS].release()));
Yurii Zubrytskyi0c0f8392020-12-08 07:33:37 -08001001 if (fds[BLOCKS_WRITTEN].ok()) {
1002 _aidl_return->blocksWritten.emplace(unique_fd(fds[BLOCKS_WRITTEN].release()));
1003 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001004 return Ok();
Songchun Fanab55cec2019-12-02 10:50:12 -08001005}
1006
1007binder::Status VoldNativeService::unmountIncFs(const std::string& dir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001008 ENFORCE_SYSTEM_OR_ROOT;
Yurii Zubrytskyi40389822021-10-18 22:33:15 -07001009 if (auto status = CheckIncrementalPath(IncrementalPathKind::Any, dir); !status.isOk()) {
1010 return status;
1011 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001012
Yurii Zubrytskyi40389822021-10-18 22:33:15 -07001013 auto [fd, symLink] = OpenDirInProcfs(dir);
1014 if (!fd.ok()) {
1015 return translate(-errno);
1016 }
1017 return translate(incfs::unmount(symLink));
Songchun Fanab55cec2019-12-02 10:50:12 -08001018}
1019
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001020binder::Status VoldNativeService::setIncFsMountOptions(
1021 const ::android::os::incremental::IncrementalFileSystemControlParcel& control,
Songchun Fan0e53c1c2021-05-10 16:19:38 -07001022 bool enableReadLogs, bool enableReadTimeouts, const std::string& sysfsName) {
Alex Buynytskyy605a44f2020-04-02 15:21:47 -07001023 ENFORCE_SYSTEM_OR_ROOT;
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001024
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -07001025 auto incfsControl =
Yurii Zubrytskyi0c0f8392020-12-08 07:33:37 -08001026 incfs::createControl(control.cmd.get(), control.pendingReads.get(), control.log.get(),
1027 control.blocksWritten ? control.blocksWritten->get() : -1);
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -07001028 auto cleanupFunc = [](auto incfsControl) {
1029 for (auto& fd : incfsControl->releaseFds()) {
1030 (void)fd.release();
1031 }
1032 };
1033 auto cleanup =
1034 std::unique_ptr<incfs::Control, decltype(cleanupFunc)>(&incfsControl, cleanupFunc);
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001035
Yurii Zubrytskyi1d7acfd2021-10-28 14:37:12 -07001036 constexpr auto minReadLogBufferPages = INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES;
1037 constexpr auto maxReadLogBufferPages = 8 * INCFS_DEFAULT_PAGE_READ_BUFFER_PAGES;
1038 auto options = incfs::MountOptions{
1039 .defaultReadTimeoutMs =
1040 enableReadTimeouts ? INCFS_DEFAULT_READ_TIMEOUT_MS : kIncFsReadNoTimeoutMs,
1041 .readLogBufferPages = enableReadLogs ? maxReadLogBufferPages : 0,
1042 .sysfsName = sysfsName.c_str()};
1043
1044 for (;;) {
1045 const auto error = incfs::setOptions(incfsControl, options);
1046 if (!error) {
1047 return Ok();
1048 }
1049 if (!enableReadLogs || error != -ENOMEM) {
1050 return binder::Status::fromServiceSpecificError(error);
1051 }
1052 // In case of memory allocation error retry with a smaller buffer.
1053 options.readLogBufferPages /= 2;
1054 if (options.readLogBufferPages < minReadLogBufferPages) {
1055 return binder::Status::fromServiceSpecificError(error);
1056 }
1057 }
1058 // unreachable, but makes the compiler happy
Yurii Zubrytskyifc7b6692020-04-22 23:23:24 -07001059 return Ok();
Alex Buynytskyy91e0bf42020-03-31 14:46:25 -07001060}
1061
Songchun Fanab55cec2019-12-02 10:50:12 -08001062binder::Status VoldNativeService::bindMount(const std::string& sourceDir,
1063 const std::string& targetDir) {
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001064 ENFORCE_SYSTEM_OR_ROOT;
Yurii Zubrytskyi40389822021-10-18 22:33:15 -07001065 if (auto status = CheckIncrementalPath(IncrementalPathKind::Any, sourceDir); !status.isOk()) {
1066 return status;
1067 }
1068 if (auto status = CheckIncrementalPath(IncrementalPathKind::Bind, targetDir); !status.isOk()) {
1069 return status;
1070 }
Yurii Zubrytskyi131365a2020-03-24 23:49:02 -07001071
Yurii Zubrytskyi40389822021-10-18 22:33:15 -07001072 auto [sourceFd, sourceSymlink] = OpenDirInProcfs(sourceDir);
1073 if (!sourceFd.ok()) {
1074 return translate(-errno);
1075 }
1076 auto [targetFd, targetSymlink] = OpenDirInProcfs(targetDir);
1077 if (!targetFd.ok()) {
1078 return translate(-errno);
1079 }
1080 return translate(incfs::bindMount(sourceSymlink, targetSymlink));
Songchun Fanab55cec2019-12-02 10:50:12 -08001081}
1082
Yo Chiang0af25a32020-10-07 14:20:00 +08001083binder::Status VoldNativeService::destroyDsuMetadataKey(const std::string& dsuSlot) {
1084 ENFORCE_SYSTEM_OR_ROOT;
1085 ACQUIRE_LOCK;
1086
1087 return translateBool(destroy_dsu_metadata_key(dsuSlot));
1088}
1089
Jeff Sharkey068c6be2017-09-06 13:47:40 -06001090} // namespace vold
1091} // namespace android