xref: /openbmc/linux/fs/nfs/direct.c (revision 031d73ed768a40684f3ca21992265ffdb6a270bf)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * There are important applications whose performance or correctness
101da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
111da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
121da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
131da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
141da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
151da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
161da177e4SLinus Torvalds  * need to cache the contents of a file.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
191da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
201da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
211da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
221da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
231da177e4SLinus Torvalds  * an application.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
261da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
271da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
281da177e4SLinus Torvalds  * an undocumented mount option.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
311da177e4SLinus Torvalds  * help from Andrew Morton.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
341da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
351da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
361da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
371da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3888467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/file.h>
461da177e4SLinus Torvalds #include <linux/pagemap.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
497ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h>
506296556fSPeng Tao #include <linux/module.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/nfs_fs.h>
531da177e4SLinus Torvalds #include <linux/nfs_page.h>
541da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
551da177e4SLinus Torvalds 
567c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
5760063497SArun Sharma #include <linux/atomic.h>
581da177e4SLinus Torvalds 
598d5658c9STrond Myklebust #include "internal.h"
6091d5b470SChuck Lever #include "iostat.h"
611763da12SFred Isaman #include "pnfs.h"
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
641da177e4SLinus Torvalds 
65e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
691da177e4SLinus Torvalds  */
700a00b77bSWeston Andros Adamson struct nfs_direct_mirror {
710a00b77bSWeston Andros Adamson 	ssize_t count;
720a00b77bSWeston Andros Adamson };
730a00b77bSWeston Andros Adamson 
741da177e4SLinus Torvalds struct nfs_direct_req {
751da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7615ce4a0cSChuck Lever 
7715ce4a0cSChuck Lever 	/* I/O parameters */
78a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
79f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
8099514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
8188467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
8215ce4a0cSChuck Lever 
8315ce4a0cSChuck Lever 	/* completion state */
84607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
8515ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
860a00b77bSWeston Andros Adamson 
870a00b77bSWeston Andros Adamson 	struct nfs_direct_mirror mirrors[NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX];
880a00b77bSWeston Andros Adamson 	int			mirror_count;
890a00b77bSWeston Andros Adamson 
90d9ee6553STrond Myklebust 	loff_t			io_start;	/* Start offset for I/O */
9115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
92ed3743a6SWeston Andros Adamson 				max_count,	/* max expected count */
9335754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
941da177e4SLinus Torvalds 				error;		/* any reported error */
95d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
96fad61490STrond Myklebust 
97fad61490STrond Myklebust 	/* commit state */
981763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
991763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
1001763da12SFred Isaman 	struct work_struct	work;
101fad61490STrond Myklebust 	int			flags;
102ad3cba22SDave Kleikamp 	/* for write */
103fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
104fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
105ad3cba22SDave Kleikamp 	/* for read */
106ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY	(3)	/* dirty user-space page after read */
107fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
1111763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
1124d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
1131763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
114607f31e8STrond Myklebust 
115607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
116607f31e8STrond Myklebust {
117607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
118607f31e8STrond Myklebust }
119607f31e8STrond Myklebust 
120607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
121607f31e8STrond Myklebust {
122607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
123607f31e8STrond Myklebust }
124607f31e8STrond Myklebust 
1250a00b77bSWeston Andros Adamson static void
126*031d73edSTrond Myklebust nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
127*031d73edSTrond Myklebust 			    const struct nfs_pgio_header *hdr,
128*031d73edSTrond Myklebust 			    ssize_t dreq_len)
1290a00b77bSWeston Andros Adamson {
130*031d73edSTrond Myklebust 	struct nfs_direct_mirror *mirror = &dreq->mirrors[hdr->pgio_mirror_idx];
1310a00b77bSWeston Andros Adamson 
132*031d73edSTrond Myklebust 	if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
133*031d73edSTrond Myklebust 	      test_bit(NFS_IOHDR_EOF, &hdr->flags)))
134*031d73edSTrond Myklebust 		return;
135*031d73edSTrond Myklebust 	if (dreq->max_count >= dreq_len) {
136*031d73edSTrond Myklebust 		dreq->max_count = dreq_len;
137*031d73edSTrond Myklebust 		if (dreq->count > dreq_len)
138*031d73edSTrond Myklebust 			dreq->count = dreq_len;
139ed3743a6SWeston Andros Adamson 
140*031d73edSTrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags))
141*031d73edSTrond Myklebust 			dreq->error = hdr->error;
142*031d73edSTrond Myklebust 		else /* Clear outstanding error if this is EOF */
143*031d73edSTrond Myklebust 			dreq->error = 0;
1445fadeb47SPeng Tao 	}
145*031d73edSTrond Myklebust 	if (mirror->count > dreq_len)
146*031d73edSTrond Myklebust 		mirror->count = dreq_len;
1470a00b77bSWeston Andros Adamson }
148*031d73edSTrond Myklebust 
149*031d73edSTrond Myklebust static void
150*031d73edSTrond Myklebust nfs_direct_count_bytes(struct nfs_direct_req *dreq,
151*031d73edSTrond Myklebust 		       const struct nfs_pgio_header *hdr)
152*031d73edSTrond Myklebust {
153*031d73edSTrond Myklebust 	struct nfs_direct_mirror *mirror = &dreq->mirrors[hdr->pgio_mirror_idx];
154*031d73edSTrond Myklebust 	loff_t hdr_end = hdr->io_start + hdr->good_bytes;
155*031d73edSTrond Myklebust 	ssize_t dreq_len = 0;
156*031d73edSTrond Myklebust 
157*031d73edSTrond Myklebust 	if (hdr_end > dreq->io_start)
158*031d73edSTrond Myklebust 		dreq_len = hdr_end - dreq->io_start;
159*031d73edSTrond Myklebust 
160*031d73edSTrond Myklebust 	nfs_direct_handle_truncated(dreq, hdr, dreq_len);
161*031d73edSTrond Myklebust 
162*031d73edSTrond Myklebust 	if (dreq_len > dreq->max_count)
163*031d73edSTrond Myklebust 		dreq_len = dreq->max_count;
164*031d73edSTrond Myklebust 
165*031d73edSTrond Myklebust 	if (mirror->count < dreq_len)
166*031d73edSTrond Myklebust 		mirror->count = dreq_len;
167*031d73edSTrond Myklebust 	if (dreq->count < dreq_len)
168*031d73edSTrond Myklebust 		dreq->count = dreq_len;
1691ccbad9fSPeng Tao }
1700a00b77bSWeston Andros Adamson 
1715002c586SWeston Andros Adamson /*
1725002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
1735002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1745002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
1756cccbb6fSWeston Andros Adamson  * @commit_idx - commit bucket index for the DS
1765002c586SWeston Andros Adamson  *
1775002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
1785002c586SWeston Andros Adamson  */
1795002c586SWeston Andros Adamson static struct nfs_writeverf *
1805002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
1815002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
1826cccbb6fSWeston Andros Adamson 		       int commit_idx)
1835002c586SWeston Andros Adamson {
1845002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
1855002c586SWeston Andros Adamson 
1865002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
187834e465bSKinglong Mee 	/*
188834e465bSKinglong Mee 	 * pNFS is in use, use the DS verf except commit_through_mds is set
189834e465bSKinglong Mee 	 * for layout segment where nbuckets is zero.
190834e465bSKinglong Mee 	 */
191834e465bSKinglong Mee 	if (ds_clp && dreq->ds_cinfo.nbuckets > 0) {
1926cccbb6fSWeston Andros Adamson 		if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
1936cccbb6fSWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
1945002c586SWeston Andros Adamson 		else
1955002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
1965002c586SWeston Andros Adamson 	}
1975002c586SWeston Andros Adamson #endif
1985002c586SWeston Andros Adamson 	return verfp;
1995002c586SWeston Andros Adamson }
2005002c586SWeston Andros Adamson 
2015002c586SWeston Andros Adamson 
2025002c586SWeston Andros Adamson /*
2035002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
2045002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2055002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
2065002c586SWeston Andros Adamson  *
2075002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
2085002c586SWeston Andros Adamson  */
2095002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
2105002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
2115002c586SWeston Andros Adamson {
2125002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2135002c586SWeston Andros Adamson 
2146cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
2155002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
2165002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
2175002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
2185002c586SWeston Andros Adamson }
2195002c586SWeston Andros Adamson 
2208fc3c386STrond Myklebust static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1,
2218fc3c386STrond Myklebust 		const struct nfs_writeverf *v2)
2228fc3c386STrond Myklebust {
2238fc3c386STrond Myklebust 	return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier);
2248fc3c386STrond Myklebust }
2258fc3c386STrond Myklebust 
2265002c586SWeston Andros Adamson /*
2275002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
2285002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2295002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
2305002c586SWeston Andros Adamson  *
2315002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
2325002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
2335002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
2345002c586SWeston Andros Adamson  */
2355002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
2365002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
2375002c586SWeston Andros Adamson {
2385002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2395002c586SWeston Andros Adamson 
2406cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
2415002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
2425002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
2435002c586SWeston Andros Adamson 		return 0;
2445002c586SWeston Andros Adamson 	}
2458fc3c386STrond Myklebust 	return nfs_direct_cmp_verf(verfp, &hdr->verf);
2465002c586SWeston Andros Adamson }
2475002c586SWeston Andros Adamson 
2485002c586SWeston Andros Adamson /*
2495002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
2505002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2515002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
2525002c586SWeston Andros Adamson  *
2535002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
2545002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
2555002c586SWeston Andros Adamson  */
2565002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
2575002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
2585002c586SWeston Andros Adamson {
2595002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2605002c586SWeston Andros Adamson 
2615002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
2625002c586SWeston Andros Adamson 					 data->ds_commit_index);
26380c76fe3SWeston Andros Adamson 
26480c76fe3SWeston Andros Adamson 	/* verifier not set so always fail */
26580c76fe3SWeston Andros Adamson 	if (verfp->committed < 0)
26680c76fe3SWeston Andros Adamson 		return 1;
26780c76fe3SWeston Andros Adamson 
2688fc3c386STrond Myklebust 	return nfs_direct_cmp_verf(verfp, &data->verf);
2695002c586SWeston Andros Adamson }
2705002c586SWeston Andros Adamson 
2711da177e4SLinus Torvalds /**
272b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
273b8a32e2bSChuck Lever  * @iocb: target I/O control block
27490090ae6SAl Viro  * @iter: I/O buffer
275b8a32e2bSChuck Lever  *
276b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
277a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
278a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
279a564b8f0SMel Gorman  * so this method is only ever called for swap.
2801da177e4SLinus Torvalds  */
281c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
282b8a32e2bSChuck Lever {
283ee8a1a8bSPeng Tao 	struct inode *inode = iocb->ki_filp->f_mapping->host;
284ee8a1a8bSPeng Tao 
285ee8a1a8bSPeng Tao 	/* we only support swap file calling nfs_direct_IO */
286ee8a1a8bSPeng Tao 	if (!IS_SWAPFILE(inode))
287ee8a1a8bSPeng Tao 		return 0;
288ee8a1a8bSPeng Tao 
28966ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
290a564b8f0SMel Gorman 
2916f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
292c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, iter);
29365a4a1caSAl Viro 	return nfs_file_direct_write(iocb, iter);
294b8a32e2bSChuck Lever }
295b8a32e2bSChuck Lever 
296749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2979c93ab7dSChuck Lever {
298749e146eSChuck Lever 	unsigned int i;
299607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
30009cbfeafSKirill A. Shutemov 		put_page(pages[i]);
3016b45d858STrond Myklebust }
3026b45d858STrond Myklebust 
3031763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
3041763da12SFred Isaman 			      struct nfs_direct_req *dreq)
3051763da12SFred Isaman {
306fe238e60SDave Wysochanski 	cinfo->inode = dreq->inode;
3071763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
3081763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
3091763da12SFred Isaman 	cinfo->dreq = dreq;
3101763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
3111763da12SFred Isaman }
3121763da12SFred Isaman 
3130a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq,
3140a00b77bSWeston Andros Adamson 					     struct nfs_pageio_descriptor *pgio,
3150a00b77bSWeston Andros Adamson 					     struct nfs_page *req)
3160a00b77bSWeston Andros Adamson {
3170a00b77bSWeston Andros Adamson 	int mirror_count = 1;
3180a00b77bSWeston Andros Adamson 
3190a00b77bSWeston Andros Adamson 	if (pgio->pg_ops->pg_get_mirror_count)
3200a00b77bSWeston Andros Adamson 		mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
3210a00b77bSWeston Andros Adamson 
3220a00b77bSWeston Andros Adamson 	dreq->mirror_count = mirror_count;
3230a00b77bSWeston Andros Adamson }
3240a00b77bSWeston Andros Adamson 
32593619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3281da177e4SLinus Torvalds 
329292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
3301da177e4SLinus Torvalds 	if (!dreq)
3311da177e4SLinus Torvalds 		return NULL;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	kref_init(&dreq->kref);
334607f31e8STrond Myklebust 	kref_get(&dreq->kref);
335d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
3361763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
3375002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
3381763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
3390a00b77bSWeston Andros Adamson 	dreq->mirror_count = 1;
34015ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
34193619e59SChuck Lever 
34293619e59SChuck Lever 	return dreq;
34393619e59SChuck Lever }
34493619e59SChuck Lever 
345b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
348a8881f5aSTrond Myklebust 
3498c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
350f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
351f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
352a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
353a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
3541da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
358b4946ffbSTrond Myklebust {
359b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
360b4946ffbSTrond Myklebust }
361b4946ffbSTrond Myklebust 
3626296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
3636296556fSPeng Tao {
3646296556fSPeng Tao 	return dreq->bytes_left;
3656296556fSPeng Tao }
3666296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
3676296556fSPeng Tao 
368d4cc948bSChuck Lever /*
369bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
370bc0fb201SChuck Lever  */
371bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
372bc0fb201SChuck Lever {
37315ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
374bc0fb201SChuck Lever 
375bc0fb201SChuck Lever 	/* Async requests don't wait here */
376bc0fb201SChuck Lever 	if (dreq->iocb)
377bc0fb201SChuck Lever 		goto out;
378bc0fb201SChuck Lever 
379150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
380bc0fb201SChuck Lever 
381d2a7de0bSTrond Myklebust 	if (!result) {
382d2a7de0bSTrond Myklebust 		result = dreq->count;
383d2a7de0bSTrond Myklebust 		WARN_ON_ONCE(dreq->count < 0);
384d2a7de0bSTrond Myklebust 	}
385bc0fb201SChuck Lever 	if (!result)
38615ce4a0cSChuck Lever 		result = dreq->error;
387bc0fb201SChuck Lever 
388bc0fb201SChuck Lever out:
389bc0fb201SChuck Lever 	return (ssize_t) result;
390bc0fb201SChuck Lever }
391bc0fb201SChuck Lever 
392bc0fb201SChuck Lever /*
393607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
394607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
39563ab46abSChuck Lever  */
396f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq)
39763ab46abSChuck Lever {
3989811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3999811cd57SChristoph Hellwig 
400fe0f07d0SJens Axboe 	inode_dio_end(inode);
4012a009ec9SChristoph Hellwig 
4022a009ec9SChristoph Hellwig 	if (dreq->iocb) {
4032a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
404d2a7de0bSTrond Myklebust 		if (dreq->count != 0) {
4052a009ec9SChristoph Hellwig 			res = (long) dreq->count;
406d2a7de0bSTrond Myklebust 			WARN_ON_ONCE(dreq->count < 0);
407d2a7de0bSTrond Myklebust 		}
40804b2fa9fSChristoph Hellwig 		dreq->iocb->ki_complete(dreq->iocb, res, 0);
409d72b7a6bSTrond Myklebust 	}
4102a009ec9SChristoph Hellwig 
411024de8f1SDaniel Wagner 	complete(&dreq->completion);
41263ab46abSChuck Lever 
413b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
41463ab46abSChuck Lever }
41563ab46abSChuck Lever 
416584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
417fdd1e74cSTrond Myklebust {
418584aa810SFred Isaman 	unsigned long bytes = 0;
419584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
420fdd1e74cSTrond Myklebust 
42115ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
422eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
423eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
424eb2c50daSTrond Myklebust 		goto out_put;
425eb2c50daSTrond Myklebust 	}
426eb2c50daSTrond Myklebust 
427*031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
42815ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
4291da177e4SLinus Torvalds 
430584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
431584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
432584aa810SFred Isaman 		struct page *page = req->wb_page;
433584aa810SFred Isaman 
434ad3cba22SDave Kleikamp 		if (!PageCompound(page) && bytes < hdr->good_bytes &&
435ad3cba22SDave Kleikamp 		    (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
4364bd8b010STrond Myklebust 			set_page_dirty(page);
437584aa810SFred Isaman 		bytes += req->wb_bytes;
438584aa810SFred Isaman 		nfs_list_remove_request(req);
439beeb5338SAnna Schumaker 		nfs_release_request(req);
440584aa810SFred Isaman 	}
441584aa810SFred Isaman out_put:
442607f31e8STrond Myklebust 	if (put_dreq(dreq))
443f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
444584aa810SFred Isaman 	hdr->release(hdr);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
447df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error)
448cd841605SFred Isaman {
449584aa810SFred Isaman 	struct nfs_page *req;
450cd841605SFred Isaman 
451584aa810SFred Isaman 	while (!list_empty(head)) {
452584aa810SFred Isaman 		req = nfs_list_entry(head->next);
453584aa810SFred Isaman 		nfs_list_remove_request(req);
454584aa810SFred Isaman 		nfs_release_request(req);
455cd841605SFred Isaman 	}
456584aa810SFred Isaman }
457584aa810SFred Isaman 
458584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
459584aa810SFred Isaman {
460584aa810SFred Isaman 	get_dreq(hdr->dreq);
461584aa810SFred Isaman }
462584aa810SFred Isaman 
463584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4643e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
465584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
466584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
467584aa810SFred Isaman };
468cd841605SFred Isaman 
469d4cc948bSChuck Lever /*
470607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
471607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
472607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
473607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
474607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4751da177e4SLinus Torvalds  */
47691f79c43SAl Viro 
47791f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
47891f79c43SAl Viro 					      struct iov_iter *iter,
47991f79c43SAl Viro 					      loff_t pos)
4801da177e4SLinus Torvalds {
48191f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
48291f79c43SAl Viro 	struct inode *inode = dreq->inode;
48391f79c43SAl Viro 	ssize_t result = -EINVAL;
48491f79c43SAl Viro 	size_t requested_bytes = 0;
48591f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
48682b145c5SChuck Lever 
48716b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
48891f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
48991f79c43SAl Viro 	get_dreq(dreq);
49091f79c43SAl Viro 	desc.pg_dreq = dreq;
491fe0f07d0SJens Axboe 	inode_dio_begin(inode);
49291f79c43SAl Viro 
49391f79c43SAl Viro 	while (iov_iter_count(iter)) {
49491f79c43SAl Viro 		struct page **pagevec;
4955dd602f2SChuck Lever 		size_t bytes;
49691f79c43SAl Viro 		size_t pgbase;
49791f79c43SAl Viro 		unsigned npages, i;
4981da177e4SLinus Torvalds 
49991f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
50091f79c43SAl Viro 						  rsize, &pgbase);
501584aa810SFred Isaman 		if (result < 0)
502749e146eSChuck Lever 			break;
503a564b8f0SMel Gorman 
50491f79c43SAl Viro 		bytes = result;
50591f79c43SAl Viro 		iov_iter_advance(iter, bytes);
50691f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
507584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
508584aa810SFred Isaman 			struct nfs_page *req;
509bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
510584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
51128b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
512584aa810SFred Isaman 						 pgbase, req_len);
513584aa810SFred Isaman 			if (IS_ERR(req)) {
514584aa810SFred Isaman 				result = PTR_ERR(req);
515dbae4c73STrond Myklebust 				break;
516584aa810SFred Isaman 			}
517584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
518584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
51991f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
52091f79c43SAl Viro 				result = desc.pg_error;
521584aa810SFred Isaman 				nfs_release_request(req);
522584aa810SFred Isaman 				break;
523584aa810SFred Isaman 			}
524584aa810SFred Isaman 			pgbase = 0;
525584aa810SFred Isaman 			bytes -= req_len;
52691f79c43SAl Viro 			requested_bytes += req_len;
527584aa810SFred Isaman 			pos += req_len;
52835754bc0SPeng Tao 			dreq->bytes_left -= req_len;
529584aa810SFred Isaman 		}
5306d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
53191f79c43SAl Viro 		kvfree(pagevec);
53219f73787SChuck Lever 		if (result < 0)
53319f73787SChuck Lever 			break;
53419f73787SChuck Lever 	}
53519f73787SChuck Lever 
536584aa810SFred Isaman 	nfs_pageio_complete(&desc);
537584aa810SFred Isaman 
538839f7ad6SChuck Lever 	/*
539839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
540839f7ad6SChuck Lever 	 * generic layer handle the completion.
541839f7ad6SChuck Lever 	 */
542839f7ad6SChuck Lever 	if (requested_bytes == 0) {
543fe0f07d0SJens Axboe 		inode_dio_end(inode);
544839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
545839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
546839f7ad6SChuck Lever 	}
547839f7ad6SChuck Lever 
54819f73787SChuck Lever 	if (put_dreq(dreq))
549f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
55085128b2bSAl Viro 	return requested_bytes;
55119f73787SChuck Lever }
55219f73787SChuck Lever 
55314a3ec79SChristoph Hellwig /**
55414a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
55514a3ec79SChristoph Hellwig  * @iocb: target I/O control block
556619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
55714a3ec79SChristoph Hellwig  *
55814a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
55914a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
56014a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
56114a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
56214a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
56314a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
56414a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
56514a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
56614a3ec79SChristoph Hellwig  *
56714a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
56814a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
56914a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
57014a3ec79SChristoph Hellwig  * cache.
57114a3ec79SChristoph Hellwig  */
572c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
5731da177e4SLinus Torvalds {
57414a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
57514a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
57614a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5771da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
578b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
57985128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
580a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
58114a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
58214a3ec79SChristoph Hellwig 
58314a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
584c8b8e32dSChristoph Hellwig 		file, count, (long long) iocb->ki_pos);
58514a3ec79SChristoph Hellwig 
58614a3ec79SChristoph Hellwig 	result = 0;
58714a3ec79SChristoph Hellwig 	if (!count)
58814a3ec79SChristoph Hellwig 		goto out;
58914a3ec79SChristoph Hellwig 
59014a3ec79SChristoph Hellwig 	task_io_account_read(count);
59114a3ec79SChristoph Hellwig 
59214a3ec79SChristoph Hellwig 	result = -ENOMEM;
593607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
594f11ac8dbSTrond Myklebust 	if (dreq == NULL)
595a5864c99STrond Myklebust 		goto out;
5961da177e4SLinus Torvalds 
59791d5b470SChuck Lever 	dreq->inode = inode;
598ed3743a6SWeston Andros Adamson 	dreq->bytes_left = dreq->max_count = count;
599c8b8e32dSChristoph Hellwig 	dreq->io_start = iocb->ki_pos;
600cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
601b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
602b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
603b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
604f11ac8dbSTrond Myklebust 		goto out_release;
605b3c54de6STrond Myklebust 	}
606b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
607487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
608487b8372SChuck Lever 		dreq->iocb = iocb;
6091da177e4SLinus Torvalds 
610ad3cba22SDave Kleikamp 	if (iter_is_iovec(iter))
611ad3cba22SDave Kleikamp 		dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
612ad3cba22SDave Kleikamp 
613a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
614a5864c99STrond Myklebust 
615619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
61685128b2bSAl Viro 	requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
617d0b9875dSChristoph Hellwig 
618a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
619d0b9875dSChristoph Hellwig 
62085128b2bSAl Viro 	if (requested > 0) {
621bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
62285128b2bSAl Viro 		if (result > 0) {
62385128b2bSAl Viro 			requested -= result;
624c8b8e32dSChristoph Hellwig 			iocb->ki_pos += result;
62514a3ec79SChristoph Hellwig 		}
62685128b2bSAl Viro 		iov_iter_revert(iter, requested);
62785128b2bSAl Viro 	} else {
62885128b2bSAl Viro 		result = requested;
62985128b2bSAl Viro 	}
630d0b9875dSChristoph Hellwig 
631f11ac8dbSTrond Myklebust out_release:
632b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
633f11ac8dbSTrond Myklebust out:
6341da177e4SLinus Torvalds 	return result;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637085d1e33STom Haynes static void
638085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
639085d1e33STom Haynes 				  struct list_head *list,
640085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
641085d1e33STom Haynes {
642e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
643085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
644085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
645085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
646085d1e33STom Haynes #endif
647085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
648e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
649085d1e33STom Haynes }
650085d1e33STom Haynes 
651fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
6521da177e4SLinus Torvalds {
6531763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
6541763da12SFred Isaman 	struct nfs_page *req, *tmp;
6551763da12SFred Isaman 	LIST_HEAD(reqs);
6561763da12SFred Isaman 	struct nfs_commit_info cinfo;
6571763da12SFred Isaman 	LIST_HEAD(failed);
6580a00b77bSWeston Andros Adamson 	int i;
6591763da12SFred Isaman 
6601763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
661085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
6621da177e4SLinus Torvalds 
663fad61490STrond Myklebust 	dreq->count = 0;
664*031d73edSTrond Myklebust 	dreq->max_count = 0;
665*031d73edSTrond Myklebust 	list_for_each_entry(req, &reqs, wb_list)
666*031d73edSTrond Myklebust 		dreq->max_count += req->wb_bytes;
667a5314a74STrond Myklebust 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;
668a5314a74STrond Myklebust 	nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
6690a00b77bSWeston Andros Adamson 	for (i = 0; i < dreq->mirror_count; i++)
6700a00b77bSWeston Andros Adamson 		dreq->mirrors[i].count = 0;
671607f31e8STrond Myklebust 	get_dreq(dreq);
6721da177e4SLinus Torvalds 
673a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6741763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6751763da12SFred Isaman 	desc.pg_dreq = dreq;
676607f31e8STrond Myklebust 
6770a00b77bSWeston Andros Adamson 	req = nfs_list_entry(reqs.next);
6780a00b77bSWeston Andros Adamson 	nfs_direct_setup_mirroring(dreq, &desc, req);
679d600ad1fSPeng Tao 	if (desc.pg_error < 0) {
680d600ad1fSPeng Tao 		list_splice_init(&reqs, &failed);
681d600ad1fSPeng Tao 		goto out_failed;
682d600ad1fSPeng Tao 	}
6830a00b77bSWeston Andros Adamson 
6841763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
68533344e0fSTrond Myklebust 		/* Bump the transmission count */
68633344e0fSTrond Myklebust 		req->wb_nio++;
6871763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
688078b5fd9STrond Myklebust 			nfs_list_move_request(req, &failed);
689fe238e60SDave Wysochanski 			spin_lock(&cinfo.inode->i_lock);
6901763da12SFred Isaman 			dreq->flags = 0;
691d600ad1fSPeng Tao 			if (desc.pg_error < 0)
692d600ad1fSPeng Tao 				dreq->error = desc.pg_error;
693d600ad1fSPeng Tao 			else
6941763da12SFred Isaman 				dreq->error = -EIO;
695fe238e60SDave Wysochanski 			spin_unlock(&cinfo.inode->i_lock);
6961763da12SFred Isaman 		}
6975a695da2STrond Myklebust 		nfs_release_request(req);
6981763da12SFred Isaman 	}
6991763da12SFred Isaman 	nfs_pageio_complete(&desc);
700607f31e8STrond Myklebust 
701d600ad1fSPeng Tao out_failed:
7024035c248STrond Myklebust 	while (!list_empty(&failed)) {
7034035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
7044035c248STrond Myklebust 		nfs_list_remove_request(req);
7051d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7064035c248STrond Myklebust 	}
707607f31e8STrond Myklebust 
708607f31e8STrond Myklebust 	if (put_dreq(dreq))
7094d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
710fad61490STrond Myklebust }
7111da177e4SLinus Torvalds 
7121763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
713fad61490STrond Myklebust {
7140b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
7151763da12SFred Isaman 	struct nfs_commit_info cinfo;
7161763da12SFred Isaman 	struct nfs_page *req;
717c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
718c9d8f89dSTrond Myklebust 
7191763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
720fe4f844dSAnna Schumaker 	if (status < 0 || nfs_direct_cmp_commit_data_verf(dreq, data))
721fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
722fad61490STrond Myklebust 
7231763da12SFred Isaman 	while (!list_empty(&data->pages)) {
7241763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
7251763da12SFred Isaman 		nfs_list_remove_request(req);
7261763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
72733344e0fSTrond Myklebust 			/*
72833344e0fSTrond Myklebust 			 * Despite the reboot, the write was successful,
72933344e0fSTrond Myklebust 			 * so reset wb_nio.
73033344e0fSTrond Myklebust 			 */
73133344e0fSTrond Myklebust 			req->wb_nio = 0;
7321763da12SFred Isaman 			/* Note the rewrite will go through mds */
733b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
734906369e4SFred Isaman 		} else
735906369e4SFred Isaman 			nfs_release_request(req);
7361d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
737fad61490STrond Myklebust 	}
738fad61490STrond Myklebust 
7391763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
7404d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
7411763da12SFred Isaman }
7421763da12SFred Isaman 
743b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
744b20135d0STrond Myklebust 		struct nfs_page *req)
7451763da12SFred Isaman {
746b20135d0STrond Myklebust 	struct nfs_direct_req *dreq = cinfo->dreq;
747b20135d0STrond Myklebust 
748b20135d0STrond Myklebust 	spin_lock(&dreq->lock);
749b20135d0STrond Myklebust 	dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
750b20135d0STrond Myklebust 	spin_unlock(&dreq->lock);
751b20135d0STrond Myklebust 	nfs_mark_request_commit(req, NULL, cinfo, 0);
7521763da12SFred Isaman }
7531763da12SFred Isaman 
7541763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
7551763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
756b20135d0STrond Myklebust 	.resched_write = nfs_direct_resched_write,
757fad61490STrond Myklebust };
758fad61490STrond Myklebust 
759fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
760fad61490STrond Myklebust {
7611763da12SFred Isaman 	int res;
7621763da12SFred Isaman 	struct nfs_commit_info cinfo;
7631763da12SFred Isaman 	LIST_HEAD(mds_list);
764fad61490STrond Myklebust 
7651763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7661763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
7671763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
7681763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
7691763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
7731da177e4SLinus Torvalds {
7741763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
775fad61490STrond Myklebust 	int flags = dreq->flags;
7761da177e4SLinus Torvalds 
777fad61490STrond Myklebust 	dreq->flags = 0;
778fad61490STrond Myklebust 	switch (flags) {
779fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
780fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
7811da177e4SLinus Torvalds 			break;
782fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
783fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		default:
786f7b5c340STrond Myklebust 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
787f7b5c340STrond Myklebust 			nfs_direct_complete(dreq);
7881da177e4SLinus Torvalds 	}
789fad61490STrond Myklebust }
790fad61490STrond Myklebust 
7914d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
792fad61490STrond Myklebust {
79346483c2eSNeilBrown 	queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
794fad61490STrond Myklebust }
7951763da12SFred Isaman 
7961763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7971763da12SFred Isaman {
7981763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7991763da12SFred Isaman 	struct nfs_commit_info cinfo;
800c65e6254SWeston Andros Adamson 	bool request_commit = false;
8011763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
8021763da12SFred Isaman 
8031763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
8041763da12SFred Isaman 
8051763da12SFred Isaman 	spin_lock(&dreq->lock);
806eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
807eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
808eb2c50daSTrond Myklebust 		goto out_put;
809eb2c50daSTrond Myklebust 	}
810eb2c50daSTrond Myklebust 
811*031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
812eb2c50daSTrond Myklebust 	if (hdr->good_bytes != 0) {
813c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
8141763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
815c65e6254SWeston Andros Adamson 				request_commit = true;
8161763da12SFred Isaman 			else if (dreq->flags == 0) {
8175002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
818c65e6254SWeston Andros Adamson 				request_commit = true;
8191763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
8201763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
821c65e6254SWeston Andros Adamson 				request_commit = true;
822c65e6254SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
8235002c586SWeston Andros Adamson 					dreq->flags =
8245002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
8251763da12SFred Isaman 			}
8261763da12SFred Isaman 		}
8271763da12SFred Isaman 	}
8281763da12SFred Isaman 	spin_unlock(&dreq->lock);
8291763da12SFred Isaman 
8301763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
8312bfc6e56SWeston Andros Adamson 
8321763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
8331763da12SFred Isaman 		nfs_list_remove_request(req);
834c65e6254SWeston Andros Adamson 		if (request_commit) {
83504277086STrond Myklebust 			kref_get(&req->wb_kref);
836b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
837b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
8381763da12SFred Isaman 		}
8391d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8401763da12SFred Isaman 	}
8411763da12SFred Isaman 
8421763da12SFred Isaman out_put:
8431763da12SFred Isaman 	if (put_dreq(dreq))
8444d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
8451763da12SFred Isaman 	hdr->release(hdr);
8461763da12SFred Isaman }
8471763da12SFred Isaman 
848df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error)
8493e9e0ca3STrond Myklebust {
8503e9e0ca3STrond Myklebust 	struct nfs_page *req;
8513e9e0ca3STrond Myklebust 
8523e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
8533e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
8543e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
8551d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8563e9e0ca3STrond Myklebust 	}
8573e9e0ca3STrond Myklebust }
8583e9e0ca3STrond Myklebust 
859dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
860dc602dd7STrond Myklebust {
861dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
862dc602dd7STrond Myklebust 
863dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
864dc602dd7STrond Myklebust 	if (dreq->error == 0) {
865dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
866dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
867dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
868dc602dd7STrond Myklebust 		hdr->good_bytes = hdr->args.count;
869dc602dd7STrond Myklebust 	}
870dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
871dc602dd7STrond Myklebust }
872dc602dd7STrond Myklebust 
8731763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
8743e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
8751763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
8761763da12SFred Isaman 	.completion = nfs_direct_write_completion,
877dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
8781763da12SFred Isaman };
8791763da12SFred Isaman 
88091f79c43SAl Viro 
88191f79c43SAl Viro /*
88291f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
88391f79c43SAl Viro  *     errors after the first one are ignored.
88491f79c43SAl Viro  */
88591f79c43SAl Viro /*
88691f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
88791f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
88891f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
88991f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
89091f79c43SAl Viro  * no requests have been sent, just return an error.
89191f79c43SAl Viro  */
89219f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
893619d30b4SAl Viro 					       struct iov_iter *iter,
89491f79c43SAl Viro 					       loff_t pos)
89519f73787SChuck Lever {
8961763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8971d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
89819f73787SChuck Lever 	ssize_t result = 0;
89919f73787SChuck Lever 	size_t requested_bytes = 0;
90091f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
90119f73787SChuck Lever 
902a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
9031763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
9041763da12SFred Isaman 	desc.pg_dreq = dreq;
90519f73787SChuck Lever 	get_dreq(dreq);
906fe0f07d0SJens Axboe 	inode_dio_begin(inode);
90719f73787SChuck Lever 
90891f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
90991f79c43SAl Viro 	while (iov_iter_count(iter)) {
91091f79c43SAl Viro 		struct page **pagevec;
91191f79c43SAl Viro 		size_t bytes;
91291f79c43SAl Viro 		size_t pgbase;
91391f79c43SAl Viro 		unsigned npages, i;
91491f79c43SAl Viro 
91591f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
91691f79c43SAl Viro 						  wsize, &pgbase);
91719f73787SChuck Lever 		if (result < 0)
91819f73787SChuck Lever 			break;
91991f79c43SAl Viro 
92091f79c43SAl Viro 		bytes = result;
92191f79c43SAl Viro 		iov_iter_advance(iter, bytes);
92291f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
92391f79c43SAl Viro 		for (i = 0; i < npages; i++) {
92491f79c43SAl Viro 			struct nfs_page *req;
92591f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
92691f79c43SAl Viro 
92728b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
92891f79c43SAl Viro 						 pgbase, req_len);
92991f79c43SAl Viro 			if (IS_ERR(req)) {
93091f79c43SAl Viro 				result = PTR_ERR(req);
93119f73787SChuck Lever 				break;
93291f79c43SAl Viro 			}
9330a00b77bSWeston Andros Adamson 
9340a00b77bSWeston Andros Adamson 			nfs_direct_setup_mirroring(dreq, &desc, req);
935d600ad1fSPeng Tao 			if (desc.pg_error < 0) {
936d600ad1fSPeng Tao 				nfs_free_request(req);
937d600ad1fSPeng Tao 				result = desc.pg_error;
938d600ad1fSPeng Tao 				break;
939d600ad1fSPeng Tao 			}
9400a00b77bSWeston Andros Adamson 
94191f79c43SAl Viro 			nfs_lock_request(req);
94291f79c43SAl Viro 			req->wb_index = pos >> PAGE_SHIFT;
94391f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
94491f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
94591f79c43SAl Viro 				result = desc.pg_error;
94691f79c43SAl Viro 				nfs_unlock_and_release_request(req);
94791f79c43SAl Viro 				break;
94891f79c43SAl Viro 			}
94991f79c43SAl Viro 			pgbase = 0;
95091f79c43SAl Viro 			bytes -= req_len;
95191f79c43SAl Viro 			requested_bytes += req_len;
95291f79c43SAl Viro 			pos += req_len;
95391f79c43SAl Viro 			dreq->bytes_left -= req_len;
95491f79c43SAl Viro 		}
95591f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
95691f79c43SAl Viro 		kvfree(pagevec);
95791f79c43SAl Viro 		if (result < 0)
95891f79c43SAl Viro 			break;
95919f73787SChuck Lever 	}
9601763da12SFred Isaman 	nfs_pageio_complete(&desc);
96119f73787SChuck Lever 
962839f7ad6SChuck Lever 	/*
963839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
964839f7ad6SChuck Lever 	 * generic layer handle the completion.
965839f7ad6SChuck Lever 	 */
966839f7ad6SChuck Lever 	if (requested_bytes == 0) {
967fe0f07d0SJens Axboe 		inode_dio_end(inode);
968839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
969839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
970839f7ad6SChuck Lever 	}
971839f7ad6SChuck Lever 
97219f73787SChuck Lever 	if (put_dreq(dreq))
9734d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
97485128b2bSAl Viro 	return requested_bytes;
97519f73787SChuck Lever }
97619f73787SChuck Lever 
9771da177e4SLinus Torvalds /**
9781da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9791da177e4SLinus Torvalds  * @iocb: target I/O control block
980619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
9811da177e4SLinus Torvalds  *
9821da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9831da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9841da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9851da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9861da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9871da177e4SLinus Torvalds  * parameter checking and report problems.
9881da177e4SLinus Torvalds  *
9891da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9901da177e4SLinus Torvalds  *
9911da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9921da177e4SLinus Torvalds  * readers of this file.
9931da177e4SLinus Torvalds  *
9941da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9951da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9961da177e4SLinus Torvalds  */
99765a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
9981da177e4SLinus Torvalds {
99985128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
100089698b24STrond Myklebust 	size_t count;
10011da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
10021da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
100322cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
100422cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
100522cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
100665a4a1caSAl Viro 	loff_t pos, end;
1007c216fd70SChuck Lever 
10086de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
10093309dd04SAl Viro 		file, iov_iter_count(iter), (long long) iocb->ki_pos);
1010027445c3SBadari Pulavarty 
101189698b24STrond Myklebust 	result = generic_write_checks(iocb, iter);
101289698b24STrond Myklebust 	if (result <= 0)
101389698b24STrond Myklebust 		return result;
101489698b24STrond Myklebust 	count = result;
101589698b24STrond Myklebust 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
10163309dd04SAl Viro 
10173309dd04SAl Viro 	pos = iocb->ki_pos;
101809cbfeafSKirill A. Shutemov 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
1019ce1a8e67SChuck Lever 
102089698b24STrond Myklebust 	task_io_account_write(count);
10217ec10f26SKonstantin Khlebnikov 
102222cd1bf1SChristoph Hellwig 	result = -ENOMEM;
102322cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
102422cd1bf1SChristoph Hellwig 	if (!dreq)
1025a5864c99STrond Myklebust 		goto out;
102622cd1bf1SChristoph Hellwig 
102722cd1bf1SChristoph Hellwig 	dreq->inode = inode;
102889698b24STrond Myklebust 	dreq->bytes_left = dreq->max_count = count;
10295fadeb47SPeng Tao 	dreq->io_start = pos;
103022cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
103122cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
103222cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
103322cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
103422cd1bf1SChristoph Hellwig 		goto out_release;
103522cd1bf1SChristoph Hellwig 	}
103622cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
103722cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
103822cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
103922cd1bf1SChristoph Hellwig 
1040a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
1041a5864c99STrond Myklebust 
104285128b2bSAl Viro 	requested = nfs_direct_write_schedule_iovec(dreq, iter, pos);
1043a9ab5e84SChristoph Hellwig 
1044a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1045a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
104609cbfeafSKirill A. Shutemov 					      pos >> PAGE_SHIFT, end);
1047a9ab5e84SChristoph Hellwig 	}
1048a9ab5e84SChristoph Hellwig 
1049a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
1050a9ab5e84SChristoph Hellwig 
105185128b2bSAl Viro 	if (requested > 0) {
105222cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
105322cd1bf1SChristoph Hellwig 		if (result > 0) {
105485128b2bSAl Viro 			requested -= result;
105522cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
1056e2592217SChristoph Hellwig 			/* XXX: should check the generic_write_sync retval */
1057e2592217SChristoph Hellwig 			generic_write_sync(iocb, result);
10581763da12SFred Isaman 		}
105985128b2bSAl Viro 		iov_iter_revert(iter, requested);
106085128b2bSAl Viro 	} else {
106185128b2bSAl Viro 		result = requested;
106222cd1bf1SChristoph Hellwig 	}
106322cd1bf1SChristoph Hellwig out_release:
106422cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
1065a5864c99STrond Myklebust out:
106622cd1bf1SChristoph Hellwig 	return result;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
106988467055SChuck Lever /**
107088467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
107188467055SChuck Lever  *
107288467055SChuck Lever  */
1073f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10761da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1077fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1078fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
107920c2df83SPaul Mundt 						NULL);
10801da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10811da177e4SLinus Torvalds 		return -ENOMEM;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	return 0;
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds 
108688467055SChuck Lever /**
1087f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
108888467055SChuck Lever  *
108988467055SChuck Lever  */
1090266bee88SDavid Brownell void nfs_destroy_directcache(void)
10911da177e4SLinus Torvalds {
10921a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10931da177e4SLinus Torvalds }
1094