blob: cb2eca9297d62cf410a0a613d445373104cbdd56 [file] [log] [blame]
Paul Crowley53af81c2015-05-19 17:31:39 +01001/*
2 * Copyright (C) 2015 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
Paul Crowley28c4df42015-07-07 17:13:17 +010017#include <memory>
Paul Crowley53af81c2015-05-19 17:31:39 +010018#include <string>
Paul Crowley5ab73e92015-07-03 16:17:23 +010019#include <vector>
Paul Crowley53af81c2015-05-19 17:31:39 +010020
Paul Crowley14c8c072018-09-18 13:30:21 -070021#include <errno.h>
22#include <fcntl.h>
23#include <linux/fiemap.h>
24#include <linux/fs.h>
25#include <mntent.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010026#include <stdio.h>
27#include <stdlib.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010028#include <sys/stat.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070029#include <sys/types.h>
Paul Crowley53af81c2015-05-19 17:31:39 +010030
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000031#include <android-base/logging.h>
Paul Crowleye4c93da2017-06-16 09:21:18 -070032#include <android-base/unique_fd.h>
Paul Crowley4432e732015-07-01 13:33:47 +010033
Paul Crowley03f89d32017-06-16 09:37:31 -070034#include "FileDeviceUtils.h"
35
Paul Crowley4432e732015-07-01 13:33:47 +010036namespace {
Paul Crowley5ab73e92015-07-03 16:17:23 +010037
38struct Options {
39 std::vector<std::string> targets;
40 bool unlink{true};
41};
42
Paul Crowley28c4df42015-07-07 17:13:17 +010043constexpr uint32_t max_extents = 32;
Paul Crowley53af81c2015-05-19 17:31:39 +010044
Paul Crowley14c8c072018-09-18 13:30:21 -070045bool read_command_line(int argc, const char* const argv[], Options& options);
46void usage(const char* progname);
47bool secdiscard_path(const std::string& path);
48bool check_fiemap(const struct fiemap& fiemap, const std::string& path);
Paul Crowley2143ee82016-06-28 14:24:07 -070049bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
Paul Crowley5ab73e92015-07-03 16:17:23 +010050
Paul Crowley14c8c072018-09-18 13:30:21 -070051} // namespace
Paul Crowley53af81c2015-05-19 17:31:39 +010052
Paul Crowley14c8c072018-09-18 13:30:21 -070053int main(int argc, const char* const argv[]) {
54 android::base::InitLogging(const_cast<char**>(argv));
Paul Crowley5ab73e92015-07-03 16:17:23 +010055 Options options;
56 if (!read_command_line(argc, argv, options)) {
Paul Crowley53af81c2015-05-19 17:31:39 +010057 usage(argv[0]);
58 return -1;
59 }
Jaegeuk Kimab515b22018-08-02 12:41:45 -070060
Paul Crowley14c8c072018-09-18 13:30:21 -070061 for (auto const& target : options.targets) {
Jaegeuk Kimab515b22018-08-02 12:41:45 -070062// F2FS-specific ioctl
63// It requires the below kernel commit merged in v4.16-rc1.
64// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
65// In android-4.4,
66// 56ee1e817908 ("f2fs: updates on v4.16-rc1")
67// In android-4.9,
68// 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
69// In android-4.14,
70// ce767d9a55bc ("f2fs: updates on v4.16-rc1")
71#ifndef F2FS_IOC_SET_PIN_FILE
72#ifndef F2FS_IOCTL_MAGIC
Paul Crowley14c8c072018-09-18 13:30:21 -070073#define F2FS_IOCTL_MAGIC 0xf5
Jaegeuk Kimab515b22018-08-02 12:41:45 -070074#endif
Paul Crowley14c8c072018-09-18 13:30:21 -070075#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
76#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
Jaegeuk Kimab515b22018-08-02 12:41:45 -070077#endif
Paul Crowley14c8c072018-09-18 13:30:21 -070078 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(target.c_str(), O_WRONLY, 0)));
Jaegeuk Kimab515b22018-08-02 12:41:45 -070079 if (fd == -1) {
80 LOG(ERROR) << "Secure discard open failed for: " << target;
81 return 0;
82 }
83 __u32 set = 1;
84 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
85
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000086 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
Paul Crowley2143ee82016-06-28 14:24:07 -070087 if (!secdiscard_path(target)) {
88 LOG(ERROR) << "Secure discard failed for: " << target;
89 }
Paul Crowley5ab73e92015-07-03 16:17:23 +010090 if (options.unlink) {
91 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +000092 PLOG(ERROR) << "Unable to unlink: " << target;
Paul Crowley5ab73e92015-07-03 16:17:23 +010093 }
94 }
Jaegeuk Kimab515b22018-08-02 12:41:45 -070095 set = 0;
96 ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set);
Paul Crowley53af81c2015-05-19 17:31:39 +010097 }
98 return 0;
99}
100
Paul Crowley4432e732015-07-01 13:33:47 +0100101namespace {
102
Paul Crowley14c8c072018-09-18 13:30:21 -0700103bool read_command_line(int argc, const char* const argv[], Options& options) {
Paul Crowley5ab73e92015-07-03 16:17:23 +0100104 for (int i = 1; i < argc; i++) {
105 if (!strcmp("--no-unlink", argv[i])) {
106 options.unlink = false;
107 } else if (!strcmp("--", argv[i])) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700108 for (int j = i + 1; j < argc; j++) {
109 if (argv[j][0] != '/') return false; // Must be absolute path
Paul Crowley5ab73e92015-07-03 16:17:23 +0100110 options.targets.emplace_back(argv[j]);
111 }
112 return options.targets.size() > 0;
113 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700114 return false; // Unknown option
Paul Crowley5ab73e92015-07-03 16:17:23 +0100115 }
116 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700117 return false; // "--" not found
Paul Crowley5ab73e92015-07-03 16:17:23 +0100118}
119
Paul Crowley14c8c072018-09-18 13:30:21 -0700120void usage(const char* progname) {
Paul Crowley5ab73e92015-07-03 16:17:23 +0100121 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
Paul Crowley53af81c2015-05-19 17:31:39 +0100122}
123
124// BLKSECDISCARD all content in "path", if it's small enough.
Paul Crowley14c8c072018-09-18 13:30:21 -0700125bool secdiscard_path(const std::string& path) {
Paul Crowley03f89d32017-06-16 09:37:31 -0700126 auto fiemap = android::vold::PathFiemap(path, max_extents);
Paul Crowley28c4df42015-07-07 17:13:17 +0100127 if (!fiemap || !check_fiemap(*fiemap, path)) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700128 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100129 }
Paul Crowley03f89d32017-06-16 09:37:31 -0700130 auto block_device = android::vold::BlockDeviceForPath(path);
Paul Crowley4432e732015-07-01 13:33:47 +0100131 if (block_device.empty()) {
Paul Crowley2143ee82016-06-28 14:24:07 -0700132 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100133 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700134 android::base::unique_fd fs_fd(
135 TEMP_FAILURE_RETRY(open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0)));
Paul Crowleye4c93da2017-06-16 09:21:18 -0700136 if (fs_fd == -1) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000137 PLOG(ERROR) << "Failed to open device " << block_device;
Paul Crowley2143ee82016-06-28 14:24:07 -0700138 return false;
Paul Crowley4432e732015-07-01 13:33:47 +0100139 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100140 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
141 uint64_t range[2];
142 range[0] = fiemap->fm_extents[i].fe_physical;
143 range[1] = fiemap->fm_extents[i].fe_length;
144 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
Rubin Xuf2e846f2019-03-21 18:13:40 +0000145 // Use zero overwrite as a fallback for BLKSECDISCARD
Paul Crowley2143ee82016-06-28 14:24:07 -0700146 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
Paul Crowley28c4df42015-07-07 17:13:17 +0100147 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100148 }
Paul Crowley2143ee82016-06-28 14:24:07 -0700149 return true;
Paul Crowley53af81c2015-05-19 17:31:39 +0100150}
151
Paul Crowley28c4df42015-07-07 17:13:17 +0100152// Ensure that the FIEMAP covers the file and is OK to discard
Paul Crowley14c8c072018-09-18 13:30:21 -0700153bool check_fiemap(const struct fiemap& fiemap, const std::string& path) {
Paul Crowley28c4df42015-07-07 17:13:17 +0100154 auto mapped = fiemap.fm_mapped_extents;
155 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700156 LOG(ERROR) << "Extent " << mapped - 1 << " was not the last in " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100157 return false;
Paul Crowley53af81c2015-05-19 17:31:39 +0100158 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100159 for (uint32_t i = 0; i < mapped; i++) {
160 auto flags = fiemap.fm_extents[i].fe_flags;
161 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
Paul Crowley8bb8fcf2016-01-11 12:26:44 +0000162 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
Paul Crowley28c4df42015-07-07 17:13:17 +0100163 return false;
164 }
Paul Crowley53af81c2015-05-19 17:31:39 +0100165 }
Paul Crowley28c4df42015-07-07 17:13:17 +0100166 return true;
167}
168
Paul Crowley2143ee82016-06-28 14:24:07 -0700169bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
170 if (lseek64(fd, start, SEEK_SET) != start) {
171 PLOG(ERROR) << "Seek failed for zero overwrite";
172 return false;
173 }
174 char buf[BUFSIZ];
175 memset(buf, 0, sizeof(buf));
176 while (length > 0) {
177 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
178 auto written = write(fd, buf, wlen);
179 if (written < 1) {
180 PLOG(ERROR) << "Write of zeroes failed";
181 return false;
182 }
183 length -= written;
184 }
185 return true;
186}
187
Paul Crowley14c8c072018-09-18 13:30:21 -0700188} // namespace