xref: /openbmc/linux/fs/nfs/direct.c (revision 1e8968c5b0582392d5f132422f581e3ebc24e627)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * There are important applications whose performance or correctness
91da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
101da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
111da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
121da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
131da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
141da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
151da177e4SLinus Torvalds  * need to cache the contents of a file.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
181da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
191da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
201da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
211da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
221da177e4SLinus Torvalds  * an application.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
251da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
261da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
271da177e4SLinus Torvalds  * an undocumented mount option.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
301da177e4SLinus Torvalds  * help from Andrew Morton.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
331da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
341da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
351da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
361da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3788467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/file.h>
451da177e4SLinus Torvalds #include <linux/pagemap.h>
461da177e4SLinus Torvalds #include <linux/kref.h>
475a0e3ad6STejun Heo #include <linux/slab.h>
487ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h>
496296556fSPeng Tao #include <linux/module.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <linux/nfs_fs.h>
521da177e4SLinus Torvalds #include <linux/nfs_page.h>
531da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <asm/uaccess.h>
5660063497SArun Sharma #include <linux/atomic.h>
571da177e4SLinus Torvalds 
588d5658c9STrond Myklebust #include "internal.h"
5991d5b470SChuck Lever #include "iostat.h"
601763da12SFred Isaman #include "pnfs.h"
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
631da177e4SLinus Torvalds 
64e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds struct nfs_direct_req {
701da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7115ce4a0cSChuck Lever 
7215ce4a0cSChuck Lever 	/* I/O parameters */
73a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
74f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7599514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7688467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7715ce4a0cSChuck Lever 
7815ce4a0cSChuck Lever 	/* completion state */
79607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
8015ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
8235754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
831da177e4SLinus Torvalds 				error;		/* any reported error */
84d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
85fad61490STrond Myklebust 
86fad61490STrond Myklebust 	/* commit state */
871763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
881763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
891763da12SFred Isaman 	struct work_struct	work;
90fad61490STrond Myklebust 	int			flags;
91fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
92fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
93fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
971763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
991763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
100607f31e8STrond Myklebust 
101607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
102607f31e8STrond Myklebust {
103607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
104607f31e8STrond Myklebust }
105607f31e8STrond Myklebust 
106607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
107607f31e8STrond Myklebust {
108607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
109607f31e8STrond Myklebust }
110607f31e8STrond Myklebust 
1111da177e4SLinus Torvalds /**
112b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
113b8a32e2bSChuck Lever  * @rw: direction (read or write)
114b8a32e2bSChuck Lever  * @iocb: target I/O control block
115b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
116b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
117b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
118b8a32e2bSChuck Lever  *
119b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
120a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
121a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
122a564b8f0SMel Gorman  * so this method is only ever called for swap.
1231da177e4SLinus Torvalds  */
124b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
125b8a32e2bSChuck Lever {
126a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP
1276de1472fSAl Viro 	dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n",
1286de1472fSAl Viro 			iocb->ki_filp, (long long) pos, nr_segs);
129b8a32e2bSChuck Lever 
130b8a32e2bSChuck Lever 	return -EINVAL;
131a564b8f0SMel Gorman #else
132a564b8f0SMel Gorman 	VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
133a564b8f0SMel Gorman 
134a564b8f0SMel Gorman 	if (rw == READ || rw == KERNEL_READ)
135a564b8f0SMel Gorman 		return nfs_file_direct_read(iocb, iov, nr_segs, pos,
136a564b8f0SMel Gorman 				rw == READ ? true : false);
137a564b8f0SMel Gorman 	return nfs_file_direct_write(iocb, iov, nr_segs, pos,
138a564b8f0SMel Gorman 				rw == WRITE ? true : false);
139a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */
140b8a32e2bSChuck Lever }
141b8a32e2bSChuck Lever 
142749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1439c93ab7dSChuck Lever {
144749e146eSChuck Lever 	unsigned int i;
145607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
146607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1476b45d858STrond Myklebust }
1486b45d858STrond Myklebust 
1491763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
1501763da12SFred Isaman 			      struct nfs_direct_req *dreq)
1511763da12SFred Isaman {
1521763da12SFred Isaman 	cinfo->lock = &dreq->lock;
1531763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
1541763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
1551763da12SFred Isaman 	cinfo->dreq = dreq;
1561763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
1571763da12SFred Isaman }
1581763da12SFred Isaman 
15993619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1621da177e4SLinus Torvalds 
163292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
1641da177e4SLinus Torvalds 	if (!dreq)
1651da177e4SLinus Torvalds 		return NULL;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	kref_init(&dreq->kref);
168607f31e8STrond Myklebust 	kref_get(&dreq->kref);
169d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1701763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
1711763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
17215ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
17393619e59SChuck Lever 
17493619e59SChuck Lever 	return dreq;
17593619e59SChuck Lever }
17693619e59SChuck Lever 
177b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
180a8881f5aSTrond Myklebust 
181f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
182f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
183a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
184a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1851da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
189b4946ffbSTrond Myklebust {
190b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
191b4946ffbSTrond Myklebust }
192b4946ffbSTrond Myklebust 
1936296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
1946296556fSPeng Tao {
1956296556fSPeng Tao 	return dreq->bytes_left;
1966296556fSPeng Tao }
1976296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
1986296556fSPeng Tao 
199d4cc948bSChuck Lever /*
200bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
201bc0fb201SChuck Lever  */
202bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
203bc0fb201SChuck Lever {
20415ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
205bc0fb201SChuck Lever 
206bc0fb201SChuck Lever 	/* Async requests don't wait here */
207bc0fb201SChuck Lever 	if (dreq->iocb)
208bc0fb201SChuck Lever 		goto out;
209bc0fb201SChuck Lever 
210150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
211bc0fb201SChuck Lever 
212bc0fb201SChuck Lever 	if (!result)
21315ce4a0cSChuck Lever 		result = dreq->error;
214bc0fb201SChuck Lever 	if (!result)
21515ce4a0cSChuck Lever 		result = dreq->count;
216bc0fb201SChuck Lever 
217bc0fb201SChuck Lever out:
218bc0fb201SChuck Lever 	return (ssize_t) result;
219bc0fb201SChuck Lever }
220bc0fb201SChuck Lever 
221bc0fb201SChuck Lever /*
222607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
223607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
22463ab46abSChuck Lever  */
22563ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
22663ab46abSChuck Lever {
22763ab46abSChuck Lever 	if (dreq->iocb) {
22815ce4a0cSChuck Lever 		long res = (long) dreq->error;
22963ab46abSChuck Lever 		if (!res)
23015ce4a0cSChuck Lever 			res = (long) dreq->count;
23163ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
232d72b7a6bSTrond Myklebust 	}
233d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
23463ab46abSChuck Lever 
235b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
23663ab46abSChuck Lever }
23763ab46abSChuck Lever 
2381385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
2391da177e4SLinus Torvalds {
240*1e8968c5SNiels de Vos 	dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
241584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
242*1e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
243584aa810SFred Isaman 		req->wb_bytes,
244584aa810SFred Isaman 		(long long)req_offset(req));
245584aa810SFred Isaman 	nfs_release_request(req);
246fdd1e74cSTrond Myklebust }
247fdd1e74cSTrond Myklebust 
248584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
249fdd1e74cSTrond Myklebust {
250584aa810SFred Isaman 	unsigned long bytes = 0;
251584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
252fdd1e74cSTrond Myklebust 
253584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
254584aa810SFred Isaman 		goto out_put;
2551da177e4SLinus Torvalds 
25615ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
257584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
258584aa810SFred Isaman 		dreq->error = hdr->error;
259584aa810SFred Isaman 	else
260584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
26115ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2621da177e4SLinus Torvalds 
263584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
264584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
265584aa810SFred Isaman 		struct page *page = req->wb_page;
266584aa810SFred Isaman 
267be7e9858SJeff Layton 		if (!PageCompound(page) && bytes < hdr->good_bytes)
2684bd8b010STrond Myklebust 			set_page_dirty(page);
269584aa810SFred Isaman 		bytes += req->wb_bytes;
270584aa810SFred Isaman 		nfs_list_remove_request(req);
271584aa810SFred Isaman 		nfs_direct_readpage_release(req);
272584aa810SFred Isaman 	}
273584aa810SFred Isaman out_put:
274607f31e8STrond Myklebust 	if (put_dreq(dreq))
27563ab46abSChuck Lever 		nfs_direct_complete(dreq);
276584aa810SFred Isaman 	hdr->release(hdr);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2793e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
280cd841605SFred Isaman {
281584aa810SFred Isaman 	struct nfs_page *req;
282cd841605SFred Isaman 
283584aa810SFred Isaman 	while (!list_empty(head)) {
284584aa810SFred Isaman 		req = nfs_list_entry(head->next);
285584aa810SFred Isaman 		nfs_list_remove_request(req);
286584aa810SFred Isaman 		nfs_release_request(req);
287cd841605SFred Isaman 	}
288584aa810SFred Isaman }
289584aa810SFred Isaman 
290584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
291584aa810SFred Isaman {
292584aa810SFred Isaman 	get_dreq(hdr->dreq);
293584aa810SFred Isaman }
294584aa810SFred Isaman 
295584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
2963e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
297584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
298584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
299584aa810SFred Isaman };
300cd841605SFred Isaman 
301d4cc948bSChuck Lever /*
302607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
303607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
304607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
305607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
306607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3071da177e4SLinus Torvalds  */
308584aa810SFred Isaman static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
30902fe4946SChuck Lever 						const struct iovec *iov,
310a564b8f0SMel Gorman 						loff_t pos, bool uio)
3111da177e4SLinus Torvalds {
312584aa810SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
313a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
3143d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
31502fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
31602fe4946SChuck Lever 	size_t count = iov->iov_len;
3175dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
318607f31e8STrond Myklebust 	unsigned int pgbase;
319607f31e8STrond Myklebust 	int result;
320607f31e8STrond Myklebust 	ssize_t started = 0;
321584aa810SFred Isaman 	struct page **pagevec = NULL;
322584aa810SFred Isaman 	unsigned int npages;
32382b145c5SChuck Lever 
3241da177e4SLinus Torvalds 	do {
3255dd602f2SChuck Lever 		size_t bytes;
326584aa810SFred Isaman 		int i;
3271da177e4SLinus Torvalds 
328e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
329bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
330e9f7bee1STrond Myklebust 
331607f31e8STrond Myklebust 		result = -ENOMEM;
332584aa810SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
333584aa810SFred Isaman 		if (!pagevec)
334584aa810SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *),
335584aa810SFred Isaman 					  GFP_KERNEL);
336584aa810SFred Isaman 		if (!pagevec)
337607f31e8STrond Myklebust 			break;
338a564b8f0SMel Gorman 		if (uio) {
339607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
340607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
341584aa810SFred Isaman 					npages, 1, 0, pagevec, NULL);
342607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
343584aa810SFred Isaman 			if (result < 0)
344749e146eSChuck Lever 				break;
345a564b8f0SMel Gorman 		} else {
346a564b8f0SMel Gorman 			WARN_ON(npages != 1);
347a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 1, pagevec);
348a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
349a564b8f0SMel Gorman 				break;
350a564b8f0SMel Gorman 		}
351a564b8f0SMel Gorman 
352584aa810SFred Isaman 		if ((unsigned)result < npages) {
353d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
354d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
355584aa810SFred Isaman 				nfs_direct_release_pages(pagevec, result);
356607f31e8STrond Myklebust 				break;
357607f31e8STrond Myklebust 			}
358d9df8d6bSTrond Myklebust 			bytes -= pgbase;
359584aa810SFred Isaman 			npages = result;
360d9df8d6bSTrond Myklebust 		}
36106cf6f2eSChuck Lever 
362584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
363584aa810SFred Isaman 			struct nfs_page *req;
364bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
365584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
366584aa810SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
367584aa810SFred Isaman 						 pagevec[i],
368584aa810SFred Isaman 						 pgbase, req_len);
369584aa810SFred Isaman 			if (IS_ERR(req)) {
370584aa810SFred Isaman 				result = PTR_ERR(req);
371dbae4c73STrond Myklebust 				break;
372584aa810SFred Isaman 			}
373584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
374584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
375584aa810SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
376584aa810SFred Isaman 				result = desc->pg_error;
377584aa810SFred Isaman 				nfs_release_request(req);
378584aa810SFred Isaman 				break;
379584aa810SFred Isaman 			}
380584aa810SFred Isaman 			pgbase = 0;
381584aa810SFred Isaman 			bytes -= req_len;
382584aa810SFred Isaman 			started += req_len;
383584aa810SFred Isaman 			user_addr += req_len;
384584aa810SFred Isaman 			pos += req_len;
385584aa810SFred Isaman 			count -= req_len;
38635754bc0SPeng Tao 			dreq->bytes_left -= req_len;
387584aa810SFred Isaman 		}
3886d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
3896d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
39071e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
391607f31e8STrond Myklebust 
392584aa810SFred Isaman 	kfree(pagevec);
393584aa810SFred Isaman 
394607f31e8STrond Myklebust 	if (started)
395c216fd70SChuck Lever 		return started;
396607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
39706cf6f2eSChuck Lever }
39806cf6f2eSChuck Lever 
39919f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
40019f73787SChuck Lever 					      const struct iovec *iov,
40119f73787SChuck Lever 					      unsigned long nr_segs,
402a564b8f0SMel Gorman 					      loff_t pos, bool uio)
40319f73787SChuck Lever {
404584aa810SFred Isaman 	struct nfs_pageio_descriptor desc;
40519f73787SChuck Lever 	ssize_t result = -EINVAL;
40619f73787SChuck Lever 	size_t requested_bytes = 0;
40719f73787SChuck Lever 	unsigned long seg;
40819f73787SChuck Lever 
40959948db3SFred Isaman 	NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
410584aa810SFred Isaman 			     &nfs_direct_read_completion_ops);
41119f73787SChuck Lever 	get_dreq(dreq);
412584aa810SFred Isaman 	desc.pg_dreq = dreq;
41319f73787SChuck Lever 
41419f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
41519f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
416a564b8f0SMel Gorman 		result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
41719f73787SChuck Lever 		if (result < 0)
41819f73787SChuck Lever 			break;
41919f73787SChuck Lever 		requested_bytes += result;
42019f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
42119f73787SChuck Lever 			break;
42219f73787SChuck Lever 		pos += vec->iov_len;
42319f73787SChuck Lever 	}
42419f73787SChuck Lever 
425584aa810SFred Isaman 	nfs_pageio_complete(&desc);
426584aa810SFred Isaman 
427839f7ad6SChuck Lever 	/*
428839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
429839f7ad6SChuck Lever 	 * generic layer handle the completion.
430839f7ad6SChuck Lever 	 */
431839f7ad6SChuck Lever 	if (requested_bytes == 0) {
432839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
433839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
434839f7ad6SChuck Lever 	}
435839f7ad6SChuck Lever 
43619f73787SChuck Lever 	if (put_dreq(dreq))
43719f73787SChuck Lever 		nfs_direct_complete(dreq);
43819f73787SChuck Lever 	return 0;
43919f73787SChuck Lever }
44019f73787SChuck Lever 
441c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
442a564b8f0SMel Gorman 			       unsigned long nr_segs, loff_t pos, bool uio)
4431da177e4SLinus Torvalds {
444f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
44599514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4461da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
447b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
4481da177e4SLinus Torvalds 
449607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
450f11ac8dbSTrond Myklebust 	if (dreq == NULL)
451f11ac8dbSTrond Myklebust 		goto out;
4521da177e4SLinus Torvalds 
45391d5b470SChuck Lever 	dreq->inode = inode;
45435754bc0SPeng Tao 	dreq->bytes_left = iov_length(iov, nr_segs);
455cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
456b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
457b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
458b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
459f11ac8dbSTrond Myklebust 		goto out_release;
460b3c54de6STrond Myklebust 	}
461b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
462487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
463487b8372SChuck Lever 		dreq->iocb = iocb;
4641da177e4SLinus Torvalds 
4657acdb026SPeng Tao 	NFS_I(inode)->read_io += iov_length(iov, nr_segs);
466a564b8f0SMel Gorman 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
467607f31e8STrond Myklebust 	if (!result)
468bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
469f11ac8dbSTrond Myklebust out_release:
470b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
471f11ac8dbSTrond Myklebust out:
4721da177e4SLinus Torvalds 	return result;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751d59d61fSTrond Myklebust static void nfs_inode_dio_write_done(struct inode *inode)
4761d59d61fSTrond Myklebust {
4771d59d61fSTrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
4781d59d61fSTrond Myklebust 	inode_dio_done(inode);
4791d59d61fSTrond Myklebust }
4801d59d61fSTrond Myklebust 
48189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
482fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4831da177e4SLinus Torvalds {
4841763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
4851763da12SFred Isaman 	struct nfs_page *req, *tmp;
4861763da12SFred Isaman 	LIST_HEAD(reqs);
4871763da12SFred Isaman 	struct nfs_commit_info cinfo;
4881763da12SFred Isaman 	LIST_HEAD(failed);
4891763da12SFred Isaman 
4901763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
4911763da12SFred Isaman 	pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
4921763da12SFred Isaman 	spin_lock(cinfo.lock);
4931763da12SFred Isaman 	nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
4941763da12SFred Isaman 	spin_unlock(cinfo.lock);
4951da177e4SLinus Torvalds 
496fad61490STrond Myklebust 	dreq->count = 0;
497607f31e8STrond Myklebust 	get_dreq(dreq);
4981da177e4SLinus Torvalds 
499c95908e4SFred Isaman 	NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
5001763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
5011763da12SFred Isaman 	desc.pg_dreq = dreq;
502607f31e8STrond Myklebust 
5031763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
5041763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
5054035c248STrond Myklebust 			nfs_list_remove_request(req);
5061763da12SFred Isaman 			nfs_list_add_request(req, &failed);
5071763da12SFred Isaman 			spin_lock(cinfo.lock);
5081763da12SFred Isaman 			dreq->flags = 0;
5091763da12SFred Isaman 			dreq->error = -EIO;
5101763da12SFred Isaman 			spin_unlock(cinfo.lock);
5111763da12SFred Isaman 		}
5125a695da2STrond Myklebust 		nfs_release_request(req);
5131763da12SFred Isaman 	}
5141763da12SFred Isaman 	nfs_pageio_complete(&desc);
515607f31e8STrond Myklebust 
5164035c248STrond Myklebust 	while (!list_empty(&failed)) {
5174035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
5184035c248STrond Myklebust 		nfs_list_remove_request(req);
5191d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
5204035c248STrond Myklebust 	}
521607f31e8STrond Myklebust 
522607f31e8STrond Myklebust 	if (put_dreq(dreq))
5231763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
524fad61490STrond Myklebust }
5251da177e4SLinus Torvalds 
5261763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
527fad61490STrond Myklebust {
5280b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
5291763da12SFred Isaman 	struct nfs_commit_info cinfo;
5301763da12SFred Isaman 	struct nfs_page *req;
531c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
532c9d8f89dSTrond Myklebust 
5331763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
534c9d8f89dSTrond Myklebust 	if (status < 0) {
53560fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
536c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
537fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
53860fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
539c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
540fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
541fad61490STrond Myklebust 	}
542fad61490STrond Myklebust 
543c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
5441763da12SFred Isaman 	while (!list_empty(&data->pages)) {
5451763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
5461763da12SFred Isaman 		nfs_list_remove_request(req);
5471763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
5481763da12SFred Isaman 			/* Note the rewrite will go through mds */
5491763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
550906369e4SFred Isaman 		} else
551906369e4SFred Isaman 			nfs_release_request(req);
5521d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
553fad61490STrond Myklebust 	}
554fad61490STrond Myklebust 
5551763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
5561763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
5571763da12SFred Isaman }
5581763da12SFred Isaman 
5591763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
5601763da12SFred Isaman {
5611763da12SFred Isaman 	/* There is no lock to clear */
5621763da12SFred Isaman }
5631763da12SFred Isaman 
5641763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
5651763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
5661763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
567fad61490STrond Myklebust };
568fad61490STrond Myklebust 
569fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
570fad61490STrond Myklebust {
5711763da12SFred Isaman 	int res;
5721763da12SFred Isaman 	struct nfs_commit_info cinfo;
5731763da12SFred Isaman 	LIST_HEAD(mds_list);
574fad61490STrond Myklebust 
5751763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
5761763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
5771763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
5781763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
5791763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5821763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
5831da177e4SLinus Torvalds {
5841763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
585fad61490STrond Myklebust 	int flags = dreq->flags;
5861da177e4SLinus Torvalds 
587fad61490STrond Myklebust 	dreq->flags = 0;
588fad61490STrond Myklebust 	switch (flags) {
589fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
590fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5911da177e4SLinus Torvalds 			break;
592fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
593fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5941da177e4SLinus Torvalds 			break;
5951da177e4SLinus Torvalds 		default:
5961d59d61fSTrond Myklebust 			nfs_inode_dio_write_done(dreq->inode);
597fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5981da177e4SLinus Torvalds 	}
599fad61490STrond Myklebust }
600fad61490STrond Myklebust 
6011763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
602fad61490STrond Myklebust {
6031763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
604fad61490STrond Myklebust }
6051763da12SFred Isaman 
606fad61490STrond Myklebust #else
60724fc9211SBryan Schumaker static void nfs_direct_write_schedule_work(struct work_struct *work)
60824fc9211SBryan Schumaker {
60924fc9211SBryan Schumaker }
610fad61490STrond Myklebust 
611fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
612fad61490STrond Myklebust {
6131d59d61fSTrond Myklebust 	nfs_inode_dio_write_done(inode);
614fad61490STrond Myklebust 	nfs_direct_complete(dreq);
615fad61490STrond Myklebust }
616fad61490STrond Myklebust #endif
617fad61490STrond Myklebust 
618c9d8f89dSTrond Myklebust /*
619c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
620c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
621c9d8f89dSTrond Myklebust  */
622462d5b32SChuck Lever /*
623607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
624607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
625607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
626607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
627607f31e8STrond Myklebust  * no requests have been sent, just return an error.
628462d5b32SChuck Lever  */
6291763da12SFred Isaman static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
63002fe4946SChuck Lever 						 const struct iovec *iov,
631a564b8f0SMel Gorman 						 loff_t pos, bool uio)
632462d5b32SChuck Lever {
6331763da12SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
634a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
6353d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
63602fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
63702fe4946SChuck Lever 	size_t count = iov->iov_len;
638462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
639607f31e8STrond Myklebust 	unsigned int pgbase;
640607f31e8STrond Myklebust 	int result;
641607f31e8STrond Myklebust 	ssize_t started = 0;
6421763da12SFred Isaman 	struct page **pagevec = NULL;
6431763da12SFred Isaman 	unsigned int npages;
64482b145c5SChuck Lever 
645462d5b32SChuck Lever 	do {
646462d5b32SChuck Lever 		size_t bytes;
6471763da12SFred Isaman 		int i;
648462d5b32SChuck Lever 
649e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
650bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
651e9f7bee1STrond Myklebust 
652607f31e8STrond Myklebust 		result = -ENOMEM;
6531763da12SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
6541763da12SFred Isaman 		if (!pagevec)
6551763da12SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
6561763da12SFred Isaman 		if (!pagevec)
657607f31e8STrond Myklebust 			break;
658607f31e8STrond Myklebust 
659a564b8f0SMel Gorman 		if (uio) {
660607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
661607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
6621763da12SFred Isaman 						npages, 0, 0, pagevec, NULL);
663607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
6641763da12SFred Isaman 			if (result < 0)
665749e146eSChuck Lever 				break;
666a564b8f0SMel Gorman 		} else {
667a564b8f0SMel Gorman 			WARN_ON(npages != 1);
668a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 0, pagevec);
669a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
670a564b8f0SMel Gorman 				break;
671a564b8f0SMel Gorman 		}
6721763da12SFred Isaman 
6731763da12SFred Isaman 		if ((unsigned)result < npages) {
674d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
675d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
6761763da12SFred Isaman 				nfs_direct_release_pages(pagevec, result);
677607f31e8STrond Myklebust 				break;
678607f31e8STrond Myklebust 			}
679d9df8d6bSTrond Myklebust 			bytes -= pgbase;
6801763da12SFred Isaman 			npages = result;
681d9df8d6bSTrond Myklebust 		}
682607f31e8STrond Myklebust 
6831763da12SFred Isaman 		for (i = 0; i < npages; i++) {
6841763da12SFred Isaman 			struct nfs_page *req;
685bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
686607f31e8STrond Myklebust 
6871763da12SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
6881763da12SFred Isaman 						 pagevec[i],
6891763da12SFred Isaman 						 pgbase, req_len);
6901763da12SFred Isaman 			if (IS_ERR(req)) {
6911763da12SFred Isaman 				result = PTR_ERR(req);
692dbae4c73STrond Myklebust 				break;
6931763da12SFred Isaman 			}
6941763da12SFred Isaman 			nfs_lock_request(req);
6951763da12SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
6961763da12SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
6971763da12SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
6981763da12SFred Isaman 				result = desc->pg_error;
6991d1afcbcSTrond Myklebust 				nfs_unlock_and_release_request(req);
70071e8cc00STrond Myklebust 				break;
7011763da12SFred Isaman 			}
7021763da12SFred Isaman 			pgbase = 0;
7031763da12SFred Isaman 			bytes -= req_len;
7041763da12SFred Isaman 			started += req_len;
7051763da12SFred Isaman 			user_addr += req_len;
7061763da12SFred Isaman 			pos += req_len;
7071763da12SFred Isaman 			count -= req_len;
70835754bc0SPeng Tao 			dreq->bytes_left -= req_len;
7091763da12SFred Isaman 		}
7106d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
7116d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
71271e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
713607f31e8STrond Myklebust 
7141763da12SFred Isaman 	kfree(pagevec);
7151763da12SFred Isaman 
716607f31e8STrond Myklebust 	if (started)
717c216fd70SChuck Lever 		return started;
718607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
71906cf6f2eSChuck Lever }
72006cf6f2eSChuck Lever 
7211763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7221763da12SFred Isaman {
7231763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7241763da12SFred Isaman 	struct nfs_commit_info cinfo;
7251763da12SFred Isaman 	int bit = -1;
7261763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7271763da12SFred Isaman 
7281763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
7291763da12SFred Isaman 		goto out_put;
7301763da12SFred Isaman 
7311763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7321763da12SFred Isaman 
7331763da12SFred Isaman 	spin_lock(&dreq->lock);
7341763da12SFred Isaman 
7351763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7361763da12SFred Isaman 		dreq->flags = 0;
7371763da12SFred Isaman 		dreq->error = hdr->error;
7381763da12SFred Isaman 	}
7391763da12SFred Isaman 	if (dreq->error != 0)
7401763da12SFred Isaman 		bit = NFS_IOHDR_ERROR;
7411763da12SFred Isaman 	else {
7421763da12SFred Isaman 		dreq->count += hdr->good_bytes;
7431763da12SFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
7441763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7451763da12SFred Isaman 			bit = NFS_IOHDR_NEED_RESCHED;
7461763da12SFred Isaman 		} else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
7471763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
7481763da12SFred Isaman 				bit = NFS_IOHDR_NEED_RESCHED;
7491763da12SFred Isaman 			else if (dreq->flags == 0) {
7509bce008bSTrond Myklebust 				memcpy(&dreq->verf, hdr->verf,
7511763da12SFred Isaman 				       sizeof(dreq->verf));
7521763da12SFred Isaman 				bit = NFS_IOHDR_NEED_COMMIT;
7531763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7541763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
7559bce008bSTrond Myklebust 				if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
7561763da12SFred Isaman 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7571763da12SFred Isaman 					bit = NFS_IOHDR_NEED_RESCHED;
7581763da12SFred Isaman 				} else
7591763da12SFred Isaman 					bit = NFS_IOHDR_NEED_COMMIT;
7601763da12SFred Isaman 			}
7611763da12SFred Isaman 		}
7621763da12SFred Isaman 	}
7631763da12SFred Isaman 	spin_unlock(&dreq->lock);
7641763da12SFred Isaman 
7651763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7661763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7671763da12SFred Isaman 		nfs_list_remove_request(req);
7681763da12SFred Isaman 		switch (bit) {
7691763da12SFred Isaman 		case NFS_IOHDR_NEED_RESCHED:
7701763da12SFred Isaman 		case NFS_IOHDR_NEED_COMMIT:
77104277086STrond Myklebust 			kref_get(&req->wb_kref);
7721763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
7731763da12SFred Isaman 		}
7741d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7751763da12SFred Isaman 	}
7761763da12SFred Isaman 
7771763da12SFred Isaman out_put:
7781763da12SFred Isaman 	if (put_dreq(dreq))
7791763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
7801763da12SFred Isaman 	hdr->release(hdr);
7811763da12SFred Isaman }
7821763da12SFred Isaman 
7833e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
7843e9e0ca3STrond Myklebust {
7853e9e0ca3STrond Myklebust 	struct nfs_page *req;
7863e9e0ca3STrond Myklebust 
7873e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
7883e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
7893e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
7901d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7913e9e0ca3STrond Myklebust 	}
7923e9e0ca3STrond Myklebust }
7933e9e0ca3STrond Myklebust 
7941763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
7953e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
7961763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
7971763da12SFred Isaman 	.completion = nfs_direct_write_completion,
7981763da12SFred Isaman };
7991763da12SFred Isaman 
80019f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
80119f73787SChuck Lever 					       const struct iovec *iov,
80219f73787SChuck Lever 					       unsigned long nr_segs,
803a564b8f0SMel Gorman 					       loff_t pos, bool uio)
80419f73787SChuck Lever {
8051763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8061d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
80719f73787SChuck Lever 	ssize_t result = 0;
80819f73787SChuck Lever 	size_t requested_bytes = 0;
80919f73787SChuck Lever 	unsigned long seg;
81019f73787SChuck Lever 
811c95908e4SFred Isaman 	NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
8121763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8131763da12SFred Isaman 	desc.pg_dreq = dreq;
81419f73787SChuck Lever 	get_dreq(dreq);
8151d59d61fSTrond Myklebust 	atomic_inc(&inode->i_dio_count);
81619f73787SChuck Lever 
8177acdb026SPeng Tao 	NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
81819f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
81919f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
820a564b8f0SMel Gorman 		result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
82119f73787SChuck Lever 		if (result < 0)
82219f73787SChuck Lever 			break;
82319f73787SChuck Lever 		requested_bytes += result;
82419f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
82519f73787SChuck Lever 			break;
82619f73787SChuck Lever 		pos += vec->iov_len;
82719f73787SChuck Lever 	}
8281763da12SFred Isaman 	nfs_pageio_complete(&desc);
82919f73787SChuck Lever 
830839f7ad6SChuck Lever 	/*
831839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
832839f7ad6SChuck Lever 	 * generic layer handle the completion.
833839f7ad6SChuck Lever 	 */
834839f7ad6SChuck Lever 	if (requested_bytes == 0) {
8351d59d61fSTrond Myklebust 		inode_dio_done(inode);
836839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
837839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
838839f7ad6SChuck Lever 	}
839839f7ad6SChuck Lever 
84019f73787SChuck Lever 	if (put_dreq(dreq))
84119f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
84219f73787SChuck Lever 	return 0;
84319f73787SChuck Lever }
84419f73787SChuck Lever 
845c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
846c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
847a564b8f0SMel Gorman 				size_t count, bool uio)
848462d5b32SChuck Lever {
849f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
850c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
851462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
852b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
853462d5b32SChuck Lever 
854607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
855462d5b32SChuck Lever 	if (!dreq)
856f11ac8dbSTrond Myklebust 		goto out;
857462d5b32SChuck Lever 
858c89f2ee5SChuck Lever 	dreq->inode = inode;
85935754bc0SPeng Tao 	dreq->bytes_left = count;
860cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
861b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
862b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
863b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
864f11ac8dbSTrond Myklebust 		goto out_release;
865b3c54de6STrond Myklebust 	}
866b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
867c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
868c89f2ee5SChuck Lever 		dreq->iocb = iocb;
869462d5b32SChuck Lever 
870a564b8f0SMel Gorman 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
871607f31e8STrond Myklebust 	if (!result)
872c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
873f11ac8dbSTrond Myklebust out_release:
874b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
875f11ac8dbSTrond Myklebust out:
8761da177e4SLinus Torvalds 	return result;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /**
8801da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8811da177e4SLinus Torvalds  * @iocb: target I/O control block
882027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
883027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
88488467055SChuck Lever  * @pos: byte offset in file where reading starts
8851da177e4SLinus Torvalds  *
8861da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
8871da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
8881da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
8891da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
8901da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
89188467055SChuck Lever  * READ where the file size could change.  Our preference is simply
8921da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
8931da177e4SLinus Torvalds  * care of managing the end of file boundary.
8941da177e4SLinus Torvalds  *
8951da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
8961da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
8971da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8981da177e4SLinus Torvalds  * cache.
8991da177e4SLinus Torvalds  */
900027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
901a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
9041da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9051da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
906c216fd70SChuck Lever 	size_t count;
9071da177e4SLinus Torvalds 
908c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
909c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
910c216fd70SChuck Lever 
9116de1472fSAl Viro 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
9126de1472fSAl Viro 		file, count, (long long) pos);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	retval = 0;
9151da177e4SLinus Torvalds 	if (!count)
9161da177e4SLinus Torvalds 		goto out;
9171da177e4SLinus Torvalds 
91829884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9191da177e4SLinus Torvalds 	if (retval)
9201da177e4SLinus Torvalds 		goto out;
9211da177e4SLinus Torvalds 
9227ec10f26SKonstantin Khlebnikov 	task_io_account_read(count);
9237ec10f26SKonstantin Khlebnikov 
924a564b8f0SMel Gorman 	retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
9251da177e4SLinus Torvalds 	if (retval > 0)
9260cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds out:
9291da177e4SLinus Torvalds 	return retval;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds /**
9331da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9341da177e4SLinus Torvalds  * @iocb: target I/O control block
935027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
936027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
93788467055SChuck Lever  * @pos: byte offset in file where writing starts
9381da177e4SLinus Torvalds  *
9391da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9401da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9411da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9421da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9431da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9441da177e4SLinus Torvalds  * parameter checking and report problems.
9451da177e4SLinus Torvalds  *
9461da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9491da177e4SLinus Torvalds  * readers of this file.
9501da177e4SLinus Torvalds  *
9511da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9521da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9531da177e4SLinus Torvalds  */
954027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
955a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
9561da177e4SLinus Torvalds {
957070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9581da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9591da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
960c216fd70SChuck Lever 	size_t count;
9611da177e4SLinus Torvalds 
962c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
963c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
964c216fd70SChuck Lever 
9656de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
9666de1472fSAl Viro 		file, count, (long long) pos);
967027445c3SBadari Pulavarty 
968ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
969ce1a8e67SChuck Lever 	if (retval)
9701da177e4SLinus Torvalds 		goto out;
971ce1a8e67SChuck Lever 
972ce1a8e67SChuck Lever 	retval = -EINVAL;
973ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9741da177e4SLinus Torvalds 		goto out;
9751da177e4SLinus Torvalds 	retval = 0;
9761da177e4SLinus Torvalds 	if (!count)
9771da177e4SLinus Torvalds 		goto out;
978ce1a8e67SChuck Lever 
97929884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9801da177e4SLinus Torvalds 	if (retval)
9811da177e4SLinus Torvalds 		goto out;
9821da177e4SLinus Torvalds 
9837ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
9847ec10f26SKonstantin Khlebnikov 
985a564b8f0SMel Gorman 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
9861763da12SFred Isaman 	if (retval > 0) {
9871763da12SFred Isaman 		struct inode *inode = mapping->host;
9889eafa8ccSChuck Lever 
989ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9901763da12SFred Isaman 		spin_lock(&inode->i_lock);
9911763da12SFred Isaman 		if (i_size_read(inode) < iocb->ki_pos)
9921763da12SFred Isaman 			i_size_write(inode, iocb->ki_pos);
9931763da12SFred Isaman 		spin_unlock(&inode->i_lock);
9941763da12SFred Isaman 	}
9951da177e4SLinus Torvalds out:
9961da177e4SLinus Torvalds 	return retval;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
99988467055SChuck Lever /**
100088467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
100188467055SChuck Lever  *
100288467055SChuck Lever  */
1003f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10041da177e4SLinus Torvalds {
10051da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10061da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1007fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1008fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
100920c2df83SPaul Mundt 						NULL);
10101da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10111da177e4SLinus Torvalds 		return -ENOMEM;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	return 0;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
101688467055SChuck Lever /**
1017f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
101888467055SChuck Lever  *
101988467055SChuck Lever  */
1020266bee88SDavid Brownell void nfs_destroy_directcache(void)
10211da177e4SLinus Torvalds {
10221a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10231da177e4SLinus Torvalds }
1024