diff options
37 files changed, 1480 insertions, 1580 deletions
diff --git a/ANRdaemon/ANRdaemon.cpp b/ANRdaemon/ANRdaemon.cpp index bd433b33..f087ff22 100644 --- a/ANRdaemon/ANRdaemon.cpp +++ b/ANRdaemon/ANRdaemon.cpp @@ -33,12 +33,12 @@ #include <binder/IServiceManager.h> #include <binder/Parcel.h> -#include <ctime> #include <cutils/properties.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <ctime> #include <sys/resource.h> #include <sys/stat.h> @@ -59,13 +59,13 @@ using namespace android; #define LOG_TAG "anrdaemon" -static const int check_period = 1; // in sec -static const int tracing_check_period = 500000; // in micro sec -static const int cpu_stat_entries = 7; // number of cpu stat entries +static const int check_period = 1; // in sec +static const int tracing_check_period = 500000; // in micro sec +static const int cpu_stat_entries = 7; // number of cpu stat entries static const int min_buffer_size = 16; static const int max_buffer_size = 2048; -static const char *min_buffer_size_str = "16"; -static const char *max_buffer_size_str = "2048"; +static const char* min_buffer_size_str = "16"; +static const char* max_buffer_size_str = "2048"; static const int time_buf_size = 20; static const int path_buf_size = 60; @@ -82,14 +82,14 @@ typedef struct cpu_stat { static int idle_threshold = 10; static bool quit = false; -static bool suspend= false; +static bool suspend = false; static bool dump_requested = false; static bool err = false; static char err_msg[100]; static bool tracing = false; -static const char *buf_size_kb = "2048"; -static const char *apps = ""; +static const char* buf_size_kb = "2048"; +static const char* apps = ""; static uint64_t tag = 0; static cpu_stat_t new_cpu; @@ -98,46 +98,36 @@ static cpu_stat_t old_cpu; /* Log certain kernel activity when enabled */ static bool log_sched = false; static bool log_stack = false; -static bool log_irq = false; -static bool log_sync = false; +static bool log_irq = false; +static bool log_sync = false; static bool log_workq = false; /* Paths for debugfs controls*/ -static const char* dfs_trace_output_path = - "/d/tracing/trace"; -static const char* dfs_irq_path = - "/d/tracing/events/irq/enable"; -static const char* dfs_sync_path = - "/d/tracing/events/sync/enable"; -static const char* dfs_workq_path = - "/d/tracing/events/workqueue/enable"; -static const char* dfs_stack_path = - "/d/tracing/options/stacktrace"; -static const char* dfs_sched_switch_path = - "/d/tracing/events/sched/sched_switch/enable"; -static const char* dfs_sched_wakeup_path = - "/d/tracing/events/sched/sched_wakeup/enable"; -static const char* dfs_control_path = - "/d/tracing/tracing_on"; -static const char* dfs_buffer_size_path = - "/d/tracing/buffer_size_kb"; +static const char* dfs_trace_output_path = "/d/tracing/trace"; +static const char* dfs_irq_path = "/d/tracing/events/irq/enable"; +static const char* dfs_sync_path = "/d/tracing/events/sync/enable"; +static const char* dfs_workq_path = "/d/tracing/events/workqueue/enable"; +static const char* dfs_stack_path = "/d/tracing/options/stacktrace"; +static const char* dfs_sched_switch_path = "/d/tracing/events/sched/sched_switch/enable"; +static const char* dfs_sched_wakeup_path = "/d/tracing/events/sched/sched_wakeup/enable"; +static const char* dfs_control_path = "/d/tracing/tracing_on"; +static const char* dfs_buffer_size_path = "/d/tracing/buffer_size_kb"; static const char* dfs_tags_property = "debug.atrace.tags.enableflags"; static const char* dfs_apps_property = "debug.atrace.app_cmdlines"; /* * Read accumulated cpu data from /proc/stat */ -static void get_cpu_stat(cpu_stat_t *cpu) { - FILE *fp = NULL; - const char *params = "cpu %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n"; +static void get_cpu_stat(cpu_stat_t* cpu) { + FILE* fp = NULL; + const char* params = "cpu %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n"; if ((fp = fopen("/proc/stat", "r")) == NULL) { err = true; snprintf(err_msg, sizeof(err_msg), "can't read from /proc/stat with errno %d", errno); } else { - if (fscanf(fp, params, &cpu->utime, &cpu->ntime, - &cpu->stime, &cpu->itime, &cpu->iowtime, &cpu->irqtime, - &cpu->sirqtime) != cpu_stat_entries) { + if (fscanf(fp, params, &cpu->utime, &cpu->ntime, &cpu->stime, &cpu->itime, &cpu->iowtime, + &cpu->irqtime, &cpu->sirqtime) != cpu_stat_entries) { /* * If failed in getting status, new_cpu won't be updated and * is_heavy_loaded() will return false. @@ -147,8 +137,8 @@ static void get_cpu_stat(cpu_stat_t *cpu) { return; } - cpu->total = cpu->utime + cpu->ntime + cpu->stime + cpu->itime - + cpu->iowtime + cpu->irqtime + cpu->sirqtime; + cpu->total = cpu->utime + cpu->ntime + cpu->stime + cpu->itime + cpu->iowtime + + cpu->irqtime + cpu->sirqtime; fclose(fp); } @@ -162,7 +152,7 @@ static void get_cpu_stat(cpu_stat_t *cpu) { */ static bool is_heavy_load(void) { unsigned long diff_idle, diff_total; - int threshold = idle_threshold + (tracing?100:0); + int threshold = idle_threshold + (tracing ? 100 : 0); get_cpu_stat(&new_cpu); diff_idle = new_cpu.itime - old_cpu.itime; diff_total = new_cpu.total - old_cpu.total; @@ -195,9 +185,9 @@ static int dfs_enable(bool enable, const char* path) { snprintf(err_msg, sizeof(err_msg), "Can't open %s. Error: %d", path, errno); return -1; } - const char* control = (enable?"1":"0"); + const char* control = (enable ? "1" : "0"); ssize_t len = strlen(control); - int max_try = 10; // Fail if write was interrupted for 10 times + int max_try = 10; // Fail if write was interrupted for 10 times while (write(fd, control, len) != len) { if (errno == EINTR && max_try-- > 0) { usleep(100); @@ -208,7 +198,7 @@ static int dfs_enable(bool enable, const char* path) { snprintf(err_msg, sizeof(err_msg), "Error %d in writing to %s.", errno, path); } close(fd); - return (err?-1:0); + return (err ? -1 : 0); } /* @@ -222,8 +212,7 @@ static void dfs_set_property(uint64_t mtag, const char* mapp, bool enable) { snprintf(err_msg, sizeof(err_msg), "Failed to set debug tags system properties."); } - if (strlen(mapp) > 0 - && property_set(dfs_apps_property, mapp) < 0) { + if (strlen(mapp) > 0 && property_set(dfs_apps_property, mapp) < 0) { err = true; snprintf(err_msg, sizeof(err_msg), "Failed to set debug applications."); } @@ -250,10 +239,9 @@ static void dfs_set_property(uint64_t mtag, const char* mapp, bool enable) { * Dump the log in a compressed format for systrace to visualize. * Create a dump file "dump_of_anrdaemon.<current_time>" under /data/misc/anrd */ -static void dump_trace() -{ +static void dump_trace() { time_t now = time(0); - struct tm tstruct; + struct tm tstruct; char time_buf[time_buf_size]; char path_buf[path_buf_size]; const char* header = " done\nTRACE:\n"; @@ -296,7 +284,7 @@ static void dump_trace() return; } - const size_t bufSize = 64*1024; + const size_t bufSize = 64 * 1024; in = (uint8_t*)malloc(bufSize); out = (uint8_t*)malloc(bufSize); flush = Z_NO_FLUSH; @@ -412,16 +400,14 @@ static int set_tracing_buffer_size(void) { snprintf(err_msg, sizeof(err_msg), "Error in writing to atrace buffer size file."); } close(fd); - return (err?-1:0); - + return (err ? -1 : 0); } /* * Main loop to moniter the cpu usage and decided whether to start logging. */ static void start(void) { - if ((set_tracing_buffer_size()) != 0) - return; + if ((set_tracing_buffer_size()) != 0) return; dfs_set_property(tag, apps, true); dfs_poke_binder(); @@ -448,8 +434,7 @@ static void start(void) { * If trace is not running, dump trace right away. * If trace is running, request to dump trace. */ -static void request_dump_trace() -{ +static void request_dump_trace() { if (!tracing) { dump_trace(); } else if (!dump_requested) { @@ -457,8 +442,7 @@ static void request_dump_trace() } } -static void handle_signal(int signo) -{ +static void handle_signal(int signo) { switch (signo) { case SIGQUIT: suspend = true; @@ -482,16 +466,15 @@ static void handle_signal(int signo) * SIGCONT: Resume the daemon as normal. * SIGUSR1: Dump the logging to a compressed format for systrace to visualize. */ -static void register_sighandler(void) -{ +static void register_sighandler(void) { struct sigaction sa; sigset_t block_mask; sigemptyset(&block_mask); - sigaddset (&block_mask, SIGQUIT); - sigaddset (&block_mask, SIGSTOP); - sigaddset (&block_mask, SIGCONT); - sigaddset (&block_mask, SIGUSR1); + sigaddset(&block_mask, SIGQUIT); + sigaddset(&block_mask, SIGSTOP); + sigaddset(&block_mask, SIGCONT); + sigaddset(&block_mask, SIGUSR1); sa.sa_flags = 0; sa.sa_mask = block_mask; @@ -503,43 +486,45 @@ static void register_sighandler(void) } static void show_help(void) { - fprintf(stderr, "usage: ANRdaemon [options] [categoris...]\n"); - fprintf(stdout, "Options includes:\n" - " -a appname enable app-level tracing for a comma " - "separated list of cmdlines\n" - " -t N cpu threshold for logging to start " - "(uint = 0.01%%, min = 5000, max = 9999, default = 9990)\n" - " -s N use a trace buffer size of N KB " - "default to 2048KB\n" - " -h show helps\n"); - fprintf(stdout, "Categoris includes:\n" - " am - activity manager\n" - " sm - sync manager\n" - " input - input\n" - " dalvik - dalvik VM\n" - " audio - Audio\n" - " gfx - Graphics\n" - " rs - RenderScript\n" - " hal - Hardware Modules\n" - " irq - kernel irq events\n" - " sched - kernel scheduler activity\n" - " stack - kernel stack\n" - " sync - kernel sync activity\n" - " workq - kernel work queues\n"); - fprintf(stdout, "Control includes:\n" - " SIGQUIT: terminate the process\n" - " SIGSTOP: suspend all function of the daemon\n" - " SIGCONT: resume the normal function\n" - " SIGUSR1: dump the current logging in a compressed form\n"); + fprintf(stdout, + "Options includes:\n" + " -a appname enable app-level tracing for a comma " + "separated list of cmdlines\n" + " -t N cpu threshold for logging to start " + "(uint = 0.01%%, min = 5000, max = 9999, default = 9990)\n" + " -s N use a trace buffer size of N KB " + "default to 2048KB\n" + " -h show helps\n"); + fprintf(stdout, + "Categoris includes:\n" + " am - activity manager\n" + " sm - sync manager\n" + " input - input\n" + " dalvik - dalvik VM\n" + " audio - Audio\n" + " gfx - Graphics\n" + " rs - RenderScript\n" + " hal - Hardware Modules\n" + " irq - kernel irq events\n" + " sched - kernel scheduler activity\n" + " stack - kernel stack\n" + " sync - kernel sync activity\n" + " workq - kernel work queues\n"); + fprintf(stdout, + "Control includes:\n" + " SIGQUIT: terminate the process\n" + " SIGSTOP: suspend all function of the daemon\n" + " SIGCONT: resume the normal function\n" + " SIGUSR1: dump the current logging in a compressed form\n"); exit(0); } -static int get_options(int argc, char *argv[]) { +static int get_options(int argc, char* argv[]) { int opt = 0; int threshold; while ((opt = getopt(argc, argv, "a:s:t:h")) >= 0) { - switch(opt) { + switch (opt) { case 'a': apps = optarg; break; @@ -563,8 +548,10 @@ static int get_options(int argc, char *argv[]) { show_help(); break; default: - fprintf(stderr, "Error in getting options.\n" - "run \"%s -h\" for usage.\n", argv[0]); + fprintf(stderr, + "Error in getting options.\n" + "run \"%s -h\" for usage.\n", + argv[0]); return 1; } } @@ -597,8 +584,10 @@ static int get_options(int argc, char *argv[]) { } else if (strcmp(argv[i], "sync") == 0) { log_sync = true; } else { - fprintf(stderr, "invalid category: %s\n" - "run \"%s -h\" for usage.\n", argv[i], argv[0]); + fprintf(stderr, + "invalid category: %s\n" + "run \"%s -h\" for usage.\n", + argv[i], argv[0]); return 1; } } @@ -612,13 +601,10 @@ static int get_options(int argc, char *argv[]) { return 0; } -int main(int argc, char *argv[]) -{ - if(get_options(argc, argv) != 0) - return 1; +int main(int argc, char* argv[]) { + if (get_options(argc, argv) != 0) return 1; - if (daemon(0, 0) != 0) - return 1; + if (daemon(0, 0) != 0) return 1; register_sighandler(); @@ -633,10 +619,9 @@ int main(int argc, char *argv[]) ALOGI("ANRdaemon starting"); start(); - if (err) - ALOGE("ANRdaemon stopped due to Error: %s", err_msg); + if (err) ALOGE("ANRdaemon stopped due to Error: %s", err_msg); ALOGI("ANRdaemon terminated."); - return (err?1:0); + return (err ? 1 : 0); } diff --git a/cpustats/cpustats.c b/cpustats/cpustats.c index b2b7c07c..998a97c6 100644 --- a/cpustats/cpustats.c +++ b/cpustats/cpustats.c @@ -43,31 +43,35 @@ struct freq_info { struct cpu_info { long unsigned utime, ntime, stime, itime, iowtime, irqtime, sirqtime; - struct freq_info *freqs; + struct freq_info* freqs; int freq_count; }; -#define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } +#define die(...) \ + { \ + fprintf(stderr, __VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } static struct cpu_info old_total_cpu, new_total_cpu, *old_cpus, *new_cpus; static int cpu_count, delay, iterations; static char minimal, aggregate_freq_stats; static int get_cpu_count(); -static int get_cpu_count_from_file(char *filename); -static long unsigned get_cpu_total_time(struct cpu_info *cpu); +static int get_cpu_count_from_file(char* filename); +static long unsigned get_cpu_total_time(struct cpu_info* cpu); static int get_freq_scales_count(int cpu); static void print_stats(); -static void print_cpu_stats(char *label, struct cpu_info *new_cpu, struct cpu_info *old_cpu, - char print_freq); -static void print_freq_stats(struct cpu_info *new_cpu, struct cpu_info *old_cpu); +static void print_cpu_stats(char* label, struct cpu_info* new_cpu, struct cpu_info* old_cpu, + char print_freq); +static void print_freq_stats(struct cpu_info* new_cpu, struct cpu_info* old_cpu); static void read_stats(); static void read_freq_stats(int cpu); static char should_aggregate_freq_stats(); static char should_print_freq_stats(); -static void usage(char *cmd); +static void usage(char* cmd); -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { struct cpu_info *tmp_cpus, tmp_total_cpu; int i, freq_count; @@ -187,8 +191,8 @@ static int get_cpu_count() { /* * Get the number of CPUs from a given filename. */ -static int get_cpu_count_from_file(char *filename) { - FILE *file; +static int get_cpu_count_from_file(char* filename) { + FILE* file; char line[MAX_BUF_SIZE]; int cpu_count; @@ -213,7 +217,7 @@ static int get_cpu_count_from_file(char *filename) { * Get the number of frequency states a given CPU can be scaled to. */ static int get_freq_scales_count(int cpu) { - FILE *file; + FILE* file; char filename[MAX_BUF_SIZE]; long unsigned freq; int count = 0; @@ -225,7 +229,7 @@ static int get_freq_scales_count(int cpu) { freq = 0; fscanf(file, "%lu %*d\n", &freq); if (freq) count++; - } while(freq); + } while (freq); fclose(file); return count; @@ -235,15 +239,15 @@ static int get_freq_scales_count(int cpu) { * Read the CPU and frequency stats for all cpus. */ static void read_stats() { - FILE *file; + FILE* file; char scanline[MAX_BUF_SIZE]; int i; file = fopen("/proc/stat", "r"); if (!file) die("Could not open /proc/stat.\n"); - fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n", - &new_total_cpu.utime, &new_total_cpu.ntime, &new_total_cpu.stime, &new_total_cpu.itime, - &new_total_cpu.iowtime, &new_total_cpu.irqtime, &new_total_cpu.sirqtime); + fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n", &new_total_cpu.utime, + &new_total_cpu.ntime, &new_total_cpu.stime, &new_total_cpu.itime, &new_total_cpu.iowtime, + &new_total_cpu.irqtime, &new_total_cpu.sirqtime); if (aggregate_freq_stats) { for (i = 0; i < new_total_cpu.freq_count; i++) { new_total_cpu.freqs[i].time = 0; @@ -264,7 +268,7 @@ static void read_stats() { * Read the frequency stats for a given cpu. */ static void read_freq_stats(int cpu) { - FILE *file; + FILE* file; char filename[MAX_BUF_SIZE]; int i; @@ -272,8 +276,7 @@ static void read_freq_stats(int cpu) { file = fopen(filename, "r"); for (i = 0; i < new_cpus[cpu].freq_count; i++) { if (file) { - fscanf(file, "%u %lu\n", &new_cpus[cpu].freqs[i].freq, - &new_cpus[cpu].freqs[i].time); + fscanf(file, "%u %lu\n", &new_cpus[cpu].freqs[i].freq, &new_cpus[cpu].freqs[i].time); } else { /* The CPU has been off lined for some reason */ new_cpus[cpu].freqs[i].freq = old_cpus[cpu].freqs[i].freq; @@ -284,14 +287,13 @@ static void read_freq_stats(int cpu) { new_total_cpu.freqs[i].time += new_cpus[cpu].freqs[i].time; } } - if (file) - fclose(file); + if (file) fclose(file); } /* * Get the sum of the cpu time from all categories. */ -static long unsigned get_cpu_total_time(struct cpu_info *cpu) { +static long unsigned get_cpu_total_time(struct cpu_info* cpu) { return (cpu->utime + cpu->ntime + cpu->stime + cpu->itime + cpu->iowtime + cpu->irqtime + cpu->sirqtime); } @@ -317,34 +319,26 @@ static void print_stats() { /* * Print the stats for a single CPU. */ -static void print_cpu_stats(char *label, struct cpu_info *new_cpu, struct cpu_info *old_cpu, - char print_freq) { +static void print_cpu_stats(char* label, struct cpu_info* new_cpu, struct cpu_info* old_cpu, + char print_freq) { long int total_delta_time; if (!minimal) { total_delta_time = get_cpu_total_time(new_cpu) - get_cpu_total_time(old_cpu); printf("%s: User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = " - "%ld\n", label, - new_cpu->utime - old_cpu->utime, - new_cpu->ntime - old_cpu->ntime, - new_cpu->stime - old_cpu->stime, - new_cpu->itime - old_cpu->itime, - new_cpu->iowtime - old_cpu->iowtime, - new_cpu->irqtime - old_cpu->irqtime, - new_cpu->sirqtime - old_cpu->sirqtime, - total_delta_time); + "%ld\n", + label, new_cpu->utime - old_cpu->utime, new_cpu->ntime - old_cpu->ntime, + new_cpu->stime - old_cpu->stime, new_cpu->itime - old_cpu->itime, + new_cpu->iowtime - old_cpu->iowtime, new_cpu->irqtime - old_cpu->irqtime, + new_cpu->sirqtime - old_cpu->sirqtime, total_delta_time); if (print_freq) { print_freq_stats(new_cpu, old_cpu); } } else { - printf("%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld", label, - new_cpu->utime - old_cpu->utime, - new_cpu->ntime - old_cpu->ntime, - new_cpu->stime - old_cpu->stime, - new_cpu->itime - old_cpu->itime, - new_cpu->iowtime - old_cpu->iowtime, - new_cpu->irqtime - old_cpu->irqtime, - new_cpu->sirqtime - old_cpu->sirqtime); + printf("%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld", label, new_cpu->utime - old_cpu->utime, + new_cpu->ntime - old_cpu->ntime, new_cpu->stime - old_cpu->stime, + new_cpu->itime - old_cpu->itime, new_cpu->iowtime - old_cpu->iowtime, + new_cpu->irqtime - old_cpu->irqtime, new_cpu->sirqtime - old_cpu->sirqtime); print_freq_stats(new_cpu, old_cpu); printf("\n"); } @@ -353,7 +347,7 @@ static void print_cpu_stats(char *label, struct cpu_info *new_cpu, struct cpu_in /* * Print the CPU stats for a single CPU. */ -static void print_freq_stats(struct cpu_info *new_cpu, struct cpu_info *old_cpu) { +static void print_freq_stats(struct cpu_info* new_cpu, struct cpu_info* old_cpu) { long int delta_time, total_delta_time; int i; @@ -375,7 +369,7 @@ static void print_freq_stats(struct cpu_info *new_cpu, struct cpu_info *old_cpu) } else { for (i = 0; i < new_cpu->freq_count; i++) { printf(",%u,%ld", new_cpu->freqs[i].freq, - new_cpu->freqs[i].time - old_cpu->freqs[i].time); + new_cpu->freqs[i].time - old_cpu->freqs[i].time); } } } @@ -394,7 +388,7 @@ static char should_print_freq_stats() { for (i = 1; i < cpu_count; i++) { for (j = 0; j < new_cpus[i].freq_count; j++) { if (new_cpus[i].freqs[j].time - old_cpus[i].freqs[j].time != - new_cpus[0].freqs[j].time - old_cpus[0].freqs[j].time) { + new_cpus[0].freqs[j].time - old_cpus[0].freqs[j].time) { return 1; } } @@ -429,8 +423,9 @@ static char should_aggregate_freq_stats() { /* * Print the usage message. */ -static void usage(char *cmd) { - fprintf(stderr, "Usage %s [ -n iterations ] [ -d delay ] [ -c cpu ] [ -m ] [ -h ]\n" +static void usage(char* cmd) { + fprintf(stderr, + "Usage %s [ -n iterations ] [ -d delay ] [ -c cpu ] [ -m ] [ -h ]\n" " -n num Updates to show before exiting.\n" " -d num Seconds to wait between updates.\n" " -m Display minimal output.\n" diff --git a/crypto-perf/crypto.cpp b/crypto-perf/crypto.cpp index c1860764..a7228324 100644 --- a/crypto-perf/crypto.cpp +++ b/crypto-perf/crypto.cpp @@ -1,11 +1,11 @@ +#include <ctype.h> +#include <sched.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include <sys/time.h> -#include <sched.h> #include <sys/resource.h> -#include <ctype.h> +#include <sys/time.h> +#include <unistd.h> #define USEC_PER_SEC 1000000ULL #define MAX_COUNT 1000000000ULL #define NUM_INSTS_GARBAGE 18 @@ -21,22 +21,21 @@ void usage() { printf("Usage:"); printf(" crypto [--cpu_to_lock CPU] [--locked_freq FREQ_IN_KHZ]\n\n"); printf("!!!!!!Lock the desired core to a desired frequency before invoking this benchmark.\n"); - printf( - "Hint: Set scaling_max_freq=scaling_min_freq=FREQ_IN_KHZ. FREQ_IN_KHZ " - "can be obtained from scaling_available_freq\n"); + printf("Hint: Set scaling_max_freq=scaling_min_freq=FREQ_IN_KHZ. FREQ_IN_KHZ " + "can be obtained from scaling_available_freq\n"); printf("--------------------------------------------------------------------------------\n"); } -int processOptions(int argc, char **argv, command_data_t *cmd_data) { +int processOptions(int argc, char** argv, command_data_t* cmd_data) { // Initialize the command_flags. cmd_data->cpu_to_lock = 0; cmd_data->locked_freq = 1; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { - int *save_value = NULL; + int* save_value = NULL; if (strcmp(argv[i], "--cpu_to_lock") == 0) { save_value = &cmd_data->cpu_to_lock; - } else if (strcmp(argv[i], "--locked_freq") == 0) { + } else if (strcmp(argv[i], "--locked_freq") == 0) { save_value = &cmd_data->locked_freq; } else { printf("Unknown option %s\n", argv[i]); @@ -47,14 +46,13 @@ int processOptions(int argc, char **argv, command_data_t *cmd_data) { // safe since as long as the argument exists, one character will // be present (\0). And if the first character is '-', then // there will always be a second character (\0 again). - if (i == argc - 1 || - (argv[i + 1][0] == '-' && !isdigit(argv[i + 1][1]))) { + if (i == argc - 1 || (argv[i + 1][0] == '-' && !isdigit(argv[i + 1][1]))) { printf("The option %s requires one argument.\n", argv[i]); return -1; } *save_value = (int)strtol(argv[++i], NULL, 0); } - } + } } return 0; } @@ -63,54 +61,53 @@ int processOptions(int argc, char **argv, command_data_t *cmd_data) { * higher performance when adjacent, and in the described order below. */ void garbage_encrypt() { __asm__ __volatile__( - "aese v0.16b, v4.16b ;" - "aesmc v0.16b, v0.16b ;" - "aese v1.16b, v4.16b ;" - "aesmc v1.16b, v1.16b ;" - "aese v2.16b, v4.16b ;" - "aesmc v2.16b, v2.16b ;" - "aese v0.16b, v5.16b ;" - "aesmc v0.16b, v0.16b ;" - "aese v1.16b, v5.16b ;" - "aesmc v1.16b, v1.16b ;" - "aese v2.16b, v5.16b ;" - "aesmc v2.16b, v2.16b ;" - "aese v0.16b, v6.16b ;" - "aesmc v0.16b, v0.16b ;" - "aese v1.16b, v6.16b ;" - "aesmc v1.16b, v1.16b ;" - "aese v2.16b, v6.16b ;" - "aesmc v2.16b, v2.16b ;"); + "aese v0.16b, v4.16b ;" + "aesmc v0.16b, v0.16b ;" + "aese v1.16b, v4.16b ;" + "aesmc v1.16b, v1.16b ;" + "aese v2.16b, v4.16b ;" + "aesmc v2.16b, v2.16b ;" + "aese v0.16b, v5.16b ;" + "aesmc v0.16b, v0.16b ;" + "aese v1.16b, v5.16b ;" + "aesmc v1.16b, v1.16b ;" + "aese v2.16b, v5.16b ;" + "aesmc v2.16b, v2.16b ;" + "aese v0.16b, v6.16b ;" + "aesmc v0.16b, v0.16b ;" + "aese v1.16b, v6.16b ;" + "aesmc v1.16b, v1.16b ;" + "aese v2.16b, v6.16b ;" + "aesmc v2.16b, v2.16b ;"); } void garbage_decrypt() { __asm__ __volatile__( - "aesd v0.16b, v4.16b ;" - "aesimc v0.16b, v0.16b ;" - "aesd v1.16b, v4.16b ;" - "aesimc v1.16b, v1.16b ;" - "aesd v2.16b, v4.16b ;" - "aesimc v2.16b, v2.16b ;" - "aesd v0.16b, v5.16b ;" - "aesimc v0.16b, v0.16b ;" - "aesd v1.16b, v5.16b ;" - "aesimc v1.16b, v1.16b ;" - "aesd v2.16b, v5.16b ;" - "aesimc v2.16b, v2.16b ;" - "aesd v0.16b, v6.16b ;" - "aesimc v0.16b, v0.16b ;" - "aesd v1.16b, v6.16b ;" - "aesimc v1.16b, v1.16b ;" - "aesd v2.16b, v6.16b ;" - "aesimc v2.16b, v2.16b ;"); + "aesd v0.16b, v4.16b ;" + "aesimc v0.16b, v0.16b ;" + "aesd v1.16b, v4.16b ;" + "aesimc v1.16b, v1.16b ;" + "aesd v2.16b, v4.16b ;" + "aesimc v2.16b, v2.16b ;" + "aesd v0.16b, v5.16b ;" + "aesimc v0.16b, v0.16b ;" + "aesd v1.16b, v5.16b ;" + "aesimc v1.16b, v1.16b ;" + "aesd v2.16b, v5.16b ;" + "aesimc v2.16b, v2.16b ;" + "aesd v0.16b, v6.16b ;" + "aesimc v0.16b, v0.16b ;" + "aesd v1.16b, v6.16b ;" + "aesimc v1.16b, v1.16b ;" + "aesd v2.16b, v6.16b ;" + "aesimc v2.16b, v2.16b ;"); } - -int main(int argc, char **argv) { +int main(int argc, char** argv) { usage(); command_data_t cmd_data; - if(processOptions(argc, argv, &cmd_data) == -1) { + if (processOptions(argc, argv, &cmd_data) == -1) { usage(); return -1; } @@ -120,51 +117,49 @@ int main(int argc, char **argv) { CPU_ZERO(&cpuset); CPU_SET(cmd_data.cpu_to_lock, &cpuset); if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) { - perror("sched_setaffinity failed"); - return 1; + perror("sched_setaffinity failed"); + return 1; } gettimeofday(&begin_time, NULL); while (count < MAX_COUNT) { - garbage_encrypt(); - count++; + garbage_encrypt(); + count++; } gettimeofday(&end_time, NULL); timersub(&end_time, &begin_time, &elapsed_time); fprintf(stderr, "encrypt: %llu us\n", elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec); - fprintf(stderr, "encrypt instructions: %llu\n", - MAX_COUNT * NUM_INSTS_GARBAGE); + fprintf(stderr, "encrypt instructions: %llu\n", MAX_COUNT * NUM_INSTS_GARBAGE); fprintf(stderr, "encrypt instructions per second: %f\n", (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / - (elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec)); + (elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec)); if (cmd_data.locked_freq != 0) { - fprintf(stderr, "encrypt instructions per cycle: %f\n", - (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / - ((elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec) * - 1000 * cmd_data.locked_freq)); + fprintf(stderr, "encrypt instructions per cycle: %f\n", + (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / + ((elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec) * 1000 * + cmd_data.locked_freq)); } printf("--------------------------------------------------------------------------------\n"); count = 0; gettimeofday(&begin_time, NULL); while (count < MAX_COUNT) { - garbage_decrypt(); - count++; + garbage_decrypt(); + count++; } gettimeofday(&end_time, NULL); timersub(&end_time, &begin_time, &elapsed_time); fprintf(stderr, "decrypt: %llu us\n", elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec); - fprintf(stderr, "decrypt instructions: %llu\n", - MAX_COUNT * NUM_INSTS_GARBAGE); + fprintf(stderr, "decrypt instructions: %llu\n", MAX_COUNT * NUM_INSTS_GARBAGE); fprintf(stderr, "decrypt instructions per second: %f\n", (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / - (elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec)); + (elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec)); if (cmd_data.locked_freq != 0) { - fprintf(stderr, "decrypt instructions per cycle: %f\n", - (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / - ((elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec) * - 1000 * cmd_data.locked_freq)); + fprintf(stderr, "decrypt instructions per cycle: %f\n", + (float)(MAX_COUNT * NUM_INSTS_GARBAGE * USEC_PER_SEC) / + ((elapsed_time.tv_sec * USEC_PER_SEC + elapsed_time.tv_usec) * 1000 * + cmd_data.locked_freq)); } return 0; } diff --git a/ext4_utils/blk_alloc_to_base_fs.cpp b/ext4_utils/blk_alloc_to_base_fs.cpp index 664648d1..65f60247 100644 --- a/ext4_utils/blk_alloc_to_base_fs.cpp +++ b/ext4_utils/blk_alloc_to_base_fs.cpp @@ -29,15 +29,13 @@ #endif #define ___STRING(x) __STRING(x) -static void usage(char *filename) -{ +static void usage(char* filename) { fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename); } -int main(int argc, char **argv) -{ +int main(int argc, char** argv) { FILE *blk_alloc_file = NULL, *base_fs_file = NULL; - char filename[MAX_PATH+1], file_version[MAX_FILE_VERSION+1], *spaced_allocs = NULL; + char filename[MAX_PATH + 1], file_version[MAX_FILE_VERSION + 1], *spaced_allocs = NULL; size_t spaced_allocs_len = 0; if (argc != 3) { @@ -54,7 +52,8 @@ int main(int argc, char **argv) fprintf(stderr, "failed to open %s: %s\n", argv[2], strerror(errno)); exit(EXIT_FAILURE); } - if (fscanf(blk_alloc_file, "Base EXT4 version %" ___STRING(MAX_FILE_VERSION) "s", file_version) > 0) { + if (fscanf(blk_alloc_file, "Base EXT4 version %" ___STRING(MAX_FILE_VERSION) "s", + file_version) > 0) { int c; printf("%s is already in *.base_fs format, just copying into %s...\n", argv[1], argv[2]); rewind(blk_alloc_file); @@ -67,7 +66,7 @@ int main(int argc, char **argv) rewind(blk_alloc_file); } fprintf(base_fs_file, "Base EXT4 version 1.0\n"); - while(fscanf(blk_alloc_file, "%" ___STRING(MAX_PATH) "s ", filename) != EOF) { + while (fscanf(blk_alloc_file, "%" ___STRING(MAX_PATH) "s ", filename) != EOF) { int i; fprintf(base_fs_file, "%s ", filename); if (getline(&spaced_allocs, &spaced_allocs_len, blk_alloc_file) == -1) { @@ -77,7 +76,8 @@ int main(int argc, char **argv) for (i = 0; spaced_allocs[i]; i++) { if (spaced_allocs[i] == ' ') { if (!isspace(spaced_allocs[i + 1])) fputc(',', base_fs_file); - } else fputc(spaced_allocs[i], base_fs_file); + } else + fputc(spaced_allocs[i], base_fs_file); } } free(spaced_allocs); diff --git a/ext4_utils/ext4_sb.cpp b/ext4_utils/ext4_sb.cpp index 4183b3aa..3d8f4c9e 100644 --- a/ext4_utils/ext4_sb.cpp +++ b/ext4_utils/ext4_sb.cpp @@ -18,30 +18,27 @@ #include "ext4_utils/ext4_sb.h" -int ext4_parse_sb(struct ext4_super_block *sb, struct fs_info *info) -{ - uint64_t len_blocks; +int ext4_parse_sb(struct ext4_super_block* sb, struct fs_info* info) { + uint64_t len_blocks; - if (sb->s_magic != EXT4_SUPER_MAGIC) - return -EINVAL; + if (sb->s_magic != EXT4_SUPER_MAGIC) return -EINVAL; - info->block_size = 1024 << sb->s_log_block_size; - info->blocks_per_group = sb->s_blocks_per_group; - info->inodes_per_group = sb->s_inodes_per_group; - info->inode_size = sb->s_inode_size; - info->inodes = sb->s_inodes_count; - info->feat_ro_compat = sb->s_feature_ro_compat; - info->feat_compat = sb->s_feature_compat; - info->feat_incompat = sb->s_feature_incompat; - info->bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks; - info->bg_desc_size = - (sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) ? - sb->s_desc_size : EXT4_MIN_DESC_SIZE; - info->label = sb->s_volume_name; + info->block_size = 1024 << sb->s_log_block_size; + info->blocks_per_group = sb->s_blocks_per_group; + info->inodes_per_group = sb->s_inodes_per_group; + info->inode_size = sb->s_inode_size; + info->inodes = sb->s_inodes_count; + info->feat_ro_compat = sb->s_feature_ro_compat; + info->feat_compat = sb->s_feature_compat; + info->feat_incompat = sb->s_feature_incompat; + info->bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks; + info->bg_desc_size = (sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) + ? sb->s_desc_size + : EXT4_MIN_DESC_SIZE; + info->label = sb->s_volume_name; - len_blocks = ((uint64_t)sb->s_blocks_count_hi << 32) + - sb->s_blocks_count_lo; - info->len = (uint64_t)info->block_size * len_blocks; + len_blocks = ((uint64_t)sb->s_blocks_count_hi << 32) + sb->s_blocks_count_lo; + info->len = (uint64_t)info->block_size * len_blocks; - return 0; + return 0; } diff --git a/ext4_utils/ext4_utils.cpp b/ext4_utils/ext4_utils.cpp index 234ceeb6..632d8292 100644 --- a/ext4_utils/ext4_utils.cpp +++ b/ext4_utils/ext4_utils.cpp @@ -45,310 +45,270 @@ struct fs_aux_info aux_info; jmp_buf setjmp_env; /* returns 1 if a is a power of b */ -static int is_power_of(int a, int b) -{ - while (a > b) { - if (a % b) - return 0; - a /= b; - } - - return (a == b) ? 1 : 0; +static int is_power_of(int a, int b) { + while (a > b) { + if (a % b) return 0; + a /= b; + } + + return (a == b) ? 1 : 0; } -int bitmap_get_bit(u8 *bitmap, u32 bit) -{ - if (bitmap[bit / 8] & (1 << (bit % 8))) - return 1; +int bitmap_get_bit(u8* bitmap, u32 bit) { + if (bitmap[bit / 8] & (1 << (bit % 8))) return 1; - return 0; + return 0; } -void bitmap_clear_bit(u8 *bitmap, u32 bit) -{ - bitmap[bit / 8] &= ~(1 << (bit % 8)); +void bitmap_clear_bit(u8* bitmap, u32 bit) { + bitmap[bit / 8] &= ~(1 << (bit % 8)); - return; + return; } /* Returns 1 if the bg contains a backup superblock. On filesystems with the sparse_super feature, only block groups 0, 1, and powers of 3, 5, and 7 have backup superblocks. Otherwise, all block groups have backup superblocks */ -int ext4_bg_has_super_block(int bg) -{ - /* Without sparse_super, every block group has a superblock */ - if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) - return 1; +int ext4_bg_has_super_block(int bg) { + /* Without sparse_super, every block group has a superblock */ + if (!(info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) return 1; - if (bg == 0 || bg == 1) - return 1; + if (bg == 0 || bg == 1) return 1; - if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) - return 1; + if (is_power_of(bg, 3) || is_power_of(bg, 5) || is_power_of(bg, 7)) return 1; - return 0; + return 0; } /* Function to read the primary superblock */ -void read_sb(int fd, struct ext4_super_block *sb) -{ - off64_t ret; - - ret = lseek64(fd, 1024, SEEK_SET); - if (ret < 0) - critical_error_errno("failed to seek to superblock"); - - ret = read(fd, sb, sizeof(*sb)); - if (ret < 0) - critical_error_errno("failed to read superblock"); - if (ret != sizeof(*sb)) - critical_error("failed to read all of superblock"); +void read_sb(int fd, struct ext4_super_block* sb) { + off64_t ret; + + ret = lseek64(fd, 1024, SEEK_SET); + if (ret < 0) critical_error_errno("failed to seek to superblock"); + + ret = read(fd, sb, sizeof(*sb)); + if (ret < 0) critical_error_errno("failed to read superblock"); + if (ret != sizeof(*sb)) critical_error("failed to read all of superblock"); } /* Compute the rest of the parameters of the filesystem from the basic info */ -void ext4_create_fs_aux_info() -{ - aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; - aux_info.len_blocks = info.len / info.block_size; - aux_info.inode_table_blocks = DIV_ROUND_UP(info.inodes_per_group * info.inode_size, - info.block_size); - aux_info.groups = DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, - info.blocks_per_group); - aux_info.blocks_per_ind = info.block_size / sizeof(u32); - aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; - aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; - - aux_info.bg_desc_blocks = - DIV_ROUND_UP(aux_info.groups * (size_t)info.bg_desc_size, info.block_size); - - aux_info.default_i_flags = EXT4_NOATIME_FL; - - u32 last_group_size = aux_info.len_blocks == info.blocks_per_group - ? aux_info.len_blocks : aux_info.len_blocks % info.blocks_per_group; - u32 last_header_size = 2 + aux_info.inode_table_blocks; - if (ext4_bg_has_super_block((int)aux_info.groups - 1)) - last_header_size += 1 + aux_info.bg_desc_blocks + - info.bg_desc_reserve_blocks; - if (aux_info.groups <= 1 && last_group_size < last_header_size) { - critical_error("filesystem size too small"); - } - if (last_group_size > 0 && last_group_size < last_header_size) { - aux_info.groups--; - aux_info.len_blocks -= last_group_size; - } - - /* A zero-filled superblock to be written firstly to the block - * device to mark the file-system as invalid - */ - aux_info.sb_zero = (struct ext4_super_block *)calloc(1, info.block_size); - if (!aux_info.sb_zero) - critical_error_errno("calloc"); - - /* The write_data* functions expect only block aligned calls. - * This is not an issue, except when we write out the super - * block on a system with a block size > 1K. So, we need to - * deal with that here. - */ - aux_info.sb_block = (struct ext4_super_block *)calloc(1, info.block_size); - if (!aux_info.sb_block) - critical_error_errno("calloc"); - - if (info.block_size > 1024) - aux_info.sb = (struct ext4_super_block *)((char *)aux_info.sb_block + 1024); - else - aux_info.sb = aux_info.sb_block; - - /* Alloc an array to hold the pointers to the backup superblocks */ - aux_info.backup_sb = (struct ext4_super_block **)calloc(aux_info.groups, sizeof(char *)); - - if (!aux_info.sb) - critical_error_errno("calloc"); - - aux_info.bg_desc = - (struct ext2_group_desc *)calloc(aux_info.groups, sizeof(struct ext2_group_desc)); - if (!aux_info.bg_desc) - critical_error_errno("calloc"); - aux_info.xattrs = NULL; +void ext4_create_fs_aux_info() { + aux_info.first_data_block = (info.block_size > 1024) ? 0 : 1; + aux_info.len_blocks = info.len / info.block_size; + aux_info.inode_table_blocks = + DIV_ROUND_UP(info.inodes_per_group * info.inode_size, info.block_size); + aux_info.groups = + DIV_ROUND_UP(aux_info.len_blocks - aux_info.first_data_block, info.blocks_per_group); + aux_info.blocks_per_ind = info.block_size / sizeof(u32); + aux_info.blocks_per_dind = aux_info.blocks_per_ind * aux_info.blocks_per_ind; + aux_info.blocks_per_tind = aux_info.blocks_per_dind * aux_info.blocks_per_dind; + + aux_info.bg_desc_blocks = + DIV_ROUND_UP(aux_info.groups * (size_t)info.bg_desc_size, info.block_size); + + aux_info.default_i_flags = EXT4_NOATIME_FL; + + u32 last_group_size = aux_info.len_blocks == info.blocks_per_group + ? aux_info.len_blocks + : aux_info.len_blocks % info.blocks_per_group; + u32 last_header_size = 2 + aux_info.inode_table_blocks; + if (ext4_bg_has_super_block((int)aux_info.groups - 1)) + last_header_size += 1 + aux_info.bg_desc_blocks + info.bg_desc_reserve_blocks; + if (aux_info.groups <= 1 && last_group_size < last_header_size) { + critical_error("filesystem size too small"); + } + if (last_group_size > 0 && last_group_size < last_header_size) { + aux_info.groups--; + aux_info.len_blocks -= last_group_size; + } + + /* A zero-filled superblock to be written firstly to the block + * device to mark the file-system as invalid + */ + aux_info.sb_zero = (struct ext4_super_block*)calloc(1, info.block_size); + if (!aux_info.sb_zero) critical_error_errno("calloc"); + + /* The write_data* functions expect only block aligned calls. + * This is not an issue, except when we write out the super + * block on a system with a block size > 1K. So, we need to + * deal with that here. + */ + aux_info.sb_block = (struct ext4_super_block*)calloc(1, info.block_size); + if (!aux_info.sb_block) critical_error_errno("calloc"); + + if (info.block_size > 1024) + aux_info.sb = (struct ext4_super_block*)((char*)aux_info.sb_block + 1024); + else + aux_info.sb = aux_info.sb_block; + + /* Alloc an array to hold the pointers to the backup superblocks */ + aux_info.backup_sb = (struct ext4_super_block**)calloc(aux_info.groups, sizeof(char*)); + + if (!aux_info.sb) critical_error_errno("calloc"); + + aux_info.bg_desc = + (struct ext2_group_desc*)calloc(aux_info.groups, sizeof(struct ext2_group_desc)); + if (!aux_info.bg_desc) critical_error_errno("calloc"); + aux_info.xattrs = NULL; } -void ext4_free_fs_aux_info() -{ - unsigned int i; - - for (i=0; i<aux_info.groups; i++) { - if (aux_info.backup_sb[i]) - free(aux_info.backup_sb[i]); - } - free(aux_info.sb_block); - free(aux_info.sb_zero); - free(aux_info.bg_desc); +void ext4_free_fs_aux_info() { + unsigned int i; + + for (i = 0; i < aux_info.groups; i++) { + if (aux_info.backup_sb[i]) free(aux_info.backup_sb[i]); + } + free(aux_info.sb_block); + free(aux_info.sb_zero); + free(aux_info.bg_desc); } -void ext4_parse_sb_info(struct ext4_super_block *sb) -{ - if (sb->s_magic != EXT4_SUPER_MAGIC) - error("superblock magic incorrect"); +void ext4_parse_sb_info(struct ext4_super_block* sb) { + if (sb->s_magic != EXT4_SUPER_MAGIC) error("superblock magic incorrect"); - if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS) - error("filesystem state not valid"); + if ((sb->s_state & EXT4_VALID_FS) != EXT4_VALID_FS) error("filesystem state not valid"); - ext4_parse_sb(sb, &info); + ext4_parse_sb(sb, &info); - ext4_create_fs_aux_info(); + ext4_create_fs_aux_info(); - memcpy(aux_info.sb, sb, sizeof(*sb)); + memcpy(aux_info.sb, sb, sizeof(*sb)); - if (aux_info.first_data_block != sb->s_first_data_block) - critical_error("first data block does not match"); + if (aux_info.first_data_block != sb->s_first_data_block) + critical_error("first data block does not match"); } -u64 get_block_device_size(int fd) -{ - u64 size = 0; - int ret; +u64 get_block_device_size(int fd) { + u64 size = 0; + int ret; #if defined(__linux__) - ret = ioctl(fd, BLKGETSIZE64, &size); + ret = ioctl(fd, BLKGETSIZE64, &size); #elif defined(__APPLE__) && defined(__MACH__) - ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); + ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size); #else - close(fd); - return 0; + close(fd); + return 0; #endif - if (ret) - return 0; + if (ret) return 0; - return size; + return size; } -int is_block_device_fd(int fd __attribute__((unused))) -{ +int is_block_device_fd(int fd __attribute__((unused))) { #ifdef _WIN32 - return 0; + return 0; #else - struct stat st; - int ret = fstat(fd, &st); - if (ret < 0) - return 0; + struct stat st; + int ret = fstat(fd, &st); + if (ret < 0) return 0; - return S_ISBLK(st.st_mode); + return S_ISBLK(st.st_mode); #endif } -u64 get_file_size(int fd) -{ - struct stat buf; - int ret; - u64 reserve_len = 0; - s64 computed_size; - - ret = fstat(fd, &buf); - if (ret) - return 0; - - if (info.len < 0) - reserve_len = -info.len; - - if (S_ISREG(buf.st_mode)) - computed_size = buf.st_size - reserve_len; - else if (S_ISBLK(buf.st_mode)) - computed_size = get_block_device_size(fd) - reserve_len; - else - computed_size = 0; - - if (computed_size < 0) { - warn("Computed filesystem size less than 0"); - computed_size = 0; - } - - return computed_size; -} +u64 get_file_size(int fd) { + struct stat buf; + int ret; + u64 reserve_len = 0; + s64 computed_size; -static void read_block_group_descriptors(int fd) -{ - size_t size = info.block_size * (size_t)aux_info.bg_desc_blocks; - void *buf = malloc(size); - ssize_t ret; - - if (!buf) - critical_error("failed to alloc buffer"); - - ret = read(fd, buf, size); - if (ret < 0) { - free(buf); - critical_error_errno("failed to read block group descriptors"); - } - if (ret != size) { - free(buf); - critical_error("failed to read all the block group descriptors"); - } - const struct ext4_group_desc *gdp = (const struct ext4_group_desc *)buf; - bool extended = (info.bg_desc_size >= EXT4_MIN_DESC_SIZE_64BIT); - for (size_t i = 0; i < aux_info.groups; i++) { - aux_info.bg_desc[i].bg_block_bitmap = - (extended ? (u64)gdp->bg_block_bitmap_hi << 32 : 0) | - gdp->bg_block_bitmap_lo; - aux_info.bg_desc[i].bg_inode_bitmap = - (extended ? (u64)gdp->bg_inode_bitmap_hi << 32 : 0) | - gdp->bg_inode_bitmap_lo; - aux_info.bg_desc[i].bg_inode_table = - (extended ? (u64)gdp->bg_inode_table_hi << 32 : 0) | - gdp->bg_inode_table_lo; - aux_info.bg_desc[i].bg_free_blocks_count = - (extended ? (u32)gdp->bg_free_blocks_count_hi << 16 : 0) | - gdp->bg_free_blocks_count_lo; - aux_info.bg_desc[i].bg_free_inodes_count = - (extended ? (u32)gdp->bg_free_inodes_count_hi << 16 : 0) | - gdp->bg_free_inodes_count_lo; - aux_info.bg_desc[i].bg_used_dirs_count = - (extended ? (u32)gdp->bg_used_dirs_count_hi << 16 : 0) | - gdp->bg_used_dirs_count_lo; - aux_info.bg_desc[i].bg_flags = gdp->bg_flags; - gdp = (const struct ext4_group_desc *)((u8 *)gdp + info.bg_desc_size); - } - free(buf); + ret = fstat(fd, &buf); + if (ret) return 0; + + if (info.len < 0) reserve_len = -info.len; + + if (S_ISREG(buf.st_mode)) + computed_size = buf.st_size - reserve_len; + else if (S_ISBLK(buf.st_mode)) + computed_size = get_block_device_size(fd) - reserve_len; + else + computed_size = 0; + + if (computed_size < 0) { + warn("Computed filesystem size less than 0"); + computed_size = 0; + } + + return computed_size; } -int read_ext(int fd, int verbose) -{ - off64_t ret; - struct ext4_super_block sb; - - read_sb(fd, &sb); - - ext4_parse_sb_info(&sb); - - ret = lseek64(fd, info.len, SEEK_SET); - if (ret < 0) - critical_error_errno("failed to seek to end of input image"); - - ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET); - if (ret < 0) - critical_error_errno("failed to seek to block group descriptors"); - - read_block_group_descriptors(fd); - - if (verbose) { - printf("Found filesystem with parameters:\n"); - printf(" Size: %" PRIu64 "\n", info.len); - printf(" Block size: %d\n", info.block_size); - printf(" Blocks per group: %d\n", info.blocks_per_group); - printf(" Inodes per group: %d\n", info.inodes_per_group); - printf(" Inode size: %d\n", info.inode_size); - printf(" Label: %s\n", info.label); - printf(" Blocks: %" PRIext4u64 "\n", aux_info.len_blocks); - printf(" Block groups: %d\n", aux_info.groups); - printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); - printf(" Block group descriptor size: %d\n", info.bg_desc_size); - printf(" Used %d/%d inodes and %d/%d blocks\n", - aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, - aux_info.sb->s_inodes_count, - aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, - aux_info.sb->s_blocks_count_lo); - } - - return 0; +static void read_block_group_descriptors(int fd) { + size_t size = info.block_size * (size_t)aux_info.bg_desc_blocks; + void* buf = malloc(size); + ssize_t ret; + + if (!buf) critical_error("failed to alloc buffer"); + + ret = read(fd, buf, size); + if (ret < 0) { + free(buf); + critical_error_errno("failed to read block group descriptors"); + } + if (ret != size) { + free(buf); + critical_error("failed to read all the block group descriptors"); + } + const struct ext4_group_desc* gdp = (const struct ext4_group_desc*)buf; + bool extended = (info.bg_desc_size >= EXT4_MIN_DESC_SIZE_64BIT); + for (size_t i = 0; i < aux_info.groups; i++) { + aux_info.bg_desc[i].bg_block_bitmap = + (extended ? (u64)gdp->bg_block_bitmap_hi << 32 : 0) | gdp->bg_block_bitmap_lo; + aux_info.bg_desc[i].bg_inode_bitmap = + (extended ? (u64)gdp->bg_inode_bitmap_hi << 32 : 0) | gdp->bg_inode_bitmap_lo; + aux_info.bg_desc[i].bg_inode_table = + (extended ? (u64)gdp->bg_inode_table_hi << 32 : 0) | gdp->bg_inode_table_lo; + aux_info.bg_desc[i].bg_free_blocks_count = + (extended ? (u32)gdp->bg_free_blocks_count_hi << 16 : 0) | + gdp->bg_free_blocks_count_lo; + aux_info.bg_desc[i].bg_free_inodes_count = + (extended ? (u32)gdp->bg_free_inodes_count_hi << 16 : 0) | + gdp->bg_free_inodes_count_lo; + aux_info.bg_desc[i].bg_used_dirs_count = + (extended ? (u32)gdp->bg_used_dirs_count_hi << 16 : 0) | gdp->bg_used_dirs_count_lo; + aux_info.bg_desc[i].bg_flags = gdp->bg_flags; + gdp = (const struct ext4_group_desc*)((u8*)gdp + info.bg_desc_size); + } + free(buf); } +int read_ext(int fd, int verbose) { + off64_t ret; + struct ext4_super_block sb; + + read_sb(fd, &sb); + + ext4_parse_sb_info(&sb); + + ret = lseek64(fd, info.len, SEEK_SET); + if (ret < 0) critical_error_errno("failed to seek to end of input image"); + + ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET); + if (ret < 0) critical_error_errno("failed to seek to block group descriptors"); + + read_block_group_descriptors(fd); + + if (verbose) { + printf("Found filesystem with parameters:\n"); + printf(" Size: %" PRIu64 "\n", info.len); + printf(" Block size: %d\n", info.block_size); + printf(" Blocks per group: %d\n", info.blocks_per_group); + printf(" Inodes per group: %d\n", info.inodes_per_group); + printf(" Inode size: %d\n", info.inode_size); + printf(" Label: %s\n", info.label); + printf(" Blocks: %" PRIext4u64 "\n", aux_info.len_blocks); + printf(" Block groups: %d\n", aux_info.groups); + printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); + printf(" Block group descriptor size: %d\n", info.bg_desc_size); + printf(" Used %d/%d inodes and %d/%d blocks\n", + aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, + aux_info.sb->s_inodes_count, + aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, + aux_info.sb->s_blocks_count_lo); + } + + return 0; +} diff --git a/ext4_utils/helpers.h b/ext4_utils/helpers.h index 09b7b457..c7509ec6 100644 --- a/ext4_utils/helpers.h +++ b/ext4_utils/helpers.h @@ -18,8 +18,19 @@ #include <stdio.h> #include <string.h> -#define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0) -#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) longjmp(setjmp_env, EXIT_FAILURE); } while (0) +#define warn(fmt, args...) \ + do { \ + fprintf(stderr, "warning: %s: " fmt "\n", __func__, ##args); \ + } while (0) +#define error(fmt, args...) \ + do { \ + fprintf(stderr, "error: %s: " fmt "\n", __func__, ##args); \ + if (!force) longjmp(setjmp_env, EXIT_FAILURE); \ + } while (0) #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno)) -#define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); longjmp(setjmp_env, EXIT_FAILURE); } while (0) +#define critical_error(fmt, args...) \ + do { \ + fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ##args); \ + longjmp(setjmp_env, EXIT_FAILURE); \ + } while (0) #define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno)) diff --git a/ext4_utils/include/ext4_utils/ext4.h b/ext4_utils/include/ext4_utils/ext4.h index 4bb1031f..54e5d86a 100644 --- a/ext4_utils/include/ext4_utils/ext4.h +++ b/ext4_utils/include/ext4_utils/ext4.h @@ -17,14 +17,20 @@ #undef EXT4FS_DEBUG #ifdef EXT4FS_DEBUG -#define ext4_debug(f, a...) do { printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:", __FILE__, __LINE__, __func__); printk(KERN_DEBUG f, ## a); } while (0) +#define ext4_debug(f, a...) \ + do { \ + printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:", __FILE__, __LINE__, __func__); \ + printk(KERN_DEBUG f, ##a); \ + } while (0) #else -#define ext4_debug(f, a...) do {} while (0) +#define ext4_debug(f, a...) \ + do { \ + } while (0) #endif -#define EXT4_ERROR_INODE(inode, fmt, a...) ext4_error_inode(__func__, (inode), (fmt), ## a); +#define EXT4_ERROR_INODE(inode, fmt, a...) ext4_error_inode(__func__, (inode), (fmt), ##a); -#define EXT4_ERROR_FILE(file, fmt, a...) ext4_error_file(__func__, (file), (fmt), ## a); +#define EXT4_ERROR_FILE(file, fmt, a...) ext4_error_file(__func__, (file), (fmt), ##a); typedef int ext4_grpblk_t; @@ -59,32 +65,31 @@ typedef unsigned int ext4_group_t; #define EXT4_MB_STREAM_ALLOC 0x0800 struct ext4_allocation_request { + struct inode* inode; - struct inode *inode; + unsigned int len; - unsigned int len; + ext4_lblk_t logical; - ext4_lblk_t logical; + ext4_lblk_t lleft; - ext4_lblk_t lleft; + ext4_lblk_t lright; - ext4_lblk_t lright; + ext4_fsblk_t goal; - ext4_fsblk_t goal; + ext4_fsblk_t pleft; - ext4_fsblk_t pleft; + ext4_fsblk_t pright; - ext4_fsblk_t pright; - - unsigned int flags; + unsigned int flags; }; -#define EXT4_BAD_INO 1 -#define EXT4_ROOT_INO 2 -#define EXT4_BOOT_LOADER_INO 5 -#define EXT4_UNDEL_DIR_INO 6 -#define EXT4_RESIZE_INO 7 -#define EXT4_JOURNAL_INO 8 +#define EXT4_BAD_INO 1 +#define EXT4_ROOT_INO 2 +#define EXT4_BOOT_LOADER_INO 5 +#define EXT4_UNDEL_DIR_INO 6 +#define EXT4_RESIZE_INO 7 +#define EXT4_JOURNAL_INO 8 #define EXT4_GOOD_OLD_FIRST_INO 11 @@ -96,35 +101,36 @@ struct ext4_allocation_request { #define EXT4_BLOCK_SIZE(s) (EXT4_MIN_BLOCK_SIZE << (s)->s_log_block_size) #define EXT4_ADDR_PER_BLOCK(s) (EXT4_BLOCK_SIZE(s) / sizeof(__u32)) #define EXT4_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10) -#define EXT4_INODE_SIZE(s) (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? EXT4_GOOD_OLD_INODE_SIZE : (s)->s_inode_size) -#define EXT4_FIRST_INO(s) (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? EXT4_GOOD_OLD_FIRST_INO : (s)->s_first_ino) +#define EXT4_INODE_SIZE(s) \ + (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? EXT4_GOOD_OLD_INODE_SIZE : (s)->s_inode_size) +#define EXT4_FIRST_INO(s) \ + (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? EXT4_GOOD_OLD_FIRST_INO : (s)->s_first_ino) #define EXT4_BLOCK_ALIGN(size, blkbits) EXT4_ALIGN((size), (1 << (blkbits))) -struct ext4_group_desc -{ - __le32 bg_block_bitmap_lo; - __le32 bg_inode_bitmap_lo; - __le32 bg_inode_table_lo; - __le16 bg_free_blocks_count_lo; - __le16 bg_free_inodes_count_lo; - __le16 bg_used_dirs_count_lo; - __le16 bg_flags; - __u32 bg_reserved[2]; - __le16 bg_itable_unused_lo; - __le16 bg_checksum; - __le32 bg_block_bitmap_hi; - __le32 bg_inode_bitmap_hi; - __le32 bg_inode_table_hi; - __le16 bg_free_blocks_count_hi; - __le16 bg_free_inodes_count_hi; - __le16 bg_used_dirs_count_hi; - __le16 bg_itable_unused_hi; - __u32 bg_reserved2[3]; +struct ext4_group_desc { + __le32 bg_block_bitmap_lo; + __le32 bg_inode_bitmap_lo; + __le32 bg_inode_table_lo; + __le16 bg_free_blocks_count_lo; + __le16 bg_free_inodes_count_lo; + __le16 bg_used_dirs_count_lo; + __le16 bg_flags; + __u32 bg_reserved[2]; + __le16 bg_itable_unused_lo; + __le16 bg_checksum; + __le32 bg_block_bitmap_hi; + __le32 bg_inode_bitmap_hi; + __le32 bg_inode_table_hi; + __le16 bg_free_blocks_count_hi; + __le16 bg_free_inodes_count_hi; + __le16 bg_used_dirs_count_hi; + __le16 bg_itable_unused_hi; + __u32 bg_reserved2[3]; }; -#define EXT4_BG_INODE_UNINIT 0x0001 -#define EXT4_BG_BLOCK_UNINIT 0x0002 -#define EXT4_BG_INODE_ZEROED 0x0004 +#define EXT4_BG_INODE_UNINIT 0x0001 +#define EXT4_BG_BLOCK_UNINIT 0x0002 +#define EXT4_BG_INODE_ZEROED 0x0004 #define EXT4_MIN_DESC_SIZE 32 #define EXT4_MIN_DESC_SIZE_64BIT 64 @@ -140,64 +146,67 @@ struct ext4_group_desc #define EXT4_TIND_BLOCK (EXT4_DIND_BLOCK + 1) #define EXT4_N_BLOCKS (EXT4_TIND_BLOCK + 1) -#define EXT4_SECRM_FL 0x00000001 -#define EXT4_UNRM_FL 0x00000002 -#define EXT4_COMPR_FL 0x00000004 -#define EXT4_SYNC_FL 0x00000008 -#define EXT4_IMMUTABLE_FL 0x00000010 -#define EXT4_APPEND_FL 0x00000020 -#define EXT4_NODUMP_FL 0x00000040 -#define EXT4_NOATIME_FL 0x00000080 +#define EXT4_SECRM_FL 0x00000001 +#define EXT4_UNRM_FL 0x00000002 +#define EXT4_COMPR_FL 0x00000004 +#define EXT4_SYNC_FL 0x00000008 +#define EXT4_IMMUTABLE_FL 0x00000010 +#define EXT4_APPEND_FL 0x00000020 +#define EXT4_NODUMP_FL 0x00000040 +#define EXT4_NOATIME_FL 0x00000080 #define EXT4_DIRTY_FL 0x00000100 -#define EXT4_COMPRBLK_FL 0x00000200 -#define EXT4_NOCOMPR_FL 0x00000400 -#define EXT4_ECOMPR_FL 0x00000800 - -#define EXT4_INDEX_FL 0x00001000 -#define EXT4_IMAGIC_FL 0x00002000 -#define EXT4_JOURNAL_DATA_FL 0x00004000 -#define EXT4_NOTAIL_FL 0x00008000 -#define EXT4_DIRSYNC_FL 0x00010000 -#define EXT4_TOPDIR_FL 0x00020000 -#define EXT4_HUGE_FILE_FL 0x00040000 -#define EXT4_EXTENTS_FL 0x00080000 -#define EXT4_EA_INODE_FL 0x00200000 -#define EXT4_EOFBLOCKS_FL 0x00400000 -#define EXT4_RESERVED_FL 0x80000000 - -#define EXT4_FL_USER_VISIBLE 0x004BDFFF -#define EXT4_FL_USER_MODIFIABLE 0x004B80FF - -#define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL | EXT4_SYNC_FL | EXT4_IMMUTABLE_FL | EXT4_APPEND_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL | EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL | EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL) +#define EXT4_COMPRBLK_FL 0x00000200 +#define EXT4_NOCOMPR_FL 0x00000400 +#define EXT4_ECOMPR_FL 0x00000800 + +#define EXT4_INDEX_FL 0x00001000 +#define EXT4_IMAGIC_FL 0x00002000 +#define EXT4_JOURNAL_DATA_FL 0x00004000 +#define EXT4_NOTAIL_FL 0x00008000 +#define EXT4_DIRSYNC_FL 0x00010000 +#define EXT4_TOPDIR_FL 0x00020000 +#define EXT4_HUGE_FILE_FL 0x00040000 +#define EXT4_EXTENTS_FL 0x00080000 +#define EXT4_EA_INODE_FL 0x00200000 +#define EXT4_EOFBLOCKS_FL 0x00400000 +#define EXT4_RESERVED_FL 0x80000000 + +#define EXT4_FL_USER_VISIBLE 0x004BDFFF +#define EXT4_FL_USER_MODIFIABLE 0x004B80FF + +#define EXT4_FL_INHERITED \ + (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL | EXT4_SYNC_FL | EXT4_IMMUTABLE_FL | \ + EXT4_APPEND_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL | EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL | \ + EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL) #define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL)) #define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL) struct ext4_new_group_data { - __u32 group; - __u64 block_bitmap; - __u64 inode_bitmap; - __u64 inode_table; - __u32 blocks_count; - __u16 reserved_blocks; - __u16 unused; - __u32 free_blocks_count; + __u32 group; + __u64 block_bitmap; + __u64 inode_bitmap; + __u64 inode_table; + __u32 blocks_count; + __u16 reserved_blocks; + __u16 unused; + __u32 free_blocks_count; }; #define EXT4_GET_BLOCKS_CREATE 0x0001 #define EXT4_GET_BLOCKS_UNINIT_EXT 0x0002 -#define EXT4_GET_BLOCKS_CREATE_UNINIT_EXT (EXT4_GET_BLOCKS_UNINIT_EXT| EXT4_GET_BLOCKS_CREATE) +#define EXT4_GET_BLOCKS_CREATE_UNINIT_EXT (EXT4_GET_BLOCKS_UNINIT_EXT | EXT4_GET_BLOCKS_CREATE) #define EXT4_GET_BLOCKS_DELALLOC_RESERVE 0x0004 #define EXT4_GET_BLOCKS_PRE_IO 0x0008 #define EXT4_GET_BLOCKS_CONVERT 0x0010 -#define EXT4_GET_BLOCKS_IO_CREATE_EXT (EXT4_GET_BLOCKS_PRE_IO| EXT4_GET_BLOCKS_CREATE_UNINIT_EXT) +#define EXT4_GET_BLOCKS_IO_CREATE_EXT (EXT4_GET_BLOCKS_PRE_IO | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT) -#define EXT4_GET_BLOCKS_IO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT| EXT4_GET_BLOCKS_CREATE_UNINIT_EXT) +#define EXT4_GET_BLOCKS_IO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT) #define EXT4_FREE_BLOCKS_METADATA 0x0001 #define EXT4_FREE_BLOCKS_FORGET 0x0002 @@ -231,83 +240,107 @@ struct ext4_new_group_data { #define EXT4_MAX_BLOCK_FILE_PHYS 0xFFFFFFFF struct ext4_inode { - __le16 i_mode; - __le16 i_uid; - __le32 i_size_lo; - __le32 i_atime; - __le32 i_ctime; - __le32 i_mtime; - __le32 i_dtime; - __le16 i_gid; - __le16 i_links_count; - __le32 i_blocks_lo; - __le32 i_flags; - union { - struct { - __le32 l_i_version; - } linux1; - struct { - __u32 h_i_translator; - } hurd1; - struct { - __u32 m_i_reserved1; - } masix1; - } osd1; - __le32 i_block[EXT4_N_BLOCKS]; - __le32 i_generation; - __le32 i_file_acl_lo; - __le32 i_size_high; - __le32 i_obso_faddr; - union { - struct { - __le16 l_i_blocks_high; - __le16 l_i_file_acl_high; - __le16 l_i_uid_high; - __le16 l_i_gid_high; - __u32 l_i_reserved2; - } linux2; - struct { - __le16 h_i_reserved1; - __u16 h_i_mode_high; - __u16 h_i_uid_high; - __u16 h_i_gid_high; - __u32 h_i_author; - } hurd2; - struct { - __le16 h_i_reserved1; - __le16 m_i_file_acl_high; - __u32 m_i_reserved2[2]; - } masix2; - } osd2; - __le16 i_extra_isize; - __le16 i_pad1; - __le32 i_ctime_extra; - __le32 i_mtime_extra; - __le32 i_atime_extra; - __le32 i_crtime; - __le32 i_crtime_extra; - __le32 i_version_hi; + __le16 i_mode; + __le16 i_uid; + __le32 i_size_lo; + __le32 i_atime; + __le32 i_ctime; + __le32 i_mtime; + __le32 i_dtime; + __le16 i_gid; + __le16 i_links_count; + __le32 i_blocks_lo; + __le32 i_flags; + union { + struct { + __le32 l_i_version; + } linux1; + struct { + __u32 h_i_translator; + } hurd1; + struct { + __u32 m_i_reserved1; + } masix1; + } osd1; + __le32 i_block[EXT4_N_BLOCKS]; + __le32 i_generation; + __le32 i_file_acl_lo; + __le32 i_size_high; + __le32 i_obso_faddr; + union { + struct { + __le16 l_i_blocks_high; + __le16 l_i_file_acl_high; + __le16 l_i_uid_high; + __le16 l_i_gid_high; + __u32 l_i_reserved2; + } linux2; + struct { + __le16 h_i_reserved1; + __u16 h_i_mode_high; + __u16 h_i_uid_high; + __u16 h_i_gid_high; + __u32 h_i_author; + } hurd2; + struct { + __le16 h_i_reserved1; + __le16 m_i_file_acl_high; + __u32 m_i_reserved2[2]; + } masix2; + } osd2; + __le16 i_extra_isize; + __le16 i_pad1; + __le32 i_ctime_extra; + __le32 i_mtime_extra; + __le32 i_atime_extra; + __le32 i_crtime; + __le32 i_crtime_extra; + __le32 i_version_hi; }; struct move_extent { - __u32 reserved; - __u32 donor_fd; - __u64 orig_start; - __u64 donor_start; - __u64 len; - __u64 moved_len; + __u32 reserved; + __u32 donor_fd; + __u64 orig_start; + __u64 donor_start; + __u64 len; + __u64 moved_len; }; #define EXT4_EPOCH_BITS 2 #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1) #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS) -#define EXT4_FITS_IN_INODE(ext4_inode, einode, field) ((offsetof(typeof(*(ext4_inode)), field) + sizeof((ext4_inode)->field)) <= (EXT4_GOOD_OLD_INODE_SIZE + (einode)->i_extra_isize)) -#define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode) do { (raw_inode)->xtime = cpu_to_le32((inode)->xtime.tv_sec); if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra)) (raw_inode)->xtime ## _extra = ext4_encode_extra_time(&(inode)->xtime); } while (0) -#define EXT4_EINODE_SET_XTIME(xtime, einode, raw_inode) do { if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) (raw_inode)->xtime = cpu_to_le32((einode)->xtime.tv_sec); if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime ## _extra)) (raw_inode)->xtime ## _extra = ext4_encode_extra_time(&(einode)->xtime); } while (0) -#define EXT4_INODE_GET_XTIME(xtime, inode, raw_inode) do { (inode)->xtime.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime); if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra)) ext4_decode_extra_time(&(inode)->xtime, (raw_inode)->xtime ## _extra); } while (0) - -#define EXT4_EINODE_GET_XTIME(xtime, einode, raw_inode) do { if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) (einode)->xtime.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime); if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime ## _extra)) ext4_decode_extra_time(&(einode)->xtime, (raw_inode)->xtime ## _extra); } while (0) +#define EXT4_FITS_IN_INODE(ext4_inode, einode, field) \ + ((offsetof(typeof(*(ext4_inode)), field) + sizeof((ext4_inode)->field)) <= \ + (EXT4_GOOD_OLD_INODE_SIZE + (einode)->i_extra_isize)) +#define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode) \ + do { \ + (raw_inode)->xtime = cpu_to_le32((inode)->xtime.tv_sec); \ + if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime##_extra)) \ + (raw_inode)->xtime##_extra = ext4_encode_extra_time(&(inode)->xtime); \ + } while (0) +#define EXT4_EINODE_SET_XTIME(xtime, einode, raw_inode) \ + do { \ + if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) \ + (raw_inode)->xtime = cpu_to_le32((einode)->xtime.tv_sec); \ + if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime##_extra)) \ + (raw_inode)->xtime##_extra = ext4_encode_extra_time(&(einode)->xtime); \ + } while (0) +#define EXT4_INODE_GET_XTIME(xtime, inode, raw_inode) \ + do { \ + (inode)->xtime.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime); \ + if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime##_extra)) \ + ext4_decode_extra_time(&(inode)->xtime, (raw_inode)->xtime##_extra); \ + } while (0) + +#define EXT4_EINODE_GET_XTIME(xtime, einode, raw_inode) \ + do { \ + if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) \ + (einode)->xtime.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime); \ + if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime##_extra)) \ + ext4_decode_extra_time(&(einode)->xtime, (raw_inode)->xtime##_extra); \ + } while (0) #define i_disk_version osd1.linux1.l_i_version #define i_reserved1 osd1.linux1.l_i_reserved1 @@ -319,48 +352,48 @@ struct move_extent { #define i_gid_high osd2.linux2.l_i_gid_high #define i_reserved2 osd2.linux2.l_i_reserved2 -#define EXT4_VALID_FS 0x0001 -#define EXT4_ERROR_FS 0x0002 -#define EXT4_ORPHAN_FS 0x0004 - -#define EXT2_FLAGS_SIGNED_HASH 0x0001 -#define EXT2_FLAGS_UNSIGNED_HASH 0x0002 -#define EXT2_FLAGS_TEST_FILESYS 0x0004 - -#define EXT4_MOUNT_OLDALLOC 0x00002 -#define EXT4_MOUNT_GRPID 0x00004 -#define EXT4_MOUNT_DEBUG 0x00008 -#define EXT4_MOUNT_ERRORS_CONT 0x00010 -#define EXT4_MOUNT_ERRORS_RO 0x00020 -#define EXT4_MOUNT_ERRORS_PANIC 0x00040 -#define EXT4_MOUNT_MINIX_DF 0x00080 -#define EXT4_MOUNT_NOLOAD 0x00100 -#define EXT4_MOUNT_DATA_FLAGS 0x00C00 -#define EXT4_MOUNT_JOURNAL_DATA 0x00400 -#define EXT4_MOUNT_ORDERED_DATA 0x00800 -#define EXT4_MOUNT_WRITEBACK_DATA 0x00C00 -#define EXT4_MOUNT_UPDATE_JOURNAL 0x01000 -#define EXT4_MOUNT_NO_UID32 0x02000 -#define EXT4_MOUNT_XATTR_USER 0x04000 -#define EXT4_MOUNT_POSIX_ACL 0x08000 -#define EXT4_MOUNT_NO_AUTO_DA_ALLOC 0x10000 -#define EXT4_MOUNT_BARRIER 0x20000 -#define EXT4_MOUNT_NOBH 0x40000 -#define EXT4_MOUNT_QUOTA 0x80000 -#define EXT4_MOUNT_USRQUOTA 0x100000 -#define EXT4_MOUNT_GRPQUOTA 0x200000 -#define EXT4_MOUNT_DIOREAD_NOLOCK 0x400000 -#define EXT4_MOUNT_JOURNAL_CHECKSUM 0x800000 -#define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT 0x1000000 -#define EXT4_MOUNT_I_VERSION 0x2000000 -#define EXT4_MOUNT_DELALLOC 0x8000000 -#define EXT4_MOUNT_DATA_ERR_ABORT 0x10000000 -#define EXT4_MOUNT_BLOCK_VALIDITY 0x20000000 -#define EXT4_MOUNT_DISCARD 0x40000000 +#define EXT4_VALID_FS 0x0001 +#define EXT4_ERROR_FS 0x0002 +#define EXT4_ORPHAN_FS 0x0004 + +#define EXT2_FLAGS_SIGNED_HASH 0x0001 +#define EXT2_FLAGS_UNSIGNED_HASH 0x0002 +#define EXT2_FLAGS_TEST_FILESYS 0x0004 + +#define EXT4_MOUNT_OLDALLOC 0x00002 +#define EXT4_MOUNT_GRPID 0x00004 +#define EXT4_MOUNT_DEBUG 0x00008 +#define EXT4_MOUNT_ERRORS_CONT 0x00010 +#define EXT4_MOUNT_ERRORS_RO 0x00020 +#define EXT4_MOUNT_ERRORS_PANIC 0x00040 +#define EXT4_MOUNT_MINIX_DF 0x00080 +#define EXT4_MOUNT_NOLOAD 0x00100 +#define EXT4_MOUNT_DATA_FLAGS 0x00C00 +#define EXT4_MOUNT_JOURNAL_DATA 0x00400 +#define EXT4_MOUNT_ORDERED_DATA 0x00800 +#define EXT4_MOUNT_WRITEBACK_DATA 0x00C00 +#define EXT4_MOUNT_UPDATE_JOURNAL 0x01000 +#define EXT4_MOUNT_NO_UID32 0x02000 +#define EXT4_MOUNT_XATTR_USER 0x04000 +#define EXT4_MOUNT_POSIX_ACL 0x08000 +#define EXT4_MOUNT_NO_AUTO_DA_ALLOC 0x10000 +#define EXT4_MOUNT_BARRIER 0x20000 +#define EXT4_MOUNT_NOBH 0x40000 +#define EXT4_MOUNT_QUOTA 0x80000 +#define EXT4_MOUNT_USRQUOTA 0x100000 +#define EXT4_MOUNT_GRPQUOTA 0x200000 +#define EXT4_MOUNT_DIOREAD_NOLOCK 0x400000 +#define EXT4_MOUNT_JOURNAL_CHECKSUM 0x800000 +#define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT 0x1000000 +#define EXT4_MOUNT_I_VERSION 0x2000000 +#define EXT4_MOUNT_DELALLOC 0x8000000 +#define EXT4_MOUNT_DATA_ERR_ABORT 0x10000000 +#define EXT4_MOUNT_BLOCK_VALIDITY 0x20000000 +#define EXT4_MOUNT_DISCARD 0x40000000 #define clear_opt(o, opt) o &= ~EXT4_MOUNT_##opt #define set_opt(o, opt) o |= EXT4_MOUNT_##opt -#define test_opt(sb, opt) (EXT4_SB(sb)->s_mount_opt & EXT4_MOUNT_##opt) +#define test_opt(sb, opt) (EXT4_SB(sb)->s_mount_opt & EXT4_MOUNT_##opt) #define ext4_set_bit ext2_set_bit #define ext4_set_bit_atomic ext2_set_bit_atomic @@ -371,84 +404,84 @@ struct move_extent { #define ext4_find_next_zero_bit ext2_find_next_zero_bit #define ext4_find_next_bit ext2_find_next_bit -#define EXT4_DFL_MAX_MNT_COUNT 20 -#define EXT4_DFL_CHECKINTERVAL 0 +#define EXT4_DFL_MAX_MNT_COUNT 20 +#define EXT4_DFL_CHECKINTERVAL 0 -#define EXT4_ERRORS_CONTINUE 1 -#define EXT4_ERRORS_RO 2 -#define EXT4_ERRORS_PANIC 3 +#define EXT4_ERRORS_CONTINUE 1 +#define EXT4_ERRORS_RO 2 +#define EXT4_ERRORS_PANIC 3 #define EXT4_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE struct ext4_super_block { - __le32 s_inodes_count; - __le32 s_blocks_count_lo; - __le32 s_r_blocks_count_lo; - __le32 s_free_blocks_count_lo; - __le32 s_free_inodes_count; - __le32 s_first_data_block; - __le32 s_log_block_size; - __le32 s_obso_log_frag_size; - __le32 s_blocks_per_group; - __le32 s_obso_frags_per_group; - __le32 s_inodes_per_group; - __le32 s_mtime; - __le32 s_wtime; - __le16 s_mnt_count; - __le16 s_max_mnt_count; - __le16 s_magic; - __le16 s_state; - __le16 s_errors; - __le16 s_minor_rev_level; - __le32 s_lastcheck; - __le32 s_checkinterval; - __le32 s_creator_os; - __le32 s_rev_level; - __le16 s_def_resuid; - __le16 s_def_resgid; - - __le32 s_first_ino; - __le16 s_inode_size; - __le16 s_block_group_nr; - __le32 s_feature_compat; - __le32 s_feature_incompat; - __le32 s_feature_ro_compat; - __u8 s_uuid[16]; - char s_volume_name[16]; - char s_last_mounted[64]; - __le32 s_algorithm_usage_bitmap; - - __u8 s_prealloc_blocks; - __u8 s_prealloc_dir_blocks; - __le16 s_reserved_gdt_blocks; - - __u8 s_journal_uuid[16]; - __le32 s_journal_inum; - __le32 s_journal_dev; - __le32 s_last_orphan; - __le32 s_hash_seed[4]; - __u8 s_def_hash_version; - __u8 s_reserved_char_pad; - __le16 s_desc_size; - __le32 s_default_mount_opts; - __le32 s_first_meta_bg; - __le32 s_mkfs_time; - __le32 s_jnl_blocks[17]; - - __le32 s_blocks_count_hi; - __le32 s_r_blocks_count_hi; - __le32 s_free_blocks_count_hi; - __le16 s_min_extra_isize; - __le16 s_want_extra_isize; - __le32 s_flags; - __le16 s_raid_stride; - __le16 s_mmp_interval; - __le64 s_mmp_block; - __le32 s_raid_stripe_width; - __u8 s_log_groups_per_flex; - __u8 s_reserved_char_pad2; - __le16 s_reserved_pad; - __le64 s_kbytes_written; - __u32 s_reserved[160]; + __le32 s_inodes_count; + __le32 s_blocks_count_lo; + __le32 s_r_blocks_count_lo; + __le32 s_free_blocks_count_lo; + __le32 s_free_inodes_count; + __le32 s_first_data_block; + __le32 s_log_block_size; + __le32 s_obso_log_frag_size; + __le32 s_blocks_per_group; + __le32 s_obso_frags_per_group; + __le32 s_inodes_per_group; + __le32 s_mtime; + __le32 s_wtime; + __le16 s_mnt_count; + __le16 s_max_mnt_count; + __le16 s_magic; + __le16 s_state; + __le16 s_errors; + __le16 s_minor_rev_level; + __le32 s_lastcheck; + __le32 s_checkinterval; + __le32 s_creator_os; + __le32 s_rev_level; + __le16 s_def_resuid; + __le16 s_def_resgid; + + __le32 s_first_ino; + __le16 s_inode_size; + __le16 s_block_group_nr; + __le32 s_feature_compat; + __le32 s_feature_incompat; + __le32 s_feature_ro_compat; + __u8 s_uuid[16]; + char s_volume_name[16]; + char s_last_mounted[64]; + __le32 s_algorithm_usage_bitmap; + + __u8 s_prealloc_blocks; + __u8 s_prealloc_dir_blocks; + __le16 s_reserved_gdt_blocks; + + __u8 s_journal_uuid[16]; + __le32 s_journal_inum; + __le32 s_journal_dev; + __le32 s_last_orphan; + __le32 s_hash_seed[4]; + __u8 s_def_hash_version; + __u8 s_reserved_char_pad; + __le16 s_desc_size; + __le32 s_default_mount_opts; + __le32 s_first_meta_bg; + __le32 s_mkfs_time; + __le32 s_jnl_blocks[17]; + + __le32 s_blocks_count_hi; + __le32 s_r_blocks_count_hi; + __le32 s_free_blocks_count_hi; + __le16 s_min_extra_isize; + __le16 s_want_extra_isize; + __le32 s_flags; + __le16 s_raid_stride; + __le16 s_mmp_interval; + __le64 s_mmp_block; + __le32 s_raid_stripe_width; + __u8 s_log_groups_per_flex; + __u8 s_reserved_char_pad2; + __le16 s_reserved_pad; + __le64 s_kbytes_written; + __u32 s_reserved[160]; }; #define EXT4_SB(sb) (sb) @@ -461,23 +494,31 @@ struct ext4_super_block { #define EXT4_OS_FREEBSD 3 #define EXT4_OS_LITES 4 -#define EXT4_GOOD_OLD_REV 0 -#define EXT4_DYNAMIC_REV 1 +#define EXT4_GOOD_OLD_REV 0 +#define EXT4_DYNAMIC_REV 1 #define EXT4_CURRENT_REV EXT4_GOOD_OLD_REV #define EXT4_MAX_SUPP_REV EXT4_DYNAMIC_REV #define EXT4_GOOD_OLD_INODE_SIZE 128 -#define EXT4_HAS_COMPAT_FEATURE(sb,mask) ((EXT4_SB(sb)->s_es->s_feature_compat & cpu_to_le32(mask)) != 0) -#define EXT4_HAS_RO_COMPAT_FEATURE(sb,mask) ((EXT4_SB(sb)->s_es->s_feature_ro_compat & cpu_to_le32(mask)) != 0) -#define EXT4_HAS_INCOMPAT_FEATURE(sb,mask) ((EXT4_SB(sb)->s_es->s_feature_incompat & cpu_to_le32(mask)) != 0) -#define EXT4_SET_COMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_compat |= cpu_to_le32(mask) -#define EXT4_SET_RO_COMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_ro_compat |= cpu_to_le32(mask) -#define EXT4_SET_INCOMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_incompat |= cpu_to_le32(mask) -#define EXT4_CLEAR_COMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_compat &= ~cpu_to_le32(mask) -#define EXT4_CLEAR_RO_COMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_ro_compat &= ~cpu_to_le32(mask) -#define EXT4_CLEAR_INCOMPAT_FEATURE(sb,mask) EXT4_SB(sb)->s_es->s_feature_incompat &= ~cpu_to_le32(mask) +#define EXT4_HAS_COMPAT_FEATURE(sb, mask) \ + ((EXT4_SB(sb)->s_es->s_feature_compat & cpu_to_le32(mask)) != 0) +#define EXT4_HAS_RO_COMPAT_FEATURE(sb, mask) \ + ((EXT4_SB(sb)->s_es->s_feature_ro_compat & cpu_to_le32(mask)) != 0) +#define EXT4_HAS_INCOMPAT_FEATURE(sb, mask) \ + ((EXT4_SB(sb)->s_es->s_feature_incompat & cpu_to_le32(mask)) != 0) +#define EXT4_SET_COMPAT_FEATURE(sb, mask) EXT4_SB(sb)->s_es->s_feature_compat |= cpu_to_le32(mask) +#define EXT4_SET_RO_COMPAT_FEATURE(sb, mask) \ + EXT4_SB(sb)->s_es->s_feature_ro_compat |= cpu_to_le32(mask) +#define EXT4_SET_INCOMPAT_FEATURE(sb, mask) \ + EXT4_SB(sb)->s_es->s_feature_incompat |= cpu_to_le32(mask) +#define EXT4_CLEAR_COMPAT_FEATURE(sb, mask) \ + EXT4_SB(sb)->s_es->s_feature_compat &= ~cpu_to_le32(mask) +#define EXT4_CLEAR_RO_COMPAT_FEATURE(sb, mask) \ + EXT4_SB(sb)->s_es->s_feature_ro_compat &= ~cpu_to_le32(mask) +#define EXT4_CLEAR_INCOMPAT_FEATURE(sb, mask) \ + EXT4_SB(sb)->s_es->s_feature_incompat &= ~cpu_to_le32(mask) #define EXT4_FEATURE_COMPAT_DIR_PREALLOC 0x0001 #define EXT4_FEATURE_COMPAT_IMAGIC_INODES 0x0002 @@ -502,21 +543,28 @@ struct ext4_super_block { #define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001 #define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002 -#define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004 -#define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 +#define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004 +#define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 #define EXT4_FEATURE_INCOMPAT_META_BG 0x0010 -#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 +#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080 #define EXT4_FEATURE_INCOMPAT_MMP 0x0100 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200 -#define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400 -#define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000 +#define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400 +#define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000 #define EXT4_FEATURE_INCOMPAT_ENCRYPT 0x10000 #define EXT4_FEATURE_INCOMPAT_CASEFOLD 0x20000 #define EXT4_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR -#define EXT4_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| EXT4_FEATURE_INCOMPAT_RECOVER| EXT4_FEATURE_INCOMPAT_META_BG| EXT4_FEATURE_INCOMPAT_EXTENTS| EXT4_FEATURE_INCOMPAT_64BIT| EXT4_FEATURE_INCOMPAT_FLEX_BG) -#define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| EXT4_FEATURE_RO_COMPAT_LARGE_FILE| EXT4_FEATURE_RO_COMPAT_GDT_CSUM| EXT4_FEATURE_RO_COMPAT_DIR_NLINK | EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | EXT4_FEATURE_RO_COMPAT_BTREE_DIR | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) +#define EXT4_FEATURE_INCOMPAT_SUPP \ + (EXT4_FEATURE_INCOMPAT_FILETYPE | EXT4_FEATURE_INCOMPAT_RECOVER | \ + EXT4_FEATURE_INCOMPAT_META_BG | EXT4_FEATURE_INCOMPAT_EXTENTS | EXT4_FEATURE_INCOMPAT_64BIT | \ + EXT4_FEATURE_INCOMPAT_FLEX_BG) +#define EXT4_FEATURE_RO_COMPAT_SUPP \ + (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \ + EXT4_FEATURE_RO_COMPAT_GDT_CSUM | EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \ + EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | EXT4_FEATURE_RO_COMPAT_BTREE_DIR | \ + EXT4_FEATURE_RO_COMPAT_HUGE_FILE) #define EXT4_DEF_RESUID 0 #define EXT4_DEF_RESGID 0 @@ -534,25 +582,25 @@ struct ext4_super_block { #define EXT4_DEFM_JMODE_WBACK 0x0060 #define EXT4_DEF_MIN_BATCH_TIME 0 -#define EXT4_DEF_MAX_BATCH_TIME 15000 +#define EXT4_DEF_MAX_BATCH_TIME 15000 #define EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME 4 #define EXT4_NAME_LEN 255 struct ext4_dir_entry { - __le32 inode; - __le16 rec_len; - __le16 name_len; - char name[EXT4_NAME_LEN]; + __le32 inode; + __le16 rec_len; + __le16 name_len; + char name[EXT4_NAME_LEN]; }; struct ext4_dir_entry_2 { - __le32 inode; - __le16 rec_len; - __u8 name_len; - __u8 file_type; - char name[EXT4_NAME_LEN]; + __le32 inode; + __le16 rec_len; + __u8 name_len; + __u8 file_type; + char name[EXT4_NAME_LEN]; }; #define EXT4_FT_UNKNOWN 0 @@ -568,10 +616,12 @@ struct ext4_dir_entry_2 { #define EXT4_DIR_PAD 4 #define EXT4_DIR_ROUND (EXT4_DIR_PAD - 1) -#define EXT4_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT4_DIR_ROUND) & ~EXT4_DIR_ROUND) -#define EXT4_MAX_REC_LEN ((1<<16)-1) +#define EXT4_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT4_DIR_ROUND) & ~EXT4_DIR_ROUND) +#define EXT4_MAX_REC_LEN ((1 << 16) - 1) -#define is_dx(dir) (EXT4_HAS_COMPAT_FEATURE((dir)->i_sb, EXT4_FEATURE_COMPAT_DIR_INDEX) && (EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) +#define is_dx(dir) \ + (EXT4_HAS_COMPAT_FEATURE((dir)->i_sb, EXT4_FEATURE_COMPAT_DIR_INDEX) && \ + (EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) #define EXT4_DIR_LINK_MAX(dir) (!is_dx(dir) && (dir)->i_nlink >= EXT4_LINK_MAX) #define EXT4_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1) @@ -583,4 +633,3 @@ struct ext4_dir_entry_2 { #define DX_HASH_TEA_UNSIGNED 5 #endif - diff --git a/ext4_utils/include/ext4_utils/ext4_extents.h b/ext4_utils/include/ext4_utils/ext4_extents.h index 68fc4a45..e69b017b 100644 --- a/ext4_utils/include/ext4_utils/ext4_extents.h +++ b/ext4_utils/include/ext4_utils/ext4_extents.h @@ -30,36 +30,36 @@ #define EXT_STATS_ struct ext4_extent { - __le32 ee_block; - __le16 ee_len; - __le16 ee_start_hi; - __le32 ee_start_lo; + __le32 ee_block; + __le16 ee_len; + __le16 ee_start_hi; + __le32 ee_start_lo; }; struct ext4_extent_idx { - __le32 ei_block; - __le32 ei_leaf_lo; - __le16 ei_leaf_hi; - __u16 ei_unused; + __le32 ei_block; + __le32 ei_leaf_lo; + __le16 ei_leaf_hi; + __u16 ei_unused; }; struct ext4_extent_header { - __le16 eh_magic; - __le16 eh_entries; - __le16 eh_max; - __le16 eh_depth; - __le32 eh_generation; + __le16 eh_magic; + __le16 eh_entries; + __le16 eh_max; + __le16 eh_depth; + __le32 eh_generation; }; #define EXT4_EXT_MAGIC 0xf30a struct ext4_ext_path { - ext4_fsblk_t p_block; - __u16 p_depth; - struct ext4_extent *p_ext; - struct ext4_extent_idx *p_idx; - struct ext4_extent_header *p_hdr; - struct buffer_head *p_bh; + ext4_fsblk_t p_block; + __u16 p_depth; + struct ext4_extent* p_ext; + struct ext4_extent_idx* p_idx; + struct ext4_extent_header* p_hdr; + struct buffer_head* p_bh; }; #define EXT4_EXT_CACHE_NO 0 @@ -75,14 +75,17 @@ struct ext4_ext_path { #define EXT_INIT_MAX_LEN (1UL << 15) #define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1) -#define EXT_FIRST_EXTENT(__hdr__) ((struct ext4_extent *) (((char *) (__hdr__)) + sizeof(struct ext4_extent_header))) -#define EXT_FIRST_INDEX(__hdr__) ((struct ext4_extent_idx *) (((char *) (__hdr__)) + sizeof(struct ext4_extent_header))) -#define EXT_HAS_FREE_INDEX(__path__) (le16_to_cpu((__path__)->p_hdr->eh_entries) < le16_to_cpu((__path__)->p_hdr->eh_max)) -#define EXT_LAST_EXTENT(__hdr__) (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) -#define EXT_LAST_INDEX(__hdr__) (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) -#define EXT_MAX_EXTENT(__hdr__) (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1) -#define EXT_MAX_INDEX(__hdr__) (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1) +#define EXT_FIRST_EXTENT(__hdr__) \ + ((struct ext4_extent*)(((char*)(__hdr__)) + sizeof(struct ext4_extent_header))) +#define EXT_FIRST_INDEX(__hdr__) \ + ((struct ext4_extent_idx*)(((char*)(__hdr__)) + sizeof(struct ext4_extent_header))) +#define EXT_HAS_FREE_INDEX(__path__) \ + (le16_to_cpu((__path__)->p_hdr->eh_entries) < le16_to_cpu((__path__)->p_hdr->eh_max)) +#define EXT_LAST_EXTENT(__hdr__) \ + (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) +#define EXT_LAST_INDEX(__hdr__) \ + (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1) +#define EXT_MAX_EXTENT(__hdr__) (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1) +#define EXT_MAX_INDEX(__hdr__) (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1) #endif - - diff --git a/ext4_utils/include/ext4_utils/ext4_sb.h b/ext4_utils/include/ext4_utils/ext4_sb.h index 3b53dbe4..81d8e73a 100644 --- a/ext4_utils/include/ext4_utils/ext4_sb.h +++ b/ext4_utils/include/ext4_utils/ext4_sb.h @@ -28,28 +28,28 @@ extern "C" { #include <stdbool.h> struct fs_info { - int64_t len; /* If set to 0, ask the block device for the size, - * if less than 0, reserve that much space at the - * end of the partition, else use the size given. */ - uint32_t block_size; - uint32_t blocks_per_group; - uint32_t flash_erase_block_size; - uint32_t flash_logical_block_size; - uint32_t inodes_per_group; - uint32_t inode_size; - uint32_t inodes; - uint32_t journal_blocks; - uint32_t feat_ro_compat; - uint32_t feat_compat; - uint32_t feat_incompat; - uint32_t bg_desc_reserve_blocks; - uint16_t bg_desc_size; - const char *label; - uint8_t no_journal; - bool block_device; /* target fd is a block device? */ + int64_t len; /* If set to 0, ask the block device for the size, + * if less than 0, reserve that much space at the + * end of the partition, else use the size given. */ + uint32_t block_size; + uint32_t blocks_per_group; + uint32_t flash_erase_block_size; + uint32_t flash_logical_block_size; + uint32_t inodes_per_group; + uint32_t inode_size; + uint32_t inodes; + uint32_t journal_blocks; + uint32_t feat_ro_compat; + uint32_t feat_compat; + uint32_t feat_incompat; + uint32_t bg_desc_reserve_blocks; + uint16_t bg_desc_size; + const char* label; + uint8_t no_journal; + bool block_device; /* target fd is a block device? */ }; -int ext4_parse_sb(struct ext4_super_block *sb, struct fs_info *info); +int ext4_parse_sb(struct ext4_super_block* sb, struct fs_info* info); #ifdef __cplusplus } diff --git a/ext4_utils/include/ext4_utils/ext4_utils.h b/ext4_utils/include/ext4_utils/ext4_utils.h index 2c2485f7..48f3ee78 100644 --- a/ext4_utils/include/ext4_utils/ext4_utils.h +++ b/ext4_utils/include/ext4_utils/ext4_utils.h @@ -29,14 +29,14 @@ extern "C" { #include <sys/types.h> #include <unistd.h> -#include <sys/types.h> #include <errno.h> +#include <setjmp.h> #include <stdarg.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <setjmp.h> -#include <stdint.h> +#include <sys/types.h> #if defined(__APPLE__) && defined(__MACH__) #define lseek64 lseek @@ -57,8 +57,8 @@ extern int force; #endif #endif -#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) -#define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) +#define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y)) +#define EXT4_ALIGN(x, y) ((y)*DIV_ROUND_UP((x), (y))) /* XXX */ #define cpu_to_le32(x) (x) @@ -85,32 +85,32 @@ struct block_group_info; struct xattr_list_element; struct ext2_group_desc { - u64 bg_block_bitmap; - u64 bg_inode_bitmap; - u64 bg_inode_table; - u32 bg_free_blocks_count; - u32 bg_free_inodes_count; - u32 bg_used_dirs_count; - u16 bg_flags; + u64 bg_block_bitmap; + u64 bg_inode_bitmap; + u64 bg_inode_table; + u32 bg_free_blocks_count; + u32 bg_free_inodes_count; + u32 bg_used_dirs_count; + u16 bg_flags; }; struct fs_aux_info { - struct ext4_super_block *sb; - struct ext4_super_block *sb_block; - struct ext4_super_block *sb_zero; - struct ext4_super_block **backup_sb; - struct ext2_group_desc *bg_desc; - struct block_group_info *bgs; - struct xattr_list_element *xattrs; - u32 first_data_block; - u64 len_blocks; - u32 inode_table_blocks; - u32 groups; - u32 bg_desc_blocks; - u32 default_i_flags; - u64 blocks_per_ind; - u64 blocks_per_dind; - u64 blocks_per_tind; + struct ext4_super_block* sb; + struct ext4_super_block* sb_block; + struct ext4_super_block* sb_zero; + struct ext4_super_block** backup_sb; + struct ext2_group_desc* bg_desc; + struct block_group_info* bgs; + struct xattr_list_element* xattrs; + u32 first_data_block; + u64 len_blocks; + u32 inode_table_blocks; + u32 groups; + u32 bg_desc_blocks; + u32 default_i_flags; + u64 blocks_per_ind; + u64 blocks_per_dind; + u64 blocks_per_tind; }; extern struct fs_info info; @@ -118,12 +118,12 @@ extern struct fs_aux_info aux_info; extern jmp_buf setjmp_env; -int bitmap_get_bit(u8 *bitmap, u32 bit); // vold -u64 get_block_device_size(int fd); // recovery -int is_block_device_fd(int fd); // wipe.c -u64 get_file_size(int fd); // fs_mgr +int bitmap_get_bit(u8* bitmap, u32 bit); // vold +u64 get_block_device_size(int fd); // recovery +int is_block_device_fd(int fd); // wipe.c +u64 get_file_size(int fd); // fs_mgr int ext4_bg_has_super_block(int bg); -int read_ext(int fd, int verbose); // vold +int read_ext(int fd, int verbose); // vold #ifdef __cplusplus } diff --git a/ext4_utils/include/ext4_utils/jbd2.h b/ext4_utils/include/ext4_utils/jbd2.h index bac58c2e..6b7b161c 100644 --- a/ext4_utils/include/ext4_utils/jbd2.h +++ b/ext4_utils/include/ext4_utils/jbd2.h @@ -21,11 +21,11 @@ #define JBD2_DEFAULT_MAX_COMMIT_AGE 5 -#define jbd_debug(f, a...) +#define jbd_debug(f, a...) #define JBD2_MIN_JOURNAL_BLOCKS 1024 -#define JBD2_MAGIC_NUMBER 0xc03b3998U +#define JBD2_MAGIC_NUMBER 0xc03b3998U #define JBD2_DESCRIPTOR_BLOCK 1 #define JBD2_COMMIT_BLOCK 2 @@ -33,11 +33,10 @@ #define JBD2_SUPERBLOCK_V2 4 #define JBD2_REVOKE_BLOCK 5 -typedef struct journal_header_s -{ - __be32 h_magic; - __be32 h_blocktype; - __be32 h_sequence; +typedef struct journal_header_s { + __be32 h_magic; + __be32 h_blocktype; + __be32 h_sequence; } journal_header_t; #define JBD2_CRC32_CHKSUM 1 @@ -49,74 +48,73 @@ typedef struct journal_header_s #define JBD2_CHECKSUM_BYTES (32 / sizeof(__u32)) struct commit_header { - __be32 h_magic; - __be32 h_blocktype; - __be32 h_sequence; - unsigned char h_chksum_type; - unsigned char h_chksum_size; - unsigned char h_padding[2]; - __be32 h_chksum[JBD2_CHECKSUM_BYTES]; - __be64 h_commit_sec; - __be32 h_commit_nsec; + __be32 h_magic; + __be32 h_blocktype; + __be32 h_sequence; + unsigned char h_chksum_type; + unsigned char h_chksum_size; + unsigned char h_padding[2]; + __be32 h_chksum[JBD2_CHECKSUM_BYTES]; + __be64 h_commit_sec; + __be32 h_commit_nsec; }; -typedef struct journal_block_tag_s -{ - __be32 t_blocknr; - __be32 t_flags; - __be32 t_blocknr_high; +typedef struct journal_block_tag_s { + __be32 t_blocknr; + __be32 t_flags; + __be32 t_blocknr_high; } journal_block_tag_t; #define JBD2_TAG_SIZE32 (offsetof(journal_block_tag_t, t_blocknr_high)) #define JBD2_TAG_SIZE64 (sizeof(journal_block_tag_t)) -typedef struct jbd2_journal_revoke_header_s -{ - journal_header_t r_header; - __be32 r_count; +typedef struct jbd2_journal_revoke_header_s { + journal_header_t r_header; + __be32 r_count; } jbd2_journal_revoke_header_t; -#define JBD2_FLAG_ESCAPE 1 -#define JBD2_FLAG_SAME_UUID 2 -#define JBD2_FLAG_DELETED 4 -#define JBD2_FLAG_LAST_TAG 8 +#define JBD2_FLAG_ESCAPE 1 +#define JBD2_FLAG_SAME_UUID 2 +#define JBD2_FLAG_DELETED 4 +#define JBD2_FLAG_LAST_TAG 8 -typedef struct journal_superblock_s -{ +typedef struct journal_superblock_s { + journal_header_t s_header; - journal_header_t s_header; + __be32 s_blocksize; + __be32 s_maxlen; + __be32 s_first; - __be32 s_blocksize; - __be32 s_maxlen; - __be32 s_first; + __be32 s_sequence; + __be32 s_start; - __be32 s_sequence; - __be32 s_start; + __be32 s_errno; - __be32 s_errno; + __be32 s_feature_compat; + __be32 s_feature_incompat; + __be32 s_feature_ro_compat; - __be32 s_feature_compat; - __be32 s_feature_incompat; - __be32 s_feature_ro_compat; + __u8 s_uuid[16]; - __u8 s_uuid[16]; + __be32 s_nr_users; - __be32 s_nr_users; + __be32 s_dynsuper; - __be32 s_dynsuper; + __be32 s_max_transaction; + __be32 s_max_trans_data; - __be32 s_max_transaction; - __be32 s_max_trans_data; + __u32 s_padding[44]; - __u32 s_padding[44]; - - __u8 s_users[16*48]; + __u8 s_users[16 * 48]; } journal_superblock_t; -#define JBD2_HAS_COMPAT_FEATURE(j,mask) ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask)))) -#define JBD2_HAS_RO_COMPAT_FEATURE(j,mask) ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask)))) -#define JBD2_HAS_INCOMPAT_FEATURE(j,mask) ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask)))) +#define JBD2_HAS_COMPAT_FEATURE(j, mask) \ + ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask)))) +#define JBD2_HAS_RO_COMPAT_FEATURE(j, mask) \ + ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask)))) +#define JBD2_HAS_INCOMPAT_FEATURE(j, mask) \ + ((j)->j_format_version >= 2 && ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask)))) #define JBD2_FEATURE_COMPAT_CHECKSUM 0x00000001 @@ -126,16 +124,17 @@ typedef struct journal_superblock_s #define JBD2_KNOWN_COMPAT_FEATURES JBD2_FEATURE_COMPAT_CHECKSUM #define JBD2_KNOWN_ROCOMPAT_FEATURES 0 -#define JBD2_KNOWN_INCOMPAT_FEATURES (JBD2_FEATURE_INCOMPAT_REVOKE | JBD2_FEATURE_INCOMPAT_64BIT | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT) - -#define BJ_None 0 -#define BJ_Metadata 1 -#define BJ_Forget 2 -#define BJ_IO 3 -#define BJ_Shadow 4 -#define BJ_LogCtl 5 -#define BJ_Reserved 6 +#define JBD2_KNOWN_INCOMPAT_FEATURES \ + (JBD2_FEATURE_INCOMPAT_REVOKE | JBD2_FEATURE_INCOMPAT_64BIT | \ + JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT) + +#define BJ_None 0 +#define BJ_Metadata 1 +#define BJ_Forget 2 +#define BJ_IO 3 +#define BJ_Shadow 4 +#define BJ_LogCtl 5 +#define BJ_Reserved 6 #define BJ_Types 7 #endif - diff --git a/ext4_utils/include/ext4_utils/wipe.h b/ext4_utils/include/ext4_utils/wipe.h index d7d089e7..09c32955 100644 --- a/ext4_utils/include/ext4_utils/wipe.h +++ b/ext4_utils/include/ext4_utils/wipe.h @@ -27,9 +27,9 @@ extern "C" { * wiping of block devices. 0 otherwise. For now, only Linux does. */ #ifdef __linux__ -# define WIPE_IS_SUPPORTED 1 +#define WIPE_IS_SUPPORTED 1 #else -# define WIPE_IS_SUPPORTED 0 +#define WIPE_IS_SUPPORTED 0 #endif int wipe_block_device(int fd, s64 len); diff --git a/ext4_utils/include/ext4_utils/xattr.h b/ext4_utils/include/ext4_utils/xattr.h index 0f323fdc..4251c288 100644 --- a/ext4_utils/include/ext4_utils/xattr.h +++ b/ext4_utils/include/ext4_utils/xattr.h @@ -7,16 +7,16 @@ #define EXT4_XATTR_INDEX_SECURITY 6 struct ext4_xattr_header { - __le32 h_magic; - __le32 h_refcount; - __le32 h_blocks; - __le32 h_hash; - __le32 h_checksum; - __u32 h_reserved[3]; + __le32 h_magic; + __le32 h_refcount; + __le32 h_blocks; + __le32 h_hash; + __le32 h_checksum; + __u32 h_reserved[3]; }; struct ext4_xattr_ibody_header { - __le32 h_magic; + __le32 h_magic; }; struct ext4_xattr_entry { @@ -30,16 +30,13 @@ struct ext4_xattr_entry { }; #define EXT4_XATTR_PAD_BITS 2 -#define EXT4_XATTR_PAD (1<<EXT4_XATTR_PAD_BITS) -#define EXT4_XATTR_ROUND (EXT4_XATTR_PAD-1) +#define EXT4_XATTR_PAD (1 << EXT4_XATTR_PAD_BITS) +#define EXT4_XATTR_ROUND (EXT4_XATTR_PAD - 1) #define EXT4_XATTR_LEN(name_len) \ - (((name_len) + EXT4_XATTR_ROUND + \ - sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND) + (((name_len) + EXT4_XATTR_ROUND + sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND) #define EXT4_XATTR_NEXT(entry) \ - ((struct ext4_xattr_entry *)( \ - (char *)(entry) + EXT4_XATTR_LEN((entry)->e_name_len))) -#define EXT4_XATTR_SIZE(size) \ - (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND) -#define IS_LAST_ENTRY(entry) (*(uint32_t *)(entry) == 0) + ((struct ext4_xattr_entry*)((char*)(entry) + EXT4_XATTR_LEN((entry)->e_name_len))) +#define EXT4_XATTR_SIZE(size) (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND) +#define IS_LAST_ENTRY(entry) (*(uint32_t*)(entry) == 0) #endif /* !_SYSTEM_EXTRAS_EXT4_UTILS_XATTR_H */ diff --git a/ext4_utils/wipe.cpp b/ext4_utils/wipe.cpp index 8b7a66bd..445c9739 100644 --- a/ext4_utils/wipe.cpp +++ b/ext4_utils/wipe.cpp @@ -30,59 +30,57 @@ #include "helpers.h" #ifndef BLKDISCARD -#define BLKDISCARD _IO(0x12,119) +#define BLKDISCARD _IO(0x12, 119) #endif #ifndef BLKSECDISCARD -#define BLKSECDISCARD _IO(0x12,125) +#define BLKSECDISCARD _IO(0x12, 125) #endif -int wipe_block_device(int fd, s64 len) -{ - u64 range[2]; - int ret; - - if (!is_block_device_fd(fd)) { - // Wiping only makes sense on a block device. - return 0; - } - - range[0] = 0; - range[1] = len; - ret = ioctl(fd, BLKSECDISCARD, &range); - if (ret < 0) { - range[0] = 0; - range[1] = len; - ret = ioctl(fd, BLKDISCARD, &range); - if (ret < 0) { - warn("Discard failed\n"); - return 1; - } else { - char buf[4096] = {0}; - - if (!android::base::WriteFully(fd, buf, 4096)) { - warn("Writing zeros failed\n"); - return 1; - } - fsync(fd); - warn("Wipe via secure discard failed, used discard instead\n"); - return 0; - } - } - - return 0; +int wipe_block_device(int fd, s64 len) { + u64 range[2]; + int ret; + + if (!is_block_device_fd(fd)) { + // Wiping only makes sense on a block device. + return 0; + } + + range[0] = 0; + range[1] = len; + ret = ioctl(fd, BLKSECDISCARD, &range); + if (ret < 0) { + range[0] = 0; + range[1] = len; + ret = ioctl(fd, BLKDISCARD, &range); + if (ret < 0) { + warn("Discard failed\n"); + return 1; + } else { + char buf[4096] = {0}; + + if (!android::base::WriteFully(fd, buf, 4096)) { + warn("Writing zeros failed\n"); + return 1; + } + fsync(fd); + warn("Wipe via secure discard failed, used discard instead\n"); + return 0; + } + } + + return 0; } -#else /* __linux__ */ +#else /* __linux__ */ #error "Missing block device wiping implementation for this platform!" #endif -#else /* WIPE_IS_SUPPORTED */ +#else /* WIPE_IS_SUPPORTED */ -int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused))) -{ - /* Wiping is not supported on this platform. */ - return 1; +int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused))) { + /* Wiping is not supported on this platform. */ + return 1; } -#endif /* WIPE_IS_SUPPORTED */ +#endif /* WIPE_IS_SUPPORTED */ diff --git a/f2fs_utils/f2fs_sparseblock.c b/f2fs_utils/f2fs_sparseblock.c index 0ec1f30c..e3292c98 100644 --- a/f2fs_utils/f2fs_sparseblock.c +++ b/f2fs_utils/f2fs_sparseblock.c @@ -16,24 +16,25 @@ #include <log/log.h> -#define D_DISP_u32(ptr, member) \ - do { \ - SLOGD("%-30s" "\t\t[0x%#08x : %u]\n", \ - #member, le32_to_cpu((ptr)->member), le32_to_cpu((ptr)->member) ); \ - } while (0); +#define D_DISP_u32(ptr, member) \ + do { \ + SLOGD("%-30s" \ + "\t\t[0x%#08x : %u]\n", \ + #member, le32_to_cpu((ptr)->member), le32_to_cpu((ptr)->member)); \ + } while (0); -#define D_DISP_u64(ptr, member) \ - do { \ - SLOGD("%-30s" "\t\t[0x%#016llx : %llu]\n", \ - #member, le64_to_cpu((ptr)->member), le64_to_cpu((ptr)->member) ); \ - } while (0); +#define D_DISP_u64(ptr, member) \ + do { \ + SLOGD("%-30s" \ + "\t\t[0x%#016llx : %llu]\n", \ + #member, le64_to_cpu((ptr)->member), le64_to_cpu((ptr)->member)); \ + } while (0); -#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno) +#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno) -#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se) +#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se) -static void dbg_print_raw_sb_info(struct f2fs_super_block *sb) -{ +static void dbg_print_raw_sb_info(struct f2fs_super_block* sb) { SLOGD("\n"); SLOGD("+--------------------------------------------------------+\n"); SLOGD("| Super block |\n"); @@ -74,8 +75,7 @@ static void dbg_print_raw_sb_info(struct f2fs_super_block *sb) D_DISP_u32(sb, cp_payload); SLOGD("\n"); } -static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint *cp) -{ +static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint* cp) { SLOGD("\n"); SLOGD("+--------------------------------------------------------+\n"); SLOGD("| Checkpoint |\n"); @@ -99,7 +99,6 @@ static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint *cp) D_DISP_u32(cp, cur_node_blkoff[1]); D_DISP_u32(cp, cur_node_blkoff[2]); - D_DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]); D_DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]); D_DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]); @@ -126,8 +125,7 @@ static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint *cp) SLOGD("\n\n"); } -static void dbg_print_info_struct(struct f2fs_info *info) -{ +static void dbg_print_info_struct(struct f2fs_info* info) { SLOGD("\n"); SLOGD("+--------------------------------------------------------+\n"); SLOGD("| F2FS_INFO |\n"); @@ -141,7 +139,7 @@ static void dbg_print_info_struct(struct f2fs_info *info) SLOGD("sit_sums loc: %p", info->sit_sums); SLOGD("sit_sums num: %d", le16_to_cpu(info->sit_sums->journal.n_sits)); unsigned int i; - for(i = 0; i < (le16_to_cpu(info->sit_sums->journal.n_sits)); i++) { + for (i = 0; i < (le16_to_cpu(info->sit_sums->journal.n_sits)); i++) { SLOGD("entry %d in journal entries is for segment %d", i, le32_to_cpu(segno_in_journal(&info->sit_sums->journal, i))); } @@ -157,10 +155,8 @@ static void dbg_print_info_struct(struct f2fs_info *info) SLOGD("\n\n"); } - /* read blocks */ -static int read_structure(int fd, unsigned long long start, void *buf, ssize_t len) -{ +static int read_structure(int fd, unsigned long long start, void* buf, ssize_t len) { off64_t ret; ret = lseek64(fd, start, SEEK_SET); @@ -181,53 +177,46 @@ static int read_structure(int fd, unsigned long long start, void *buf, ssize_t l return 0; } -static int read_structure_blk(int fd, unsigned long long start_blk, void *buf, size_t len) -{ - return read_structure(fd, F2FS_BLKSIZE*start_blk, buf, F2FS_BLKSIZE * len); +static int read_structure_blk(int fd, unsigned long long start_blk, void* buf, size_t len) { + return read_structure(fd, F2FS_BLKSIZE * start_blk, buf, F2FS_BLKSIZE * len); } -static int read_f2fs_sb(int fd, struct f2fs_super_block *sb) -{ +static int read_f2fs_sb(int fd, struct f2fs_super_block* sb) { int rc; rc = read_structure(fd, F2FS_SUPER_OFFSET, sb, sizeof(*sb)); if (le32_to_cpu(sb->magic) != F2FS_SUPER_MAGIC) { - SLOGE("Not a valid F2FS super block. Magic:%#08x != %#08x", - le32_to_cpu(sb->magic), F2FS_SUPER_MAGIC); + SLOGE("Not a valid F2FS super block. Magic:%#08x != %#08x", le32_to_cpu(sb->magic), + F2FS_SUPER_MAGIC); return -1; } return 0; } -unsigned int get_f2fs_filesystem_size_sec(char *dev) -{ +unsigned int get_f2fs_filesystem_size_sec(char* dev) { int fd; if ((fd = open(dev, O_RDONLY)) < 0) { SLOGE("Cannot open device to get filesystem size "); return 0; } struct f2fs_super_block sb; - if(read_f2fs_sb(fd, &sb)) - return 0; - return (unsigned int)(le64_to_cpu(sb.block_count)*F2FS_BLKSIZE/DEFAULT_SECTOR_SIZE); + if (read_f2fs_sb(fd, &sb)) return 0; + return (unsigned int)(le64_to_cpu(sb.block_count) * F2FS_BLKSIZE / DEFAULT_SECTOR_SIZE); } -static struct f2fs_checkpoint *validate_checkpoint(block_t cp_addr, - unsigned long long *version, int fd) -{ +static struct f2fs_checkpoint* validate_checkpoint(block_t cp_addr, unsigned long long* version, + int fd) { unsigned char *cp_block_1, *cp_block_2; - struct f2fs_checkpoint *cp_block; + struct f2fs_checkpoint* cp_block; uint64_t cp1_version = 0, cp2_version = 0; cp_block_1 = malloc(F2FS_BLKSIZE); - if (!cp_block_1) - return NULL; + if (!cp_block_1) return NULL; /* Read the 1st cp block in this CP pack */ - if (read_structure_blk(fd, cp_addr, cp_block_1, 1)) - goto invalid_cp1; + if (read_structure_blk(fd, cp_addr, cp_block_1, 1)) goto invalid_cp1; /* get the version number */ - cp_block = (struct f2fs_checkpoint *)cp_block_1; + cp_block = (struct f2fs_checkpoint*)cp_block_1; cp1_version = le64_to_cpu(cp_block->checkpoint_ver); @@ -241,14 +230,14 @@ static struct f2fs_checkpoint *validate_checkpoint(block_t cp_addr, goto invalid_cp2; } - cp_block = (struct f2fs_checkpoint *)cp_block_2; + cp_block = (struct f2fs_checkpoint*)cp_block_2; cp2_version = le64_to_cpu(cp_block->checkpoint_ver); if (cp2_version == cp1_version) { *version = cp2_version; free(cp_block_2); - return (struct f2fs_checkpoint *)cp_block_1; + return (struct f2fs_checkpoint*)cp_block_1; } /* There must be something wrong with this checkpoint */ @@ -259,8 +248,8 @@ invalid_cp1: return NULL; } -int get_valid_checkpoint_info(int fd, struct f2fs_super_block *sb, struct f2fs_checkpoint **cp, struct f2fs_info *info) -{ +int get_valid_checkpoint_info(int fd, struct f2fs_super_block* sb, struct f2fs_checkpoint** cp, + struct f2fs_info* info) { struct f2fs_checkpoint *cp1, *cp2, *cur_cp; unsigned long blk_size; unsigned long long cp1_version = 0, cp2_version = 0; @@ -308,26 +297,25 @@ fail_no_cp: return -EINVAL; } -static int gather_sit_info(int fd, struct f2fs_info *info) -{ - uint64_t num_segments = (info->total_blocks - info->main_blkaddr - + info->blocks_per_segment - 1) / info->blocks_per_segment; +static int gather_sit_info(int fd, struct f2fs_info* info) { + uint64_t num_segments = + (info->total_blocks - info->main_blkaddr + info->blocks_per_segment - 1) / + info->blocks_per_segment; uint64_t num_sit_blocks = (num_segments + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK; uint64_t sit_block; info->sit_blocks = malloc(num_sit_blocks * sizeof(struct f2fs_sit_block)); - if (!info->sit_blocks) - return -1; + if (!info->sit_blocks) return -1; - for(sit_block = 0; sit_block<num_sit_blocks; sit_block++) { + for (sit_block = 0; sit_block < num_sit_blocks; sit_block++) { off64_t address = info->sit_blkaddr + sit_block; - if (f2fs_test_bit(sit_block, info->sit_bmp)) - address += info->blocks_per_sit; + if (f2fs_test_bit(sit_block, info->sit_bmp)) address += info->blocks_per_sit; - SLOGD("Reading cache block starting at block %"PRIu64, address); - if (read_structure(fd, address * F2FS_BLKSIZE, &info->sit_blocks[sit_block], sizeof(struct f2fs_sit_block))) { - SLOGE("Could not read sit block at block %"PRIu64, address); + SLOGD("Reading cache block starting at block %" PRIu64, address); + if (read_structure(fd, address * F2FS_BLKSIZE, &info->sit_blocks[sit_block], + sizeof(struct f2fs_sit_block))) { + SLOGE("Could not read sit block at block %" PRIu64, address); free(info->sit_blocks); info->sit_blocks = NULL; return -1; @@ -336,29 +324,27 @@ static int gather_sit_info(int fd, struct f2fs_info *info) return 0; } -static inline int is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) -{ +static inline int is_set_ckpt_flags(struct f2fs_checkpoint* cp, unsigned int f) { unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags); return !!(ckpt_flags & f); } -static inline uint64_t sum_blk_addr(struct f2fs_checkpoint *cp, struct f2fs_info *info, int base, int type) -{ - return info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_total_block_count) - - (base + 1) + type; +static inline uint64_t sum_blk_addr(struct f2fs_checkpoint* cp, struct f2fs_info* info, int base, + int type) { + return info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_total_block_count) - (base + 1) + + type; } -static int get_sit_summary(int fd, struct f2fs_info *info, struct f2fs_checkpoint *cp) -{ +static int get_sit_summary(int fd, struct f2fs_info* info, struct f2fs_checkpoint* cp) { char buffer[F2FS_BLKSIZE]; info->sit_sums = calloc(1, sizeof(struct f2fs_summary_block)); - if (!info->sit_sums) - return -1; + if (!info->sit_sums) return -1; /* CURSEG_COLD_DATA where the journaled SIT entries are. */ if (is_set_ckpt_flags(cp, CP_COMPACT_SUM_FLAG)) { - if (read_structure_blk(fd, info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_start_sum), buffer, 1)) + if (read_structure_blk(fd, info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_start_sum), + buffer, 1)) return -1; memcpy(&info->sit_sums->journal.n_sits, &buffer[SUM_JOURNAL_SIZE], SUM_JOURNAL_SIZE); } else { @@ -368,19 +354,17 @@ static int get_sit_summary(int fd, struct f2fs_info *info, struct f2fs_checkpoin else blk_addr = sum_blk_addr(cp, info, NR_CURSEG_DATA_TYPE, CURSEG_COLD_DATA); - if (read_structure_blk(fd, blk_addr, buffer, 1)) - return -1; + if (read_structure_blk(fd, blk_addr, buffer, 1)) return -1; memcpy(info->sit_sums, buffer, sizeof(struct f2fs_summary_block)); } return 0; } -struct f2fs_info *generate_f2fs_info(int fd) -{ - struct f2fs_super_block *sb = NULL; - struct f2fs_checkpoint *cp = NULL; - struct f2fs_info *info; +struct f2fs_info* generate_f2fs_info(int fd) { + struct f2fs_super_block* sb = NULL; + struct f2fs_checkpoint* cp = NULL; + struct f2fs_info* info; info = calloc(1, sizeof(*info)); if (!info) { @@ -389,7 +373,7 @@ struct f2fs_info *generate_f2fs_info(int fd) } sb = malloc(sizeof(*sb)); - if(!sb) { + if (!sb) { SLOGE("Out of memory!"); free(info); return NULL; @@ -409,11 +393,11 @@ struct f2fs_info *generate_f2fs_info(int fd) info->main_blkaddr = le32_to_cpu(sb->main_blkaddr); info->block_size = F2FS_BLKSIZE; info->total_blocks = sb->block_count; - info->blocks_per_sit = (le32_to_cpu(sb->segment_count_sit) >> 1) << le32_to_cpu(sb->log_blocks_per_seg); + info->blocks_per_sit = (le32_to_cpu(sb->segment_count_sit) >> 1) + << le32_to_cpu(sb->log_blocks_per_seg); info->blocks_per_segment = 1U << le32_to_cpu(sb->log_blocks_per_seg); - if (get_valid_checkpoint_info(fd, sb, &cp, info)) - goto error; + if (get_valid_checkpoint_info(fd, sb, &cp, info)) goto error; dbg_print_raw_ckpt_struct(cp); info->total_user_used = le32_to_cpu(cp->valid_block_count); @@ -422,20 +406,21 @@ struct f2fs_info *generate_f2fs_info(int fd) /* get sit validity bitmap */ info->sit_bmp = malloc(bmp_size); - if(!info->sit_bmp) { + if (!info->sit_bmp) { SLOGE("Out of memory!"); goto error; } info->sit_bmp_size = bmp_size; - if (read_structure(fd, info->cp_valid_cp_blkaddr * F2FS_BLKSIZE - + offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap), - info->sit_bmp, bmp_size)) { + if (read_structure(fd, + info->cp_valid_cp_blkaddr * F2FS_BLKSIZE + + offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap), + info->sit_bmp, bmp_size)) { SLOGE("Error getting SIT validity bitmap"); goto error; } - if (gather_sit_info(fd , info)) { + if (gather_sit_info(fd, info)) { SLOGE("Error getting SIT information"); goto error; } @@ -452,8 +437,7 @@ error: return NULL; } -void free_f2fs_info(struct f2fs_info *info) -{ +void free_f2fs_info(struct f2fs_info* info) { if (info) { free(info->sit_blocks); info->sit_blocks = NULL; @@ -467,23 +451,22 @@ void free_f2fs_info(struct f2fs_info *info) free(info); } -uint64_t get_num_blocks_used(struct f2fs_info *info) -{ +uint64_t get_num_blocks_used(struct f2fs_info* info) { return info->main_blkaddr + info->total_user_used; } -int f2fs_test_bit(unsigned int nr, const char *p) -{ +int f2fs_test_bit(unsigned int nr, const char* p) { int mask; - char *addr = (char *)p; + char* addr = (char*)p; addr += (nr >> 3); mask = 1 << (7 - (nr & 0x07)); return (mask & *addr) != 0; } -int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)(uint64_t pos, void *data), void *data) { - struct f2fs_sit_entry * sit_entry; +int run_on_used_blocks(uint64_t startblock, struct f2fs_info* info, + int (*func)(uint64_t pos, void* data), void* data) { + struct f2fs_sit_entry* sit_entry; uint64_t sit_block_num_cur = 0, segnum = 0, block_offset; uint64_t block; unsigned int used, found, i; @@ -498,11 +481,11 @@ int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)( } } else { /* Main Section */ - segnum = (block - info->main_blkaddr)/info->blocks_per_segment; + segnum = (block - info->main_blkaddr) / info->blocks_per_segment; /* check the SIT entries in the journal */ found = 0; - for(i = 0; i < le16_to_cpu(info->sit_sums->journal.n_sits); i++) { + for (i = 0; i < le16_to_cpu(info->sit_sums->journal.n_sits); i++) { if (le32_to_cpu(segno_in_journal(&info->sit_sums->journal, i)) == segnum) { sit_entry = &sit_in_journal(&info->sit_sums->journal, i); found = 1; @@ -513,7 +496,8 @@ int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)( /* get SIT entry from SIT section */ if (!found) { sit_block_num_cur = segnum / SIT_ENTRY_PER_BLOCK; - sit_entry = &info->sit_blocks[sit_block_num_cur].entries[segnum % SIT_ENTRY_PER_BLOCK]; + sit_entry = + &info->sit_blocks[sit_block_num_cur].entries[segnum % SIT_ENTRY_PER_BLOCK]; } block_offset = (block - info->main_blkaddr) % info->blocks_per_segment; @@ -523,10 +507,9 @@ int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)( continue; } - used = f2fs_test_bit(block_offset, (char *)sit_entry->valid_map); - if(used) - if (func(block, data)) - return -1; + used = f2fs_test_bit(block_offset, (char*)sit_entry->valid_map); + if (used) + if (func(block, data)) return -1; } block++; @@ -534,27 +517,24 @@ int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)( return 0; } -struct privdata -{ +struct privdata { int count; int infd; int outfd; char* buf; - char *zbuf; + char* zbuf; int done; - struct f2fs_info *info; + struct f2fs_info* info; }; - /* * This is a simple test program. It performs a block to block copy of a * filesystem, replacing blocks identified as unused with 0's. */ -int copy_used(uint64_t pos, void *data) -{ - struct privdata *d = data; - char *buf; +int copy_used(uint64_t pos, void* data) { + struct privdata* d = data; + char* buf; int pdone = (pos * 100) / d->info->total_blocks; if (pdone > d->done) { d->done = pdone; @@ -563,7 +543,7 @@ int copy_used(uint64_t pos, void *data) d->count++; buf = d->buf; - if(read_structure_blk(d->infd, (unsigned long long)pos, d->buf, 1)) { + if (read_structure_blk(d->infd, (unsigned long long)pos, d->buf, 1)) { printf("Error reading!!!\n"); return -1; } @@ -587,19 +567,17 @@ int copy_used(uint64_t pos, void *data) return 0; } -int main(int argc, char **argv) -{ - if (argc != 3) - printf("Usage: %s fs_file_in fs_file_out\n", argv[0]); - char *in = argv[1]; - char *out = argv[2]; +int main(int argc, char** argv) { + if (argc != 3) printf("Usage: %s fs_file_in fs_file_out\n", argv[0]); + char* in = argv[1]; + char* out = argv[2]; int infd, outfd; if ((infd = open(in, O_RDONLY)) < 0) { SLOGE("Cannot open device"); return 0; } - if ((outfd = open(out, O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR)) < 0) { + if ((outfd = open(out, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { SLOGE("Cannot open output"); return 0; } @@ -608,13 +586,13 @@ int main(int argc, char **argv) d.infd = infd; d.outfd = outfd; d.count = 0; - struct f2fs_info *info = generate_f2fs_info(infd); + struct f2fs_info* info = generate_f2fs_info(infd); if (!info) { printf("Failed to generate info!"); return -1; } - char *buf = malloc(F2FS_BLKSIZE); - char *zbuf = calloc(1, F2FS_BLKSIZE); + char* buf = malloc(F2FS_BLKSIZE); + char* zbuf = calloc(1, F2FS_BLKSIZE); d.buf = buf; d.zbuf = zbuf; d.done = 0; diff --git a/f2fs_utils/f2fs_sparseblock.h b/f2fs_utils/f2fs_sparseblock.h index e5c5f846..e047ed28 100644 --- a/f2fs_utils/f2fs_sparseblock.h +++ b/f2fs_utils/f2fs_sparseblock.h @@ -23,13 +23,13 @@ extern "C" { #endif -#define ver_after(a, b) (typecheck(unsigned long long, a) && \ - typecheck(unsigned long long, b) && \ - ((long long)((a) - (b)) > 0)) +#define ver_after(a, b) \ + (typecheck(unsigned long long, a) && typecheck(unsigned long long, b) && \ + ((long long)((a) - (b)) > 0)) -#define ver_equal(a, b) (typecheck(unsigned long long, a) && \ - typecheck(unsigned long long, b) && \ - ((long long)((a) - (b)) == 0)) +#define ver_equal(a, b) \ + (typecheck(unsigned long long, a) && typecheck(unsigned long long, b) && \ + ((long long)((a) - (b)) == 0)) struct f2fs_sit_block; struct f2fs_summary_block; @@ -38,11 +38,11 @@ struct f2fs_info { uint64_t blocks_per_segment; uint32_t block_size; - char *sit_bmp; + char* sit_bmp; uint32_t sit_bmp_size; uint64_t blocks_per_sit; - struct f2fs_sit_block *sit_blocks; - struct f2fs_summary_block *sit_sums; + struct f2fs_sit_block* sit_blocks; + struct f2fs_summary_block* sit_sums; uint64_t cp_blkaddr; uint64_t cp_valid_cp_blkaddr; @@ -59,11 +59,12 @@ struct f2fs_info { uint64_t total_blocks; }; -uint64_t get_num_blocks_used(struct f2fs_info *info); -struct f2fs_info *generate_f2fs_info(int fd); -void free_f2fs_info(struct f2fs_info *info); -unsigned int get_f2fs_filesystem_size_sec(char *dev); -int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)(uint64_t pos, void *data), void *data); +uint64_t get_num_blocks_used(struct f2fs_info* info); +struct f2fs_info* generate_f2fs_info(int fd); +void free_f2fs_info(struct f2fs_info* info); +unsigned int get_f2fs_filesystem_size_sec(char* dev); +int run_on_used_blocks(uint64_t startblock, struct f2fs_info* info, + int (*func)(uint64_t pos, void* data), void* data); #ifdef __cplusplus } diff --git a/iotop/.clang-format b/iotop/.clang-format new file mode 120000 index 00000000..fd0645fd --- /dev/null +++ b/iotop/.clang-format @@ -0,0 +1 @@ +../.clang-format-2
\ No newline at end of file diff --git a/iotop/iotop.cpp b/iotop/iotop.cpp index fe476bf5..06b82b1b 100644 --- a/iotop/iotop.cpp +++ b/iotop/iotop.cpp @@ -30,7 +30,7 @@ constexpr uint64_t NSEC_PER_SEC = 1000000000; static uint64_t BytesToKB(uint64_t bytes) { - return (bytes + 1024-1) / 1024; + return (bytes + 1024 - 1) / 1024; } static float TimeToTgidPercent(uint64_t ns, int time, const TaskStatistics& stats) { @@ -125,38 +125,38 @@ int main(int argc, char* argv[]) { break; } switch (c) { - case 'a': - accumulated = true; - break; - case 'd': - delay = atoi(optarg); - break; - case 'h': - usage(argv[0]); - return(EXIT_SUCCESS); - case 'm': - limit = atoi(optarg); - break; - case 'n': - cycles = atoi(optarg); - break; - case 's': { - sorter = GetSorter(optarg); - if (sorter == nullptr) { - LOG(ERROR) << "Invalid sort column \"" << optarg << "\""; + case 'a': + accumulated = true; + break; + case 'd': + delay = atoi(optarg); + break; + case 'h': usage(argv[0]); - return EXIT_FAILURE; + return (EXIT_SUCCESS); + case 'm': + limit = atoi(optarg); + break; + case 'n': + cycles = atoi(optarg); + break; + case 's': { + sorter = GetSorter(optarg); + if (sorter == nullptr) { + LOG(ERROR) << "Invalid sort column \"" << optarg << "\""; + usage(argv[0]); + return EXIT_FAILURE; + } + break; } - break; - } - case 'P': - processes = true; - break; - case '?': - usage(argv[0]); - return EXIT_FAILURE; - default: - abort(); + case 'P': + processes = true; + break; + case '?': + usage(argv[0]); + return EXIT_FAILURE; + default: + abort(); } } @@ -224,25 +224,14 @@ int main(int argc, char* argv[]) { printf("\n"); } if (accumulated) { - printf("%6s %-16s %20s %14s %34s\n", "", "", - "---- IO (KiB) ----", "--- faults ---", "----------- delayed on ----------"); + printf("%6s %-16s %20s %14s %34s\n", "", "", "---- IO (KiB) ----", "--- faults ---", + "----------- delayed on ----------"); } else { - printf("%6s %-16s %20s %14s %34s\n", "", "", - "--- IO (KiB/s) ---", "--- faults ---", "----------- delayed on ----------"); + printf("%6s %-16s %20s %14s %34s\n", "", "", "--- IO (KiB/s) ---", "--- faults ---", + "----------- delayed on ----------"); } - printf("%6s %-16s %6s %6s %6s %6s %6s %-5s %-5s %-5s %-5s %-5s\n", - "PID", - "Command", - "read", - "write", - "total", - "major", - "minor", - "IO", - "swap", - "sched", - "mem", - "total"); + printf("%6s %-16s %6s %6s %6s %6s %6s %-5s %-5s %-5s %-5s %-5s\n", "PID", "Command", + "read", "write", "total", "major", "minor", "IO", "swap", "sched", "mem", "total"); int n = limit; const int delay_div = accumulated ? 1 : delay; uint64_t total_read = 0; @@ -263,24 +252,21 @@ int main(int argc, char* argv[]) { n--; } - printf("%6d %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64" %5.2f%% %5.2f%% %5.2f%% %5.2f%% %5.2f%%\n", - statistics.pid(), - statistics.comm().c_str(), - BytesToKB(statistics.read()) / delay_div, - BytesToKB(statistics.write()) / delay_div, - BytesToKB(statistics.read_write()) / delay_div, - statistics.majflt(), statistics.minflt(), - TimeToTgidPercent(statistics.delay_io(), delay, statistics), - TimeToTgidPercent(statistics.delay_swap(), delay, statistics), - TimeToTgidPercent(statistics.delay_sched(), delay, statistics), - TimeToTgidPercent(statistics.delay_mem(), delay, statistics), - TimeToTgidPercent(statistics.delay_total(), delay, statistics)); + printf("%6d %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 + " %5.2f%% %5.2f%% %5.2f%% %5.2f%% %5.2f%%\n", + statistics.pid(), statistics.comm().c_str(), + BytesToKB(statistics.read()) / delay_div, BytesToKB(statistics.write()) / delay_div, + BytesToKB(statistics.read_write()) / delay_div, statistics.majflt(), + statistics.minflt(), TimeToTgidPercent(statistics.delay_io(), delay, statistics), + TimeToTgidPercent(statistics.delay_swap(), delay, statistics), + TimeToTgidPercent(statistics.delay_sched(), delay, statistics), + TimeToTgidPercent(statistics.delay_mem(), delay, statistics), + TimeToTgidPercent(statistics.delay_total(), delay, statistics)); } - printf("%6s %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64"\n", "", "TOTAL", - BytesToKB(total_read) / delay_div, - BytesToKB(total_write) / delay_div, - BytesToKB(total_read_write) / delay_div, - total_majflt / delay_div, total_minflt / delay_div); + printf("%6s %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 "\n", "", + "TOTAL", BytesToKB(total_read) / delay_div, BytesToKB(total_write) / delay_div, + BytesToKB(total_read_write) / delay_div, total_majflt / delay_div, + total_minflt / delay_div); second = false; diff --git a/iotop/tasklist.cpp b/iotop/tasklist.cpp index 07087414..dc649265 100644 --- a/iotop/tasklist.cpp +++ b/iotop/tasklist.cpp @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include <sys/types.h> #include <dirent.h> #include <errno.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> #include <map> #include <memory> @@ -27,7 +27,7 @@ #include "tasklist.h" -template<typename Func> +template <typename Func> static bool ScanPidsInDir(const std::string& name, Func f) { std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(name.c_str()), closedir); if (!dir) { @@ -59,7 +59,5 @@ bool TaskList::Scan(std::map<pid_t, std::vector<pid_t>>& tgid_map) { bool TaskList::ScanPid(pid_t tgid, std::vector<pid_t>& pid_list) { std::string filename = android::base::StringPrintf("/proc/%d/task", tgid); - return ScanPidsInDir(filename, [&pid_list](pid_t pid) { - pid_list.push_back(pid); - }); + return ScanPidsInDir(filename, [&pid_list](pid_t pid) { pid_list.push_back(pid); }); } diff --git a/iotop/tasklist.h b/iotop/tasklist.h index 1a19c8fb..071dc591 100644 --- a/iotop/tasklist.h +++ b/iotop/tasklist.h @@ -19,12 +19,12 @@ #define _IOTOP_TASKLIST_H class TaskList { -public: + public: static bool Scan(std::map<pid_t, std::vector<pid_t>>&); -private: + private: TaskList() {} static bool ScanPid(pid_t pid, std::vector<pid_t>&); }; -#endif // _IOTOP_TASKLIST_H +#endif // _IOTOP_TASKLIST_H diff --git a/iotop/taskstats.cpp b/iotop/taskstats.cpp index eff1d4df..263c92a9 100644 --- a/iotop/taskstats.cpp +++ b/iotop/taskstats.cpp @@ -13,9 +13,9 @@ // limitations under the License. #include <linux/taskstats.h> -#include <netlink/socket.h> #include <netlink/genl/ctrl.h> #include <netlink/genl/genl.h> +#include <netlink/socket.h> #include <algorithm> #include <memory> @@ -24,13 +24,10 @@ #include "taskstats.h" -TaskstatsSocket::TaskstatsSocket() - : nl_(nullptr, nl_socket_free), family_id_(0) { -} +TaskstatsSocket::TaskstatsSocket() : nl_(nullptr, nl_socket_free), family_id_(0) {} bool TaskstatsSocket::Open() { - std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl( - nl_socket_alloc(), nl_socket_free); + std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl(nl_socket_alloc(), nl_socket_free); if (!nl.get()) { LOG(ERROR) << "Failed to allocate netlink socket"; return false; @@ -44,7 +41,8 @@ bool TaskstatsSocket::Open() { int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME); if (family_id < 0) { - LOG(ERROR) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)"; + LOG(ERROR) << nl_geterror(family_id) << std::endl + << "Unable to determine taskstats family id (does your kernel support taskstats?)"; return false; } @@ -63,25 +61,23 @@ struct TaskStatsRequest { taskstats stats; }; -static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size, - taskstats* stats) { +static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size, taskstats* stats) { pid_t received_pid = -1; nla_for_each_attr(attr, attr, attr_size, attr_size) { switch (nla_type(attr)) { - case TASKSTATS_TYPE_PID: - case TASKSTATS_TYPE_TGID: - received_pid = nla_get_u32(attr); - break; - case TASKSTATS_TYPE_STATS: - { - int len = static_cast<int>(sizeof(*stats)); - len = std::min(len, nla_len(attr)); - nla_memcpy(stats, attr, len); - return received_pid; - } - default: - LOG(ERROR) << "unexpected attribute inside AGGR"; - return -1; + case TASKSTATS_TYPE_PID: + case TASKSTATS_TYPE_TGID: + received_pid = nla_get_u32(attr); + break; + case TASKSTATS_TYPE_STATS: { + int len = static_cast<int>(sizeof(*stats)); + len = std::min(len, nla_len(attr)); + nla_memcpy(stats, attr, len); + return received_pid; + } + default: + LOG(ERROR) << "unexpected attribute inside AGGR"; + return -1; } } @@ -96,28 +92,27 @@ static int ParseTaskStats(nl_msg* msg, void* arg) { nla_for_each_attr(attr, attr, remaining, remaining) { switch (nla_type(attr)) { - case TASKSTATS_TYPE_AGGR_PID: - case TASKSTATS_TYPE_AGGR_TGID: - { - nlattr* nested_attr = static_cast<nlattr*>(nla_data(attr)); - taskstats stats; - pid_t ret; - - ret = ParseAggregateTaskStats(nested_attr, nla_len(attr), &stats); - if (ret < 0) { - LOG(ERROR) << "Bad AGGR_PID contents"; - } else if (ret == taskstats_request->requested_pid) { - taskstats_request->stats = stats; - } else { - LOG(WARNING) << "got taskstats for unexpected pid " << ret << - " (expected " << taskstats_request->requested_pid << ", continuing..."; + case TASKSTATS_TYPE_AGGR_PID: + case TASKSTATS_TYPE_AGGR_TGID: { + nlattr* nested_attr = static_cast<nlattr*>(nla_data(attr)); + taskstats stats; + pid_t ret; + + ret = ParseAggregateTaskStats(nested_attr, nla_len(attr), &stats); + if (ret < 0) { + LOG(ERROR) << "Bad AGGR_PID contents"; + } else if (ret == taskstats_request->requested_pid) { + taskstats_request->stats = stats; + } else { + LOG(WARNING) << "got taskstats for unexpected pid " << ret << " (expected " + << taskstats_request->requested_pid << ", continuing..."; + } + break; } - break; - } - case TASKSTATS_TYPE_NULL: - break; - default: - LOG(ERROR) << "unexpected attribute in taskstats"; + case TASKSTATS_TYPE_NULL: + break; + default: + LOG(ERROR) << "unexpected attribute in taskstats"; } } return NL_OK; @@ -127,11 +122,10 @@ bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) { TaskStatsRequest taskstats_request = TaskStatsRequest(); taskstats_request.requested_pid = pid; - std::unique_ptr<nl_msg, decltype(&nlmsg_free)> message(nlmsg_alloc(), - nlmsg_free); + std::unique_ptr<nl_msg, decltype(&nlmsg_free)> message(nlmsg_alloc(), nlmsg_free); - genlmsg_put(message.get(), NL_AUTO_PID, NL_AUTO_SEQ, family_id_, 0, 0, - TASKSTATS_CMD_GET, TASKSTATS_VERSION); + genlmsg_put(message.get(), NL_AUTO_PID, NL_AUTO_SEQ, family_id_, 0, 0, TASKSTATS_CMD_GET, + TASKSTATS_VERSION); nla_put_u32(message.get(), type, pid); int result = nl_send_auto_complete(nl_.get(), message.get()); @@ -139,8 +133,7 @@ bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) { return false; } - std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks( - nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put); + std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks(nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put); nl_cb_set(callbacks.get(), NL_CB_VALID, NL_CB_CUSTOM, &ParseTaskStats, static_cast<void*>(&taskstats_request)); @@ -188,8 +181,7 @@ TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) { reclaim_delay_count_ = taskstats_stats.freepages_count; reclaim_delay_ns_ = taskstats_stats.freepages_delay_total; - total_delay_ns_ = - cpu_delay_ns_ + block_io_delay_ns_ + swap_in_delay_ns_ + reclaim_delay_ns_; + total_delay_ns_ = cpu_delay_ns_ + block_io_delay_ns_ + swap_in_delay_ns_ + reclaim_delay_ns_; cpu_time_real_ = taskstats_stats.cpu_run_real_total; cpu_time_virtual_ = taskstats_stats.cpu_run_virtual_total; @@ -207,9 +199,9 @@ TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) { void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) { // tgid statistics already contain delay values totalled across all pids // only add IO statistics - read_bytes_ += pid_statistics.read_bytes_; - write_bytes_ += pid_statistics.write_bytes_; - read_write_bytes_ += pid_statistics.read_write_bytes_; + read_bytes_ += pid_statistics.read_bytes_; + write_bytes_ += pid_statistics.write_bytes_; + read_write_bytes_ += pid_statistics.read_write_bytes_; cancelled_write_bytes_ += pid_statistics.cancelled_write_bytes_; if (pid_ == pid_statistics.pid_) { comm_ = pid_statistics.comm_; @@ -224,22 +216,22 @@ void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) { // Store new statistics and return the delta from the old statistics TaskStatistics TaskStatistics::Update(const TaskStatistics& new_statistics) { TaskStatistics delta = new_statistics; - delta.minflt_ -= minflt_; - delta.majflt_ -= majflt_; - delta.cpu_delay_count_ -= cpu_delay_count_; - delta.cpu_delay_ns_ -= cpu_delay_ns_; - delta.block_io_delay_count_ -= block_io_delay_count_; - delta.block_io_delay_ns_ -= block_io_delay_ns_; - delta.swap_in_delay_count_ -= swap_in_delay_count_; - delta.swap_in_delay_ns_ -= swap_in_delay_ns_; - delta.reclaim_delay_count_ -= reclaim_delay_count_; - delta.reclaim_delay_ns_ -= reclaim_delay_ns_; - delta.total_delay_ns_ -= total_delay_ns_; - delta.cpu_time_real_ -= cpu_time_real_; - delta.cpu_time_virtual_ -= cpu_time_virtual_; - delta.read_bytes_ -= read_bytes_; - delta.write_bytes_ -= write_bytes_; - delta.read_write_bytes_ -= read_write_bytes_; + delta.minflt_ -= minflt_; + delta.majflt_ -= majflt_; + delta.cpu_delay_count_ -= cpu_delay_count_; + delta.cpu_delay_ns_ -= cpu_delay_ns_; + delta.block_io_delay_count_ -= block_io_delay_count_; + delta.block_io_delay_ns_ -= block_io_delay_ns_; + delta.swap_in_delay_count_ -= swap_in_delay_count_; + delta.swap_in_delay_ns_ -= swap_in_delay_ns_; + delta.reclaim_delay_count_ -= reclaim_delay_count_; + delta.reclaim_delay_ns_ -= reclaim_delay_ns_; + delta.total_delay_ns_ -= total_delay_ns_; + delta.cpu_time_real_ -= cpu_time_real_; + delta.cpu_time_virtual_ -= cpu_time_virtual_; + delta.read_bytes_ -= read_bytes_; + delta.write_bytes_ -= write_bytes_; + delta.read_write_bytes_ -= read_write_bytes_; delta.cancelled_write_bytes_ -= cancelled_write_bytes_; *this = new_statistics; return delta; diff --git a/iotop/taskstats.h b/iotop/taskstats.h index 7aaae10a..9a82abdd 100644 --- a/iotop/taskstats.h +++ b/iotop/taskstats.h @@ -24,7 +24,7 @@ struct nl_sock; struct taskstats; class TaskStatistics { -public: + public: explicit TaskStatistics(const taskstats&); TaskStatistics() = default; TaskStatistics(const TaskStatistics&) = default; @@ -48,7 +48,7 @@ public: void set_pid(pid_t pid) { pid_ = pid; } -private: + private: std::string comm_; uid_t uid_; gid_t gid_; @@ -84,17 +84,18 @@ private: }; class TaskstatsSocket { -public: + public: TaskstatsSocket(); bool Open(); void Close(); bool GetPidStats(int, TaskStatistics&); bool GetTgidStats(int, TaskStatistics&); -private: + + private: bool GetStats(int, int, TaskStatistics& stats); - std::unique_ptr<nl_sock, void(*)(nl_sock*)> nl_; + std::unique_ptr<nl_sock, void (*)(nl_sock*)> nl_; int family_id_; }; -#endif // _IOTOP_TASKSTATS_H +#endif // _IOTOP_TASKSTATS_H diff --git a/kexec_tools/kexec.h b/kexec_tools/kexec.h index c45b1b31..a772fa6c 100644 --- a/kexec_tools/kexec.h +++ b/kexec_tools/kexec.h @@ -1,24 +1,24 @@ #ifndef _SYS_KEXEC_H #define _SYS_KEXEC_H -#include <sys/cdefs.h> #include <linux/kexec.h> -#include <unistd.h> +#include <sys/cdefs.h> #include <sys/syscall.h> +#include <unistd.h> #include "kexec.h" #define KEXEC_SEGMENT_MAX 16 #define KEXEC_TYPE_DEFAULT 0 -#define KEXEC_TYPE_CRASH 1 +#define KEXEC_TYPE_CRASH 1 /* * Prototypes */ static inline long kexec_load(unsigned int entry, unsigned long nr_segments, - struct kexec_segment *segment, unsigned long flags) { - return syscall(__NR_kexec_load, entry, nr_segments, segment, flags); + struct kexec_segment* segment, unsigned long flags) { + return syscall(__NR_kexec_load, entry, nr_segments, segment, flags); } #endif /* _SYS_KEXEC_H */ diff --git a/kexec_tools/kexecload.c b/kexec_tools/kexecload.c index 71365435..cef01475 100644 --- a/kexec_tools/kexecload.c +++ b/kexec_tools/kexecload.c @@ -1,10 +1,10 @@ #include <errno.h> -#include <getopt.h> #include <fcntl.h> -#include <sys/mman.h> -#include <stdlib.h> +#include <getopt.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <sys/mman.h> #include <sys/stat.h> #include <sys/syscall.h> #include <sys/types.h> @@ -14,20 +14,19 @@ #include "kexec.h" // Offsets same as in kernel asm/kexec.h -#define KEXEC_ARM_ATAGS_OFFSET 0x1000 +#define KEXEC_ARM_ATAGS_OFFSET 0x1000 #define KEXEC_ARM_ZIMAGE_OFFSET 0x8000 #define MEMORY_SIZE 0x0800000 // Physical buffer address cannot overlap with other regions #define START_ADDRESS 0x44000000 -#define ROUND_TO_PAGE(address,pagesize) (((address) + (pagesize) - 1) & (~((pagesize) - 1))) +#define ROUND_TO_PAGE(address, pagesize) (((address) + (pagesize)-1) & (~((pagesize)-1))) /* * Gives file position and resets current position to begining of file */ -int get_file_size(int f) -{ +int get_file_size(int f) { struct stat st; fstat(f, &st); return st.st_size; @@ -48,40 +47,33 @@ int test_kexeccall() { return 0; } -void usage(void) -{ +void usage(void) { fprintf(stderr, "usage: kexecload [ <option> ] <atags path> <kernel path>\n" "\n" "options:\n" " -t tests syscall\n" - " -s <start address> specify start address of kernel\n" - ); + " -s <start address> specify start address of kernel\n"); } /* * Loads kexec into the kernel and sets kexec on crash */ -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { int rv; - int atag_file, - zimage_file; - int atag_size, - zimage_size; - void *atag_buffer; - void *zimage_buffer; + int atag_file, zimage_file; + int atag_size, zimage_size; + void* atag_buffer; + void* zimage_buffer; struct kexec_segment segment[2]; int page_size = getpagesize(); - void *start_address = (void *)START_ADDRESS; + void* start_address = (void*)START_ADDRESS; int c; - const struct option longopts[] = { - {"start_address", required_argument, 0, 's'}, - {"test", 0, 0, 't'}, - {"help", 0, 0, 'h'}, - {0, 0, 0, 0} - }; + const struct option longopts[] = {{"start_address", required_argument, 0, 's'}, + {"test", 0, 0, 't'}, + {"help", 0, 0, 'h'}, + {0, 0, 0, 0}}; while (1) { c = getopt_long(argc, argv, "s:th", longopts, NULL); @@ -90,19 +82,19 @@ int main(int argc, char *argv[]) } /* Alphabetical cases */ switch (c) { - case 's': - start_address = (void *) strtoul(optarg, 0, 16); - break; - case 'h': - usage(); - return 1; - case 't': - test_kexeccall(); - return 1; - case '?': - return 1; - default: - abort(); + case 's': + start_address = (void*)strtoul(optarg, 0, 16); + break; + case 'h': + usage(); + return 1; + case 't': + test_kexeccall(); + return 1; + case '?': + return 1; + default: + abort(); } } @@ -118,7 +110,8 @@ int main(int argc, char *argv[]) zimage_file = open(argv[1], O_RDONLY); if (atag_file < 0 || zimage_file < 0) { - fprintf(stderr, "Error during opening of atag file or the zImage file %s\n", strerror(errno)); + fprintf(stderr, "Error during opening of atag file or the zImage file %s\n", + strerror(errno)); return 1; } @@ -130,26 +123,27 @@ int main(int argc, char *argv[]) return 1; } - atag_buffer = (char *) mmap(NULL, atag_size, PROT_READ, MAP_POPULATE | MAP_PRIVATE, atag_file, 0); - zimage_buffer = (char *) mmap(NULL, zimage_size, PROT_READ, MAP_POPULATE | MAP_PRIVATE, zimage_file, 0); + atag_buffer = (char*)mmap(NULL, atag_size, PROT_READ, MAP_POPULATE | MAP_PRIVATE, atag_file, 0); + zimage_buffer = + (char*)mmap(NULL, zimage_size, PROT_READ, MAP_POPULATE | MAP_PRIVATE, zimage_file, 0); - if(atag_buffer == MAP_FAILED || zimage_buffer == MAP_FAILED) { + if (atag_buffer == MAP_FAILED || zimage_buffer == MAP_FAILED) { fprintf(stderr, "Unable to map files into memory"); return 1; } segment[0].buf = zimage_buffer; segment[0].bufsz = zimage_size; - segment[0].mem = (void *) ((uintptr_t) start_address + KEXEC_ARM_ZIMAGE_OFFSET); + segment[0].mem = (void*)((uintptr_t)start_address + KEXEC_ARM_ZIMAGE_OFFSET); segment[0].memsz = zimage_size; segment[1].buf = atag_buffer; segment[1].bufsz = atag_size; - segment[1].mem = (void *) ((uintptr_t) start_address + KEXEC_ARM_ATAGS_OFFSET); + segment[1].mem = (void*)((uintptr_t)start_address + KEXEC_ARM_ATAGS_OFFSET); segment[1].memsz = atag_size; - rv = kexec_load(((uintptr_t) start_address + KEXEC_ARM_ZIMAGE_OFFSET), - 2, (void *) segment, KEXEC_ARCH_DEFAULT | KEXEC_ON_CRASH); + rv = kexec_load(((uintptr_t)start_address + KEXEC_ARM_ZIMAGE_OFFSET), 2, (void*)segment, + KEXEC_ARCH_DEFAULT | KEXEC_ON_CRASH); if (rv != 0) { fprintf(stderr, "Kexec_load returned non-zero exit code: %d with errno %d\n", rv, errno); @@ -160,6 +154,4 @@ int main(int argc, char *argv[]) printf("New kernel should start at 0x%08x\n", START_ADDRESS + KEXEC_ARM_ZIMAGE_OFFSET); return 0; - } - diff --git a/latencytop/latencytop.c b/latencytop/latencytop.c index d20bd7f5..84bf8744 100644 --- a/latencytop/latencytop.c +++ b/latencytop/latencytop.c @@ -26,46 +26,47 @@ #define MAX_LINE 512 #define MAX_FILENAME 64 -const char *EXPECTED_VERSION = "Latency Top version : v0.1\n"; -const char *SYSCTL_FILE = "/proc/sys/kernel/latencytop"; -const char *GLOBAL_STATS_FILE = "/proc/latency_stats"; -const char *THREAD_STATS_FILE_FORMAT = "/proc/%d/task/%d/latency"; +const char* EXPECTED_VERSION = "Latency Top version : v0.1\n"; +const char* SYSCTL_FILE = "/proc/sys/kernel/latencytop"; +const char* GLOBAL_STATS_FILE = "/proc/latency_stats"; +const char* THREAD_STATS_FILE_FORMAT = "/proc/%d/task/%d/latency"; struct latency_entry { - struct latency_entry *next; + struct latency_entry* next; unsigned long count; unsigned long max; unsigned long total; char reason[MAX_LINE]; }; -static inline void check_latencytop() { } +static inline void check_latencytop() {} -static struct latency_entry *read_global_stats(struct latency_entry *list, int erase); -static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid); -static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal); +static struct latency_entry* read_global_stats(struct latency_entry* list, int erase); +static struct latency_entry* read_process_stats(struct latency_entry* list, int erase, int pid); +static struct latency_entry* read_thread_stats(struct latency_entry* list, int erase, int pid, + int tid, int fatal); -static struct latency_entry *alloc_latency_entry(void); +static struct latency_entry* alloc_latency_entry(void); static void set_latencytop(int on); -static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list); +static struct latency_entry* read_latency_file(FILE* f, struct latency_entry* list); -static struct latency_entry *find_latency_entry(struct latency_entry *e, char *reason); -static void print_latency_entries(struct latency_entry *head); +static struct latency_entry* find_latency_entry(struct latency_entry* e, char* reason); +static void print_latency_entries(struct latency_entry* head); static void signal_handler(int sig); static void disable_latencytop(void); static int numcmp(const long long a, const long long b); -static int lat_cmp(const void *a, const void *b); +static int lat_cmp(const void* a, const void* b); static void clear_screen(void); -static void usage(const char *cmd); +static void usage(const char* cmd); -struct latency_entry *free_entries; +struct latency_entry* free_entries; -int main(int argc, char *argv[]) { - struct latency_entry *e; +int main(int argc, char* argv[]) { + struct latency_entry* e; int delay, iterations; int pid, tid; int count, erase; @@ -74,7 +75,7 @@ int main(int argc, char *argv[]) { delay = 1; iterations = 0; pid = tid = 0; - + for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d")) { if (i >= argc - 1) { @@ -118,7 +119,8 @@ int main(int argc, char *argv[]) { } if (tid && !pid) { - fprintf(stderr, "If you provide a thread ID with -t, you must provide a process ID with -p.\n"); + fprintf(stderr, + "If you provide a thread ID with -t, you must provide a process ID with -p.\n"); exit(EXIT_FAILURE); } @@ -137,7 +139,6 @@ int main(int argc, char *argv[]) { erase = 1; while ((iterations == 0) || (count++ < iterations)) { - sleep(delay); e = NULL; @@ -170,9 +171,9 @@ int main(int argc, char *argv[]) { return 0; } -static struct latency_entry *read_global_stats(struct latency_entry *list, int erase) { - FILE *f; - struct latency_entry *e; +static struct latency_entry* read_global_stats(struct latency_entry* list, int erase) { + FILE* f; + struct latency_entry* e; if (erase) { f = fopen(GLOBAL_STATS_FILE, "w"); @@ -183,7 +184,7 @@ static struct latency_entry *read_global_stats(struct latency_entry *list, int e fprintf(f, "erase\n"); fclose(f); } - + f = fopen(GLOBAL_STATS_FILE, "r"); if (!f) { fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno)); @@ -197,11 +198,11 @@ static struct latency_entry *read_global_stats(struct latency_entry *list, int e return e; } -static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid) { +static struct latency_entry* read_process_stats(struct latency_entry* list, int erase, int pid) { char dirname[MAX_FILENAME]; - DIR *dir; - struct dirent *ent; - struct latency_entry *e; + DIR* dir; + struct dirent* ent; + struct latency_entry* e; int tid; sprintf(dirname, "/proc/%d/task", pid); @@ -214,8 +215,7 @@ static struct latency_entry *read_process_stats(struct latency_entry *list, int e = list; while ((ent = readdir(dir))) { - if (!isdigit(ent->d_name[0])) - continue; + if (!isdigit(ent->d_name[0])) continue; tid = atoi(ent->d_name); @@ -227,10 +227,11 @@ static struct latency_entry *read_process_stats(struct latency_entry *list, int return e; } -static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal) { +static struct latency_entry* read_thread_stats(struct latency_entry* list, int erase, int pid, + int tid, int fatal) { char filename[MAX_FILENAME]; - FILE *f; - struct latency_entry *e; + FILE* f; + struct latency_entry* e; sprintf(filename, THREAD_STATS_FILE_FORMAT, pid, tid); @@ -248,7 +249,7 @@ static struct latency_entry *read_thread_stats(struct latency_entry *list, int e fprintf(f, "erase\n"); fclose(f); } - + f = fopen(GLOBAL_STATS_FILE, "r"); if (!f) { if (fatal) { @@ -267,8 +268,8 @@ static struct latency_entry *read_thread_stats(struct latency_entry *list, int e return e; } -static struct latency_entry *alloc_latency_entry(void) { - struct latency_entry *e; +static struct latency_entry* alloc_latency_entry(void) { + struct latency_entry* e; if (free_entries) { e = free_entries; @@ -284,14 +285,13 @@ static struct latency_entry *alloc_latency_entry(void) { return e; } -static struct latency_entry *find_latency_entry(struct latency_entry *head, char *reason) { - struct latency_entry *e; +static struct latency_entry* find_latency_entry(struct latency_entry* head, char* reason) { + struct latency_entry* e; e = head; while (e) { - if (!strcmp(e->reason, reason)) - return e; + if (!strcmp(e->reason, reason)) return e; e = e->next; } @@ -299,7 +299,7 @@ static struct latency_entry *find_latency_entry(struct latency_entry *head, char } static void set_latencytop(int on) { - FILE *f; + FILE* f; f = fopen(SYSCTL_FILE, "w"); if (!f) { @@ -312,7 +312,7 @@ static void set_latencytop(int on) { fclose(f); } -static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list) { +static struct latency_entry* read_latency_file(FILE* f, struct latency_entry* list) { struct latency_entry *e, *head; char line[MAX_LINE]; unsigned long count, max, total; @@ -337,8 +337,7 @@ static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *li e = find_latency_entry(head, reason); if (e) { e->count += count; - if (max > e->max) - e->max = max; + if (max > e->max) e->max = max; e->total += total; } else { e = alloc_latency_entry(); @@ -355,7 +354,7 @@ static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *li return head; } -static void print_latency_entries(struct latency_entry *head) { +static void print_latency_entries(struct latency_entry* head) { struct latency_entry *e, **array; unsigned long average; int i, count; @@ -368,7 +367,7 @@ static void print_latency_entries(struct latency_entry *head) { } e = head; - array = calloc(count, sizeof(struct latency_entry *)); + array = calloc(count, sizeof(struct latency_entry*)); if (!array) { fprintf(stderr, "Error allocating array: %s\n", strerror(errno)); exit(EXIT_FAILURE); @@ -378,17 +377,14 @@ static void print_latency_entries(struct latency_entry *head) { e = e->next; } - qsort(array, count, sizeof(struct latency_entry *), &lat_cmp); + qsort(array, count, sizeof(struct latency_entry*), &lat_cmp); printf("%10s %10s %7s %s\n", "Maximum", "Average", "Count", "Reason"); for (i = 0; i < count; i++) { e = array[i]; average = e->total / e->count; - printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n", - e->max / 1000, (e->max % 1000) / 10, - average / 1000, (average % 1000) / 10, - e->count, - e->reason); + printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n", e->max / 1000, (e->max % 1000) / 10, + average / 1000, (average % 1000) / 10, e->count, e->reason); } free(array); @@ -406,14 +402,15 @@ static void clear_screen(void) { printf("\n\n"); } -static void usage(const char *cmd) { - fprintf(stderr, "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n" - " -d delay Time to sleep between updates.\n" - " -n iterations Number of updates to show (0 = infinite).\n" - " -p pid Process to monitor (default is all).\n" - " -t tid Thread (within specified process) to monitor (default is all).\n" - " -h Display this help screen.\n", - cmd); +static void usage(const char* cmd) { + fprintf(stderr, + "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n" + " -d delay Time to sleep between updates.\n" + " -n iterations Number of updates to show (0 = infinite).\n" + " -p pid Process to monitor (default is all).\n" + " -t tid Thread (within specified process) to monitor (default is all).\n" + " -h Display this help screen.\n", + cmd); } static int numcmp(const long long a, const long long b) { @@ -422,11 +419,11 @@ static int numcmp(const long long a, const long long b) { return 0; } -static int lat_cmp(const void *a, const void *b) { +static int lat_cmp(const void* a, const void* b) { const struct latency_entry *pa, *pb; - pa = (*((struct latency_entry **)a)); - pb = (*((struct latency_entry **)b)); + pa = (*((struct latency_entry**)a)); + pb = (*((struct latency_entry**)b)); return numcmp(pb->max, pa->max); } diff --git a/libfscrypt/fscrypt.cpp b/libfscrypt/fscrypt.cpp index 7126b5ce..f6e97f11 100644 --- a/libfscrypt/fscrypt.cpp +++ b/libfscrypt/fscrypt.cpp @@ -98,7 +98,7 @@ static void log_ls(const char* dirname) { std::array<const char*, 3> argv = {"ls", "-laZ", dirname}; int status = 0; auto res = - logwrap_fork_execvp(argv.size(), argv.data(), &status, false, LOG_ALOG, false, nullptr); + logwrap_fork_execvp(argv.size(), argv.data(), &status, false, LOG_ALOG, false, nullptr); if (res != 0) { PLOG(ERROR) << argv[0] << " " << argv[1] << " " << argv[2] << "failed"; return; @@ -196,7 +196,7 @@ bool ParseOptionsForApiLevel(unsigned int first_api_level, const std::string& op } else { options->filenames_mode = FSCRYPT_MODE_AES_256_CTS; } - if (parts.size() > 1 && !parts[1].empty()){ + if (parts.size() > 1 && !parts[1].empty()) { if (!LookupModeByName(filenames_modes, parts[1], &options->filenames_mode)) { LOG(ERROR) << "Invalid file names encryption mode: " << parts[1]; return false; @@ -244,12 +244,16 @@ bool ParseOptionsForApiLevel(unsigned int first_api_level, const std::string& op // encryption modes. if (options->contents_mode == FSCRYPT_MODE_ADIANTUM) { if (options->filenames_mode != FSCRYPT_MODE_ADIANTUM) { - LOG(ERROR) << "Adiantum must be both contents and filenames mode or neither, invalid options: " << options_string; + LOG(ERROR) << "Adiantum must be both contents and filenames mode or neither, invalid " + "options: " + << options_string; return false; } options->flags |= FSCRYPT_POLICY_FLAG_DIRECT_KEY; } else if (options->filenames_mode == FSCRYPT_MODE_ADIANTUM) { - LOG(ERROR) << "Adiantum must be both contents and filenames mode or neither, invalid options: " << options_string; + LOG(ERROR) + << "Adiantum must be both contents and filenames mode or neither, invalid options: " + << options_string; return false; } diff --git a/libfscrypt/include/fscrypt/fscrypt.h b/libfscrypt/include/fscrypt/fscrypt.h index 78b12560..f3779d00 100644 --- a/libfscrypt/include/fscrypt/fscrypt.h +++ b/libfscrypt/include/fscrypt/fscrypt.h @@ -63,8 +63,8 @@ bool EnsurePolicy(const EncryptionPolicy& policy, const std::string& directory); inline bool operator==(const EncryptionOptions& lhs, const EncryptionOptions& rhs) { return (lhs.version == rhs.version) && (lhs.contents_mode == rhs.contents_mode) && - (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) && - (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key); + (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) && + (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key); } inline bool operator!=(const EncryptionOptions& lhs, const EncryptionOptions& rhs) { @@ -82,4 +82,4 @@ inline bool operator!=(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) } // namespace fscrypt } // namespace android -#endif // _FSCRYPT_H_ +#endif // _FSCRYPT_H_ diff --git a/libfscrypt/tests/fscrypt_test.cpp b/libfscrypt/tests/fscrypt_test.cpp index 4fbd742c..70eb1780 100644 --- a/libfscrypt/tests/fscrypt_test.cpp +++ b/libfscrypt/tests/fscrypt_test.cpp @@ -38,7 +38,7 @@ const EncryptionOptions TestString(unsigned int first_api_level, const std::stri } #define TEST_STRING(first_api_level, instring, outstring) \ - SCOPED_TRACE(instring); \ + SCOPED_TRACE(instring); \ auto options = TestString(first_api_level, instring, outstring); TEST(fscrypt, ParseOptions) { @@ -179,11 +179,12 @@ TEST(fscrypt, ParseOptions) { } TEST(fscrypt, ComparePolicies) { -#define TEST_INEQUALITY(foo, field, value) { \ - auto bar = foo; \ - bar.field = value; \ - EXPECT_NE(foo, bar); \ -} +#define TEST_INEQUALITY(foo, field, value) \ + { \ + auto bar = foo; \ + bar.field = value; \ + EXPECT_NE(foo, bar); \ + } EncryptionPolicy foo; foo.key_raw_ref = "foo"; EncryptionOptions foo_options; diff --git a/libjsonpb/.clang-format b/libjsonpb/.clang-format new file mode 120000 index 00000000..fd0645fd --- /dev/null +++ b/libjsonpb/.clang-format @@ -0,0 +1 @@ +../.clang-format-2
\ No newline at end of file diff --git a/libjsonpb/parse/include/jsonpb/error_or.h b/libjsonpb/parse/include/jsonpb/error_or.h index 66e22969..3fd3e997 100644 --- a/libjsonpb/parse/include/jsonpb/error_or.h +++ b/libjsonpb/parse/include/jsonpb/error_or.h @@ -14,7 +14,6 @@ * limitations under the License. */ - #pragma once #include <string> @@ -27,45 +26,45 @@ namespace jsonpb { template <typename T> struct ErrorOr { - template <class... Args> - explicit ErrorOr(Args&&... args) : data_(kIndex1, std::forward<Args>(args)...) {} - T& operator*() { - CHECK(ok()); - return *std::get_if<1u>(&data_); - } - const T& operator*() const { - CHECK(ok()); - return *std::get_if<1u>(&data_); - } - T* operator->() { - CHECK(ok()); - return std::get_if<1u>(&data_); - } - const T* operator->() const { - CHECK(ok()); - return std::get_if<1u>(&data_); - } - const std::string& error() const { - CHECK(!ok()); - return *std::get_if<0u>(&data_); - } - bool ok() const { return data_.index() != 0; } - static ErrorOr<T> MakeError(const std::string& message) { - return ErrorOr<T>(message, Tag::kDummy); - } + template <class... Args> + explicit ErrorOr(Args&&... args) : data_(kIndex1, std::forward<Args>(args)...) {} + T& operator*() { + CHECK(ok()); + return *std::get_if<1u>(&data_); + } + const T& operator*() const { + CHECK(ok()); + return *std::get_if<1u>(&data_); + } + T* operator->() { + CHECK(ok()); + return std::get_if<1u>(&data_); + } + const T* operator->() const { + CHECK(ok()); + return std::get_if<1u>(&data_); + } + const std::string& error() const { + CHECK(!ok()); + return *std::get_if<0u>(&data_); + } + bool ok() const { return data_.index() != 0; } + static ErrorOr<T> MakeError(const std::string& message) { + return ErrorOr<T>(message, Tag::kDummy); + } - private: - enum class Tag { kDummy }; - static constexpr std::in_place_index_t<0> kIndex0{}; - static constexpr std::in_place_index_t<1> kIndex1{}; - ErrorOr(const std::string& msg, Tag) : data_(kIndex0, msg) {} + private: + enum class Tag { kDummy }; + static constexpr std::in_place_index_t<0> kIndex0{}; + static constexpr std::in_place_index_t<1> kIndex1{}; + ErrorOr(const std::string& msg, Tag) : data_(kIndex0, msg) {} - std::variant<std::string, T> data_; + std::variant<std::string, T> data_; }; template <typename T> inline ErrorOr<T> MakeError(const std::string& message) { - return ErrorOr<T>::MakeError(message); + return ErrorOr<T>::MakeError(message); } } // namespace jsonpb diff --git a/libjsonpb/parse/include/jsonpb/jsonpb.h b/libjsonpb/parse/include/jsonpb/jsonpb.h index 350db7fb..0b91a220 100644 --- a/libjsonpb/parse/include/jsonpb/jsonpb.h +++ b/libjsonpb/parse/include/jsonpb/jsonpb.h @@ -14,7 +14,6 @@ * limitations under the License. */ - #pragma once #include <string> @@ -37,12 +36,12 @@ ErrorOr<std::monostate> JsonStringToMessage(const std::string& content, // when the android tree gets updated template <typename T> ErrorOr<T> JsonStringToMessage(const std::string& content) { - ErrorOr<T> ret; - auto error = internal::JsonStringToMessage(content, &*ret); - if (!error.ok()) { - return MakeError<T>(error.error()); - } - return ret; + ErrorOr<T> ret; + auto error = internal::JsonStringToMessage(content, &*ret); + if (!error.ok()) { + return MakeError<T>(error.error()); + } + return ret; } // TODO: MessageToJsonString is a newly added function in protobuf diff --git a/libjsonpb/parse/jsonpb.cpp b/libjsonpb/parse/jsonpb.cpp index d7feb670..3a042e71 100644 --- a/libjsonpb/parse/jsonpb.cpp +++ b/libjsonpb/parse/jsonpb.cpp @@ -33,40 +33,40 @@ using google::protobuf::util::TypeResolver; static constexpr char kTypeUrlPrefix[] = "type.googleapis.com"; std::string GetTypeUrl(const Message& message) { - return std::string(kTypeUrlPrefix) + "/" + message.GetDescriptor()->full_name(); + return std::string(kTypeUrlPrefix) + "/" + message.GetDescriptor()->full_name(); } ErrorOr<std::string> MessageToJsonString(const Message& message) { - std::unique_ptr<TypeResolver> resolver( - NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); + std::unique_ptr<TypeResolver> resolver( + NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); - google::protobuf::util::JsonOptions options; - options.add_whitespace = true; + google::protobuf::util::JsonOptions options; + options.add_whitespace = true; - std::string json; - auto status = BinaryToJsonString(resolver.get(), GetTypeUrl(message), - message.SerializeAsString(), &json, options); + std::string json; + auto status = BinaryToJsonString(resolver.get(), GetTypeUrl(message), message.SerializeAsString(), + &json, options); - if (!status.ok()) { - return MakeError<std::string>(status.error_message().as_string()); - } - return ErrorOr<std::string>(std::move(json)); + if (!status.ok()) { + return MakeError<std::string>(status.error_message().as_string()); + } + return ErrorOr<std::string>(std::move(json)); } namespace internal { ErrorOr<std::monostate> JsonStringToMessage(const std::string& content, Message* message) { - std::unique_ptr<TypeResolver> resolver( - NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); + std::unique_ptr<TypeResolver> resolver( + NewTypeResolverForDescriptorPool(kTypeUrlPrefix, DescriptorPool::generated_pool())); - std::string binary; - auto status = JsonToBinaryString(resolver.get(), GetTypeUrl(*message), content, &binary); - if (!status.ok()) { - return MakeError<std::monostate>(status.error_message().as_string()); - } - if (!message->ParseFromString(binary)) { - return MakeError<std::monostate>("Fail to parse."); - } - return ErrorOr<std::monostate>(); + std::string binary; + auto status = JsonToBinaryString(resolver.get(), GetTypeUrl(*message), content, &binary); + if (!status.ok()) { + return MakeError<std::monostate>(status.error_message().as_string()); + } + if (!message->ParseFromString(binary)) { + return MakeError<std::monostate>("Fail to parse."); + } + return ErrorOr<std::monostate>(); } } // namespace internal diff --git a/libjsonpb/verify/include/jsonpb/json_schema_test.h b/libjsonpb/verify/include/jsonpb/json_schema_test.h index 3db19310..6fa834b2 100644 --- a/libjsonpb/verify/include/jsonpb/json_schema_test.h +++ b/libjsonpb/verify/include/jsonpb/json_schema_test.h @@ -14,7 +14,6 @@ * limitations under the License. */ - #pragma once #include <unistd.h> @@ -46,12 +45,9 @@ class JsonSchemaTestConfig { /** * If it returns true, tests are skipped when the file is not found. */ - virtual bool optional() const { - return false; - } + virtual bool optional() const { return false; } }; -using JsonSchemaTestConfigFactory = - std::function<std::unique_ptr<JsonSchemaTestConfig>()>; +using JsonSchemaTestConfigFactory = std::function<std::unique_ptr<JsonSchemaTestConfig>()>; template <typename T> class AbstractJsonSchemaTestConfig : public JsonSchemaTestConfig { @@ -68,22 +64,18 @@ class AbstractJsonSchemaTestConfig : public JsonSchemaTestConfig { template <typename T> JsonSchemaTestConfigFactory MakeTestParam(const std::string& path) { - return [path]() { - return std::make_unique<AbstractJsonSchemaTestConfig<T>>(path); - }; + return [path]() { return std::make_unique<AbstractJsonSchemaTestConfig<T>>(path); }; } -class JsonSchemaTest - : public ::testing::TestWithParam<JsonSchemaTestConfigFactory> { +class JsonSchemaTest : public ::testing::TestWithParam<JsonSchemaTestConfigFactory> { public: void SetUp() override { - auto&& config = - ::testing::TestWithParam<JsonSchemaTestConfigFactory>::GetParam()(); + auto&& config = ::testing::TestWithParam<JsonSchemaTestConfigFactory>::GetParam()(); file_path_ = config->file_path(); if (access(file_path_.c_str(), F_OK) == -1) { - ASSERT_EQ(ENOENT, errno) << "File '" << file_path_ << "' is not accessible: " - << strerror(errno); + ASSERT_EQ(ENOENT, errno) << "File '" << file_path_ + << "' is not accessible: " << strerror(errno); ASSERT_TRUE(config->optional()) << "Missing mandatory file " << file_path_; GTEST_SKIP(); } @@ -92,12 +84,9 @@ class JsonSchemaTest object_ = config->CreateMessage(); auto res = internal::JsonStringToMessage(json_, object_.get()); - ASSERT_TRUE(res.ok()) << "Invalid format of file " << file_path_ - << ": " << res.error(); - } - google::protobuf::Message* message() const { - return object_.get(); + ASSERT_TRUE(res.ok()) << "Invalid format of file " << file_path_ << ": " << res.error(); } + google::protobuf::Message* message() const { return object_.get(); } std::string file_path_; std::string json_; std::unique_ptr<google::protobuf::Message> object_; diff --git a/libjsonpb/verify/include/jsonpb/verify.h b/libjsonpb/verify/include/jsonpb/verify.h index c05b13d2..bb247e7e 100644 --- a/libjsonpb/verify/include/jsonpb/verify.h +++ b/libjsonpb/verify/include/jsonpb/verify.h @@ -14,7 +14,6 @@ * limitations under the License. */ - #pragma once #include <sstream> @@ -64,8 +63,8 @@ namespace jsonpb { // path: path to navigate inside JSON tree. For example, {"foo", "bar"} for // the value "string" in // {"foo": {"bar" : "string"}} -bool AllFieldsAreKnown(const google::protobuf::Message& message, - const std::string& json, std::string* error); +bool AllFieldsAreKnown(const google::protobuf::Message& message, const std::string& json, + std::string* error); // Format the given JSON string according to Prototype T. This will serialize // the JSON string to a Prototype message, then re-print the message as JSON. By @@ -78,14 +77,12 @@ bool AllFieldsAreKnown(const google::protobuf::Message& message, // scratch_space: The scratch space to use to store the Protobuf message. It // must be a pointer // to the schema that the JSON string conforms to. -bool EqReformattedJson(const std::string& json, - google::protobuf::Message* scratch_space, +bool EqReformattedJson(const std::string& json, google::protobuf::Message* scratch_space, std::string* error); namespace internal { // See EqReformattedJson(). -ErrorOr<std::string> FormatJson(const std::string& json, - google::protobuf::Message* scratch_space); +ErrorOr<std::string> FormatJson(const std::string& json, google::protobuf::Message* scratch_space); } // namespace internal diff --git a/libjsonpb/verify/test.cpp b/libjsonpb/verify/test.cpp index cb98f47f..a430c134 100644 --- a/libjsonpb/verify/test.cpp +++ b/libjsonpb/verify/test.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ - #include <limits> #include <sstream> @@ -46,15 +45,12 @@ class JsonKeyTest : public LibJsonpbVerifyTest { } template <typename T> - void TestParseOkWithUnknownKey(const std::string& field_name, - const std::string& json_key) { + void TestParseOkWithUnknownKey(const std::string& field_name, const std::string& json_key) { std::string json = "{\"" + json_key + "\": \"test\"}"; auto object = JsonStringToMessage<T>(json); ASSERT_TRUE(object.ok()) << object.error(); - EXPECT_EQ( - "test", - object->GetReflection()->GetString( - *object, object->GetDescriptor()->FindFieldByName(field_name))); + EXPECT_EQ("test", object->GetReflection()->GetString( + *object, object->GetDescriptor()->FindFieldByName(field_name))); std::string error; ASSERT_FALSE(AllFieldsAreKnown(*object, json, &error)) << "AllFieldsAreKnown should return false"; @@ -148,8 +144,7 @@ TEST_F(JsonKeyTest, NoJsonNameQuxQuux) { class EmbeddedJsonKeyTest : public LibJsonpbVerifyTest { public: - ErrorOr<Parent> TestEmbeddedError(const std::string& json, - const std::string& unknown_key) { + ErrorOr<Parent> TestEmbeddedError(const std::string& json, const std::string& unknown_key) { auto object = JsonStringToMessage<Parent>(json); if (!object.ok()) return object; std::string error; @@ -184,30 +179,28 @@ TEST_F(EmbeddedJsonKeyTest, Ok) { } TEST_F(EmbeddedJsonKeyTest, FooBar) { - auto object = TestEmbeddedError( - "{\"with_json_name\": {\"foo_bar\": \"test\"}}", "foo_bar"); + auto object = TestEmbeddedError("{\"with_json_name\": {\"foo_bar\": \"test\"}}", "foo_bar"); ASSERT_TRUE(object.ok()) << object.error(); EXPECT_EQ("test", object->with_json_name().foo_bar()); } TEST_F(EmbeddedJsonKeyTest, BarBaz) { - auto object = TestEmbeddedError( - "{\"repeated_with_json_name\": [{\"barBaz\": \"test\"}]}", "barBaz"); + auto object = + TestEmbeddedError("{\"repeated_with_json_name\": [{\"barBaz\": \"test\"}]}", "barBaz"); ASSERT_TRUE(object.ok()) << object.error(); ASSERT_EQ(1u, object->repeated_with_json_name().size()); EXPECT_EQ("test", object->repeated_with_json_name().begin()->barbaz()); } TEST_F(EmbeddedJsonKeyTest, NoJsonName) { - auto object = TestEmbeddedError( - "{\"no_json_name\": {\"QUXQUUX\": \"test\"}}", "QUXQUUX"); + auto object = TestEmbeddedError("{\"no_json_name\": {\"QUXQUUX\": \"test\"}}", "QUXQUUX"); ASSERT_TRUE(object.ok()) << object.error(); EXPECT_EQ("test", object->no_json_name().qux_quux()); } TEST_F(EmbeddedJsonKeyTest, QuxQuux) { - auto object = TestEmbeddedError( - "{\"repeated_no_json_name\": [{\"QUXQUUX\": \"test\"}]}", "QUXQUUX"); + auto object = + TestEmbeddedError("{\"repeated_no_json_name\": [{\"QUXQUUX\": \"test\"}]}", "QUXQUUX"); ASSERT_TRUE(object.ok()) << object.error(); ASSERT_EQ(1u, object->repeated_no_json_name().size()); EXPECT_EQ("test", object->repeated_no_json_name().begin()->qux_quux()); @@ -215,24 +208,20 @@ TEST_F(EmbeddedJsonKeyTest, QuxQuux) { class ScalarTest : public LibJsonpbVerifyTest { public: - ::testing::AssertionResult IsJsonEq(const std::string& l, - const std::string& r) { + ::testing::AssertionResult IsJsonEq(const std::string& l, const std::string& r) { Json::Reader reader; Json::Value lvalue; if (!reader.parse(l, lvalue)) - return ::testing::AssertionFailure() - << reader.getFormattedErrorMessages(); + return ::testing::AssertionFailure() << reader.getFormattedErrorMessages(); Json::Value rvalue; if (!reader.parse(r, rvalue)) - return ::testing::AssertionFailure() - << reader.getFormattedErrorMessages(); + return ::testing::AssertionFailure() << reader.getFormattedErrorMessages(); Json::StyledWriter writer; return lvalue == rvalue ? (::testing::AssertionSuccess() << "Both are \n" << writer.write(lvalue)) - : (::testing::AssertionFailure() - << writer.write(lvalue) << "\n does not equal \n" - << writer.write(rvalue)); + : (::testing::AssertionFailure() << writer.write(lvalue) << "\n does not equal \n" + << writer.write(rvalue)); } bool EqReformattedJson(const std::string& json, std::string* error) { @@ -262,9 +251,8 @@ TEST_F(ScalarTest, Ok) { } using ScalarTestErrorParam = std::tuple<const char*, const char*>; -class ScalarTestError - : public ScalarTest, - public ::testing::WithParamInterface<ScalarTestErrorParam> {}; +class ScalarTestError : public ScalarTest, + public ::testing::WithParamInterface<ScalarTestErrorParam> {}; TEST_P(ScalarTestError, Test) { std::string json; @@ -273,8 +261,7 @@ TEST_P(ScalarTestError, Test) { auto formatted = FormatJson(json, &scalar_); ASSERT_TRUE(formatted.ok()) << formatted.error(); EXPECT_FALSE(IsJsonEq(json, *formatted)) << message; - EXPECT_FALSE(EqReformattedJson(json, &error_)) - << "EqReformattedJson should return false"; + EXPECT_FALSE(EqReformattedJson(json, &error_)) << "EqReformattedJson should return false"; } static const std::vector<ScalarTestErrorParam> gScalarTestErrorParams = { @@ -287,8 +274,7 @@ static const std::vector<ScalarTestErrorParam> gScalarTestErrorParams = { {"{\"e\": 1}", "Should not allow integers for enums"}, }; -INSTANTIATE_TEST_SUITE_P(Jsonpb, ScalarTestError, - ::testing::ValuesIn(gScalarTestErrorParams)); +INSTANTIATE_TEST_SUITE_P(Jsonpb, ScalarTestError, ::testing::ValuesIn(gScalarTestErrorParams)); int main(int argc, char** argv) { using ::testing::AddGlobalTestEnvironment; diff --git a/libjsonpb/verify/verify.cpp b/libjsonpb/verify/verify.cpp index c411de81..c879afb6 100644 --- a/libjsonpb/verify/verify.cpp +++ b/libjsonpb/verify/verify.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ - #include <jsonpb/verify.h> #include <iostream> @@ -47,13 +46,11 @@ const std::string& GetJsonName(const FieldDescriptor& field_descriptor) { // bumped. FieldDescriptorProto proto; field_descriptor.CopyTo(&proto); - return proto.has_json_name() ? field_descriptor.json_name() - : field_descriptor.name(); + return proto.has_json_name() ? field_descriptor.json_name() : field_descriptor.name(); } bool AllFieldsAreKnown(const Message& message, const Json::Value& json, - std::vector<std::string>* path, - std::stringstream* error) { + std::vector<std::string>* path, std::stringstream* error) { if (!json.isObject()) { *error << base::Join(*path, ".") << ": Not a JSON object\n"; return false; @@ -69,14 +66,12 @@ bool AllFieldsAreKnown(const Message& message, const Json::Value& json, } std::set<std::string> unknown_keys; - std::set_difference(json_keys.begin(), json_keys.end(), known_keys.begin(), - known_keys.end(), + std::set_difference(json_keys.begin(), json_keys.end(), known_keys.begin(), known_keys.end(), std::inserter(unknown_keys, unknown_keys.begin())); if (!unknown_keys.empty()) { *error << base::Join(*path, ".") << ": contains unknown keys: [" - << base::Join(unknown_keys, ", ") - << "]. Keys must be a known field name of " + << base::Join(unknown_keys, ", ") << "]. Keys must be a known field name of " << descriptor->full_name() << "(or its json_name option if set): [" << base::Join(known_keys, ", ") << "]\n"; return false; @@ -89,8 +84,7 @@ bool AllFieldsAreKnown(const Message& message, const Json::Value& json, std::vector<const FieldDescriptor*> set_field_descriptors; reflection->ListFields(message, &set_field_descriptors); for (auto&& field_descriptor : set_field_descriptors) { - if (field_descriptor->cpp_type() != - FieldDescriptor::CppType::CPPTYPE_MESSAGE) { + if (field_descriptor->cpp_type() != FieldDescriptor::CppType::CPPTYPE_MESSAGE) { continue; } if (field_descriptor->is_map()) { @@ -101,20 +95,17 @@ bool AllFieldsAreKnown(const Message& message, const Json::Value& json, const Json::Value& json_value = json[json_name]; if (field_descriptor->is_repeated()) { - auto&& fields = - reflection->GetRepeatedFieldRef<Message>(message, field_descriptor); + auto&& fields = reflection->GetRepeatedFieldRef<Message>(message, field_descriptor); if (json_value.type() != Json::ValueType::arrayValue) { - *error << base::Join(*path, ".") - << ": not a JSON list. This should not happen.\n"; + *error << base::Join(*path, ".") << ": not a JSON list. This should not happen.\n"; success = false; continue; } if (json_value.size() != static_cast<size_t>(fields.size())) { - *error << base::Join(*path, ".") << ": JSON list has size " - << json_value.size() << " but message has size " << fields.size() - << ". This should not happen.\n"; + *error << base::Join(*path, ".") << ": JSON list has size " << json_value.size() + << " but message has size " << fields.size() << ". This should not happen.\n"; success = false; continue; } @@ -122,8 +113,8 @@ bool AllFieldsAreKnown(const Message& message, const Json::Value& json, std::unique_ptr<Message> scratch_space(fields.NewMessage()); for (int i = 0; i < fields.size(); ++i) { path->push_back(json_name + "[" + std::to_string(i) + "]"); - auto res = AllFieldsAreKnown(fields.Get(i, scratch_space.get()), - json_value[i], path, error); + auto res = + AllFieldsAreKnown(fields.Get(i, scratch_space.get()), json_value[i], path, error); path->pop_back(); if (!res) { success = false; @@ -142,8 +133,8 @@ bool AllFieldsAreKnown(const Message& message, const Json::Value& json, return success; } -bool AllFieldsAreKnown(const google::protobuf::Message& message, - const std::string& json, std::string* error) { +bool AllFieldsAreKnown(const google::protobuf::Message& message, const std::string& json, + std::string* error) { Json::Reader reader; Json::Value value; if (!reader.parse(json, value)) { @@ -160,8 +151,7 @@ bool AllFieldsAreKnown(const google::protobuf::Message& message, return true; } -bool EqReformattedJson(const std::string& json, - google::protobuf::Message* scratch_space, +bool EqReformattedJson(const std::string& json, google::protobuf::Message* scratch_space, std::string* error) { Json::Reader reader; Json::Value old_json; @@ -207,8 +197,7 @@ bool EqReformattedJson(const std::string& json, } namespace internal { -ErrorOr<std::string> FormatJson(const std::string& json, - google::protobuf::Message* scratch_space) { +ErrorOr<std::string> FormatJson(const std::string& json, google::protobuf::Message* scratch_space) { auto res = internal::JsonStringToMessage(json, scratch_space); if (!res.ok()) { return MakeError<std::string>(res.error()); |