Skip to content

Commit a10e552

Browse files
bwatkinsonmmaybeemattmacyBrian Behlendorf
authored
Adding Direct IO Support
Adding O_DIRECT support to ZFS to bypass the ARC for writes/reads. O_DIRECT support in ZFS will always ensure there is coherency between buffered and O_DIRECT IO requests. This ensures that all IO requests, whether buffered or direct, will see the same file contents at all times. Just as in other FS's , O_DIRECT does not imply O_SYNC. While data is written directly to VDEV disks, metadata will not be synced until the associated TXG is synced. For both O_DIRECT read and write request the offset and request sizes, at a minimum, must be PAGE_SIZE aligned. In the event they are not, then EINVAL is returned unless the direct property is set to always (see below). For O_DIRECT writes: The request also must be block aligned (recordsize) or the write request will take the normal (buffered) write path. In the event that request is block aligned and a cached copy of the buffer in the ARC, then it will be discarded from the ARC forcing all further reads to retrieve the data from disk. For O_DIRECT reads: The only alignment restrictions are PAGE_SIZE alignment. In the event that the requested data is in buffered (in the ARC) it will just be copied from the ARC into the user buffer. For both O_DIRECT writes and reads the O_DIRECT flag will be ignored in the event that file contents are mmap'ed. In this case, all requests that are at least PAGE_SIZE aligned will just fall back to the buffered paths. If the request however is not PAGE_SIZE aligned, EINVAL will be returned as always regardless if the file's contents are mmap'ed. Since O_DIRECT writes go through the normal ZIO pipeline, the following operations are supported just as with normal buffered writes: Checksum Compression Encryption Erasure Coding There is one caveat for the data integrity of O_DIRECT writes that is distinct for each of the OS's supported by ZFS. FreeBSD - FreeBSD is able to place user pages under write protection so any data in the user buffers and written directly down to the VDEV disks is guaranteed to not change. There is no concern with data integrity and O_DIRECT writes. Linux - Linux is not able to place anonymous user pages under write protection. Because of this, if the user decides to manipulate the page contents while the write operation is occurring, data integrity can not be guaranteed. However, there is a module parameter `zfs_vdev_direct_write_verify` that controls the if a O_DIRECT writes that can occur to a top-level VDEV before a checksum verify is run before the contents of the I/O buffer are committed to disk. In the event of a checksum verification failure the write will return EIO. The number of O_DIRECT write checksum verification errors can be observed by doing `zpool status -d`, which will list all verification errors that have occurred on a top-level VDEV. Along with `zpool status`, a ZED event will be issues as `dio_verify` when a checksum verification error occurs. ZVOLs and dedup is not currently supported with Direct I/O. A new dataset property `direct` has been added with the following 3 allowable values: disabled - Accepts O_DIRECT flag, but silently ignores it and treats the request as a buffered IO request. standard - Follows the alignment restrictions outlined above for write/read IO requests when the O_DIRECT flag is used. always - Treats every write/read IO request as though it passed O_DIRECT and will do O_DIRECT if the alignment restrictions are met otherwise will redirect through the ARC. This property will not allow a request to fail. There is also a module parameter zfs_dio_enabled that can be used to force all reads and writes through the ARC. By setting this module parameter to 0, it mimics as if the direct dataset property is set to disabled. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Tony Hutter <[email protected]> Signed-off-by: Brian Atkinson <[email protected]> Co-authored-by: Mark Maybee <[email protected]> Co-authored-by: Matt Macy <[email protected]> Co-authored-by: Brian Behlendorf <[email protected]> Closes #10018
1 parent 1713aa7 commit a10e552

File tree

111 files changed

+5998
-735
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+5998
-735
lines changed

