xref: /openbmc/linux/fs/nfs/direct.c (revision be2fd1560eb57b7298aa3c258ddcca0d53ecdea3)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * There are important applications whose performance or correctness
101da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
111da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
121da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
131da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
141da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
151da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
161da177e4SLinus Torvalds  * need to cache the contents of a file.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
191da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
201da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
211da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
221da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
231da177e4SLinus Torvalds  * an application.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
261da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
271da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
281da177e4SLinus Torvalds  * an undocumented mount option.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
311da177e4SLinus Torvalds  * help from Andrew Morton.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
341da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
351da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
361da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
371da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3888467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/file.h>
461da177e4SLinus Torvalds #include <linux/pagemap.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
497ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h>
506296556fSPeng Tao #include <linux/module.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/nfs_fs.h>
531da177e4SLinus Torvalds #include <linux/nfs_page.h>
541da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
551da177e4SLinus Torvalds 
567c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
5760063497SArun Sharma #include <linux/atomic.h>
581da177e4SLinus Torvalds 
598d5658c9STrond Myklebust #include "internal.h"
6091d5b470SChuck Lever #include "iostat.h"
611763da12SFred Isaman #include "pnfs.h"
62a6b5a28eSDave Wysochanski #include "fscache.h"
638efc4bbeSJeff Layton #include "nfstrace.h"
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
661da177e4SLinus Torvalds 
67e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
681da177e4SLinus Torvalds 
691763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
701763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
714d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
721763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
73607f31e8STrond Myklebust 
74607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
75607f31e8STrond Myklebust {
76607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
77607f31e8STrond Myklebust }
78607f31e8STrond Myklebust 
79607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
80607f31e8STrond Myklebust {
81607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
82607f31e8STrond Myklebust }
83607f31e8STrond Myklebust 
840a00b77bSWeston Andros Adamson static void
85031d73edSTrond Myklebust nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
86031d73edSTrond Myklebust 			    const struct nfs_pgio_header *hdr,
87031d73edSTrond Myklebust 			    ssize_t dreq_len)
880a00b77bSWeston Andros Adamson {
89031d73edSTrond Myklebust 	if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
90031d73edSTrond Myklebust 	      test_bit(NFS_IOHDR_EOF, &hdr->flags)))
91031d73edSTrond Myklebust 		return;
92031d73edSTrond Myklebust 	if (dreq->max_count >= dreq_len) {
93031d73edSTrond Myklebust 		dreq->max_count = dreq_len;
94031d73edSTrond Myklebust 		if (dreq->count > dreq_len)
95031d73edSTrond Myklebust 			dreq->count = dreq_len;
96ed3743a6SWeston Andros Adamson 
97031d73edSTrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags))
98031d73edSTrond Myklebust 			dreq->error = hdr->error;
99031d73edSTrond Myklebust 		else /* Clear outstanding error if this is EOF */
100031d73edSTrond Myklebust 			dreq->error = 0;
1015fadeb47SPeng Tao 	}
1020a00b77bSWeston Andros Adamson }
103031d73edSTrond Myklebust 
104031d73edSTrond Myklebust static void
105031d73edSTrond Myklebust nfs_direct_count_bytes(struct nfs_direct_req *dreq,
106031d73edSTrond Myklebust 		       const struct nfs_pgio_header *hdr)
107031d73edSTrond Myklebust {
108031d73edSTrond Myklebust 	loff_t hdr_end = hdr->io_start + hdr->good_bytes;
109031d73edSTrond Myklebust 	ssize_t dreq_len = 0;
110031d73edSTrond Myklebust 
111031d73edSTrond Myklebust 	if (hdr_end > dreq->io_start)
112031d73edSTrond Myklebust 		dreq_len = hdr_end - dreq->io_start;
113031d73edSTrond Myklebust 
114031d73edSTrond Myklebust 	nfs_direct_handle_truncated(dreq, hdr, dreq_len);
115031d73edSTrond Myklebust 
116031d73edSTrond Myklebust 	if (dreq_len > dreq->max_count)
117031d73edSTrond Myklebust 		dreq_len = dreq->max_count;
118031d73edSTrond Myklebust 
119031d73edSTrond Myklebust 	if (dreq->count < dreq_len)
120031d73edSTrond Myklebust 		dreq->count = dreq_len;
1211ccbad9fSPeng Tao }
1220a00b77bSWeston Andros Adamson 
1231da177e4SLinus Torvalds /**
124eb79f3afSNeilBrown  * nfs_swap_rw - NFS address space operation for swap I/O
125b8a32e2bSChuck Lever  * @iocb: target I/O control block
12690090ae6SAl Viro  * @iter: I/O buffer
127b8a32e2bSChuck Lever  *
128eb79f3afSNeilBrown  * Perform IO to the swap-file.  This is much like direct IO.
1291da177e4SLinus Torvalds  */
130eb79f3afSNeilBrown int nfs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
131b8a32e2bSChuck Lever {
132eb79f3afSNeilBrown 	ssize_t ret;
133ee8a1a8bSPeng Tao 
13466ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
135a564b8f0SMel Gorman 
1366f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
137eb79f3afSNeilBrown 		ret = nfs_file_direct_read(iocb, iter, true);
138eb79f3afSNeilBrown 	else
139eb79f3afSNeilBrown 		ret = nfs_file_direct_write(iocb, iter, true);
140eb79f3afSNeilBrown 	if (ret < 0)
141eb79f3afSNeilBrown 		return ret;
142eb79f3afSNeilBrown 	return 0;
143b8a32e2bSChuck Lever }
144b8a32e2bSChuck Lever 
145749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1469c93ab7dSChuck Lever {
147749e146eSChuck Lever 	unsigned int i;
148607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
14909cbfeafSKirill A. Shutemov 		put_page(pages[i]);
1506b45d858STrond Myklebust }
1516b45d858STrond Myklebust 
1521763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
1531763da12SFred Isaman 			      struct nfs_direct_req *dreq)
1541763da12SFred Isaman {
155fe238e60SDave Wysochanski 	cinfo->inode = dreq->inode;
1561763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
1571763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
1581763da12SFred Isaman 	cinfo->dreq = dreq;
1591763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
1601763da12SFred Isaman }
1611763da12SFred Isaman 
16293619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1651da177e4SLinus Torvalds 
166292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
1671da177e4SLinus Torvalds 	if (!dreq)
1681da177e4SLinus Torvalds 		return NULL;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	kref_init(&dreq->kref);
171607f31e8STrond Myklebust 	kref_get(&dreq->kref);
172d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1731763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
174c21e7168STrond Myklebust 	pnfs_init_ds_commit_info(&dreq->ds_cinfo);
1751763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
17615ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
17793619e59SChuck Lever 
17893619e59SChuck Lever 	return dreq;
17993619e59SChuck Lever }
18093619e59SChuck Lever 
181b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
184a8881f5aSTrond Myklebust 
18518f41296STrond Myklebust 	pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode);
186f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
187f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
188a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
189a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1901da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
194b4946ffbSTrond Myklebust {
195b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
196b4946ffbSTrond Myklebust }
197b4946ffbSTrond Myklebust 
1986296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
1996296556fSPeng Tao {
2006296556fSPeng Tao 	return dreq->bytes_left;
2016296556fSPeng Tao }
2026296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
2036296556fSPeng Tao 
204d4cc948bSChuck Lever /*
205bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
206bc0fb201SChuck Lever  */
207bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
208bc0fb201SChuck Lever {
20915ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
210bc0fb201SChuck Lever 
211bc0fb201SChuck Lever 	/* Async requests don't wait here */
212bc0fb201SChuck Lever 	if (dreq->iocb)
213bc0fb201SChuck Lever 		goto out;
214bc0fb201SChuck Lever 
215150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
216bc0fb201SChuck Lever 
217d2a7de0bSTrond Myklebust 	if (!result) {
218d2a7de0bSTrond Myklebust 		result = dreq->count;
219d2a7de0bSTrond Myklebust 		WARN_ON_ONCE(dreq->count < 0);
220d2a7de0bSTrond Myklebust 	}
221bc0fb201SChuck Lever 	if (!result)
22215ce4a0cSChuck Lever 		result = dreq->error;
223bc0fb201SChuck Lever 
224bc0fb201SChuck Lever out:
225bc0fb201SChuck Lever 	return (ssize_t) result;
226bc0fb201SChuck Lever }
227bc0fb201SChuck Lever 
228bc0fb201SChuck Lever /*
229607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
230607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
23163ab46abSChuck Lever  */
232f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq)
23363ab46abSChuck Lever {
2349811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
2359811cd57SChristoph Hellwig 
23665caafd0SOlga Kornievskaia 	inode_dio_end(inode);
23765caafd0SOlga Kornievskaia 
2382a009ec9SChristoph Hellwig 	if (dreq->iocb) {
2392a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
240d2a7de0bSTrond Myklebust 		if (dreq->count != 0) {
2412a009ec9SChristoph Hellwig 			res = (long) dreq->count;
242d2a7de0bSTrond Myklebust 			WARN_ON_ONCE(dreq->count < 0);
243d2a7de0bSTrond Myklebust 		}
2446b19b766SJens Axboe 		dreq->iocb->ki_complete(dreq->iocb, res);
245d72b7a6bSTrond Myklebust 	}
2462a009ec9SChristoph Hellwig 
247024de8f1SDaniel Wagner 	complete(&dreq->completion);
24863ab46abSChuck Lever 
249b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
25063ab46abSChuck Lever }
25163ab46abSChuck Lever 
252584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
253fdd1e74cSTrond Myklebust {
254584aa810SFred Isaman 	unsigned long bytes = 0;
255584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
256fdd1e74cSTrond Myklebust 
25715ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
258eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
259eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
260eb2c50daSTrond Myklebust 		goto out_put;
261eb2c50daSTrond Myklebust 	}
262eb2c50daSTrond Myklebust 
263031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
26415ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2651da177e4SLinus Torvalds 
266584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
267584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
268584aa810SFred Isaman 		struct page *page = req->wb_page;
269584aa810SFred Isaman 
270ad3cba22SDave Kleikamp 		if (!PageCompound(page) && bytes < hdr->good_bytes &&
271ad3cba22SDave Kleikamp 		    (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
2724bd8b010STrond Myklebust 			set_page_dirty(page);
273584aa810SFred Isaman 		bytes += req->wb_bytes;
274584aa810SFred Isaman 		nfs_list_remove_request(req);
275beeb5338SAnna Schumaker 		nfs_release_request(req);
276584aa810SFred Isaman 	}
277584aa810SFred Isaman out_put:
278607f31e8STrond Myklebust 	if (put_dreq(dreq))
279f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
280584aa810SFred Isaman 	hdr->release(hdr);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
283df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error)
284cd841605SFred Isaman {
285584aa810SFred Isaman 	struct nfs_page *req;
286cd841605SFred Isaman 
287584aa810SFred Isaman 	while (!list_empty(head)) {
288584aa810SFred Isaman 		req = nfs_list_entry(head->next);
289584aa810SFred Isaman 		nfs_list_remove_request(req);
290584aa810SFred Isaman 		nfs_release_request(req);
291cd841605SFred Isaman 	}
292584aa810SFred Isaman }
293584aa810SFred Isaman 
294584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
295584aa810SFred Isaman {
296584aa810SFred Isaman 	get_dreq(hdr->dreq);
297584aa810SFred Isaman }
298584aa810SFred Isaman 
299584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
3003e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
301584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
302584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
303584aa810SFred Isaman };
304cd841605SFred Isaman 
305d4cc948bSChuck Lever /*
306607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
307607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
308607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
309607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
310607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3111da177e4SLinus Torvalds  */
31291f79c43SAl Viro 
31391f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
31491f79c43SAl Viro 					      struct iov_iter *iter,
31591f79c43SAl Viro 					      loff_t pos)
3161da177e4SLinus Torvalds {
31791f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
31891f79c43SAl Viro 	struct inode *inode = dreq->inode;
31991f79c43SAl Viro 	ssize_t result = -EINVAL;
32091f79c43SAl Viro 	size_t requested_bytes = 0;
32191f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
32282b145c5SChuck Lever 
32316b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
32491f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
32591f79c43SAl Viro 	get_dreq(dreq);
32691f79c43SAl Viro 	desc.pg_dreq = dreq;
327fe0f07d0SJens Axboe 	inode_dio_begin(inode);
32891f79c43SAl Viro 
32991f79c43SAl Viro 	while (iov_iter_count(iter)) {
33091f79c43SAl Viro 		struct page **pagevec;
3315dd602f2SChuck Lever 		size_t bytes;
33291f79c43SAl Viro 		size_t pgbase;
33391f79c43SAl Viro 		unsigned npages, i;
3341da177e4SLinus Torvalds 
3351ef255e2SAl Viro 		result = iov_iter_get_pages_alloc2(iter, &pagevec,
33691f79c43SAl Viro 						  rsize, &pgbase);
337584aa810SFred Isaman 		if (result < 0)
338749e146eSChuck Lever 			break;
339a564b8f0SMel Gorman 
34091f79c43SAl Viro 		bytes = result;
34191f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
342584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
343584aa810SFred Isaman 			struct nfs_page *req;
344bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
345584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
34670e9db69STrond Myklebust 			req = nfs_page_create_from_page(dreq->ctx, pagevec[i],
34770e9db69STrond Myklebust 							pgbase, pos, req_len);
348584aa810SFred Isaman 			if (IS_ERR(req)) {
349584aa810SFred Isaman 				result = PTR_ERR(req);
350dbae4c73STrond Myklebust 				break;
351584aa810SFred Isaman 			}
35291f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
35391f79c43SAl Viro 				result = desc.pg_error;
354584aa810SFred Isaman 				nfs_release_request(req);
355584aa810SFred Isaman 				break;
356584aa810SFred Isaman 			}
357584aa810SFred Isaman 			pgbase = 0;
358584aa810SFred Isaman 			bytes -= req_len;
35991f79c43SAl Viro 			requested_bytes += req_len;
360584aa810SFred Isaman 			pos += req_len;
36135754bc0SPeng Tao 			dreq->bytes_left -= req_len;
362584aa810SFred Isaman 		}
3636d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
36491f79c43SAl Viro 		kvfree(pagevec);
36519f73787SChuck Lever 		if (result < 0)
36619f73787SChuck Lever 			break;
36719f73787SChuck Lever 	}
36819f73787SChuck Lever 
369584aa810SFred Isaman 	nfs_pageio_complete(&desc);
370584aa810SFred Isaman 
371839f7ad6SChuck Lever 	/*
372839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
373839f7ad6SChuck Lever 	 * generic layer handle the completion.
374839f7ad6SChuck Lever 	 */
375839f7ad6SChuck Lever 	if (requested_bytes == 0) {
376d03727b2SOlga Kornievskaia 		inode_dio_end(inode);
37765caafd0SOlga Kornievskaia 		nfs_direct_req_release(dreq);
378839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
379839f7ad6SChuck Lever 	}
380839f7ad6SChuck Lever 
38119f73787SChuck Lever 	if (put_dreq(dreq))
382f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
38385128b2bSAl Viro 	return requested_bytes;
38419f73787SChuck Lever }
38519f73787SChuck Lever 
38614a3ec79SChristoph Hellwig /**
38714a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
38814a3ec79SChristoph Hellwig  * @iocb: target I/O control block
389619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
39064158668SNeilBrown  * @swap: flag indicating this is swap IO, not O_DIRECT IO
39114a3ec79SChristoph Hellwig  *
39214a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
39314a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
39414a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
39514a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
39614a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
39714a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
39814a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
39914a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
40014a3ec79SChristoph Hellwig  *
40114a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
40214a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
40314a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
40414a3ec79SChristoph Hellwig  * cache.
40514a3ec79SChristoph Hellwig  */
40664158668SNeilBrown ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
40764158668SNeilBrown 			     bool swap)
4081da177e4SLinus Torvalds {
40914a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
41014a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
41114a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
4121da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
413b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
41486b93667SColin Ian King 	ssize_t result, requested;
415a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
41614a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
41714a3ec79SChristoph Hellwig 
41814a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
419c8b8e32dSChristoph Hellwig 		file, count, (long long) iocb->ki_pos);
42014a3ec79SChristoph Hellwig 
42114a3ec79SChristoph Hellwig 	result = 0;
42214a3ec79SChristoph Hellwig 	if (!count)
42314a3ec79SChristoph Hellwig 		goto out;
42414a3ec79SChristoph Hellwig 
42514a3ec79SChristoph Hellwig 	task_io_account_read(count);
42614a3ec79SChristoph Hellwig 
42714a3ec79SChristoph Hellwig 	result = -ENOMEM;
428607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
429f11ac8dbSTrond Myklebust 	if (dreq == NULL)
430a5864c99STrond Myklebust 		goto out;
4311da177e4SLinus Torvalds 
43291d5b470SChuck Lever 	dreq->inode = inode;
433ed3743a6SWeston Andros Adamson 	dreq->bytes_left = dreq->max_count = count;
434c8b8e32dSChristoph Hellwig 	dreq->io_start = iocb->ki_pos;
435cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
436b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
437b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
438b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
4398605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
440f11ac8dbSTrond Myklebust 		goto out_release;
441b3c54de6STrond Myklebust 	}
442b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
443487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
444487b8372SChuck Lever 		dreq->iocb = iocb;
4451da177e4SLinus Torvalds 
446fcb14cb1SAl Viro 	if (user_backed_iter(iter))
447ad3cba22SDave Kleikamp 		dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
448ad3cba22SDave Kleikamp 
44964158668SNeilBrown 	if (!swap)
450a5864c99STrond Myklebust 		nfs_start_io_direct(inode);
451a5864c99STrond Myklebust 
452619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
45385128b2bSAl Viro 	requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
454d0b9875dSChristoph Hellwig 
45564158668SNeilBrown 	if (!swap)
456a5864c99STrond Myklebust 		nfs_end_io_direct(inode);
457d0b9875dSChristoph Hellwig 
45885128b2bSAl Viro 	if (requested > 0) {
459bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
46085128b2bSAl Viro 		if (result > 0) {
46185128b2bSAl Viro 			requested -= result;
462c8b8e32dSChristoph Hellwig 			iocb->ki_pos += result;
46314a3ec79SChristoph Hellwig 		}
46485128b2bSAl Viro 		iov_iter_revert(iter, requested);
46585128b2bSAl Viro 	} else {
46685128b2bSAl Viro 		result = requested;
46785128b2bSAl Viro 	}
468d0b9875dSChristoph Hellwig 
469f11ac8dbSTrond Myklebust out_release:
470b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
471f11ac8dbSTrond Myklebust out:
4721da177e4SLinus Torvalds 	return result;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
475*be2fd156STrond Myklebust static void nfs_direct_join_group(struct list_head *list, struct inode *inode)
476ed5d588fSTrond Myklebust {
477*be2fd156STrond Myklebust 	struct nfs_page *req, *subreq;
478ed5d588fSTrond Myklebust 
479ed5d588fSTrond Myklebust 	list_for_each_entry(req, list, wb_list) {
480*be2fd156STrond Myklebust 		if (req->wb_head != req)
481ed5d588fSTrond Myklebust 			continue;
482*be2fd156STrond Myklebust 		subreq = req->wb_this_page;
483*be2fd156STrond Myklebust 		if (subreq == req)
484*be2fd156STrond Myklebust 			continue;
485*be2fd156STrond Myklebust 		do {
486*be2fd156STrond Myklebust 			/*
487*be2fd156STrond Myklebust 			 * Remove subrequests from this list before freeing
488*be2fd156STrond Myklebust 			 * them in the call to nfs_join_page_group().
489*be2fd156STrond Myklebust 			 */
490*be2fd156STrond Myklebust 			if (!list_empty(&subreq->wb_list)) {
491*be2fd156STrond Myklebust 				nfs_list_remove_request(subreq);
492*be2fd156STrond Myklebust 				nfs_release_request(subreq);
493ed5d588fSTrond Myklebust 			}
494*be2fd156STrond Myklebust 		} while ((subreq = subreq->wb_this_page) != req);
495ed5d588fSTrond Myklebust 		nfs_join_page_group(req, inode);
496ed5d588fSTrond Myklebust 	}
497ed5d588fSTrond Myklebust }
498ed5d588fSTrond Myklebust 
499ed5d588fSTrond Myklebust static void
500085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
501085d1e33STom Haynes 				  struct list_head *list,
502085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
503085d1e33STom Haynes {
504e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
5059c455a8cSTrond Myklebust 	pnfs_recover_commit_reqs(list, cinfo);
506085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
507e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
508085d1e33STom Haynes }
509085d1e33STom Haynes 
510fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
5111da177e4SLinus Torvalds {
5121763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
5131763da12SFred Isaman 	struct nfs_page *req, *tmp;
5141763da12SFred Isaman 	LIST_HEAD(reqs);
5151763da12SFred Isaman 	struct nfs_commit_info cinfo;
5161763da12SFred Isaman 	LIST_HEAD(failed);
5171763da12SFred Isaman 
5181763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
519085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
5201da177e4SLinus Torvalds 
521ed5d588fSTrond Myklebust 	nfs_direct_join_group(&reqs, dreq->inode);
522ed5d588fSTrond Myklebust 
523fad61490STrond Myklebust 	dreq->count = 0;
524031d73edSTrond Myklebust 	dreq->max_count = 0;
525031d73edSTrond Myklebust 	list_for_each_entry(req, &reqs, wb_list)
526031d73edSTrond Myklebust 		dreq->max_count += req->wb_bytes;
527a5314a74STrond Myklebust 	nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
528607f31e8STrond Myklebust 	get_dreq(dreq);
5291da177e4SLinus Torvalds 
530a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
5311763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
5321763da12SFred Isaman 	desc.pg_dreq = dreq;
533607f31e8STrond Myklebust 
5341763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
53533344e0fSTrond Myklebust 		/* Bump the transmission count */
53633344e0fSTrond Myklebust 		req->wb_nio++;
5371763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
538078b5fd9STrond Myklebust 			nfs_list_move_request(req, &failed);
539fe238e60SDave Wysochanski 			spin_lock(&cinfo.inode->i_lock);
5401763da12SFred Isaman 			dreq->flags = 0;
541d600ad1fSPeng Tao 			if (desc.pg_error < 0)
542d600ad1fSPeng Tao 				dreq->error = desc.pg_error;
543d600ad1fSPeng Tao 			else
5441763da12SFred Isaman 				dreq->error = -EIO;
545fe238e60SDave Wysochanski 			spin_unlock(&cinfo.inode->i_lock);
5461763da12SFred Isaman 		}
5475a695da2STrond Myklebust 		nfs_release_request(req);
5481763da12SFred Isaman 	}
5491763da12SFred Isaman 	nfs_pageio_complete(&desc);
550607f31e8STrond Myklebust 
5514035c248STrond Myklebust 	while (!list_empty(&failed)) {
5524035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
5534035c248STrond Myklebust 		nfs_list_remove_request(req);
5541d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
5554035c248STrond Myklebust 	}
556607f31e8STrond Myklebust 
557607f31e8STrond Myklebust 	if (put_dreq(dreq))
5584d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
559fad61490STrond Myklebust }
5601da177e4SLinus Torvalds 
5611763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
562fad61490STrond Myklebust {
5631f28476dSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
5640b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
5651763da12SFred Isaman 	struct nfs_commit_info cinfo;
5661763da12SFred Isaman 	struct nfs_page *req;
567c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
568c9d8f89dSTrond Myklebust 
5698efc4bbeSJeff Layton 	trace_nfs_direct_commit_complete(dreq);
5708efc4bbeSJeff Layton 
571fb5f7f20STrond Myklebust 	if (status < 0) {
572fb5f7f20STrond Myklebust 		/* Errors in commit are fatal */
573fb5f7f20STrond Myklebust 		dreq->error = status;
574fb5f7f20STrond Myklebust 		dreq->max_count = 0;
575fb5f7f20STrond Myklebust 		dreq->count = 0;
576fb5f7f20STrond Myklebust 		dreq->flags = NFS_ODIRECT_DONE;
57755051c0cSJeff Layton 	} else {
578fb5f7f20STrond Myklebust 		status = dreq->error;
57955051c0cSJeff Layton 	}
580fb5f7f20STrond Myklebust 
5811763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
582fad61490STrond Myklebust 
5831763da12SFred Isaman 	while (!list_empty(&data->pages)) {
5841763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
5851763da12SFred Isaman 		nfs_list_remove_request(req);
5861f28476dSTrond Myklebust 		if (status >= 0 && !nfs_write_match_verf(verf, req)) {
5871f28476dSTrond Myklebust 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
58833344e0fSTrond Myklebust 			/*
58933344e0fSTrond Myklebust 			 * Despite the reboot, the write was successful,
59033344e0fSTrond Myklebust 			 * so reset wb_nio.
59133344e0fSTrond Myklebust 			 */
59233344e0fSTrond Myklebust 			req->wb_nio = 0;
593b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
5941f28476dSTrond Myklebust 		} else /* Error or match */
595906369e4SFred Isaman 			nfs_release_request(req);
5961d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
597fad61490STrond Myklebust 	}
598fad61490STrond Myklebust 
599133a48abSTrond Myklebust 	if (nfs_commit_end(cinfo.mds))
6004d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
6011763da12SFred Isaman }
6021763da12SFred Isaman 
603b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
604b20135d0STrond Myklebust 		struct nfs_page *req)
6051763da12SFred Isaman {
606b20135d0STrond Myklebust 	struct nfs_direct_req *dreq = cinfo->dreq;
607b20135d0STrond Myklebust 
6088efc4bbeSJeff Layton 	trace_nfs_direct_resched_write(dreq);
6098efc4bbeSJeff Layton 
610b20135d0STrond Myklebust 	spin_lock(&dreq->lock);
611fb5f7f20STrond Myklebust 	if (dreq->flags != NFS_ODIRECT_DONE)
612b20135d0STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
613b20135d0STrond Myklebust 	spin_unlock(&dreq->lock);
614b20135d0STrond Myklebust 	nfs_mark_request_commit(req, NULL, cinfo, 0);
6151763da12SFred Isaman }
6161763da12SFred Isaman 
6171763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
6181763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
619b20135d0STrond Myklebust 	.resched_write = nfs_direct_resched_write,
620fad61490STrond Myklebust };
621fad61490STrond Myklebust 
622fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
623fad61490STrond Myklebust {
6241763da12SFred Isaman 	int res;
6251763da12SFred Isaman 	struct nfs_commit_info cinfo;
6261763da12SFred Isaman 	LIST_HEAD(mds_list);
627fad61490STrond Myklebust 
6281763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6291763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
6301763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
6311763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
6321763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
635fb5f7f20STrond Myklebust static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq)
636fb5f7f20STrond Myklebust {
637fb5f7f20STrond Myklebust 	struct nfs_commit_info cinfo;
638fb5f7f20STrond Myklebust 	struct nfs_page *req;
639fb5f7f20STrond Myklebust 	LIST_HEAD(reqs);
640fb5f7f20STrond Myklebust 
641fb5f7f20STrond Myklebust 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
642fb5f7f20STrond Myklebust 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
643fb5f7f20STrond Myklebust 
644fb5f7f20STrond Myklebust 	while (!list_empty(&reqs)) {
645fb5f7f20STrond Myklebust 		req = nfs_list_entry(reqs.next);
646fb5f7f20STrond Myklebust 		nfs_list_remove_request(req);
647f02cec9dSTrond Myklebust 		nfs_release_request(req);
648fb5f7f20STrond Myklebust 		nfs_unlock_and_release_request(req);
649fb5f7f20STrond Myklebust 	}
650fb5f7f20STrond Myklebust }
651fb5f7f20STrond Myklebust 
6521763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
6531da177e4SLinus Torvalds {
6541763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
655fad61490STrond Myklebust 	int flags = dreq->flags;
6561da177e4SLinus Torvalds 
657fad61490STrond Myklebust 	dreq->flags = 0;
658fad61490STrond Myklebust 	switch (flags) {
659fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
660fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6611da177e4SLinus Torvalds 			break;
662fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
663fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6641da177e4SLinus Torvalds 			break;
6651da177e4SLinus Torvalds 		default:
666fb5f7f20STrond Myklebust 			nfs_direct_write_clear_reqs(dreq);
667f7b5c340STrond Myklebust 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
668f7b5c340STrond Myklebust 			nfs_direct_complete(dreq);
6691da177e4SLinus Torvalds 	}
670fad61490STrond Myklebust }
671fad61490STrond Myklebust 
6724d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
673fad61490STrond Myklebust {
6748efc4bbeSJeff Layton 	trace_nfs_direct_write_complete(dreq);
67546483c2eSNeilBrown 	queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
676fad61490STrond Myklebust }
6771763da12SFred Isaman 
6781763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
6791763da12SFred Isaman {
6801763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
6811763da12SFred Isaman 	struct nfs_commit_info cinfo;
6821763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6833731d44bSTrond Myklebust 	int flags = NFS_ODIRECT_DONE;
6841763da12SFred Isaman 
6858efc4bbeSJeff Layton 	trace_nfs_direct_write_completion(dreq);
6868efc4bbeSJeff Layton 
6871763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6881763da12SFred Isaman 
6891763da12SFred Isaman 	spin_lock(&dreq->lock);
690eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
691eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
692eb2c50daSTrond Myklebust 		goto out_put;
693eb2c50daSTrond Myklebust 	}
694eb2c50daSTrond Myklebust 
695031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
69669d96651SJeff Layton 	if (test_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags)) {
6973731d44bSTrond Myklebust 		if (!dreq->flags)
6981763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_DO_COMMIT;
6993731d44bSTrond Myklebust 		flags = dreq->flags;
7001763da12SFred Isaman 	}
7011763da12SFred Isaman 	spin_unlock(&dreq->lock);
7021763da12SFred Isaman 
7031763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7042bfc6e56SWeston Andros Adamson 
7051763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7061763da12SFred Isaman 		nfs_list_remove_request(req);
7073731d44bSTrond Myklebust 		if (flags == NFS_ODIRECT_DO_COMMIT) {
70804277086STrond Myklebust 			kref_get(&req->wb_kref);
709ba838a75SChuck Lever 			memcpy(&req->wb_verf, &hdr->verf.verifier,
710ba838a75SChuck Lever 			       sizeof(req->wb_verf));
711b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
712b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
7133731d44bSTrond Myklebust 		} else if (flags == NFS_ODIRECT_RESCHED_WRITES) {
7143731d44bSTrond Myklebust 			kref_get(&req->wb_kref);
7153731d44bSTrond Myklebust 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
7161763da12SFred Isaman 		}
7171d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7181763da12SFred Isaman 	}
7191763da12SFred Isaman 
7201763da12SFred Isaman out_put:
7211763da12SFred Isaman 	if (put_dreq(dreq))
7224d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
7231763da12SFred Isaman 	hdr->release(hdr);
7241763da12SFred Isaman }
7251763da12SFred Isaman 
726df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error)
7273e9e0ca3STrond Myklebust {
7283e9e0ca3STrond Myklebust 	struct nfs_page *req;
7293e9e0ca3STrond Myklebust 
7303e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
7313e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
7323e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
7331d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7343e9e0ca3STrond Myklebust 	}
7353e9e0ca3STrond Myklebust }
7363e9e0ca3STrond Myklebust 
737dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
738dc602dd7STrond Myklebust {
739dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
740dc602dd7STrond Myklebust 
7418efc4bbeSJeff Layton 	trace_nfs_direct_write_reschedule_io(dreq);
7428efc4bbeSJeff Layton 
743dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
744dc602dd7STrond Myklebust 	if (dreq->error == 0) {
745dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
746dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
747dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
7484daaeba9STrond Myklebust 		hdr->good_bytes = hdr->args.offset + hdr->args.count -
7494daaeba9STrond Myklebust 			hdr->io_start;
750dc602dd7STrond Myklebust 	}
751dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
752dc602dd7STrond Myklebust }
753dc602dd7STrond Myklebust 
7541763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
7553e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
7561763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
7571763da12SFred Isaman 	.completion = nfs_direct_write_completion,
758dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
7591763da12SFred Isaman };
7601763da12SFred Isaman 
76191f79c43SAl Viro 
76291f79c43SAl Viro /*
76391f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
76491f79c43SAl Viro  *     errors after the first one are ignored.
76591f79c43SAl Viro  */
76691f79c43SAl Viro /*
76791f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
76891f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
76991f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
77091f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
77191f79c43SAl Viro  * no requests have been sent, just return an error.
77291f79c43SAl Viro  */
77319f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
774619d30b4SAl Viro 					       struct iov_iter *iter,
775c265de25SNeilBrown 					       loff_t pos, int ioflags)
77619f73787SChuck Lever {
7771763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
7781d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
77919f73787SChuck Lever 	ssize_t result = 0;
78019f73787SChuck Lever 	size_t requested_bytes = 0;
78191f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
78219f73787SChuck Lever 
7838efc4bbeSJeff Layton 	trace_nfs_direct_write_schedule_iovec(dreq);
7848efc4bbeSJeff Layton 
785c265de25SNeilBrown 	nfs_pageio_init_write(&desc, inode, ioflags, false,
7861763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
7871763da12SFred Isaman 	desc.pg_dreq = dreq;
78819f73787SChuck Lever 	get_dreq(dreq);
789fe0f07d0SJens Axboe 	inode_dio_begin(inode);
79019f73787SChuck Lever 
79191f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
79291f79c43SAl Viro 	while (iov_iter_count(iter)) {
79391f79c43SAl Viro 		struct page **pagevec;
79491f79c43SAl Viro 		size_t bytes;
79591f79c43SAl Viro 		size_t pgbase;
79691f79c43SAl Viro 		unsigned npages, i;
79791f79c43SAl Viro 
7981ef255e2SAl Viro 		result = iov_iter_get_pages_alloc2(iter, &pagevec,
79991f79c43SAl Viro 						  wsize, &pgbase);
80019f73787SChuck Lever 		if (result < 0)
80119f73787SChuck Lever 			break;
80291f79c43SAl Viro 
80391f79c43SAl Viro 		bytes = result;
80491f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
80591f79c43SAl Viro 		for (i = 0; i < npages; i++) {
80691f79c43SAl Viro 			struct nfs_page *req;
80791f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
80891f79c43SAl Viro 
80970e9db69STrond Myklebust 			req = nfs_page_create_from_page(dreq->ctx, pagevec[i],
81070e9db69STrond Myklebust 							pgbase, pos, req_len);
81191f79c43SAl Viro 			if (IS_ERR(req)) {
81291f79c43SAl Viro 				result = PTR_ERR(req);
81319f73787SChuck Lever 				break;
81491f79c43SAl Viro 			}
8150a00b77bSWeston Andros Adamson 
816d600ad1fSPeng Tao 			if (desc.pg_error < 0) {
817d600ad1fSPeng Tao 				nfs_free_request(req);
818d600ad1fSPeng Tao 				result = desc.pg_error;
819d600ad1fSPeng Tao 				break;
820d600ad1fSPeng Tao 			}
8210a00b77bSWeston Andros Adamson 
82291f79c43SAl Viro 			nfs_lock_request(req);
82391f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
82491f79c43SAl Viro 				result = desc.pg_error;
82591f79c43SAl Viro 				nfs_unlock_and_release_request(req);
82691f79c43SAl Viro 				break;
82791f79c43SAl Viro 			}
82891f79c43SAl Viro 			pgbase = 0;
82991f79c43SAl Viro 			bytes -= req_len;
83091f79c43SAl Viro 			requested_bytes += req_len;
83191f79c43SAl Viro 			pos += req_len;
83291f79c43SAl Viro 			dreq->bytes_left -= req_len;
83391f79c43SAl Viro 		}
83491f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
83591f79c43SAl Viro 		kvfree(pagevec);
83691f79c43SAl Viro 		if (result < 0)
83791f79c43SAl Viro 			break;
83819f73787SChuck Lever 	}
8391763da12SFred Isaman 	nfs_pageio_complete(&desc);
84019f73787SChuck Lever 
841839f7ad6SChuck Lever 	/*
842839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
843839f7ad6SChuck Lever 	 * generic layer handle the completion.
844839f7ad6SChuck Lever 	 */
845839f7ad6SChuck Lever 	if (requested_bytes == 0) {
846d03727b2SOlga Kornievskaia 		inode_dio_end(inode);
84765caafd0SOlga Kornievskaia 		nfs_direct_req_release(dreq);
848839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
849839f7ad6SChuck Lever 	}
850839f7ad6SChuck Lever 
85119f73787SChuck Lever 	if (put_dreq(dreq))
8524d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
85385128b2bSAl Viro 	return requested_bytes;
85419f73787SChuck Lever }
85519f73787SChuck Lever 
8561da177e4SLinus Torvalds /**
8571da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8581da177e4SLinus Torvalds  * @iocb: target I/O control block
859619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
86064158668SNeilBrown  * @swap: flag indicating this is swap IO, not O_DIRECT IO
8611da177e4SLinus Torvalds  *
8621da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8631da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8641da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8651da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8661da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8671da177e4SLinus Torvalds  * parameter checking and report problems.
8681da177e4SLinus Torvalds  *
8691da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8701da177e4SLinus Torvalds  *
8711da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8721da177e4SLinus Torvalds  * readers of this file.
8731da177e4SLinus Torvalds  *
8741da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8751da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8761da177e4SLinus Torvalds  */
87764158668SNeilBrown ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
87864158668SNeilBrown 			      bool swap)
8791da177e4SLinus Torvalds {
8809a74a2b8SColin Ian King 	ssize_t result, requested;
88189698b24STrond Myklebust 	size_t count;
8821da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8831da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
88422cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
88522cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
88622cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
88765a4a1caSAl Viro 	loff_t pos, end;
888c216fd70SChuck Lever 
8896de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
8903309dd04SAl Viro 		file, iov_iter_count(iter), (long long) iocb->ki_pos);
891027445c3SBadari Pulavarty 
89264158668SNeilBrown 	if (swap)
89364158668SNeilBrown 		/* bypass generic checks */
89464158668SNeilBrown 		result =  iov_iter_count(iter);
89564158668SNeilBrown 	else
89689698b24STrond Myklebust 		result = generic_write_checks(iocb, iter);
89789698b24STrond Myklebust 	if (result <= 0)
89889698b24STrond Myklebust 		return result;
89989698b24STrond Myklebust 	count = result;
90089698b24STrond Myklebust 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
9013309dd04SAl Viro 
9023309dd04SAl Viro 	pos = iocb->ki_pos;
90309cbfeafSKirill A. Shutemov 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
904ce1a8e67SChuck Lever 
90589698b24STrond Myklebust 	task_io_account_write(count);
9067ec10f26SKonstantin Khlebnikov 
90722cd1bf1SChristoph Hellwig 	result = -ENOMEM;
90822cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
90922cd1bf1SChristoph Hellwig 	if (!dreq)
910a5864c99STrond Myklebust 		goto out;
91122cd1bf1SChristoph Hellwig 
91222cd1bf1SChristoph Hellwig 	dreq->inode = inode;
91389698b24STrond Myklebust 	dreq->bytes_left = dreq->max_count = count;
9145fadeb47SPeng Tao 	dreq->io_start = pos;
91522cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
91622cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
91722cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
91822cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
9198605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
92022cd1bf1SChristoph Hellwig 		goto out_release;
92122cd1bf1SChristoph Hellwig 	}
92222cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
92322cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
92422cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
9259c455a8cSTrond Myklebust 	pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode);
92622cd1bf1SChristoph Hellwig 
92764158668SNeilBrown 	if (swap) {
928c265de25SNeilBrown 		requested = nfs_direct_write_schedule_iovec(dreq, iter, pos,
929c265de25SNeilBrown 							    FLUSH_STABLE);
93064158668SNeilBrown 	} else {
931a5864c99STrond Myklebust 		nfs_start_io_direct(inode);
932a5864c99STrond Myklebust 
933c265de25SNeilBrown 		requested = nfs_direct_write_schedule_iovec(dreq, iter, pos,
934c265de25SNeilBrown 							    FLUSH_COND_STABLE);
935a9ab5e84SChristoph Hellwig 
936a9ab5e84SChristoph Hellwig 		if (mapping->nrpages) {
937a9ab5e84SChristoph Hellwig 			invalidate_inode_pages2_range(mapping,
93809cbfeafSKirill A. Shutemov 						      pos >> PAGE_SHIFT, end);
939a9ab5e84SChristoph Hellwig 		}
940a9ab5e84SChristoph Hellwig 
941a5864c99STrond Myklebust 		nfs_end_io_direct(inode);
94264158668SNeilBrown 	}
943a9ab5e84SChristoph Hellwig 
94485128b2bSAl Viro 	if (requested > 0) {
94522cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
94622cd1bf1SChristoph Hellwig 		if (result > 0) {
94785128b2bSAl Viro 			requested -= result;
94822cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
949e2592217SChristoph Hellwig 			/* XXX: should check the generic_write_sync retval */
950e2592217SChristoph Hellwig 			generic_write_sync(iocb, result);
9511763da12SFred Isaman 		}
95285128b2bSAl Viro 		iov_iter_revert(iter, requested);
95385128b2bSAl Viro 	} else {
95485128b2bSAl Viro 		result = requested;
95522cd1bf1SChristoph Hellwig 	}
956a6b5a28eSDave Wysochanski 	nfs_fscache_invalidate(inode, FSCACHE_INVAL_DIO_WRITE);
95722cd1bf1SChristoph Hellwig out_release:
95822cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
959a5864c99STrond Myklebust out:
96022cd1bf1SChristoph Hellwig 	return result;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
96388467055SChuck Lever /**
96488467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
96588467055SChuck Lever  *
96688467055SChuck Lever  */
967f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9701da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
971fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
972fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
97320c2df83SPaul Mundt 						NULL);
9741da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9751da177e4SLinus Torvalds 		return -ENOMEM;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	return 0;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
98088467055SChuck Lever /**
981f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
98288467055SChuck Lever  *
98388467055SChuck Lever  */
984266bee88SDavid Brownell void nfs_destroy_directcache(void)
9851da177e4SLinus Torvalds {
9861a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
9871da177e4SLinus Torvalds }
988