xref: /openbmc/linux/fs/nfs/direct.c (revision 1f28476dcb98797e838a0c1dd6eae2fda213dd81)
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"
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
641da177e4SLinus Torvalds 
65e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds struct nfs_direct_req {
681da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
6915ce4a0cSChuck Lever 
7015ce4a0cSChuck Lever 	/* I/O parameters */
71a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
72f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7399514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7488467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7515ce4a0cSChuck Lever 
7615ce4a0cSChuck Lever 	/* completion state */
77607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7815ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
790a00b77bSWeston Andros Adamson 
80d9ee6553STrond Myklebust 	loff_t			io_start;	/* Start offset for I/O */
8115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
82ed3743a6SWeston Andros Adamson 				max_count,	/* max expected count */
8335754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
841da177e4SLinus Torvalds 				error;		/* any reported error */
85d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
86fad61490STrond Myklebust 
87fad61490STrond Myklebust 	/* commit state */
881763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
891763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
901763da12SFred Isaman 	struct work_struct	work;
91fad61490STrond Myklebust 	int			flags;
92ad3cba22SDave Kleikamp 	/* for write */
93fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
94fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
95ad3cba22SDave Kleikamp 	/* for read */
96ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY	(3)	/* dirty user-space page after read */
97fb5f7f20STrond Myklebust #define NFS_ODIRECT_DONE		INT_MAX	/* write verification failed */
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
1011763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
1024d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
1031763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
104607f31e8STrond Myklebust 
105607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
106607f31e8STrond Myklebust {
107607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
108607f31e8STrond Myklebust }
109607f31e8STrond Myklebust 
110607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
111607f31e8STrond Myklebust {
112607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
113607f31e8STrond Myklebust }
114607f31e8STrond Myklebust 
1150a00b77bSWeston Andros Adamson static void
116031d73edSTrond Myklebust nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
117031d73edSTrond Myklebust 			    const struct nfs_pgio_header *hdr,
118031d73edSTrond Myklebust 			    ssize_t dreq_len)
1190a00b77bSWeston Andros Adamson {
120031d73edSTrond Myklebust 	if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
121031d73edSTrond Myklebust 	      test_bit(NFS_IOHDR_EOF, &hdr->flags)))
122031d73edSTrond Myklebust 		return;
123031d73edSTrond Myklebust 	if (dreq->max_count >= dreq_len) {
124031d73edSTrond Myklebust 		dreq->max_count = dreq_len;
125031d73edSTrond Myklebust 		if (dreq->count > dreq_len)
126031d73edSTrond Myklebust 			dreq->count = dreq_len;
127ed3743a6SWeston Andros Adamson 
128031d73edSTrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags))
129031d73edSTrond Myklebust 			dreq->error = hdr->error;
130031d73edSTrond Myklebust 		else /* Clear outstanding error if this is EOF */
131031d73edSTrond Myklebust 			dreq->error = 0;
1325fadeb47SPeng Tao 	}
1330a00b77bSWeston Andros Adamson }
134031d73edSTrond Myklebust 
135031d73edSTrond Myklebust static void
136031d73edSTrond Myklebust nfs_direct_count_bytes(struct nfs_direct_req *dreq,
137031d73edSTrond Myklebust 		       const struct nfs_pgio_header *hdr)
138031d73edSTrond Myklebust {
139031d73edSTrond Myklebust 	loff_t hdr_end = hdr->io_start + hdr->good_bytes;
140031d73edSTrond Myklebust 	ssize_t dreq_len = 0;
141031d73edSTrond Myklebust 
142031d73edSTrond Myklebust 	if (hdr_end > dreq->io_start)
143031d73edSTrond Myklebust 		dreq_len = hdr_end - dreq->io_start;
144031d73edSTrond Myklebust 
145031d73edSTrond Myklebust 	nfs_direct_handle_truncated(dreq, hdr, dreq_len);
146031d73edSTrond Myklebust 
147031d73edSTrond Myklebust 	if (dreq_len > dreq->max_count)
148031d73edSTrond Myklebust 		dreq_len = dreq->max_count;
149031d73edSTrond Myklebust 
150031d73edSTrond Myklebust 	if (dreq->count < dreq_len)
151031d73edSTrond Myklebust 		dreq->count = dreq_len;
1521ccbad9fSPeng Tao }
1530a00b77bSWeston Andros Adamson 
1541da177e4SLinus Torvalds /**
155b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
156b8a32e2bSChuck Lever  * @iocb: target I/O control block
15790090ae6SAl Viro  * @iter: I/O buffer
158b8a32e2bSChuck Lever  *
159b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
160a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
161a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
162a564b8f0SMel Gorman  * so this method is only ever called for swap.
1631da177e4SLinus Torvalds  */
164c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
165b8a32e2bSChuck Lever {
166ee8a1a8bSPeng Tao 	struct inode *inode = iocb->ki_filp->f_mapping->host;
167ee8a1a8bSPeng Tao 
168ee8a1a8bSPeng Tao 	/* we only support swap file calling nfs_direct_IO */
169ee8a1a8bSPeng Tao 	if (!IS_SWAPFILE(inode))
170ee8a1a8bSPeng Tao 		return 0;
171ee8a1a8bSPeng Tao 
17266ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
173a564b8f0SMel Gorman 
1746f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
175c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, iter);
17665a4a1caSAl Viro 	return nfs_file_direct_write(iocb, iter);
177b8a32e2bSChuck Lever }
178b8a32e2bSChuck Lever 
179749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1809c93ab7dSChuck Lever {
181749e146eSChuck Lever 	unsigned int i;
182607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
18309cbfeafSKirill A. Shutemov 		put_page(pages[i]);
1846b45d858STrond Myklebust }
1856b45d858STrond Myklebust 
1861763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
1871763da12SFred Isaman 			      struct nfs_direct_req *dreq)
1881763da12SFred Isaman {
189fe238e60SDave Wysochanski 	cinfo->inode = dreq->inode;
1901763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
1911763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
1921763da12SFred Isaman 	cinfo->dreq = dreq;
1931763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
1941763da12SFred Isaman }
1951763da12SFred Isaman 
19693619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1991da177e4SLinus Torvalds 
200292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
2011da177e4SLinus Torvalds 	if (!dreq)
2021da177e4SLinus Torvalds 		return NULL;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	kref_init(&dreq->kref);
205607f31e8STrond Myklebust 	kref_get(&dreq->kref);
206d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
2071763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
208c21e7168STrond Myklebust 	pnfs_init_ds_commit_info(&dreq->ds_cinfo);
2091763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
21015ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
21193619e59SChuck Lever 
21293619e59SChuck Lever 	return dreq;
21393619e59SChuck Lever }
21493619e59SChuck Lever 
215b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
218a8881f5aSTrond Myklebust 
21918f41296STrond Myklebust 	pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode);
2208c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
221f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
222f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
223a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
224a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2251da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
228b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
229b4946ffbSTrond Myklebust {
230b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
231b4946ffbSTrond Myklebust }
232b4946ffbSTrond Myklebust 
2336296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
2346296556fSPeng Tao {
2356296556fSPeng Tao 	return dreq->bytes_left;
2366296556fSPeng Tao }
2376296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
2386296556fSPeng Tao 
239d4cc948bSChuck Lever /*
240bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
241bc0fb201SChuck Lever  */
242bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
243bc0fb201SChuck Lever {
24415ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
245bc0fb201SChuck Lever 
246bc0fb201SChuck Lever 	/* Async requests don't wait here */
247bc0fb201SChuck Lever 	if (dreq->iocb)
248bc0fb201SChuck Lever 		goto out;
249bc0fb201SChuck Lever 
250150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
251bc0fb201SChuck Lever 
252d2a7de0bSTrond Myklebust 	if (!result) {
253d2a7de0bSTrond Myklebust 		result = dreq->count;
254d2a7de0bSTrond Myklebust 		WARN_ON_ONCE(dreq->count < 0);
255d2a7de0bSTrond Myklebust 	}
256bc0fb201SChuck Lever 	if (!result)
25715ce4a0cSChuck Lever 		result = dreq->error;
258bc0fb201SChuck Lever 
259bc0fb201SChuck Lever out:
260bc0fb201SChuck Lever 	return (ssize_t) result;
261bc0fb201SChuck Lever }
262bc0fb201SChuck Lever 
263bc0fb201SChuck Lever /*
264607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
265607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
26663ab46abSChuck Lever  */
267f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq)
26863ab46abSChuck Lever {
2699811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
2709811cd57SChristoph Hellwig 
271fe0f07d0SJens Axboe 	inode_dio_end(inode);
2722a009ec9SChristoph Hellwig 
2732a009ec9SChristoph Hellwig 	if (dreq->iocb) {
2742a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
275d2a7de0bSTrond Myklebust 		if (dreq->count != 0) {
2762a009ec9SChristoph Hellwig 			res = (long) dreq->count;
277d2a7de0bSTrond Myklebust 			WARN_ON_ONCE(dreq->count < 0);
278d2a7de0bSTrond Myklebust 		}
27904b2fa9fSChristoph Hellwig 		dreq->iocb->ki_complete(dreq->iocb, res, 0);
280d72b7a6bSTrond Myklebust 	}
2812a009ec9SChristoph Hellwig 
282024de8f1SDaniel Wagner 	complete(&dreq->completion);
28363ab46abSChuck Lever 
284b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
28563ab46abSChuck Lever }
28663ab46abSChuck Lever 
287584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
288fdd1e74cSTrond Myklebust {
289584aa810SFred Isaman 	unsigned long bytes = 0;
290584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
291fdd1e74cSTrond Myklebust 
29215ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
293eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
294eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
295eb2c50daSTrond Myklebust 		goto out_put;
296eb2c50daSTrond Myklebust 	}
297eb2c50daSTrond Myklebust 
298031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
29915ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3001da177e4SLinus Torvalds 
301584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
302584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
303584aa810SFred Isaman 		struct page *page = req->wb_page;
304584aa810SFred Isaman 
305ad3cba22SDave Kleikamp 		if (!PageCompound(page) && bytes < hdr->good_bytes &&
306ad3cba22SDave Kleikamp 		    (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
3074bd8b010STrond Myklebust 			set_page_dirty(page);
308584aa810SFred Isaman 		bytes += req->wb_bytes;
309584aa810SFred Isaman 		nfs_list_remove_request(req);
310beeb5338SAnna Schumaker 		nfs_release_request(req);
311584aa810SFred Isaman 	}
312584aa810SFred Isaman out_put:
313607f31e8STrond Myklebust 	if (put_dreq(dreq))
314f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
315584aa810SFred Isaman 	hdr->release(hdr);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error)
319cd841605SFred Isaman {
320584aa810SFred Isaman 	struct nfs_page *req;
321cd841605SFred Isaman 
322584aa810SFred Isaman 	while (!list_empty(head)) {
323584aa810SFred Isaman 		req = nfs_list_entry(head->next);
324584aa810SFred Isaman 		nfs_list_remove_request(req);
325584aa810SFred Isaman 		nfs_release_request(req);
326cd841605SFred Isaman 	}
327584aa810SFred Isaman }
328584aa810SFred Isaman 
329584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
330584aa810SFred Isaman {
331584aa810SFred Isaman 	get_dreq(hdr->dreq);
332584aa810SFred Isaman }
333584aa810SFred Isaman 
334584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
3353e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
336584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
337584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
338584aa810SFred Isaman };
339cd841605SFred Isaman 
340d4cc948bSChuck Lever /*
341607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
342607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
343607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
344607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
345607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3461da177e4SLinus Torvalds  */
34791f79c43SAl Viro 
34891f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
34991f79c43SAl Viro 					      struct iov_iter *iter,
35091f79c43SAl Viro 					      loff_t pos)
3511da177e4SLinus Torvalds {
35291f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
35391f79c43SAl Viro 	struct inode *inode = dreq->inode;
35491f79c43SAl Viro 	ssize_t result = -EINVAL;
35591f79c43SAl Viro 	size_t requested_bytes = 0;
35691f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
35782b145c5SChuck Lever 
35816b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
35991f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
36091f79c43SAl Viro 	get_dreq(dreq);
36191f79c43SAl Viro 	desc.pg_dreq = dreq;
362fe0f07d0SJens Axboe 	inode_dio_begin(inode);
36391f79c43SAl Viro 
36491f79c43SAl Viro 	while (iov_iter_count(iter)) {
36591f79c43SAl Viro 		struct page **pagevec;
3665dd602f2SChuck Lever 		size_t bytes;
36791f79c43SAl Viro 		size_t pgbase;
36891f79c43SAl Viro 		unsigned npages, i;
3691da177e4SLinus Torvalds 
37091f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
37191f79c43SAl Viro 						  rsize, &pgbase);
372584aa810SFred Isaman 		if (result < 0)
373749e146eSChuck Lever 			break;
374a564b8f0SMel Gorman 
37591f79c43SAl Viro 		bytes = result;
37691f79c43SAl Viro 		iov_iter_advance(iter, bytes);
37791f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
378584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
379584aa810SFred Isaman 			struct nfs_page *req;
380bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
381584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
38228b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
383584aa810SFred Isaman 						 pgbase, req_len);
384584aa810SFred Isaman 			if (IS_ERR(req)) {
385584aa810SFred Isaman 				result = PTR_ERR(req);
386dbae4c73STrond Myklebust 				break;
387584aa810SFred Isaman 			}
388584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
389584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
39091f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
39191f79c43SAl Viro 				result = desc.pg_error;
392584aa810SFred Isaman 				nfs_release_request(req);
393584aa810SFred Isaman 				break;
394584aa810SFred Isaman 			}
395584aa810SFred Isaman 			pgbase = 0;
396584aa810SFred Isaman 			bytes -= req_len;
39791f79c43SAl Viro 			requested_bytes += req_len;
398584aa810SFred Isaman 			pos += req_len;
39935754bc0SPeng Tao 			dreq->bytes_left -= req_len;
400584aa810SFred Isaman 		}
4016d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
40291f79c43SAl Viro 		kvfree(pagevec);
40319f73787SChuck Lever 		if (result < 0)
40419f73787SChuck Lever 			break;
40519f73787SChuck Lever 	}
40619f73787SChuck Lever 
407584aa810SFred Isaman 	nfs_pageio_complete(&desc);
408584aa810SFred Isaman 
409839f7ad6SChuck Lever 	/*
410839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
411839f7ad6SChuck Lever 	 * generic layer handle the completion.
412839f7ad6SChuck Lever 	 */
413839f7ad6SChuck Lever 	if (requested_bytes == 0) {
414fe0f07d0SJens Axboe 		inode_dio_end(inode);
415839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
416839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
417839f7ad6SChuck Lever 	}
418839f7ad6SChuck Lever 
41919f73787SChuck Lever 	if (put_dreq(dreq))
420f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
42185128b2bSAl Viro 	return requested_bytes;
42219f73787SChuck Lever }
42319f73787SChuck Lever 
42414a3ec79SChristoph Hellwig /**
42514a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
42614a3ec79SChristoph Hellwig  * @iocb: target I/O control block
427619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
42814a3ec79SChristoph Hellwig  *
42914a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
43014a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
43114a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
43214a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
43314a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
43414a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
43514a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
43614a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
43714a3ec79SChristoph Hellwig  *
43814a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
43914a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
44014a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
44114a3ec79SChristoph Hellwig  * cache.
44214a3ec79SChristoph Hellwig  */
443c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
4441da177e4SLinus Torvalds {
44514a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
44614a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
44714a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
4481da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
449b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
45085128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
451a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
45214a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
45314a3ec79SChristoph Hellwig 
45414a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
455c8b8e32dSChristoph Hellwig 		file, count, (long long) iocb->ki_pos);
45614a3ec79SChristoph Hellwig 
45714a3ec79SChristoph Hellwig 	result = 0;
45814a3ec79SChristoph Hellwig 	if (!count)
45914a3ec79SChristoph Hellwig 		goto out;
46014a3ec79SChristoph Hellwig 
46114a3ec79SChristoph Hellwig 	task_io_account_read(count);
46214a3ec79SChristoph Hellwig 
46314a3ec79SChristoph Hellwig 	result = -ENOMEM;
464607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
465f11ac8dbSTrond Myklebust 	if (dreq == NULL)
466a5864c99STrond Myklebust 		goto out;
4671da177e4SLinus Torvalds 
46891d5b470SChuck Lever 	dreq->inode = inode;
469ed3743a6SWeston Andros Adamson 	dreq->bytes_left = dreq->max_count = count;
470c8b8e32dSChristoph Hellwig 	dreq->io_start = iocb->ki_pos;
471cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
472b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
473b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
474b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
4758605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
476f11ac8dbSTrond Myklebust 		goto out_release;
477b3c54de6STrond Myklebust 	}
478b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
479487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
480487b8372SChuck Lever 		dreq->iocb = iocb;
4811da177e4SLinus Torvalds 
482ad3cba22SDave Kleikamp 	if (iter_is_iovec(iter))
483ad3cba22SDave Kleikamp 		dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
484ad3cba22SDave Kleikamp 
485a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
486a5864c99STrond Myklebust 
487619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
48885128b2bSAl Viro 	requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
489d0b9875dSChristoph Hellwig 
490a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
491d0b9875dSChristoph Hellwig 
49285128b2bSAl Viro 	if (requested > 0) {
493bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
49485128b2bSAl Viro 		if (result > 0) {
49585128b2bSAl Viro 			requested -= result;
496c8b8e32dSChristoph Hellwig 			iocb->ki_pos += result;
49714a3ec79SChristoph Hellwig 		}
49885128b2bSAl Viro 		iov_iter_revert(iter, requested);
49985128b2bSAl Viro 	} else {
50085128b2bSAl Viro 		result = requested;
50185128b2bSAl Viro 	}
502d0b9875dSChristoph Hellwig 
503f11ac8dbSTrond Myklebust out_release:
504b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
505f11ac8dbSTrond Myklebust out:
5061da177e4SLinus Torvalds 	return result;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
509085d1e33STom Haynes static void
510085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
511085d1e33STom Haynes 				  struct list_head *list,
512085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
513085d1e33STom Haynes {
514e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
515085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
516085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
517085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
518085d1e33STom Haynes #endif
519085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
520e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
521085d1e33STom Haynes }
522085d1e33STom Haynes 
523fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
5241da177e4SLinus Torvalds {
5251763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
5261763da12SFred Isaman 	struct nfs_page *req, *tmp;
5271763da12SFred Isaman 	LIST_HEAD(reqs);
5281763da12SFred Isaman 	struct nfs_commit_info cinfo;
5291763da12SFred Isaman 	LIST_HEAD(failed);
5301763da12SFred Isaman 
5311763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
532085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
5331da177e4SLinus Torvalds 
534fad61490STrond Myklebust 	dreq->count = 0;
535031d73edSTrond Myklebust 	dreq->max_count = 0;
536031d73edSTrond Myklebust 	list_for_each_entry(req, &reqs, wb_list)
537031d73edSTrond Myklebust 		dreq->max_count += req->wb_bytes;
538a5314a74STrond Myklebust 	nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
539607f31e8STrond Myklebust 	get_dreq(dreq);
5401da177e4SLinus Torvalds 
541a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
5421763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
5431763da12SFred Isaman 	desc.pg_dreq = dreq;
544607f31e8STrond Myklebust 
5451763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
54633344e0fSTrond Myklebust 		/* Bump the transmission count */
54733344e0fSTrond Myklebust 		req->wb_nio++;
5481763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
549078b5fd9STrond Myklebust 			nfs_list_move_request(req, &failed);
550fe238e60SDave Wysochanski 			spin_lock(&cinfo.inode->i_lock);
5511763da12SFred Isaman 			dreq->flags = 0;
552d600ad1fSPeng Tao 			if (desc.pg_error < 0)
553d600ad1fSPeng Tao 				dreq->error = desc.pg_error;
554d600ad1fSPeng Tao 			else
5551763da12SFred Isaman 				dreq->error = -EIO;
556fe238e60SDave Wysochanski 			spin_unlock(&cinfo.inode->i_lock);
5571763da12SFred Isaman 		}
5585a695da2STrond Myklebust 		nfs_release_request(req);
5591763da12SFred Isaman 	}
5601763da12SFred Isaman 	nfs_pageio_complete(&desc);
561607f31e8STrond Myklebust 
5624035c248STrond Myklebust 	while (!list_empty(&failed)) {
5634035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
5644035c248STrond Myklebust 		nfs_list_remove_request(req);
5651d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
5664035c248STrond Myklebust 	}
567607f31e8STrond Myklebust 
568607f31e8STrond Myklebust 	if (put_dreq(dreq))
5694d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
570fad61490STrond Myklebust }
5711da177e4SLinus Torvalds 
5721763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
573fad61490STrond Myklebust {
574*1f28476dSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
5750b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
5761763da12SFred Isaman 	struct nfs_commit_info cinfo;
5771763da12SFred Isaman 	struct nfs_page *req;
578c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
579c9d8f89dSTrond Myklebust 
580fb5f7f20STrond Myklebust 	if (status < 0) {
581fb5f7f20STrond Myklebust 		/* Errors in commit are fatal */
582fb5f7f20STrond Myklebust 		dreq->error = status;
583fb5f7f20STrond Myklebust 		dreq->max_count = 0;
584fb5f7f20STrond Myklebust 		dreq->count = 0;
585fb5f7f20STrond Myklebust 		dreq->flags = NFS_ODIRECT_DONE;
586fb5f7f20STrond Myklebust 	} else if (dreq->flags == NFS_ODIRECT_DONE)
587fb5f7f20STrond Myklebust 		status = dreq->error;
588fb5f7f20STrond Myklebust 
5891763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
590fad61490STrond Myklebust 
5911763da12SFred Isaman 	while (!list_empty(&data->pages)) {
5921763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
5931763da12SFred Isaman 		nfs_list_remove_request(req);
594*1f28476dSTrond Myklebust 		if (status >= 0 && !nfs_write_match_verf(verf, req)) {
595*1f28476dSTrond Myklebust 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
59633344e0fSTrond Myklebust 			/*
59733344e0fSTrond Myklebust 			 * Despite the reboot, the write was successful,
59833344e0fSTrond Myklebust 			 * so reset wb_nio.
59933344e0fSTrond Myklebust 			 */
60033344e0fSTrond Myklebust 			req->wb_nio = 0;
601b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
602*1f28476dSTrond Myklebust 		} else /* Error or match */
603906369e4SFred Isaman 			nfs_release_request(req);
6041d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
605fad61490STrond Myklebust 	}
606fad61490STrond Myklebust 
6071763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
6084d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
6091763da12SFred Isaman }
6101763da12SFred Isaman 
611b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
612b20135d0STrond Myklebust 		struct nfs_page *req)
6131763da12SFred Isaman {
614b20135d0STrond Myklebust 	struct nfs_direct_req *dreq = cinfo->dreq;
615b20135d0STrond Myklebust 
616b20135d0STrond Myklebust 	spin_lock(&dreq->lock);
617fb5f7f20STrond Myklebust 	if (dreq->flags != NFS_ODIRECT_DONE)
618b20135d0STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
619b20135d0STrond Myklebust 	spin_unlock(&dreq->lock);
620b20135d0STrond Myklebust 	nfs_mark_request_commit(req, NULL, cinfo, 0);
6211763da12SFred Isaman }
6221763da12SFred Isaman 
6231763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
6241763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
625b20135d0STrond Myklebust 	.resched_write = nfs_direct_resched_write,
626fad61490STrond Myklebust };
627fad61490STrond Myklebust 
628fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
629fad61490STrond Myklebust {
6301763da12SFred Isaman 	int res;
6311763da12SFred Isaman 	struct nfs_commit_info cinfo;
6321763da12SFred Isaman 	LIST_HEAD(mds_list);
633fad61490STrond Myklebust 
6341763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6351763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
6361763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
6371763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
6381763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
641fb5f7f20STrond Myklebust static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq)
642fb5f7f20STrond Myklebust {
643fb5f7f20STrond Myklebust 	struct nfs_commit_info cinfo;
644fb5f7f20STrond Myklebust 	struct nfs_page *req;
645fb5f7f20STrond Myklebust 	LIST_HEAD(reqs);
646fb5f7f20STrond Myklebust 
647fb5f7f20STrond Myklebust 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
648fb5f7f20STrond Myklebust 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
649fb5f7f20STrond Myklebust 
650fb5f7f20STrond Myklebust 	while (!list_empty(&reqs)) {
651fb5f7f20STrond Myklebust 		req = nfs_list_entry(reqs.next);
652fb5f7f20STrond Myklebust 		nfs_list_remove_request(req);
653fb5f7f20STrond Myklebust 		nfs_unlock_and_release_request(req);
654fb5f7f20STrond Myklebust 	}
655fb5f7f20STrond Myklebust }
656fb5f7f20STrond Myklebust 
6571763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
6581da177e4SLinus Torvalds {
6591763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
660fad61490STrond Myklebust 	int flags = dreq->flags;
6611da177e4SLinus Torvalds 
662fad61490STrond Myklebust 	dreq->flags = 0;
663fad61490STrond Myklebust 	switch (flags) {
664fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
665fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6661da177e4SLinus Torvalds 			break;
667fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
668fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6691da177e4SLinus Torvalds 			break;
6701da177e4SLinus Torvalds 		default:
671fb5f7f20STrond Myklebust 			nfs_direct_write_clear_reqs(dreq);
672f7b5c340STrond Myklebust 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
673f7b5c340STrond Myklebust 			nfs_direct_complete(dreq);
6741da177e4SLinus Torvalds 	}
675fad61490STrond Myklebust }
676fad61490STrond Myklebust 
6774d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
678fad61490STrond Myklebust {
67946483c2eSNeilBrown 	queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
680fad61490STrond Myklebust }
6811763da12SFred Isaman 
6821763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
6831763da12SFred Isaman {
6841763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
6851763da12SFred Isaman 	struct nfs_commit_info cinfo;
686c65e6254SWeston Andros Adamson 	bool request_commit = false;
6871763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6881763da12SFred Isaman 
6891763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6901763da12SFred Isaman 
6911763da12SFred Isaman 	spin_lock(&dreq->lock);
692eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
693eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
694eb2c50daSTrond Myklebust 		goto out_put;
695eb2c50daSTrond Myklebust 	}
696eb2c50daSTrond Myklebust 
697031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
698*1f28476dSTrond Myklebust 	if (hdr->good_bytes != 0 && nfs_write_need_commit(hdr)) {
699*1f28476dSTrond Myklebust 		switch (dreq->flags) {
700*1f28476dSTrond Myklebust 		case 0:
7011763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_DO_COMMIT;
702c65e6254SWeston Andros Adamson 			request_commit = true;
703*1f28476dSTrond Myklebust 			break;
704*1f28476dSTrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
705*1f28476dSTrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
706*1f28476dSTrond Myklebust 			request_commit = true;
7071763da12SFred Isaman 		}
7081763da12SFred Isaman 	}
7091763da12SFred Isaman 	spin_unlock(&dreq->lock);
7101763da12SFred Isaman 
7111763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7122bfc6e56SWeston Andros Adamson 
7131763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7141763da12SFred Isaman 		nfs_list_remove_request(req);
715c65e6254SWeston Andros Adamson 		if (request_commit) {
71604277086STrond Myklebust 			kref_get(&req->wb_kref);
717b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
718b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
7191763da12SFred Isaman 		}
7201d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7211763da12SFred Isaman 	}
7221763da12SFred Isaman 
7231763da12SFred Isaman out_put:
7241763da12SFred Isaman 	if (put_dreq(dreq))
7254d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
7261763da12SFred Isaman 	hdr->release(hdr);
7271763da12SFred Isaman }
7281763da12SFred Isaman 
729df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error)
7303e9e0ca3STrond Myklebust {
7313e9e0ca3STrond Myklebust 	struct nfs_page *req;
7323e9e0ca3STrond Myklebust 
7333e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
7343e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
7353e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
7361d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7373e9e0ca3STrond Myklebust 	}
7383e9e0ca3STrond Myklebust }
7393e9e0ca3STrond Myklebust 
740dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
741dc602dd7STrond Myklebust {
742dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
743dc602dd7STrond Myklebust 
744dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
745dc602dd7STrond Myklebust 	if (dreq->error == 0) {
746dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
747dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
748dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
7494daaeba9STrond Myklebust 		hdr->good_bytes = hdr->args.offset + hdr->args.count -
7504daaeba9STrond Myklebust 			hdr->io_start;
751dc602dd7STrond Myklebust 	}
752dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
753dc602dd7STrond Myklebust }
754dc602dd7STrond Myklebust 
7551763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
7563e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
7571763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
7581763da12SFred Isaman 	.completion = nfs_direct_write_completion,
759dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
7601763da12SFred Isaman };
7611763da12SFred Isaman 
76291f79c43SAl Viro 
76391f79c43SAl Viro /*
76491f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
76591f79c43SAl Viro  *     errors after the first one are ignored.
76691f79c43SAl Viro  */
76791f79c43SAl Viro /*
76891f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
76991f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
77091f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
77191f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
77291f79c43SAl Viro  * no requests have been sent, just return an error.
77391f79c43SAl Viro  */
77419f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
775619d30b4SAl Viro 					       struct iov_iter *iter,
77691f79c43SAl Viro 					       loff_t pos)
77719f73787SChuck Lever {
7781763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
7791d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
78019f73787SChuck Lever 	ssize_t result = 0;
78119f73787SChuck Lever 	size_t requested_bytes = 0;
78291f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
78319f73787SChuck Lever 
784a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
7851763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
7861763da12SFred Isaman 	desc.pg_dreq = dreq;
78719f73787SChuck Lever 	get_dreq(dreq);
788fe0f07d0SJens Axboe 	inode_dio_begin(inode);
78919f73787SChuck Lever 
79091f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
79191f79c43SAl Viro 	while (iov_iter_count(iter)) {
79291f79c43SAl Viro 		struct page **pagevec;
79391f79c43SAl Viro 		size_t bytes;
79491f79c43SAl Viro 		size_t pgbase;
79591f79c43SAl Viro 		unsigned npages, i;
79691f79c43SAl Viro 
79791f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
79891f79c43SAl Viro 						  wsize, &pgbase);
79919f73787SChuck Lever 		if (result < 0)
80019f73787SChuck Lever 			break;
80191f79c43SAl Viro 
80291f79c43SAl Viro 		bytes = result;
80391f79c43SAl Viro 		iov_iter_advance(iter, bytes);
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 
80928b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
81091f79c43SAl Viro 						 pgbase, 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 			req->wb_index = pos >> PAGE_SHIFT;
82491f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
82591f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
82691f79c43SAl Viro 				result = desc.pg_error;
82791f79c43SAl Viro 				nfs_unlock_and_release_request(req);
82891f79c43SAl Viro 				break;
82991f79c43SAl Viro 			}
83091f79c43SAl Viro 			pgbase = 0;
83191f79c43SAl Viro 			bytes -= req_len;
83291f79c43SAl Viro 			requested_bytes += req_len;
83391f79c43SAl Viro 			pos += req_len;
83491f79c43SAl Viro 			dreq->bytes_left -= req_len;
83591f79c43SAl Viro 		}
83691f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
83791f79c43SAl Viro 		kvfree(pagevec);
83891f79c43SAl Viro 		if (result < 0)
83991f79c43SAl Viro 			break;
84019f73787SChuck Lever 	}
8411763da12SFred Isaman 	nfs_pageio_complete(&desc);
84219f73787SChuck Lever 
843839f7ad6SChuck Lever 	/*
844839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
845839f7ad6SChuck Lever 	 * generic layer handle the completion.
846839f7ad6SChuck Lever 	 */
847839f7ad6SChuck Lever 	if (requested_bytes == 0) {
848fe0f07d0SJens Axboe 		inode_dio_end(inode);
849839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
850839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
851839f7ad6SChuck Lever 	}
852839f7ad6SChuck Lever 
85319f73787SChuck Lever 	if (put_dreq(dreq))
8544d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
85585128b2bSAl Viro 	return requested_bytes;
85619f73787SChuck Lever }
85719f73787SChuck Lever 
8581da177e4SLinus Torvalds /**
8591da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8601da177e4SLinus Torvalds  * @iocb: target I/O control block
861619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
8621da177e4SLinus Torvalds  *
8631da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8641da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8651da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8661da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8671da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8681da177e4SLinus Torvalds  * parameter checking and report problems.
8691da177e4SLinus Torvalds  *
8701da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8711da177e4SLinus Torvalds  *
8721da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8731da177e4SLinus Torvalds  * readers of this file.
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8761da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8771da177e4SLinus Torvalds  */
87865a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
8791da177e4SLinus Torvalds {
88085128b2bSAl Viro 	ssize_t result = -EINVAL, 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 
89289698b24STrond Myklebust 	result = generic_write_checks(iocb, iter);
89389698b24STrond Myklebust 	if (result <= 0)
89489698b24STrond Myklebust 		return result;
89589698b24STrond Myklebust 	count = result;
89689698b24STrond Myklebust 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
8973309dd04SAl Viro 
8983309dd04SAl Viro 	pos = iocb->ki_pos;
89909cbfeafSKirill A. Shutemov 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
900ce1a8e67SChuck Lever 
90189698b24STrond Myklebust 	task_io_account_write(count);
9027ec10f26SKonstantin Khlebnikov 
90322cd1bf1SChristoph Hellwig 	result = -ENOMEM;
90422cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
90522cd1bf1SChristoph Hellwig 	if (!dreq)
906a5864c99STrond Myklebust 		goto out;
90722cd1bf1SChristoph Hellwig 
90822cd1bf1SChristoph Hellwig 	dreq->inode = inode;
90989698b24STrond Myklebust 	dreq->bytes_left = dreq->max_count = count;
9105fadeb47SPeng Tao 	dreq->io_start = pos;
91122cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
91222cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
91322cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
91422cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
9158605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
91622cd1bf1SChristoph Hellwig 		goto out_release;
91722cd1bf1SChristoph Hellwig 	}
91822cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
91922cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
92022cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
92122cd1bf1SChristoph Hellwig 
922a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
923a5864c99STrond Myklebust 
92485128b2bSAl Viro 	requested = nfs_direct_write_schedule_iovec(dreq, iter, pos);
925a9ab5e84SChristoph Hellwig 
926a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
927a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
92809cbfeafSKirill A. Shutemov 					      pos >> PAGE_SHIFT, end);
929a9ab5e84SChristoph Hellwig 	}
930a9ab5e84SChristoph Hellwig 
931a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
932a9ab5e84SChristoph Hellwig 
93385128b2bSAl Viro 	if (requested > 0) {
93422cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
93522cd1bf1SChristoph Hellwig 		if (result > 0) {
93685128b2bSAl Viro 			requested -= result;
93722cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
938e2592217SChristoph Hellwig 			/* XXX: should check the generic_write_sync retval */
939e2592217SChristoph Hellwig 			generic_write_sync(iocb, result);
9401763da12SFred Isaman 		}
94185128b2bSAl Viro 		iov_iter_revert(iter, requested);
94285128b2bSAl Viro 	} else {
94385128b2bSAl Viro 		result = requested;
94422cd1bf1SChristoph Hellwig 	}
94522cd1bf1SChristoph Hellwig out_release:
94622cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
947a5864c99STrond Myklebust out:
94822cd1bf1SChristoph Hellwig 	return result;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
95188467055SChuck Lever /**
95288467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
95388467055SChuck Lever  *
95488467055SChuck Lever  */
955f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9581da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
959fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
960fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
96120c2df83SPaul Mundt 						NULL);
9621da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9631da177e4SLinus Torvalds 		return -ENOMEM;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	return 0;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
96888467055SChuck Lever /**
969f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
97088467055SChuck Lever  *
97188467055SChuck Lever  */
972266bee88SDavid Brownell void nfs_destroy_directcache(void)
9731da177e4SLinus Torvalds {
9741a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
9751da177e4SLinus Torvalds }
976