Skip to content

Commit a7de203

Browse files
authored
txg: generalise txg_wait_synced_sig() to txg_wait_synced_flags() (#17284)
txg_wait_synced_sig() is "wait for txg, unless a signal arrives". We expect that future development will require similar "wait unless X" behaviour. This generalises the API as txg_wait_synced_flags(), where the provided flags describe the events that should cause the call to return. Instead of a boolean, the return is now an error code, which the caller can use to know which event caused the call to return. The existing call to txg_wait_synced_sig() is now txg_wait_synced_flags(TXG_WAIT_SIGNAL). Sponsored-by: Klara, Inc. Sponsored-by: Wasabi Technology, Inc. Signed-off-by: Rob Norris <robn@despairlabs.com> Reviewed-by: Allan Jude <allan@klarasystems.com> Reviewed-by: Alexander Motin <mav@FreeBSD.org>
1 parent f85c96e commit a7de203

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

include/sys/txg.h

+20-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ typedef struct txg_list {
6666
txg_node_t *tl_head[TXG_SIZE];
6767
} txg_list_t;
6868

69+
/*
70+
* Wait flags for txg_wait_synced_flags(). By default (TXG_WAIT_NONE), it will
71+
* wait until the wanted txg is reached, or block forever. Additional flags
72+
* indicate other conditions that the caller is interested in, that will cause
73+
* the wait to break and return an error code describing the condition.
74+
*/
75+
typedef enum {
76+
/* No special flags. Guaranteed to block forever or return 0 */
77+
TXG_WAIT_NONE = 0,
78+
79+
/* If a signal arrives while waiting, abort and return EINTR */
80+
TXG_WAIT_SIGNAL = (1 << 0),
81+
} txg_wait_flag_t;
82+
6983
struct dsl_pool;
7084

7185
extern void txg_init(struct dsl_pool *dp, uint64_t txg);
@@ -86,13 +100,16 @@ extern void txg_kick(struct dsl_pool *dp, uint64_t txg);
86100
* Try to make this happen as soon as possible (eg. kick off any
87101
* necessary syncs immediately). If txg==0, wait for the currently open
88102
* txg to finish syncing.
103+
* See txg_wait_flag_t above for a description of how the flags affect the wait.
89104
*/
90-
extern void txg_wait_synced(struct dsl_pool *dp, uint64_t txg);
105+
extern int txg_wait_synced_flags(struct dsl_pool *dp, uint64_t txg,
106+
txg_wait_flag_t flags);
91107

92108
/*
93-
* Wait as above. Returns true if the thread was signaled while waiting.
109+
* Traditional form of txg_wait_synced_flags, waits forever.
110+
* Shorthand for VERIFY0(txg_wait_synced_flags(dp, TXG_WAIT_NONE))
94111
*/
95-
extern boolean_t txg_wait_synced_sig(struct dsl_pool *dp, uint64_t txg);
112+
extern void txg_wait_synced(struct dsl_pool *dp, uint64_t txg);
96113

97114
/*
98115
* Wait until the given transaction group, or one after it, is

module/zfs/dsl_synctask.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,19 @@ dsl_sync_task_common(const char *pool, dsl_checkfunc_t *checkfunc,
8686

8787
dmu_tx_commit(tx);
8888

89-
if (sigfunc != NULL && txg_wait_synced_sig(dp, dst.dst_txg)) {
90-
/* current contract is to call func once */
91-
sigfunc(arg, tx);
92-
sigfunc = NULL; /* in case we're performing an EAGAIN retry */
93-
}
94-
txg_wait_synced(dp, dst.dst_txg);
89+
if (sigfunc != NULL) {
90+
err = txg_wait_synced_flags(dp, dst.dst_txg, TXG_WAIT_SIGNAL);
91+
if (err != 0) {
92+
VERIFY3U(err, ==, EINTR);
93+
/* current contract is to call func once */
94+
sigfunc(arg, tx);
95+
/* in case we're performing an EAGAIN retry */
96+
sigfunc = NULL;
97+
98+
txg_wait_synced(dp, dst.dst_txg);
99+
}
100+
} else
101+
txg_wait_synced(dp, dst.dst_txg);
95102

