Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 17 | #include <memory> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 18 | #include <string> |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 19 | #include <vector> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 20 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <linux/fiemap.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <mntent.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 28 | #include <sys/stat.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 30 | |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 33 | |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 34 | #include "FileDeviceUtils.h" |
| 35 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 36 | namespace { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 37 | |
| 38 | struct Options { |
| 39 | std::vector<std::string> targets; |
| 40 | bool unlink{true}; |
| 41 | }; |
| 42 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 43 | constexpr uint32_t max_extents = 32; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 44 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 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); |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 49 | bool overwrite_with_zeros(int fd, off64_t start, off64_t length); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 50 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 51 | } // namespace |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 52 | |
Daeho Jeong | d1b946e | 2020-09-08 12:59:28 +0900 | [diff] [blame] | 53 | #ifndef F2FS_IOCTL_MAGIC |
| 54 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 55 | #endif |
| 56 | |
| 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") |
| 60 | // In android12-5.4, |
| 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 { |
| 65 | __u64 start; |
| 66 | __u64 len; |
| 67 | __u64 flags; |
| 68 | }; |
| 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 |
| 72 | #endif |
| 73 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 74 | int main(int argc, const char* const argv[]) { |
| 75 | android::base::InitLogging(const_cast<char**>(argv)); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 76 | Options options; |
| 77 | if (!read_command_line(argc, argv, options)) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 78 | usage(argv[0]); |
| 79 | return -1; |
| 80 | } |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame] | 81 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 82 | for (auto const& target : options.targets) { |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame] | 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") |
| 86 | // In android-4.4, |
| 87 | // 56ee1e817908 ("f2fs: updates on v4.16-rc1") |
| 88 | // In android-4.9, |
| 89 | // 2f17e34672a8 ("f2fs: updates on v4.16-rc1") |
| 90 | // In android-4.14, |
| 91 | // ce767d9a55bc ("f2fs: updates on v4.16-rc1") |
| 92 | #ifndef F2FS_IOC_SET_PIN_FILE |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 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) |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame] | 95 | #endif |
Nick Kralevich | e7e89ac | 2019-03-29 16:03:51 -0700 | [diff] [blame] | 96 | android::base::unique_fd fd( |
| 97 | TEMP_FAILURE_RETRY(open(target.c_str(), O_WRONLY | O_CLOEXEC, 0))); |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame] | 98 | if (fd == -1) { |
| 99 | LOG(ERROR) << "Secure discard open failed for: " << target; |
| 100 | return 0; |
| 101 | } |
| 102 | __u32 set = 1; |
| 103 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
| 104 | |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 105 | LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink; |
Daeho Jeong | d1b946e | 2020-09-08 12:59:28 +0900 | [diff] [blame] | 106 | struct f2fs_sectrim_range secRange; |
| 107 | secRange.start = 0; |
| 108 | secRange.len = -1; // until end of file |
| 109 | secRange.flags = F2FS_TRIM_FILE_DISCARD | F2FS_TRIM_FILE_ZEROOUT; |
| 110 | /* |
| 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. |
| 115 | */ |
| 116 | int ret = ioctl(fd, F2FS_IOC_SEC_TRIM_FILE, &secRange); |
| 117 | if (ret != 0) { |
| 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); |
| 122 | } |
| 123 | if (ret != 0 && errno != ENOTTY) { |
| 124 | PLOG(WARNING) << "F2FS_IOC_SEC_TRIM_FILE failed on " << target; |
| 125 | } |
| 126 | } |
| 127 | if (ret != 0) { |
| 128 | if (!secdiscard_path(target)) { |
| 129 | LOG(ERROR) << "Secure discard failed for: " << target; |
| 130 | } |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 131 | } |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 132 | if (options.unlink) { |
| 133 | if (unlink(target.c_str()) != 0 && errno != ENOENT) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 134 | PLOG(ERROR) << "Unable to unlink: " << target; |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame] | 137 | set = 0; |
| 138 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 143 | namespace { |
| 144 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 145 | bool read_command_line(int argc, const char* const argv[], Options& options) { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 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])) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 150 | for (int j = i + 1; j < argc; j++) { |
| 151 | if (argv[j][0] != '/') return false; // Must be absolute path |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 152 | options.targets.emplace_back(argv[j]); |
| 153 | } |
| 154 | return options.targets.size() > 0; |
| 155 | } else { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 156 | return false; // Unknown option |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 159 | return false; // "--" not found |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 162 | void usage(const char* progname) { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 163 | fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // BLKSECDISCARD all content in "path", if it's small enough. |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 167 | bool secdiscard_path(const std::string& path) { |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 168 | auto fiemap = android::vold::PathFiemap(path, max_extents); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 169 | if (!fiemap || !check_fiemap(*fiemap, path)) { |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 170 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 171 | } |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 172 | auto block_device = android::vold::BlockDeviceForPath(path); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 173 | if (block_device.empty()) { |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 174 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 175 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 176 | android::base::unique_fd fs_fd( |
| 177 | TEMP_FAILURE_RETRY(open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0))); |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 178 | if (fs_fd == -1) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 179 | PLOG(ERROR) << "Failed to open device " << block_device; |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 180 | return false; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 181 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 182 | for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) { |
| 183 | uint64_t range[2]; |
| 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) { |
Rubin Xu | f2e846f | 2019-03-21 18:13:40 +0000 | [diff] [blame] | 187 | // Use zero overwrite as a fallback for BLKSECDISCARD |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 188 | if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 189 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 190 | } |
Jaegeuk Kim | 6c5e453 | 2019-08-08 09:07:58 -0700 | [diff] [blame] | 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. |
| 194 | fsync(fs_fd.get()); |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 195 | return true; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 198 | // Ensure that the FIEMAP covers the file and is OK to discard |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 199 | bool check_fiemap(const struct fiemap& fiemap, const std::string& path) { |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 200 | auto mapped = fiemap.fm_mapped_extents; |
| 201 | if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 202 | LOG(ERROR) << "Extent " << mapped - 1 << " was not the last in " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 203 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 204 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 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)) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 208 | LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 209 | return false; |
| 210 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 211 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 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"; |
| 218 | return false; |
| 219 | } |
| 220 | char buf[BUFSIZ]; |
| 221 | memset(buf, 0, sizeof(buf)); |
| 222 | while (length > 0) { |
| 223 | size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length)); |
| 224 | auto written = write(fd, buf, wlen); |
| 225 | if (written < 1) { |
| 226 | PLOG(ERROR) << "Write of zeroes failed"; |
| 227 | return false; |
| 228 | } |
| 229 | length -= written; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 234 | } // namespace |