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 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/fiemap.h> |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 29 | #include <mntent.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 | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 45 | bool read_command_line(int argc, const char * const argv[], Options &options); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 46 | void usage(const char *progname); |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 47 | bool secdiscard_path(const std::string &path); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 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 | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 51 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 52 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 53 | int main(int argc, const char * const argv[]) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 54 | android::base::InitLogging(const_cast<char **>(argv)); |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 55 | Options options; |
| 56 | if (!read_command_line(argc, argv, options)) { |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 57 | usage(argv[0]); |
| 58 | return -1; |
| 59 | } |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame^] | 60 | |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 61 | for (auto const &target: options.targets) { |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame^] | 62 | // 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 |
| 73 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 74 | #endif |
| 75 | #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) |
| 76 | #define F2FS_IOC_GET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 14, __u32) |
| 77 | #endif |
| 78 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open( |
| 79 | target.c_str(), O_WRONLY, 0))); |
| 80 | if (fd == -1) { |
| 81 | LOG(ERROR) << "Secure discard open failed for: " << target; |
| 82 | return 0; |
| 83 | } |
| 84 | __u32 set = 1; |
| 85 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
| 86 | |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 87 | LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink; |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 88 | if (!secdiscard_path(target)) { |
| 89 | LOG(ERROR) << "Secure discard failed for: " << target; |
| 90 | } |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 91 | if (options.unlink) { |
| 92 | if (unlink(target.c_str()) != 0 && errno != ENOENT) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 93 | PLOG(ERROR) << "Unable to unlink: " << target; |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
Jaegeuk Kim | ab515b2 | 2018-08-02 12:41:45 -0700 | [diff] [blame^] | 96 | set = 0; |
| 97 | ioctl(fd, F2FS_IOC_SET_PIN_FILE, &set); |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 98 | LOG(DEBUG) << "Discarded: " << target; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 103 | namespace { |
| 104 | |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 105 | bool read_command_line(int argc, const char * const argv[], Options &options) { |
| 106 | for (int i = 1; i < argc; i++) { |
| 107 | if (!strcmp("--no-unlink", argv[i])) { |
| 108 | options.unlink = false; |
| 109 | } else if (!strcmp("--", argv[i])) { |
| 110 | for (int j = i+1; j < argc; j++) { |
| 111 | if (argv[j][0] != '/') return false; // Must be absolute path |
| 112 | options.targets.emplace_back(argv[j]); |
| 113 | } |
| 114 | return options.targets.size() > 0; |
| 115 | } else { |
| 116 | return false; // Unknown option |
| 117 | } |
| 118 | } |
| 119 | return false; // "--" not found |
| 120 | } |
| 121 | |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 122 | void usage(const char *progname) { |
Paul Crowley | 5ab73e9 | 2015-07-03 16:17:23 +0100 | [diff] [blame] | 123 | fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname); |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // BLKSECDISCARD all content in "path", if it's small enough. |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 127 | bool secdiscard_path(const std::string &path) { |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 128 | auto fiemap = android::vold::PathFiemap(path, max_extents); |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 129 | if (!fiemap || !check_fiemap(*fiemap, path)) { |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 130 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 131 | } |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 132 | auto block_device = android::vold::BlockDeviceForPath(path); |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 133 | if (block_device.empty()) { |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 134 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 135 | } |
Paul Crowley | e4c93da | 2017-06-16 09:21:18 -0700 | [diff] [blame] | 136 | android::base::unique_fd fs_fd(TEMP_FAILURE_RETRY(open( |
| 137 | block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC, 0))); |
| 138 | if (fs_fd == -1) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 139 | PLOG(ERROR) << "Failed to open device " << block_device; |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 140 | return false; |
Paul Crowley | 4432e73 | 2015-07-01 13:33:47 +0100 | [diff] [blame] | 141 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 142 | for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) { |
| 143 | uint64_t range[2]; |
| 144 | range[0] = fiemap->fm_extents[i].fe_physical; |
| 145 | range[1] = fiemap->fm_extents[i].fe_length; |
| 146 | if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 147 | PLOG(ERROR) << "Unable to BLKSECDISCARD " << path; |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 148 | if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false; |
| 149 | LOG(DEBUG) << "Used zero overwrite"; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 150 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 151 | } |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 152 | return true; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 155 | // Ensure that the FIEMAP covers the file and is OK to discard |
| 156 | bool check_fiemap(const struct fiemap &fiemap, const std::string &path) { |
| 157 | auto mapped = fiemap.fm_mapped_extents; |
| 158 | if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 159 | LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 160 | return false; |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 161 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 162 | for (uint32_t i = 0; i < mapped; i++) { |
| 163 | auto flags = fiemap.fm_extents[i].fe_flags; |
| 164 | if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) { |
Paul Crowley | 8bb8fcf | 2016-01-11 12:26:44 +0000 | [diff] [blame] | 165 | LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path; |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 166 | return false; |
| 167 | } |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 168 | } |
Paul Crowley | 28c4df4 | 2015-07-07 17:13:17 +0100 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
Paul Crowley | 2143ee8 | 2016-06-28 14:24:07 -0700 | [diff] [blame] | 172 | bool overwrite_with_zeros(int fd, off64_t start, off64_t length) { |
| 173 | if (lseek64(fd, start, SEEK_SET) != start) { |
| 174 | PLOG(ERROR) << "Seek failed for zero overwrite"; |
| 175 | return false; |
| 176 | } |
| 177 | char buf[BUFSIZ]; |
| 178 | memset(buf, 0, sizeof(buf)); |
| 179 | while (length > 0) { |
| 180 | size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length)); |
| 181 | auto written = write(fd, buf, wlen); |
| 182 | if (written < 1) { |
| 183 | PLOG(ERROR) << "Write of zeroes failed"; |
| 184 | return false; |
| 185 | } |
| 186 | length -= written; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
Paul Crowley | 53af81c | 2015-05-19 17:31:39 +0100 | [diff] [blame] | 191 | } |