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.
24 #include <sys/types.h>
28 #include <linux/fiemap.h>
31 #include <android-base/logging.h>
33 #include <AutoCloseFD.h>
38 std::vector<std::string> targets;
42 constexpr uint32_t max_extents = 32;
44 bool read_command_line(int argc, const char * const argv[], Options &options);
45 void usage(const char *progname);
46 bool secdiscard_path(const std::string &path);
47 std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
48 bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
49 std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
50 std::string block_device_for_path(const std::string &path);
51 bool overwrite_with_zeros(int fd, off64_t start, off64_t length);
55 int main(int argc, const char * const argv[]) {
56 android::base::InitLogging(const_cast<char **>(argv));
58 if (!read_command_line(argc, argv, options)) {
62 for (auto const &target: options.targets) {
63 LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
64 if (!secdiscard_path(target)) {
65 LOG(ERROR) << "Secure discard failed for: " << target;
68 if (unlink(target.c_str()) != 0 && errno != ENOENT) {
69 PLOG(ERROR) << "Unable to unlink: " << target;
72 LOG(DEBUG) << "Discarded: " << target;
79 bool read_command_line(int argc, const char * const argv[], Options &options) {
80 for (int i = 1; i < argc; i++) {
81 if (!strcmp("--no-unlink", argv[i])) {
82 options.unlink = false;
83 } else if (!strcmp("--", argv[i])) {
84 for (int j = i+1; j < argc; j++) {
85 if (argv[j][0] != '/') return false; // Must be absolute path
86 options.targets.emplace_back(argv[j]);
88 return options.targets.size() > 0;
90 return false; // Unknown option
93 return false; // "--" not found
96 void usage(const char *progname) {
97 fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
100 // BLKSECDISCARD all content in "path", if it's small enough.
101 bool secdiscard_path(const std::string &path) {
102 auto fiemap = path_fiemap(path, max_extents);
103 if (!fiemap || !check_fiemap(*fiemap, path)) {
106 auto block_device = block_device_for_path(path);
107 if (block_device.empty()) {
110 AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
112 PLOG(ERROR) << "Failed to open device " << block_device;
115 for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
117 range[0] = fiemap->fm_extents[i].fe_physical;
118 range[1] = fiemap->fm_extents[i].fe_length;
119 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
120 PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
121 if (!overwrite_with_zeros(fs_fd.get(), range[0], range[1])) return false;
122 LOG(DEBUG) << "Used zero overwrite";
128 // Read the file's FIEMAP
129 std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
131 AutoCloseFD fd(path);
133 if (errno == ENOENT) {
134 PLOG(DEBUG) << "Unable to open " << path;
136 PLOG(ERROR) << "Unable to open " << path;
140 auto fiemap = alloc_fiemap(extent_count);
141 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
142 PLOG(ERROR) << "Unable to FIEMAP " << path;
145 auto mapped = fiemap->fm_mapped_extents;
146 if (mapped < 1 || mapped > extent_count) {
147 LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
154 // Ensure that the FIEMAP covers the file and is OK to discard
155 bool check_fiemap(const struct fiemap &fiemap, const std::string &path) {
156 auto mapped = fiemap.fm_mapped_extents;
157 if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
158 LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
161 for (uint32_t i = 0; i < mapped; i++) {
162 auto flags = fiemap.fm_extents[i].fe_flags;
163 if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
164 LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
171 std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count)
173 size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
174 std::unique_ptr<struct fiemap> res(new (::operator new (allocsize)) struct fiemap);
175 memset(res.get(), 0, allocsize);
177 res->fm_length = UINT64_MAX;
179 res->fm_extent_count = extent_count;
180 res->fm_mapped_extents = 0;
184 // Given a file path, look for the corresponding block device in /proc/mount
185 std::string block_device_for_path(const std::string &path)
187 std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
189 PLOG(ERROR) << "Unable to open /proc/mounts";
193 size_t best_length = 0;
194 struct mntent *mnt; // getmntent returns a thread local, so it's safe.
195 while ((mnt = getmntent(mnts.get())) != nullptr) {
196 auto l = strlen(mnt->mnt_dir);
197 if (l > best_length &&
200 path.compare(0, l, mnt->mnt_dir) == 0) {
201 result = mnt->mnt_fsname;
205 if (result.empty()) {
206 LOG(ERROR) <<"Didn't find a mountpoint to match path " << path;
209 LOG(DEBUG) << "For path " << path << " block device is " << result;
213 bool overwrite_with_zeros(int fd, off64_t start, off64_t length) {
214 if (lseek64(fd, start, SEEK_SET) != start) {
215 PLOG(ERROR) << "Seek failed for zero overwrite";
219 memset(buf, 0, sizeof(buf));
221 size_t wlen = static_cast<size_t>(std::min(static_cast<off64_t>(sizeof(buf)), length));
222 auto written = write(fd, buf, wlen);
224 PLOG(ERROR) << "Write of zeroes failed";