cmd/zpool/zpool_main.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ get_usage(zpool_help_t idx)
522522
return (gettext("\tstatus [--power] [-j [--json-int, "
523523
"--json-flat-vdevs, ...\n"
524524
"\t --json-pool-key-guid]] [-c [script1,script2,...]] "
525-
"[-DegiLpPstvx] ...\n"
525+
"[-dDegiLpPstvx] ...\n"
526526
"\t [-T d|u] [pool] [interval [count]]\n"));
527527
case HELP_UPGRADE:
528528
return (gettext("\tupgrade\n"
@@ -2602,6 +2602,7 @@ typedef struct status_cbdata {
26022602
boolean_t cb_print_unhealthy;
26032603
boolean_t cb_print_status;
26042604
boolean_t cb_print_slow_ios;
2605+
boolean_t cb_print_dio_verify;
26052606
boolean_t cb_print_vdev_init;
26062607
boolean_t cb_print_vdev_trim;
26072608
vdev_cmd_data_list_t *vcdl;
@@ -2879,7 +2880,7 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
28792880
uint_t c, i, vsc, children;
28802881
pool_scan_stat_t *ps = NULL;
28812882
vdev_stat_t *vs;
2882-
char rbuf[6], wbuf[6], cbuf[6];
2883+
char rbuf[6], wbuf[6], cbuf[6], dbuf[6];
28832884
char *vname;
28842885
uint64_t notpresent;
28852886
spare_cbdata_t spare_cb;
@@ -2997,6 +2998,17 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
29972998
printf(" %5s", "-");
29982999
}
29993000
}
3001+
if (VDEV_STAT_VALID(vs_dio_verify_errors, vsc) &&
3002+
cb->cb_print_dio_verify) {
3003+
zfs_nicenum(vs->vs_dio_verify_errors, dbuf,
3004+
sizeof (dbuf));
3005+
3006+
if (cb->cb_literal)
3007+
printf(" %5llu",
3008+
(u_longlong_t)vs->vs_dio_verify_errors);
3009+
else
3010+
printf(" %5s", dbuf);
3011+
}
30003012
}
30013013

30023014
if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
@@ -10873,6 +10885,10 @@ status_callback(zpool_handle_t *zhp, void *data)
1087310885
printf_color(ANSI_BOLD, " %5s", gettext("POWER"));
1087410886
}
1087510887

10888+
if (cbp->cb_print_dio_verify) {
10889+
printf_color(ANSI_BOLD, " %5s", gettext("DIO"));
10890+
}
10891+
1087610892
if (cbp->vcdl != NULL)
1087710893
print_cmd_columns(cbp->vcdl, 0);
1087810894

@@ -10921,10 +10937,11 @@ status_callback(zpool_handle_t *zhp, void *data)
1092110937
}
1092210938

1092310939
/*
10924-
* zpool status [-c [script1,script2,...]] [-DegiLpPstvx] [--power] [-T d|u] ...
10925-
* [pool] [interval [count]]
10940+
* zpool status [-c [script1,script2,...]] [-dDegiLpPstvx] [--power] ...
10941+
* [-T d|u] [pool] [interval [count]]
1092610942
*
1092710943
* -c CMD For each vdev, run command CMD
10944+
* -d Display Direct I/O write verify errors
1092810945
* -D Display dedup status (undocumented)
1092910946
* -e Display only unhealthy vdevs
1093010947
* -g Display guid for individual vdev name.
@@ -10967,7 +10984,7 @@ zpool_do_status(int argc, char **argv)
1096710984
};
1096810985

1096910986
/* check options */
10970-
while ((c = getopt_long(argc, argv, "c:jDegiLpPstT:vx", long_options,
10987+
while ((c = getopt_long(argc, argv, "c:jdDegiLpPstT:vx", long_options,
1097110988
NULL)) != -1) {
1097210989
switch (c) {
1097310990
case 'c':
@@ -10994,6 +11011,9 @@ zpool_do_status(int argc, char **argv)
1099411011
}
1099511012
cmd = optarg;
1099611013
break;
11014+
case 'd':
11015+
cb.cb_print_dio_verify = B_TRUE;
11016+
break;
1099711017
case 'D':
1099811018
if (++cb.cb_dedup_stats > 2)
1099911019
cb.cb_dedup_stats = 2;

cmd/ztest.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,13 @@ ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
22622262
if (ztest_random(4) != 0) {
22632263
int prefetch = ztest_random(2) ?
22642264
DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
2265+
2266+
/*
2267+
* We will randomly set when to do O_DIRECT on a read.
2268+
*/
2269+
if (ztest_random(4) == 0)
2270+
prefetch |= DMU_DIRECTIO;
2271+
22652272
ztest_block_tag_t rbt;
22662273

22672274
VERIFY(dmu_read(os, lr->lr_foid, offset,
@@ -2813,6 +2820,13 @@ ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
28132820
enum ztest_io_type io_type;
28142821
uint64_t blocksize;
28152822
void *data;
2823+
uint32_t dmu_read_flags = DMU_READ_NO_PREFETCH;
2824+
2825+
/*
2826+
* We will randomly set when to do O_DIRECT on a read.
2827+
*/
2828+
if (ztest_random(4) == 0)
2829+
dmu_read_flags |= DMU_DIRECTIO;
28162830

28172831
VERIFY0(dmu_object_info(zd->zd_os, object, &doi));
28182832
blocksize = doi.doi_data_block_size;
@@ -2878,7 +2892,7 @@ ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
28782892
(void) pthread_rwlock_unlock(&ztest_name_lock);
28792893

28802894
VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2881-
DMU_READ_NO_PREFETCH));
2895+
dmu_read_flags));
28822896

28832897
(void) ztest_write(zd, object, offset, blocksize, data);
28842898
break;
@@ -5045,6 +5059,13 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
50455059
uint64_t stride = 123456789ULL;
50465060
uint64_t width = 40;
50475061
int free_percent = 5;
5062+
uint32_t dmu_read_flags = DMU_READ_PREFETCH;
5063+
5064+
/*
5065+
* We will randomly set when to do O_DIRECT on a read.
5066+
*/
5067+
if (ztest_random(4) == 0)
5068+
dmu_read_flags |= DMU_DIRECTIO;
50485069

50495070
/*
50505071
* This test uses two objects, packobj and bigobj, that are always
@@ -5123,10 +5144,10 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
51235144
* Read the current contents of our objects.
51245145
*/
51255146
error = dmu_read(os, packobj, packoff, packsize, packbuf,
5126-
DMU_READ_PREFETCH);
5147+
dmu_read_flags);
51275148
ASSERT0(error);
51285149
error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
5129-
DMU_READ_PREFETCH);
5150+
dmu_read_flags);
51305151
ASSERT0(error);
51315152

