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.
25 #include <logwrap/logwrap.h>
27 #include <sys/statvfs.h>
28 #include <utils/Log.h>
30 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32 #define MAX_IO_WRITE_CHUNK_SIZE 0x100000
35 #define min(a,b) ((a) < (b) ? (a) : (b))
38 typedef unsigned long u64;
40 static void usage(const char * const progname) {
42 "Usage: %s [-s <seed>] -h <hole size in bytes> -t <total hole size in bytes> "
47 static u64 get_free_space(const char * const path) {
50 if (statvfs(path, &s) < 0) {
51 fprintf(stderr, "\nerrno: %d. Failed to get free disk space on %s\n",
55 return (u64)s.f_bsize * (u64)s.f_bfree;
59 static u64 get_random_num(const u64 start, const u64 end) {
62 assert(RAND_MAX >= 0x7FFFFFFF);
63 if ((end - start) > 0x7FFFFFFF)
64 return start + (((u64)random() << 31) | (u64)random()) % (end - start);
65 return start + (random() % (end - start));
68 static char get_random_char() {
69 return 'A' + random() % ('Z' - 'A');
72 static bool create_unique_file(const char * const dir_path, const u64 size,
73 const u64 id, char * const base,
74 const u64 base_length) {
77 char file_path[FILENAME_MAX];
80 base[random() % min(base_length, size)] = get_random_char();
82 sprintf(file_path, "%s/file_%lu", dir_path, id);
83 fd = open(file_path, O_WRONLY | O_CREAT | O_SYNC, 0777);
85 // We suppress ENOSPC erros as that is common as we approach the
86 // last few MBs of the fs as we don't account for the size of the newly
87 // added meta data after the initial free space computation.
89 fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, file_path);
93 while (length + base_length < size) {
94 if (write(fd, base, base_length) < 0) {
96 fprintf(stderr, "\nerrno: %d. Failed to write %lu bytes to %s\n",
97 errno, base_length, file_path);
102 length += base_length;
104 if (write(fd, base, size - length) < 0) {
106 fprintf(stderr, "\nerrno: %d. Failed to write last %lu bytes to %s\n",
107 errno, size - length, file_path);
114 fprintf(stderr, "\nFailed to close %s\n", file_path);
120 static bool create_unique_dir(char *dir, const char * const root_path) {
121 char random_string[15];
124 for (i = 0; i < 14; ++i) {
125 random_string[i] = get_random_char();
127 random_string[14] = '\0';
129 sprintf(dir, "%s/%s", root_path, random_string);
131 if (mkdir(dir, 0777) < 0) {
132 fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, dir);
138 static bool puncture_fs (const char * const path, const u64 total_size,
139 const u64 hole_size, const u64 total_hole_size) {
140 u64 increments = (hole_size * total_size) / total_hole_size;
142 u64 starting_max = 0;
143 u64 ending_max = increments;
144 char stay_dir[FILENAME_MAX], delete_dir[FILENAME_MAX];
145 char *rm_bin_argv[] = { "/system/bin/rm", "-rf", ""};
147 char *base_file_data;
150 if (!create_unique_dir(stay_dir, path) ||
151 !create_unique_dir(delete_dir, path)) {
155 base_file_data = (char*) malloc(MAX_IO_WRITE_CHUNK_SIZE);
156 for (i = 0; i < MAX_IO_WRITE_CHUNK_SIZE; ++i) {
157 base_file_data[i] = get_random_char();
159 fprintf(stderr, "\n");
160 while (ending_max <= total_size) {
161 fprintf(stderr, "\rSTAGE 1/2: %d%% Complete",
162 (int) (100.0 * starting_max / total_size));
163 hole_max = get_random_num(starting_max, ending_max);
165 create_unique_file(stay_dir,
166 hole_max - starting_max,
169 MAX_IO_WRITE_CHUNK_SIZE);
170 create_unique_file(delete_dir,
174 MAX_IO_WRITE_CHUNK_SIZE);
176 starting_max = hole_max + hole_size;
177 ending_max += increments;
179 create_unique_file(stay_dir,
180 (ending_max - increments - starting_max),
183 MAX_IO_WRITE_CHUNK_SIZE);
184 fprintf(stderr, "\rSTAGE 1/2: 100%% Complete\n");
185 fprintf(stderr, "\rSTAGE 2/2: 0%% Complete");
186 free(base_file_data);
187 rm_bin_argv[2] = delete_dir;
188 if (android_fork_execvp_ext(ARRAY_SIZE(rm_bin_argv), rm_bin_argv,
189 NULL, 1, LOG_KLOG, 0, NULL) < 0) {
190 fprintf(stderr, "\nFailed to delete %s\n", rm_bin_argv[2]);
193 fprintf(stderr, "\rSTAGE 2/2: 100%% Complete\n");
197 int main (const int argc, char ** const argv) {
201 int seed = time(NULL);
205 u64 total_hole_size = 0;
208 while ((opt = getopt(argc, argv, "s:h:t:")) != -1) {
214 hole_size = atoll(optarg);
218 total_hole_size = atoll(optarg);
230 if (optind >= argc) {
231 fprintf(stderr, "\nExpected path name after options.\n");
235 path = argv[optind++];
238 fprintf(stderr, "\nUnexpected argument: %s\n", argv[optind]);
244 fprintf(stderr, "\nRandom seed is: %d\n", seed);
246 total_size = get_free_space(path);
250 if (total_size < total_hole_size || total_hole_size < hole_size) {
251 fprintf(stderr, "\nInvalid sizes: total available size should be "
252 "larger than total hole size which is larger than "
257 if (!puncture_fs(path, total_size, hole_size, total_hole_size)) {