Skip to content

Commit 4dde5c3

Browse files
committed
Add last_scrubbed_txg property and option to scrub from last saved txg
The `last_scrubbed_txg` property indicates the transaction group (TXG) up to which the most recent scrub operation has checked and repaired the dataset. This provides administrators with insight into the data integrity status of their pool at a specific point in time. Sponsored-By: Wasabi Technology, Inc. Sponsored-By: Klara Inc. Signed-off-by: Mariusz Zaborski <[email protected]>
1 parent 065e76e commit 4dde5c3

File tree

16 files changed

+188
-8
lines changed

16 files changed

+188
-8
lines changed

cmd/zpool/zpool_main.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ get_usage(zpool_help_t idx)
506506
return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> "
507507
"[<device> ...]\n"));
508508
case HELP_SCRUB:
509-
return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n"));
509+
return (gettext("\tscrub [-s | -p] [-w] [-e] [-C] "
510+
"<pool> ...\n"));
510511
case HELP_RESILVER:
511512
return (gettext("\tresilver <pool> ...\n"));
512513
case HELP_TRIM:
@@ -8391,8 +8392,9 @@ wait_callback(zpool_handle_t *zhp, void *data)
83918392
}
83928393

83938394
/*
8394-
* zpool scrub [-s | -p] [-w] [-e] <pool> ...
8395+
* zpool scrub [-s | -p] [-w] [-e] [-C] <pool> ...
83958396
*
8397+
* -C Scrub from last saved txg.
83968398
* -e Only scrub blocks in the error log.
83978399
* -s Stop. Stops any in-progress scrub.
83988400
* -p Pause. Pause in-progress scrub.
@@ -8412,10 +8414,14 @@ zpool_do_scrub(int argc, char **argv)
84128414
boolean_t is_error_scrub = B_FALSE;
84138415
boolean_t is_pause = B_FALSE;
84148416
boolean_t is_stop = B_FALSE;
8417+
boolean_t is_txg_continue = B_FALSE;
84158418

84168419
/* check options */
8417-
while ((c = getopt(argc, argv, "spwe")) != -1) {
8420+
while ((c = getopt(argc, argv, "spweC")) != -1) {
84188421
switch (c) {
8422+
case 'C':
8423+
is_txg_continue = B_TRUE;
8424+
break;
84198425
case 'e':
84208426
is_error_scrub = B_TRUE;
84218427
break;
@@ -8439,6 +8445,18 @@ zpool_do_scrub(int argc, char **argv)
84398445
(void) fprintf(stderr, gettext("invalid option "
84408446
"combination :-s and -p are mutually exclusive\n"));
84418447
usage(B_FALSE);
8448+
} else if (is_pause && is_txg_continue) {
8449+
(void) fprintf(stderr, gettext("invalid option "
8450+
"combination :-p and -C are mutually exclusive\n"));
8451+
usage(B_FALSE);
8452+
} else if (is_stop && is_txg_continue) {
8453+
(void) fprintf(stderr, gettext("invalid option "
8454+
"combination :-s and -C are mutually exclusive\n"));
8455+
usage(B_FALSE);
8456+
} else if (is_error_scrub && is_txg_continue) {
8457+
(void) fprintf(stderr, gettext("invalid option "
8458+
"combination :-e and -C are mutually exclusive\n"));
8459+
usage(B_FALSE);
84428460
} else {
84438461
if (is_error_scrub)
84448462
cb.cb_type = POOL_SCAN_ERRORSCRUB;
@@ -8447,6 +8465,8 @@ zpool_do_scrub(int argc, char **argv)
84478465
cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
84488466
} else if (is_stop) {
84498467
cb.cb_type = POOL_SCAN_NONE;
8468+
} else if (is_txg_continue) {
8469+
cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG;
84508470
} else {
84518471
cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
84528472
}

include/sys/dmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ typedef struct dmu_buf {
381381
#define DMU_POOL_CREATION_VERSION "creation_version"
382382
#define DMU_POOL_SCAN "scan"
383383
#define DMU_POOL_ERRORSCRUB "error_scrub"
384+
#define DMU_POOL_LAST_SCRUBBED_TXG "last_scrubbed_txg"
384385
#define DMU_POOL_FREE_BPOBJ "free_bpobj"
385386
#define DMU_POOL_BPTREE_OBJ "bptree_obj"
386387
#define DMU_POOL_EMPTY_BPOBJ "empty_bpobj"

include/sys/fs/zfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ typedef enum {
261261
ZPOOL_PROP_DEDUP_TABLE_SIZE,
262262
ZPOOL_PROP_DEDUP_TABLE_QUOTA,
263263
ZPOOL_PROP_DEDUPCACHED,
264+
ZPOOL_PROP_LAST_SCRUBBED_TXG,
264265
ZPOOL_NUM_PROPS
265266
} zpool_prop_t;
266267

@@ -1075,6 +1076,7 @@ typedef enum pool_scan_func {
10751076
typedef enum pool_scrub_cmd {
10761077
POOL_SCRUB_NORMAL = 0,
10771078
POOL_SCRUB_PAUSE,
1079+
POOL_SCRUB_FROM_LAST_TXG,
10781080
POOL_SCRUB_FLAGS_END
10791081
} pool_scrub_cmd_t;
10801082

include/sys/spa.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ extern uint64_t spa_get_deadman_failmode(spa_t *spa);
10691069
extern void spa_set_deadman_failmode(spa_t *spa, const char *failmode);
10701070
extern boolean_t spa_suspended(spa_t *spa);
10711071
extern uint64_t spa_bootfs(spa_t *spa);
1072+
extern uint64_t spa_get_last_scrubbed_txg(spa_t *spa);
10721073
extern uint64_t spa_delegation(spa_t *spa);
10731074
extern objset_t *spa_meta_objset(spa_t *spa);
10741075
extern space_map_t *spa_syncing_log_sm(spa_t *spa);

include/sys/spa_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ struct spa {
318318
uint64_t spa_scan_pass_scrub_spent_paused; /* total paused */
319319
uint64_t spa_scan_pass_exam; /* examined bytes per pass */
320320
uint64_t spa_scan_pass_issued; /* issued bytes per pass */
321+
uint64_t spa_scrubbed_last_txg; /* last txg scrubbed */
321322

322323
/* error scrub pause time in milliseconds */
323324
uint64_t spa_scan_pass_errorscrub_pause;

lib/libzfs/libzfs.abi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,8 @@
29552955
<enumerator name='ZPOOL_PROP_DEDUP_TABLE_SIZE' value='36'/>
29562956
<enumerator name='ZPOOL_PROP_DEDUP_TABLE_QUOTA' value='37'/>
29572957
<enumerator name='ZPOOL_PROP_DEDUPCACHED' value='38'/>
2958-
<enumerator name='ZPOOL_NUM_PROPS' value='39'/>
2958+
<enumerator name='ZPOOL_PROP_LAST_SCRUBBED_TXG' value='39'/>
2959+
<enumerator name='ZPOOL_NUM_PROPS' value='40'/>
29592960
</enum-decl>
29602961
<typedef-decl name='zpool_prop_t' type-id='af1ba157' id='5d0c23fb'/>
29612962
<typedef-decl name='regoff_t' type-id='95e97e5e' id='54a2a2a8'/>
@@ -5807,7 +5808,8 @@
58075808
<underlying-type type-id='9cac1fee'/>
58085809
<enumerator name='POOL_SCRUB_NORMAL' value='0'/>
58095810
<enumerator name='POOL_SCRUB_PAUSE' value='1'/>
5810-
<enumerator name='POOL_SCRUB_FLAGS_END' value='2'/>
5811+
<enumerator name='POOL_SCRUB_FROM_LAST_TXG' value='2'/>
5812+
<enumerator name='POOL_SCRUB_FLAGS_END' value='3'/>
58115813
</enum-decl>
58125814
<typedef-decl name='pool_scrub_cmd_t' type-id='a1474cbd' id='b51cf3c2'/>
58135815
<enum-decl name='zpool_errata' id='d9abbf54'>

lib/libzfs/libzfs_pool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
378378
case ZPOOL_PROP_BCLONEUSED:
379379
case ZPOOL_PROP_DEDUP_TABLE_SIZE:
380380
case ZPOOL_PROP_DEDUPCACHED:
381+
case ZPOOL_PROP_LAST_SCRUBBED_TXG:
381382
if (literal)
382383
(void) snprintf(buf, len, "%llu",
383384
(u_longlong_t)intval);

man/man7/zpoolprops.7

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ A unique identifier for the pool.
135135
The current health of the pool.
136136
Health can be one of
137137
.Sy ONLINE , DEGRADED , FAULTED , OFFLINE, REMOVED , UNAVAIL .
138+
.It Sy last_scrubbed_txg
139+
Indicates the transaction group (TXG) up to which the most recent scrub
140+
operation has checked and repaired the dataset.
141+
This provides insight into the data integrity status of their pool at
142+
a specific point in time.
143+
The
144+
.Xr zpool-scrub 8
145+
might be used to utilize this property.
138146
.It Sy leaked
139147
Space not released while
140148
.Sy freeing

man/man8/zpool-scrub.8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
.Op Fl s Ns | Ns Fl p
4040
.Op Fl w
4141
.Op Fl e
42+
.Op Fl C
4243
.Ar pool Ns
4344
.
4445
.Sh DESCRIPTION
@@ -114,6 +115,10 @@ The pool must have been scrubbed at least once with the
114115
feature enabled to use this option.
115116
Error scrubbing cannot be run simultaneously with regular scrubbing or
116117
resilvering, nor can it be run when a regular scrub is paused.
118+
.It Fl C
119+
Continue scrub from last saved txg (see zpool
120+
.Sy last_scrubbed_txg
121+
property).
117122
.El
118123
.Sh EXAMPLES
119124
.Ss Example 1

module/zcommon/zpool_prop.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ zpool_prop_init(void)
129129
0, PROP_READONLY, ZFS_TYPE_POOL, "<size>", "DDTSIZE", B_FALSE,
130130
sfeatures);
131131

132+
zprop_register_number(ZPOOL_PROP_LAST_SCRUBBED_TXG,
133+
"last_scrubbed_txg", 0, PROP_READONLY, ZFS_TYPE_POOL, "<txg>",
134+
"LAST_SCRUBBED_TXG", B_FALSE, sfeatures);
135+
132136
/* default number properties */
133137
zprop_register_number(ZPOOL_PROP_VERSION, "version", SPA_VERSION,
134138
PROP_DEFAULT, ZFS_TYPE_POOL, "<version>", "VERSION", B_FALSE,

module/zfs/dsl_scan.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ static int zfs_resilver_disable_defer = B_FALSE;
228228
((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
229229
(scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
230230

231+
#define DSL_SCAN_IS_SCRUB(scn) \
232+
((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB)
233+
231234
/*
232235
* Enable/disable the processing of the free_bpobj object.
233236
*/
@@ -1133,15 +1136,24 @@ dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
11331136

11341137
spa_notify_waiters(spa);
11351138

1136-
if (dsl_scan_restarting(scn, tx))
1139+
if (dsl_scan_restarting(scn, tx)) {
11371140
spa_history_log_internal(spa, "scan aborted, restarting", tx,
11381141
"errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa));
1139-
else if (!complete)
1142+
} else if (!complete) {
11401143
spa_history_log_internal(spa, "scan cancelled", tx,
11411144
"errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa));
1142-
else
1145+
} else {
11431146
spa_history_log_internal(spa, "scan done", tx,
11441147
"errors=%llu", (u_longlong_t)spa_approx_errlog_size(spa));
1148+
if (DSL_SCAN_IS_SCRUB(scn)) {
1149+
VERIFY0(zap_update(dp->dp_meta_objset,
1150+
DMU_POOL_DIRECTORY_OBJECT,
1151+
DMU_POOL_LAST_SCRUBBED_TXG,
1152+
sizeof (uint64_t), 1,
1153+
&scn->scn_phys.scn_max_txg, tx));
1154+
spa->spa_scrubbed_last_txg = scn->scn_phys.scn_max_txg;
1155+
}
1156+
}
11451157

11461158
if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
11471159
spa->spa_scrub_active = B_FALSE;

module/zfs/spa.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
458458
spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUP_TABLE_SIZE, NULL,
459459
ddt_get_ddt_dsize(spa), src);
460460

461+
spa_prop_add_list(*nvp, ZPOOL_PROP_LAST_SCRUBBED_TXG, NULL,
462+
spa_get_last_scrubbed_txg(spa), src);
463+
461464
spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
462465
rvd->vdev_state, src);
463466

@@ -4737,6 +4740,12 @@ spa_ld_get_props(spa_t *spa)
47374740
if (error != 0 && error != ENOENT)
47384741
return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
47394742

4743+
/* Load the last scrubbed txg. */
4744+
error = spa_dir_prop(spa, DMU_POOL_LAST_SCRUBBED_TXG,
4745+
&spa->spa_scrubbed_last_txg, B_FALSE);
4746+
if (error != 0 && error != ENOENT)
4747+
return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4748+
47404749
/*
47414750
* Load the livelist deletion field. If a livelist is queued for
47424751
* deletion, indicate that in the spa

module/zfs/spa_misc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,12 @@ spa_mode(spa_t *spa)
26792679
return (spa->spa_mode);
26802680
}
26812681

2682+
uint64_t
2683+
spa_get_last_scrubbed_txg(spa_t *spa)
2684+
{
2685+
return (spa->spa_scrubbed_last_txg);
2686+
}
2687+
26822688
uint64_t
26832689
spa_bootfs(spa_t *spa)
26842690
{

module/zfs/zfs_ioctl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,9 @@ zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
17191719
error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
17201720
} else if (scan_type == POOL_SCAN_NONE) {
17211721
error = spa_scan_stop(spa);
1722+
} else if (scan_cmd == POOL_SCRUB_FROM_LAST_TXG) {
1723+
error = spa_scan_range(spa, scan_type,
1724+
spa_get_last_scrubbed_txg(spa), 0);
17221725
} else {
17231726
error = spa_scan(spa, scan_type);
17241727
}

tests/zfs-tests/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
12181218
functional/cli_root/zpool_scrub/zpool_scrub_multiple_copies.ksh \
12191219
functional/cli_root/zpool_scrub/zpool_scrub_offline_device.ksh \
12201220
functional/cli_root/zpool_scrub/zpool_scrub_print_repairing.ksh \
1221+
functional/cli_root/zpool_scrub/zpool_scrub_txg_continue_from_last.ksh \
12211222
functional/cli_root/zpool_scrub/zpool_error_scrub_001_pos.ksh \
12221223
functional/cli_root/zpool_scrub/zpool_error_scrub_002_pos.ksh \
12231224
functional/cli_root/zpool_scrub/zpool_error_scrub_003_pos.ksh \
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/ksh -p
2+
#
3+
# CDDL HEADER START
4+
#
5+
# The contents of this file are subject to the terms of the
6+
# Common Development and Distribution License (the "License").
7+
# You may not use this file except in compliance with the License.
8+
#
9+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
# or https://opensource.org/licenses/CDDL-1.0.
11+
# See the License for the specific language governing permissions
12+
# and limitations under the License.
13+
#
14+
# When distributing Covered Code, include this CDDL HEADER in each
15+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
# If applicable, add the following below this CDDL HEADER, with the
17+
# fields enclosed by brackets "[]" replaced with your own identifying
18+
# information: Portions Copyright [yyyy] [name of copyright owner]
19+
#
20+
# CDDL HEADER END
21+
#
22+
23+
# Copyright (c) 2023, Klara Inc.
24+
#
25+
# This software was developed by
26+
# Mariusz Zaborski <[email protected]>
27+
# under sponsorship from Wasabi Technology, Inc. and Klara Inc.
28+
29+
. $STF_SUITE/include/libtest.shlib
30+
. $STF_SUITE/tests/functional/cli_root/zpool_scrub/zpool_scrub.cfg
31+
. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.kshlib
32+
33+
#
34+
# DESCRIPTION:
35+
# Verify scrub -C
36+
#
37+
# STRATEGY:
38+
# 1. Create a pool and create one file.
39+
# 2. Verify that the last_txg_scrub is 0.
40+
# 3. Run scrub.
41+
# 4. Verify that the last_txg_scrub is set.
42+
# 5. Create second file.
43+
# 6. Invalidate both files.
44+
# 7. Run scrub only from last point.
45+
# 8. Verify that only one file, that was created with newer txg,
46+
# was detected.
47+
#
48+
49+
verify_runnable "global"
50+
51+
function cleanup
52+
{
53+
log_must zinject -c all
54+
log_must rm -f $mntpnt/f1
55+
log_must rm -f $mntpnt/f2
56+
}
57+
58+
log_onexit cleanup
59+
60+
log_assert "Verify scrub -C."
61+
62+
# Create one file.
63+
mntpnt=$(get_prop mountpoint $TESTPOOL/$TESTFS)
64+
65+
log_must file_write -b 1048576 -c 10 -o create -d 0 -f $mntpnt/f1
66+
log_must sync_pool $TESTPOOL true
67+
f1txg=$(get_last_txg_synced $TESTPOOL)
68+
69+
# Verify that last_scrubbed_txg isn't set.
70+
zpoollasttxg=$(zpool get -H -o value last_scrubbed_txg $TESTPOOL)
71+
log_must [ $zpoollasttxg -eq 0 ]
72+
73+
# Run scrub.
74+
log_must zpool scrub -w $TESTPOOL
75+
76+
# Verify that last_scrubbed_txg is set.
77+
zpoollasttxg=$(zpool get -H -o value last_scrubbed_txg $TESTPOOL)
78+
log_must [ $zpoollasttxg -ne 0 ]
79+
80+
# Create second file.
81+
log_must file_write -b 1048576 -c 10 -o create -d 0 -f $mntpnt/f2
82+
log_must sync_pool $TESTPOOL true
83+
f2txg=$(get_last_txg_synced $TESTPOOL)
84+
85+
# Make sure that the sync txg are different.
86+
log_must [ $f1txg -ne $f2txg ]
87+
88+
# Insert faults.
89+
log_must zinject -a -t data -e io -T read $mntpnt/f1
90+
log_must zinject -a -t data -e io -T read $mntpnt/f2
91+
92+
# Run scrub from last saved point.
93+
log_must zpool scrub -w -C $TESTPOOL
94+
95+
# Verify that only newer file was detected.
96+
log_mustnot eval "zpool status -v $TESTPOOL | grep '$mntpnt/f1'"
97+
log_must eval "zpool status -v $TESTPOOL | grep '$mntpnt/f2'"
98+
99+
# Verify that both files are corrupted.
100+
log_must zpool scrub -w $TESTPOOL
101+
log_must eval "zpool status -v $TESTPOOL | grep '$mntpnt/f1'"
102+
log_must eval "zpool status -v $TESTPOOL | grep '$mntpnt/f2'"
103+
104+
log_pass "Verified scrub -C show expected status."

0 commit comments

Comments
 (0)