2 * Copyright (C) 2014 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.
26 #include <logwrap/logwrap.h>
28 #include <sys/statvfs.h>
29 #include <utils/Log.h>
31 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33 #define MAX_IO_WRITE_CHUNK_SIZE 0x100000
36 #define min(a,b) ((a) < (b) ? (a) : (b))
39 typedef unsigned long u64;
41 static void usage(const char * const progname) {
43 "Usage: %s [-s <seed>] -h <hole size in bytes> -t <total hole size in bytes> "
48 static u64 get_free_space(const char * const path) {
51 if (statvfs(path, &s) < 0) {
52 fprintf(stderr, "\nerrno: %d. Failed to get free disk space on %s\n",
56 return (u64)s.f_bsize * (u64)s.f_bfree;
60 static u64 get_random_num(const u64 start, const u64 end) {
63 assert(RAND_MAX >= 0x7FFFFFFF);
64 if ((end - start) > 0x7FFFFFFF)
65 return start + (((u64)random() << 31) | (u64)random()) % (end - start);
66 return start + (random() % (end - start));
69 static char get_random_char() {
70 return 'A' + random() % ('Z' - 'A');
73 static bool create_unique_file(const char * const dir_path, const u64 size,
74 const u64 id, char * const base,
75 const u64 base_length) {
78 char file_path[FILENAME_MAX];
81 base[random() % min(base_length, size)] = get_random_char();
83 sprintf(file_path, "%s/file_%lu", dir_path, id);
84 fd = open(file_path, O_WRONLY | O_CREAT | O_SYNC, 0777);
86 // We suppress ENOSPC erros as that is common as we approach the
87 // last few MBs of the fs as we don't account for the size of the newly
88 // added meta data after the initial free space computation.
90 fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, file_path);
94 while (length + base_length < size) {
95 if (write(fd, base, base_length) < 0) {
97 fprintf(stderr, "\nerrno: %d. Failed to write %lu bytes to %s\n",
98 errno, base_length, file_path);
103 length += base_length;
105 if (write(fd, base, size - length) < 0) {
107 fprintf(stderr, "\nerrno: %d. Failed to write last %lu bytes to %s\n",
108 errno, size - length, file_path);
115 fprintf(stderr, "\nFailed to close %s\n", file_path);
121 static bool create_unique_dir(char *dir, const char * const root_path) {
122 char random_string[15];
125 for (i = 0; i < 14; ++i) {
126 random_string[i] = get_random_char();
128 random_string[14] = '\0';
130 sprintf(dir, "%s/%s", root_path, random_string);
132 if (mkdir(dir, 0777) < 0) {
133 fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, dir);
139 static bool puncture_fs (const char * const path, const u64 total_size,
140 const u64 hole_size, const u64 total_hole_size) {
141 u64 increments = (hole_size * total_size) / total_hole_size;
143 u64 starting_max = 0;
144 u64 ending_max = increments;
145 char stay_dir[FILENAME_MAX], delete_dir[FILENAME_MAX];
146 char *rm_bin_argv[] = { "/system/bin/rm", "-rf", ""};
148 char *base_file_data;
151 if (!create_unique_dir(stay_dir, path) ||
152 !create_unique_dir(delete_dir, path)) {
156 base_file_data = (char*) malloc(MAX_IO_WRITE_CHUNK_SIZE);
157 for (i = 0; i < MAX_IO_WRITE_CHUNK_SIZE; ++i) {
158 base_file_data[i] = get_random_char();
160 fprintf(stderr, "\n");
161 while (ending_max <= total_size) {
162 fprintf(stderr, "\rSTAGE 1/2: %d%% Complete",
163 (int) (100.0 * starting_max / total_size));
164 hole_max = get_random_num(starting_max, ending_max);
166 create_unique_file(stay_dir,
167 hole_max - starting_max,
170 MAX_IO_WRITE_CHUNK_SIZE);
171 create_unique_file(delete_dir,
175 MAX_IO_WRITE_CHUNK_SIZE);
177 starting_max = hole_max + hole_size;
178 ending_max += increments;
180 create_unique_file(stay_dir,
181 (ending_max - increments - starting_max),
184 MAX_IO_WRITE_CHUNK_SIZE);
185 fprintf(stderr, "\rSTAGE 1/2: 100%% Complete\n");
186 fprintf(stderr, "\rSTAGE 2/2: 0%% Complete");
187 free(base_file_data);
188 rm_bin_argv[2] = delete_dir;
189 if (android_fork_execvp_ext(ARRAY_SIZE(rm_bin_argv), rm_bin_argv,
190 NULL, 1, LOG_KLOG, 0, NULL, NULL, 0) < 0) {
191 fprintf(stderr, "\nFailed to delete %s\n", rm_bin_argv[2]);
194 fprintf(stderr, "\rSTAGE 2/2: 100%% Complete\n");
198 int main (const int argc, char ** const argv) {
202 int seed = time(NULL);
206 u64 total_hole_size = 0;
209 while ((opt = getopt(argc, argv, "s:h:t:")) != -1) {
215 hole_size = atoll(optarg);
219 total_hole_size = atoll(optarg);
231 if (optind >= argc) {
232 fprintf(stderr, "\nExpected path name after options.\n");
236 path = argv[optind++];
239 fprintf(stderr, "\nUnexpected argument: %s\n", argv[optind]);
245 fprintf(stderr, "\nRandom seed is: %d\n", seed);
247 total_size = get_free_space(path);
251 if (total_size < total_hole_size || total_hole_size < hole_size) {
252 fprintf(stderr, "\nInvalid sizes: total available size should be "
253 "larger than total hole size which is larger than "
258 if (!puncture_fs(path, total_size, hole_size, total_hole_size)) {