xref: /openbmc/linux/fs/nfs/direct.c (revision c21e7168848d4ff4158120dbd4464f0d5cfb1456)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * There are important applications whose performance or correctness
101da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
111da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
121da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
131da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
141da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
151da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
161da177e4SLinus Torvalds  * need to cache the contents of a file.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
191da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
201da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
211da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
221da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
231da177e4SLinus Torvalds  * an application.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
261da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
271da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
281da177e4SLinus Torvalds  * an undocumented mount option.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
311da177e4SLinus Torvalds  * help from Andrew Morton.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
341da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
351da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
361da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
371da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3888467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/file.h>
461da177e4SLinus Torvalds #include <linux/pagemap.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
497ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h>
506296556fSPeng Tao #include <linux/module.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/nfs_fs.h>
531da177e4SLinus Torvalds #include <linux/nfs_page.h>
541da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
551da177e4SLinus Torvalds 
567c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
5760063497SArun Sharma #include <linux/atomic.h>
581da177e4SLinus Torvalds 
598d5658c9STrond Myklebust #include "internal.h"
6091d5b470SChuck Lever #include "iostat.h"
611763da12SFred Isaman #include "pnfs.h"
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
641da177e4SLinus Torvalds 
65e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds struct nfs_direct_req {
681da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
6915ce4a0cSChuck Lever 
7015ce4a0cSChuck Lever 	/* I/O parameters */
71a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
72f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7399514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7488467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7515ce4a0cSChuck Lever 
7615ce4a0cSChuck Lever 	/* completion state */
77607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7815ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
790a00b77bSWeston Andros Adamson 
80d9ee6553STrond Myklebust 	loff_t			io_start;	/* Start offset for I/O */
8115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
82ed3743a6SWeston Andros Adamson 				max_count,	/* max expected count */
8335754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
841da177e4SLinus Torvalds 				error;		/* any reported error */
85d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
86fad61490STrond Myklebust 
87fad61490STrond Myklebust 	/* commit state */
881763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
891763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
901763da12SFred Isaman 	struct work_struct	work;
91fad61490STrond Myklebust 	int			flags;
92ad3cba22SDave Kleikamp 	/* for write */
93fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
94fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
95ad3cba22SDave Kleikamp 	/* for read */
96ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY	(3)	/* dirty user-space page after read */
97fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
1011763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
1024d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
1031763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
104607f31e8STrond Myklebust 
105607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
106607f31e8STrond Myklebust {
107607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
108607f31e8STrond Myklebust }
109607f31e8STrond Myklebust 
110607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
111607f31e8STrond Myklebust {
112607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
113607f31e8STrond Myklebust }
114607f31e8STrond Myklebust 
1150a00b77bSWeston Andros Adamson static void
116031d73edSTrond Myklebust nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
117031d73edSTrond Myklebust 			    const struct nfs_pgio_header *hdr,
118031d73edSTrond Myklebust 			    ssize_t dreq_len)
1190a00b77bSWeston Andros Adamson {
120031d73edSTrond Myklebust 	if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
121031d73edSTrond Myklebust 	      test_bit(NFS_IOHDR_EOF, &hdr->flags)))
122031d73edSTrond Myklebust 		return;
123031d73edSTrond Myklebust 	if (dreq->max_count >= dreq_len) {
124031d73edSTrond Myklebust 		dreq->max_count = dreq_len;
125031d73edSTrond Myklebust 		if (dreq->count > dreq_len)
126031d73edSTrond Myklebust 			dreq->count = dreq_len;
127ed3743a6SWeston Andros Adamson 
128031d73edSTrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags))
129031d73edSTrond Myklebust 			dreq->error = hdr->error;
130031d73edSTrond Myklebust 		else /* Clear outstanding error if this is EOF */
131031d73edSTrond Myklebust 			dreq->error = 0;
1325fadeb47SPeng Tao 	}
1330a00b77bSWeston Andros Adamson }
134031d73edSTrond Myklebust 
135031d73edSTrond Myklebust static void
136031d73edSTrond Myklebust nfs_direct_count_bytes(struct nfs_direct_req *dreq,
137031d73edSTrond Myklebust 		       const struct nfs_pgio_header *hdr)
138031d73edSTrond Myklebust {
139031d73edSTrond Myklebust 	loff_t hdr_end = hdr->io_start + hdr->good_bytes;
140031d73edSTrond Myklebust 	ssize_t dreq_len = 0;
141031d73edSTrond Myklebust 
142031d73edSTrond Myklebust 	if (hdr_end > dreq->io_start)
143031d73edSTrond Myklebust 		dreq_len = hdr_end - dreq->io_start;
144031d73edSTrond Myklebust 
145031d73edSTrond Myklebust 	nfs_direct_handle_truncated(dreq, hdr, dreq_len);
146031d73edSTrond Myklebust 
147031d73edSTrond Myklebust 	if (dreq_len > dreq->max_count)
148031d73edSTrond Myklebust 		dreq_len = dreq->max_count;
149031d73edSTrond Myklebust 
150031d73edSTrond Myklebust 	if (dreq->count < dreq_len)
151031d73edSTrond Myklebust 		dreq->count = dreq_len;
1521ccbad9fSPeng Tao }
1530a00b77bSWeston Andros Adamson 
1545002c586SWeston Andros Adamson /*
1555002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
1565002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1575002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
1586cccbb6fSWeston Andros Adamson  * @commit_idx - commit bucket index for the DS
1595002c586SWeston Andros Adamson  *
1605002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
1615002c586SWeston Andros Adamson  */
1625002c586SWeston Andros Adamson static struct nfs_writeverf *
1635002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
1645002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
1656cccbb6fSWeston Andros Adamson 		       int commit_idx)
1665002c586SWeston Andros Adamson {
1675002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
1685002c586SWeston Andros Adamson 
1695002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
170834e465bSKinglong Mee 	/*
171834e465bSKinglong Mee 	 * pNFS is in use, use the DS verf except commit_through_mds is set
172834e465bSKinglong Mee 	 * for layout segment where nbuckets is zero.
173834e465bSKinglong Mee 	 */
174834e465bSKinglong Mee 	if (ds_clp && dreq->ds_cinfo.nbuckets > 0) {
1756cccbb6fSWeston Andros Adamson 		if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
1766cccbb6fSWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
1775002c586SWeston Andros Adamson 		else
1785002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
1795002c586SWeston Andros Adamson 	}
1805002c586SWeston Andros Adamson #endif
1815002c586SWeston Andros Adamson 	return verfp;
1825002c586SWeston Andros Adamson }
1835002c586SWeston Andros Adamson 
1845002c586SWeston Andros Adamson 
1855002c586SWeston Andros Adamson /*
1865002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
1875002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1885002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
1895002c586SWeston Andros Adamson  *
1905002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
1915002c586SWeston Andros Adamson  */
1925002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
1935002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
1945002c586SWeston Andros Adamson {
1955002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1965002c586SWeston Andros Adamson 
1976cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
1985002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
1995002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
2005002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
2015002c586SWeston Andros Adamson }
2025002c586SWeston Andros Adamson 
2038fc3c386STrond Myklebust static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1,
2048fc3c386STrond Myklebust 		const struct nfs_writeverf *v2)
2058fc3c386STrond Myklebust {
2068fc3c386STrond Myklebust 	return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier);
2078fc3c386STrond Myklebust }
2088fc3c386STrond Myklebust 
2095002c586SWeston Andros Adamson /*
2105002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
2115002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2125002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
2135002c586SWeston Andros Adamson  *
2145002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
2155002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
2165002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
2175002c586SWeston Andros Adamson  */
2185002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
2195002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
2205002c586SWeston Andros Adamson {
2215002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2225002c586SWeston Andros Adamson 
2236cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
2245002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
2255002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
2265002c586SWeston Andros Adamson 		return 0;
2275002c586SWeston Andros Adamson 	}
2288fc3c386STrond Myklebust 	return nfs_direct_cmp_verf(verfp, &hdr->verf);
2295002c586SWeston Andros Adamson }
2305002c586SWeston Andros Adamson 
2315002c586SWeston Andros Adamson /*
2325002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
2335002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2345002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
2355002c586SWeston Andros Adamson  *
2365002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
2375002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
2385002c586SWeston Andros Adamson  */
2395002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
2405002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
2415002c586SWeston Andros Adamson {
2425002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2435002c586SWeston Andros Adamson 
2445002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
2455002c586SWeston Andros Adamson 					 data->ds_commit_index);
24680c76fe3SWeston Andros Adamson 
24780c76fe3SWeston Andros Adamson 	/* verifier not set so always fail */
248221203ceSTrond Myklebust 	if (verfp->committed < 0 || data->res.verf->committed <= NFS_UNSTABLE)
24980c76fe3SWeston Andros Adamson 		return 1;
25080c76fe3SWeston Andros Adamson 
251221203ceSTrond Myklebust 	return nfs_direct_cmp_verf(verfp, data->res.verf);
2525002c586SWeston Andros Adamson }
2535002c586SWeston Andros Adamson 
2541da177e4SLinus Torvalds /**
255b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
256b8a32e2bSChuck Lever  * @iocb: target I/O control block
25790090ae6SAl Viro  * @iter: I/O buffer
258b8a32e2bSChuck Lever  *
259b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
260a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
261a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
262a564b8f0SMel Gorman  * so this method is only ever called for swap.
2631da177e4SLinus Torvalds  */
264c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
265b8a32e2bSChuck Lever {
266ee8a1a8bSPeng Tao 	struct inode *inode = iocb->ki_filp->f_mapping->host;
267ee8a1a8bSPeng Tao 
268ee8a1a8bSPeng Tao 	/* we only support swap file calling nfs_direct_IO */
269ee8a1a8bSPeng Tao 	if (!IS_SWAPFILE(inode))
270ee8a1a8bSPeng Tao 		return 0;
271ee8a1a8bSPeng Tao 
27266ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
273a564b8f0SMel Gorman 
2746f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
275c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, iter);
27665a4a1caSAl Viro 	return nfs_file_direct_write(iocb, iter);
277b8a32e2bSChuck Lever }
278b8a32e2bSChuck Lever 
279749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2809c93ab7dSChuck Lever {
281749e146eSChuck Lever 	unsigned int i;
282607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
28309cbfeafSKirill A. Shutemov 		put_page(pages[i]);
2846b45d858STrond Myklebust }
2856b45d858STrond Myklebust 
2861763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
2871763da12SFred Isaman 			      struct nfs_direct_req *dreq)
2881763da12SFred Isaman {
289fe238e60SDave Wysochanski 	cinfo->inode = dreq->inode;
2901763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
2911763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
2921763da12SFred Isaman 	cinfo->dreq = dreq;
2931763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
2941763da12SFred Isaman }
2951763da12SFred Isaman 
29693619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2991da177e4SLinus Torvalds 
300292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
3011da177e4SLinus Torvalds 	if (!dreq)
3021da177e4SLinus Torvalds 		return NULL;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	kref_init(&dreq->kref);
305607f31e8STrond Myklebust 	kref_get(&dreq->kref);
306d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
3071763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
308*c21e7168STrond Myklebust 	pnfs_init_ds_commit_info(&dreq->ds_cinfo);
3095002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
3101763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
31115ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
31293619e59SChuck Lever 
31393619e59SChuck Lever 	return dreq;
31493619e59SChuck Lever }
31593619e59SChuck Lever 
316b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
319a8881f5aSTrond Myklebust 
3208c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
321f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
322f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
323a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
324a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
3251da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
329b4946ffbSTrond Myklebust {
330b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
331b4946ffbSTrond Myklebust }
332b4946ffbSTrond Myklebust 
3336296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
3346296556fSPeng Tao {
3356296556fSPeng Tao 	return dreq->bytes_left;
3366296556fSPeng Tao }
3376296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
3386296556fSPeng Tao 
339d4cc948bSChuck Lever /*
340bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
341bc0fb201SChuck Lever  */
342bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
343bc0fb201SChuck Lever {
34415ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
345bc0fb201SChuck Lever 
346bc0fb201SChuck Lever 	/* Async requests don't wait here */
347bc0fb201SChuck Lever 	if (dreq->iocb)
348bc0fb201SChuck Lever 		goto out;
349bc0fb201SChuck Lever 
350150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
351bc0fb201SChuck Lever 
352d2a7de0bSTrond Myklebust 	if (!result) {
353d2a7de0bSTrond Myklebust 		result = dreq->count;
354d2a7de0bSTrond Myklebust 		WARN_ON_ONCE(dreq->count < 0);
355d2a7de0bSTrond Myklebust 	}
356bc0fb201SChuck Lever 	if (!result)
35715ce4a0cSChuck Lever 		result = dreq->error;
358bc0fb201SChuck Lever 
359bc0fb201SChuck Lever out:
360bc0fb201SChuck Lever 	return (ssize_t) result;
361bc0fb201SChuck Lever }
362bc0fb201SChuck Lever 
363bc0fb201SChuck Lever /*
364607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
365607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
36663ab46abSChuck Lever  */
367f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq)
36863ab46abSChuck Lever {
3699811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3709811cd57SChristoph Hellwig 
371fe0f07d0SJens Axboe 	inode_dio_end(inode);
3722a009ec9SChristoph Hellwig 
3732a009ec9SChristoph Hellwig 	if (dreq->iocb) {
3742a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
375d2a7de0bSTrond Myklebust 		if (dreq->count != 0) {
3762a009ec9SChristoph Hellwig 			res = (long) dreq->count;
377d2a7de0bSTrond Myklebust 			WARN_ON_ONCE(dreq->count < 0);
378d2a7de0bSTrond Myklebust 		}
37904b2fa9fSChristoph Hellwig 		dreq->iocb->ki_complete(dreq->iocb, res, 0);
380d72b7a6bSTrond Myklebust 	}
3812a009ec9SChristoph Hellwig 
382024de8f1SDaniel Wagner 	complete(&dreq->completion);
38363ab46abSChuck Lever 
384b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
38563ab46abSChuck Lever }
38663ab46abSChuck Lever 
387584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
388fdd1e74cSTrond Myklebust {
389584aa810SFred Isaman 	unsigned long bytes = 0;
390584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
391fdd1e74cSTrond Myklebust 
39215ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
393eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
394eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
395eb2c50daSTrond Myklebust 		goto out_put;
396eb2c50daSTrond Myklebust 	}
397eb2c50daSTrond Myklebust 
398031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
39915ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
4001da177e4SLinus Torvalds 
401584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
402584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
403584aa810SFred Isaman 		struct page *page = req->wb_page;
404584aa810SFred Isaman 
405ad3cba22SDave Kleikamp 		if (!PageCompound(page) && bytes < hdr->good_bytes &&
406ad3cba22SDave Kleikamp 		    (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
4074bd8b010STrond Myklebust 			set_page_dirty(page);
408584aa810SFred Isaman 		bytes += req->wb_bytes;
409584aa810SFred Isaman 		nfs_list_remove_request(req);
410beeb5338SAnna Schumaker 		nfs_release_request(req);
411584aa810SFred Isaman 	}
412584aa810SFred Isaman out_put:
413607f31e8STrond Myklebust 	if (put_dreq(dreq))
414f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
415584aa810SFred Isaman 	hdr->release(hdr);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error)
419cd841605SFred Isaman {
420584aa810SFred Isaman 	struct nfs_page *req;
421cd841605SFred Isaman 
422584aa810SFred Isaman 	while (!list_empty(head)) {
423584aa810SFred Isaman 		req = nfs_list_entry(head->next);
424584aa810SFred Isaman 		nfs_list_remove_request(req);
425584aa810SFred Isaman 		nfs_release_request(req);
426cd841605SFred Isaman 	}
427584aa810SFred Isaman }
428584aa810SFred Isaman 
429584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
430584aa810SFred Isaman {
431584aa810SFred Isaman 	get_dreq(hdr->dreq);
432584aa810SFred Isaman }
433584aa810SFred Isaman 
434584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4353e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
436584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
437584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
438584aa810SFred Isaman };
439cd841605SFred Isaman 
440d4cc948bSChuck Lever /*
441607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
442607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
443607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
444607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
445607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4461da177e4SLinus Torvalds  */
44791f79c43SAl Viro 
44891f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
44991f79c43SAl Viro 					      struct iov_iter *iter,
45091f79c43SAl Viro 					      loff_t pos)
4511da177e4SLinus Torvalds {
45291f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
45391f79c43SAl Viro 	struct inode *inode = dreq->inode;
45491f79c43SAl Viro 	ssize_t result = -EINVAL;
45591f79c43SAl Viro 	size_t requested_bytes = 0;
45691f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
45782b145c5SChuck Lever 
45816b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
45991f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
46091f79c43SAl Viro 	get_dreq(dreq);
46191f79c43SAl Viro 	desc.pg_dreq = dreq;
462fe0f07d0SJens Axboe 	inode_dio_begin(inode);
46391f79c43SAl Viro 
46491f79c43SAl Viro 	while (iov_iter_count(iter)) {
46591f79c43SAl Viro 		struct page **pagevec;
4665dd602f2SChuck Lever 		size_t bytes;
46791f79c43SAl Viro 		size_t pgbase;
46891f79c43SAl Viro 		unsigned npages, i;
4691da177e4SLinus Torvalds 
47091f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
47191f79c43SAl Viro 						  rsize, &pgbase);
472584aa810SFred Isaman 		if (result < 0)
473749e146eSChuck Lever 			break;
474a564b8f0SMel Gorman 
47591f79c43SAl Viro 		bytes = result;
47691f79c43SAl Viro 		iov_iter_advance(iter, bytes);
47791f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
478584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
479584aa810SFred Isaman 			struct nfs_page *req;
480bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
481584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
48228b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
483584aa810SFred Isaman 						 pgbase, req_len);
484584aa810SFred Isaman 			if (IS_ERR(req)) {
485584aa810SFred Isaman 				result = PTR_ERR(req);
486dbae4c73STrond Myklebust 				break;
487584aa810SFred Isaman 			}
488584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
489584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
49091f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
49191f79c43SAl Viro 				result = desc.pg_error;
492584aa810SFred Isaman 				nfs_release_request(req);
493584aa810SFred Isaman 				break;
494584aa810SFred Isaman 			}
495584aa810SFred Isaman 			pgbase = 0;
496584aa810SFred Isaman 			bytes -= req_len;
49791f79c43SAl Viro 			requested_bytes += req_len;
498584aa810SFred Isaman 			pos += req_len;
49935754bc0SPeng Tao 			dreq->bytes_left -= req_len;
500584aa810SFred Isaman 		}
5016d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
50291f79c43SAl Viro 		kvfree(pagevec);
50319f73787SChuck Lever 		if (result < 0)
50419f73787SChuck Lever 			break;
50519f73787SChuck Lever 	}
50619f73787SChuck Lever 
507584aa810SFred Isaman 	nfs_pageio_complete(&desc);
508584aa810SFred Isaman 
509839f7ad6SChuck Lever 	/*
510839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
511839f7ad6SChuck Lever 	 * generic layer handle the completion.
512839f7ad6SChuck Lever 	 */
513839f7ad6SChuck Lever 	if (requested_bytes == 0) {
514fe0f07d0SJens Axboe 		inode_dio_end(inode);
515839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
516839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
517839f7ad6SChuck Lever 	}
518839f7ad6SChuck Lever 
51919f73787SChuck Lever 	if (put_dreq(dreq))
520f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
52185128b2bSAl Viro 	return requested_bytes;
52219f73787SChuck Lever }
52319f73787SChuck Lever 
52414a3ec79SChristoph Hellwig /**
52514a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
52614a3ec79SChristoph Hellwig  * @iocb: target I/O control block
527619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
52814a3ec79SChristoph Hellwig  *
52914a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
53014a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
53114a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
53214a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
53314a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
53414a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
53514a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
53614a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
53714a3ec79SChristoph Hellwig  *
53814a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
53914a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
54014a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
54114a3ec79SChristoph Hellwig  * cache.
54214a3ec79SChristoph Hellwig  */
543c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
5441da177e4SLinus Torvalds {
54514a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
54614a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
54714a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5481da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
549b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
55085128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
551a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
55214a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
55314a3ec79SChristoph Hellwig 
55414a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
555c8b8e32dSChristoph Hellwig 		file, count, (long long) iocb->ki_pos);
55614a3ec79SChristoph Hellwig 
55714a3ec79SChristoph Hellwig 	result = 0;
55814a3ec79SChristoph Hellwig 	if (!count)
55914a3ec79SChristoph Hellwig 		goto out;
56014a3ec79SChristoph Hellwig 
56114a3ec79SChristoph Hellwig 	task_io_account_read(count);
56214a3ec79SChristoph Hellwig 
56314a3ec79SChristoph Hellwig 	result = -ENOMEM;
564607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
565f11ac8dbSTrond Myklebust 	if (dreq == NULL)
566a5864c99STrond Myklebust 		goto out;
5671da177e4SLinus Torvalds 
56891d5b470SChuck Lever 	dreq->inode = inode;
569ed3743a6SWeston Andros Adamson 	dreq->bytes_left = dreq->max_count = count;
570c8b8e32dSChristoph Hellwig 	dreq->io_start = iocb->ki_pos;
571cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
572b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
573b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
574b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
5758605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
576f11ac8dbSTrond Myklebust 		goto out_release;
577b3c54de6STrond Myklebust 	}
578b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
579487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
580487b8372SChuck Lever 		dreq->iocb = iocb;
5811da177e4SLinus Torvalds 
582ad3cba22SDave Kleikamp 	if (iter_is_iovec(iter))
583ad3cba22SDave Kleikamp 		dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
584ad3cba22SDave Kleikamp 
585a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
586a5864c99STrond Myklebust 
587619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
58885128b2bSAl Viro 	requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
589d0b9875dSChristoph Hellwig 
590a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
591d0b9875dSChristoph Hellwig 
59285128b2bSAl Viro 	if (requested > 0) {
593bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
59485128b2bSAl Viro 		if (result > 0) {
59585128b2bSAl Viro 			requested -= result;
596c8b8e32dSChristoph Hellwig 			iocb->ki_pos += result;
59714a3ec79SChristoph Hellwig 		}
59885128b2bSAl Viro 		iov_iter_revert(iter, requested);
59985128b2bSAl Viro 	} else {
60085128b2bSAl Viro 		result = requested;
60185128b2bSAl Viro 	}
602d0b9875dSChristoph Hellwig 
603f11ac8dbSTrond Myklebust out_release:
604b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
605f11ac8dbSTrond Myklebust out:
6061da177e4SLinus Torvalds 	return result;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
609085d1e33STom Haynes static void
610085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
611085d1e33STom Haynes 				  struct list_head *list,
612085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
613085d1e33STom Haynes {
614e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
615085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
616085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
617085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
618085d1e33STom Haynes #endif
619085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
620e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
621085d1e33STom Haynes }
622085d1e33STom Haynes 
623fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
6241da177e4SLinus Torvalds {
6251763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
6261763da12SFred Isaman 	struct nfs_page *req, *tmp;
6271763da12SFred Isaman 	LIST_HEAD(reqs);
6281763da12SFred Isaman 	struct nfs_commit_info cinfo;
6291763da12SFred Isaman 	LIST_HEAD(failed);
6301763da12SFred Isaman 
6311763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
632085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
6331da177e4SLinus Torvalds 
634fad61490STrond Myklebust 	dreq->count = 0;
635031d73edSTrond Myklebust 	dreq->max_count = 0;
636031d73edSTrond Myklebust 	list_for_each_entry(req, &reqs, wb_list)
637031d73edSTrond Myklebust 		dreq->max_count += req->wb_bytes;
638a5314a74STrond Myklebust 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;
639a5314a74STrond Myklebust 	nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
640607f31e8STrond Myklebust 	get_dreq(dreq);
6411da177e4SLinus Torvalds 
642a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6431763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6441763da12SFred Isaman 	desc.pg_dreq = dreq;
645607f31e8STrond Myklebust 
6461763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
64733344e0fSTrond Myklebust 		/* Bump the transmission count */
64833344e0fSTrond Myklebust 		req->wb_nio++;
6491763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
650078b5fd9STrond Myklebust 			nfs_list_move_request(req, &failed);
651fe238e60SDave Wysochanski 			spin_lock(&cinfo.inode->i_lock);
6521763da12SFred Isaman 			dreq->flags = 0;
653d600ad1fSPeng Tao 			if (desc.pg_error < 0)
654d600ad1fSPeng Tao 				dreq->error = desc.pg_error;
655d600ad1fSPeng Tao 			else
6561763da12SFred Isaman 				dreq->error = -EIO;
657fe238e60SDave Wysochanski 			spin_unlock(&cinfo.inode->i_lock);
6581763da12SFred Isaman 		}
6595a695da2STrond Myklebust 		nfs_release_request(req);
6601763da12SFred Isaman 	}
6611763da12SFred Isaman 	nfs_pageio_complete(&desc);
662607f31e8STrond Myklebust 
6634035c248STrond Myklebust 	while (!list_empty(&failed)) {
6644035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
6654035c248STrond Myklebust 		nfs_list_remove_request(req);
6661d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
6674035c248STrond Myklebust 	}
668607f31e8STrond Myklebust 
669607f31e8STrond Myklebust 	if (put_dreq(dreq))
6704d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
671fad61490STrond Myklebust }
6721da177e4SLinus Torvalds 
6731763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
674fad61490STrond Myklebust {
6750b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
6761763da12SFred Isaman 	struct nfs_commit_info cinfo;
6771763da12SFred Isaman 	struct nfs_page *req;
678c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
679c9d8f89dSTrond Myklebust 
6801763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
681fe4f844dSAnna Schumaker 	if (status < 0 || nfs_direct_cmp_commit_data_verf(dreq, data))
682fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
683fad61490STrond Myklebust 
6841763da12SFred Isaman 	while (!list_empty(&data->pages)) {
6851763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
6861763da12SFred Isaman 		nfs_list_remove_request(req);
6871763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
68833344e0fSTrond Myklebust 			/*
68933344e0fSTrond Myklebust 			 * Despite the reboot, the write was successful,
69033344e0fSTrond Myklebust 			 * so reset wb_nio.
69133344e0fSTrond Myklebust 			 */
69233344e0fSTrond Myklebust 			req->wb_nio = 0;
6931763da12SFred Isaman 			/* Note the rewrite will go through mds */
694b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
695906369e4SFred Isaman 		} else
696906369e4SFred Isaman 			nfs_release_request(req);
6971d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
698fad61490STrond Myklebust 	}
699fad61490STrond Myklebust 
7001763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
7014d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
7021763da12SFred Isaman }
7031763da12SFred Isaman 
704b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
705b20135d0STrond Myklebust 		struct nfs_page *req)
7061763da12SFred Isaman {
707b20135d0STrond Myklebust 	struct nfs_direct_req *dreq = cinfo->dreq;
708b20135d0STrond Myklebust 
709b20135d0STrond Myklebust 	spin_lock(&dreq->lock);
710b20135d0STrond Myklebust 	dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
711b20135d0STrond Myklebust 	spin_unlock(&dreq->lock);
712b20135d0STrond Myklebust 	nfs_mark_request_commit(req, NULL, cinfo, 0);
7131763da12SFred Isaman }
7141763da12SFred Isaman 
7151763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
7161763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
717b20135d0STrond Myklebust 	.resched_write = nfs_direct_resched_write,
718fad61490STrond Myklebust };
719fad61490STrond Myklebust 
720fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
721fad61490STrond Myklebust {
7221763da12SFred Isaman 	int res;
7231763da12SFred Isaman 	struct nfs_commit_info cinfo;
7241763da12SFred Isaman 	LIST_HEAD(mds_list);
725fad61490STrond Myklebust 
7261763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7271763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
7281763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
7291763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
7301763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
7341da177e4SLinus Torvalds {
7351763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
736fad61490STrond Myklebust 	int flags = dreq->flags;
7371da177e4SLinus Torvalds 
738fad61490STrond Myklebust 	dreq->flags = 0;
739fad61490STrond Myklebust 	switch (flags) {
740fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
741fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
7421da177e4SLinus Torvalds 			break;
743fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
744fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
7451da177e4SLinus Torvalds 			break;
7461da177e4SLinus Torvalds 		default:
747f7b5c340STrond Myklebust 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
748f7b5c340STrond Myklebust 			nfs_direct_complete(dreq);
7491da177e4SLinus Torvalds 	}
750fad61490STrond Myklebust }
751fad61490STrond Myklebust 
7524d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
753fad61490STrond Myklebust {
75446483c2eSNeilBrown 	queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
755fad61490STrond Myklebust }
7561763da12SFred Isaman 
7571763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7581763da12SFred Isaman {
7591763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7601763da12SFred Isaman 	struct nfs_commit_info cinfo;
761c65e6254SWeston Andros Adamson 	bool request_commit = false;
7621763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7631763da12SFred Isaman 
7641763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7651763da12SFred Isaman 
7661763da12SFred Isaman 	spin_lock(&dreq->lock);
767eb2c50daSTrond Myklebust 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
768eb2c50daSTrond Myklebust 		spin_unlock(&dreq->lock);
769eb2c50daSTrond Myklebust 		goto out_put;
770eb2c50daSTrond Myklebust 	}
771eb2c50daSTrond Myklebust 
772031d73edSTrond Myklebust 	nfs_direct_count_bytes(dreq, hdr);
773eb2c50daSTrond Myklebust 	if (hdr->good_bytes != 0) {
774c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
7751763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
776c65e6254SWeston Andros Adamson 				request_commit = true;
7771763da12SFred Isaman 			else if (dreq->flags == 0) {
7785002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
779c65e6254SWeston Andros Adamson 				request_commit = true;
7801763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7811763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
782c65e6254SWeston Andros Adamson 				request_commit = true;
783c65e6254SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
7845002c586SWeston Andros Adamson 					dreq->flags =
7855002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
7861763da12SFred Isaman 			}
7871763da12SFred Isaman 		}
7881763da12SFred Isaman 	}
7891763da12SFred Isaman 	spin_unlock(&dreq->lock);
7901763da12SFred Isaman 
7911763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7922bfc6e56SWeston Andros Adamson 
7931763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7941763da12SFred Isaman 		nfs_list_remove_request(req);
795c65e6254SWeston Andros Adamson 		if (request_commit) {
79604277086STrond Myklebust 			kref_get(&req->wb_kref);
797b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
798b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
7991763da12SFred Isaman 		}
8001d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8011763da12SFred Isaman 	}
8021763da12SFred Isaman 
8031763da12SFred Isaman out_put:
8041763da12SFred Isaman 	if (put_dreq(dreq))
8054d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
8061763da12SFred Isaman 	hdr->release(hdr);
8071763da12SFred Isaman }
8081763da12SFred Isaman 
809df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error)
8103e9e0ca3STrond Myklebust {
8113e9e0ca3STrond Myklebust 	struct nfs_page *req;
8123e9e0ca3STrond Myklebust 
8133e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
8143e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
8153e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
8161d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8173e9e0ca3STrond Myklebust 	}
8183e9e0ca3STrond Myklebust }
8193e9e0ca3STrond Myklebust 
820dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
821dc602dd7STrond Myklebust {
822dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
823dc602dd7STrond Myklebust 
824dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
825dc602dd7STrond Myklebust 	if (dreq->error == 0) {
826dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
827dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
828dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
8294daaeba9STrond Myklebust 		hdr->good_bytes = hdr->args.offset + hdr->args.count -
8304daaeba9STrond Myklebust 			hdr->io_start;
831dc602dd7STrond Myklebust 	}
832dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
833dc602dd7STrond Myklebust }
834dc602dd7STrond Myklebust 
8351763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
8363e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
8371763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
8381763da12SFred Isaman 	.completion = nfs_direct_write_completion,
839dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
8401763da12SFred Isaman };
8411763da12SFred Isaman 
84291f79c43SAl Viro 
84391f79c43SAl Viro /*
84491f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
84591f79c43SAl Viro  *     errors after the first one are ignored.
84691f79c43SAl Viro  */
84791f79c43SAl Viro /*
84891f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
84991f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
85091f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
85191f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
85291f79c43SAl Viro  * no requests have been sent, just return an error.
85391f79c43SAl Viro  */
85419f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
855619d30b4SAl Viro 					       struct iov_iter *iter,
85691f79c43SAl Viro 					       loff_t pos)
85719f73787SChuck Lever {
8581763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8591d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
86019f73787SChuck Lever 	ssize_t result = 0;
86119f73787SChuck Lever 	size_t requested_bytes = 0;
86291f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
86319f73787SChuck Lever 
864a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
8651763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8661763da12SFred Isaman 	desc.pg_dreq = dreq;
86719f73787SChuck Lever 	get_dreq(dreq);
868fe0f07d0SJens Axboe 	inode_dio_begin(inode);
86919f73787SChuck Lever 
87091f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
87191f79c43SAl Viro 	while (iov_iter_count(iter)) {
87291f79c43SAl Viro 		struct page **pagevec;
87391f79c43SAl Viro 		size_t bytes;
87491f79c43SAl Viro 		size_t pgbase;
87591f79c43SAl Viro 		unsigned npages, i;
87691f79c43SAl Viro 
87791f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
87891f79c43SAl Viro 						  wsize, &pgbase);
87919f73787SChuck Lever 		if (result < 0)
88019f73787SChuck Lever 			break;
88191f79c43SAl Viro 
88291f79c43SAl Viro 		bytes = result;
88391f79c43SAl Viro 		iov_iter_advance(iter, bytes);
88491f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
88591f79c43SAl Viro 		for (i = 0; i < npages; i++) {
88691f79c43SAl Viro 			struct nfs_page *req;
88791f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
88891f79c43SAl Viro 
88928b1d3f5STrond Myklebust 			req = nfs_create_request(dreq->ctx, pagevec[i],
89091f79c43SAl Viro 						 pgbase, req_len);
89191f79c43SAl Viro 			if (IS_ERR(req)) {
89291f79c43SAl Viro 				result = PTR_ERR(req);
89319f73787SChuck Lever 				break;
89491f79c43SAl Viro 			}
8950a00b77bSWeston Andros Adamson 
896d600ad1fSPeng Tao 			if (desc.pg_error < 0) {
897d600ad1fSPeng Tao 				nfs_free_request(req);
898d600ad1fSPeng Tao 				result = desc.pg_error;
899d600ad1fSPeng Tao 				break;
900d600ad1fSPeng Tao 			}
9010a00b77bSWeston Andros Adamson 
90291f79c43SAl Viro 			nfs_lock_request(req);
90391f79c43SAl Viro 			req->wb_index = pos >> PAGE_SHIFT;
90491f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
90591f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
90691f79c43SAl Viro 				result = desc.pg_error;
90791f79c43SAl Viro 				nfs_unlock_and_release_request(req);
90891f79c43SAl Viro 				break;
90991f79c43SAl Viro 			}
91091f79c43SAl Viro 			pgbase = 0;
91191f79c43SAl Viro 			bytes -= req_len;
91291f79c43SAl Viro 			requested_bytes += req_len;
91391f79c43SAl Viro 			pos += req_len;
91491f79c43SAl Viro 			dreq->bytes_left -= req_len;
91591f79c43SAl Viro 		}
91691f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
91791f79c43SAl Viro 		kvfree(pagevec);
91891f79c43SAl Viro 		if (result < 0)
91991f79c43SAl Viro 			break;
92019f73787SChuck Lever 	}
9211763da12SFred Isaman 	nfs_pageio_complete(&desc);
92219f73787SChuck Lever 
923839f7ad6SChuck Lever 	/*
924839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
925839f7ad6SChuck Lever 	 * generic layer handle the completion.
926839f7ad6SChuck Lever 	 */
927839f7ad6SChuck Lever 	if (requested_bytes == 0) {
928fe0f07d0SJens Axboe 		inode_dio_end(inode);
929839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
930839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
931839f7ad6SChuck Lever 	}
932839f7ad6SChuck Lever 
93319f73787SChuck Lever 	if (put_dreq(dreq))
9344d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
93585128b2bSAl Viro 	return requested_bytes;
93619f73787SChuck Lever }
93719f73787SChuck Lever 
9381da177e4SLinus Torvalds /**
9391da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9401da177e4SLinus Torvalds  * @iocb: target I/O control block
941619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
9421da177e4SLinus Torvalds  *
9431da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9441da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9451da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9461da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9471da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9481da177e4SLinus Torvalds  * parameter checking and report problems.
9491da177e4SLinus Torvalds  *
9501da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9511da177e4SLinus Torvalds  *
9521da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9531da177e4SLinus Torvalds  * readers of this file.
9541da177e4SLinus Torvalds  *
9551da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9561da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9571da177e4SLinus Torvalds  */
95865a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
9591da177e4SLinus Torvalds {
96085128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
96189698b24STrond Myklebust 	size_t count;
9621da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9631da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
96422cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
96522cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
96622cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
96765a4a1caSAl Viro 	loff_t pos, end;
968c216fd70SChuck Lever 
9696de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
9703309dd04SAl Viro 		file, iov_iter_count(iter), (long long) iocb->ki_pos);
971027445c3SBadari Pulavarty 
97289698b24STrond Myklebust 	result = generic_write_checks(iocb, iter);
97389698b24STrond Myklebust 	if (result <= 0)
97489698b24STrond Myklebust 		return result;
97589698b24STrond Myklebust 	count = result;
97689698b24STrond Myklebust 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
9773309dd04SAl Viro 
9783309dd04SAl Viro 	pos = iocb->ki_pos;
97909cbfeafSKirill A. Shutemov 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
980ce1a8e67SChuck Lever 
98189698b24STrond Myklebust 	task_io_account_write(count);
9827ec10f26SKonstantin Khlebnikov 
98322cd1bf1SChristoph Hellwig 	result = -ENOMEM;
98422cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
98522cd1bf1SChristoph Hellwig 	if (!dreq)
986a5864c99STrond Myklebust 		goto out;
98722cd1bf1SChristoph Hellwig 
98822cd1bf1SChristoph Hellwig 	dreq->inode = inode;
98989698b24STrond Myklebust 	dreq->bytes_left = dreq->max_count = count;
9905fadeb47SPeng Tao 	dreq->io_start = pos;
99122cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
99222cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
99322cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
99422cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
9958605cf0eSMisono Tomohiro 		nfs_direct_req_release(dreq);
99622cd1bf1SChristoph Hellwig 		goto out_release;
99722cd1bf1SChristoph Hellwig 	}
99822cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
99922cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
100022cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
100122cd1bf1SChristoph Hellwig 
1002a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
1003a5864c99STrond Myklebust 
100485128b2bSAl Viro 	requested = nfs_direct_write_schedule_iovec(dreq, iter, pos);
1005a9ab5e84SChristoph Hellwig 
1006a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1007a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
100809cbfeafSKirill A. Shutemov 					      pos >> PAGE_SHIFT, end);
1009a9ab5e84SChristoph Hellwig 	}
1010a9ab5e84SChristoph Hellwig 
1011a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
1012a9ab5e84SChristoph Hellwig 
101385128b2bSAl Viro 	if (requested > 0) {
101422cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
101522cd1bf1SChristoph Hellwig 		if (result > 0) {
101685128b2bSAl Viro 			requested -= result;
101722cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
1018e2592217SChristoph Hellwig 			/* XXX: should check the generic_write_sync retval */
1019e2592217SChristoph Hellwig 			generic_write_sync(iocb, result);
10201763da12SFred Isaman 		}
102185128b2bSAl Viro 		iov_iter_revert(iter, requested);
102285128b2bSAl Viro 	} else {
102385128b2bSAl Viro 		result = requested;
102422cd1bf1SChristoph Hellwig 	}
102522cd1bf1SChristoph Hellwig out_release:
102622cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
1027a5864c99STrond Myklebust out:
102822cd1bf1SChristoph Hellwig 	return result;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
103188467055SChuck Lever /**
103288467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
103388467055SChuck Lever  *
103488467055SChuck Lever  */
1035f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10381da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1039fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1040fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
104120c2df83SPaul Mundt 						NULL);
10421da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10431da177e4SLinus Torvalds 		return -ENOMEM;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	return 0;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
104888467055SChuck Lever /**
1049f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
105088467055SChuck Lever  *
105188467055SChuck Lever  */
1052266bee88SDavid Brownell void nfs_destroy_directcache(void)
10531da177e4SLinus Torvalds {
10541a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10551da177e4SLinus Torvalds }
1056