OSDN Git Service

a7500439b61bd569405822bfb96951f44fb30aad
[android-x86/system-vold.git] / secdiscard.cpp
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
17 #include <string>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <linux/fs.h>
26 #include <linux/fiemap.h>
27 #include <mntent.h>
28
29 #define LOG_TAG "secdiscard"
30 #include "cutils/log.h"
31
32 #include <AutoCloseFD.h>
33
34 namespace {
35 // Deliberately limit ourselves to wiping small files.
36 constexpr uint64_t max_wipe_length = 4096;
37
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);
42 }
43
44 int main(int argc, const char * const argv[]) {
45     if (argc != 2 || argv[1][0] != '/') {
46         usage(argv[0]);
47         return -1;
48     }
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));
54         return -1;
55     }
56     SLOGD("Discarded %s", argv[1]);
57     return 0;
58 }
59
60 namespace {
61
62 void usage(const char *progname) {
63     fprintf(stderr, "Usage: %s <absolute path>\n", progname);
64 }
65
66 // BLKSECDISCARD all content in "path", if it's small enough.
67 int secdiscard_path(const std::string &path) {
68     uint64_t range[2];
69     if (path_device_range(path, range) == -1) {
70         return -1;
71     }
72     auto block_device = block_device_for_path(path);
73     if (block_device.empty()) {
74         return -1;
75     }
76     AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
77     if (!fs_fd) {
78         SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno));
79         return -1;
80     }
81     if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
82         SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno));
83         return -1;
84     }
85     return 0;
86 }
87
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])
91 {
92     AutoCloseFD fd(path);
93     if (!fd) {
94         if (errno == ENOENT) {
95             SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno));
96         } else {
97             SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno));
98         }
99         return -1;
100     }
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));
111         return -1;
112     }
113     if (fiemap->fm_mapped_extents != 1) {
114         SLOGE("Expecting one extent, got %d in %s", fiemap->fm_mapped_extents, path.c_str());
115         return -1;
116     }
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());
120         return -1;
121     }
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());
125         return -1;
126     }
127     if (extent->fe_length > max_wipe_length) {
128         SLOGE("Extent too big, %llu bytes in %s", extent->fe_length, path.c_str());
129         return -1;
130     }
131     range[0] = extent->fe_physical;
132     range[1] = extent->fe_length;
133     return 0;
134 }
135
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)
138 {
139     std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
140     if (!mnts) {
141         SLOGE("Unable to open /proc/mounts: %s", strerror(errno));
142         return "";
143     }
144     std::string result;
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 &&
150             path.size() > l &&
151             path[l] == '/' &&
152             path.compare(0, l, mnt->mnt_dir) == 0) {
153                 result = mnt->mnt_fsname;
154                 best_length = l;
155         }
156     }
157     if (result.empty()) {
158         SLOGE("Didn't find a mountpoint to match path %s", path.c_str());
159         return "";
160     }
161     SLOGD("For path %s block device is %s", path.c_str(), result.c_str());
162     return result;
163 }
164
165 }