Add TXG timestamp database

This feature enables tracking of when TXGs are committed to disk,
providing an estimated timestamp for each TXG.

With this information, it becomes possible to perform scrubs based
on specific date ranges, improving the granularity of data
management and recovery operations.

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Alexander Motin <alexander.motin@TrueNAS.com>
Reviewed-by: Paul Dagnelie <paul.dagnelie@klarasystems.com>
Signed-off-by: Mariusz Zaborski <mariusz.zaborski@klarasystems.com>
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Closes #16853
This commit is contained in:
Mariusz Zaborski
2025-08-06 19:31:21 +02:00
committed by GitHub
parent c3496b5cc6
commit 894edd084e
21 changed files with 736 additions and 10 deletions
+1
View File
@@ -406,6 +406,7 @@ ZFS_OBJS := \
zfs_byteswap.o \
zfs_chksum.o \
zfs_debug_common.o \
zfs_crrd.o \
zfs_fm.o \
zfs_fuid.o \
zfs_impl.o \
+1
View File
@@ -217,6 +217,7 @@ SRCS+= abd_os.c \
vdev_label_os.c \
zfs_acl.c \
zfs_ctldir.c \
zfs_crrd.c \
zfs_debug.c \
zfs_dir.c \
zfs_file_os.c \
+160
View File
@@ -100,6 +100,7 @@
#include <sys/vmsystm.h>
#endif /* _KERNEL */
#include "zfs_crrd.h"
#include "zfs_prop.h"
#include "zfs_comutil.h"
#include <cityhash.h>
@@ -310,6 +311,41 @@ static int zfs_livelist_condense_zthr_cancel = 0;
*/
static int zfs_livelist_condense_new_alloc = 0;
/*
* Time variable to decide how often the txg should be added into the
* database (in seconds).
* The smallest available resolution is in minutes, which means an update occurs
* each time we reach `spa_note_txg_time` and the txg has changed. We provide
* a 256-slot ring buffer for minute-level resolution. The number is limited by
* the size of the structure we use and the maximum amount of bytes we can write
* into ZAP. Setting `spa_note_txg_time` to 10 minutes results in approximately
* 144 records per day. Given the 256 slots, this provides roughly 1.5 days of
* high-resolution data.
*
* The user can decrease `spa_note_txg_time` to increase resolution within
* a day, at the cost of retaining fewer days of data. Alternatively, increasing
* the interval allows storing data over a longer period, but with lower
* frequency.
*
* This parameter does not affect the daily or monthly databases, as those only
* store one record per day and per month, respectively.
*/
static uint_t spa_note_txg_time = 10 * 60;
/*
* How often flush txg database to a disk (in seconds).
* We flush data every time we write to it, making it the most reliable option.
* Since this happens every 10 minutes, it shouldn't introduce any noticeable
* overhead for the system. In case of failure, we will always have an
* up-to-date version of the database.
*
* The user can adjust the flush interval to a lower value, but it probably
* doesn't make sense to flush more often than the database is updated.
* The user can also increase the interval if they're concerned about the
* performance of writing the entire database to disk.
*/
static uint_t spa_flush_txg_time = 10 * 60;
/*
* ==========================================================================
* SPA properties routines
@@ -2040,6 +2076,111 @@ spa_destroy_aux_threads(spa_t *spa)
}
}
static void
spa_sync_time_logger(spa_t *spa, uint64_t txg)
{
uint64_t curtime;
dmu_tx_t *tx;
if (!spa_writeable(spa)) {
return;
}
curtime = gethrestime_sec();
if (curtime < spa->spa_last_noted_txg_time + spa_note_txg_time) {
return;
}
if (txg > spa->spa_last_noted_txg) {
spa->spa_last_noted_txg_time = curtime;
spa->spa_last_noted_txg = txg;
mutex_enter(&spa->spa_txg_log_time_lock);
dbrrd_add(&spa->spa_txg_log_time, curtime, txg);
mutex_exit(&spa->spa_txg_log_time_lock);
}
if (curtime < spa->spa_last_flush_txg_time + spa_flush_txg_time) {
return;
}
spa->spa_last_flush_txg_time = curtime;
tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
VERIFY0(zap_update(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_MINUTES, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_minutes, tx));
VERIFY0(zap_update(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_DAYS, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_days, tx));
VERIFY0(zap_update(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_MONTHS, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_months, tx));
dmu_tx_commit(tx);
}
static void
spa_unload_sync_time_logger(spa_t *spa)
{
uint64_t txg;
dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT));
txg = dmu_tx_get_txg(tx);
spa->spa_last_noted_txg_time = 0;
spa->spa_last_flush_txg_time = 0;
spa_sync_time_logger(spa, txg);
dmu_tx_commit(tx);
}
static void
spa_load_txg_log_time(spa_t *spa)
{
int error;
error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_MINUTES, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_minutes);
if (error != 0 && error != ENOENT) {
spa_load_note(spa, "unable to load a txg time database with "
"minute resolution [error=%d]", error);
}
error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_DAYS, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_days);
if (error != 0 && error != ENOENT) {
spa_load_note(spa, "unable to load a txg time database with "
"day resolution [error=%d]", error);
}
error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
DMU_POOL_TXG_LOG_TIME_MONTHS, RRD_ENTRY_SIZE, RRD_STRUCT_ELEM,
&spa->spa_txg_log_time.dbr_months);
if (error != 0 && error != ENOENT) {
spa_load_note(spa, "unable to load a txg time database with "
"month resolution [error=%d]", error);
}
}
static boolean_t
spa_should_sync_time_logger_on_unload(spa_t *spa)
{
if (!spa_writeable(spa))
return (B_FALSE);
if (!spa->spa_sync_on)
return (B_FALSE);
if (spa_state(spa) != POOL_STATE_EXPORTED)
return (B_FALSE);
if (spa->spa_last_noted_txg == 0)
return (B_FALSE);
return (B_TRUE);
}
/*
* Opposite of spa_load().
*/
@@ -2061,6 +2202,9 @@ spa_unload(spa_t *spa)
* we delay the final TXGs beyond what spa_final_txg is set at.
*/
if (spa->spa_final_txg == UINT64_MAX) {
if (spa_should_sync_time_logger_on_unload(spa))
spa_unload_sync_time_logger(spa);
/*
* If the log space map feature is enabled and the pool is
* getting exported (but not destroyed), we want to spend some
@@ -4717,6 +4861,9 @@ spa_ld_get_props(spa_t *spa)
if (error != 0 && error != ENOENT)
return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
/* Load time log */
spa_load_txg_log_time(spa);
/*
* Load the persistent error log. If we have an older pool, this will
* not be present.
@@ -7140,6 +7287,9 @@ spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig,
spa_config_exit(spa, SCL_ALL, FTAG);
}
if (spa_should_sync_time_logger_on_unload(spa))
spa_unload_sync_time_logger(spa);
/*
* If the log space map feature is enabled and the pool is
* getting exported (but not destroyed), we want to spend some
@@ -10190,6 +10340,8 @@ spa_sync(spa_t *spa, uint64_t txg)
*/
brt_pending_apply(spa, txg);
spa_sync_time_logger(spa, txg);
/*
* Lock out configuration changes.
*/
@@ -10232,6 +10384,7 @@ spa_sync(spa_t *spa, uint64_t txg)
dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
spa->spa_sync_starttime = gethrtime();
taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
@@ -11105,6 +11258,13 @@ ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT,
"Whether extra ALLOC blkptrs were added to a livelist entry while it "
"was being condensed");
ZFS_MODULE_PARAM(zfs_spa, spa_, note_txg_time, UINT, ZMOD_RW,
"How frequently TXG timestamps are stored internally (in seconds)");
ZFS_MODULE_PARAM(zfs_spa, spa_, flush_txg_time, UINT, ZMOD_RW,
"How frequently the TXG timestamps database should be flushed "
"to disk (in seconds)");
#ifdef _KERNEL
ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio, zio_, taskq_read,
spa_taskq_read_param_set, spa_taskq_read_param_get, ZMOD_RW,
+2
View File
@@ -715,6 +715,7 @@ spa_add(const char *name, nvlist_t *config, const char *altroot)
mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&spa->spa_txg_log_time_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
@@ -903,6 +904,7 @@ spa_remove(spa_t *spa)
mutex_destroy(&spa->spa_vdev_top_lock);
mutex_destroy(&spa->spa_feat_stats_lock);
mutex_destroy(&spa->spa_activities_lock);
mutex_destroy(&spa->spa_txg_log_time_lock);
kmem_free(spa, sizeof (spa_t));
}
+227
View File
@@ -0,0 +1,227 @@
// SPDX-License-Identifier: CDDL-1.0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2024 Klara Inc.
*
* This software was developed by
* Mariusz Zaborski <mariusz.zaborski@klarasystems.com>
* Fred Weigel <fred.weigel@klarasystems.com>
* under sponsorship from Wasabi Technology, Inc. and Klara Inc.
*/
/*
* This file implements a round-robin database that stores timestamps and txg
* numbers. Due to limited space, we use a round-robin approach, where
* the oldest records are overwritten when there is no longer enough room.
* This is a best-effort mechanism, and the database should be treated as
* an approximation. Consider this before consuming it.
*
* The database is linear, meaning we assume each new entry is newer than the
* ones already stored. Because of this, if time is manipulated, the database
* will only accept records that are newer than the existing ones.
* (For example, jumping 10 years into the future and then back can lead to
* situation when for 10 years we wont write anything to database)
*
* All times stored in the database use UTC, which makes it easy to convert to
* and from local time.
*
* Each database holds 256 records (as defined in the `RRD_MAX_ENTRIES` macro).
* This limit comes from the maximum size of a ZAP object, where we store the
* binary blob.
*
* We've split the database into three smaller ones.
* The `minute database` provides high resolution (default: every 10 minutes),
* but only covers approximately 1.5 days. This gives a detailed view of recent
* activity, useful, for example, when performing a scrub of the last hour.
* The `daily database` records one txg per day. With 256 entries, it retains
* roughly 8 months of data. This allows users to scrub or analyze txgs across
* a range of days.
* The `monthly database` stores one record per month, giving approximately
* 21 years of history.
* All these calculations assume the worst-case scenario: the pool is always
* online and actively written to.
*
* A potential source of confusion is that the database does not store data
* while the pool is offline, leading to potential gaps in timeline. Also,
* the database contains no records from before this feature was enabled.
* Both, upon reflection, are expected.
*/
#include <sys/zfs_context.h>
#include "zfs_crrd.h"
rrd_data_t *
rrd_tail_entry(rrd_t *rrd)
{
size_t n;
if (rrd_len(rrd) == 0)
return (NULL);
if (rrd->rrd_tail == 0)
n = RRD_MAX_ENTRIES - 1;
else
n = rrd->rrd_tail - 1;
return (&rrd->rrd_entries[n]);
}
uint64_t
rrd_tail(rrd_t *rrd)
{
const rrd_data_t *tail;
tail = rrd_tail_entry(rrd);
return (tail == NULL ? 0 : tail->rrdd_time);
}
/*
* Return length of data in the rrd.
* rrd_get works from 0..rrd_len()-1.
*/
size_t
rrd_len(rrd_t *rrd)
{
return (rrd->rrd_length);
}
const rrd_data_t *
rrd_entry(rrd_t *rrd, size_t i)
{
size_t n;
if (i >= rrd_len(rrd)) {
return (0);
}
n = (rrd->rrd_head + i) % RRD_MAX_ENTRIES;
return (&rrd->rrd_entries[n]);
}
uint64_t
rrd_get(rrd_t *rrd, size_t i)
{
const rrd_data_t *data = rrd_entry(rrd, i);
return (data == NULL ? 0 : data->rrdd_txg);
}
/* Add value to database. */
void
rrd_add(rrd_t *rrd, hrtime_t time, uint64_t txg)
{
rrd_data_t *tail;
tail = rrd_tail_entry(rrd);
if (tail != NULL && tail->rrdd_time == time) {
if (tail->rrdd_txg < txg) {
tail->rrdd_txg = txg;
} else {
return;
}
}
rrd->rrd_entries[rrd->rrd_tail].rrdd_time = time;
rrd->rrd_entries[rrd->rrd_tail].rrdd_txg = txg;
rrd->rrd_tail = (rrd->rrd_tail + 1) % RRD_MAX_ENTRIES;
if (rrd->rrd_length < RRD_MAX_ENTRIES) {
rrd->rrd_length++;
} else {
rrd->rrd_head = (rrd->rrd_head + 1) % RRD_MAX_ENTRIES;
}
}
void
dbrrd_add(dbrrd_t *db, hrtime_t time, uint64_t txg)
{
hrtime_t daydiff, monthdiff, minutedif;
minutedif = time - rrd_tail(&db->dbr_minutes);
daydiff = time - rrd_tail(&db->dbr_days);
monthdiff = time - rrd_tail(&db->dbr_months);
if (monthdiff >= 0 && monthdiff >= SEC2NSEC(30 * 24 * 60 * 60))
rrd_add(&db->dbr_months, time, txg);
else if (daydiff >= 0 && daydiff >= SEC2NSEC(24 * 60 * 60))
rrd_add(&db->dbr_days, time, txg);
else if (minutedif >= 0)
rrd_add(&db->dbr_minutes, time, txg);
}
/*
* We could do a binary search here, but the routine isn't frequently
* called and the data is small so we stick to a simple loop.
*/
static const rrd_data_t *
rrd_query(rrd_t *rrd, hrtime_t tv, dbrrd_rounding_t rounding)
{
const rrd_data_t *data = NULL;
for (size_t i = 0; i < rrd_len(rrd); i++) {
const rrd_data_t *cur = rrd_entry(rrd, i);
if (rounding == DBRRD_FLOOR) {
if (tv < cur->rrdd_time) {
break;
}
data = cur;
} else {
/* DBRRD_CEILING */
if (tv <= cur->rrdd_time) {
data = cur;
break;
}
}
}
return (data);
}
static const rrd_data_t *
dbrrd_closest(hrtime_t tv, const rrd_data_t *r1, const rrd_data_t *r2)
{
if (r1 == NULL)
return (r2);
if (r2 == NULL)
return (r1);
return (ABS(tv - r1->rrdd_time) < ABS(tv - r2->rrdd_time) ? r1 : r2);
}
uint64_t
dbrrd_query(dbrrd_t *r, hrtime_t tv, dbrrd_rounding_t rounding)
{
const rrd_data_t *data, *dm, *dd, *dy;
data = NULL;
dm = rrd_query(&r->dbr_minutes, tv, rounding);
dd = rrd_query(&r->dbr_days, tv, rounding);
dy = rrd_query(&r->dbr_months, tv, rounding);
data = dbrrd_closest(tv, dbrrd_closest(tv, dd, dm), dy);
return (data == NULL ? 0 : data->rrdd_txg);
}
+26 -1
View File
@@ -1704,6 +1704,8 @@ zfs_ioc_pool_scan(zfs_cmd_t *zc)
static const zfs_ioc_key_t zfs_keys_pool_scrub[] = {
{"scan_type", DATA_TYPE_UINT64, 0},
{"scan_command", DATA_TYPE_UINT64, 0},
{"scan_date_start", DATA_TYPE_UINT64, ZK_OPTIONAL},
{"scan_date_end", DATA_TYPE_UINT64, ZK_OPTIONAL},
};
static int
@@ -1712,6 +1714,7 @@ zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
spa_t *spa;
int error;
uint64_t scan_type, scan_cmd;
uint64_t date_start, date_end;
if (nvlist_lookup_uint64(innvl, "scan_type", &scan_type) != 0)
return (SET_ERROR(EINVAL));
@@ -1721,6 +1724,11 @@ zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
if (scan_cmd >= POOL_SCRUB_FLAGS_END)
return (SET_ERROR(EINVAL));
if (nvlist_lookup_uint64(innvl, "scan_date_start", &date_start) != 0)
date_start = 0;
if (nvlist_lookup_uint64(innvl, "scan_date_end", &date_end) != 0)
date_end = 0;
if ((error = spa_open(poolname, &spa, FTAG)) != 0)
return (error);
@@ -1732,7 +1740,24 @@ zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
error = spa_scan_range(spa, scan_type,
spa_get_last_scrubbed_txg(spa), 0);
} else {
error = spa_scan(spa, scan_type);
uint64_t txg_start, txg_end;
txg_start = txg_end = 0;
if (date_start != 0 || date_end != 0) {
mutex_enter(&spa->spa_txg_log_time_lock);
if (date_start != 0) {
txg_start = dbrrd_query(&spa->spa_txg_log_time,
date_start, DBRRD_FLOOR);
}
if (date_end != 0) {
txg_end = dbrrd_query(&spa->spa_txg_log_time,
date_end, DBRRD_CEILING);
}
mutex_exit(&spa->spa_txg_log_time_lock);
}
error = spa_scan_range(spa, scan_type, txg_start, txg_end);
}
spa_close(spa, FTAG);