Skip to content

Commit e6e5b4c

Browse files
bwatkinsonmmaybeemattmacyBrian Behlendorf
committed
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 requeset 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 Dedup 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_pct` that contols the percentage of O_DIRECT writes that can occur to a top-level VDEV before a checksum verify is run before the contents of the user buffers are committed to disk. In the event of a checksum verification failure the write will be redirected through the ARC. The deafault value for `zfs_vdev_direct_write_verify_pct` is 2 percent of Direct I/O writes to a top-level VDEV. 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. 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. 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]>
1 parent c98295e commit e6e5b4c

File tree

110 files changed

+9736
-7364
lines changed

Some content is hidden

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

110 files changed

+9736
-7364
lines changed

cmd/zpool/zpool_main.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ get_usage(zpool_help_t idx)
423423
"[<device> ...]\n"));
424424
case HELP_STATUS:
425425
return (gettext("\tstatus [--power] [-c [script1,script2,...]] "
426-
"[-DegiLpPstvx] [-T d|u] [pool] ...\n"
426+
"[-dDegiLpPstvx] [-T d|u] [pool] ...\n"
427427
"\t [interval [count]]\n"));
428428
case HELP_UPGRADE:
429429
return (gettext("\tupgrade\n"
@@ -2286,6 +2286,7 @@ typedef struct status_cbdata {
22862286
boolean_t cb_print_unhealthy;
22872287
boolean_t cb_print_status;
22882288
boolean_t cb_print_slow_ios;
2289+
boolean_t cb_print_dio_verify;
22892290
boolean_t cb_print_vdev_init;
22902291
boolean_t cb_print_vdev_trim;
22912292
vdev_cmd_data_list_t *vcdl;
@@ -2518,7 +2519,7 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
25182519
uint_t c, i, vsc, children;
25192520
pool_scan_stat_t *ps = NULL;
25202521
vdev_stat_t *vs;
2521-
char rbuf[6], wbuf[6], cbuf[6];
2522+
char rbuf[6], wbuf[6], cbuf[6], dbuf[6];
25222523
char *vname;
25232524
uint64_t notpresent;
25242525
spare_cbdata_t spare_cb;
@@ -2636,6 +2637,17 @@ print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
26362637
printf(" %5s", "-");
26372638
}
26382639
}
2640+
if (VDEV_STAT_VALID(vs_dio_verify_errors, vsc) &&
2641+
cb->cb_print_dio_verify) {
2642+
zfs_nicenum(vs->vs_dio_verify_errors, dbuf,
2643+
sizeof (dbuf));
2644+
2645+
if (cb->cb_literal)
2646+
printf(" %5llu",
2647+
(u_longlong_t)vs->vs_dio_verify_errors);
2648+
else
2649+
printf(" %5s", dbuf);
2650+
}
26392651
}
26402652

26412653
if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
@@ -9300,6 +9312,10 @@ status_callback(zpool_handle_t *zhp, void *data)
93009312
printf_color(ANSI_BOLD, " %5s", gettext("POWER"));
93019313
}
93029314

9315+
if (cbp->cb_print_dio_verify) {
9316+
printf_color(ANSI_BOLD, " %5s", gettext("DIO"));
9317+
}
9318+
93039319
if (cbp->vcdl != NULL)
93049320
print_cmd_columns(cbp->vcdl, 0);
93059321

@@ -9348,10 +9364,11 @@ status_callback(zpool_handle_t *zhp, void *data)
93489364
}
93499365

93509366
/*
9351-
* zpool status [-c [script1,script2,...]] [-DegiLpPstvx] [--power] [-T d|u] ...
9352-
* [pool] [interval [count]]
9367+
* zpool status [-c [script1,script2,...]] [-dDegiLpPstvx] [--power] ...
9368+
* [-T d|u] [pool] [interval [count]]
93539369
*
93549370
* -c CMD For each vdev, run command CMD
9371+
* -d Display Direct I/O write verify errors
93559372
* -D Display dedup status (undocumented)
93569373
* -e Display only unhealthy vdevs
93579374
* -g Display guid for individual vdev name.
@@ -9384,7 +9401,7 @@ zpool_do_status(int argc, char **argv)
93849401
};
93859402

93869403
/* check options */
9387-
while ((c = getopt_long(argc, argv, "c:DegiLpPstT:vx", long_options,
9404+
while ((c = getopt_long(argc, argv, "c:dDegiLpPstT:vx", long_options,
93889405
NULL)) != -1) {
93899406
switch (c) {
93909407
case 'c':
@@ -9411,6 +9428,9 @@ zpool_do_status(int argc, char **argv)
94119428
}
94129429
cmd = optarg;
94139430
break;
9431+
case 'd':
9432+
cb.cb_print_dio_verify = B_TRUE;
9433+
break;
94149434
case 'D':
94159435
cb.cb_dedup_stats = B_TRUE;
94169436
break;

cmd/ztest.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,13 @@ ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
22552255
if (ztest_random(4) != 0) {
22562256
int prefetch = ztest_random(2) ?
22572257
DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
2258+
2259+
/*
2260+
* We will randomly set when to do O_DIRECT on a read.
2261+
*/
2262+
if (ztest_random(4) == 0)
2263+
prefetch |= DMU_DIRECTIO;
2264+
22582265
ztest_block_tag_t rbt;
22592266

22602267
VERIFY(dmu_read(os, lr->lr_foid, offset,
@@ -2806,6 +2813,13 @@ ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
28062813
enum ztest_io_type io_type;
28072814
uint64_t blocksize;
28082815
void *data;
2816+
uint32_t dmu_read_flags = DMU_READ_NO_PREFETCH;
2817+
2818+
/*
2819+
* We will randomly set when to do O_DIRECT on a read.
2820+
*/
2821+
if (ztest_random(4) == 0)
2822+
dmu_read_flags |= DMU_DIRECTIO;
28092823

28102824
VERIFY0(dmu_object_info(zd->zd_os, object, &doi));
28112825
blocksize = doi.doi_data_block_size;
@@ -2871,7 +2885,7 @@ ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
28712885
(void) pthread_rwlock_unlock(&ztest_name_lock);
28722886

28732887
VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2874-
DMU_READ_NO_PREFETCH));
2888+
dmu_read_flags));
28752889

28762890
(void) ztest_write(zd, object, offset, blocksize, data);
28772891
break;
@@ -5038,6 +5052,13 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
50385052
uint64_t stride = 123456789ULL;
50395053
uint64_t width = 40;
50405054
int free_percent = 5;
5055+
uint32_t dmu_read_flags = DMU_READ_PREFETCH;
5056+
5057+
/*
5058+
* We will randomly set when to do O_DIRECT on a read.
5059+
*/
5060+
if (ztest_random(4) == 0)
5061+
dmu_read_flags |= DMU_DIRECTIO;
50415062

50425063
/*
50435064
* This test uses two objects, packobj and bigobj, that are always
@@ -5116,10 +5137,10 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
51165137
* Read the current contents of our objects.
51175138
*/
51185139
error = dmu_read(os, packobj, packoff, packsize, packbuf,
5119-
DMU_READ_PREFETCH);
5140+
dmu_read_flags);
51205141
ASSERT0(error);
51215142
error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
5122-
DMU_READ_PREFETCH);
5143+
dmu_read_flags);
51235144
ASSERT0(error);
51245145

51255146
/*
@@ -5237,9 +5258,9 @@ ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
52375258
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
52385259

52395260
VERIFY0(dmu_read(os, packobj, packoff,
5240-
packsize, packcheck, DMU_READ_PREFETCH));
5261+
packsize, packcheck, dmu_read_flags));
52415262
VERIFY0(dmu_read(os, bigobj, bigoff,
5242-
bigsize, bigcheck, DMU_READ_PREFETCH));
5263+
bigsize, bigcheck, dmu_read_flags));
52435264

52445265
ASSERT0(memcmp(packbuf, packcheck, packsize));
52455266
ASSERT0(memcmp(bigbuf, bigcheck, bigsize));
@@ -5329,6 +5350,13 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
53295350
dmu_buf_t *bonus_db;
53305351
arc_buf_t **bigbuf_arcbufs;
53315352
dmu_object_info_t doi;
5353+
uint32_t dmu_read_flags = DMU_READ_PREFETCH;
5354+
5355+
/*
5356+
* We will randomly set when to do O_DIRECT on a read.
5357+
*/
5358+
if (ztest_random(4) == 0)
5359+
dmu_read_flags |= DMU_DIRECTIO;
53325360

53335361
size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
53345362
od = umem_alloc(size, UMEM_NOFAIL);
@@ -5459,10 +5487,10 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
54595487
*/
54605488
if (i != 0 || ztest_random(2) != 0) {
54615489
error = dmu_read(os, packobj, packoff,
5462-
packsize, packbuf, DMU_READ_PREFETCH);
5490+
packsize, packbuf, dmu_read_flags);
54635491
ASSERT0(error);
54645492
error = dmu_read(os, bigobj, bigoff, bigsize,
5465-
bigbuf, DMU_READ_PREFETCH);
5493+
bigbuf, dmu_read_flags);
54665494
ASSERT0(error);
54675495
}
54685496
compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
@@ -5522,9 +5550,9 @@ ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
55225550
void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
55235551

55245552
VERIFY0(dmu_read(os, packobj, packoff,
5525-
packsize, packcheck, DMU_READ_PREFETCH));
5553+
packsize, packcheck, dmu_read_flags));
55265554
VERIFY0(dmu_read(os, bigobj, bigoff,
5527-
bigsize, bigcheck, DMU_READ_PREFETCH));
5555+
bigsize, bigcheck, dmu_read_flags));
55285556

55295557
ASSERT0(memcmp(packbuf, packcheck, packsize));
55305558
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)