Module Name: src
Committed By: riastradh
Date: Wed Jan 22 06:14:46 UTC 2014
Modified Files:
src/usr.bin/vndcompress: Makefile vndcompress.c vnduncompress.c
Added Files:
src/usr.bin/vndcompress: offtab.c offtab.h
Log Message:
Abstract handling of the cloop2 offset table.
Preparation for converting it to use a fixed-size window.
To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/vndcompress/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.bin/vndcompress/offtab.c \
src/usr.bin/vndcompress/offtab.h
cvs rdiff -u -r1.14 -r1.15 src/usr.bin/vndcompress/vndcompress.c
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/vndcompress/vnduncompress.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/vndcompress/Makefile
diff -u src/usr.bin/vndcompress/Makefile:1.5 src/usr.bin/vndcompress/Makefile:1.6
--- src/usr.bin/vndcompress/Makefile:1.5 Wed Jan 22 06:14:20 2014
+++ src/usr.bin/vndcompress/Makefile Wed Jan 22 06:14:46 2014
@@ -1,5 +1,5 @@
PROG= vndcompress
-SRCS= main.c utils.c vndcompress.c vnduncompress.c
+SRCS= main.c offtab.c utils.c vndcompress.c vnduncompress.c
LINKS= ${BINDIR}/vndcompress ${BINDIR}/vnduncompress
MLINKS= vndcompress.1 vnduncompress.1
Index: src/usr.bin/vndcompress/vndcompress.c
diff -u src/usr.bin/vndcompress/vndcompress.c:1.14 src/usr.bin/vndcompress/vndcompress.c:1.15
--- src/usr.bin/vndcompress/vndcompress.c:1.14 Wed Jan 22 06:14:20 2014
+++ src/usr.bin/vndcompress/vndcompress.c Wed Jan 22 06:14:46 2014
@@ -1,4 +1,4 @@
-/* $NetBSD: vndcompress.c,v 1.14 2014/01/22 06:14:20 riastradh Exp $ */
+/* $NetBSD: vndcompress.c,v 1.15 2014/01/22 06:14:46 riastradh Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: vndcompress.c,v 1.14 2014/01/22 06:14:20 riastradh Exp $");
+__RCSID("$NetBSD: vndcompress.c,v 1.15 2014/01/22 06:14:46 riastradh Exp $");
#include <sys/endian.h>
@@ -50,6 +50,7 @@ __RCSID("$NetBSD: vndcompress.c,v 1.14 2
#include <zlib.h>
#include "common.h"
+#include "offtab.h"
#include "utils.h"
/*
@@ -72,7 +73,7 @@ struct compress_state {
uint32_t checkpoint_blocks; /* blocks before checkpoint */
int image_fd;
int cloop2_fd;
- uint64_t *offset_table;
+ struct offtab offtab;
uint32_t n_checkpointed_blocks;
volatile sig_atomic_t
initialized; /* everything above initialized? */
@@ -133,10 +134,6 @@ vndcompress(int argc, char **argv, const
compress_init(argc, argv, O, S);
assert(MIN_BLOCKSIZE <= S->blocksize);
assert(S->blocksize <= MAX_BLOCKSIZE);
- assert(S->offset_table != NULL);
- assert(S->n_offsets > 0);
- assert(S->offset_table[0] == htobe64(sizeof(struct cloop2_header) +
- (S->n_offsets * sizeof(uint64_t))));
/*
* Allocate compression buffers.
@@ -172,6 +169,7 @@ vndcompress(int argc, char **argv, const
/* Checkpoint if appropriate. */
compress_maybe_checkpoint(S);
+ offtab_prepare_put(&S->offtab, (S->blkno + 1));
/* Choose read size: partial if last block, full if not. */
const uint32_t readsize = (S->blkno == S->n_full_blocks?
@@ -208,7 +206,7 @@ vndcompress(int argc, char **argv, const
block_signals(&old_sigmask);
S->blkno += 1; /* (a) */
S->offset += complen; /* (b) */
- S->offset_table[S->blkno] = htobe64(S->offset); /* (c) */
+ offtab_put(&S->offtab, S->blkno, S->offset); /* (c) */
restore_sigmask(&old_sigmask);
}
}
@@ -244,7 +242,7 @@ vndcompress(int argc, char **argv, const
}
out:
- /* Commit the offset table. */
+ /* One last checkpoint to commit the offset table. */
assert(S->offset <= OFF_MAX);
assert((off_t)S->offset == lseek(S->cloop2_fd, 0, SEEK_CUR));
compress_checkpoint(S);
@@ -405,7 +403,6 @@ static void
compress_init(int argc, char **argv, const struct options *O,
struct compress_state *S)
{
- uint32_t i;
if (!((argc == 2) || (argc == 3)))
usage();
@@ -494,14 +491,13 @@ compress_init(int argc, char **argv, con
S->blocksize, S->size);
assert(S->n_blocks <= MAX_N_BLOCKS);
- /* Allocate an offset table for the blocks; one extra for the end. */
+ /* Create an offset table for the blocks; one extra for the end. */
__CTASSERT(MAX_N_BLOCKS <= (UINT32_MAX - 1));
S->n_offsets = (S->n_blocks + 1);
__CTASSERT(MAX_N_OFFSETS == (MAX_N_BLOCKS + 1));
__CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
- S->offset_table = malloc(S->n_offsets * sizeof(uint64_t));
- if (S->offset_table == NULL)
- err(1, "malloc offset table");
+ offtab_init(&S->offtab, S->n_offsets, S->cloop2_fd,
+ CLOOP2_OFFSET_TABLE_OFFSET);
/* Attempt to restart a partial transfer if requested. */
if (ISSET(O->flags, FLAG_r)) {
@@ -534,18 +530,6 @@ compress_init(int argc, char **argv, con
}
}
- /*
- * Initialize the offset table to all ones (except for the
- * fixed first offset) so that we can easily detect where we
- * were interrupted if we want to restart.
- */
- __CTASSERT(MAX_N_OFFSETS <= UINT32_MAX);
- assert(S->n_offsets > 0);
- S->offset_table[0] = htobe64(sizeof(struct cloop2_header) +
- (S->n_offsets * sizeof(uint64_t)));
- for (i = 1; i < S->n_offsets; i++)
- S->offset_table[i] = ~(uint64_t)0;
-
/* Write a bogus (zero) header for now, until we checkpoint. */
static const struct cloop2_header zero_header;
const ssize_t h_written = write(S->cloop2_fd, &zero_header,
@@ -557,16 +541,8 @@ compress_init(int argc, char **argv, con
errx(1, "partial write of header: %zu != %zu",
(size_t)h_written, sizeof(zero_header));
- /* Write the initial (empty) offset table. */
- const ssize_t ot_written = write(S->cloop2_fd, S->offset_table,
- (S->n_offsets * sizeof(uint64_t)));
- if (ot_written == -1)
- err(1, "write initial offset table");
- assert(ot_written >= 0);
- if ((size_t)ot_written != (S->n_offsets * sizeof(uint64_t)))
- errx(1, "partial write of initial offset bytes: %zu <= %zu",
- (size_t)ot_written,
- (size_t)(S->n_offsets * sizeof(uint64_t)));
+ /* Reset the offset table to be empty and write it. */
+ offtab_reset_write(&S->offtab);
/* Start at the beginning of the image. */
S->blkno = 0;
@@ -581,11 +557,11 @@ compress_init(int argc, char **argv, con
/*
* Try to recover state from an existing output file.
*
- * On success, fill S->offset_table with what's in the file, set
+ * On success, fill the offset table with what's in the file, set
* S->blkno and S->offset to reflect our position, and seek to the
* respective positions in the input and output files.
*
- * On failure, return false. May clobber S->offset_table, S->blkno,
+ * On failure, return false. May clobber the offset table, S->blkno,
* S->offset, and the file pointers.
*/
static bool
@@ -631,36 +607,33 @@ compress_restart(struct compress_state *
}
/* Read in the partial offset table. */
- const ssize_t ot_read = read_block(S->cloop2_fd, S->offset_table,
- (S->n_offsets * sizeof(uint64_t)));
- if (ot_read == -1) {
- warn("failed to read offset table");
+ if (!offtab_reset_read(&S->offtab, &warn, &warnx))
return false;
- }
- assert(ot_read >= 0);
- if ((size_t)ot_read != (S->n_offsets * sizeof(uint64_t))) {
- warnx("partial read of offset table");
+ if (!offtab_prepare_get(&S->offtab, 0))
return false;
- }
-
- if (be64toh(S->offset_table[0]) != (sizeof(struct cloop2_header) +
+ const uint64_t first_offset = offtab_get(&S->offtab, 0);
+ if (first_offset != (sizeof(struct cloop2_header) +
(S->n_offsets * sizeof(uint64_t)))) {
warnx("first offset is not %"PRIu64": %"PRIu64,
((uint64_t)S->n_offsets * sizeof(uint64_t)),
- be64toh(S->offset_table[0]));
+ first_offset);
return false;
}
/* Find where we left off. */
__CTASSERT(MAX_N_OFFSETS <= UINT32_MAX);
uint32_t blkno = 0;
+ uint64_t last_offset = first_offset;
for (blkno = 0; blkno < S->n_blocks; blkno++) {
- if (S->offset_table[blkno] == ~(uint64_t)0)
+ if (!offtab_prepare_get(&S->offtab, blkno))
+ return false;
+ const uint64_t offset = offtab_get(&S->offtab, blkno);
+ if (offset == ~(uint64_t)0)
break;
+
if (0 < blkno) {
- const uint64_t start =
- be64toh(S->offset_table[blkno - 1]);
- const uint64_t end = be64toh(S->offset_table[blkno]);
+ const uint64_t start = last_offset;
+ const uint64_t end = offset;
if (end <= start) {
warnx("bad offset table: 0x%"PRIx64
", 0x%"PRIx64, start, end);
@@ -675,6 +648,8 @@ compress_restart(struct compress_state *
return false;
}
}
+
+ last_offset = offset;
}
if (blkno == 0) {
@@ -687,11 +662,13 @@ compress_restart(struct compress_state *
uint32_t nblkno;
for (nblkno = blkno; nblkno < S->n_blocks; nblkno++) {
- if (S->offset_table[nblkno] != ~(uint64_t)0) {
+ if (!offtab_prepare_get(&S->offtab, nblkno))
+ return false;
+ const uint64_t offset = offtab_get(&S->offtab, nblkno);
+ if (offset != ~(uint64_t)0) {
warnx("bad partial offset table entry"
" at %"PRIu32": %"PRIu64,
- nblkno,
- be64toh(S->offset_table[nblkno]));
+ nblkno, offset);
return false;
}
}
@@ -745,17 +722,18 @@ compress_restart(struct compress_state *
}
/* Seek to the output position. */
- const uint64_t offset = be64toh(S->offset_table[blkno]);
- assert(offset <= OFF_MAX);
- if (lseek(S->cloop2_fd, offset, SEEK_SET) == -1) {
- warn("lseek output cloop2 to %"PRIx64" failed",
- S->offset);
+ assert(last_offset <= OFF_MAX);
+ if (lseek(S->cloop2_fd, last_offset, SEEK_SET) == -1) {
+ warn("lseek output cloop2 to %"PRIx64" failed", last_offset);
return false;
}
+ /* Switch from reading to writing the offset table. */
+ offtab_transmogrify_read_to_write(&S->offtab);
+
/* Start where we left off. */
S->blkno = blkno;
- S->offset = offset;
+ S->offset = last_offset;
S->n_checkpointed_blocks = blkno;
/* Good to go and ready for interruption by a signal. */
@@ -865,36 +843,22 @@ compress_checkpoint(struct compress_stat
warn_ss("fsync of output failed");
/* Say the data blocks are ready. */
- const ssize_t n_written = pwrite(S->cloop2_fd, S->offset_table,
- (n_offsets * sizeof(uint64_t)), CLOOP2_OFFSET_TABLE_OFFSET);
- if (n_written == -1)
- err_ss(1, "write partial offset table");
- assert(n_written >= 0);
- if ((size_t)n_written != (n_offsets * sizeof(uint64_t)))
- errx_ss(1, "partial write of partial offset table: %zu != %zu",
- (size_t)n_written,
- (size_t)(n_offsets * sizeof(uint64_t)));
+ offtab_checkpoint(&S->offtab, n_offsets,
+ (S->n_checkpointed_blocks == 0? OFFTAB_CHECKPOINT_SYNC : 0));
/*
* If this is the first checkpoint, initialize the header.
* Signal handler can race with main code here, but it is
* harmless -- just an extra fsync and write of the header,
* which are both idempotent.
+ *
+ * Once we have synchronously checkpointed the offset table,
+ * subsequent writes will preserve a valid state.
*/
if (S->n_checkpointed_blocks == 0) {
static const struct cloop2_header zero_header;
struct cloop2_header header = zero_header;
- /* Force the offset table to disk before we set the header. */
- if (fsync_range(S->cloop2_fd, (FFILESYNC | FDISKSYNC),
- 0,
- (CLOOP2_OFFSET_TABLE_OFFSET
- + (n_offsets * (sizeof(uint64_t)))))
- == -1)
- warn_ss("fsync of offset table failed");
-
- /* Subsequent writes will preserve a valid state. */
-
/* Format the header. */
__CTASSERT(sizeof(cloop2_magic) <= sizeof(header.cl2h_magic));
(void)memcpy(header.cl2h_magic, cloop2_magic,
@@ -929,8 +893,8 @@ static void
compress_exit(struct compress_state *S)
{
- /* Done with the offset table. Free it. */
- free(S->offset_table);
+ /* Done with the offset table. Destroy it. */
+ offtab_destroy(&S->offtab);
/* Done with the files. Close them. */
if (close(S->cloop2_fd) == -1)
Index: src/usr.bin/vndcompress/vnduncompress.c
diff -u src/usr.bin/vndcompress/vnduncompress.c:1.4 src/usr.bin/vndcompress/vnduncompress.c:1.5
--- src/usr.bin/vndcompress/vnduncompress.c:1.4 Wed Jan 22 06:14:28 2014
+++ src/usr.bin/vndcompress/vnduncompress.c Wed Jan 22 06:14:46 2014
@@ -1,4 +1,4 @@
-/* $NetBSD: vnduncompress.c,v 1.4 2014/01/22 06:14:28 riastradh Exp $ */
+/* $NetBSD: vnduncompress.c,v 1.5 2014/01/22 06:14:46 riastradh Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: vnduncompress.c,v 1.4 2014/01/22 06:14:28 riastradh Exp $");
+__RCSID("$NetBSD: vnduncompress.c,v 1.5 2014/01/22 06:14:46 riastradh Exp $");
#include <sys/endian.h>
@@ -46,11 +46,33 @@ __RCSID("$NetBSD: vnduncompress.c,v 1.4
#include <zlib.h>
#include "common.h"
+#include "offtab.h"
#include "utils.h"
+static void __dead
+err1(const char *fmt, ...)
+{
+ va_list va;
+
+ va_start(va, fmt);
+ verr(1, fmt, va);
+ va_end(va);
+}
+
+static void __dead
+errx1(const char *fmt, ...)
+{
+ va_list va;
+
+ va_start(va, fmt);
+ verrx(1, fmt, va);
+ va_end(va);
+}
+
int
vnduncompress(int argc, char **argv, const struct options *O __unused)
{
+ struct offtab offtab;
if (argc != 2)
usage();
@@ -103,25 +125,12 @@ vnduncompress(int argc, char **argv, con
errx(1, "too many blocks: %"PRIu32" (max %"PRIu32")",
n_blocks, (uint32_t)MAX_N_BLOCKS);
- /* Allocate an offset table. */
+ /* Initialize the offset table. */
__CTASSERT(MAX_N_BLOCKS <= (UINT32_MAX - 1));
__CTASSERT((MAX_N_BLOCKS + 1) == MAX_N_OFFSETS);
const uint32_t n_offsets = (n_blocks + 1);
-
- __CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
- uint64_t *const offset_table = malloc(n_offsets * sizeof(uint64_t));
- if (offset_table == NULL)
- err(1, "malloc offset table");
-
- /* Read the offset table in. */
- const ssize_t ot_read = read_block(cloop2_fd, offset_table,
- (n_offsets * sizeof(uint64_t)));
- if (ot_read == -1)
- err(1, "read offset table");
- assert(ot_read >= 0);
- if ((size_t)ot_read != (n_offsets * sizeof(uint64_t)))
- errx(1, "partial read of offset table: %zu != %zu",
- (size_t)ot_read, (size_t)(n_offsets * sizeof(uint64_t)));
+ offtab_init(&offtab, n_offsets, cloop2_fd, CLOOP2_OFFSET_TABLE_OFFSET);
+ offtab_reset_read(&offtab, &err1, &errx1);
/* Allocate compression buffers. */
/* XXX compression ratio bound */
@@ -144,9 +153,13 @@ vnduncompress(int argc, char **argv, con
__CTASSERT(OFF_MAX <= UINT64_MAX);
uint64_t offset = (sizeof(header) + (n_offsets * sizeof(uint64_t)));
uint32_t blkno;
+ (void)offtab_prepare_get(&offtab, 0);
+ uint64_t last = offtab_get(&offtab, 0);
for (blkno = 0; blkno < n_blocks; blkno++) {
- const uint64_t start = be64toh(offset_table[blkno]);
- const uint64_t end = be64toh(offset_table[blkno + 1]);
+ (void)offtab_prepare_get(&offtab, (blkno + 1));
+
+ const uint64_t start = last;
+ const uint64_t end = offtab_get(&offtab, (blkno + 1));
/* Sanity-check the offsets. */
if (start != offset)
@@ -201,10 +214,11 @@ vnduncompress(int argc, char **argv, con
/* Advance our position. */
assert((size_t)n_read <= (MIN(OFF_MAX, UINT64_MAX) - offset));
offset += (size_t)n_read;
+ last = end;
}
- /* Free the offset table and compression buffers. */
- free(offset_table);
+ /* Destroy the offset table and free the compression buffers. */
+ offtab_destroy(&offtab);
free(uncompbuf);
free(compbuf);
Added files:
Index: src/usr.bin/vndcompress/offtab.c
diff -u /dev/null src/usr.bin/vndcompress/offtab.c:1.1
--- /dev/null Wed Jan 22 06:14:46 2014
+++ src/usr.bin/vndcompress/offtab.c Wed Jan 22 06:14:46 2014
@@ -0,0 +1,284 @@
+/* $NetBSD: offtab.c,v 1.1 2014/01/22 06:14:46 riastradh Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD");
+
+#include <sys/types.h>
+#include <sys/endian.h>
+
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "utils.h"
+
+#include "offtab.h"
+
+static void __printflike(1,2)
+offtab_bug(const char *fmt, ...)
+{
+
+ errx(1, "bug in offtab, please report");
+}
+
+static void __printflike(1,2)
+offtab_bugx(const char *fmt, ...)
+{
+
+ errx(1, "bug in offtab, please report");
+}
+
+/*
+ * Initialize an offtab to support the specified number of offsets read
+ * to or written from fd at byte position fdpos.
+ */
+void
+offtab_init(struct offtab *offtab, uint32_t n_offsets, int fd, off_t fdpos)
+{
+
+ assert(offtab != NULL);
+ assert(0 < n_offsets);
+ assert(0 <= fd);
+ assert(0 <= fdpos);
+
+ offtab->ot_n_offsets = n_offsets;
+ __CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
+ offtab->ot_offsets = malloc(n_offsets * sizeof(uint64_t));
+ if (offtab->ot_offsets == NULL)
+ err(1, "malloc offset table");
+ offtab->ot_blkno = (uint32_t)-1;
+ offtab->ot_fd = fd;
+ offtab->ot_fdpos = fdpos;
+ offtab->ot_report = &offtab_bug;
+ offtab->ot_reportx = &offtab_bugx;
+ offtab->ot_mode = OFFTAB_MODE_NONE;
+}
+
+/*
+ * Destroy an offtab.
+ */
+void
+offtab_destroy(struct offtab *offtab)
+{
+
+ free(offtab->ot_offsets);
+}
+
+/*
+ * For an offtab that has been used to read data from disk, convert it
+ * to an offtab that can be used to write subsequent data to disk.
+ */
+void
+offtab_transmogrify_read_to_write(struct offtab *offtab)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_READ);
+ assert(offtab->ot_offsets[0] == htobe64(offtab->ot_fdpos +
+ (offtab->ot_n_offsets * sizeof(uint64_t))));
+ offtab->ot_mode = OFFTAB_MODE_WRITE;
+}
+
+/*
+ * Reset an offtab for reading an offset table from the beginning.
+ * Initializes in-memory state and may read data from offtab->ot_fd,
+ * which must currently be at byte position offtab->ot_fdpos. Failure
+ * will be reported by the report/reportx routines, which are called
+ * like warn/warnx. May fail; returns true on success, false on
+ * failure.
+ */
+bool
+offtab_reset_read(struct offtab *offtab,
+ void (*report)(const char *, ...) __printflike(1,2),
+ void (*reportx)(const char *, ...) __printflike(1,2))
+{
+
+ assert((lseek(offtab->ot_fd, 0, SEEK_CUR) == offtab->ot_fdpos) ||
+ ((lseek(offtab->ot_fd, 0, SEEK_CUR) == -1) && (errno == ESPIPE)));
+
+ offtab->ot_report = report;
+ offtab->ot_reportx = reportx;
+ offtab->ot_mode = OFFTAB_MODE_READ;
+
+ const ssize_t n_read = read_block(offtab->ot_fd, offtab->ot_offsets,
+ (offtab->ot_n_offsets * sizeof(uint64_t)));
+ if (n_read == -1) {
+ (*offtab->ot_report)("read offset table");
+ return false;
+ }
+ assert(n_read >= 0);
+ if ((size_t)n_read != (offtab->ot_n_offsets * sizeof(uint64_t))) {
+ (*offtab->ot_reportx)("partial read of offset table"
+ ": %zu != %zu",
+ (size_t)n_read,
+ (size_t)(offtab->ot_n_offsets * sizeof(uint64_t)));
+ return false;
+ }
+
+ return true;
+}
+
+/*
+ * Do any I/O or bookkeeping necessary to fetch the offset for blkno in
+ * preparation for a call to offtab_get. May fail; returns true on
+ * success, false on failure.
+ */
+bool
+offtab_prepare_get(struct offtab *offtab, uint32_t blkno)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_READ);
+ assert(blkno < offtab->ot_n_offsets);
+ offtab->ot_blkno = blkno;
+ return true;
+}
+
+/*
+ * Return the offset for blkno. Caller must have called
+ * offtab_prepare_get beforehand.
+ */
+uint64_t
+offtab_get(struct offtab *offtab, uint32_t blkno)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_READ);
+ assert(blkno < offtab->ot_n_offsets);
+ assert(blkno == offtab->ot_blkno);
+ return be64toh(offtab->ot_offsets[blkno]);
+}
+
+/*
+ * Reset offtab for writing a fresh offset table. Initializes
+ * in-memory state and writes an empty offset table to offtab->ot_fd,
+ * which must currently be at byte position offtab->ot_fdpos. May
+ * fail; returns on success, aborts with err(3) on failure.
+ */
+void
+offtab_reset_write(struct offtab *offtab)
+{
+ uint32_t i;
+
+ assert(lseek(offtab->ot_fd, 0, SEEK_CUR) == offtab->ot_fdpos);
+
+ offtab->ot_mode = OFFTAB_MODE_WRITE;
+
+ /*
+ * Initialize the offset table to all ones (except for the
+ * fixed first offset) so that we can easily detect where we
+ * were interrupted if we want to restart.
+ */
+ __CTASSERT(MAX_N_OFFSETS <= UINT32_MAX);
+ assert(offtab->ot_n_offsets > 0);
+ offtab->ot_offsets[0] = htobe64(offtab->ot_fdpos +
+ (offtab->ot_n_offsets * sizeof(uint64_t)));
+ for (i = 1; i < offtab->ot_n_offsets; i++)
+ offtab->ot_offsets[i] = ~(uint64_t)0;
+
+ /* Write the initial (empty) offset table. */
+ const ssize_t n_written = write(offtab->ot_fd, offtab->ot_offsets,
+ (offtab->ot_n_offsets * sizeof(uint64_t)));
+ if (n_written == -1)
+ err(1, "write initial offset table");
+ assert(n_written >= 0);
+ if ((size_t)n_written != (offtab->ot_n_offsets * sizeof(uint64_t)))
+ errx(1, "partial write of initial offset bytes: %zu <= %zu",
+ (size_t)n_written,
+ (size_t)(offtab->ot_n_offsets * sizeof(uint64_t)));
+}
+
+/*
+ * Guarantee that the disk reflects block offsets [0, n_offsets). If
+ * OFFTAB_CHECKPOINT_SYNC is set in flags, will also fsync the entire
+ * offset table. May fail; returns on success, aborts with err(3) on
+ * failure. Fsync failure is considered success but is reported with a
+ * warning.
+ *
+ * This routine does not write state in memory, and does not read state
+ * that is not signal-safe. The only state read is static for the
+ * existence of the offset table.
+ */
+void
+offtab_checkpoint(struct offtab *offtab, uint32_t n_offsets, int flags)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
+ assert(n_offsets <= offtab->ot_n_offsets);
+
+ const ssize_t n_written = pwrite(offtab->ot_fd, offtab->ot_offsets,
+ (n_offsets * sizeof(uint64_t)), offtab->ot_fdpos);
+ if (n_written == -1)
+ err_ss(1, "write partial offset table");
+ assert(n_written >= 0);
+ if ((size_t)n_written != (n_offsets * sizeof(uint64_t)))
+ errx_ss(1, "partial write of partial offset table: %zu != %zu",
+ (size_t)n_written,
+ (size_t)(n_offsets * sizeof(uint64_t)));
+
+ if (ISSET(flags, OFFTAB_CHECKPOINT_SYNC)) {
+ if (fsync_range(offtab->ot_fd, (FFILESYNC | FDISKSYNC),
+ offtab->ot_fdpos,
+ (offtab->ot_fdpos + (n_offsets * (sizeof(uint64_t)))))
+ == -1)
+ warn_ss("fsync of offset table failed");
+ }
+}
+
+/*
+ * Do any I/O or bookkeeping necessary to set an offset for blkno. May
+ * fail; returns on success, aborts with err(3) on failure.
+ */
+void
+offtab_prepare_put(struct offtab *offtab, uint32_t blkno)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
+ assert(blkno < offtab->ot_n_offsets);
+ offtab->ot_blkno = blkno;
+}
+
+/*
+ * Actually set the offset for blkno.
+ */
+void
+offtab_put(struct offtab *offtab, uint32_t blkno, uint64_t offset)
+{
+
+ assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
+ assert(blkno < offtab->ot_n_offsets);
+ assert(blkno == offtab->ot_blkno);
+ offtab->ot_offsets[blkno] = htobe64(offset);
+}
Index: src/usr.bin/vndcompress/offtab.h
diff -u /dev/null src/usr.bin/vndcompress/offtab.h:1.1
--- /dev/null Wed Jan 22 06:14:46 2014
+++ src/usr.bin/vndcompress/offtab.h Wed Jan 22 06:14:46 2014
@@ -0,0 +1,72 @@
+/* $NetBSD: offtab.h,v 1.1 2014/01/22 06:14:46 riastradh Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef VNDCOMPRESS_OFFTAB_H
+#define VNDCOMPRESS_OFFTAB_H
+
+#include <sys/cdefs.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+struct offtab {
+ uint32_t ot_n_offsets;
+ uint64_t *ot_offsets;
+ uint32_t ot_blkno;
+ int ot_fd;
+ off_t ot_fdpos;
+ void (*ot_report)(const char *, ...) __printflike(1,2);
+ void (*ot_reportx)(const char *, ...) __printflike(1,2);
+ enum offtab_mode {
+ OFFTAB_MODE_NONE,
+ OFFTAB_MODE_READ,
+ OFFTAB_MODE_WRITE,
+ } ot_mode;
+};
+
+void offtab_init(struct offtab *, uint32_t, int, off_t);
+void offtab_destroy(struct offtab *);
+
+void offtab_transmogrify_read_to_write(struct offtab *);
+
+bool offtab_reset_read(struct offtab *,
+ void (*)(const char *, ...) __printflike(1,2),
+ void (*)(const char *, ...) __printflike(1,2));
+bool offtab_prepare_get(struct offtab *, uint32_t);
+uint64_t offtab_get(struct offtab *, uint32_t);
+
+void offtab_reset_write(struct offtab *);
+void offtab_checkpoint(struct offtab *, uint32_t, int);
+#define OFFTAB_CHECKPOINT_SYNC 1
+void offtab_prepare_put(struct offtab *, uint32_t);
+void offtab_put(struct offtab *, uint32_t, uint64_t);
+
+#endif /* VNDCOMPRESS_OFFTAB_H */