zinject: count matches and injections atomically
The injection hooks run concurrently for zios on different vdevs and
taskq threads, and hold inject_lock only as a reader, so the plain
increments of zi_match_count and zi_inject_count added in #16938 can
lose updates. zinject_counts compares the two at 100% frequency, and
has been seen failing with match=98 inject=97.
Update the counts with atomic_inc_64() wherever they are bumped under
the reader lock. The delay hook keeps its plain increments: it bumps
them under inject_delay_mtx, and no other hook counts for a delay
handler.
Atomic counts are still not enough for that comparison: the listing
could copy a record while a hook is between counting a match and
counting its injection, and report one more match than injections.
Copy the record with inject_lock held as a writer instead, so that no
hook is half way through. This also keeps the 64-bit counts from being
read torn on 32-bit platforms. Only the zinject command lists handlers,
[5 lines not shown]
vdev_initialize: resume a suspended initialize at a sector boundary
Initializing records its offset when it issues a write, even if the
write later fails. So an initialize that ran before this series with a
chunk size below the sector size could record a last offset in the
middle of a sector: where writes to a 4Kn disk failed, the offset
still advanced and initializing stayed active. After an upgrade,
resuming from that offset would issue misaligned writes for the rest
of that range. Round the saved offset down to the top-level sector
size when it is loaded.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: Matthew Ahrens <matt at mahrens.org>
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Closes #20036
vdev_initialize: align chunk size to the top-level ashift
Initialize ranges are aligned to the top-level vdev's ashift, but the
chunk size was rounded to the leaf's vdev_ashift. A leaf's vdev_ashift
is its logical sector size, which can be smaller than the top-level
ashift, for example for 512e disks in an ashift=12 mirror. A chunk size
such as 512 or 6144 was then used as is, so initializing issued writes
smaller than a block, or starting off a block boundary, which such
disks can only do with a read-modify-write.
Round the chunk size to the top-level ashift instead. It is never
smaller than any child's logical sector size, so writes to disks with
4096-byte sectors stay sector-aligned. Update the tunable's description
in zfs(4) to match.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: Matthew Ahrens <matt at mahrens.org>
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Closes #20036
ZTS: initialize a leaf whose ashift is below the top-level ashift
A leaf attached to a mirror keeps the ashift of its own disk, which can
be smaller than that of its top-level vdev. Attach a disk to a pool with
ashift=12 and initialize the new leaf with a 512-byte chunk size, and
check that no write to it is smaller than 4096 bytes. The test fails
until the following commit aligns the chunk size to the top-level
ashift.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: Matthew Ahrens <matt at mahrens.org>
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Closes #20036
ZTS: address review feedback on zpool_initialize_chunk_size
Rework the test as requested in review of #19096:
- Use the first disk in $DISKS instead of a file vdev.
- Save and restore the tunable with save_tunable/restore_tunable.
- Clear injected faults unconditionally in cleanup, and inject the
write delay with zinject -T write directly.
- Check that initializing started the way the other initialize tests
do, and replace the wait helpers with wait_for_write, which waits up
to 30 seconds for the initialized byte count to grow. Around each
change to the tunable, wait for a write before the change, so that
the running thread has sampled the tunable, and for the first write
sized after it.
- Add an in-range, sector-unaligned chunk size, so that rounding down
to the sector size is exercised.
- Check that initializing wrote only whole sectors, so that a missing
clamp or round-down fails the test even where the disk accepts
partial-sector writes.
- Resume with a chunk size that differs from the suspended thread's.
[12 lines not shown]
vdev_initialize: bound and snapshot chunk size
The initialisation buffer allocation and write loop read
zfs_initialize_chunk_size independently. A concurrent tunable change can
make the write size inconsistent with the allocated buffer.
Snapshot and normalise the value once per device initialisation thread.
Clamp it to the configured vdev sector size and maximum block size.
Derive every write split from the allocated ABD size. This makes the
allocation the single source of truth.
Document the effective bounds and when tunable changes take effect. Add
regression coverage for bounds, unaligned values, tunable changes, and
suspend/resume.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Matthew Ahrens <matt at mahrens.org>
Signed-off-by: Matthias Goergens <matthias.goergens at gmail.com>
Closes #20036
ZTS: initialize a 4Kn disk with a sub-sector chunk size
The zpool_initialize and zpool_wait tests set zfs_initialize_chunk_size
to 512 bytes but run on 512-byte-sector loop devices in CI, so they
never issue initialize writes smaller than a disk sector. On disks
with 4096-byte logical sectors, those writes trip the alignment
assertion in vdev_disk_check_alignment() on debug builds. A chunk size
of 1536 bytes or more that is not a multiple of the sector size also
panics release builds: its scatter ABD fails the same check after
vdev_disk_io_rw() copies it into a new one, and the VERIFY there fails.
Add a Linux test that initializes a scsi_debug 4Kn disk with a
512-byte chunk size and checks that it completes without errors. It
reads the per-vdev initializing state and error count from
"zpool status -j" with a new initialize_stat helper in
zpool_initialize.kshlib. The test fails until the following commits
bound and align the chunk size.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
[4 lines not shown]
Linux: handle const mnt_idmap in VFS callbacks
In our generated VFS callback trampolines, we need to correctly name the
time (const or not) in the prototype, or we'll fail to compile. So, when
we detect mnt_idmap in use, also check if its the const version, and if
so, include a const qualifier in our generated functions.
We also need to check for const mnt_idmap in some callback prototype
checks, otherwise those checks will fail to compile, which we'll
interpret as a missing or older prototype, and we'll end up generating
the wrong thing there too. Ideally, we would propagate the result of the
above configure check into subsequents checks, but this is awkward to do
with concurrent checks, and there's only a handful of them so we just
repeate them.
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
Linux: pass zidmap_t around as const
Linux 7.4 makes the struct mnt_idmap * const throughout the VFS, as
there is never a reason to modify it. Since we don't ever modify the
zidmap_t we derive from mnt_idmap or user_namespace, it's worth also
making those pointers const throughout the ZPL. There's no change for
release kernels, and it sidesteps the need for a bunch of casting for
7.4.
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
Linux 7.4: PageWriteback() no longer exists.
This is the macro for test if a page is currently in writeback. The
folio version yields the same result, so we wrap it.
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
Linux: include umh.h directly for usermode helper
7.4 slimmed down some include lists, and we are no longer getting it
through some transitive header. Include it directly.
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
Linux 7.4: PG_private no longer exists
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
Linux/ARC: look for the SPL reclaim thread, not kswapd
That the SPL reclaim thread is backed by kswapd is an implementation
detail. The ARC reclaim path should always have been asking the SPL via
current_is_reclaim_thread().
Sponsored-by: TrueNAS
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Rob Norris <rob.norris at truenas.com>
Closes #19507
brt: forget the vdev array when freeing it
brt_vdevs_free() frees spa_brt_vdevs but leaves it and spa_brt_nvdevs
set. When spa_load() fails after brt_load() -- for example in
spa_load_verify() -- the pool is unloaded and the load is retried on
the same spa_t. If the pool has deduplicated blocks, ddt_load() reset
the dedup space estimate, so the retry's vdev_load() recomputes the
pool's space through spa_get_slop_space() -> spa_update_dspace() ->
brt_get_dspace() before brt_load() runs again. brt_get_dspace() then
walks the freed array and the freed brt_vdev_t structures, and the
import oopses.
Clear both fields after freeing the array, as brt_alloc() does.
The test checks the retry itself by counting the dbgmsg lines of the
failed verifications and the retries (suggested by @tigerblue77 in
review), and that the rewind target lies between the checkpointed
state and the last txg before the damage. It then checks the file
contents, the block cloning and dedup accounting and the checkpoint.
[5 lines not shown]
ZTS: zeroing part of an unfilled readahead page leaves no stale bytes
On Linux before 6.14, read_pages() leaves the pages that ->readahead()
did not take in the page cache, unlocked and not up to date.
zfs_zero_partial_page() zeroes part of a cached page when a write
extends the file past its last page, and when a hole is punched inside
a page. If it then marks such a page up to date, read(2) and mmap
return the rest of the page, which was never read.
Add a test for it. With 128K records, a file of 8292 bytes is a single
block of 8704 bytes. The pool is exported and imported to drop the
file's pages, the first page is touched through a MADV_RANDOM mapping,
so that zpl_fadvise() goes on to generic_fadvise(), and
posix_fadvise(WILLNEED) is called over the whole file. Without
MADV_RANDOM, a nonzero readahead window makes the fault read around the
page and set the async part of the window, and read_pages() then removes
the pages ->readahead() did not take instead of keeping them. A
one-byte write at 20000, or a hole punched at [8200, 8250), then zeroes
part of the last page. The test fails if pread() of that page, or of
[17 lines not shown]
ZTS: fadvise(WILLNEED) readahead keeps up with writes to a mapped file
Four writers rewrite a file while four readers drop a span of it from
the page cache, fault one page of the span back in through a MADV_RANDOM
mapping, so that zpl_fadvise() goes on to generic_fadvise(), and ask for
the span with fadvise(WILLNEED). A readahead that waits for a writer's
range lock with the rest of its batch locked, while that writer waits in
update_pages() for a page of the batch, hangs both, and the pool stops
syncing behind them (#19447).
The test fails when neither side has moved for ten seconds, or when the
helper is still running a minute after it should have finished. On a
module that hangs here the pool stays hung after the test has failed.
Reviewed-by: Tigerblue77 <37409593+tigerblue77 at users.noreply.github.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: ElXreno <elxreno at gmail.com>
Closes #19447
Closes #19651
ZTS: sub-page write racing fadvise(WILLNEED) leaves no stale bytes
When readahead gives up on a page because a writer holds its range
lock, the page stays in the page cache, unlocked and not up to date.
If update_pages() then copies in only the bytes the write covered and
marks the page up to date, the rest of the page is stale for mmap and
read(2).
Add a test for it. The file's data is kept out of the ARC and reads
are delayed with zinject, so a 512-byte write inside the first page of
a record holds the range lock while it waits for that record.
posix_fadvise(WILLNEED) over the record and a cached page beyond it
then reaches the written page while the writer holds the lock. Once
the delay is cleared, the page is compared, through a shared mapping
and through pread(), with a copy of the file kept outside the pool,
and the test fails on any difference. Freed pages of a file of 0xA5
bytes make stale bytes non-zero where new pages are not zeroed, and
zeroes differ from the random file data too.
[12 lines not shown]
Linux: fill a readahead batch from one hold of its dbufs
zpl_readahead() filled a batch one page at a time through zfs_getpage(),
so every 4K page entered the filesystem, took and dropped the range
lock, held and released the dbufs behind it and told the prefetcher
about a one page read. With recordsize=128K that is 32 rounds of all of
it per block, and the readers of one file queue on the mutex of its
range lock.
It could also hang the pool. The kernel hands ->readahead() the whole
batch locked, and zfs_getpage() unlocks only its own page before it
waits for the range lock. zfs_write() holds the range lock while
update_pages() locks the pages of the range it wrote, so a writer
waiting for a page further down the batch and a readahead waiting for
the writer's range never move again (#19447). posix_fadvise(WILLNEED)
reaches it with the readahead window at zero, through bdi->io_pages.
Fill the batch in zfs_getpages() instead: enter the filesystem once, try
the range lock over the whole batch, hold the dbufs behind it with
[18 lines not shown]
Linux: leave unfilled pages to their filler in update_pages()
update_pages() copies only the bytes a write covered into each cached
page and then marks the page up to date. For a page that was never
filled, the rest of the page is whatever the allocator left there, and
it becomes visible to mmap and to mappedread().
Such pages exist. zfs_getpage() unlocks its page while it waits for
the range lock, so a writer holding that lock can reach the page before
it is filled, and other readers can see it after the writer is done and
before zfs_getpage() relocks it. Readahead that stops early also
leaves its unconsumed pages cached and not up to date on kernels before
6.14, whose read_pages() removes only the async part of the window.
Skip a page that is neither up to date nor dirty. Every filler
(zfs_getpage(), mappedread(), readahead) takes the range lock, which
our callers hold as writer, so the filler reads this write from the
DMU once it is done. This is also needed before readahead may stop
early on range lock contention.
[12 lines not shown]
zdb: report a set-aside livelist block once, with its own message
livelist_metaslab_validate() sets aside a livelist block that a FREE
entry of the metaslab's own space map with the block's birth txg
covers, and reports it when the metaslab is done unless an ALLOC entry
places it again. The block stays in mv_livelist_allocs meanwhile, so a
later FREE entry that covers it reports it too, and it is then reported
a second time when the metaslab is done: without log space maps, when
its space is allocated again and freed in later txgs; with them, when
the log space maps not yet flushed hold such an allocation and its
free. Remove the block from mv_freed_at_birth when a FREE entry reports
it.
What is left when the metaslab is done was printed as "FREED at TXG"
with the block's birth txg. With log space maps that is not the txg of
the free: the FREE entry seen is the one a flush wrote, and the block
is still set aside because its allocation and its real free, in a later
txg, cancelled out before the next flush. Print what was seen instead:
the block was freed in its birth txg and not allocated again.
[11 lines not shown]
zdb: do not report a livelist block born in the txg of a flush
livelist_metaslab_validate() reports a livelist block as freed while
alive when a FREE entry of a space map covers it and the block was born
in that entry's txg or before. But a metaslab's own space map gets FREE
entries with a txg before the ALLOC entries of that txg:
spa_flush_metaslabs() runs before vdev_sync() and writes the frees of
earlier txgs with the txg of the flush, and metaslab_condense() writes
what the txg is allocating as free. A block that a clone allocates in
that txg, in a range freed earlier, is then reported: ztest failed on
"ERROR: Livelist ALLOC [f2f9800:1000] from TXG 808 FREED at TXG 808",
in a pool where the metaslab's unflushed txg was 0x808 and the block's
allocation was in the log space map of txg 0x808.
Such a block is placed again by a later ALLOC entry: without log space
maps, one of the same txg in the metaslab's own space map; with them,
one in the log space map of that txg or, once the metaslab has been
flushed again, one of its own space map with the txg of that flush. A
block freed in the txg of its birth is not, since freed space is not
[16 lines not shown]
Linux: wait for zvol_open() before freeing a removed zvol
zvol_open() reads the zvol_state_t from the gendisk's private_data and
then takes its zv_state_lock, with nothing holding the zvol in between.
Since 8a0e5e8b5 ("zvol: stop using zvol_state_lock to protect OS-side
private data"), zvol_os_remove_minor() relies on del_gendisk() to wait
for such a caller once private_data is cleared: on Linux 5.14 and newer
zvol_open() and zvol_release() run under the disk's open_mutex, which
del_gendisk() takes.
On Linux 5.10 and older they run under the whole disk's bd_mutex, and
del_gendisk() does not take it. A zvol_open() that read private_data
just before it was cleared can still be queued on zv_state_lock after
zvol_shutdown_minor() has taken and dropped that lock for the last time
and zvol_remove_minors_impl() has freed the zvol: a woken waiter does
not own the mutex yet, so the enter/exit pair at the end of
zvol_shutdown_minor() does not wait for it. A CI run on AlmaLinux 8
(4.18.0-553) hit this in zvol_misc_volmode_race: systemd-udevd's open
died in __mutex_lock() called from zvol_open() with "list_del
[17 lines not shown]
zed: show the search path when a command cannot be found
A mail program that works in an interactive shell can be missing from
the restricted PATH used by zedlets. Include the search path in the
lookup error and document how to configure a custom mail program.
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: zibuyu <zibuyu423 at gmail.com>
Closes #13752
Closes #20125
libzfs: keep --json-int property values exact
zprop_nvlist_one_property() prints a property value made only of digits
as a JSON integer for --json-int, and reads it with sscanf("%lld") into
a uint64_t. Four kinds of values come out wrong:
- A value above INT64_MAX does not fit in a long long, and glibc stores
INT64_MAX instead. About half of all guids come out as
9223372036854775807 in 'zpool get -j --json-int guid', and 'zpool
list', 'zfs get' and 'zfs list' do the same with --json-int.
- A run of digits longer than a uint64, which a user property can hold,
is clamped the same way instead of being kept.
- A value with a leading zero, "007" for one, which a user property or
the comment of a pool or vdev can hold, is printed as 7.
- An empty value passes the digits-only test, as strspn() and strlen()
are both 0. sscanf() then converts nothing and the uninitialized
uint64_t is printed. 'zfs set org.test:e= pool' stores such a user
property.
[13 lines not shown]
Fix out-of-bound regression at vdev_mirror_io_done()
This is a follow up fix after commit f0b335239 where the
regression was introduced, mm->mm_child[last_good_copy].mc_abd
referencing (line 930) without last_good_copy >= 0 guarding.
Solution: best_abd already has the needed value at this point,
which is also guarded, use it instead.
Also, drop duplicate 'redundancy_mirror' test entry from
tests/runfiles/common.run, another regression in the same commit.
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: Andriy Tkachuk <atkachuk at wasabi.com>
Co-authored-by: xfcyhuang <xfcyhuang at aliyun.com>
Closes #20139
tests: add ZTS tests for Linux quotactl_fd(2)
Add userquota_015_pos.ksh and userquota_015_neg.ksh to verify ZFS user,
group, and project quotas, space and inode limits, and default quotas
via quotactl_fd(2), plus rejection of unsupported operations and
read-only targets.
Use quota(1) and setquota(8) when quota-tools >= 4.10 is available
(patching tmpfs->zfs for 4.10-4.11). On quota-tools <= 4.09, verify
the expected error text and fall back to the quotactl_fd test helper
(tests/zfs-tests/cmd/quotactl_fd.c) on kernels >= 5.14.
Install the quota package in Linux CI VMs and register the tests in
tests/zfs-tests/tests/Makefile.am, tests/zfs-tests/cmd/Makefile.am,
and tests/runfiles/common.run.
Assisted-by: Antigravity
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
[2 lines not shown]
Linux: implement quotactl_ops for quota control
Implement struct quotactl_ops (sb->s_qcop) on Linux so standard VFS
quota calls such as quotactl_fd(2) query and modify ZFS user, group,
and project quotas.
This allows quota-aware applications like Samba to inspect user and
group quotas directly via standard kernel interfaces without spawning
external helpers or requiring specialized out-of-tree modules.
Assisted-by: Antigravity
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Tony Hutter <hutter2 at llnl.gov>
Signed-off-by: Ananda Bhattacharya <a3217055 at gmail.com>
Closes #19658
CI: fall back to the FreeBSD CI image for 16-CURRENT
The release engineering mirror can have no amd64 snapshot of CURRENT.
Fall back to the FreeBSD CI test VM and fetch its kernel source at
the same revision. Use its first-boot tar disk to set up networking
and SSH, and allow enough time for the larger download.
The test VM already mounts fdescfs and preloads the base ZFS module.
Reuse the existing mount and clear the test image module list for
subsequent boots so ZTS can load the module it built.
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Reviewed-by: Will Rouesnel <wrouesnel at wrouesnel.com>
Signed-off-by: zibuyu <zibuyu423 at gmail.com>
Closes #20122
zpool_detach_spare_throttle: check that the pool reaches its dirty limit
Fail the test if dmu_tx_dirty_over_max hasn't increased by the time the
detach starts, so that a pass means the detach really ran while the pool
was at its dirty data limit.
Reviewed by: George Wilson <gwilson at zfsmail.com>
Reviewed-by: Brian Behlendorf <behlendorf1 at llnl.gov>
Signed-off-by: Matthew Ahrens <matt at mahrens.org>
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
Closes #19871
Don't wait for a tx under the config lock to log history
`zpool detach` of the original device of an in-use hot spare, and
`zpool split`, write history records while they hold the config lock
as writer. Both get a new tx for the record, and when the pool is at
its dirty data limit (or suspended), dmu_tx_assign() waits for a txg
sync, which can't start without the config lock, so the pool hangs.
Log these records with a tx assigned to the txg that the config change
goes into, as device removal does for its sync tasks. log_internal()
now writes the record itself only in syncing context; in open context
it always queues a sync task, including for an assigned tx.
dmu_tx_assign() now also asserts that a caller that might wait doesn't
hold any spa config lock as writer, which catches this kind of bug
without needing a full dirty data limit or a suspended pool.
Attach and detach now log the same way, before they drop the config
lock, instead of copying the device paths to log them afterwards, so
[7 lines not shown]