Author SHA1 Message Date
cnmcdee 902d10d0e7 Wire CUDA Chase-1 into deep repair (L1) + lzply -r repair mode
L1 uses chase_cuda_scan1 when a device is present (weak symbols, CPU fallback otherwise); results identical (full-scan trial counts match CPU exactly). Deep repair is now built into the main CLI as lzply -r (same levels/options as lzply_deep_repair, which is unchanged in behaviour and shares the driver via repair_cmd.c). Docs cover the CUDA path + article link. Full make test green; 524 KiB-block repair 173.7s CPU -> 1.8s GPU end to end.
2026-12-03 21:08:15 -05:00
10 changed files with 635 additions and 334 deletions

No files matched your search

+29 -8
View File
@@ -3,6 +3,17 @@ CFLAGS = -O2 -Wall -Wextra
LDLIBS = -llz
PREFIX = /usr/local
# CUDA Chase-1 (optional): nvcc lives here on hot_house (not on PATH
# by default). When it exists, chase_cuda.o is built and linked into
# lzply and lzply_deep_repair; otherwise deep_repair.c falls back to
# the CPU Chase via its weak references and the build is unchanged.
NVCC ?= /usr/local/cuda/bin/nvcc
NVCCFLAGS = -O3 -arch=sm_86 -Xcompiler -O2
CHASE_CUDA_OBJ =
ifneq ($(wildcard $(NVCC)),)
CHASE_CUDA_OBJ = chase_cuda.o
endif
all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
# Shared core: the v5 archive format, the per-entry pipeline
@@ -22,11 +33,15 @@ sha256.o: sha256.c sha256.h
bch.o: bch.c bch.h bch_polys.h
$(CC) $(CFLAGS) -c -o $@ bch.c
lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h
lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h deep_repair.h repair_cmd.h
$(CC) $(CFLAGS) -c -o $@ lzply.c
lzply: lzply.o liblzply.a
$(CC) $(CFLAGS) -o $@ lzply.o liblzply.a $(LDLIBS)
lzply: lzply.o deep_repair.o repair_cmd.o liblzrs.a liblzply.a $(CHASE_CUDA_OBJ)
ifeq ($(CHASE_CUDA_OBJ),)
$(CC) $(CFLAGS) -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a liblzply.a $(LDLIBS)
else
$(NVCC) -arch=sm_86 -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS)
endif
# FUSE filesystem. Needs libfuse3-dev; no pkg-config on the server, so
# the flags are explicit (-D_FILE_OFFSET_BITS=64 is required by FUSE).
@@ -90,8 +105,7 @@ bch_test: bch.c bch_test.c bch.h bch_polys.h
# CUDA Chase-1 bench (experiments/cuda-chase): GPU port of the
# deep-repair Chase-1 scan (t=8 only). Needs CUDA 13.x; NVCC is
# /usr/local/cuda/bin/nvcc on hot_house (not on PATH by default).
NVCC ?= /usr/local/cuda/bin/nvcc
NVCCFLAGS = -O3 -arch=sm_86 -Xcompiler -O2
# NVCC / NVCCFLAGS are defined at the top of this file.
chase_bench_cpu: chase_bench.c bch.c bch.h bch_polys.h
$(CC) $(CFLAGS) -o $@ chase_bench.c bch.c
@@ -137,11 +151,18 @@ crc16_test: crc16_test.c liblzply.a
deep_repair.o: deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h rs.h sha256.h
$(CC) $(CFLAGS) -c -o $@ deep_repair.c
lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h
lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h repair_cmd.h
$(CC) $(CFLAGS) -c -o $@ lzply_deep_repair.c
lzply_deep_repair: lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a
$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o deep_repair.o liblzrs.a liblzply.a $(LDLIBS)
repair_cmd.o: repair_cmd.c repair_cmd.h deep_repair.h lzply_lib.h lzrs_lib.h bch.h
$(CC) $(CFLAGS) -c -o $@ repair_cmd.c
lzply_deep_repair: lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a liblzply.a $(CHASE_CUDA_OBJ)
ifeq ($(CHASE_CUDA_OBJ),)
$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a liblzply.a $(LDLIBS)
else
$(NVCC) -arch=sm_86 -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS)
endif
# bch_polys.h is generated by gen_bch_polys.py from the rssearch data
# (~/workspace/rssearch/polys.txt + finds.txt) and checked in; it is
+8 -3
View File
@@ -191,7 +191,8 @@ is the only LZRS format.
## Deep repair — past the BCH limit
`lzply_deep_repair` recovers entries whose bit errors exceed the
`lzply_deep_repair` -- also built into the main tool as `lzply -r` --
recovers entries whose bit errors exceed the
BCH correction limit t, in escalating levels, reporting per-level
timing ("time of arrival": how long each level took and at which
level the repair landed):
@@ -227,7 +228,9 @@ CRC-checked, 1,024 rejected by CRC, 0 false accepts.
Limits, measured honestly: Chase scans are O(nbits) trials at
~7,800 trials/s on m=22 blocks (a full scan of a 524 KiB block
takes ~9 minutes), so beyond-t repair of large blocks is bounded
takes ~9 minutes on the CPU; ~0.35 s on a CUDA build -- the L1
scan runs on the GPU when a device is present, with CPU fallback),
so beyond-t repair of large blocks is bounded
by the time budget (default 120 s per entry) and reports partial
coverage rather than pretending. Two bugs found by measurement
and fixed: the trial flip was not applied to the reconstructed
@@ -236,7 +239,9 @@ candidates before trying them, truncating the list before the
genuine candidate arrived (a 600 KiB file with 9 errors in one
block failed at a 60 s budget before the fix; it recovers after
it). Full design, bounds, and measured numbers:
`docs/deep-repair.md`. Tests: `tests/run_deep_repair_tests.sh`
`docs/deep-repair.md`. CUDA Chase write-up:
https://www.lzply.com/cuda-chase-500x-deep-repair/ . Tests:
`tests/run_deep_repair_tests.sh`
(round-trips, old-format rejection, corruption matrix,
false-accept measurement).
+56 -3
View File
@@ -23,6 +23,16 @@
#include "rs.h"
#include "sha256.h"
/* CUDA Chase-1 (chase_cuda.cu), linked only when nvcc is available
* at build time; weak so a CPU-only build links and runs unchanged
* (same pattern as bch_cuda in bch.c on main). */
extern int chase_cuda_available(void) __attribute__((weak));
extern int chase_cuda_scan1(const uint8_t *in, size_t in_len, size_t data_len,
const bch_poly_t *p, uint8_t *cands, int max_cands,
const size_t *suspects, int nsuspects,
long *trials_out, long *split_pass_out,
long *codewords_out) __attribute__((weak));
/* Decompress a blob and verify its SHA-256. Returns a malloc'd raw
* buffer (caller frees) or NULL on failure. Level 0 means stored
* uncompressed (the blob is the raw bytes). */
@@ -139,6 +149,42 @@ static void pool_free(block_pool_t *pool, size_t *pool_bytes)
pool->dlen = 0;
}
/* Chase-1 for one block: the CUDA scan when a device is present,
* the CPU scan otherwise. Same candidate contract as
* bch_chase1_block (suspects first, then ascending positions;
* candidates in scan order, so the L1 direct-try order is
* unchanged). *used_cuda is set to 1 when the GPU scan ran.
* The GPU scan has no deadline (it runs the full scan in ~0.35 s
* on a 524 KiB block); when the deadline is already past, the CPU
* scan runs instead and reports partial as usual. A -2 from the
* GPU scan (not t=8, or CUDA failed) also falls back to CPU. */
static int chase1_block(const uint8_t *in, size_t in_len, size_t data_len,
const bch_poly_t *p, uint8_t *cands, int max_cands,
const size_t *suspects, int nsuspects,
bch_chase_stats_t *stats, double deadline,
int *used_cuda)
{
*used_cuda = 0;
if (chase_cuda_available && chase_cuda_available() &&
(deadline < 0 || bch_now() < deadline)) {
long trials = 0, split_pass = 0, codewords = 0;
int ncc = chase_cuda_scan1(in, in_len, data_len, p, cands,
max_cands, suspects, nsuspects,
&trials, &split_pass, &codewords);
if (ncc >= 0) {
stats->trials = trials;
stats->split_pass = split_pass;
stats->codewords = codewords;
stats->candidates = ncc;
stats->partial = 0;
*used_cuda = 1;
return ncc;
}
}
return bch_chase1_block(in, in_len, data_len, p, cands, max_cands,
suspects, nsuspects, stats, deadline);
}
int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
int is_bch, uint16_t poly_id, int rs_level,
const uint8_t want_sha[32], size_t blob_size,
@@ -372,7 +418,8 @@ int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
free(bc);
}
added1 = pools[bi].n; /* pool size after pass-2 */
/* Chase-1 candidates (suspects first, then scan). */
/* Chase-1 candidates (suspects first, then scan) --
* GPU when a device is present, else CPU. */
{
size_t *susp = malloc(sizeof(size_t) *
BCH_CHASE_MAX_SUSPECTS);
@@ -389,10 +436,16 @@ int deep_repair_entry(const uint8_t *stored, size_t stored_len, int level,
bslen[bi], bdlen[bi],
poly, susp,
BCH_CHASE_MAX_SUSPECTS);
ncc = bch_chase1_block(bch_stored + s_off,
{
int used_cuda = 0;
ncc = chase1_block(bch_stored + s_off,
bslen[bi], bdlen[bi], poly,
cc, BCH_CHASE_MAX_CANDS,
susp, ns, &cs, deadline);
susp, ns, &cs, deadline,
&used_cuda);
if (used_cuda)
stats->chase_cuda_used = 1;
}
stats->trials[1] += cs.trials;
stats->split_pass[1] += cs.split_pass;
stats->codewords[1] += cs.codewords;
+1
View File
@@ -67,6 +67,7 @@ typedef struct {
long blocks_total; /* BCH blocks in the blob */
long blocks_failed; /* blocks failing standard decode */
int partial; /* a level stopped early on its budget */
int chase_cuda_used; /* L1 ran on the GPU (CUDA Chase-1 scan) */
char fail_stage[24]; /* "ok", "rs", "len", "crc", "sha",
"budget", "noecc", "nocand" */
} deep_stats_t;
+56 -5
View File
@@ -1,11 +1,13 @@
# Deep repair — past the BCH limit t
`lzply_deep_repair` recovers archive entries whose bit errors exceed
Deep repair recovers archive entries whose bit errors exceed
the BCH correction limit t, in escalating levels, reporting
per-level timing ("time of arrival": how long each level took and
at which level the repair landed). The CRC16 is the oracle: every
beyond-t candidate is CRC-verified before acceptance, then
decompressed and SHA-256-checked.
at which level the repair landed). It is built into the main tool
(`lzply -r archive`, same levels and options as the standalone
`lzply_deep_repair` binary, which is still shipped). The CRC16
is the oracle: every beyond-t candidate is CRC-verified before
acceptance, then decompressed and SHA-256-checked.
## The levels
@@ -37,6 +39,43 @@ decompressed and SHA-256-checked.
holds its true data in the pool but neither single-block
variation passes the CRC.
## The CUDA Chase-1 scan (L1 on the GPU)
On a CUDA build (nvcc present at build time -- `make` links
`chase_cuda.o` into `lzply` and `lzply_deep_repair` automatically;
without nvcc the build is CPU-only and nothing changes), the L1
Chase-1 scan runs on the GPU when a CUDA device is present. The
dispatch is automatic: there is no flag. The scan is the same
algorithm as the CPU one (one thread per trial position:
Berlekamp-Massey + the lambda-splits test, Chien search in
parallel for survivors, host syndrome-zero verification), and
produces the same candidates in the same order -- verified
bit-identical across the 5-pattern validation and a 24-pattern
sweep (10/10 CPU/CUDA hash agreements, zero mismatches).
Measured on hot_house (RTX 3080 Ti), one 524 KiB block (m=22,
t=8, -e4), 4,194,296 trials: **171.571 s CPU -> 0.335-0.379 s
CUDA (453-512x on the timed scan, ~190x end to end)**; the
extended sweep runs 453-562x. A beyond-t repair of a 524 KiB
block that took ~8 minutes at L1 now lands in under a second.
`-v` marks the L1 line `(CUDA)` when the GPU scan ran.
Fallback: the CPU scan runs whenever the GPU scan cannot -- no
CUDA device, a CPU-only build, a non-t=8 poly (the kernel is
t=8 only), a CUDA failure, or a deadline already past when the
scan would start. The symbols are referenced weakly, so a
CPU-only build links and runs unchanged.
Limits: the kernel is t=8 only; Chase-2 is not ported (L2 stays
on the CPU). The GPU scan has no deadline -- it runs the full
scan, then truncates at `BCH_CHASE_MAX_CANDS` (the CPU scan
stops early at the cap when the budget hits); the candidate set
still matches. On small blocks the ~0.115 s CUDA first-touch
cost dominates -- a 512-byte block repairs in ~0.11 s on the GPU
vs ~0.04 s on the CPU -- so the win is on large blocks.
Full write-up: https://www.lzply.com/cuda-chase-500x-deep-repair/
## Why the suspect set does not help Chase-2 (much)
With hard-decision input there is no channel reliability, so
@@ -98,6 +137,16 @@ lzply_deep_repair [-l max-level 0..3] [--time-budget seconds] [-v]
[-o outdir] [--entry name] archive
```
The same repair is built into the main tool:
```
lzply -r [--level 0..3] [--time-budget seconds] [-v]
[-o outdir] [--entry name] archive [outdir]
```
`-r` takes the same levels, budget, and output options
(`--level` in place of `-l`, which is the list mode in `lzply`).
Per-entry output:
```
@@ -168,7 +217,9 @@ wrong-but-valid candidates per trial and the CRC rejects all
`bch_test` 7,975 checks still pass). A full Chase-1 scan of a
524 KiB block (4,194,296 trials) takes **471.9 s** (measured,
probe5: RECOVERED at L1 in 471.918 s, trials 4,194,296,
split 126, codewords 126). The tool scans fully before trying
split 126, codewords 126). On a CUDA build the same scan runs
on the GPU in ~0.35 s (see the CUDA section above); 471.9 s is
the CPU scan. The tool scans fully before trying
candidates (the chase layer has no oracle), so the landing time
is the full-scan time, not the time to the lowest true error.
At the default 120 s budget a beyond-t repair of a 524 KiB
+95 -3
View File
@@ -105,7 +105,9 @@
#include <unistd.h>
#include "bch.h"
#include "deep_repair.h"
#include "lzply_lib.h"
#include "repair_cmd.h"
@@ -1571,7 +1573,7 @@ static int cmd_tree(const char *archive, const char **selectors,
static void usage(FILE *out)
{
fprintf(out,
"Usage: %s -c|-d|-l|-t|-a|-i|-tr [options] <input> [output] [selectors...]\n"
"Usage: %s -c|-d|-l|-t|-a|-i|-tr|-r [options] <input> [output] [selectors...]\n"
"\n"
" -c compress a file or directory into one .lzply archive\n"
" -a append files/dirs to an archive (creates it if\n"
@@ -1594,6 +1596,15 @@ static void usage(FILE *out)
" -tr print an archive as a tree, like the Linux\n"
" `tree` command, with a directory/file count.\n"
" Selectors filter it.\n"
" -r repair an archive: recover entries whose bit\n"
" errors exceed the BCH limit t, in escalating\n"
" levels L0-L3 (standard decode, Chase-1,\n"
" Chase-2, cross-block). Recovered files go to\n"
" [output] dir (default ./repaired). --level N\n"
" caps the level (default 3), --time-budget S\n"
" the seconds per entry (default 120). On a\n"
" CUDA build the L1 Chase-1 scan runs on the\n"
" GPU when a device is present, else CPU.\n"
" -datestart <date> only entries with mtime on or after this date\n"
" -dateend <date> only entries with mtime on or before this date\n"
" Dates are YYYY-MM-DD (whole day, inclusive) or\n"
@@ -1648,16 +1659,18 @@ static void usage(FILE *out)
" %s -i backup.lzply -datestart 2024-01-01 -dateend 2024-06-30\n"
" inspect only Jan-Jun 2024 entries\n"
" %s -tr backup.lzply photos/ -year 2025 tree of 2025 photos only\n"
" %s -r backup.lzply repair into ./repaired\n"
" %s -r -o fixed/ backup.lzply repair into fixed/\n"
"\n"
"Exit codes: 0 ok, 1 usage, 2 I/O error, 3 corrupt/unrecoverable.\n",
lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname,
lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname, lzply_progname,
lzply_progname, lzply_progname, lzply_progname, lzply_progname,
lzply_progname);
lzply_progname, lzply_progname, lzply_progname);
}
enum { MODE_NONE, MODE_COMPRESS, MODE_DECOMPRESS, MODE_LIST, MODE_TEST,
MODE_APPEND, MODE_INSPECT, MODE_TREE };
MODE_APPEND, MODE_INSPECT, MODE_TREE, MODE_REPAIR };
/* Parse a poly=NN value: hex if it has a 0x prefix or any a-f/A-F
* letter, else decimal. Returns the poly, or NULL if it is not a
@@ -1696,6 +1709,11 @@ int main(int argc, char **argv)
int ecc_level = 4; /* default -e 4 -> BCH t=8 */
const bch_poly_t *pinned_poly = NULL;
int ow_mode = OW_ASK;
const char *repair_outdir = "./repaired";
int repair_verbose = 0;
int repair_max_level = DEEP_MAX_LEVEL;
double repair_budget = DEEP_DEFAULT_BUDGET_SEC;
const char *repair_only_entry = NULL;
date_filter_t dfilter;
const char **args = NULL; /* positional arguments after the options */
int nargs = 0;
@@ -1744,6 +1762,64 @@ int main(int argc, char **argv)
mode = MODE_INSPECT;
} else if (strcmp(a, "-tr") == 0 || strcmp(a, "--tree") == 0) {
mode = MODE_TREE;
} else if (strcmp(a, "-r") == 0 || strcmp(a, "--repair") == 0) {
mode = MODE_REPAIR;
} else if (strcmp(a, "-o") == 0 || strcmp(a, "--output") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "%s: -o needs a directory\n",
lzply_progname);
return EXIT_USAGE;
}
repair_outdir = argv[++i];
} else if (strcmp(a, "-v") == 0 || strcmp(a, "--verbose") == 0) {
repair_verbose = 1;
} else if (strcmp(a, "--level") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "%s: --level needs 0..3\n",
lzply_progname);
return EXIT_USAGE;
}
repair_max_level = atoi(argv[++i]);
if (repair_max_level < 0 || repair_max_level > DEEP_MAX_LEVEL) {
fprintf(stderr, "%s: --level must be 0..3\n",
lzply_progname);
return EXIT_USAGE;
}
} else if (strncmp(a, "--level=", 8) == 0) {
repair_max_level = atoi(a + 8);
if (repair_max_level < 0 || repair_max_level > DEEP_MAX_LEVEL) {
fprintf(stderr, "%s: --level must be 0..3\n",
lzply_progname);
return EXIT_USAGE;
}
} else if (strcmp(a, "--time-budget") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "%s: --time-budget needs seconds\n",
lzply_progname);
return EXIT_USAGE;
}
repair_budget = atof(argv[++i]);
if (repair_budget <= 0) {
fprintf(stderr, "%s: --time-budget must be > 0\n",
lzply_progname);
return EXIT_USAGE;
}
} else if (strncmp(a, "--time-budget=", 14) == 0) {
repair_budget = atof(a + 14);
if (repair_budget <= 0) {
fprintf(stderr, "%s: --time-budget must be > 0\n",
lzply_progname);
return EXIT_USAGE;
}
} else if (strcmp(a, "--entry") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "%s: --entry needs a stored path\n",
lzply_progname);
return EXIT_USAGE;
}
repair_only_entry = argv[++i];
} else if (strncmp(a, "--entry=", 8) == 0) {
repair_only_entry = a + 8;
} else if (strcmp(a, "-datestart") == 0 ||
strcmp(a, "--datestart") == 0) {
if (i + 1 >= argc) {
@@ -1931,6 +2007,22 @@ int main(int argc, char **argv)
nargs >= 2 ? nargs - 1 : 0,
&dfilter);
break;
case MODE_REPAIR:
/* archive, optional outdir. */
if (nargs < 1 || nargs > 2) {
fprintf(stderr,
"%s: -r needs an archive and an optional output directory\n",
lzply_progname);
usage(stderr);
rc = EXIT_USAGE;
break;
}
if (nargs == 2)
repair_outdir = args[1];
rc = repair_archive(lzply_progname, args[0], repair_outdir,
repair_max_level, repair_budget,
repair_only_entry, repair_verbose);
break;
default:
rc = EXIT_USAGE;
break;
+6 -312
View File
@@ -18,27 +18,23 @@
* and how many bits were corrected. -v adds the per-level trial
* counts (BM trials, splitting-filter passes, valid codewords).
*
* The archive-walking driver lives in repair_cmd.c (shared with
* the lzply -r mode); this file is only the option parsing.
*
* Exit codes: 0 all entries recovered, 1 usage, 2 I/O error,
* 3 any entry unrecoverable (matching lzply's EXIT_CORRUPT).
*/
#define _FILE_OFFSET_BITS 64
#define _DEFAULT_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "bch.h"
#include "deep_repair.h"
#include "lzply_lib.h"
#include "lzrs_lib.h"
#include "repair_cmd.h"
static const char *prog = "lzply_deep_repair";
@@ -57,56 +53,6 @@ static void usage(void)
prog, DEEP_DEFAULT_BUDGET_SEC);
}
/* mkdir -p (mode 0755 for created parents). Returns 0 on success. */
static int mkdir_p(const char *path, mode_t mode)
{
char *tmp = xstrdup(path);
size_t n = strlen(tmp);
size_t i;
if (n == 0) {
free(tmp);
return 0;
}
while (n > 1 && tmp[n - 1] == '/')
tmp[--n] = '\0';
for (i = 1; i <= n; i++) {
if (tmp[i] == '/' || tmp[i] == '\0') {
char save = tmp[i];
struct stat st;
tmp[i] = '\0';
if (stat(tmp, &st) != 0) {
if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
free(tmp);
return -1;
}
} else if (!S_ISDIR(st.st_mode)) {
free(tmp);
return -1;
}
tmp[i] = save;
}
}
free(tmp);
return 0;
}
static int write_out_file(const char *path, const uint8_t *data, size_t len,
uint32_t mode)
{
FILE *f = fopen(path, "wb");
if (!f)
return -1;
if (len > 0 && fwrite(data, 1, len, f) != len) {
fclose(f);
return -1;
}
if (fclose(f) != 0)
return -1;
if (mode)
chmod(path, (mode_t)(mode & 07777));
return 0;
}
int main(int argc, char **argv)
{
int max_level = DEEP_MAX_LEVEL;
@@ -115,9 +61,6 @@ int main(int argc, char **argv)
const char *outdir = "./repaired";
const char *only_entry = NULL;
const char *archive = NULL;
int is_lzrs = 0;
FILE *f = NULL;
uint8_t magic[4];
int opt;
static struct option long_opts[] = {
{ "time-budget", required_argument, NULL, 1000 },
@@ -167,255 +110,6 @@ int main(int argc, char **argv)
}
archive = argv[optind];
f = fopen(archive, "rb");
if (!f) {
fprintf(stderr, "%s: cannot open %s: %s\n", prog, archive,
strerror(errno));
return EXIT_IO;
}
if (fread(magic, 1, 4, f) != 4) {
fprintf(stderr, "%s: %s is too short to be an archive\n",
prog, archive);
fclose(f);
return EXIT_CORRUPT;
}
if (memcmp(magic, "LZRS", 4) == 0)
is_lzrs = 1;
else if (memcmp(magic, "LZPL", 4) != 0) {
fprintf(stderr, "%s: %s is not an LZPL or LZRS archive\n",
prog, archive);
fclose(f);
return EXIT_CORRUPT;
}
/* Validate the archive up front (version, headers, end marker),
* so a structurally broken archive fails before any repair is
* attempted. The scan also gives the clear v4 message for old
* LZPL archives. */
{
int version = 0;
uint32_t count = 0;
off_t size = 0, end_off = 0;
scan_list_t entries;
int s;
rewind(f);
if (is_lzrs)
s = lzrs_scan_archive(archive, &version, &count, &size,
&entries, &end_off);
else
s = scan_archive(archive, &version, &count, &size,
&entries, &end_off);
if (s != 0) {
fclose(f);
return EXIT_CORRUPT;
}
scan_list_free(&entries);
rewind(f);
}
if (mkdir_p(outdir, 0755) != 0) {
fprintf(stderr, "%s: cannot create output directory %s: %s\n",
prog, outdir, strerror(errno));
fclose(f);
return EXIT_IO;
}
/* Skip the 16-byte global header; then walk the entries. */
if (fseeko(f, 16, SEEK_SET) != 0) {
fprintf(stderr, "%s: cannot seek in %s\n", prog, archive);
fclose(f);
return EXIT_IO;
}
{
int worst = 0;
long n_ok = 0, n_fail = 0, n_dir = 0;
long landed[4] = { 0, 0, 0, 0 };
double t_start = bch_now();
for (;;) {
char *path = NULL;
int level = 0, is_bch = 0, rs_level = 0, is_dir = 0;
uint16_t poly_id = 0, want_crc16 = 0;
uint64_t stored_size = 0, blob_size = 0, orig_size = 0;
uint32_t mode = 0;
uint8_t sha[32];
uint8_t *stored = NULL;
int r;
if (is_lzrs) {
lzrs_ehdr_t h;
r = lzrs_read_entry_header(f, &h);
if (r == 1)
break;
if (r < 0) {
fprintf(stderr, "%s: truncated or corrupt entry header\n",
prog);
worst = EXIT_CORRUPT;
break;
}
path = h.path;
level = h.level;
is_bch = (h.flags & FLAG_BCH) != 0;
rs_level = h.rs_level;
is_dir = (h.flags & FLAG_ISDIR) != 0;
poly_id = h.bch_poly_id;
want_crc16 = h.crc16;
stored_size = h.stored_size;
blob_size = h.blob_size;
orig_size = h.orig_size;
mode = h.mode;
memcpy(sha, h.sha, 32);
} else {
ehdr_t h;
r = read_entry_header(f, &h);
if (r == 1)
break;
if (r < 0) {
fprintf(stderr, "%s: truncated or corrupt entry header\n",
prog);
worst = EXIT_CORRUPT;
break;
}
path = h.path;
level = h.level;
is_bch = (h.flags & FLAG_BCH) != 0;
rs_level = 0;
is_dir = (h.flags & FLAG_ISDIR) != 0;
poly_id = (uint16_t)(h.rs_level |
((uint16_t)h.reserved << 8));
want_crc16 = h.crc16;
stored_size = h.stored_size;
blob_size = h.blob_size;
orig_size = h.orig_size;
mode = h.mode;
memcpy(sha, h.sha, 32);
}
stored = xmalloc(stored_size ? (size_t)stored_size : 1);
if (stored_size > 0 &&
fread(stored, 1, (size_t)stored_size, f) !=
(size_t)stored_size) {
fprintf(stderr, "%s: truncated data for %s\n", prog, path);
free(stored);
free(path);
worst = EXIT_CORRUPT;
break;
}
if (only_entry && strcmp(path, only_entry) != 0) {
free(stored);
free(path);
continue;
}
if (is_dir) {
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
sprintf(dp, "%s/%s", outdir, path);
if (mkdir_p(dp, mode ? (mode_t)(mode & 07777) : 0755) != 0)
fprintf(stderr, "%s: cannot create directory %s: %s\n",
prog, dp, strerror(errno));
free(dp);
free(stored);
free(path);
n_dir++;
continue;
}
{
uint8_t *raw = NULL;
size_t raw_len = 0;
deep_stats_t st;
int drc = deep_repair_entry(stored, (size_t)stored_size,
level, is_bch, poly_id,
rs_level, sha,
(size_t)blob_size, want_crc16,
max_level, budget,
&raw, &raw_len, &st);
free(stored);
if (drc != 0) {
printf("%s: FAILED (stage %s) after %.3fs, "
"cands %ld crc-rejects %ld sha-rejects %ld\n",
path, st.fail_stage, st.t_total,
st.cands_tried, st.crc_rejects, st.sha_rejects);
if (verbose) {
int lv;
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
printf(" L%d: %.3fs trials %ld split %ld "
"codewords %ld\n", lv, st.t_level[lv],
st.trials[lv], st.split_pass[lv],
st.codewords[lv]);
printf(" blocks %ld/%ld failed%s\n",
st.blocks_failed, st.blocks_total,
st.partial ? " (partial: budget hit)" : "");
}
n_fail++;
if (EXIT_CORRUPT > worst)
worst = EXIT_CORRUPT;
free(path);
continue;
}
if (raw_len != orig_size) {
fprintf(stderr, "%s: %s: size mismatch after repair "
"(got %zu, expected %llu)\n", prog, path,
raw_len, (unsigned long long)orig_size);
free(raw);
free(path);
n_fail++;
if (EXIT_CORRUPT > worst)
worst = EXIT_CORRUPT;
continue;
}
{
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
char *pd;
sprintf(dp, "%s/%s", outdir, path);
pd = parent_dir(dp);
if (mkdir_p(pd, 0755) != 0)
fprintf(stderr, "%s: cannot create %s: %s\n",
prog, pd, strerror(errno));
free(pd);
if (write_out_file(dp, raw, raw_len, mode) != 0) {
fprintf(stderr, "%s: cannot write %s: %s\n",
prog, dp, strerror(errno));
if (EXIT_IO > worst)
worst = EXIT_IO;
free(dp);
free(raw);
free(path);
continue;
}
free(dp);
}
{
char rssuf[48];
rssuf[0] = '\0';
if (st.rs_corrected > 0)
snprintf(rssuf, sizeof rssuf,
" (RS corrected %d symbols)",
st.rs_corrected);
printf("%s: RECOVERED at L%d in %.3fs "
"(L0 %.3fs, L1 %.3fs, L2 %.3fs, L3 %.3fs) "
"cands %ld crc-rejects %ld sha-rejects %ld "
"bch-corrected %d%s\n",
path, st.level_landed, st.t_total,
st.t_level[0], st.t_level[1], st.t_level[2],
st.t_level[3], st.cands_tried, st.crc_rejects,
st.sha_rejects, st.bch_corrected, rssuf);
}
if (verbose) {
int lv;
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
printf(" L%d: %.3fs trials %ld split %ld "
"codewords %ld\n", lv, st.t_level[lv],
st.trials[lv], st.split_pass[lv],
st.codewords[lv]);
printf(" blocks %ld/%ld failed%s\n",
st.blocks_failed, st.blocks_total,
st.partial ? " (partial: budget hit)" : "");
}
landed[st.level_landed]++;
n_ok++;
free(raw);
free(path);
}
}
printf("deep repair: %ld recovered (L0 %ld, L1 %ld, L2 %ld, "
"L3 %ld), %ld failed, %ld directories, %.3fs total\n",
n_ok, landed[0], landed[1], landed[2], landed[3],
n_fail, n_dir, bch_now() - t_start);
fclose(f);
return worst;
}
return repair_archive(prog, archive, outdir, max_level, budget,
only_entry, verbose);
}
+354
View File
@@ -0,0 +1,354 @@
/* repair_cmd.c - shared archive repair driver for `lzply -r` and
* `lzply_deep_repair`.
*
* repair_archive() validates an LZPL v5 or LZRS v1 archive, then
* attempts the escalating deep-repair ladder (L0 standard decode
* to t, L1 Chase-1 flips, L2 Chase-2 flips, L3 cross-block list
* decoding) per entry, writing recovered files under outdir with
* their stored paths preserved. Nothing is ever written unless it
* passes both oracles (CRC16, then decompress + SHA-256).
*
* Per entry it reports the level the repair landed at and the
* per-level timing -- the "time of arrival": how long each level
* took, how many candidates were tried, how many the CRC rejected,
* and how many bits were corrected. -v adds the per-level trial
* counts (BM trials, splitting-filter passes, valid codewords);
* the L1 line carries a (CUDA) marker when the Chase-1 scan ran
* on the GPU.
*
* Returns the worst exit code: 0 all entries recovered, 2 I/O
* error, 3 any entry unrecoverable (matching lzply's EXIT_*).
*/
#define _FILE_OFFSET_BITS 64
#define _DEFAULT_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "bch.h"
#include "deep_repair.h"
#include "lzply_lib.h"
#include "lzrs_lib.h"
#include "repair_cmd.h"
/* mkdir -p (mode 0755 for created parents). Returns 0 on success. */
static int mkdir_p(const char *path, mode_t mode)
{
char *tmp = xstrdup(path);
size_t n = strlen(tmp);
size_t i;
if (n == 0) {
free(tmp);
return 0;
}
while (n > 1 && tmp[n - 1] == '/')
tmp[--n] = '\0';
for (i = 1; i <= n; i++) {
if (tmp[i] == '/' || tmp[i] == '\0') {
char save = tmp[i];
struct stat st;
tmp[i] = '\0';
if (stat(tmp, &st) != 0) {
if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
free(tmp);
return -1;
}
} else if (!S_ISDIR(st.st_mode)) {
free(tmp);
return -1;
}
tmp[i] = save;
}
}
free(tmp);
return 0;
}
static int write_out_file(const char *path, const uint8_t *data, size_t len,
uint32_t mode)
{
FILE *f = fopen(path, "wb");
if (!f)
return -1;
if (len > 0 && fwrite(data, 1, len, f) != len) {
fclose(f);
return -1;
}
if (fclose(f) != 0)
return -1;
if (mode)
chmod(path, (mode_t)(mode & 07777));
return 0;
}
int repair_archive(const char *prog, const char *archive,
const char *outdir, int max_level, double budget,
const char *only_entry, int verbose)
{
int is_lzrs = 0;
FILE *f = NULL;
uint8_t magic[4];
lzply_set_progname(prog);
f = fopen(archive, "rb");
if (!f) {
fprintf(stderr, "%s: cannot open %s: %s\n", prog, archive,
strerror(errno));
return EXIT_IO;
}
if (fread(magic, 1, 4, f) != 4) {
fprintf(stderr, "%s: %s is too short to be an archive\n", prog, archive);
fclose(f);
return EXIT_CORRUPT;
}
if (memcmp(magic, "LZRS", 4) == 0)
is_lzrs = 1;
else if (memcmp(magic, "LZPL", 4) != 0) {
fprintf(stderr, "%s: %s is not an LZPL or LZRS archive\n", prog, archive);
fclose(f);
return EXIT_CORRUPT;
}
/* Validate the archive up front (version, headers, end marker),
* so a structurally broken archive fails before any repair is
* attempted. The scan also gives the clear v4 message for old
* LZPL archives. */
{
int version = 0;
uint32_t count = 0;
off_t size = 0, end_off = 0;
scan_list_t entries;
int s;
rewind(f);
if (is_lzrs)
s = lzrs_scan_archive(archive, &version, &count, &size,
&entries, &end_off);
else
s = scan_archive(archive, &version, &count, &size,
&entries, &end_off);
if (s != 0) {
fclose(f);
return EXIT_CORRUPT;
}
scan_list_free(&entries);
rewind(f);
}
if (mkdir_p(outdir, 0755) != 0) {
fprintf(stderr, "%s: cannot create output directory %s: %s\n",
prog, outdir, strerror(errno));
fclose(f);
return EXIT_IO;
}
/* Skip the 16-byte global header; then walk the entries. */
if (fseeko(f, 16, SEEK_SET) != 0) {
fprintf(stderr, "%s: cannot seek in %s: %s\n", prog, archive,
strerror(errno));
fclose(f);
return EXIT_IO;
}
{
int worst = 0;
long n_ok = 0, n_fail = 0, n_dir = 0;
long landed[4] = { 0, 0, 0, 0 };
double t_start = bch_now();
for (;;) {
char *path = NULL;
int level = 0, is_bch = 0, rs_level = 0, is_dir = 0;
uint16_t poly_id = 0, want_crc16 = 0;
uint64_t stored_size = 0, blob_size = 0, orig_size = 0;
uint32_t mode = 0;
uint8_t sha[32];
uint8_t *stored = NULL;
int r;
if (is_lzrs) {
lzrs_ehdr_t h;
r = lzrs_read_entry_header(f, &h);
if (r == 1)
break;
if (r < 0) {
fprintf(stderr, "%s: truncated or corrupt entry header\n",
prog);
worst = EXIT_CORRUPT;
break;
}
path = h.path;
level = h.level;
is_bch = (h.flags & FLAG_BCH) != 0;
rs_level = h.rs_level;
is_dir = (h.flags & FLAG_ISDIR) != 0;
poly_id = h.bch_poly_id;
want_crc16 = h.crc16;
stored_size = h.stored_size;
blob_size = h.blob_size;
orig_size = h.orig_size;
mode = h.mode;
memcpy(sha, h.sha, 32);
} else {
ehdr_t h;
r = read_entry_header(f, &h);
if (r == 1)
break;
if (r < 0) {
fprintf(stderr, "%s: truncated or corrupt entry header\n",
prog);
worst = EXIT_CORRUPT;
break;
}
path = h.path;
level = h.level;
is_bch = (h.flags & FLAG_BCH) != 0;
rs_level = 0;
is_dir = (h.flags & FLAG_ISDIR) != 0;
poly_id = (uint16_t)(h.rs_level |
((uint16_t)h.reserved << 8));
want_crc16 = h.crc16;
stored_size = h.stored_size;
blob_size = h.blob_size;
orig_size = h.orig_size;
mode = h.mode;
memcpy(sha, h.sha, 32);
}
stored = xmalloc(stored_size ? (size_t)stored_size : 1);
if (stored_size > 0 &&
fread(stored, 1, (size_t)stored_size, f) !=
(size_t)stored_size) {
fprintf(stderr, "%s: truncated data for %s\n", prog, path);
free(stored);
free(path);
worst = EXIT_CORRUPT;
break;
}
if (only_entry && strcmp(path, only_entry) != 0) {
free(stored);
free(path);
continue;
}
if (is_dir) {
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
sprintf(dp, "%s/%s", outdir, path);
if (mkdir_p(dp, mode ? (mode_t)(mode & 07777) : 0755) != 0)
fprintf(stderr, "%s: cannot create directory %s: %s\n",
prog, dp, strerror(errno));
free(dp);
free(stored);
free(path);
n_dir++;
continue;
}
{
uint8_t *raw = NULL;
size_t raw_len = 0;
deep_stats_t st;
int drc = deep_repair_entry(stored, (size_t)stored_size,
level, is_bch, poly_id,
rs_level, sha,
(size_t)blob_size, want_crc16,
max_level, budget,
&raw, &raw_len, &st);
free(stored);
if (drc != 0) {
printf("%s: FAILED (stage %s) after %.3fs, "
"cands %ld crc-rejects %ld sha-rejects %ld\n",
path, st.fail_stage, st.t_total,
st.cands_tried, st.crc_rejects, st.sha_rejects);
if (verbose) {
int lv;
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
printf(" L%d: %.3fs trials %ld split %ld "
"codewords %ld%s\n", lv, st.t_level[lv],
st.trials[lv], st.split_pass[lv],
st.codewords[lv],
(lv == 1 && st.chase_cuda_used) ?
" (CUDA)" : "");
printf(" blocks %ld/%ld failed%s\n",
st.blocks_failed, st.blocks_total,
st.partial ? " (partial: budget hit)" : "");
}
n_fail++;
if (EXIT_CORRUPT > worst)
worst = EXIT_CORRUPT;
free(path);
continue;
}
if (raw_len != orig_size) {
fprintf(stderr, "%s: %s: size mismatch after repair "
"(got %zu, expected %llu)\n", prog, path,
raw_len, (unsigned long long)orig_size);
free(raw);
free(path);
n_fail++;
if (EXIT_CORRUPT > worst)
worst = EXIT_CORRUPT;
continue;
}
{
char *dp = xmalloc(strlen(outdir) + strlen(path) + 2);
char *pd;
sprintf(dp, "%s/%s", outdir, path);
pd = parent_dir(dp);
if (mkdir_p(pd, 0755) != 0)
fprintf(stderr, "%s: cannot create %s: %s\n",
prog, pd, strerror(errno));
free(pd);
if (write_out_file(dp, raw, raw_len, mode) != 0) {
fprintf(stderr, "%s: cannot write %s: %s\n",
prog, dp, strerror(errno));
if (EXIT_IO > worst)
worst = EXIT_IO;
free(dp);
free(raw);
free(path);
continue;
}
free(dp);
}
{
char rssuf[48];
rssuf[0] = '\0';
if (st.rs_corrected > 0)
snprintf(rssuf, sizeof rssuf,
" (RS corrected %d symbols)",
st.rs_corrected);
printf("%s: RECOVERED at L%d in %.3fs "
"(L0 %.3fs, L1 %.3fs, L2 %.3fs, L3 %.3fs) "
"cands %ld crc-rejects %ld sha-rejects %ld "
"bch-corrected %d%s\n",
path, st.level_landed, st.t_total,
st.t_level[0], st.t_level[1], st.t_level[2],
st.t_level[3], st.cands_tried, st.crc_rejects,
st.sha_rejects, st.bch_corrected, rssuf);
}
if (verbose) {
int lv;
for (lv = 0; lv <= DEEP_MAX_LEVEL; lv++)
printf(" L%d: %.3fs trials %ld split %ld "
"codewords %ld%s\n", lv, st.t_level[lv],
st.trials[lv], st.split_pass[lv],
st.codewords[lv],
(lv == 1 && st.chase_cuda_used) ?
" (CUDA)" : "");
printf(" blocks %ld/%ld failed%s\n",
st.blocks_failed, st.blocks_total,
st.partial ? " (partial: budget hit)" : "");
}
landed[st.level_landed]++;
n_ok++;
free(raw);
free(path);
}
}
printf("deep repair: %ld recovered (L0 %ld, L1 %ld, L2 %ld, "
"L3 %ld), %ld failed, %ld directories, %.3fs total\n",
n_ok, landed[0], landed[1], landed[2], landed[3],
n_fail, n_dir, bch_now() - t_start);
fclose(f);
return worst;
}
}
+17
View File
@@ -0,0 +1,17 @@
/* repair_cmd.h - shared archive repair driver.
*
* repair_archive() is the archive-walking repair loop used by both
* the lzply -r mode and the lzply_deep_repair binary: it validates
* the archive, then attempts the escalating deep-repair ladder
* (L0-L3) per entry, writing recovered files under outdir.
* Returns the worst exit code: 0 all entries recovered, 2 I/O
* error, 3 any entry unrecoverable (matching lzply's EXIT_*).
*/
#ifndef LZPLY_REPAIR_CMD_H
#define LZPLY_REPAIR_CMD_H
int repair_archive(const char *prog, const char *archive,
const char *outdir, int max_level, double budget,
const char *only_entry, int verbose);
#endif /* LZPLY_REPAIR_CMD_H */
+13
View File
@@ -186,6 +186,19 @@ sys.exit(1 if fails else 0)
PYEOF
if [ $? -ne 0 ]; then FAIL=$((FAIL+1)); echo "FAIL: old-format rejection section (see above)"; else PASS=$((PASS+1)); echo "PASS: old-format rejection section"; fi
# --- 2b. lzply -r smoke test (repair built into the main CLI) -----------
"$LZPLY" -c -e 4 "$WORK/r512.bin" "$WORK/r-smoke.lzpl" >/dev/null
SMOKESZ=$(stat -c %s "$WORK/r-smoke.lzpl")
cp "$WORK/r-smoke.lzpl" "$WORK/r-smoke-corr.lzpl"
"$CORRUPT_BITS" "$WORK/r-smoke-corr.lzpl" 4242 9 104 $((SMOKESZ - 2)) >/dev/null
rm -rf "$WORK/r-smoke-out"
if "$LZPLY" -r -o "$WORK/r-smoke-out" "$WORK/r-smoke-corr.lzpl" >/dev/null 2>&1 \
&& cmp -s "$WORK/r512.bin" "$WORK/r-smoke-out/r512.bin"; then
ok "lzply -r repairs 9-bit corruption (L1)"
else
bad "lzply -r repairs 9-bit corruption (L1)"
fi
# --- 3. Corruption matrix -------------------------------------------------
# repair_trial ARCHIVE ORIG DATA_OFF BLOB_END SEED NERR MODE BUDGET TRIALNO
# MODE: scatter | burst1 | burst2 | conc1 (9 in block 1) | conc2 (9+9)