Skip to content

Commit a5864c9

Browse files
committed
NFS: Do not serialise O_DIRECT reads and writes
Allow dio requests to be scheduled in parallel, but ensuring that they do not conflict with buffered I/O. Signed-off-by: Trond Myklebust <[email protected]>
1 parent 1829065 commit a5864c9

File tree

6 files changed

+174
-37
lines changed

6 files changed

+174
-37
lines changed

fs/nfs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ obj-$(CONFIG_NFS_FS) += nfs.o
66

77
CFLAGS_nfstrace.o += -I$(src)
88
nfs-y := client.o dir.o file.o getroot.o inode.o super.o \
9-
direct.o pagelist.o read.o symlink.o unlink.o \
9+
io.o direct.o pagelist.o read.o symlink.o unlink.o \
1010
write.o namespace.o mount_clnt.o nfstrace.o
1111
nfs-$(CONFIG_ROOT_NFS) += nfsroot.o
1212
nfs-$(CONFIG_SYSCTL) += sysctl.o

fs/nfs/direct.c

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -578,17 +578,12 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
578578
if (!count)
579579
goto out;
580580

581-
inode_lock(inode);
582-
result = nfs_sync_mapping(mapping);
583-
if (result)
584-
goto out_unlock;
585-
586581
task_io_account_read(count);
587582

588583
result = -ENOMEM;
589584
dreq = nfs_direct_req_alloc();
590585
if (dreq == NULL)
591-
goto out_unlock;
586+
goto out;
592587

593588
dreq->inode = inode;
594589
dreq->bytes_left = dreq->max_count = count;
@@ -603,24 +598,21 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
603598
if (!is_sync_kiocb(iocb))
604599
dreq->iocb = iocb;
605600

601+
nfs_start_io_direct(inode);
602+
606603
NFS_I(inode)->read_io += count;
607604
result = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
608605

609-
inode_unlock(inode);
606+
nfs_end_io_direct(inode);
610607

611608
if (!result) {
612609
result = nfs_direct_wait(dreq);
613610
if (result > 0)
614611
iocb->ki_pos += result;
615612
}
616613

617-
nfs_direct_req_release(dreq);
618-
return result;
619-
620614
out_release:
621615
nfs_direct_req_release(dreq);
622-
out_unlock:
623-
inode_unlock(inode);
624616
out:
625617
return result;
626618
}
@@ -1008,25 +1000,12 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
10081000
pos = iocb->ki_pos;
10091001
end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
10101002

1011-
inode_lock(inode);
1012-
1013-
result = nfs_sync_mapping(mapping);
1014-
if (result)
1015-
goto out_unlock;
1016-
1017-
if (mapping->nrpages) {
1018-
result = invalidate_inode_pages2_range(mapping,
1019-
pos >> PAGE_SHIFT, end);
1020-
if (result)
1021-
goto out_unlock;
1022-
}
1023-
10241003
task_io_account_write(count);
10251004

10261005
result = -ENOMEM;
10271006
dreq = nfs_direct_req_alloc();
10281007
if (!dreq)
1029-
goto out_unlock;
1008+
goto out;
10301009

10311010
dreq->inode = inode;
10321011
dreq->bytes_left = dreq->max_count = count;
@@ -1041,14 +1020,16 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
10411020
if (!is_sync_kiocb(iocb))
10421021
dreq->iocb = iocb;
10431022

1023+
nfs_start_io_direct(inode);
1024+
10441025
result = nfs_direct_write_schedule_iovec(dreq, iter, pos);
10451026

10461027
if (mapping->nrpages) {
10471028
invalidate_inode_pages2_range(mapping,
10481029
pos >> PAGE_SHIFT, end);
10491030
}
10501031

1051-
inode_unlock(inode);
1032+
nfs_end_io_direct(inode);
10521033

10531034
if (!result) {
10541035
result = nfs_direct_wait(dreq);
@@ -1058,13 +1039,9 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
10581039
generic_write_sync(iocb, result);
10591040
}
10601041
}
1061-
nfs_direct_req_release(dreq);
1062-
return result;
1063-
10641042
out_release:
10651043
nfs_direct_req_release(dreq);
1066-
out_unlock:
1067-
inode_unlock(inode);
1044+
out:
10681045
return result;
10691046
}
10701047

