/*-
* 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.
*/
static bool
offtab_maybe_read_window(struct offtab *offtab, uint32_t blkno, int read_flags)
{
/* Don't bother if blkno is already in the window. */
if ((offtab->ot_window_start <= blkno) &&
(blkno < offtab_current_window_end(offtab)))
return true;
if (!offtab_read_window(offtab, blkno, read_flags))
return false;
/* Don't bother if [start, end) does not cover our window. */
if (end <= offtab->ot_window_start)
return;
if (offtab_current_window_end(offtab) < start)
return;
offtab_write_window(offtab);
}
/*
* 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, uint32_t window_size,
int fd, off_t fdpos)
{
/*
* 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.
* blkno is the last valid blkno read from disk.
*/
bool
offtab_transmogrify_read_to_write(struct offtab *offtab, uint32_t blkno)
{
/*
* 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.
*
* This almost has copypasta of offtab_prepare_get, but this uses read,
* rather than pread, so that it will work on nonseekable input if the
* window is the whole offset table.
*/
bool
offtab_reset_read(struct offtab *offtab,
void (*report)(const char *, ...) __printflike(1,2),
void (*reportx)(const char *, ...) __printflike(1,2))
{
/*
* 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)
{
/*
* Return the offset for blkno. Caller must have called
* offtab_prepare_get beforehand.
*/
uint64_t
offtab_get(struct offtab *offtab, uint32_t 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;
/*
* 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);
/* Initialize window of all ones. */
for (i = 0; i < offtab->ot_window_size; i++)
offtab->ot_window[i] = ~(uint64_t)0;
/* Write the window to every position in the table. */
const uint32_t n_windows =
howmany(offtab->ot_n_offsets, offtab->ot_window_size);
for (i = 1; i < n_windows; i++) {
/* Change the start but reuse the all-ones buffer. */
offtab->ot_window_start = (i * offtab->ot_window_size);
offtab_write_window(offtab);
}
/* Compute the number of bytes in the offset table. */
__CTASSERT(MUL_OK(off_t, MAX_N_OFFSETS, sizeof(uint64_t)));
const off_t offtab_bytes = ((off_t)offtab->ot_n_offsets *
sizeof(uint64_t));
/* Compute the offset of the first block. */
assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
__CTASSERT(ADD_OK(off_t, OFFTAB_MAX_FDPOS,
MAX_N_OFFSETS*sizeof(uint64_t)));
assert(ADD_OK(off_t, offtab->ot_fdpos, offtab_bytes));
const off_t first_offset = (offtab->ot_fdpos + offtab_bytes);
/* Assert that it fits in 64 bits. */
__CTASSERT(MUL_OK(uint64_t, MAX_N_OFFSETS, sizeof(uint64_t)));
__CTASSERT(ADD_OK(uint64_t, OFFTAB_MAX_FDPOS,
(uint64_t)MAX_N_OFFSETS*sizeof(uint64_t)));
/* Write out the first window with the first offset. */
offtab->ot_window_start = 0;
offtab->ot_window[0] = htobe64((uint64_t)first_offset);
offtab_write_window(offtab);
if (lseek(offtab->ot_fd, first_offset, SEEK_SET) == -1)
err(1, "lseek to first offset failed");
}
/*
* 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 offtab->ot_window,
* offtab->ot_window_start, and quantities that are static for the
* signal-interruptable existence of the offset table.
*/
void
offtab_checkpoint(struct offtab *offtab, uint32_t n_offsets, int flags)
{
/*
* Write the window unless we just did that and were
* interrupted before we could move the window.
*/
if (offtab->ot_window != NULL)
offtab_maybe_write_window(offtab, 0, n_offsets);
/*
* 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)
{
uint32_t i;
/*
* Assume, for convenience, that we write blocks in order.
* Thus we need not do another read -- we can just clear the
* window.
*/
assert((offtab->ot_blkno == (uint32_t)-1) ||
((offtab->ot_blkno + 1) == blkno));
/* If it's already in our window, we're good to go. */
if ((offtab->ot_window_start <= blkno) &&
(blkno < offtab_current_window_end(offtab)))
goto win;
/* Otherwise, write out the current window and choose a new one. */
offtab_write_window(offtab);
/*
* Mark the window as being updated so nobody tries to write it
* (since we just wrote it) while we fill it with ones.
*/
block_signals(&sigmask);
window = offtab->ot_window;
offtab->ot_window = NULL;
restore_sigmask(&sigmask);
/* Fill the window with ones. */
for (i = 0; i < offtab_current_window_size(offtab); i++)
window[i] = ~(uint64_t)0;
/* Restore the window as ready again. */
block_signals(&sigmask);
offtab->ot_window = window;
offtab->ot_window_start = rounddown(blkno, offtab->ot_window_size);
restore_sigmask(&sigmask);
}