Fix lseek(SEEK_DATA/SEEK_HOLE) mmap consistency

When using lseek(2) to report data/holes memory mapped regions of
the file were ignored.  This could result in incorrect results.
To handle this zfs_holey_common() was updated to asynchronously
writeback any dirty mmap(2) regions prior to reporting holes.

Additionally, while not strictly required, the dn_struct_rwlock is
now held over the dirty check to prevent the dnode structure from
changing.  This ensures that a clean dnode can't be dirtied before
the data/hole is located.  The range lock is now also taken to
ensure the call cannot race with zfs_write().

Furthermore, the code was refactored to provide a dnode_is_dirty()
helper function which checks the dnode for any dirty records to
determine its dirtiness.

Reviewed-by: Matthew Ahrens <mahrens@delphix.com>
Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Reviewed-by: Rich Ercolani <rincebrain@gmail.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #11900
Closes #12724
This commit is contained in:
Brian Behlendorf
2021-11-07 13:27:44 -08:00
committed by Tony Hutter
parent 5bf81fea2f
commit 664d487a5d
18 changed files with 305 additions and 32 deletions
+1 -1
View File
@@ -669,7 +669,7 @@ tests = ['migration_001_pos', 'migration_002_pos', 'migration_003_pos',
tags = ['functional', 'migration']
[tests/functional/mmap]
tests = ['mmap_write_001_pos', 'mmap_read_001_pos']
tests = ['mmap_write_001_pos', 'mmap_read_001_pos', 'mmap_seek_001_pos']
tags = ['functional', 'mmap']
[tests/functional/mount]
+1
View File
@@ -19,6 +19,7 @@ SUBDIRS = \
mktree \
mmap_exec \
mmap_libaio \
mmap_seek \
mmapwrite \
nvlist_to_lua \
randwritecomp \
+1
View File
@@ -0,0 +1 @@
/mmap_seek
@@ -0,0 +1,6 @@
include $(top_srcdir)/config/Rules.am
pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/bin
pkgexec_PROGRAMS = mmap_seek
mmap_seek_SOURCES = mmap_seek.c
+147
View File
@@ -0,0 +1,147 @@
/*
* 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 http://www.opensolaris.org/os/licensing.
* 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) 2021 by Lawrence Livermore National Security, LLC.
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
static void
seek_data(int fd, off_t offset, off_t expected)
{
off_t data_offset = lseek(fd, offset, SEEK_DATA);
if (data_offset != expected) {
fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
(int)offset, (int)data_offset, (int)expected);
exit(2);
}
}
static void
seek_hole(int fd, off_t offset, off_t expected)
{
off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
if (hole_offset != expected) {
fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
(int)offset, (int)hole_offset, (int)expected);
exit(2);
}
}
int
main(int argc, char **argv)
{
char *execname = argv[0];
char *file_path = argv[1];
char *buf = NULL;
int err;
if (argc != 4) {
(void) printf("usage: %s <file name> <file size> "
"<block size>\n", argv[0]);
exit(1);
}
int fd = open(file_path, O_RDWR | O_CREAT, 0666);
if (fd == -1) {
(void) fprintf(stderr, "%s: %s: ", execname, file_path);
perror("open");
exit(2);
}
off_t file_size = atoi(argv[2]);
off_t block_size = atoi(argv[3]);
if (block_size * 2 > file_size) {
(void) fprintf(stderr, "file size must be at least "
"double the block size\n");
exit(2);
}
err = ftruncate(fd, file_size);
if (err == -1) {
perror("ftruncate");
exit(2);
}
if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0)) == MAP_FAILED) {
perror("mmap");
exit(2);
}
/* Verify the file is sparse and reports no data. */
seek_data(fd, 0, -1);
/* Verify the file is reported as a hole. */
seek_hole(fd, 0, 0);
/* Verify search beyond end of file is an error. */
seek_data(fd, 2 * file_size, -1);
seek_hole(fd, 2 * file_size, -1);
/* Dirty the first byte. */
memset(buf, 'a', 1);
seek_data(fd, 0, 0);
seek_data(fd, block_size, -1);
seek_hole(fd, 0, block_size);
seek_hole(fd, block_size, block_size);
/* Dirty the first half of the file. */
memset(buf, 'b', file_size / 2);
seek_data(fd, 0, 0);
seek_data(fd, block_size, block_size);
seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size));
seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size));
/* Dirty the whole file. */
memset(buf, 'c', file_size);
seek_data(fd, 0, 0);
seek_data(fd, file_size * 3 / 4,
P2ROUNDUP(file_size * 3 / 4, block_size));
seek_hole(fd, 0, file_size);
seek_hole(fd, file_size / 2, file_size);
/* Punch a hole (required compression be enabled). */
memset(buf + block_size, 0, block_size);
seek_data(fd, 0, 0);
seek_data(fd, block_size, 2 * block_size);
seek_hole(fd, 0, block_size);
seek_hole(fd, block_size, block_size);
seek_hole(fd, 2 * block_size, file_size);
err = munmap(buf, file_size);
if (err == -1) {
perror("munmap");
exit(2);
}
close(fd);
return (0);
}
+1
View File
@@ -209,6 +209,7 @@ export ZFSTEST_FILES='badsend
mktree
mmap_exec
mmap_libaio
mmap_seek
mmapwrite
nvlist_to_lua
randfree_file
+1
View File
@@ -33,6 +33,7 @@ DEADMAN_FAILMODE deadman.failmode zfs_deadman_failmode
DEADMAN_SYNCTIME_MS deadman.synctime_ms zfs_deadman_synctime_ms
DEADMAN_ZIOTIME_MS deadman.ziotime_ms zfs_deadman_ziotime_ms
DISABLE_IVSET_GUID_CHECK disable_ivset_guid_check zfs_disable_ivset_guid_check
DMU_OFFSET_NEXT_SYNC dmu_offset_next_sync zfs_dmu_offset_next_sync
INITIALIZE_CHUNK_SIZE initialize_chunk_size zfs_initialize_chunk_size
INITIALIZE_VALUE initialize_value zfs_initialize_value
KEEP_LOG_SPACEMAPS_AT_EXPORT keep_log_spacemaps_at_export zfs_keep_log_spacemaps_at_export
@@ -4,7 +4,8 @@ dist_pkgdata_SCRIPTS = \
cleanup.ksh \
mmap_read_001_pos.ksh \
mmap_write_001_pos.ksh \
mmap_libaio_001_pos.ksh
mmap_libaio_001_pos.ksh \
mmap_seek_001_pos.ksh
dist_pkgdata_DATA = \
mmap.cfg
@@ -0,0 +1,67 @@
#!/bin/ksh -p
#
# 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 http://www.opensolaris.org/os/licensing.
# 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) 2021 by Lawrence Livermore National Security, LLC.
#
. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/mmap/mmap.cfg
#
# DESCRIPTION:
# lseek() data/holes for an mmap()'d file.
#
# STRATEGY:
# 1. Enable compression and hole reporting for dirty files.
# 2. Call mmap_seek binary test case for various record sizes.
#
verify_runnable "global"
function cleanup
{
log_must zfs set compression=off $TESTPOOL/$TESTFS
log_must zfs set recordsize=128k $TESTPOOL/$TESTFS
log_must rm -f $TESTDIR/test-mmap-file
log_must set_tunable64 DMU_OFFSET_NEXT_SYNC $dmu_offset_next_sync
}
log_assert "lseek() data/holes for an mmap()'d file."
log_onexit cleanup
# Enable hole reporting for dirty files.
typeset dmu_offset_next_sync=$(get_tunable DMU_OFFSET_NEXT_SYNC)
log_must set_tunable64 DMU_OFFSET_NEXT_SYNC 1
# Compression must be enabled to convert zero'd blocks to holes.
# This behavior is checked by the mmap_seek test.
log_must zfs set compression=on $TESTPOOL/$TESTFS
for bs in 4096 8192 16384 32768 65536 131072; do
log_must zfs set recordsize=$bs $TESTPOOL/$TESTFS
log_must mmap_seek $TESTDIR/test-mmap-file $((1024*1024)) $bs
log_must rm $TESTDIR/test-mmap-file
done
log_pass "lseek() data/holes for an mmap()'d file succeeded."