Gentoo Archives: gentoo-commits

From: Georgy Yakovlev <gyakovlev@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: sys-fs/zfs-kmod/, sys-fs/zfs-kmod/files/
Date: Sun, 07 Nov 2021 23:59:26
Message-Id: 1636329453.8e5626dc90e4e6166c2e296371b6ff5a9d13a8c4.gyakovlev@gentoo
1 commit: 8e5626dc90e4e6166c2e296371b6ff5a9d13a8c4
2 Author: Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
3 AuthorDate: Sun Nov 7 22:59:41 2021 +0000
4 Commit: Georgy Yakovlev <gyakovlev <AT> gentoo <DOT> org>
5 CommitDate: Sun Nov 7 23:57:33 2021 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=8e5626dc
7
8 sys-fs/zfs-kmod: revbump, add HOLE patch and dist-kernel version limit
9
10 https://github.com/openzfs/zfs/pull/12724
11 https://github.com/openzfs/zfs/issues/11900
12
13 Signed-off-by: Georgy Yakovlev <gyakovlev <AT> gentoo.org>
14
15 sys-fs/zfs-kmod/files/2.1.1-fix-lseek-mmap.patch | 594 +++++++++++++++++++++++
16 sys-fs/zfs-kmod/zfs-kmod-2.1.1-r1.ebuild | 207 ++++++++
17 2 files changed, 801 insertions(+)
18
19 diff --git a/sys-fs/zfs-kmod/files/2.1.1-fix-lseek-mmap.patch b/sys-fs/zfs-kmod/files/2.1.1-fix-lseek-mmap.patch
20 new file mode 100644
21 index 00000000000..2c3a66a97bf
22 --- /dev/null
23 +++ b/sys-fs/zfs-kmod/files/2.1.1-fix-lseek-mmap.patch
24 @@ -0,0 +1,594 @@
25 +From de198f2d9507b6dcf3d0d8f037ba33940208733e Mon Sep 17 00:00:00 2001
26 +From: Brian Behlendorf <behlendorf1@××××.gov>
27 +Date: Sun, 7 Nov 2021 13:27:44 -0800
28 +Subject: [PATCH] Fix lseek(SEEK_DATA/SEEK_HOLE) mmap consistency
29 +
30 +When using lseek(2) to report data/holes memory mapped regions of
31 +the file were ignored. This could result in incorrect results.
32 +To handle this zfs_holey_common() was updated to asynchronously
33 +writeback any dirty mmap(2) regions prior to reporting holes.
34 +
35 +Additionally, while not strictly required, the dn_struct_rwlock is
36 +now held over the dirty check to prevent the dnode structure from
37 +changing. This ensures that a clean dnode can't be dirtied before
38 +the data/hole is located. The range lock is now also taken to
39 +ensure the call cannot race with zfs_write().
40 +
41 +Furthermore, the code was refactored to provide a dnode_is_dirty()
42 +helper function which checks the dnode for any dirty records to
43 +determine its dirtiness.
44 +
45 +Reviewed-by: Matthew Ahrens <mahrens@×××××××.com>
46 +Reviewed-by: Tony Hutter <hutter2@××××.gov>
47 +Reviewed-by: Rich Ercolani <rincebrain@×××××.com>
48 +Signed-off-by: Brian Behlendorf <behlendorf1@××××.gov>
49 +Issue #11900
50 +Closes #12724
51 +---
52 + configure.ac | 1 +
53 + include/os/freebsd/spl/sys/vnode.h | 18 +++
54 + include/os/freebsd/zfs/sys/zfs_znode_impl.h | 3 +-
55 + include/os/linux/zfs/sys/zfs_znode_impl.h | 1 +
56 + include/sys/dnode.h | 1 +
57 + man/man4/zfs.4 | 2 +-
58 + module/zfs/dmu.c | 53 ++++---
59 + module/zfs/dnode.c | 20 +++
60 + module/zfs/zfs_vnops.c | 9 +-
61 + tests/runfiles/common.run | 2 +-
62 + tests/zfs-tests/cmd/Makefile.am | 1 +
63 + tests/zfs-tests/cmd/mmap_seek/.gitignore | 1 +
64 + tests/zfs-tests/cmd/mmap_seek/Makefile.am | 6 +
65 + tests/zfs-tests/cmd/mmap_seek/mmap_seek.c | 147 ++++++++++++++++++
66 + tests/zfs-tests/include/commands.cfg | 1 +
67 + tests/zfs-tests/include/tunables.cfg | 1 +
68 + .../tests/functional/mmap/Makefile.am | 3 +-
69 + .../functional/mmap/mmap_seek_001_pos.ksh | 67 ++++++++
70 + 18 files changed, 305 insertions(+), 32 deletions(-)
71 + create mode 100644 tests/zfs-tests/cmd/mmap_seek/.gitignore
72 + create mode 100644 tests/zfs-tests/cmd/mmap_seek/Makefile.am
73 + create mode 100644 tests/zfs-tests/cmd/mmap_seek/mmap_seek.c
74 + create mode 100755 tests/zfs-tests/tests/functional/mmap/mmap_seek_001_pos.ksh
75 +
76 +diff --git a/configure.ac b/configure.ac
77 +index 6f34b210d2b..ebc7b276a64 100644
78 +--- a/configure.ac
79 ++++ b/configure.ac
80 +@@ -221,6 +221,7 @@ AC_CONFIG_FILES([
81 + tests/zfs-tests/cmd/mktree/Makefile
82 + tests/zfs-tests/cmd/mmap_exec/Makefile
83 + tests/zfs-tests/cmd/mmap_libaio/Makefile
84 ++ tests/zfs-tests/cmd/mmap_seek/Makefile
85 + tests/zfs-tests/cmd/mmapwrite/Makefile
86 + tests/zfs-tests/cmd/nvlist_to_lua/Makefile
87 + tests/zfs-tests/cmd/randfree_file/Makefile
88 +diff --git a/include/os/freebsd/spl/sys/vnode.h b/include/os/freebsd/spl/sys/vnode.h
89 +index 3670712a045..3bc8a18eeb7 100644
90 +--- a/include/os/freebsd/spl/sys/vnode.h
91 ++++ b/include/os/freebsd/spl/sys/vnode.h
92 +@@ -59,6 +59,8 @@ enum symfollow { NO_FOLLOW = NOFOLLOW };
93 + #include <sys/file.h>
94 + #include <sys/filedesc.h>
95 + #include <sys/syscallsubr.h>
96 ++#include <sys/vm.h>
97 ++#include <vm/vm_object.h>
98 +
99 + typedef struct vop_vector vnodeops_t;
100 + #define VOP_FID VOP_VPTOFH
101 +@@ -83,6 +85,22 @@ vn_is_readonly(vnode_t *vp)
102 + #define vn_has_cached_data(vp) \
103 + ((vp)->v_object != NULL && \
104 + (vp)->v_object->resident_page_count > 0)
105 ++
106 ++static __inline void
107 ++vn_flush_cached_data(vnode_t *vp, boolean_t sync)
108 ++{
109 ++#if __FreeBSD_version > 1300054
110 ++ if (vm_object_mightbedirty(vp->v_object)) {
111 ++#else
112 ++ if (vp->v_object->flags & OBJ_MIGHTBEDIRTY) {
113 ++#endif
114 ++ int flags = sync ? OBJPC_SYNC : 0;
115 ++ zfs_vmobject_wlock(vp->v_object);
116 ++ vm_object_page_clean(vp->v_object, 0, 0, flags);
117 ++ zfs_vmobject_wunlock(vp->v_object);
118 ++ }
119 ++}
120 ++
121 + #define vn_exists(vp) do { } while (0)
122 + #define vn_invalid(vp) do { } while (0)
123 + #define vn_renamepath(tdvp, svp, tnm, lentnm) do { } while (0)
124 +diff --git a/include/os/freebsd/zfs/sys/zfs_znode_impl.h b/include/os/freebsd/zfs/sys/zfs_znode_impl.h
125 +index 7d28bddbf51..4456046e6e4 100644
126 +--- a/include/os/freebsd/zfs/sys/zfs_znode_impl.h
127 ++++ b/include/os/freebsd/zfs/sys/zfs_znode_impl.h
128 +@@ -116,7 +116,8 @@ typedef struct zfs_soft_state {
129 + #define Z_ISLNK(type) ((type) == VLNK)
130 + #define Z_ISDIR(type) ((type) == VDIR)
131 +
132 +-#define zn_has_cached_data(zp) vn_has_cached_data(ZTOV(zp))
133 ++#define zn_has_cached_data(zp) vn_has_cached_data(ZTOV(zp))
134 ++#define zn_flush_cached_data(zp, sync) vn_flush_cached_data(ZTOV(zp), sync)
135 + #define zn_rlimit_fsize(zp, uio) \
136 + vn_rlimit_fsize(ZTOV(zp), GET_UIO_STRUCT(uio), zfs_uio_td(uio))
137 +
138 +diff --git a/include/os/linux/zfs/sys/zfs_znode_impl.h b/include/os/linux/zfs/sys/zfs_znode_impl.h
139 +index 0a6273442b7..de46fc8f2bd 100644
140 +--- a/include/os/linux/zfs/sys/zfs_znode_impl.h
141 ++++ b/include/os/linux/zfs/sys/zfs_znode_impl.h
142 +@@ -71,6 +71,7 @@ extern "C" {
143 + #define Z_ISDIR(type) S_ISDIR(type)
144 +
145 + #define zn_has_cached_data(zp) ((zp)->z_is_mapped)
146 ++#define zn_flush_cached_data(zp, sync) write_inode_now(ZTOI(zp), sync)
147 + #define zn_rlimit_fsize(zp, uio) (0)
148 +
149 + /*
150 +diff --git a/include/sys/dnode.h b/include/sys/dnode.h
151 +index e7cccd044ab..3f5fcc958c3 100644
152 +--- a/include/sys/dnode.h
153 ++++ b/include/sys/dnode.h
154 +@@ -425,6 +425,7 @@ boolean_t dnode_add_ref(dnode_t *dn, void *ref);
155 + void dnode_rele(dnode_t *dn, void *ref);
156 + void dnode_rele_and_unlock(dnode_t *dn, void *tag, boolean_t evicting);
157 + int dnode_try_claim(objset_t *os, uint64_t object, int slots);
158 ++boolean_t dnode_is_dirty(dnode_t *dn);
159 + void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
160 + void dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, void *tag);
161 + void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
162 +diff --git a/man/man4/zfs.4 b/man/man4/zfs.4
163 +index d7fc31bfde1..a136690c76e 100644
164 +--- a/man/man4/zfs.4
165 ++++ b/man/man4/zfs.4
166 +@@ -1586,7 +1586,7 @@ Allow no-operation writes.
167 + The occurrence of nopwrites will further depend on other pool properties
168 + .Pq i.a. the checksumming and compression algorithms .
169 + .
170 +-.It Sy zfs_dmu_offset_next_sync Ns = Ns Sy 0 Ns | ns 1 Pq int
171 ++.It Sy zfs_dmu_offset_next_sync Ns = Ns Sy 0 Ns | Ns 1 Pq int
172 + Enable forcing TXG sync to find holes.
173 + When enabled forces ZFS to act like prior versions when
174 + .Sy SEEK_HOLE No or Sy SEEK_DATA
175 +diff --git a/module/zfs/dmu.c b/module/zfs/dmu.c
176 +index b29d82fd793..f12c5eda8b5 100644
177 +--- a/module/zfs/dmu.c
178 ++++ b/module/zfs/dmu.c
179 +@@ -2093,42 +2093,41 @@ int
180 + dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
181 + {
182 + dnode_t *dn;
183 +- int i, err;
184 +- boolean_t clean = B_TRUE;
185 ++ int err;
186 +
187 ++restart:
188 + err = dnode_hold(os, object, FTAG, &dn);
189 + if (err)
190 + return (err);
191 +
192 +- /*
193 +- * Check if dnode is dirty
194 +- */
195 +- for (i = 0; i < TXG_SIZE; i++) {
196 +- if (multilist_link_active(&dn->dn_dirty_link[i])) {
197 +- clean = B_FALSE;
198 +- break;
199 +- }
200 +- }
201 ++ rw_enter(&dn->dn_struct_rwlock, RW_READER);
202 +
203 +- /*
204 +- * If compatibility option is on, sync any current changes before
205 +- * we go trundling through the block pointers.
206 +- */
207 +- if (!clean && zfs_dmu_offset_next_sync) {
208 +- clean = B_TRUE;
209 +- dnode_rele(dn, FTAG);
210 +- txg_wait_synced(dmu_objset_pool(os), 0);
211 +- err = dnode_hold(os, object, FTAG, &dn);
212 +- if (err)
213 +- return (err);
214 +- }
215 ++ if (dnode_is_dirty(dn)) {
216 ++ /*
217 ++ * If the zfs_dmu_offset_next_sync module option is enabled
218 ++ * then strict hole reporting has been requested. Dirty
219 ++ * dnodes must be synced to disk to accurately report all
220 ++ * holes. When disabled (the default) dirty dnodes are
221 ++ * reported to not have any holes which is always safe.
222 ++ *
223 ++ * When called by zfs_holey_common() the zp->z_rangelock
224 ++ * is held to prevent zfs_write() and mmap writeback from
225 ++ * re-dirtying the dnode after txg_wait_synced().
226 ++ */
227 ++ if (zfs_dmu_offset_next_sync) {
228 ++ rw_exit(&dn->dn_struct_rwlock);
229 ++ dnode_rele(dn, FTAG);
230 ++ txg_wait_synced(dmu_objset_pool(os), 0);
231 ++ goto restart;
232 ++ }
233 +
234 +- if (clean)
235 +- err = dnode_next_offset(dn,
236 +- (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
237 +- else
238 + err = SET_ERROR(EBUSY);
239 ++ } else {
240 ++ err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |
241 ++ (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
242 ++ }
243 +
244 ++ rw_exit(&dn->dn_struct_rwlock);
245 + dnode_rele(dn, FTAG);
246 +
247 + return (err);
248 +diff --git a/module/zfs/dnode.c b/module/zfs/dnode.c
249 +index 900240479c7..6f87f49f89f 100644
250 +--- a/module/zfs/dnode.c
251 ++++ b/module/zfs/dnode.c
252 +@@ -1648,6 +1648,26 @@ dnode_try_claim(objset_t *os, uint64_t object, int slots)
253 + slots, NULL, NULL));
254 + }
255 +
256 ++/*
257 ++ * Checks if the dnode contains any uncommitted dirty records.
258 ++ */
259 ++boolean_t
260 ++dnode_is_dirty(dnode_t *dn)
261 ++{
262 ++ mutex_enter(&dn->dn_mtx);
263 ++
264 ++ for (int i = 0; i < TXG_SIZE; i++) {
265 ++ if (list_head(&dn->dn_dirty_records[i]) != NULL) {
266 ++ mutex_exit(&dn->dn_mtx);
267 ++ return (B_TRUE);
268 ++ }
269 ++ }
270 ++
271 ++ mutex_exit(&dn->dn_mtx);
272 ++
273 ++ return (B_FALSE);
274 ++}
275 ++
276 + void
277 + dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
278 + {
279 +diff --git a/module/zfs/zfs_vnops.c b/module/zfs/zfs_vnops.c
280 +index a83f0b02ab5..7cbb70f499a 100644
281 +--- a/module/zfs/zfs_vnops.c
282 ++++ b/module/zfs/zfs_vnops.c
283 +@@ -85,6 +85,7 @@ zfs_fsync(znode_t *zp, int syncflag, cred_t *cr)
284 + static int
285 + zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off)
286 + {
287 ++ zfs_locked_range_t *lr;
288 + uint64_t noff = (uint64_t)*off; /* new offset */
289 + uint64_t file_sz;
290 + int error;
291 +@@ -100,12 +101,18 @@ zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off)
292 + else
293 + hole = B_FALSE;
294 +
295 ++ /* Flush any mmap()'d data to disk */
296 ++ if (zn_has_cached_data(zp))
297 ++ zn_flush_cached_data(zp, B_FALSE);
298 ++
299 ++ lr = zfs_rangelock_enter(&zp->z_rangelock, 0, file_sz, RL_READER);
300 + error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
301 ++ zfs_rangelock_exit(lr);
302 +
303 + if (error == ESRCH)
304 + return (SET_ERROR(ENXIO));
305 +
306 +- /* file was dirty, so fall back to using generic logic */
307 ++ /* File was dirty, so fall back to using generic logic */
308 + if (error == EBUSY) {
309 + if (hole)
310 + *off = file_sz;
311 +diff --git a/tests/runfiles/common.run b/tests/runfiles/common.run
312 +index 7f7d161be35..9f181b53e15 100644
313 +--- a/tests/runfiles/common.run
314 ++++ b/tests/runfiles/common.run
315 +@@ -675,7 +675,7 @@ tests = ['migration_001_pos', 'migration_002_pos', 'migration_003_pos',
316 + tags = ['functional', 'migration']
317 +
318 + [tests/functional/mmap]
319 +-tests = ['mmap_write_001_pos', 'mmap_read_001_pos']
320 ++tests = ['mmap_write_001_pos', 'mmap_read_001_pos', 'mmap_seek_001_pos']
321 + tags = ['functional', 'mmap']
322 +
323 + [tests/functional/mount]
324 +diff --git a/tests/zfs-tests/cmd/Makefile.am b/tests/zfs-tests/cmd/Makefile.am
325 +index 2b965ca7000..d1c29fcd1c6 100644
326 +--- a/tests/zfs-tests/cmd/Makefile.am
327 ++++ b/tests/zfs-tests/cmd/Makefile.am
328 +@@ -19,6 +19,7 @@ SUBDIRS = \
329 + mktree \
330 + mmap_exec \
331 + mmap_libaio \
332 ++ mmap_seek \
333 + mmapwrite \
334 + nvlist_to_lua \
335 + randwritecomp \
336 +diff --git a/tests/zfs-tests/cmd/mmap_seek/.gitignore b/tests/zfs-tests/cmd/mmap_seek/.gitignore
337 +new file mode 100644
338 +index 00000000000..6b05a791750
339 +--- /dev/null
340 ++++ b/tests/zfs-tests/cmd/mmap_seek/.gitignore
341 +@@ -0,0 +1 @@
342 ++/mmap_seek
343 +diff --git a/tests/zfs-tests/cmd/mmap_seek/Makefile.am b/tests/zfs-tests/cmd/mmap_seek/Makefile.am
344 +new file mode 100644
345 +index 00000000000..b938931125f
346 +--- /dev/null
347 ++++ b/tests/zfs-tests/cmd/mmap_seek/Makefile.am
348 +@@ -0,0 +1,6 @@
349 ++include $(top_srcdir)/config/Rules.am
350 ++
351 ++pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/bin
352 ++
353 ++pkgexec_PROGRAMS = mmap_seek
354 ++mmap_seek_SOURCES = mmap_seek.c
355 +diff --git a/tests/zfs-tests/cmd/mmap_seek/mmap_seek.c b/tests/zfs-tests/cmd/mmap_seek/mmap_seek.c
356 +new file mode 100644
357 +index 00000000000..f476e1dba9a
358 +--- /dev/null
359 ++++ b/tests/zfs-tests/cmd/mmap_seek/mmap_seek.c
360 +@@ -0,0 +1,147 @@
361 ++/*
362 ++ * CDDL HEADER START
363 ++ *
364 ++ * The contents of this file are subject to the terms of the
365 ++ * Common Development and Distribution License (the "License").
366 ++ * You may not use this file except in compliance with the License.
367 ++ *
368 ++ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
369 ++ * or http://www.opensolaris.org/os/licensing.
370 ++ * See the License for the specific language governing permissions
371 ++ * and limitations under the License.
372 ++ *
373 ++ * When distributing Covered Code, include this CDDL HEADER in each
374 ++ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
375 ++ * If applicable, add the following below this CDDL HEADER, with the
376 ++ * fields enclosed by brackets "[]" replaced with your own identifying
377 ++ * information: Portions Copyright [yyyy] [name of copyright owner]
378 ++ *
379 ++ * CDDL HEADER END
380 ++ */
381 ++
382 ++/*
383 ++ * Copyright (c) 2021 by Lawrence Livermore National Security, LLC.
384 ++ */
385 ++
386 ++#include <unistd.h>
387 ++#include <fcntl.h>
388 ++#include <stdio.h>
389 ++#include <stdlib.h>
390 ++#include <string.h>
391 ++#include <sys/mman.h>
392 ++#include <errno.h>
393 ++
394 ++static void
395 ++seek_data(int fd, off_t offset, off_t expected)
396 ++{
397 ++ off_t data_offset = lseek(fd, offset, SEEK_DATA);
398 ++ if (data_offset != expected) {
399 ++ fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
400 ++ (int)offset, (int)data_offset, (int)expected);
401 ++ exit(2);
402 ++ }
403 ++}
404 ++
405 ++static void
406 ++seek_hole(int fd, off_t offset, off_t expected)
407 ++{
408 ++ off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
409 ++ if (hole_offset != expected) {
410 ++ fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
411 ++ (int)offset, (int)hole_offset, (int)expected);
412 ++ exit(2);
413 ++ }
414 ++}
415 ++
416 ++int
417 ++main(int argc, char **argv)
418 ++{
419 ++ char *execname = argv[0];
420 ++ char *file_path = argv[1];
421 ++ char *buf = NULL;
422 ++ int err;
423 ++
424 ++ if (argc != 4) {
425 ++ (void) printf("usage: %s <file name> <file size> "
426 ++ "<block size>\n", argv[0]);
427 ++ exit(1);
428 ++ }
429 ++
430 ++ int fd = open(file_path, O_RDWR | O_CREAT, 0666);
431 ++ if (fd == -1) {
432 ++ (void) fprintf(stderr, "%s: %s: ", execname, file_path);
433 ++ perror("open");
434 ++ exit(2);
435 ++ }
436 ++
437 ++ off_t file_size = atoi(argv[2]);
438 ++ off_t block_size = atoi(argv[3]);
439 ++
440 ++ if (block_size * 2 > file_size) {
441 ++ (void) fprintf(stderr, "file size must be at least "
442 ++ "double the block size\n");
443 ++ exit(2);
444 ++ }
445 ++
446 ++ err = ftruncate(fd, file_size);
447 ++ if (err == -1) {
448 ++ perror("ftruncate");
449 ++ exit(2);
450 ++ }
451 ++
452 ++ if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
453 ++ MAP_SHARED, fd, 0)) == MAP_FAILED) {
454 ++ perror("mmap");
455 ++ exit(2);
456 ++ }
457 ++
458 ++ /* Verify the file is sparse and reports no data. */
459 ++ seek_data(fd, 0, -1);
460 ++
461 ++ /* Verify the file is reported as a hole. */
462 ++ seek_hole(fd, 0, 0);
463 ++
464 ++ /* Verify search beyond end of file is an error. */
465 ++ seek_data(fd, 2 * file_size, -1);
466 ++ seek_hole(fd, 2 * file_size, -1);
467 ++
468 ++ /* Dirty the first byte. */
469 ++ memset(buf, 'a', 1);
470 ++ seek_data(fd, 0, 0);
471 ++ seek_data(fd, block_size, -1);
472 ++ seek_hole(fd, 0, block_size);
473 ++ seek_hole(fd, block_size, block_size);
474 ++
475 ++ /* Dirty the first half of the file. */
476 ++ memset(buf, 'b', file_size / 2);
477 ++ seek_data(fd, 0, 0);
478 ++ seek_data(fd, block_size, block_size);
479 ++ seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size));
480 ++ seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size));
481 ++
482 ++ /* Dirty the whole file. */
483 ++ memset(buf, 'c', file_size);
484 ++ seek_data(fd, 0, 0);
485 ++ seek_data(fd, file_size * 3 / 4,
486 ++ P2ROUNDUP(file_size * 3 / 4, block_size));
487 ++ seek_hole(fd, 0, file_size);
488 ++ seek_hole(fd, file_size / 2, file_size);
489 ++
490 ++ /* Punch a hole (required compression be enabled). */
491 ++ memset(buf + block_size, 0, block_size);
492 ++ seek_data(fd, 0, 0);
493 ++ seek_data(fd, block_size, 2 * block_size);
494 ++ seek_hole(fd, 0, block_size);
495 ++ seek_hole(fd, block_size, block_size);
496 ++ seek_hole(fd, 2 * block_size, file_size);
497 ++
498 ++ err = munmap(buf, file_size);
499 ++ if (err == -1) {
500 ++ perror("munmap");
501 ++ exit(2);
502 ++ }
503 ++
504 ++ close(fd);
505 ++
506 ++ return (0);
507 ++}
508 +diff --git a/tests/zfs-tests/include/commands.cfg b/tests/zfs-tests/include/commands.cfg
509 +index 1ec73f25bae..4497a6248b4 100644
510 +--- a/tests/zfs-tests/include/commands.cfg
511 ++++ b/tests/zfs-tests/include/commands.cfg
512 +@@ -209,6 +209,7 @@ export ZFSTEST_FILES='badsend
513 + mktree
514 + mmap_exec
515 + mmap_libaio
516 ++ mmap_seek
517 + mmapwrite
518 + nvlist_to_lua
519 + randfree_file
520 +diff --git a/tests/zfs-tests/include/tunables.cfg b/tests/zfs-tests/include/tunables.cfg
521 +index 56d430a3987..fff43e46916 100644
522 +--- a/tests/zfs-tests/include/tunables.cfg
523 ++++ b/tests/zfs-tests/include/tunables.cfg
524 +@@ -33,6 +33,7 @@ DEADMAN_FAILMODE deadman.failmode zfs_deadman_failmode
525 + DEADMAN_SYNCTIME_MS deadman.synctime_ms zfs_deadman_synctime_ms
526 + DEADMAN_ZIOTIME_MS deadman.ziotime_ms zfs_deadman_ziotime_ms
527 + DISABLE_IVSET_GUID_CHECK disable_ivset_guid_check zfs_disable_ivset_guid_check
528 ++DMU_OFFSET_NEXT_SYNC dmu_offset_next_sync zfs_dmu_offset_next_sync
529 + INITIALIZE_CHUNK_SIZE initialize_chunk_size zfs_initialize_chunk_size
530 + INITIALIZE_VALUE initialize_value zfs_initialize_value
531 + KEEP_LOG_SPACEMAPS_AT_EXPORT keep_log_spacemaps_at_export zfs_keep_log_spacemaps_at_export
532 +diff --git a/tests/zfs-tests/tests/functional/mmap/Makefile.am b/tests/zfs-tests/tests/functional/mmap/Makefile.am
533 +index 2adc398b8c0..b26791ee7ce 100644
534 +--- a/tests/zfs-tests/tests/functional/mmap/Makefile.am
535 ++++ b/tests/zfs-tests/tests/functional/mmap/Makefile.am
536 +@@ -4,7 +4,8 @@ dist_pkgdata_SCRIPTS = \
537 + cleanup.ksh \
538 + mmap_read_001_pos.ksh \
539 + mmap_write_001_pos.ksh \
540 +- mmap_libaio_001_pos.ksh
541 ++ mmap_libaio_001_pos.ksh \
542 ++ mmap_seek_001_pos.ksh
543 +
544 + dist_pkgdata_DATA = \
545 + mmap.cfg
546 +diff --git a/tests/zfs-tests/tests/functional/mmap/mmap_seek_001_pos.ksh b/tests/zfs-tests/tests/functional/mmap/mmap_seek_001_pos.ksh
547 +new file mode 100755
548 +index 00000000000..6188549ad8d
549 +--- /dev/null
550 ++++ b/tests/zfs-tests/tests/functional/mmap/mmap_seek_001_pos.ksh
551 +@@ -0,0 +1,67 @@
552 ++#!/bin/ksh -p
553 ++#
554 ++# CDDL HEADER START
555 ++#
556 ++# The contents of this file are subject to the terms of the
557 ++# Common Development and Distribution License (the "License").
558 ++# You may not use this file except in compliance with the License.
559 ++#
560 ++# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
561 ++# or http://www.opensolaris.org/os/licensing.
562 ++# See the License for the specific language governing permissions
563 ++# and limitations under the License.
564 ++#
565 ++# When distributing Covered Code, include this CDDL HEADER in each
566 ++# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
567 ++# If applicable, add the following below this CDDL HEADER, with the
568 ++# fields enclosed by brackets "[]" replaced with your own identifying
569 ++# information: Portions Copyright [yyyy] [name of copyright owner]
570 ++#
571 ++# CDDL HEADER END
572 ++#
573 ++
574 ++#
575 ++# Copyright (c) 2021 by Lawrence Livermore National Security, LLC.
576 ++#
577 ++
578 ++. $STF_SUITE/include/libtest.shlib
579 ++. $STF_SUITE/tests/functional/mmap/mmap.cfg
580 ++
581 ++#
582 ++# DESCRIPTION:
583 ++# lseek() data/holes for an mmap()'d file.
584 ++#
585 ++# STRATEGY:
586 ++# 1. Enable compression and hole reporting for dirty files.
587 ++# 2. Call mmap_seek binary test case for various record sizes.
588 ++#
589 ++
590 ++verify_runnable "global"
591 ++
592 ++function cleanup
593 ++{
594 ++ log_must zfs set compression=off $TESTPOOL/$TESTFS
595 ++ log_must zfs set recordsize=128k $TESTPOOL/$TESTFS
596 ++ log_must rm -f $TESTDIR/test-mmap-file
597 ++ log_must set_tunable64 DMU_OFFSET_NEXT_SYNC $dmu_offset_next_sync
598 ++}
599 ++
600 ++log_assert "lseek() data/holes for an mmap()'d file."
601 ++
602 ++log_onexit cleanup
603 ++
604 ++# Enable hole reporting for dirty files.
605 ++typeset dmu_offset_next_sync=$(get_tunable DMU_OFFSET_NEXT_SYNC)
606 ++log_must set_tunable64 DMU_OFFSET_NEXT_SYNC 1
607 ++
608 ++# Compression must be enabled to convert zero'd blocks to holes.
609 ++# This behavior is checked by the mmap_seek test.
610 ++log_must zfs set compression=on $TESTPOOL/$TESTFS
611 ++
612 ++for bs in 4096 8192 16384 32768 65536 131072; do
613 ++ log_must zfs set recordsize=$bs $TESTPOOL/$TESTFS
614 ++ log_must mmap_seek $TESTDIR/test-mmap-file $((1024*1024)) $bs
615 ++ log_must rm $TESTDIR/test-mmap-file
616 ++done
617 ++
618 ++log_pass "lseek() data/holes for an mmap()'d file succeeded."
619
620 diff --git a/sys-fs/zfs-kmod/zfs-kmod-2.1.1-r1.ebuild b/sys-fs/zfs-kmod/zfs-kmod-2.1.1-r1.ebuild
621 new file mode 100644
622 index 00000000000..5939b99e648
623 --- /dev/null
624 +++ b/sys-fs/zfs-kmod/zfs-kmod-2.1.1-r1.ebuild
625 @@ -0,0 +1,207 @@
626 +# Copyright 1999-2021 Gentoo Authors
627 +# Distributed under the terms of the GNU General Public License v2
628 +
629 +EAPI=7
630 +
631 +inherit autotools dist-kernel-utils flag-o-matic linux-mod toolchain-funcs
632 +
633 +DESCRIPTION="Linux ZFS kernel module for sys-fs/zfs"
634 +HOMEPAGE="https://github.com/openzfs/zfs"
635 +
636 +if [[ ${PV} == "9999" ]]; then
637 + inherit git-r3
638 + EGIT_REPO_URI="https://github.com/openzfs/zfs.git"
639 +else
640 + VERIFY_SIG_OPENPGP_KEY_PATH=${BROOT}/usr/share/openpgp-keys/openzfs.asc
641 + inherit verify-sig
642 +
643 + MY_PV="${PV/_rc/-rc}"
644 + SRC_URI="https://github.com/openzfs/zfs/releases/download/zfs-${MY_PV}/zfs-${MY_PV}.tar.gz"
645 + SRC_URI+=" verify-sig? ( https://github.com/openzfs/zfs/releases/download/zfs-${MY_PV}/zfs-${MY_PV}.tar.gz.asc )"
646 + S="${WORKDIR}/zfs-${PV%_rc?}"
647 + ZFS_KERNEL_COMPAT="5.14"
648 +
649 + # increments minor eg 5.14 -> 5.15, and still supports override.
650 + ZFS_KERNEL_DEP="${ZFS_KERNEL_COMPAT_OVERRIDE:-${ZFS_KERNEL_COMPAT}}"
651 + ZFS_KERNEL_DEP="${ZFS_KERNEL_DEP%%.*}.$(( ${ZFS_KERNEL_DEP##*.} + 1))"
652 +
653 + if [[ ${PV} != *_rc* ]]; then
654 + KEYWORDS="~amd64 ~arm64 ~ppc64 ~riscv"
655 + fi
656 +fi
657 +
658 +LICENSE="CDDL MIT debug? ( GPL-2+ )"
659 +SLOT="0/${PVR}"
660 +IUSE="custom-cflags debug +rootfs"
661 +
662 +RDEPEND="${DEPEND}
663 + !sys-kernel/spl
664 +"
665 +
666 +BDEPEND="
667 + dev-lang/perl
668 + virtual/awk
669 +"
670 +
671 +# we want dist-kernel block in BDEPEND because of portage resolver.
672 +# since linux-mod.eclass already sets version-unbounded dep, portage
673 +# will pull new versions. So we set it in BDEPEND which takes priority.
674 +# and we don't need in in git ebuild.
675 +if [[ ${PV} != "9999" ]] ; then
676 + BDEPEND+="
677 + verify-sig? ( app-crypt/openpgp-keys-openzfs )
678 + dist-kernel? ( <virtual/dist-kernel-${ZFS_KERNEL_DEP}:= )
679 + "
680 +fi
681 +
682 +# PDEPEND in this form is needed to trick portage suggest
683 +# enabling dist-kernel if only 1 package have it set
684 +PDEPEND="dist-kernel? ( ~sys-fs/zfs-${PV}[dist-kernel] )"
685 +
686 +RESTRICT="debug? ( strip ) test"
687 +
688 +DOCS=( AUTHORS COPYRIGHT META README.md )
689 +
690 +PATCHES=( "${FILESDIR}/2.1.1-fix-lseek-mmap.patch" )
691 +
692 +pkg_pretend() {
693 + use rootfs || return 0
694 +
695 + if has_version virtual/dist-kernel && ! use dist-kernel; then
696 + ewarn "You have virtual/dist-kernel installed, but"
697 + ewarn "USE=\"dist-kernel\" is not enabled for ${CATEGORY}/${PN}"
698 + ewarn "It's recommended to globally enable dist-kernel USE flag"
699 + ewarn "to auto-trigger initrd rebuilds with kernel updates"
700 + fi
701 +}
702 +
703 +pkg_setup() {
704 + CONFIG_CHECK="
705 + !DEBUG_LOCK_ALLOC
706 + EFI_PARTITION
707 + MODULES
708 + !PAX_KERNEXEC_PLUGIN_METHOD_OR
709 + !TRIM_UNUSED_KSYMS
710 + ZLIB_DEFLATE
711 + ZLIB_INFLATE
712 + "
713 +
714 + use debug && CONFIG_CHECK="${CONFIG_CHECK}
715 + FRAME_POINTER
716 + DEBUG_INFO
717 + !DEBUG_INFO_REDUCED
718 + "
719 +
720 + use rootfs && \
721 + CONFIG_CHECK="${CONFIG_CHECK}
722 + BLK_DEV_INITRD
723 + DEVTMPFS
724 + "
725 +
726 + kernel_is -lt 5 && CONFIG_CHECK="${CONFIG_CHECK} IOSCHED_NOOP"
727 +
728 + if [[ ${PV} != "9999" ]]; then
729 + local kv_major_max kv_minor_max zcompat
730 + zcompat="${ZFS_KERNEL_COMPAT_OVERRIDE:-${ZFS_KERNEL_COMPAT}}"
731 + kv_major_max="${zcompat%%.*}"
732 + zcompat="${zcompat#*.}"
733 + kv_minor_max="${zcompat%%.*}"
734 + kernel_is -le "${kv_major_max}" "${kv_minor_max}" || die \
735 + "Linux ${kv_major_max}.${kv_minor_max} is the latest supported version"
736 +
737 + fi
738 +
739 + kernel_is -ge 3 10 || die "Linux 3.10 or newer required"
740 +
741 + linux-mod_pkg_setup
742 +}
743 +
744 +src_prepare() {
745 + default
746 +
747 + # Run unconditionally (bug #792627)
748 + eautoreconf
749 +
750 + if [[ ${PV} != "9999" ]]; then
751 + # Set module revision number
752 + sed -i "s/\(Release:\)\(.*\)1/\1\2${PR}-gentoo/" META || die "Could not set Gentoo release"
753 + fi
754 +}
755 +
756 +src_configure() {
757 + set_arch_to_kernel
758 +
759 + use custom-cflags || strip-flags
760 +
761 + filter-ldflags -Wl,*
762 +
763 + # Set CROSS_COMPILE in the environment.
764 + # This allows the user to override it via make.conf or via a local Makefile.
765 + # https://bugs.gentoo.org/811600
766 + export CROSS_COMPILE=${CROSS_COMPILE-${CHOST}-}
767 +
768 + local myconf=(
769 + HOSTCC="$(tc-getBUILD_CC)"
770 + --bindir="${EPREFIX}/bin"
771 + --sbindir="${EPREFIX}/sbin"
772 + --with-config=kernel
773 + --with-linux="${KV_DIR}"
774 + --with-linux-obj="${KV_OUT_DIR}"
775 + $(use_enable debug)
776 + )
777 +
778 + econf "${myconf[@]}"
779 +}
780 +
781 +src_compile() {
782 + set_arch_to_kernel
783 +
784 + myemakeargs=(
785 + HOSTCC="$(tc-getBUILD_CC)"
786 + V=1
787 + )
788 +
789 + emake "${myemakeargs[@]}"
790 +}
791 +
792 +src_install() {
793 + set_arch_to_kernel
794 +
795 + myemakeargs+=(
796 + DEPMOD=:
797 + DESTDIR="${D}"
798 + INSTALL_MOD_PATH="${EPREFIX:-/}" # lib/modules/<kver> added by KBUILD
799 + )
800 +
801 + emake "${myemakeargs[@]}" install
802 +
803 + einstalldocs
804 +}
805 +
806 +pkg_postinst() {
807 + linux-mod_pkg_postinst
808 +
809 + if [[ -z ${ROOT} ]] && use dist-kernel; then
810 + set_arch_to_portage
811 + dist-kernel_reinstall_initramfs "${KV_DIR}" "${KV_FULL}"
812 + fi
813 +
814 + if use x86 || use arm; then
815 + ewarn "32-bit kernels will likely require increasing vmalloc to"
816 + ewarn "at least 256M and decreasing zfs_arc_max to some value less than that."
817 + fi
818 +
819 + if has_version sys-boot/grub; then
820 + ewarn "This version of OpenZFS includes support for new feature flags"
821 + ewarn "that are incompatible with previous versions. GRUB2 support for"
822 + ewarn "/boot with the new feature flags is not yet available."
823 + ewarn "Do *NOT* upgrade root pools to use the new feature flags."
824 + ewarn "Any new pools will be created with the new feature flags by default"
825 + ewarn "and will not be compatible with older versions of ZFSOnLinux. To"
826 + ewarn "create a newpool that is backward compatible wih GRUB2, use "
827 + ewarn
828 + ewarn "zpool create -o compatibility=grub2 ..."
829 + ewarn
830 + ewarn "Refer to /etc/zfs/compatibility.d/grub2 for list of features."
831 + fi
832 +}