fs/nfs/file.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
170170
iocb->ki_filp,
171171
iov_iter_count(to), (unsigned long) iocb->ki_pos);
172172

173-
result = nfs_revalidate_mapping_protected(inode, iocb->ki_filp->f_mapping);
173+
nfs_start_io_read(inode);
174+
result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
174175
if (!result) {
175176
result = generic_file_read_iter(iocb, to);
176177
if (result > 0)
177178
nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
178179
}
180+
nfs_end_io_read(inode);
179181
return result;
180182
}
181183
EXPORT_SYMBOL_GPL(nfs_file_read);
@@ -191,12 +193,14 @@ nfs_file_splice_read(struct file *filp, loff_t *ppos,
191193
dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n",
192194
filp, (unsigned long) count, (unsigned long long) *ppos);
193195

194-
res = nfs_revalidate_mapping_protected(inode, filp->f_mapping);
196+
nfs_start_io_read(inode);
197+
res = nfs_revalidate_mapping(inode, filp->f_mapping);
195198
if (!res) {
196199
res = generic_file_splice_read(filp, ppos, pipe, count, flags);
197200
if (res > 0)
198201
nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
199202
}
203+
nfs_end_io_read(inode);
200204
return res;
201205
}
202206
EXPORT_SYMBOL_GPL(nfs_file_splice_read);
@@ -645,14 +649,14 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
645649
goto out;
646650
}
647651

648-
inode_lock(inode);
652+
nfs_start_io_write(inode);
649653
result = generic_write_checks(iocb, from);
650654
if (result > 0) {
651655
current->backing_dev_info = inode_to_bdi(inode);
652656
result = generic_perform_write(file, from, iocb->ki_pos);
653657
current->backing_dev_info = NULL;
654658
}
655-
inode_unlock(inode);
659+
nfs_end_io_write(inode);
656660
if (result <= 0)
657661
goto out;
658662

fs/nfs/internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,14 @@ extern void __exit unregister_nfs_fs(void);
411411
extern bool nfs_sb_active(struct super_block *sb);
412412
extern void nfs_sb_deactive(struct super_block *sb);
413413

