1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "apexd_dm.h"
18
19 #include <ApexProperties.sysprop.h>
20 #include <android-base/logging.h>
21 #include <utils/Trace.h>
22
23 using android::base::ErrnoError;
24 using android::base::Error;
25 using android::base::Result;
26 using android::dm::DeviceMapper;
27 using android::dm::DmDeviceState;
28 using android::dm::DmTable;
29
30 namespace android::apex {
31
~DmDevice()32 DmDevice::~DmDevice() {
33 if (!cleared_) {
34 Result<void> ret = DeleteDmDevice(name_, /* deferred= */ false);
35 if (!ret.ok()) {
36 LOG(ERROR) << ret.error();
37 }
38 }
39 }
40
CreateDmDeviceInternal(DeviceMapper & dm,const std::string & name,const DmTable & table,const std::chrono::milliseconds & timeout)41 static Result<DmDevice> CreateDmDeviceInternal(
42 DeviceMapper& dm, const std::string& name, const DmTable& table,
43 const std::chrono::milliseconds& timeout) {
44 std::string dev_path;
45 if (!dm.CreateDevice(name, table, &dev_path, timeout)) {
46 return Error() << "Couldn't create dm-device.";
47 }
48 return DmDevice(name, dev_path);
49 }
50
CreateDmDevice(const std::string & name,const DmTable & table,bool reuse_device)51 Result<DmDevice> CreateDmDevice(const std::string& name, const DmTable& table,
52 bool reuse_device) {
53 ATRACE_NAME("CreateDmDevice");
54 LOG(VERBOSE) << "Creating dm-device " << name;
55
56 auto timeout = std::chrono::milliseconds(
57 android::sysprop::ApexProperties::dm_create_timeout().value_or(1000));
58
59 DeviceMapper& dm = DeviceMapper::Instance();
60
61 auto state = dm.GetState(name);
62 if (state == DmDeviceState::INVALID) {
63 return CreateDmDeviceInternal(dm, name, table, timeout);
64 }
65
66 if (reuse_device) {
67 if (state == DmDeviceState::ACTIVE) {
68 LOG(WARNING) << "Deleting existing active dm-device " << name;
69 OR_RETURN(DeleteDmDevice(name, /* deferred= */ false));
70 return CreateDmDeviceInternal(dm, name, table, timeout);
71 }
72 if (!dm.LoadTableAndActivate(name, table)) {
73 dm.DeleteDevice(name);
74 return Error() << "Failed to activate dm-device " << name;
75 }
76 std::string path;
77 if (!dm.WaitForDevice(name, timeout, &path)) {
78 dm.DeleteDevice(name);
79 return Error() << "Failed waiting for dm-device " << name;
80 }
81 return DmDevice(name, path);
82 } else {
83 // Delete dangling dm-device. This can happen if apexd fails to delete it
84 // while unmounting an apex.
85 LOG(WARNING) << "Deleting existing dm-device " << name;
86 OR_RETURN(DeleteDmDevice(name, /* deferred= */ false));
87 return CreateDmDeviceInternal(dm, name, table, timeout);
88 }
89 }
90
91 // Deletes a device-mapper device with a given name and path
92 // Synchronizes on the device actually being deleted from userspace.
DeleteDmDevice(const std::string & name,bool deferred)93 Result<void> DeleteDmDevice(const std::string& name, bool deferred) {
94 DeviceMapper& dm = DeviceMapper::Instance();
95 if (deferred) {
96 if (!dm.DeleteDeviceDeferred(name)) {
97 return ErrnoError() << "Failed to issue deferred delete of dm-device "
98 << name;
99 }
100 return {};
101 }
102 auto timeout = std::chrono::milliseconds(
103 android::sysprop::ApexProperties::dm_delete_timeout().value_or(750));
104 if (!dm.DeleteDevice(name, timeout)) {
105 return Error() << "Failed to delete dm-device " << name;
106 }
107 return {};
108 }
109
110 } // namespace android::apex