Mohamad Ayyash | a2b7dbe | 2014-06-25 16:18:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <assert.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <getopt.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <logwrap/logwrap.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/statvfs.h> |
| 28 | #include <utils/Log.h> |
| 29 | |
| 30 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 31 | |
| 32 | #define MAX_IO_WRITE_CHUNK_SIZE 0x100000 |
| 33 | |
| 34 | #ifndef min |
| 35 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
| 36 | #endif |
| 37 | |
| 38 | typedef unsigned long u64; |
| 39 | |
| 40 | static void usage(const char * const progname) { |
| 41 | fprintf(stderr, |
| 42 | "Usage: %s [-s <seed>] -h <hole size in bytes> -t <total hole size in bytes> " |
| 43 | "path\n", |
| 44 | progname); |
| 45 | } |
| 46 | |
| 47 | static u64 get_free_space(const char * const path) { |
| 48 | struct statvfs s; |
| 49 | |
| 50 | if (statvfs(path, &s) < 0) { |
| 51 | fprintf(stderr, "\nerrno: %d. Failed to get free disk space on %s\n", |
| 52 | errno, path); |
| 53 | return 0; |
| 54 | } else { |
| 55 | return (u64)s.f_bsize * (u64)s.f_bfree; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static u64 get_random_num(const u64 start, const u64 end) { |
| 60 | if (end - start <= 0) |
| 61 | return start; |
| 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)); |
| 66 | } |
| 67 | |
| 68 | static char get_random_char() { |
| 69 | return 'A' + random() % ('Z' - 'A'); |
| 70 | } |
| 71 | |
| 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) { |
| 75 | u64 length = 0; |
| 76 | int fd; |
| 77 | char file_path[FILENAME_MAX]; |
| 78 | bool ret = true; |
| 79 | |
| 80 | base[random() % min(base_length, size)] = get_random_char(); |
| 81 | |
| 82 | sprintf(file_path, "%s/file_%lu", dir_path, id); |
| 83 | fd = open(file_path, O_WRONLY | O_CREAT | O_SYNC, 0777); |
| 84 | if (fd < 0) { |
| 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. |
| 88 | if (errno != 28) { |
| 89 | fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, file_path); |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | while (length + base_length < size) { |
| 94 | if (write(fd, base, base_length) < 0) { |
| 95 | if (errno != 28) { |
| 96 | fprintf(stderr, "\nerrno: %d. Failed to write %lu bytes to %s\n", |
| 97 | errno, base_length, file_path); |
| 98 | } |
| 99 | ret = false; |
| 100 | goto done; |
| 101 | } |
| 102 | length += base_length; |
| 103 | } |
| 104 | if (write(fd, base, size - length) < 0) { |
| 105 | if (errno != 28) { |
| 106 | fprintf(stderr, "\nerrno: %d. Failed to write last %lu bytes to %s\n", |
| 107 | errno, size - length, file_path); |
| 108 | } |
| 109 | ret = false; |
| 110 | goto done; |
| 111 | } |
| 112 | done: |
| 113 | if (close(fd) < 0) { |
| 114 | fprintf(stderr, "\nFailed to close %s\n", file_path); |
| 115 | ret = false; |
| 116 | } |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static bool create_unique_dir(char *dir, const char * const root_path) { |
| 121 | char random_string[15]; |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < 14; ++i) { |
| 125 | random_string[i] = get_random_char(); |
| 126 | } |
| 127 | random_string[14] = '\0'; |
| 128 | |
| 129 | sprintf(dir, "%s/%s", root_path, random_string); |
| 130 | |
| 131 | if (mkdir(dir, 0777) < 0) { |
| 132 | fprintf(stderr, "\nerrno: %d. Failed to create %s\n", errno, dir); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 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; |
| 141 | u64 hole_max; |
| 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", ""}; |
| 146 | u64 file_id = 1; |
| 147 | char *base_file_data; |
| 148 | u64 i = 0; |
| 149 | |
| 150 | if (!create_unique_dir(stay_dir, path) || |
| 151 | !create_unique_dir(delete_dir, path)) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 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(); |
| 158 | } |
| 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); |
| 164 | |
| 165 | create_unique_file(stay_dir, |
| 166 | hole_max - starting_max, |
| 167 | file_id++, |
| 168 | base_file_data, |
| 169 | MAX_IO_WRITE_CHUNK_SIZE); |
| 170 | create_unique_file(delete_dir, |
| 171 | hole_size, |
| 172 | file_id++, |
| 173 | base_file_data, |
| 174 | MAX_IO_WRITE_CHUNK_SIZE); |
| 175 | |
| 176 | starting_max = hole_max + hole_size; |
| 177 | ending_max += increments; |
| 178 | } |
| 179 | create_unique_file(stay_dir, |
| 180 | (ending_max - increments - starting_max), |
| 181 | file_id++, |
| 182 | base_file_data, |
| 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]); |
| 191 | return false; |
| 192 | } |
| 193 | fprintf(stderr, "\rSTAGE 2/2: 100%% Complete\n"); |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | int main (const int argc, char ** const argv) { |
| 198 | int opt; |
| 199 | int mandatory_opt; |
| 200 | char *path = NULL; |
| 201 | int seed = time(NULL); |
| 202 | |
| 203 | u64 total_size = 0; |
| 204 | u64 hole_size = 0; |
| 205 | u64 total_hole_size = 0; |
| 206 | |
| 207 | mandatory_opt = 2; |
| 208 | while ((opt = getopt(argc, argv, "s:h:t:")) != -1) { |
| 209 | switch(opt) { |
| 210 | case 's': |
| 211 | seed = atoi(optarg); |
| 212 | break; |
| 213 | case 'h': |
| 214 | hole_size = atoll(optarg); |
| 215 | mandatory_opt--; |
| 216 | break; |
| 217 | case 't': |
| 218 | total_hole_size = atoll(optarg); |
| 219 | mandatory_opt--; |
| 220 | break; |
| 221 | default: |
| 222 | usage(argv[0]); |
| 223 | exit(EXIT_FAILURE); |
| 224 | } |
| 225 | } |
| 226 | if (mandatory_opt) { |
| 227 | usage(argv[0]); |
| 228 | exit(EXIT_FAILURE); |
| 229 | } |
| 230 | if (optind >= argc) { |
| 231 | fprintf(stderr, "\nExpected path name after options.\n"); |
| 232 | usage(argv[0]); |
| 233 | exit(EXIT_FAILURE); |
| 234 | } |
| 235 | path = argv[optind++]; |
| 236 | |
| 237 | if (optind < argc) { |
| 238 | fprintf(stderr, "\nUnexpected argument: %s\n", argv[optind]); |
| 239 | usage(argv[0]); |
| 240 | exit(EXIT_FAILURE); |
| 241 | } |
| 242 | |
| 243 | srandom(seed); |
| 244 | fprintf(stderr, "\nRandom seed is: %d\n", seed); |
| 245 | |
| 246 | total_size = get_free_space(path); |
| 247 | if (!total_size) { |
| 248 | exit(EXIT_FAILURE); |
| 249 | } |
| 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 " |
| 253 | "hole size\n"); |
| 254 | exit(EXIT_FAILURE); |
| 255 | } |
| 256 | |
| 257 | if (!puncture_fs(path, total_size, hole_size, total_hole_size)) { |
| 258 | exit(EXIT_FAILURE); |
| 259 | } |
| 260 | return 0; |
| 261 | } |