DF-2633 / phase2.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | /* * DF-2633 phase 2 — exercised once the PFS is at/over the ENOSPC wall: * creates, tail write, mmap write, fsync rc's. Prints KEY=VALUE lines. */ #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> #include <fcntl.h> #include <setjmp.h> #include <signal.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> static uint64_t xs64(uint64_t *s) { uint64_t x = *s; x ^= x << 13; x ^= x >> 7; x ^= x << 17; *s = x; return x; } static void fillbuf(uint64_t seed, uint8_t *buf, size_t n) { size_t i; for (i = 0; i < n; i += 8) { uint64_t r = xs64(&seed); memcpy(buf + i, &r, (n - i >= 8) ? 8 : n - i); } } static sigjmp_buf bus_jmp; static volatile sig_atomic_t got_bus; static void bus_handler(int sig) { (void)sig; got_bus = 1; siglongjmp(bus_jmp, 1); } #define NCREATE 500 #define TAILSZ (128 * 4096) #define MMSZ (1 << 20) int main(int argc, char **argv) { const char *mp, *ctrl; char path[1024]; uint8_t *tbuf; int fd, rc, i; long ok, fail; struct stat sb; if (argc != 3) { fprintf(stderr, "usage: %s <mountpoint> <controldir>\n", argv[0]); return 2; } mp = argv[1]; ctrl = argv[2]; setvbuf(stdout, NULL, _IONBF, 0); tbuf = malloc(TAILSZ > MMSZ ? TAILSZ : MMSZ); /* creates */ ok = fail = 0; for (i = 0; i < NCREATE; ++i) { snprintf(path, sizeof(path), "%s/c%04d", mp, i); rc = open(path, O_CREAT | O_EXCL | O_WRONLY, 0644); if (rc < 0) ++fail; else { ++ok; close(rc); } } { long exist = 0; for (i = 0; i < NCREATE; ++i) { snprintf(path, sizeof(path), "%s/c%04d", mp, i); if (stat(path, &sb) == 0) ++exist; } printf("REGIME_CREATES rc_ok=%ld rc_fail=%ld exist_now=%ld\n", ok, fail, exist); } /* small single-block write to existing fill.bin (append 4KB) */ snprintf(path, sizeof(path), "%s/fill.bin", mp); fd = open(path, O_WRONLY | O_APPEND); if (fd < 0) { printf("APPEND_OPEN_FAIL errno=%d (%s)\n", errno, strerror(errno)); } else { fillbuf(0x1111, tbuf, 4096); rc = write(fd, tbuf, 4096); printf("APPEND_WRITE rc=%d errno=%d (%s)\n", rc, errno, rc < 0 ? strerror(errno) : "ok"); errno = 0; rc = fsync(fd); printf("APPEND_FSYNC rc=%d errno=%d (%s)\n", rc, errno, rc ? strerror(errno) : "ok"); close(fd); } /* tail */ fillbuf(0x5EEDAB1E, tbuf, TAILSZ); snprintf(path, sizeof(path), "%s/tail.control", ctrl); fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fd >= 0) { size_t off = 0; int crc = 0; while (off < TAILSZ) { ssize_t r = write(fd, tbuf + off, TAILSZ - off); if (r < 0) { crc = -1; break; } off += r; } printf("TAIL_CONTROL rc=%d\n", crc); fsync(fd); close(fd); } snprintf(path, sizeof(path), "%s/tail.bin", mp); fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fd < 0) { printf("TAIL_OPEN_FAIL errno=%d (%s)\n", errno, strerror(errno)); } else { ok = fail = 0; for (i = 0; i < TAILSZ / 4096; ++i) { rc = write(fd, tbuf + i * 4096, 4096); if (rc < 0) ++fail; else ++ok; } printf("TAIL_WRITES ok=%ld fail=%ld\n", ok, fail); errno = 0; rc = fsync(fd); printf("TAIL_FSYNC rc=%d errno=%d (%s)\n", rc, errno, rc ? strerror(errno) : "ok"); errno = 0; rc = close(fd); printf("TAIL_CLOSE rc=%d errno=%d\n", rc, errno); } /* mmap */ fillbuf(0xAB1E5EED, tbuf, MMSZ); snprintf(path, sizeof(path), "%s/mm.control", ctrl); fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fd >= 0) { size_t off = 0; int crc = 0; while (off < MMSZ) { ssize_t r = write(fd, tbuf + off, MMSZ - off); if (r < 0) { crc = -1; break; } off += r; } printf("MMAP_CONTROL rc=%d\n", crc); fsync(fd); close(fd); } snprintf(path, sizeof(path), "%s/mm.bin", mp); fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644); if (fd < 0) { printf("MMAP_OPEN_FAIL errno=%d (%s)\n", errno, strerror(errno)); } else { if (ftruncate(fd, MMSZ) < 0) { printf("MMAP_FTRUNCATE_FAIL errno=%d (%s)\n", errno, strerror(errno)); } else { uint8_t *m = mmap(NULL, MMSZ, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (m == MAP_FAILED) { printf("MMAP_MAP_FAIL errno=%d (%s)\n", errno, strerror(errno)); } else { got_bus = 0; signal(SIGBUS, bus_handler); if (sigsetjmp(bus_jmp, 1) == 0) { memcpy(m, tbuf, MMSZ); printf("MMAP_STORES ok=1\n"); } else { printf("MMAP_STORES sigbus=1\n"); } errno = 0; rc = msync(m, MMSZ, MS_SYNC); printf("MMAP_MSYNC rc=%d errno=%d (%s) sigbus=%d\n", rc, errno, rc ? strerror(errno) : "ok", got_bus); munmap(m, MMSZ); signal(SIGBUS, SIG_DFL); } } errno = 0; rc = close(fd); printf("MMAP_CLOSE rc=%d errno=%d\n", rc, errno); } printf("PHASE2_DONE\n"); return 0; } |