1 /*
2 * Copyright (C) 2018 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 "libdm/loop_control.h"
18
19 #include <fcntl.h>
20 #include <linux/loop.h>
21 #include <stdint.h>
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 #include <android-base/unique_fd.h>
29
30 #include "utility.h"
31
32 namespace android {
33 namespace dm {
34
LoopControl()35 LoopControl::LoopControl() : control_fd_(-1) {
36 control_fd_.reset(TEMP_FAILURE_RETRY(open(kLoopControlDevice, O_RDWR | O_CLOEXEC)));
37 if (control_fd_ < 0) {
38 PLOG(ERROR) << "Failed to open loop-control";
39 }
40 }
41
Attach(int file_fd,const std::chrono::milliseconds & timeout_ms,std::string * loopdev) const42 bool LoopControl::Attach(int file_fd, const std::chrono::milliseconds& timeout_ms,
43 std::string* loopdev) const {
44 auto start_time = std::chrono::steady_clock::now();
45 auto condition = [&]() -> WaitResult {
46 if (!FindFreeLoopDevice(loopdev)) {
47 LOG(ERROR) << "Failed to attach, no free loop devices";
48 return WaitResult::Fail;
49 }
50
51 auto now = std::chrono::steady_clock::now();
52 auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
53 if (!WaitForFile(*loopdev, timeout_ms - time_elapsed)) {
54 LOG(ERROR) << "Timed out waiting for path: " << *loopdev;
55 return WaitResult::Fail;
56 }
57
58 android::base::unique_fd loop_fd(
59 TEMP_FAILURE_RETRY(open(loopdev->c_str(), O_RDWR | O_CLOEXEC)));
60 if (loop_fd < 0) {
61 PLOG(ERROR) << "Failed to open: " << *loopdev;
62 return WaitResult::Fail;
63 }
64
65 if (int rc = ioctl(loop_fd, LOOP_SET_FD, file_fd); rc == 0) {
66 return WaitResult::Done;
67 }
68 if (errno != EBUSY) {
69 PLOG(ERROR) << "Failed LOOP_SET_FD";
70 return WaitResult::Fail;
71 }
72 return WaitResult::Wait;
73 };
74 if (!WaitForCondition(condition, timeout_ms)) {
75 LOG(ERROR) << "Timed out trying to acquire a loop device";
76 return false;
77 }
78 return true;
79 }
80
Detach(const std::string & loopdev) const81 bool LoopControl::Detach(const std::string& loopdev) const {
82 if (loopdev.empty()) {
83 LOG(ERROR) << "Must provide a loop device";
84 return false;
85 }
86
87 android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev.c_str(), O_RDWR | O_CLOEXEC)));
88 if (loop_fd < 0) {
89 PLOG(ERROR) << "Failed to open: " << loopdev;
90 return false;
91 }
92
93 int rc = ioctl(loop_fd, LOOP_CLR_FD, 0);
94 if (rc) {
95 PLOG(ERROR) << "Failed LOOP_CLR_FD for '" << loopdev << "'";
96 return false;
97 }
98 return true;
99 }
100
FindFreeLoopDevice(std::string * loopdev) const101 bool LoopControl::FindFreeLoopDevice(std::string* loopdev) const {
102 int rc = ioctl(control_fd_, LOOP_CTL_GET_FREE);
103 if (rc < 0) {
104 PLOG(ERROR) << "Failed to get free loop device";
105 return false;
106 }
107
108 // Ueventd on android creates all loop devices as /dev/block/loopX
109 // The total number of available devices is determined by 'loop.max_part'
110 // kernel command line argument.
111 *loopdev = ::android::base::StringPrintf("/dev/block/loop%d", rc);
112 return true;
113 }
114
EnableDirectIo(int fd)115 bool LoopControl::EnableDirectIo(int fd) {
116 #if !defined(LOOP_SET_BLOCK_SIZE)
117 static constexpr int LOOP_SET_BLOCK_SIZE = 0x4C09;
118 #endif
119 #if !defined(LOOP_SET_DIRECT_IO)
120 static constexpr int LOOP_SET_DIRECT_IO = 0x4C08;
121 #endif
122
123 // Note: the block size has to be >= the logical block size of the underlying
124 // block device, *not* the filesystem block size.
125 if (ioctl(fd, LOOP_SET_BLOCK_SIZE, 4096)) {
126 PLOG(ERROR) << "Could not set loop device block size";
127 return false;
128 }
129 if (ioctl(fd, LOOP_SET_DIRECT_IO, 1)) {
130 PLOG(ERROR) << "Could not set loop direct IO";
131 return false;
132 }
133 return true;
134 }
135
LoopDevice(android::base::borrowed_fd fd,const std::chrono::milliseconds & timeout_ms,bool auto_close)136 LoopDevice::LoopDevice(android::base::borrowed_fd fd, const std::chrono::milliseconds& timeout_ms,
137 bool auto_close)
138 : fd_(fd), owned_fd_(-1) {
139 if (auto_close) {
140 owned_fd_.reset(fd.get());
141 }
142 Init(timeout_ms);
143 }
144
LoopDevice(const std::string & path,const std::chrono::milliseconds & timeout_ms)145 LoopDevice::LoopDevice(const std::string& path, const std::chrono::milliseconds& timeout_ms)
146 : fd_(-1), owned_fd_(-1) {
147 owned_fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
148 if (owned_fd_ == -1) {
149 PLOG(ERROR) << "open failed for " << path;
150 return;
151 }
152 fd_ = owned_fd_;
153 Init(timeout_ms);
154 }
155
~LoopDevice()156 LoopDevice::~LoopDevice() {
157 if (valid()) {
158 control_.Detach(device_);
159 }
160 }
161
Init(const std::chrono::milliseconds & timeout_ms)162 void LoopDevice::Init(const std::chrono::milliseconds& timeout_ms) {
163 valid_ = control_.Attach(fd_.get(), timeout_ms, &device_);
164 }
165
166 } // namespace dm
167 } // namespace android
168