414+
/* io.c */
415+
extern void nfs_start_io_read(struct inode *inode);
416+
extern void nfs_end_io_read(struct inode *inode);
417+
extern void nfs_start_io_write(struct inode *inode);
418+
extern void nfs_end_io_write(struct inode *inode);
419+
extern void nfs_start_io_direct(struct inode *inode);
420+
extern void nfs_end_io_direct(struct inode *inode);
421+
414422
/* namespace.c */
415423
#define NFS_PATH_CANONICAL 1
416424
extern char *nfs_path(char **p, struct dentry *dentry,

fs/nfs/io.c

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2016 Trond Myklebust
3+
*
4+
* I/O and data path helper functionality.
5+
*/
6+
7+
#include <linux/types.h>
8+
#include <linux/kernel.h>
9+
#include <linux/bitops.h>
10+
#include <linux/rwsem.h>
11+
#include <linux/fs.h>
12+
#include <linux/nfs_fs.h>
13+
14+
#include "internal.h"
15+
16+
/* Call with exclusively locked inode->i_rwsem */
17+
static void nfs_block_o_direct(struct nfs_inode *nfsi, struct inode *inode)
18+
{
19+
if (test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
20+
clear_bit(NFS_INO_ODIRECT, &nfsi->flags);
21+
inode_dio_wait(inode);
22+
}
23+
}
24+
25+
/**
26+
* nfs_start_io_read - declare the file is being used for buffered reads
27+
* @inode - file inode
28+
*
29+
* Declare that a buffered read operation is about to start, and ensure
30+
* that we block all direct I/O.
31+
* On exit, the function ensures that the NFS_INO_ODIRECT flag is unset,
32+
* and holds a shared lock on inode->i_rwsem to ensure that the flag
33+
* cannot be changed.
34+
* In practice, this means that buffered read operations are allowed to
35+
* execute in parallel, thanks to the shared lock, whereas direct I/O
36+
* operations need to wait to grab an exclusive lock in order to set
37+
* NFS_INO_ODIRECT.
38+
* Note that buffered writes and truncates both take a write lock on
39+
* inode->i_rwsem, meaning that those are serialised w.r.t. the reads.
40+
*/
41+
void
42+
nfs_start_io_read(struct inode *inode)
43+
{
44+
struct nfs_inode *nfsi = NFS_I(inode);
45+
/* Be an optimist! */
46+
down_read(&inode->i_rwsem);
47+
if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0)
48+
return;
49+
up_read(&inode->i_rwsem);
50+
/* Slow path.... */
51+
down_write(&inode->i_rwsem);
52+
nfs_block_o_direct(nfsi, inode);
53+
downgrade_write(&inode->i_rwsem);
54+
}
55+
56+
/**
57+
* nfs_end_io_read - declare that the buffered read operation is done
58+
* @inode - file inode
59+
*
60+
* Declare that a buffered read operation is done, and release the shared
61+
* lock on inode->i_rwsem.
62+
*/
63+
void
64+
nfs_end_io_read(struct inode *inode)
65+
{
66+
up_read(&inode->i_rwsem);
67+
}
68+
69+
/**
70+
* nfs_start_io_write - declare the file is being used for buffered writes
71+
* @inode - file inode
72+
*
73+
* Declare that a buffered read operation is about to start, and ensure
74+
* that we block all direct I/O.
75+
*/
76+
void
77+
nfs_start_io_write(struct inode *inode)
78+
{
79+
down_write(&inode->i_rwsem);
80+
nfs_block_o_direct(NFS_I(inode), inode);
81+
}
82+
83+
/**
84+
* nfs_end_io_write - declare that the buffered write operation is done
85+
* @inode - file inode
86+
*
87+
* Declare that a buffered write operation is done, and release the
88+
* lock on inode->i_rwsem.
89+
*/
90+
void
91+
nfs_end_io_write(struct inode *inode)
92+
{
93+
up_write(&inode->i_rwsem);
94+
}
95+
96+
/* Call with exclusively locked inode->i_rwsem */
97+
static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
98+
{
99+
if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
100+
set_bit(NFS_INO_ODIRECT, &nfsi->flags);
101+
nfs_wb_all(inode);
102+
}
103+
}
104+
105+
/**
106+
* nfs_end_io_direct - declare the file is being used for direct i/o
107+
* @inode - file inode
108+
*
109+
* Declare that a direct I/O operation is about to start, and ensure
110+
* that we block all buffered I/O.
111+
* On exit, the function ensures that the NFS_INO_ODIRECT flag is set,
112+
* and holds a shared lock on inode->i_rwsem to ensure that the flag
113+
* cannot be changed.
114+
* In practice, this means that direct I/O operations are allowed to
115+
* execute in parallel, thanks to the shared lock, whereas buffered I/O
116+
* operations need to wait to grab an exclusive lock in order to clear
117+
* NFS_INO_ODIRECT.
118+
* Note that buffered writes and truncates both take a write lock on
119+
* inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
120+
*/
121+
void
122+
nfs_start_io_direct(struct inode *inode)
123+
{
124+
struct nfs_inode *nfsi = NFS_I(inode);
125+
/* Be an optimist! */
126+
down_read(&inode->i_rwsem);
127+
if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) != 0)
128+
return;
129+
up_read(&inode->i_rwsem);
130+
/* Slow path.... */
131+
down_write(&inode->i_rwsem);
132+
nfs_block_buffered(nfsi, inode);
133+
downgrade_write(&inode->i_rwsem);
134+
}
135+
136+
/**
137+
* nfs_end_io_direct - declare that the direct i/o operation is done
138+
* @inode - file inode
139+
*
140+
* Declare that a direct I/O operation is done, and release the shared
141+
* lock on inode->i_rwsem.
142+
*/
143+
void
144+
nfs_end_io_direct(struct inode *inode)
145+
{
146+
up_read(&inode->i_rwsem);
147+
}

include/linux/nfs_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct nfs_inode {
210210
#define NFS_INO_LAYOUTCOMMIT (9) /* layoutcommit required */
211211
#define NFS_INO_LAYOUTCOMMITTING (10) /* layoutcommit inflight */
212212
#define NFS_INO_LAYOUTSTATS (11) /* layoutstats inflight */
213+
#define NFS_INO_ODIRECT (12) /* I/O setting is O_DIRECT */
213214

214215
static inline struct nfs_inode *NFS_I(const struct inode *inode)
215216
{

0 commit comments

Comments
 (0)