1 // SPDX-License-Identifier: GPL-2.0
3 * Generates files or directories with hash collisions on a XFS filesystem
4 * Copyright (C) 2014 Hannes Frederic Sowa
23 } touch_mode = ILLEGAL;
25 static bool one_hash = false;
27 static uint32_t rol32(uint32_t word, unsigned int shift)
29 return (word << shift) | (word >> (32 - shift));
32 static uint32_t xfs_da_hashname(const uint8_t *name, int namelen)
36 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
37 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
38 (name[3] << 0) ^ rol32(hash, 7 * 4);
43 "misbalanced input buffer to xfs_da_hashname - "
44 "overlapping %d bytes\n", namelen);
51 static uint8_t gen_rand(void)
54 while (!(r = rand()));
58 static uint8_t buffer[252+1] = {0};
60 static void gen_name(void)
66 for (idx = 0; idx < 252-4; idx+=4) {
67 buffer[idx + 0] = gen_rand();
68 buffer[idx + 1] = gen_rand();
69 buffer[idx + 2] = gen_rand();
70 buffer[idx + 3] = gen_rand();
73 hash = rol32(xfs_da_hashname(buffer, 248), 7 * 4);
79 buffer[idx + 3] = last & 0x7fU;
80 buffer[idx + 2] = (last >> 7) & 0x7fU;
81 buffer[idx + 1] = (last >> 14) & 0x7fU;
82 buffer[idx + 0] = ((last >> 21) & 0xffU);
84 if (memchr(buffer, '.', sizeof(buffer)) ||
85 memchr(buffer, '/', sizeof(buffer)))
89 /* very poor - can be improved later */
90 static bool done = false;
91 static uint32_t filter;
94 filter = xfs_da_hashname(buffer, 252);
99 if (filter != xfs_da_hashname(buffer, 252))
104 static int touch(const char *buffer)
106 if (touch_mode == DIRECTORY) {
107 if (mkdir(buffer, S_IRWXU)) {
108 /* ignore if directory is already present */
111 perror("mkdir with random directory name");
114 } else if (touch_mode == FILENAME) {
115 int fd = creat(buffer, S_IRWXU);
117 /* ignore duplicate files */
120 perror("creat with random directory name");
124 perror("close is leaking a file descriptor");
132 static void do_seed(void)
135 if (gettimeofday(&tv, NULL)) {
136 perror("gettimeofday");
139 srand(tv.tv_sec ^ tv.tv_usec ^ getpid());
142 static void usage_and_exit(const char *pname)
144 fprintf(stderr, "usage: %s [-d] [-f] [-n num] [-s] directory\n"
145 "\t-f\tcreate files (the default)\n"
146 "\t-d\tcreate directories\n"
147 "\t-n num\tcreate num directories or files (default 200000)\n"
148 "\t-s\tonly generate one hash\n"
149 "\tdirectory\tthe directory to chdir() to\n",
154 int main(int argc, char **argv)
156 const char allopts[] = "hsdfn:";
157 int c, orig_cycles, errors = 0, cycles = 200000;
159 while ((c = getopt(argc, argv, allopts)) != -1) {
162 if (touch_mode != ILLEGAL)
163 usage_and_exit(argv[0]);
164 touch_mode = DIRECTORY;
167 if (touch_mode != ILLEGAL)
168 usage_and_exit(argv[0]);
169 touch_mode = FILENAME;
173 if (sscanf(optarg, "%d", &cycles) != 1 ||
175 fputs("could not parse number of iterations", stderr);
183 usage_and_exit(argv[0]);
188 if (argc <= optind || touch_mode == ILLEGAL)
189 usage_and_exit(argv[0]);
191 if (chdir(argv[optind])) {
196 orig_cycles = cycles;
202 errors += touch((char *)buffer);
206 fprintf(stderr, "creating %d %s caused %d errors\n",
207 orig_cycles, touch_mode == FILENAME ? "files" : "directories",