xref: /netbsd-src/usr.bin/vndcompress/offtab.c (revision 76d4b812519cd793ec395025fc72068d22cb7c23)
1*76d4b812Sriastradh /*	$NetBSD: offtab.c,v 1.15 2017/07/29 21:04:07 riastradh Exp $	*/
2735c2397Sriastradh 
3735c2397Sriastradh /*-
4735c2397Sriastradh  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5735c2397Sriastradh  * All rights reserved.
6735c2397Sriastradh  *
7735c2397Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
8735c2397Sriastradh  * by Taylor R. Campbell.
9735c2397Sriastradh  *
10735c2397Sriastradh  * Redistribution and use in source and binary forms, with or without
11735c2397Sriastradh  * modification, are permitted provided that the following conditions
12735c2397Sriastradh  * are met:
13735c2397Sriastradh  * 1. Redistributions of source code must retain the above copyright
14735c2397Sriastradh  *    notice, this list of conditions and the following disclaimer.
15735c2397Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
16735c2397Sriastradh  *    notice, this list of conditions and the following disclaimer in the
17735c2397Sriastradh  *    documentation and/or other materials provided with the distribution.
18735c2397Sriastradh  *
19735c2397Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20735c2397Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21735c2397Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22735c2397Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23735c2397Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24735c2397Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25735c2397Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26735c2397Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27735c2397Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28735c2397Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29735c2397Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
30735c2397Sriastradh  */
31735c2397Sriastradh 
32735c2397Sriastradh #include <sys/cdefs.h>
33*76d4b812Sriastradh __RCSID("$NetBSD: offtab.c,v 1.15 2017/07/29 21:04:07 riastradh Exp $");
34735c2397Sriastradh 
35735c2397Sriastradh #include <sys/types.h>
36735c2397Sriastradh #include <sys/endian.h>
37735c2397Sriastradh 
38735c2397Sriastradh #include <assert.h>
39735c2397Sriastradh #include <err.h>
40735c2397Sriastradh #include <errno.h>
41735c2397Sriastradh #include <inttypes.h>
42735c2397Sriastradh #include <limits.h>
43735c2397Sriastradh #include <stdbool.h>
44735c2397Sriastradh #include <stdlib.h>
45735c2397Sriastradh #include <unistd.h>
46735c2397Sriastradh 
47735c2397Sriastradh #include "common.h"
48735c2397Sriastradh #include "utils.h"
49735c2397Sriastradh 
50735c2397Sriastradh #include "offtab.h"
51735c2397Sriastradh 
52eecb404dSjoerg static void __printflike(1,2) __dead
offtab_bug(const char * fmt,...)53735c2397Sriastradh offtab_bug(const char *fmt, ...)
54735c2397Sriastradh {
55735c2397Sriastradh 
56735c2397Sriastradh 	errx(1, "bug in offtab, please report");
57735c2397Sriastradh }
58735c2397Sriastradh 
59eecb404dSjoerg static void __printflike(1,2) __dead
offtab_bugx(const char * fmt,...)60735c2397Sriastradh offtab_bugx(const char *fmt, ...)
61735c2397Sriastradh {
62735c2397Sriastradh 
63735c2397Sriastradh 	errx(1, "bug in offtab, please report");
64735c2397Sriastradh }
653e40e9d7Sriastradh 
663e40e9d7Sriastradh static uint32_t
offtab_compute_window_size(struct offtab * offtab,uint32_t start)67a4a41d77Sriastradh offtab_compute_window_size(struct offtab *offtab, uint32_t start)
683e40e9d7Sriastradh {
693e40e9d7Sriastradh 
70a4a41d77Sriastradh 	assert(start < offtab->ot_n_offsets);
71a4a41d77Sriastradh 	return MIN(offtab->ot_window_size, (offtab->ot_n_offsets - start));
723e40e9d7Sriastradh }
733e40e9d7Sriastradh 
743e40e9d7Sriastradh static uint32_t
offtab_current_window_size(struct offtab * offtab)753e40e9d7Sriastradh offtab_current_window_size(struct offtab *offtab)
763e40e9d7Sriastradh {
773e40e9d7Sriastradh 
78a4a41d77Sriastradh 	return offtab_compute_window_size(offtab, offtab->ot_window_start);
793e40e9d7Sriastradh }
803e40e9d7Sriastradh 
813e40e9d7Sriastradh static uint32_t
offtab_current_window_end(struct offtab * offtab)823e40e9d7Sriastradh offtab_current_window_end(struct offtab *offtab)
833e40e9d7Sriastradh {
843e40e9d7Sriastradh 
853e40e9d7Sriastradh 	assert(offtab->ot_window_start < offtab->ot_n_offsets);
863e40e9d7Sriastradh 	assert(offtab_current_window_size(offtab) <=
873e40e9d7Sriastradh 	    (offtab->ot_n_offsets - offtab->ot_window_start));
883e40e9d7Sriastradh 	return (offtab->ot_window_start + offtab_current_window_size(offtab));
893e40e9d7Sriastradh }
903e40e9d7Sriastradh 
91482c113dSriastradh static void
offtab_compute_window_position(struct offtab * offtab,uint32_t window_start,size_t * bytes,off_t * pos)92482c113dSriastradh offtab_compute_window_position(struct offtab *offtab, uint32_t window_start,
93482c113dSriastradh     size_t *bytes, off_t *pos)
94482c113dSriastradh {
95482c113dSriastradh 	const uint32_t window_size = offtab_compute_window_size(offtab,
96482c113dSriastradh 	    window_start);
97482c113dSriastradh 
98*76d4b812Sriastradh 	__CTASSERT(MUL_OK(size_t, MAX_WINDOW_SIZE, sizeof(uint64_t)));
99482c113dSriastradh 	*bytes = (window_size * sizeof(uint64_t));
100482c113dSriastradh 
101482c113dSriastradh 	assert(window_start <= offtab->ot_n_offsets);
102*76d4b812Sriastradh 	__CTASSERT(MUL_OK(off_t, MAX_N_OFFSETS, sizeof(uint64_t)));
103482c113dSriastradh 	const off_t window_offset = ((off_t)window_start *
104482c113dSriastradh 	    (off_t)sizeof(uint64_t));
105482c113dSriastradh 
1069719b6dcSriastradh 	assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
107*76d4b812Sriastradh 	__CTASSERT(ADD_OK(off_t, OFFTAB_MAX_FDPOS,
108*76d4b812Sriastradh 		(off_t)MAX_N_OFFSETS*sizeof(uint64_t)));
109*76d4b812Sriastradh 	assert(ADD_OK(off_t, offtab->ot_fdpos, window_offset));
110482c113dSriastradh 	*pos = (offtab->ot_fdpos + window_offset);
111482c113dSriastradh }
112482c113dSriastradh 
1133e40e9d7Sriastradh #define	OFFTAB_READ_SEEK	0x01
1143e40e9d7Sriastradh #define	OFFTAB_READ_NOSEEK	0x00
1153e40e9d7Sriastradh 
1163e40e9d7Sriastradh static bool
offtab_read_window(struct offtab * offtab,uint32_t blkno,int read_flags)1173e40e9d7Sriastradh offtab_read_window(struct offtab *offtab, uint32_t blkno, int read_flags)
1183e40e9d7Sriastradh {
119482c113dSriastradh 	const uint32_t window_start = rounddown(blkno, offtab->ot_window_size);
120482c113dSriastradh 	size_t window_bytes;
121482c113dSriastradh 	off_t window_pos;
1223e40e9d7Sriastradh 
1233e40e9d7Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_READ);
1243e40e9d7Sriastradh 	assert(ISSET(read_flags, OFFTAB_READ_SEEK) ||
1253e40e9d7Sriastradh 	    (lseek(offtab->ot_fd, 0, SEEK_CUR) == offtab->ot_fdpos) ||
1263e40e9d7Sriastradh 	    ((lseek(offtab->ot_fd, 0, SEEK_CUR) == -1) && (errno == ESPIPE)));
127482c113dSriastradh 
128482c113dSriastradh 	offtab_compute_window_position(offtab, window_start,
129482c113dSriastradh 	    &window_bytes, &window_pos);
1303e40e9d7Sriastradh 	const ssize_t n_read = (ISSET(read_flags, OFFTAB_READ_SEEK)
131482c113dSriastradh 	    ? pread_block(offtab->ot_fd, offtab->ot_window, window_bytes,
132482c113dSriastradh 		window_pos)
133482c113dSriastradh 	    : read_block(offtab->ot_fd, offtab->ot_window, window_bytes));
1343e40e9d7Sriastradh 	if (n_read == -1) {
1353e40e9d7Sriastradh 		(*offtab->ot_report)("read offset table at %"PRIuMAX,
13608530105Sriastradh 		    (uintmax_t)window_pos);
1373e40e9d7Sriastradh 		return false;
1383e40e9d7Sriastradh 	}
1393e40e9d7Sriastradh 	assert(n_read >= 0);
140482c113dSriastradh 	if ((size_t)n_read != window_bytes) {
1413e40e9d7Sriastradh 		(*offtab->ot_reportx)("partial read of offset table"
1423e40e9d7Sriastradh 		    " at %"PRIuMAX": %zu != %zu",
143482c113dSriastradh 		    (uintmax_t)window_pos, (size_t)n_read, window_bytes);
1443e40e9d7Sriastradh 		return false;
1453e40e9d7Sriastradh 	}
146482c113dSriastradh 
1473e40e9d7Sriastradh 	offtab->ot_window_start = window_start;
1483e40e9d7Sriastradh 
1493e40e9d7Sriastradh 	return true;
1503e40e9d7Sriastradh }
1513e40e9d7Sriastradh 
1523e40e9d7Sriastradh static bool
offtab_maybe_read_window(struct offtab * offtab,uint32_t blkno,int read_flags)1533e40e9d7Sriastradh offtab_maybe_read_window(struct offtab *offtab, uint32_t blkno, int read_flags)
1543e40e9d7Sriastradh {
1553e40e9d7Sriastradh 
1563e40e9d7Sriastradh 	/* Don't bother if blkno is already in the window.  */
1573e40e9d7Sriastradh 	if ((offtab->ot_window_start <= blkno) &&
1583e40e9d7Sriastradh 	    (blkno < offtab_current_window_end(offtab)))
1593e40e9d7Sriastradh 		return true;
1603e40e9d7Sriastradh 
1613e40e9d7Sriastradh 	if (!offtab_read_window(offtab, blkno, read_flags))
1623e40e9d7Sriastradh 		return false;
1633e40e9d7Sriastradh 
1643e40e9d7Sriastradh 	return true;
1653e40e9d7Sriastradh }
1663e40e9d7Sriastradh 
1673e40e9d7Sriastradh static void
offtab_write_window(struct offtab * offtab)168e70e0058Sriastradh offtab_write_window(struct offtab *offtab)
1693e40e9d7Sriastradh {
170482c113dSriastradh 	size_t window_bytes;
171482c113dSriastradh 	off_t window_pos;
1723e40e9d7Sriastradh 
1733e40e9d7Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
1743e40e9d7Sriastradh 
175482c113dSriastradh 	offtab_compute_window_position(offtab, offtab->ot_window_start,
176482c113dSriastradh 	    &window_bytes, &window_pos);
1773e40e9d7Sriastradh 	const ssize_t n_written = pwrite(offtab->ot_fd, offtab->ot_window,
178482c113dSriastradh 	    window_bytes, window_pos);
1793e40e9d7Sriastradh 	if (n_written == -1)
1803e40e9d7Sriastradh 		err_ss(1, "write initial offset table");
1813e40e9d7Sriastradh 	assert(n_written >= 0);
182482c113dSriastradh 	if ((size_t)n_written != window_bytes)
1833e40e9d7Sriastradh 		errx_ss(1, "partial write of initial offset bytes: %zu <= %zu",
1843e40e9d7Sriastradh 		    (size_t)n_written,
185482c113dSriastradh 		    window_bytes);
1863e40e9d7Sriastradh }
187e70e0058Sriastradh 
188e70e0058Sriastradh static void
offtab_maybe_write_window(struct offtab * offtab,uint32_t start,uint32_t end)189e70e0058Sriastradh offtab_maybe_write_window(struct offtab *offtab, uint32_t start, uint32_t end)
190e70e0058Sriastradh {
191e70e0058Sriastradh 
192e70e0058Sriastradh 	/* Don't bother if [start, end) does not cover our window.  */
193e70e0058Sriastradh 	if (end <= offtab->ot_window_start)
194e70e0058Sriastradh 		return;
195e70e0058Sriastradh 	if (offtab_current_window_end(offtab) < start)
196e70e0058Sriastradh 		return;
197e70e0058Sriastradh 
198e70e0058Sriastradh 	offtab_write_window(offtab);
199e70e0058Sriastradh }
200735c2397Sriastradh 
201735c2397Sriastradh /*
202735c2397Sriastradh  * Initialize an offtab to support the specified number of offsets read
203735c2397Sriastradh  * to or written from fd at byte position fdpos.
204735c2397Sriastradh  */
205735c2397Sriastradh void
offtab_init(struct offtab * offtab,uint32_t n_offsets,uint32_t window_size,int fd,off_t fdpos)2063e40e9d7Sriastradh offtab_init(struct offtab *offtab, uint32_t n_offsets, uint32_t window_size,
2073e40e9d7Sriastradh     int fd, off_t fdpos)
208735c2397Sriastradh {
209735c2397Sriastradh 
210735c2397Sriastradh 	assert(offtab != NULL);
211735c2397Sriastradh 	assert(0 < n_offsets);
212735c2397Sriastradh 	assert(0 <= fd);
213735c2397Sriastradh 	assert(0 <= fdpos);
2149719b6dcSriastradh 	assert(fdpos <= OFFTAB_MAX_FDPOS);
215735c2397Sriastradh 
216735c2397Sriastradh 	offtab->ot_n_offsets = n_offsets;
2173e40e9d7Sriastradh 	if ((window_size == 0) || (n_offsets < window_size))
2183e40e9d7Sriastradh 		offtab->ot_window_size = n_offsets;
2193e40e9d7Sriastradh 	else
2203e40e9d7Sriastradh 		offtab->ot_window_size = window_size;
2213e40e9d7Sriastradh 	assert(offtab->ot_window_size <= offtab->ot_n_offsets);
2223e40e9d7Sriastradh 	offtab->ot_window_start = (uint32_t)-1;
223*76d4b812Sriastradh 	__CTASSERT(MUL_OK(size_t, MAX_WINDOW_SIZE, sizeof(uint64_t)));
2243e40e9d7Sriastradh 	offtab->ot_window = malloc(offtab->ot_window_size * sizeof(uint64_t));
2253e40e9d7Sriastradh 	if (offtab->ot_window == NULL)
226735c2397Sriastradh 		err(1, "malloc offset table");
227735c2397Sriastradh 	offtab->ot_blkno = (uint32_t)-1;
228735c2397Sriastradh 	offtab->ot_fd = fd;
229735c2397Sriastradh 	offtab->ot_fdpos = fdpos;
230735c2397Sriastradh 	offtab->ot_report = &offtab_bug;
231735c2397Sriastradh 	offtab->ot_reportx = &offtab_bugx;
232735c2397Sriastradh 	offtab->ot_mode = OFFTAB_MODE_NONE;
233735c2397Sriastradh }
234735c2397Sriastradh 
235735c2397Sriastradh /*
236735c2397Sriastradh  * Destroy an offtab.
237735c2397Sriastradh  */
238735c2397Sriastradh void
offtab_destroy(struct offtab * offtab)239735c2397Sriastradh offtab_destroy(struct offtab *offtab)
240735c2397Sriastradh {
241735c2397Sriastradh 
2423e40e9d7Sriastradh 	free(offtab->ot_window);
243735c2397Sriastradh }
244735c2397Sriastradh 
245735c2397Sriastradh /*
246735c2397Sriastradh  * For an offtab that has been used to read data from disk, convert it
247735c2397Sriastradh  * to an offtab that can be used to write subsequent data to disk.
2483e40e9d7Sriastradh  * blkno is the last valid blkno read from disk.
249735c2397Sriastradh  */
2503e40e9d7Sriastradh bool
offtab_transmogrify_read_to_write(struct offtab * offtab,uint32_t blkno)2513e40e9d7Sriastradh offtab_transmogrify_read_to_write(struct offtab *offtab, uint32_t blkno)
252735c2397Sriastradh {
253735c2397Sriastradh 
254735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_READ);
2553e40e9d7Sriastradh 	assert(0 < blkno);
2563e40e9d7Sriastradh 
2573e40e9d7Sriastradh 	if (!offtab_maybe_read_window(offtab, blkno, OFFTAB_READ_SEEK))
2583e40e9d7Sriastradh 		return false;
2593e40e9d7Sriastradh 
260735c2397Sriastradh 	offtab->ot_mode = OFFTAB_MODE_WRITE;
2613e40e9d7Sriastradh 	offtab->ot_blkno = blkno;
2623e40e9d7Sriastradh 
2633e40e9d7Sriastradh 	return true;
264735c2397Sriastradh }
265735c2397Sriastradh 
266735c2397Sriastradh /*
267735c2397Sriastradh  * Reset an offtab for reading an offset table from the beginning.
268735c2397Sriastradh  * Initializes in-memory state and may read data from offtab->ot_fd,
269735c2397Sriastradh  * which must currently be at byte position offtab->ot_fdpos.  Failure
270735c2397Sriastradh  * will be reported by the report/reportx routines, which are called
271735c2397Sriastradh  * like warn/warnx.  May fail; returns true on success, false on
272735c2397Sriastradh  * failure.
2733e40e9d7Sriastradh  *
2743e40e9d7Sriastradh  * This almost has copypasta of offtab_prepare_get, but this uses read,
2753e40e9d7Sriastradh  * rather than pread, so that it will work on nonseekable input if the
2763e40e9d7Sriastradh  * window is the whole offset table.
277735c2397Sriastradh  */
278735c2397Sriastradh bool
279735c2397Sriastradh offtab_reset_read(struct offtab *offtab,
280735c2397Sriastradh     void (*report)(const char *, ...) __printflike(1,2),
281735c2397Sriastradh     void (*reportx)(const char *, ...) __printflike(1,2))
282735c2397Sriastradh {
283735c2397Sriastradh 
284735c2397Sriastradh 	assert((lseek(offtab->ot_fd, 0, SEEK_CUR) == offtab->ot_fdpos) ||
285735c2397Sriastradh 	    ((lseek(offtab->ot_fd, 0, SEEK_CUR) == -1) && (errno == ESPIPE)));
286735c2397Sriastradh 
287735c2397Sriastradh 	offtab->ot_report = report;
288735c2397Sriastradh 	offtab->ot_reportx = reportx;
289735c2397Sriastradh 	offtab->ot_mode = OFFTAB_MODE_READ;
2903e40e9d7Sriastradh 	offtab->ot_blkno = (uint32_t)-1;
291735c2397Sriastradh 
2923e40e9d7Sriastradh 	if (!offtab_read_window(offtab, 0, OFFTAB_READ_NOSEEK))
293735c2397Sriastradh 		return false;
294735c2397Sriastradh 
295a3544454Sriastradh 	if (offtab->ot_window_size < offtab->ot_n_offsets) {
296*76d4b812Sriastradh 		__CTASSERT(MUL_OK(off_t, MAX_N_OFFSETS, sizeof(uint64_t)));
29708530105Sriastradh 		const off_t offtab_bytes = ((off_t)offtab->ot_n_offsets *
29808530105Sriastradh 		    (off_t)sizeof(uint64_t));
2999719b6dcSriastradh 		assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
300*76d4b812Sriastradh 		__CTASSERT(ADD_OK(off_t, OFFTAB_MAX_FDPOS,
301*76d4b812Sriastradh 			(off_t)MAX_N_OFFSETS*sizeof(uint64_t)));
302*76d4b812Sriastradh 		assert(ADD_OK(off_t, offtab->ot_fdpos, offtab_bytes));
30308530105Sriastradh 		const off_t first_offset = (offtab->ot_fdpos + offtab_bytes);
304a3544454Sriastradh 		if (lseek(offtab->ot_fd, first_offset, SEEK_SET) == -1) {
305a3544454Sriastradh 			(*offtab->ot_report)("lseek to first offset 0x%"PRIx64,
306a3544454Sriastradh 			    first_offset);
307a3544454Sriastradh 			return false;
308a3544454Sriastradh 		}
309a3544454Sriastradh 	}
310a3544454Sriastradh 
311735c2397Sriastradh 	return true;
312735c2397Sriastradh }
313735c2397Sriastradh 
314735c2397Sriastradh /*
315735c2397Sriastradh  * Do any I/O or bookkeeping necessary to fetch the offset for blkno in
316735c2397Sriastradh  * preparation for a call to offtab_get.  May fail; returns true on
317735c2397Sriastradh  * success, false on failure.
318735c2397Sriastradh  */
319735c2397Sriastradh bool
offtab_prepare_get(struct offtab * offtab,uint32_t blkno)320735c2397Sriastradh offtab_prepare_get(struct offtab *offtab, uint32_t blkno)
321735c2397Sriastradh {
322735c2397Sriastradh 
323735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_READ);
324735c2397Sriastradh 	assert(blkno < offtab->ot_n_offsets);
3253e40e9d7Sriastradh 
3263e40e9d7Sriastradh 	if (!offtab_maybe_read_window(offtab, blkno, OFFTAB_READ_SEEK))
3273e40e9d7Sriastradh 		return false;
3283e40e9d7Sriastradh 
3293e40e9d7Sriastradh 	assert(offtab->ot_window_start <= blkno);
3303e40e9d7Sriastradh 	assert(blkno < offtab_current_window_end(offtab));
3313e40e9d7Sriastradh 
332735c2397Sriastradh 	offtab->ot_blkno = blkno;
333735c2397Sriastradh 	return true;
334735c2397Sriastradh }
335735c2397Sriastradh 
336735c2397Sriastradh /*
337735c2397Sriastradh  * Return the offset for blkno.  Caller must have called
338735c2397Sriastradh  * offtab_prepare_get beforehand.
339735c2397Sriastradh  */
340735c2397Sriastradh uint64_t
offtab_get(struct offtab * offtab,uint32_t blkno)341735c2397Sriastradh offtab_get(struct offtab *offtab, uint32_t blkno)
342735c2397Sriastradh {
343735c2397Sriastradh 
344735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_READ);
345735c2397Sriastradh 	assert(blkno == offtab->ot_blkno);
3463e40e9d7Sriastradh 	assert(offtab->ot_window_start <= blkno);
3473e40e9d7Sriastradh 	assert(blkno < offtab_current_window_end(offtab));
3483e40e9d7Sriastradh 
3493e40e9d7Sriastradh 	return be64toh(offtab->ot_window[blkno - offtab->ot_window_start]);
350735c2397Sriastradh }
351735c2397Sriastradh 
352735c2397Sriastradh /*
353735c2397Sriastradh  * Reset offtab for writing a fresh offset table.  Initializes
354735c2397Sriastradh  * in-memory state and writes an empty offset table to offtab->ot_fd,
355735c2397Sriastradh  * which must currently be at byte position offtab->ot_fdpos.  May
356735c2397Sriastradh  * fail; returns on success, aborts with err(3) on failure.
357735c2397Sriastradh  */
358735c2397Sriastradh void
offtab_reset_write(struct offtab * offtab)359735c2397Sriastradh offtab_reset_write(struct offtab *offtab)
360735c2397Sriastradh {
361735c2397Sriastradh 	uint32_t i;
362735c2397Sriastradh 
363735c2397Sriastradh 	assert(lseek(offtab->ot_fd, 0, SEEK_CUR) == offtab->ot_fdpos);
364735c2397Sriastradh 
365735c2397Sriastradh 	offtab->ot_mode = OFFTAB_MODE_WRITE;
3663e40e9d7Sriastradh 	offtab->ot_blkno = (uint32_t)-1;
367735c2397Sriastradh 
368735c2397Sriastradh 	/*
369735c2397Sriastradh 	 * Initialize the offset table to all ones (except for the
370735c2397Sriastradh 	 * fixed first offset) so that we can easily detect where we
371735c2397Sriastradh 	 * were interrupted if we want to restart.
372735c2397Sriastradh 	 */
373735c2397Sriastradh 	__CTASSERT(MAX_N_OFFSETS <= UINT32_MAX);
374735c2397Sriastradh 	assert(offtab->ot_n_offsets > 0);
375735c2397Sriastradh 
3769719b6dcSriastradh 	/* Initialize window of all ones.  */
3773e40e9d7Sriastradh 	for (i = 0; i < offtab->ot_window_size; i++)
3783e40e9d7Sriastradh 		offtab->ot_window[i] = ~(uint64_t)0;
3793e40e9d7Sriastradh 
3809719b6dcSriastradh 	/* Write the window to every position in the table.  */
3813e40e9d7Sriastradh 	const uint32_t n_windows =
3823e40e9d7Sriastradh 	    howmany(offtab->ot_n_offsets, offtab->ot_window_size);
3833e40e9d7Sriastradh 	for (i = 1; i < n_windows; i++) {
3843e40e9d7Sriastradh 		/* Change the start but reuse the all-ones buffer.  */
3853e40e9d7Sriastradh 		offtab->ot_window_start = (i * offtab->ot_window_size);
386e70e0058Sriastradh 		offtab_write_window(offtab);
3873e40e9d7Sriastradh 	}
3883e40e9d7Sriastradh 
3899719b6dcSriastradh 	/* Compute the number of bytes in the offset table.  */
390*76d4b812Sriastradh 	__CTASSERT(MUL_OK(off_t, MAX_N_OFFSETS, sizeof(uint64_t)));
39108530105Sriastradh 	const off_t offtab_bytes = ((off_t)offtab->ot_n_offsets *
39208530105Sriastradh 	    sizeof(uint64_t));
3939719b6dcSriastradh 
3949719b6dcSriastradh 	/* Compute the offset of the first block.  */
3959719b6dcSriastradh 	assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
396*76d4b812Sriastradh 	__CTASSERT(ADD_OK(off_t, OFFTAB_MAX_FDPOS,
397*76d4b812Sriastradh 		MAX_N_OFFSETS*sizeof(uint64_t)));
398*76d4b812Sriastradh 	assert(ADD_OK(off_t, offtab->ot_fdpos, offtab_bytes));
39908530105Sriastradh 	const off_t first_offset = (offtab->ot_fdpos + offtab_bytes);
4009719b6dcSriastradh 
4019719b6dcSriastradh 	/* Assert that it fits in 64 bits.  */
402*76d4b812Sriastradh 	__CTASSERT(MUL_OK(uint64_t, MAX_N_OFFSETS, sizeof(uint64_t)));
403*76d4b812Sriastradh 	__CTASSERT(ADD_OK(uint64_t, OFFTAB_MAX_FDPOS,
404*76d4b812Sriastradh 		(uint64_t)MAX_N_OFFSETS*sizeof(uint64_t)));
4059719b6dcSriastradh 
4069719b6dcSriastradh 	/* Write out the first window with the first offset.  */
4079719b6dcSriastradh 	offtab->ot_window_start = 0;
40808530105Sriastradh 	offtab->ot_window[0] = htobe64((uint64_t)first_offset);
409e70e0058Sriastradh 	offtab_write_window(offtab);
4103e40e9d7Sriastradh 
4113e40e9d7Sriastradh 	if (lseek(offtab->ot_fd, first_offset, SEEK_SET) == -1)
4123e40e9d7Sriastradh 		err(1, "lseek to first offset failed");
413735c2397Sriastradh }
414735c2397Sriastradh 
415735c2397Sriastradh /*
416735c2397Sriastradh  * Guarantee that the disk reflects block offsets [0, n_offsets).  If
417735c2397Sriastradh  * OFFTAB_CHECKPOINT_SYNC is set in flags, will also fsync the entire
418735c2397Sriastradh  * offset table.  May fail; returns on success, aborts with err(3) on
419735c2397Sriastradh  * failure.  Fsync failure is considered success but is reported with a
420735c2397Sriastradh  * warning.
421735c2397Sriastradh  *
422735c2397Sriastradh  * This routine does not write state in memory, and does not read state
4233e40e9d7Sriastradh  * that is not signal-safe.  The only state read is offtab->ot_window,
4243e40e9d7Sriastradh  * offtab->ot_window_start, and quantities that are static for the
4253e40e9d7Sriastradh  * signal-interruptable existence of the offset table.
426735c2397Sriastradh  */
427735c2397Sriastradh void
offtab_checkpoint(struct offtab * offtab,uint32_t n_offsets,int flags)428735c2397Sriastradh offtab_checkpoint(struct offtab *offtab, uint32_t n_offsets, int flags)
429735c2397Sriastradh {
430735c2397Sriastradh 
431735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
432735c2397Sriastradh 	assert(n_offsets <= offtab->ot_n_offsets);
433735c2397Sriastradh 
4343e40e9d7Sriastradh 	/*
4353e40e9d7Sriastradh 	 * Write the window unless we just did that and were
4363e40e9d7Sriastradh 	 * interrupted before we could move the window.
4373e40e9d7Sriastradh 	 */
4383e40e9d7Sriastradh 	if (offtab->ot_window != NULL)
439e70e0058Sriastradh 		offtab_maybe_write_window(offtab, 0, n_offsets);
440735c2397Sriastradh 
441735c2397Sriastradh 	if (ISSET(flags, OFFTAB_CHECKPOINT_SYNC)) {
442*76d4b812Sriastradh 		__CTASSERT(MUL_OK(off_t, MAX_N_OFFSETS, sizeof(uint64_t)));
44308530105Sriastradh 		const off_t sync_bytes = ((off_t)n_offsets *
44408530105Sriastradh 		    (off_t)sizeof(uint64_t));
445*76d4b812Sriastradh 		__CTASSERT(ADD_OK(off_t, OFFTAB_MAX_FDPOS,
446*76d4b812Sriastradh 			MAX_N_OFFSETS*sizeof(uint64_t)));
447*76d4b812Sriastradh 		assert(ADD_OK(off_t, offtab->ot_fdpos, sync_bytes));
448735c2397Sriastradh 		if (fsync_range(offtab->ot_fd, (FFILESYNC | FDISKSYNC),
44908530105Sriastradh 			offtab->ot_fdpos, (offtab->ot_fdpos + sync_bytes))
450735c2397Sriastradh 		    == -1)
451735c2397Sriastradh 			warn_ss("fsync of offset table failed");
452735c2397Sriastradh 	}
453735c2397Sriastradh }
454735c2397Sriastradh 
455735c2397Sriastradh /*
456735c2397Sriastradh  * Do any I/O or bookkeeping necessary to set an offset for blkno.  May
457735c2397Sriastradh  * fail; returns on success, aborts with err(3) on failure.
458735c2397Sriastradh  */
459735c2397Sriastradh void
offtab_prepare_put(struct offtab * offtab,uint32_t blkno)460735c2397Sriastradh offtab_prepare_put(struct offtab *offtab, uint32_t blkno)
461735c2397Sriastradh {
4623e40e9d7Sriastradh 	uint32_t i;
463735c2397Sriastradh 
464735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
465735c2397Sriastradh 	assert(blkno < offtab->ot_n_offsets);
4663e40e9d7Sriastradh 
4673e40e9d7Sriastradh 	/*
4683e40e9d7Sriastradh 	 * Assume, for convenience, that we write blocks in order.
4693e40e9d7Sriastradh 	 * Thus we need not do another read -- we can just clear the
4703e40e9d7Sriastradh 	 * window.
4713e40e9d7Sriastradh 	 */
4723e40e9d7Sriastradh 	assert((offtab->ot_blkno == (uint32_t)-1) ||
4733e40e9d7Sriastradh 	    ((offtab->ot_blkno + 1) == blkno));
4743e40e9d7Sriastradh 
4753e40e9d7Sriastradh 	/* If it's already in our window, we're good to go.  */
4763e40e9d7Sriastradh 	if ((offtab->ot_window_start <= blkno) &&
4773e40e9d7Sriastradh 	    (blkno < offtab_current_window_end(offtab)))
4783e40e9d7Sriastradh 		goto win;
4793e40e9d7Sriastradh 
4803e40e9d7Sriastradh 	/* Otherwise, write out the current window and choose a new one.  */
481e70e0058Sriastradh 	offtab_write_window(offtab);
4823e40e9d7Sriastradh 
4833e40e9d7Sriastradh 	assert(offtab->ot_window_size <= blkno);
4843e40e9d7Sriastradh 	assert(offtab->ot_window_start == (blkno - offtab->ot_window_size));
4853e40e9d7Sriastradh 	assert((offtab->ot_window_start + offtab->ot_window_size) ==
4863e40e9d7Sriastradh 	    rounddown(blkno, offtab->ot_window_size));
4873e40e9d7Sriastradh 
4883e40e9d7Sriastradh     {
4893e40e9d7Sriastradh 	uint64_t *window;
4903e40e9d7Sriastradh 	sigset_t sigmask;
4913e40e9d7Sriastradh 
4923e40e9d7Sriastradh 	/*
4933e40e9d7Sriastradh 	 * Mark the window as being updated so nobody tries to write it
494a0dfc294Sriastradh 	 * (since we just wrote it) while we fill it with ones.
4953e40e9d7Sriastradh 	 */
4963e40e9d7Sriastradh 	block_signals(&sigmask);
4973e40e9d7Sriastradh 	window = offtab->ot_window;
4983e40e9d7Sriastradh 	offtab->ot_window = NULL;
4993e40e9d7Sriastradh 	restore_sigmask(&sigmask);
5003e40e9d7Sriastradh 
5013e40e9d7Sriastradh 	/* Fill the window with ones.  */
5023e40e9d7Sriastradh 	for (i = 0; i < offtab_current_window_size(offtab); i++)
5033e40e9d7Sriastradh 		window[i] = ~(uint64_t)0;
5043e40e9d7Sriastradh 
5053e40e9d7Sriastradh 	/* Restore the window as ready again.  */
5063e40e9d7Sriastradh 	block_signals(&sigmask);
5073e40e9d7Sriastradh 	offtab->ot_window = window;
5083e40e9d7Sriastradh 	offtab->ot_window_start = rounddown(blkno, offtab->ot_window_size);
5093e40e9d7Sriastradh 	restore_sigmask(&sigmask);
5103e40e9d7Sriastradh     }
5113e40e9d7Sriastradh 
5123e40e9d7Sriastradh win:	assert(offtab->ot_window_start <= blkno);
5133e40e9d7Sriastradh 	assert(blkno < offtab_current_window_end(offtab));
5143e40e9d7Sriastradh 
515735c2397Sriastradh 	offtab->ot_blkno = blkno;
516735c2397Sriastradh }
517735c2397Sriastradh 
518735c2397Sriastradh /*
519735c2397Sriastradh  * Actually set the offset for blkno.
520735c2397Sriastradh  */
521735c2397Sriastradh void
offtab_put(struct offtab * offtab,uint32_t blkno,uint64_t offset)522735c2397Sriastradh offtab_put(struct offtab *offtab, uint32_t blkno, uint64_t offset)
523735c2397Sriastradh {
524735c2397Sriastradh 
525735c2397Sriastradh 	assert(offtab->ot_mode == OFFTAB_MODE_WRITE);
526735c2397Sriastradh 	assert(blkno == offtab->ot_blkno);
5273e40e9d7Sriastradh 	assert(offtab->ot_window_start <= blkno);
5283e40e9d7Sriastradh 	assert(blkno < offtab_current_window_end(offtab));
5293e40e9d7Sriastradh 
5303e40e9d7Sriastradh 	offtab->ot_window[blkno - offtab->ot_window_start] = htobe64(offset);
531735c2397Sriastradh }
532