2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <linux/fiemap.h>
29 #include <sys/types.h>
31 #include <android-base/logging.h>
32 #include <android-base/unique_fd.h>
34 #include "FileDeviceUtils.h"
39 std::vector<std::string> targets;
43 constexpr uint32_t max_extents = 32;
45 bool read_command_line(int argc, const char* const argv[], Options& options);
46 void usage(const char* progname);
47 bool secdiscard_path(const std::string& path);
48 bool check_fiemap(const struct fiemap& fiemap, const std::string& path);
49 bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
53 #ifndef F2FS_IOCTL_MAGIC
54 #define F2FS_IOCTL_MAGIC 0xf5
57 // F2FS-specific ioctl
58 // It requires the below kernel commit merged in v5.9-rc1.
59 // 9af846486d78 ("f2fs: add F2FS_IOC_SEC_TRIM_FILE ioctl")
61 // 7fc27297c44d ("Merge remote-tracking branch 'aosp/upstream-f2fs-stable-linux-5.4.y'
62 // into android12-5.4")
63 #ifndef F2FS_IOC_SEC_TRIM_FILE
64 struct f2fs_sectrim_range {
69 #define F2FS_IOC_SEC_TRIM_FILE _IOW(F2FS_IOCTL_MAGIC, 20, struct f2fs_sectrim_range)
70 #define F2FS_TRIM_FILE_DISCARD 0x1
71 #define F2FS_TRIM_FILE_ZEROOUT 0x2
74 int main(int argc, const char* const argv[]) {
75 android::base::InitLogging(const_cast<char**>(argv));
77 if (!read_command_line(argc, argv, options)) {
82 for (auto const& target : options.targets) {
83 // F2FS-specific ioctl
84 // It requires the below kernel commit merged in v4.16-rc1.
85 // 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
87 // 56ee1e817908 ("f2fs: updates on v4.16-rc1")
89 // 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
91 // ce767d9a55bc ("f2fs: updates on v4.16-rc1")
92 #ifndef F2FS_IOC_SET_PIN_FILE
93 #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
94 #define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
96 android::base::unique_fd fd(
97 TEMP_FAILURE_RETRY(open(target.c_str(), O_WRONLY | O_CLOEXEC, 0)));
99 LOG(ERROR) << "Secure discard open failed for: " << target;
103 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
105 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
106 struct f2fs_sectrim_range secRange;
108 secRange.len = -1; // until end of file
109 secRange.flags = F2FS_TRIM_FILE_DISCARD | F2FS_TRIM_FILE_ZEROOUT;
111 * F2FS_IOC_SEC_TRIM_FILE is only supported by F2FS.
112 * 1. If device supports secure discard, it sends secure discard command on the file.
113 * 2. Otherwise, it sends discard command on the file.
114 * 3. Lastly, it overwrites zero data on it.
116 int ret = ioctl(fd, F2FS_IOC_SEC_TRIM_FILE, &secRange);
118 if (errno == EOPNOTSUPP) {
119 // If device doesn't support any type of discard, just overwrite zero data.
120 secRange.flags = F2FS_TRIM_FILE_ZEROOUT;
121 ret = ioctl(fd, F2FS_IOC_SEC_TRIM_FILE, &secRange);
123 if (ret != 0 && errno != ENOTTY) {
124 PLOG(WARNING) << "F2FS_IOC_SEC_TRIM_FILE failed on " << target;
128 if (!secdiscard_path(target)) {
129 LOG(ERROR) << "Secure discard failed for: " << target;
132 if (options.unlink) {
133 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
134 PLOG(ERROR) << "Unable to unlink: " << target;
138 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
145 bool read_command_line(int argc, const char* const argv[], Options& options) {
146 for (int i = 1; i < argc; i++) {
147 if (!strcmp("--no-unlink", argv[i])) {
148 options.unlink = false;
149 } else if (!strcmp("--", argv[i])) {
150 for (int j = i + 1; j < argc; j++) {
151 if (argv[j][0] != '/') return false; // Must be absolute path
152 options.targets.emplace_back(argv[j]);
154 return options.targets.size() > 0;
156 return false; // Unknown option
159 return false; // "--" not found
162 void usage(const char* progname) {
163 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
166 // BLKSECDISCARD all content in "path", if it's small enough.
167 bool secdiscard_path(const std::string& path) {
168 auto fiemap = android::vold::PathFiemap(path, max_extents);
169 if (!fiemap || !check_fiemap(*fiemap, path)) {
172 auto block_device = android::vold::BlockDeviceForPath(path);
173 if (block_device.empty()) {
176 android::base::unique_fd fs_fd(
177 TEMP_FAILURE_RETRY(open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
179 PLOG(ERROR) << "Failed to open device " << block_device;
182 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
184 range[0] = fiemap->fm_extents[i].fe_physical;
185 range[1] = fiemap->fm_extents[i].fe_length;
186 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
187 // Use zero overwrite as a fallback for BLKSECDISCARD
188 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
191 // Should wait for overwrites completion. Otherwise after unlink(),
192 // filesystem can allocate these blocks and IO can be reordered, resulting
193 // in making zero blocks to filesystem blocks.
198 // Ensure that the FIEMAP covers the file and is OK to discard
199 bool check_fiemap(const struct fiemap& fiemap, const std::string& path) {
200 auto mapped = fiemap.fm_mapped_extents;
201 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
202 LOG(ERROR) << "Extent " << mapped - 1 << " was not the last in " << path;
205 for (uint32_t i = 0; i < mapped; i++) {
206 auto flags = fiemap.fm_extents[i].fe_flags;
207 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
208 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
215 bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
216 if (lseek64(fd, start, SEEK_SET) != start) {
217 PLOG(ERROR) << "Seek failed for zero overwrite";
221 memset(buf, 0, sizeof(buf));
223 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
224 auto written = write(fd, buf, wlen);
226 PLOG(ERROR) << "Write of zeroes failed";