51325153
/*
@@ -5244,9 +5265,9 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
52445265
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
52455266

52465267
VERIFY0(dmu_read(os, packobj, packoff,
5247-
packsize, packcheck, DMU_READ_PREFETCH));
5268+
packsize, packcheck, dmu_read_flags));
52485269
VERIFY0(dmu_read(os, bigobj, bigoff,
5249-
bigsize, bigcheck, DMU_READ_PREFETCH));
5270+
bigsize, bigcheck, dmu_read_flags));
52505271

52515272
ASSERT0(memcmp(packbuf, packcheck, packsize));
52525273
ASSERT0(memcmp(bigbuf, bigcheck, bigsize));
@@ -5336,6 +5357,13 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
53365357
dmu_buf_t *bonus_db;
53375358
arc_buf_t **bigbuf_arcbufs;
53385359
dmu_object_info_t doi;
5360+
uint32_t dmu_read_flags = DMU_READ_PREFETCH;
5361+
5362+
/*
5363+
* We will randomly set when to do O_DIRECT on a read.
5364+
*/
5365+
if (ztest_random(4) == 0)
5366+
dmu_read_flags |= DMU_DIRECTIO;
53395367

53405368
size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
53415369
od = umem_alloc(size, UMEM_NOFAIL);
@@ -5466,10 +5494,10 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
54665494
*/
54675495
if (i != 0 || ztest_random(2) != 0) {
54685496
error = dmu_read(os, packobj, packoff,
5469-
packsize, packbuf, DMU_READ_PREFETCH);
5497+
packsize, packbuf, dmu_read_flags);
54705498
ASSERT0(error);
54715499
error = dmu_read(os, bigobj, bigoff, bigsize,
5472-
bigbuf, DMU_READ_PREFETCH);
5500+
bigbuf, dmu_read_flags);
54735501
ASSERT0(error);
54745502
}
54755503
compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
@@ -5529,9 +5557,9 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
55295557
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
55305558

55315559
VERIFY0(dmu_read(os, packobj, packoff,
5532-
packsize, packcheck, DMU_READ_PREFETCH));
5560+
packsize, packcheck, dmu_read_flags));
55335561
VERIFY0(dmu_read(os, bigobj, bigoff,
5534-
bigsize, bigcheck, DMU_READ_PREFETCH));
5562+
bigsize, bigcheck, dmu_read_flags));
55355563

55365564
ASSERT0(memcmp(packbuf, packcheck, packsize));
55375565
ASSERT0(memcmp(bigbuf, bigcheck, bigsize));

