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.
22 #include <sys/types.h>
26 #include <linux/fiemap.h>
29 #define LOG_TAG "secdiscard"
30 #include "cutils/log.h"
32 #include <AutoCloseFD.h>
35 // Deliberately limit ourselves to wiping small files.
36 constexpr uint64_t max_wipe_length = 4096;
38 void usage(const char *progname);
39 int secdiscard_path(const std::string &path);
40 int path_device_range(const std::string &path, uint64_t range[2]);
41 std::string block_device_for_path(const std::string &path);
44 int main(int argc, const char * const argv[]) {
45 if (argc != 2 || argv[1][0] != '/') {
49 SLOGD("Running: %s %s", argv[0], argv[1]);
50 secdiscard_path(argv[1]);
51 if (unlink(argv[1]) != 0 && errno != ENOENT) {
52 SLOGE("Unable to delete %s: %s",
53 argv[1], strerror(errno));
56 SLOGD("Discarded %s", argv[1]);
62 void usage(const char *progname) {
63 fprintf(stderr, "Usage: %s <absolute path>\n", progname);
66 // BLKSECDISCARD all content in "path", if it's small enough.
67 int secdiscard_path(const std::string &path) {
69 if (path_device_range(path, range) == -1) {
72 auto block_device = block_device_for_path(path);
73 if (block_device.empty()) {
76 AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
78 SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno));
81 if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
82 SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno));
88 // Find a short range that completely covers the file.
89 // If there isn't one, return -1, otherwise 0.
90 int path_device_range(const std::string &path, uint64_t range[2])
94 if (errno == ENOENT) {
95 SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno));
97 SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno));
101 alignas(struct fiemap) char fiemap_buffer[offsetof(struct fiemap, fm_extents[1])];
102 memset(fiemap_buffer, 0, sizeof(fiemap_buffer));
103 struct fiemap *fiemap = (struct fiemap *)fiemap_buffer;
104 fiemap->fm_start = 0;
105 fiemap->fm_length = UINT64_MAX;
106 fiemap->fm_flags = 0;
107 fiemap->fm_extent_count = 1;
108 fiemap->fm_mapped_extents = 0;
109 if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap) != 0) {
110 SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno));
113 if (fiemap->fm_mapped_extents != 1) {
114 SLOGE("Expecting one extent, got %d in %s", fiemap->fm_mapped_extents, path.c_str());
117 struct fiemap_extent *extent = &fiemap->fm_extents[0];
118 if (!(extent->fe_flags & FIEMAP_EXTENT_LAST)) {
119 SLOGE("First extent was not the last in %s", path.c_str());
122 if (extent->fe_flags &
123 (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
124 SLOGE("Extent has unexpected flags %ulx: %s", extent->fe_flags, path.c_str());
127 if (extent->fe_length > max_wipe_length) {
128 SLOGE("Extent too big, %llu bytes in %s", extent->fe_length, path.c_str());
131 range[0] = extent->fe_physical;
132 range[1] = extent->fe_length;
136 // Given a file path, look for the corresponding block device in /proc/mount
137 std::string block_device_for_path(const std::string &path)
139 std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
141 SLOGE("Unable to open /proc/mounts: %s", strerror(errno));
145 size_t best_length = 0;
146 struct mntent *mnt; // getmntent returns a thread local, so it's safe.
147 while ((mnt = getmntent(mnts.get())) != nullptr) {
148 auto l = strlen(mnt->mnt_dir);
149 if (l > best_length &&
152 path.compare(0, l, mnt->mnt_dir) == 0) {
153 result = mnt->mnt_fsname;
157 if (result.empty()) {
158 SLOGE("Didn't find a mountpoint to match path %s", path.c_str());
161 SLOGD("For path %s block device is %s", path.c_str(), result.c_str());