96103
if (dst.dst_error == EAGAIN) {
97104
txg_wait_synced(dp, dst.dst_txg + TXG_DEFER_SIZE);

module/zfs/txg.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,13 @@ txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
699699
mutex_exit(&tx->tx_sync_lock);
700700
}
701701

702-
static boolean_t
703-
txg_wait_synced_impl(dsl_pool_t *dp, uint64_t txg, boolean_t wait_sig)
702+
int
703+
txg_wait_synced_flags(dsl_pool_t *dp, uint64_t txg, txg_wait_flag_t flags)
704704
{
705+
int error = 0;
705706
tx_state_t *tx = &dp->dp_tx;
706707

708+
ASSERT0(flags & ~TXG_WAIT_SIGNAL);
707709
ASSERT(!dsl_pool_config_held(dp));
708710

709711
mutex_enter(&tx->tx_sync_lock);
@@ -715,45 +717,43 @@ txg_wait_synced_impl(dsl_pool_t *dp, uint64_t txg, boolean_t wait_sig)
715717
dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
716718
(u_longlong_t)txg, (u_longlong_t)tx->tx_quiesce_txg_waiting,
717719
(u_longlong_t)tx->tx_sync_txg_waiting);
720+
721+
/*
722+
* Keep pushing util the pool gets to the wanted txg. If something
723+
* else interesting happens, we'll set an error and break out.
724+
*/
718725
while (tx->tx_synced_txg < txg) {
719726
dprintf("broadcasting sync more "
720727
"tx_synced=%llu waiting=%llu dp=%px\n",
721728
(u_longlong_t)tx->tx_synced_txg,
722729
(u_longlong_t)tx->tx_sync_txg_waiting, dp);
723730
cv_broadcast(&tx->tx_sync_more_cv);
724-
if (wait_sig) {
731+
732+
if (flags & TXG_WAIT_SIGNAL) {
725733
/*
726734
* Condition wait here but stop if the thread receives a
727735
* signal. The caller may call txg_wait_synced*() again
728736
* to resume waiting for this txg.
729737
*/
730738
if (cv_wait_io_sig(&tx->tx_sync_done_cv,
731739
&tx->tx_sync_lock) == 0) {
732-
mutex_exit(&tx->tx_sync_lock);
733-
return (B_TRUE);
740+
error = SET_ERROR(EINTR);
741+
break;
734742
}
735743
} else {
744+
/* Uninterruptable wait, until the condvar fires */
736745
cv_wait_io(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
737746
}
738747
}
748+
739749
mutex_exit(&tx->tx_sync_lock);
740-
return (B_FALSE);
750+
return (error);
741751
}
742752

743753
void
744754
txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
745755
{
746-
VERIFY0(txg_wait_synced_impl(dp, txg, B_FALSE));
747-
}
748-
749-
/*
750-
* Similar to a txg_wait_synced but it can be interrupted from a signal.
751-
* Returns B_TRUE if the thread was signaled while waiting.
752-
*/
753-
boolean_t
754-
txg_wait_synced_sig(dsl_pool_t *dp, uint64_t txg)
755-
{
756-
return (txg_wait_synced_impl(dp, txg, B_TRUE));
756+
VERIFY0(txg_wait_synced_flags(dp, txg, TXG_WAIT_NONE));
757757
}
758758

759759
/*

module/zfs/zcp.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,8 @@ zcp_pool_error(zcp_run_info_t *ri, const char *poolname, int error)
970970
}
971971

972972
/*
973-
* This callback is called when txg_wait_synced_sig encountered a signal.
974-
* The txg_wait_synced_sig will continue to wait for the txg to complete
973+
* This callback is called when txg_wait_synced_flags encountered a signal.
974+
* The txg_wait_synced_flags will continue to wait for the txg to complete
975975
* after calling this callback.
976976
*/
977977
static void

0 commit comments

Comments
 (0)