config/kernel-get-user-pages.m4

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
dnl #
2+
dnl # get_user_pages_unlocked() function was not available till 4.0.
3+
dnl # In earlier kernels (< 4.0) get_user_pages() is available().
4+
dnl #
5+
dnl # 4.0 API change,
6+
dnl # long get_user_pages_unlocked(struct task_struct *tsk,
7+
dnl # struct mm_struct *mm, unsigned long start, unsigned long nr_pages,
8+
dnl # int write, int force, struct page **pages)
9+
dnl #
10+
dnl # 4.8 API change,
11+
dnl # long get_user_pages_unlocked(unsigned long start,
12+
dnl # unsigned long nr_pages, int write, int force, struct page **page)
13+
dnl #
14+
dnl # 4.9 API change,
15+
dnl # long get_user_pages_unlocked(usigned long start, int nr_pages,
16+
dnl # struct page **pages, unsigned int gup_flags)
17+
dnl #
18+
19+
dnl#
20+
dnl# Check available get_user_pages/_unlocked interfaces.
21+
dnl#
22+
AC_DEFUN([ZFS_AC_KERNEL_SRC_GET_USER_PAGES], [
23+
ZFS_LINUX_TEST_SRC([get_user_pages_unlocked_gup_flags], [
24+
#include <linux/mm.h>
25+
], [
26+
unsigned long start = 0;
27+
unsigned long nr_pages = 1;
28+
unsigned int gup_flags = 0;
29+
struct page **pages = NULL;
30+
long ret __attribute__ ((unused));
31+
32+
ret = get_user_pages_unlocked(start, nr_pages, pages,
33+
gup_flags);
34+
])
35+
36+
ZFS_LINUX_TEST_SRC([get_user_pages_unlocked_write_flag], [
37+
#include <linux/mm.h>
38+
], [
39+
unsigned long start = 0;
40+
unsigned long nr_pages = 1;
41+
int write = 0;
42+
int force = 0;
43+
long ret __attribute__ ((unused));
44+
struct page **pages = NULL;
45+
46+
ret = get_user_pages_unlocked(start, nr_pages, write, force,
47+
pages);
48+
])
49+
50+
ZFS_LINUX_TEST_SRC([get_user_pages_unlocked_task_struct], [
51+
#include <linux/mm.h>
52+
], [
53+
struct task_struct *tsk = NULL;
54+
struct mm_struct *mm = NULL;
55+
unsigned long start = 0;
56+
unsigned long nr_pages = 1;
57+
int write = 0;
58+
int force = 0;
59+
struct page **pages = NULL;
60+
long ret __attribute__ ((unused));
61+
62+
ret = get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
63+
force, pages);
64+
])
65+
66+
ZFS_LINUX_TEST_SRC([get_user_pages_unlocked_task_struct_gup_flags], [
67+
#include <linux/mm.h>
68+
], [
69+
struct task_struct *tsk = NULL;
70+
struct mm_struct *mm = NULL;
71+
unsigned long start = 0;
72+
unsigned long nr_pages = 1;
73+
struct page **pages = NULL;
74+
unsigned int gup_flags = 0;
75+
long ret __attribute__ ((unused));
76+
77+
ret = get_user_pages_unlocked(tsk, mm, start, nr_pages,
78+
pages, gup_flags);
79+
])
80+
81+
ZFS_LINUX_TEST_SRC([get_user_pages_task_struct], [
82+
#include <linux/mm.h>
83+
], [
84+
struct task_struct *tsk = NULL;
85+
struct mm_struct *mm = NULL;
86+
struct vm_area_struct **vmas = NULL;
87+
unsigned long start = 0;
88+
unsigned long nr_pages = 1;
89+
int write = 0;
90+
int force = 0;
91+
struct page **pages = NULL;
92+
int ret __attribute__ ((unused));
93+
94+
ret = get_user_pages(tsk, mm, start, nr_pages, write,
95+
force, pages, vmas);
96+
])
97+
])
98+
99+
dnl #
100+
dnl # Supported get_user_pages/_unlocked interfaces checked newest to oldest.
101+
dnl # We first check for get_user_pages_unlocked as that is available in
102+
dnl # newer kernels.
103+
dnl #
104+
AC_DEFUN([ZFS_AC_KERNEL_GET_USER_PAGES], [
105+
dnl #
106+
dnl # Current API (as of 4.9) of get_user_pages_unlocked
107+
dnl #
108+
AC_MSG_CHECKING([whether get_user_pages_unlocked() takes gup flags])
109+
ZFS_LINUX_TEST_RESULT([get_user_pages_unlocked_gup_flags], [
110+
AC_MSG_RESULT(yes)
111+
AC_DEFINE(HAVE_GET_USER_PAGES_UNLOCKED_GUP_FLAGS, 1,
112+
[get_user_pages_unlocked() takes gup flags])
113+
], [
114+
AC_MSG_RESULT(no)
115+
116+
dnl #
117+
dnl # 4.8 API change, get_user_pages_unlocked
118+
dnl #
119+
AC_MSG_CHECKING(
120+
[whether get_user_pages_unlocked() takes write flag])
121+
ZFS_LINUX_TEST_RESULT([get_user_pages_unlocked_write_flag], [
122+
AC_MSG_RESULT(yes)
123+
AC_DEFINE(HAVE_GET_USER_PAGES_UNLOCKED_WRITE_FLAG, 1,
124+
[get_user_pages_unlocked() takes write flag])
125+
], [
126+
AC_MSG_RESULT(no)
127+
128+
dnl #
129+
dnl # 4.0-4.3, 4.5-4.7 API, get_user_pages_unlocked
130+
dnl #
131+
AC_MSG_CHECKING(
132+
[whether get_user_pages_unlocked() takes task_struct])
133+
ZFS_LINUX_TEST_RESULT(
134+
[get_user_pages_unlocked_task_struct], [
135+
AC_MSG_RESULT(yes)
136+
AC_DEFINE(
137+
HAVE_GET_USER_PAGES_UNLOCKED_TASK_STRUCT, 1,
138+
[get_user_pages_unlocked() takes task_struct])
139+
], [
140+
AC_MSG_RESULT(no)
141+
142+
dnl #
143+
dnl # 4.4 API, get_user_pages_unlocked
144+
dnl #
145+
AC_MSG_CHECKING(
146+
[whether get_user_pages_unlocked() takes task_struct, gup_flags])
147+
ZFS_LINUX_TEST_RESULT(
148+
[get_user_pages_unlocked_task_struct_gup_flags], [
149+
AC_MSG_RESULT(yes)
150+
AC_DEFINE(
151+
HAVE_GET_USER_PAGES_UNLOCKED_TASK_STRUCT_GUP_FLAGS, 1,
152+
[get_user_pages_unlocked() takes task_struct, gup_flags])
153+
], [
154+
AC_MSG_RESULT(no)
155+
156+
dnl #
157+
dnl # get_user_pages
158+
dnl #
159+
AC_MSG_CHECKING(
160+
[whether get_user_pages() takes struct task_struct])
161+
ZFS_LINUX_TEST_RESULT(
162+
[get_user_pages_task_struct], [
163+
AC_MSG_RESULT(yes)
164+
AC_DEFINE(
165+
HAVE_GET_USER_PAGES_TASK_STRUCT, 1,
166+
[get_user_pages() takes task_struct])
167+
], [
168+
dnl #
169+
dnl # If we cannot map the user's
170+
dnl # pages in then we cannot do
171+
dnl # Direct I/O
172+
dnl #
173+
ZFS_LINUX_TEST_ERROR([Direct I/O])
174+
])
175+
])
176+
])
177+
])
178+
])
179+
])

config/kernel-vfs-direct_IO.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl #
2-
dnl # Check for direct IO interfaces.
2+
dnl # Check for Direct I/O interfaces.
33
dnl #
44
AC_DEFUN([ZFS_AC_KERNEL_SRC_VFS_DIRECT_IO], [
55
ZFS_LINUX_TEST_SRC([direct_io_iter], [
@@ -100,7 +100,7 @@ AC_DEFUN([ZFS_AC_KERNEL_VFS_DIRECT_IO], [
100100
AC_DEFINE(HAVE_VFS_DIRECT_IO_IOVEC, 1,
101101
[aops->direct_IO() uses iovec])
102102
],[
103-
ZFS_LINUX_TEST_ERROR([direct IO])
103+
ZFS_LINUX_TEST_ERROR([Direct I/O])
104104
AC_MSG_RESULT([no])
105105
])
106106
])

0 commit comments

Comments
 (0)