xref: /openbmc/linux/fs/nfs/direct.c (revision 5002c58639d41b93e800c8a4b7eca49c40d57822)
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 
111*5002c586SWeston Andros Adamson /*
112*5002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
113*5002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
114*5002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
115*5002c586SWeston Andros Adamson  * @ds_idx - index of data server in data server list, only valid if ds_clp set
116*5002c586SWeston Andros Adamson  *
117*5002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
118*5002c586SWeston Andros Adamson  */
119*5002c586SWeston Andros Adamson static struct nfs_writeverf *
120*5002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
121*5002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
122*5002c586SWeston Andros Adamson 		       int ds_idx)
123*5002c586SWeston Andros Adamson {
124*5002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
125*5002c586SWeston Andros Adamson 
126*5002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
127*5002c586SWeston Andros Adamson 	if (ds_clp) {
128*5002c586SWeston Andros Adamson 		/* pNFS is in use, use the DS verf */
129*5002c586SWeston Andros Adamson 		if (ds_idx >= 0 && ds_idx < dreq->ds_cinfo.nbuckets)
130*5002c586SWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[ds_idx].direct_verf;
131*5002c586SWeston Andros Adamson 		else
132*5002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
133*5002c586SWeston Andros Adamson 	}
134*5002c586SWeston Andros Adamson #endif
135*5002c586SWeston Andros Adamson 	return verfp;
136*5002c586SWeston Andros Adamson }
137*5002c586SWeston Andros Adamson 
138*5002c586SWeston Andros Adamson 
139*5002c586SWeston Andros Adamson /*
140*5002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
141*5002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
142*5002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
143*5002c586SWeston Andros Adamson  *
144*5002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
145*5002c586SWeston Andros Adamson  */
146*5002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
147*5002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
148*5002c586SWeston Andros Adamson {
149*5002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
150*5002c586SWeston Andros Adamson 
151*5002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->data->ds_clp,
152*5002c586SWeston Andros Adamson 				      hdr->data->ds_idx);
153*5002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
154*5002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
155*5002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
156*5002c586SWeston Andros Adamson }
157*5002c586SWeston Andros Adamson 
158*5002c586SWeston Andros Adamson /*
159*5002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
160*5002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
161*5002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
162*5002c586SWeston Andros Adamson  *
163*5002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
164*5002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
165*5002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
166*5002c586SWeston Andros Adamson  */
167*5002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
168*5002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
169*5002c586SWeston Andros Adamson {
170*5002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
171*5002c586SWeston Andros Adamson 
172*5002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->data->ds_clp,
173*5002c586SWeston Andros Adamson 					 hdr->data->ds_idx);
174*5002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
175*5002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
176*5002c586SWeston Andros Adamson 		return 0;
177*5002c586SWeston Andros Adamson 	}
178*5002c586SWeston Andros Adamson 	return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
179*5002c586SWeston Andros Adamson }
180*5002c586SWeston Andros Adamson 
181*5002c586SWeston Andros Adamson #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
182*5002c586SWeston Andros Adamson /*
183*5002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
184*5002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
185*5002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
186*5002c586SWeston Andros Adamson  *
187*5002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
188*5002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
189*5002c586SWeston Andros Adamson  */
190*5002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
191*5002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
192*5002c586SWeston Andros Adamson {
193*5002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
194*5002c586SWeston Andros Adamson 
195*5002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
196*5002c586SWeston Andros Adamson 					 data->ds_commit_index);
197*5002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
198*5002c586SWeston Andros Adamson 	return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf));
199*5002c586SWeston Andros Adamson }
200*5002c586SWeston Andros Adamson #endif
201*5002c586SWeston Andros Adamson 
2021da177e4SLinus Torvalds /**
203b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
204b8a32e2bSChuck Lever  * @rw: direction (read or write)
205b8a32e2bSChuck Lever  * @iocb: target I/O control block
206b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
207b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
208b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
209b8a32e2bSChuck Lever  *
210b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
211a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
212a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
213a564b8f0SMel Gorman  * so this method is only ever called for swap.
2141da177e4SLinus Torvalds  */
215b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
216b8a32e2bSChuck Lever {
217a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP
2186de1472fSAl Viro 	dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n",
2196de1472fSAl Viro 			iocb->ki_filp, (long long) pos, nr_segs);
220b8a32e2bSChuck Lever 
221b8a32e2bSChuck Lever 	return -EINVAL;
222a564b8f0SMel Gorman #else
223a564b8f0SMel Gorman 	VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
224a564b8f0SMel Gorman 
225a564b8f0SMel Gorman 	if (rw == READ || rw == KERNEL_READ)
226a564b8f0SMel Gorman 		return nfs_file_direct_read(iocb, iov, nr_segs, pos,
227a564b8f0SMel Gorman 				rw == READ ? true : false);
228a564b8f0SMel Gorman 	return nfs_file_direct_write(iocb, iov, nr_segs, pos,
229a564b8f0SMel Gorman 				rw == WRITE ? true : false);
230a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */
231b8a32e2bSChuck Lever }
232b8a32e2bSChuck Lever 
233749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2349c93ab7dSChuck Lever {
235749e146eSChuck Lever 	unsigned int i;
236607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
237607f31e8STrond Myklebust 		page_cache_release(pages[i]);
2386b45d858STrond Myklebust }
2396b45d858STrond Myklebust 
2401763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
2411763da12SFred Isaman 			      struct nfs_direct_req *dreq)
2421763da12SFred Isaman {
2431763da12SFred Isaman 	cinfo->lock = &dreq->lock;
2441763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
2451763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
2461763da12SFred Isaman 	cinfo->dreq = dreq;
2471763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
2481763da12SFred Isaman }
2491763da12SFred Isaman 
25093619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2531da177e4SLinus Torvalds 
254292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
2551da177e4SLinus Torvalds 	if (!dreq)
2561da177e4SLinus Torvalds 		return NULL;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	kref_init(&dreq->kref);
259607f31e8STrond Myklebust 	kref_get(&dreq->kref);
260d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
2611763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
262*5002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
2631763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
26415ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
26593619e59SChuck Lever 
26693619e59SChuck Lever 	return dreq;
26793619e59SChuck Lever }
26893619e59SChuck Lever 
269b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
272a8881f5aSTrond Myklebust 
273f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
274f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
275a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
276a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2771da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
281b4946ffbSTrond Myklebust {
282b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
283b4946ffbSTrond Myklebust }
284b4946ffbSTrond Myklebust 
2856296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
2866296556fSPeng Tao {
2876296556fSPeng Tao 	return dreq->bytes_left;
2886296556fSPeng Tao }
2896296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
2906296556fSPeng Tao 
291d4cc948bSChuck Lever /*
292bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
293bc0fb201SChuck Lever  */
294bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
295bc0fb201SChuck Lever {
29615ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
297bc0fb201SChuck Lever 
298bc0fb201SChuck Lever 	/* Async requests don't wait here */
299bc0fb201SChuck Lever 	if (dreq->iocb)
300bc0fb201SChuck Lever 		goto out;
301bc0fb201SChuck Lever 
302150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
303bc0fb201SChuck Lever 
304bc0fb201SChuck Lever 	if (!result)
30515ce4a0cSChuck Lever 		result = dreq->error;
306bc0fb201SChuck Lever 	if (!result)
30715ce4a0cSChuck Lever 		result = dreq->count;
308bc0fb201SChuck Lever 
309bc0fb201SChuck Lever out:
310bc0fb201SChuck Lever 	return (ssize_t) result;
311bc0fb201SChuck Lever }
312bc0fb201SChuck Lever 
313bc0fb201SChuck Lever /*
314607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
315607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
31663ab46abSChuck Lever  */
3179811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
31863ab46abSChuck Lever {
3199811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3209811cd57SChristoph Hellwig 
3212a009ec9SChristoph Hellwig 	if (dreq->iocb && write) {
3229811cd57SChristoph Hellwig 		loff_t pos = dreq->iocb->ki_pos + dreq->count;
3239811cd57SChristoph Hellwig 
3249811cd57SChristoph Hellwig 		spin_lock(&inode->i_lock);
3259811cd57SChristoph Hellwig 		if (i_size_read(inode) < pos)
3269811cd57SChristoph Hellwig 			i_size_write(inode, pos);
3279811cd57SChristoph Hellwig 		spin_unlock(&inode->i_lock);
3289811cd57SChristoph Hellwig 	}
3299811cd57SChristoph Hellwig 
3301f90ee27SChristoph Hellwig 	if (write)
3312a009ec9SChristoph Hellwig 		nfs_zap_mapping(inode, inode->i_mapping);
3321f90ee27SChristoph Hellwig 
3332a009ec9SChristoph Hellwig 	inode_dio_done(inode);
3342a009ec9SChristoph Hellwig 
3352a009ec9SChristoph Hellwig 	if (dreq->iocb) {
3362a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
3372a009ec9SChristoph Hellwig 		if (!res)
3382a009ec9SChristoph Hellwig 			res = (long) dreq->count;
33963ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
340d72b7a6bSTrond Myklebust 	}
3412a009ec9SChristoph Hellwig 
342d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
34363ab46abSChuck Lever 
344b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
34563ab46abSChuck Lever }
34663ab46abSChuck Lever 
3471385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
3481da177e4SLinus Torvalds {
3491e8968c5SNiels de Vos 	dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
350584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
3511e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
352584aa810SFred Isaman 		req->wb_bytes,
353584aa810SFred Isaman 		(long long)req_offset(req));
354584aa810SFred Isaman 	nfs_release_request(req);
355fdd1e74cSTrond Myklebust }
356fdd1e74cSTrond Myklebust 
357584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
358fdd1e74cSTrond Myklebust {
359584aa810SFred Isaman 	unsigned long bytes = 0;
360584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
361fdd1e74cSTrond Myklebust 
362584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
363584aa810SFred Isaman 		goto out_put;
3641da177e4SLinus Torvalds 
36515ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
366584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
367584aa810SFred Isaman 		dreq->error = hdr->error;
368584aa810SFred Isaman 	else
369584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
37015ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3711da177e4SLinus Torvalds 
372584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
373584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
374584aa810SFred Isaman 		struct page *page = req->wb_page;
375584aa810SFred Isaman 
376be7e9858SJeff Layton 		if (!PageCompound(page) && bytes < hdr->good_bytes)
3774bd8b010STrond Myklebust 			set_page_dirty(page);
378584aa810SFred Isaman 		bytes += req->wb_bytes;
379584aa810SFred Isaman 		nfs_list_remove_request(req);
380584aa810SFred Isaman 		nfs_direct_readpage_release(req);
381584aa810SFred Isaman 	}
382584aa810SFred Isaman out_put:
383607f31e8STrond Myklebust 	if (put_dreq(dreq))
3849811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
385584aa810SFred Isaman 	hdr->release(hdr);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3883e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
389cd841605SFred Isaman {
390584aa810SFred Isaman 	struct nfs_page *req;
391cd841605SFred Isaman 
392584aa810SFred Isaman 	while (!list_empty(head)) {
393584aa810SFred Isaman 		req = nfs_list_entry(head->next);
394584aa810SFred Isaman 		nfs_list_remove_request(req);
395584aa810SFred Isaman 		nfs_release_request(req);
396cd841605SFred Isaman 	}
397584aa810SFred Isaman }
398584aa810SFred Isaman 
399584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
400584aa810SFred Isaman {
401584aa810SFred Isaman 	get_dreq(hdr->dreq);
402584aa810SFred Isaman }
403584aa810SFred Isaman 
404584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4053e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
406584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
407584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
408584aa810SFred Isaman };
409cd841605SFred Isaman 
410d4cc948bSChuck Lever /*
411607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
412607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
413607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
414607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
415607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4161da177e4SLinus Torvalds  */
417584aa810SFred Isaman static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
41802fe4946SChuck Lever 						const struct iovec *iov,
419a564b8f0SMel Gorman 						loff_t pos, bool uio)
4201da177e4SLinus Torvalds {
421584aa810SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
422a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
4233d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
42402fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
42502fe4946SChuck Lever 	size_t count = iov->iov_len;
4265dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
427607f31e8STrond Myklebust 	unsigned int pgbase;
428607f31e8STrond Myklebust 	int result;
429607f31e8STrond Myklebust 	ssize_t started = 0;
430584aa810SFred Isaman 	struct page **pagevec = NULL;
431584aa810SFred Isaman 	unsigned int npages;
43282b145c5SChuck Lever 
4331da177e4SLinus Torvalds 	do {
4345dd602f2SChuck Lever 		size_t bytes;
435584aa810SFred Isaman 		int i;
4361da177e4SLinus Torvalds 
437e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
438bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
439e9f7bee1STrond Myklebust 
440607f31e8STrond Myklebust 		result = -ENOMEM;
441584aa810SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
442584aa810SFred Isaman 		if (!pagevec)
443584aa810SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *),
444584aa810SFred Isaman 					  GFP_KERNEL);
445584aa810SFred Isaman 		if (!pagevec)
446607f31e8STrond Myklebust 			break;
447a564b8f0SMel Gorman 		if (uio) {
448607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
449607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
450584aa810SFred Isaman 					npages, 1, 0, pagevec, NULL);
451607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
452584aa810SFred Isaman 			if (result < 0)
453749e146eSChuck Lever 				break;
454a564b8f0SMel Gorman 		} else {
455a564b8f0SMel Gorman 			WARN_ON(npages != 1);
456a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 1, pagevec);
457a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
458a564b8f0SMel Gorman 				break;
459a564b8f0SMel Gorman 		}
460a564b8f0SMel Gorman 
461584aa810SFred Isaman 		if ((unsigned)result < npages) {
462d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
463d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
464584aa810SFred Isaman 				nfs_direct_release_pages(pagevec, result);
465607f31e8STrond Myklebust 				break;
466607f31e8STrond Myklebust 			}
467d9df8d6bSTrond Myklebust 			bytes -= pgbase;
468584aa810SFred Isaman 			npages = result;
469d9df8d6bSTrond Myklebust 		}
47006cf6f2eSChuck Lever 
471584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
472584aa810SFred Isaman 			struct nfs_page *req;
473bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
474584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
4752bfc6e56SWeston Andros Adamson 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
476584aa810SFred Isaman 						 pgbase, req_len);
477584aa810SFred Isaman 			if (IS_ERR(req)) {
478584aa810SFred Isaman 				result = PTR_ERR(req);
479dbae4c73STrond Myklebust 				break;
480584aa810SFred Isaman 			}
481584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
482584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
483584aa810SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
484584aa810SFred Isaman 				result = desc->pg_error;
485584aa810SFred Isaman 				nfs_release_request(req);
486584aa810SFred Isaman 				break;
487584aa810SFred Isaman 			}
488584aa810SFred Isaman 			pgbase = 0;
489584aa810SFred Isaman 			bytes -= req_len;
490584aa810SFred Isaman 			started += req_len;
491584aa810SFred Isaman 			user_addr += req_len;
492584aa810SFred Isaman 			pos += req_len;
493584aa810SFred Isaman 			count -= req_len;
49435754bc0SPeng Tao 			dreq->bytes_left -= req_len;
495584aa810SFred Isaman 		}
4966d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
4976d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
49871e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
499607f31e8STrond Myklebust 
500584aa810SFred Isaman 	kfree(pagevec);
501584aa810SFred Isaman 
502607f31e8STrond Myklebust 	if (started)
503c216fd70SChuck Lever 		return started;
504607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
50506cf6f2eSChuck Lever }
50606cf6f2eSChuck Lever 
50719f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
50819f73787SChuck Lever 					      const struct iovec *iov,
50919f73787SChuck Lever 					      unsigned long nr_segs,
510a564b8f0SMel Gorman 					      loff_t pos, bool uio)
51119f73787SChuck Lever {
512584aa810SFred Isaman 	struct nfs_pageio_descriptor desc;
5131f90ee27SChristoph Hellwig 	struct inode *inode = dreq->inode;
51419f73787SChuck Lever 	ssize_t result = -EINVAL;
51519f73787SChuck Lever 	size_t requested_bytes = 0;
51619f73787SChuck Lever 	unsigned long seg;
51719f73787SChuck Lever 
518fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&desc, dreq->inode, false,
519584aa810SFred Isaman 			     &nfs_direct_read_completion_ops);
52019f73787SChuck Lever 	get_dreq(dreq);
521584aa810SFred Isaman 	desc.pg_dreq = dreq;
5221f90ee27SChristoph Hellwig 	atomic_inc(&inode->i_dio_count);
52319f73787SChuck Lever 
52419f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
52519f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
526a564b8f0SMel Gorman 		result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
52719f73787SChuck Lever 		if (result < 0)
52819f73787SChuck Lever 			break;
52919f73787SChuck Lever 		requested_bytes += result;
53019f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
53119f73787SChuck Lever 			break;
53219f73787SChuck Lever 		pos += vec->iov_len;
53319f73787SChuck Lever 	}
53419f73787SChuck Lever 
535584aa810SFred Isaman 	nfs_pageio_complete(&desc);
536584aa810SFred Isaman 
537839f7ad6SChuck Lever 	/*
538839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
539839f7ad6SChuck Lever 	 * generic layer handle the completion.
540839f7ad6SChuck Lever 	 */
541839f7ad6SChuck Lever 	if (requested_bytes == 0) {
5421f90ee27SChristoph Hellwig 		inode_dio_done(inode);
543839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
544839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
545839f7ad6SChuck Lever 	}
546839f7ad6SChuck Lever 
54719f73787SChuck Lever 	if (put_dreq(dreq))
5489811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
54919f73787SChuck Lever 	return 0;
55019f73787SChuck Lever }
55119f73787SChuck Lever 
55214a3ec79SChristoph Hellwig /**
55314a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
55414a3ec79SChristoph Hellwig  * @iocb: target I/O control block
55514a3ec79SChristoph Hellwig  * @iov: vector of user buffers into which to read data
55614a3ec79SChristoph Hellwig  * @nr_segs: size of iov vector
55714a3ec79SChristoph Hellwig  * @pos: byte offset in file where reading starts
55814a3ec79SChristoph Hellwig  *
55914a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
56014a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
56114a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
56214a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
56314a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
56414a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
56514a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
56614a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
56714a3ec79SChristoph Hellwig  *
56814a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
56914a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
57014a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
57114a3ec79SChristoph Hellwig  * cache.
57214a3ec79SChristoph Hellwig  */
57314a3ec79SChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
574a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
5751da177e4SLinus Torvalds {
57614a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
57714a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
57814a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5791da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
580b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
58114a3ec79SChristoph Hellwig 	ssize_t result = -EINVAL;
58214a3ec79SChristoph Hellwig 	size_t count;
5831da177e4SLinus Torvalds 
58414a3ec79SChristoph Hellwig 	count = iov_length(iov, nr_segs);
58514a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
58614a3ec79SChristoph Hellwig 
58714a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
58814a3ec79SChristoph Hellwig 		file, count, (long long) pos);
58914a3ec79SChristoph Hellwig 
59014a3ec79SChristoph Hellwig 	result = 0;
59114a3ec79SChristoph Hellwig 	if (!count)
59214a3ec79SChristoph Hellwig 		goto out;
59314a3ec79SChristoph Hellwig 
594d0b9875dSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
59514a3ec79SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
59614a3ec79SChristoph Hellwig 	if (result)
597d0b9875dSChristoph Hellwig 		goto out_unlock;
59814a3ec79SChristoph Hellwig 
59914a3ec79SChristoph Hellwig 	task_io_account_read(count);
60014a3ec79SChristoph Hellwig 
60114a3ec79SChristoph Hellwig 	result = -ENOMEM;
602607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
603f11ac8dbSTrond Myklebust 	if (dreq == NULL)
604d0b9875dSChristoph Hellwig 		goto out_unlock;
6051da177e4SLinus Torvalds 
60691d5b470SChuck Lever 	dreq->inode = inode;
60735754bc0SPeng Tao 	dreq->bytes_left = iov_length(iov, nr_segs);
608cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
609b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
610b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
611b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
612f11ac8dbSTrond Myklebust 		goto out_release;
613b3c54de6STrond Myklebust 	}
614b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
615487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
616487b8372SChuck Lever 		dreq->iocb = iocb;
6171da177e4SLinus Torvalds 
6187acdb026SPeng Tao 	NFS_I(inode)->read_io += iov_length(iov, nr_segs);
619a564b8f0SMel Gorman 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
620d0b9875dSChristoph Hellwig 
621d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
622d0b9875dSChristoph Hellwig 
62314a3ec79SChristoph Hellwig 	if (!result) {
624bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
62514a3ec79SChristoph Hellwig 		if (result > 0)
62614a3ec79SChristoph Hellwig 			iocb->ki_pos = pos + result;
62714a3ec79SChristoph Hellwig 	}
628d0b9875dSChristoph Hellwig 
629d0b9875dSChristoph Hellwig 	nfs_direct_req_release(dreq);
630d0b9875dSChristoph Hellwig 	return result;
631d0b9875dSChristoph Hellwig 
632f11ac8dbSTrond Myklebust out_release:
633b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
634d0b9875dSChristoph Hellwig out_unlock:
635d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
636f11ac8dbSTrond Myklebust out:
6371da177e4SLinus Torvalds 	return result;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
64089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
641fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
6421da177e4SLinus Torvalds {
6431763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
6441763da12SFred Isaman 	struct nfs_page *req, *tmp;
6451763da12SFred Isaman 	LIST_HEAD(reqs);
6461763da12SFred Isaman 	struct nfs_commit_info cinfo;
6471763da12SFred Isaman 	LIST_HEAD(failed);
6481763da12SFred Isaman 
6491763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6501763da12SFred Isaman 	pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
6511763da12SFred Isaman 	spin_lock(cinfo.lock);
6521763da12SFred Isaman 	nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
6531763da12SFred Isaman 	spin_unlock(cinfo.lock);
6541da177e4SLinus Torvalds 
655fad61490STrond Myklebust 	dreq->count = 0;
656607f31e8STrond Myklebust 	get_dreq(dreq);
6571da177e4SLinus Torvalds 
658a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6591763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6601763da12SFred Isaman 	desc.pg_dreq = dreq;
661607f31e8STrond Myklebust 
6621763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
6631763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
6644035c248STrond Myklebust 			nfs_list_remove_request(req);
6651763da12SFred Isaman 			nfs_list_add_request(req, &failed);
6661763da12SFred Isaman 			spin_lock(cinfo.lock);
6671763da12SFred Isaman 			dreq->flags = 0;
6681763da12SFred Isaman 			dreq->error = -EIO;
6691763da12SFred Isaman 			spin_unlock(cinfo.lock);
6701763da12SFred Isaman 		}
6715a695da2STrond Myklebust 		nfs_release_request(req);
6721763da12SFred Isaman 	}
6731763da12SFred Isaman 	nfs_pageio_complete(&desc);
674607f31e8STrond Myklebust 
6754035c248STrond Myklebust 	while (!list_empty(&failed)) {
6764035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
6774035c248STrond Myklebust 		nfs_list_remove_request(req);
6781d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
6794035c248STrond Myklebust 	}
680607f31e8STrond Myklebust 
681607f31e8STrond Myklebust 	if (put_dreq(dreq))
6821763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
683fad61490STrond Myklebust }
6841da177e4SLinus Torvalds 
6851763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
686fad61490STrond Myklebust {
6870b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
6881763da12SFred Isaman 	struct nfs_commit_info cinfo;
6891763da12SFred Isaman 	struct nfs_page *req;
690c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
691c9d8f89dSTrond Myklebust 
6921763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
693c9d8f89dSTrond Myklebust 	if (status < 0) {
69460fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
695c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
696fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
697*5002c586SWeston Andros Adamson 	} else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
698c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
699fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
700fad61490STrond Myklebust 	}
701fad61490STrond Myklebust 
702c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
7031763da12SFred Isaman 	while (!list_empty(&data->pages)) {
7041763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
7051763da12SFred Isaman 		nfs_list_remove_request(req);
7061763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
7071763da12SFred Isaman 			/* Note the rewrite will go through mds */
7081763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
709906369e4SFred Isaman 		} else
710906369e4SFred Isaman 			nfs_release_request(req);
7111d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
712fad61490STrond Myklebust 	}
713fad61490STrond Myklebust 
7141763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
7151763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
7161763da12SFred Isaman }
7171763da12SFred Isaman 
7181763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
7191763da12SFred Isaman {
7201763da12SFred Isaman 	/* There is no lock to clear */
7211763da12SFred Isaman }
7221763da12SFred Isaman 
7231763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
7241763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
7251763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
726fad61490STrond Myklebust };
727fad61490STrond Myklebust 
728fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
729fad61490STrond Myklebust {
7301763da12SFred Isaman 	int res;
7311763da12SFred Isaman 	struct nfs_commit_info cinfo;
7321763da12SFred Isaman 	LIST_HEAD(mds_list);
733fad61490STrond Myklebust 
7341763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7351763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
7361763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
7371763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
7381763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
7421da177e4SLinus Torvalds {
7431763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
744fad61490STrond Myklebust 	int flags = dreq->flags;
7451da177e4SLinus Torvalds 
746fad61490STrond Myklebust 	dreq->flags = 0;
747fad61490STrond Myklebust 	switch (flags) {
748fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
749fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
7501da177e4SLinus Torvalds 			break;
751fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
752fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
7531da177e4SLinus Torvalds 			break;
7541da177e4SLinus Torvalds 		default:
7559811cd57SChristoph Hellwig 			nfs_direct_complete(dreq, true);
7561da177e4SLinus Torvalds 	}
757fad61490STrond Myklebust }
758fad61490STrond Myklebust 
7591763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
760fad61490STrond Myklebust {
7611763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
762fad61490STrond Myklebust }
7631763da12SFred Isaman 
764fad61490STrond Myklebust #else
76524fc9211SBryan Schumaker static void nfs_direct_write_schedule_work(struct work_struct *work)
76624fc9211SBryan Schumaker {
76724fc9211SBryan Schumaker }
768fad61490STrond Myklebust 
769fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
770fad61490STrond Myklebust {
7719811cd57SChristoph Hellwig 	nfs_direct_complete(dreq, true);
772fad61490STrond Myklebust }
773fad61490STrond Myklebust #endif
774fad61490STrond Myklebust 
775c9d8f89dSTrond Myklebust /*
776c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
777c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
778c9d8f89dSTrond Myklebust  */
779462d5b32SChuck Lever /*
780607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
781607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
782607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
783607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
784607f31e8STrond Myklebust  * no requests have been sent, just return an error.
785462d5b32SChuck Lever  */
7861763da12SFred Isaman static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
78702fe4946SChuck Lever 						 const struct iovec *iov,
788a564b8f0SMel Gorman 						 loff_t pos, bool uio)
789462d5b32SChuck Lever {
7901763da12SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
791a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
7923d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
79302fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
79402fe4946SChuck Lever 	size_t count = iov->iov_len;
795462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
796607f31e8STrond Myklebust 	unsigned int pgbase;
797607f31e8STrond Myklebust 	int result;
798607f31e8STrond Myklebust 	ssize_t started = 0;
7991763da12SFred Isaman 	struct page **pagevec = NULL;
8001763da12SFred Isaman 	unsigned int npages;
80182b145c5SChuck Lever 
802462d5b32SChuck Lever 	do {
803462d5b32SChuck Lever 		size_t bytes;
8041763da12SFred Isaman 		int i;
805462d5b32SChuck Lever 
806e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
807bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
808e9f7bee1STrond Myklebust 
809607f31e8STrond Myklebust 		result = -ENOMEM;
8101763da12SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
8111763da12SFred Isaman 		if (!pagevec)
8121763da12SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
8131763da12SFred Isaman 		if (!pagevec)
814607f31e8STrond Myklebust 			break;
815607f31e8STrond Myklebust 
816a564b8f0SMel Gorman 		if (uio) {
817607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
818607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
8191763da12SFred Isaman 						npages, 0, 0, pagevec, NULL);
820607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
8211763da12SFred Isaman 			if (result < 0)
822749e146eSChuck Lever 				break;
823a564b8f0SMel Gorman 		} else {
824a564b8f0SMel Gorman 			WARN_ON(npages != 1);
825a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 0, pagevec);
826a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
827a564b8f0SMel Gorman 				break;
828a564b8f0SMel Gorman 		}
8291763da12SFred Isaman 
8301763da12SFred Isaman 		if ((unsigned)result < npages) {
831d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
832d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
8331763da12SFred Isaman 				nfs_direct_release_pages(pagevec, result);
834607f31e8STrond Myklebust 				break;
835607f31e8STrond Myklebust 			}
836d9df8d6bSTrond Myklebust 			bytes -= pgbase;
8371763da12SFred Isaman 			npages = result;
838d9df8d6bSTrond Myklebust 		}
839607f31e8STrond Myklebust 
8401763da12SFred Isaman 		for (i = 0; i < npages; i++) {
8411763da12SFred Isaman 			struct nfs_page *req;
842bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
843607f31e8STrond Myklebust 
8442bfc6e56SWeston Andros Adamson 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
8451763da12SFred Isaman 						 pgbase, req_len);
8461763da12SFred Isaman 			if (IS_ERR(req)) {
8471763da12SFred Isaman 				result = PTR_ERR(req);
848dbae4c73STrond Myklebust 				break;
8491763da12SFred Isaman 			}
8501763da12SFred Isaman 			nfs_lock_request(req);
8511763da12SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
8521763da12SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
8531763da12SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
8541763da12SFred Isaman 				result = desc->pg_error;
8551d1afcbcSTrond Myklebust 				nfs_unlock_and_release_request(req);
85671e8cc00STrond Myklebust 				break;
8571763da12SFred Isaman 			}
8581763da12SFred Isaman 			pgbase = 0;
8591763da12SFred Isaman 			bytes -= req_len;
8601763da12SFred Isaman 			started += req_len;
8611763da12SFred Isaman 			user_addr += req_len;
8621763da12SFred Isaman 			pos += req_len;
8631763da12SFred Isaman 			count -= req_len;
86435754bc0SPeng Tao 			dreq->bytes_left -= req_len;
8651763da12SFred Isaman 		}
8666d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
8676d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
86871e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
869607f31e8STrond Myklebust 
8701763da12SFred Isaman 	kfree(pagevec);
8711763da12SFred Isaman 
872607f31e8STrond Myklebust 	if (started)
873c216fd70SChuck Lever 		return started;
874607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
87506cf6f2eSChuck Lever }
87606cf6f2eSChuck Lever 
8771763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
8781763da12SFred Isaman {
8791763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
8801763da12SFred Isaman 	struct nfs_commit_info cinfo;
8811763da12SFred Isaman 	int bit = -1;
8821763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
8831763da12SFred Isaman 
8841763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
8851763da12SFred Isaman 		goto out_put;
8861763da12SFred Isaman 
8871763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
8881763da12SFred Isaman 
8891763da12SFred Isaman 	spin_lock(&dreq->lock);
8901763da12SFred Isaman 
8911763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
8921763da12SFred Isaman 		dreq->flags = 0;
8931763da12SFred Isaman 		dreq->error = hdr->error;
8941763da12SFred Isaman 	}
8951763da12SFred Isaman 	if (dreq->error != 0)
8961763da12SFred Isaman 		bit = NFS_IOHDR_ERROR;
8971763da12SFred Isaman 	else {
8981763da12SFred Isaman 		dreq->count += hdr->good_bytes;
8991763da12SFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
9001763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
9011763da12SFred Isaman 			bit = NFS_IOHDR_NEED_RESCHED;
9021763da12SFred Isaman 		} else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
9031763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
9041763da12SFred Isaman 				bit = NFS_IOHDR_NEED_RESCHED;
9051763da12SFred Isaman 			else if (dreq->flags == 0) {
906*5002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
9071763da12SFred Isaman 				bit = NFS_IOHDR_NEED_COMMIT;
9081763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
9091763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
910*5002c586SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) {
911*5002c586SWeston Andros Adamson 					dreq->flags =
912*5002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
9131763da12SFred Isaman 					bit = NFS_IOHDR_NEED_RESCHED;
9141763da12SFred Isaman 				} else
9151763da12SFred Isaman 					bit = NFS_IOHDR_NEED_COMMIT;
9161763da12SFred Isaman 			}
9171763da12SFred Isaman 		}
9181763da12SFred Isaman 	}
9191763da12SFred Isaman 	spin_unlock(&dreq->lock);
9201763da12SFred Isaman 
9211763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
9222bfc6e56SWeston Andros Adamson 		bool do_destroy = true;
9232bfc6e56SWeston Andros Adamson 
9241763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
9251763da12SFred Isaman 		nfs_list_remove_request(req);
9261763da12SFred Isaman 		switch (bit) {
9271763da12SFred Isaman 		case NFS_IOHDR_NEED_RESCHED:
9281763da12SFred Isaman 		case NFS_IOHDR_NEED_COMMIT:
92904277086STrond Myklebust 			kref_get(&req->wb_kref);
9301763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
9312bfc6e56SWeston Andros Adamson 			do_destroy = false;
9321763da12SFred Isaman 		}
9331d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
9341763da12SFred Isaman 	}
9351763da12SFred Isaman 
9361763da12SFred Isaman out_put:
9371763da12SFred Isaman 	if (put_dreq(dreq))
9381763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
9391763da12SFred Isaman 	hdr->release(hdr);
9401763da12SFred Isaman }
9411763da12SFred Isaman 
9423e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
9433e9e0ca3STrond Myklebust {
9443e9e0ca3STrond Myklebust 	struct nfs_page *req;
9453e9e0ca3STrond Myklebust 
9463e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
9473e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
9483e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
9491d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
9503e9e0ca3STrond Myklebust 	}
9513e9e0ca3STrond Myklebust }
9523e9e0ca3STrond Myklebust 
9531763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
9543e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
9551763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
9561763da12SFred Isaman 	.completion = nfs_direct_write_completion,
9571763da12SFred Isaman };
9581763da12SFred Isaman 
95919f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
96019f73787SChuck Lever 					       const struct iovec *iov,
96119f73787SChuck Lever 					       unsigned long nr_segs,
962a564b8f0SMel Gorman 					       loff_t pos, bool uio)
96319f73787SChuck Lever {
9641763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
9651d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
96619f73787SChuck Lever 	ssize_t result = 0;
96719f73787SChuck Lever 	size_t requested_bytes = 0;
96819f73787SChuck Lever 	unsigned long seg;
96919f73787SChuck Lever 
970a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
9711763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
9721763da12SFred Isaman 	desc.pg_dreq = dreq;
97319f73787SChuck Lever 	get_dreq(dreq);
9741d59d61fSTrond Myklebust 	atomic_inc(&inode->i_dio_count);
97519f73787SChuck Lever 
9767acdb026SPeng Tao 	NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
97719f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
97819f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
979a564b8f0SMel Gorman 		result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
98019f73787SChuck Lever 		if (result < 0)
98119f73787SChuck Lever 			break;
98219f73787SChuck Lever 		requested_bytes += result;
98319f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
98419f73787SChuck Lever 			break;
98519f73787SChuck Lever 		pos += vec->iov_len;
98619f73787SChuck Lever 	}
9871763da12SFred Isaman 	nfs_pageio_complete(&desc);
98819f73787SChuck Lever 
989839f7ad6SChuck Lever 	/*
990839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
991839f7ad6SChuck Lever 	 * generic layer handle the completion.
992839f7ad6SChuck Lever 	 */
993839f7ad6SChuck Lever 	if (requested_bytes == 0) {
9941d59d61fSTrond Myklebust 		inode_dio_done(inode);
995839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
996839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
997839f7ad6SChuck Lever 	}
998839f7ad6SChuck Lever 
99919f73787SChuck Lever 	if (put_dreq(dreq))
100019f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
100119f73787SChuck Lever 	return 0;
100219f73787SChuck Lever }
100319f73787SChuck Lever 
10041da177e4SLinus Torvalds /**
10051da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
10061da177e4SLinus Torvalds  * @iocb: target I/O control block
1007027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
1008027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
100988467055SChuck Lever  * @pos: byte offset in file where writing starts
10101da177e4SLinus Torvalds  *
10111da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
10121da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
10131da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
10141da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
10151da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
10161da177e4SLinus Torvalds  * parameter checking and report problems.
10171da177e4SLinus Torvalds  *
10181da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
10191da177e4SLinus Torvalds  *
10201da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
10211da177e4SLinus Torvalds  * readers of this file.
10221da177e4SLinus Torvalds  *
10231da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
10241da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
10251da177e4SLinus Torvalds  */
1026027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
1027a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
10281da177e4SLinus Torvalds {
102922cd1bf1SChristoph Hellwig 	ssize_t result = -EINVAL;
10301da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
10311da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
103222cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
103322cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
103422cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
1035a9ab5e84SChristoph Hellwig 	loff_t end;
1036c216fd70SChuck Lever 	size_t count;
10371da177e4SLinus Torvalds 
1038c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
1039a9ab5e84SChristoph Hellwig 	end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
1040a9ab5e84SChristoph Hellwig 
1041c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
1042c216fd70SChuck Lever 
10436de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
10446de1472fSAl Viro 		file, count, (long long) pos);
1045027445c3SBadari Pulavarty 
104622cd1bf1SChristoph Hellwig 	result = generic_write_checks(file, &pos, &count, 0);
104722cd1bf1SChristoph Hellwig 	if (result)
10481da177e4SLinus Torvalds 		goto out;
1049ce1a8e67SChuck Lever 
105022cd1bf1SChristoph Hellwig 	result = -EINVAL;
1051ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
10521da177e4SLinus Torvalds 		goto out;
105322cd1bf1SChristoph Hellwig 	result = 0;
10541da177e4SLinus Torvalds 	if (!count)
10551da177e4SLinus Torvalds 		goto out;
1056ce1a8e67SChuck Lever 
1057a9ab5e84SChristoph Hellwig 	mutex_lock(&inode->i_mutex);
1058a9ab5e84SChristoph Hellwig 
105922cd1bf1SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
106022cd1bf1SChristoph Hellwig 	if (result)
1061a9ab5e84SChristoph Hellwig 		goto out_unlock;
1062a9ab5e84SChristoph Hellwig 
1063a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1064a9ab5e84SChristoph Hellwig 		result = invalidate_inode_pages2_range(mapping,
1065a9ab5e84SChristoph Hellwig 					pos >> PAGE_CACHE_SHIFT, end);
1066a9ab5e84SChristoph Hellwig 		if (result)
1067a9ab5e84SChristoph Hellwig 			goto out_unlock;
1068a9ab5e84SChristoph Hellwig 	}
10691da177e4SLinus Torvalds 
10707ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
10717ec10f26SKonstantin Khlebnikov 
107222cd1bf1SChristoph Hellwig 	result = -ENOMEM;
107322cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
107422cd1bf1SChristoph Hellwig 	if (!dreq)
1075a9ab5e84SChristoph Hellwig 		goto out_unlock;
107622cd1bf1SChristoph Hellwig 
107722cd1bf1SChristoph Hellwig 	dreq->inode = inode;
107822cd1bf1SChristoph Hellwig 	dreq->bytes_left = count;
107922cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
108022cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
108122cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
108222cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
108322cd1bf1SChristoph Hellwig 		goto out_release;
108422cd1bf1SChristoph Hellwig 	}
108522cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
108622cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
108722cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
108822cd1bf1SChristoph Hellwig 
108922cd1bf1SChristoph Hellwig 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
1090a9ab5e84SChristoph Hellwig 
1091a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1092a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
1093a9ab5e84SChristoph Hellwig 					      pos >> PAGE_CACHE_SHIFT, end);
1094a9ab5e84SChristoph Hellwig 	}
1095a9ab5e84SChristoph Hellwig 
1096a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
1097a9ab5e84SChristoph Hellwig 
109822cd1bf1SChristoph Hellwig 	if (!result) {
109922cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
110022cd1bf1SChristoph Hellwig 		if (result > 0) {
11011763da12SFred Isaman 			struct inode *inode = mapping->host;
11029eafa8ccSChuck Lever 
110322cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
11041763da12SFred Isaman 			spin_lock(&inode->i_lock);
11051763da12SFred Isaman 			if (i_size_read(inode) < iocb->ki_pos)
11061763da12SFred Isaman 				i_size_write(inode, iocb->ki_pos);
11071763da12SFred Isaman 			spin_unlock(&inode->i_lock);
11081763da12SFred Isaman 		}
110922cd1bf1SChristoph Hellwig 	}
1110a9ab5e84SChristoph Hellwig 	nfs_direct_req_release(dreq);
1111a9ab5e84SChristoph Hellwig 	return result;
1112a9ab5e84SChristoph Hellwig 
111322cd1bf1SChristoph Hellwig out_release:
111422cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
1115a9ab5e84SChristoph Hellwig out_unlock:
1116a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
11171da177e4SLinus Torvalds out:
111822cd1bf1SChristoph Hellwig 	return result;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
112188467055SChuck Lever /**
112288467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
112388467055SChuck Lever  *
112488467055SChuck Lever  */
1125f7b422b1SDavid Howells int __init nfs_init_directcache(void)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
11281da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1129fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1130fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
113120c2df83SPaul Mundt 						NULL);
11321da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
11331da177e4SLinus Torvalds 		return -ENOMEM;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	return 0;
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
113888467055SChuck Lever /**
1139f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
114088467055SChuck Lever  *
114188467055SChuck Lever  */
1142266bee88SDavid Brownell void nfs_destroy_directcache(void)
11431da177e4SLinus Torvalds {
11441a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
11451da177e4SLinus Torvalds }
1146